magento2
magento2 copied to clipboard
Slow configurable product attribute option loading
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.
-
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection::_afterLoad
-
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection::loadOptions
-
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable::getAttributeOptions
-
\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
- [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- [X] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- [ ] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
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:
-
@magento give me 2.4-develop instance
- upcoming 2.4.x release - For more details, review the Magento Contributor Assistant documentation.
- Add a comment to assign the issue:
@magento I am working on this
- To learn more about issue processing workflow, refer to the Code Contributions.
Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.
Hello @abdel-aouby Thank you for your contribution I think this is great idea to discuss it with community
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:
- [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
- [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
- [ ] 3. Add
Area: XXXXX
label to the ticket, indicating the functional areas it may be related to. - [ ] 4. Verify that the issue is reproducible on
2.4-develop
branchDetails
- Add the comment@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure.
- If the issue is reproducible on2.4-develop
branch, please, add the labelReproduced on 2.4.x
.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here! - [ ] 5. Add label
Issue: Confirmed
once verification is complete. - [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.
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.
:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-10831 is successfully created for this GitHub issue.
: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.
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