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.44k stars 9.29k forks source link

Slow configurable product attribute option loading #38344

Open abdel-aouby opened 7 months ago

abdel-aouby commented 7 months ago

Summary

When adding configurable products to the cart. it is loading all attribute options regardless if they are product attributes or not. then it is filtering them in a cycle. this is a super slow DB query if you have many options in every attribute. the query that is used is

SELECT `main_table`.*, `tdv`.`value` AS `default_value`, `tsv`.`value` AS `store_default_value`, IF(tsv.value_id > ?, tsv.value, tdv.value) AS `value` FROM `eav_attribute_option` AS `main_table`
 INNER JOIN `eav_attribute_option_value` AS `tdv` ON tdv.option_id = main_table.option_id
 LEFT JOIN `eav_attribute_option_value` AS `tsv` ON tsv.option_id = main_table.option_id AND tsv.store_id = ?
WHERE (`main_table`.`attribute_id` = ?) AND (tdv.store_id = ?) ORDER BY main_table.sort_order ASC, value ASC

this is coming from the method loadOption which is called in the collection method _afterLoad \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection::_afterLoad

down the investigation, I noticed that this was the case (loading only specific options) until 2015 when it was changed here

https://github.com/magento/magento2/commit/e3cecafccd999ee3b9ab5df0094dec02c12fad8c#diff-c249eb28ea2a6ea127a89163935e65daea074ebe64b9cd895fa791168b5e7e03R256-R283

any reason for this change?

This is called at least 5 times when using AddProductToCart graphQl request on a configurable product. resulting in a significant performance issue when the configurable product has attributes with thousands of options

Examples

Down the stack trace of the method loadOptions() that is called in _afterLoad() we can see.

  1. \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection::_afterLoad
  2. \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection::loadOptions
  3. \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable::getAttributeOptions
  4. \Magento\ConfigurableProduct\Model\AttributeOptionProvider::getAttributeOptions

In the method getAttributeOptions(AbstractAttribute $superAttribute, $productId) there is a call for getAllOptions(false); which is loading all attribute options, then down is a foreach loop foreach ($data as $key => $value) and inside we are looping the $data and taking/selecting only the options that match $value['value_index'] ignoring the rest of options that are all loaded. because they are not needed at first

my solution would be to initially not call getAllOptions(false); and use values of the index $value['value_index'] to load only necessary options. using $superAttribute->getSource()->getSpecificOptions(array_unique($attributeValues));

$attributeValues will be the values of value_index from the array $data. which could be retrieved using a combination of array_search array_column or a classic loop on $data

Proposed solution

-  $options = $superAttribute->getSource()->getAllOptions(false);
+ $optionIds = [];
+ foreach ($data as $key => $value) {
+       $optionIds[] = $value['value_index'];
+ }

+ $options = $superAttribute->getSource()->getSpecificOptions(array_unique($optionIds), false);

Release note

No response

Triage and priority

m2-assistant[bot] commented 7 months ago

Hi @abdel-aouby. 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:

andrewbess commented 7 months ago

Hello @abdel-aouby Thank you for your contribution I think this is great idea to discuss it with community

m2-assistant[bot] commented 7 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 7 months ago

Hello @abdel-aouby,

Thank you for the report and collaboration!

Verified this issue on 2.4-develop. When adding a configurable product to cart, all the attributes are being loaded with the below query, which are not required.

## 77937 ## QUERY
SQL: SELECT `main_table`.*, `tdv`.`value` AS `default_value`, `tsv`.`value` AS `store_default_value`, IF(tsv.value_id > 0, tsv.value, tdv.value) AS `value` FROM `eav_attribute_option` AS `main_table`
 INNER JOIN `eav_attribute_option_value` AS `tdv` ON tdv.option_id = main_table.option_id
 LEFT JOIN `eav_attribute_option_value` AS `tsv` ON tsv.option_id = main_table.option_id AND tsv.store_id = '1' WHERE (`main_table`.`attribute_id` = '83') AND (tdv.store_id = 0) ORDER BY main_table.sort_order ASC, `value` ASC

Hence issue can be confirmed.

Thank you.

github-jira-sync-bot commented 7 months ago

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

m2-assistant[bot] commented 7 months 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.

martin-cod commented 1 month ago

my solution would be to initially not call getAllOptions(false); and use values of the index $value['value_index'] to load only necessary options. using $superAttribute->getSource()->getSpecificOptions(array_unique($attributeValues));

I want to argue regarding the proposed solution because this approach has another performance degradation issue #38934 related to not optimized getSpecificOptions

Also, please keep in mind that getSpecificOptions is a unique method for only class \Magento\Eav\Model\Entity\Attribute\Source\Table and this is not a public contract method of \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource class or \Magento\Eav\Model\Entity\Attribute\Source\SourceInterface interface