magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.55k stars 9.32k forks source link

Product Collection - addMediaGalleryData calls getSize when the collection maybe or will be loaded (Can use count to avoid an extra DB query) #39111

Open steven-hoffman-jomashop opened 2 months ago

steven-hoffman-jomashop commented 2 months ago

Preconditions and environment

The call to getSize is clearly un-needed for the GQL code path for products.

It should be safe to add a if($this->isLoaded()) prior to calling getSize, and if loaded, call count instead of getSize. (It should also be safe to just use the count/empty of getItems, (As getItems calls load), but calling isLoaded and using count should be safe and would mitigate this specific issue).

Steps to reproduce

  1. Enable dev query log
  2. This should be reproducible by calling the products GQL call with the media_gallery/media_gallery_entries field selected.
  3. The count call should be in the query log, traced to addMediaGalleryData

Expected result

getSize is not called on a loaded product collection. (When the non-paginated count is not needed).

Actual result

getSize is called on a loaded paginated collection to check if it has data.

Additional information

No response

Release note

No response

Triage and priority

m2-assistant[bot] commented 2 months ago

Hi @steven-hoffman-jomashop. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

m2-assistant[bot] commented 2 months ago

Hi @engcom-November. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-November commented 2 months ago

Hello @steven-hoffman-jomashop,

Thank you for the report and collaboration!

I agree that if the collection is already loaded we should call count, but addMediaGalleryData function has been called in other classes which does not specify if the collection is loaded or not, can you make sure that this will not cause problems in these classes.

steven-hoffman-jomashop commented 1 month ago

Hi @engcom-November,

... addMediaGalleryData function has been called in other classes which does not specify if the collection is loaded or not, ... this will not cause problems ...

Please see above.

It should be safe to add a if($this->isLoaded()) prior to calling getSize, and if loaded, call count instead of getSize. ...

In summary: Those other uses, where the category is not yet loaded, will return false for isLoaded, and thus addMediaGalleryData can call getSize for that code path; (and if isLoadedtrue, it can call count). This avoid the issue of the other code paths and speeds up the code paths where the collection is loaded.

engcom-November commented 1 month ago

Hello @steven-hoffman-jomashop,

I agree with this, since there is a if clause it won't effect other implementation, hence confirming the issue.

Thank you.

github-jira-sync-bot commented 1 month ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-13055 is successfully created for this GitHub issue.

m2-assistant[bot] commented 1 month ago

:white_check_mark: Confirmed by @engcom-November. Thank you for verifying the issue.
Issue Available: @engcom-November, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

github-jira-sync-bot commented 1 month ago

:x: You don't have permission to export this issue.