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.5k stars 9.3k forks source link

[Performance] Loading order through repository loads the order multiple times from DB. (ShippingBuilder) #36636

Closed Nuranto closed 11 months ago

Nuranto commented 1 year ago

Summary

Magento 2.4.5-p1

Loading order through repository loads the order three times from DB :

$this->orderRepository->get($orderId);

Loads the order from database, then calls setShippingAssignments() which calls TaxManagement::getShippingAssignmentBuilderDependency() which loads the order from database again.

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Sales/Model/Order/ShippingBuilder.php#L79

and

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Sales/Model/Order/ShippingAssignmentBuilder.php#L79

Examples

$this->orderRepository->get($orderId);

Steps to reproduce

  1. Login in frontend with the user which has atleast 1 order
  2. Enable DB log
  3. Add breakpoints in the below lines: https://github.com/magento/magento2/blob/9ef88b46ff666673623e3f04d969c46ecf396cad/app/code/Magento/Sales/Model/OrderRepository.php#L137

https://github.com/magento/magento2/blob/9ef88b46ff666673623e3f04d969c46ecf396cad/app/code/Magento/Sales/Model/Order/ShippingAssignmentBuilder.php#L79

https://github.com/magento/magento2/blob/9ef88b46ff666673623e3f04d969c46ecf396cad/app/code/Magento/Sales/Model/Order/ShippingBuilder.php#L79

  1. Try to access My Account page
  2. The function $this->orderFactory->create()->load($this->getOrderId()) is calling 3 times for order id 2. Below is the MySQL query fetched from db.log:
    SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
    SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
    SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')

Proposed solution

I guess line $shippingAssignments->setOrderId($order->getEntityId()); should be replaced somehow by $shippingAssignments->setOrder($order);

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @Nuranto. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

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.


: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.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 1 year ago

Hi @engcom-Hotel. 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-Hotel commented 1 year ago

Hello @Nuranto,

Thanks for the report and collaboration!

We need some more information in order to reproduce the issue. As per my understanding, the method $this->getOrderId() is calling 3 times in the below function:

https://github.com/magento/magento2/blob/3e5be99a940ef572e1a99fee8b9258fda5be9ff4/app/code/Magento/Sales/Model/Order/ShippingBuilder.php#L75-L92

We can save the value of the output of $this->getOrderId() and use it wherever required. Am I on the same page? Or please let us know the steps to reproduce the issue.

Thanks

Nuranto commented 1 year ago

Hello @engcom-Hotel,

I'm not sure we're on the same page. Calling $this->getOrderId() is not an issue to me.

Issue is when we call Magento\Sales\Model\OrderRepository::get():

https://github.com/magento/magento2/blob/3e5be99a940ef572e1a99fee8b9258fda5be9ff4/app/code/Magento/Sales/Model/OrderRepository.php#L130-L144

You can see on line 137 that the order is loaded from database.

But line 144 is calling those two lines :

https://github.com/magento/magento2/blob/3e5be99a940ef572e1a99fee8b9258fda5be9ff4/app/code/Magento/Sales/Model/Order/ShippingBuilder.php#L79

https://github.com/magento/magento2/blob/3e5be99a940ef572e1a99fee8b9258fda5be9ff4/app/code/Magento/Sales/Model/Order/ShippingAssignmentBuilder.php#L79

Result is that when we call Magento\Sales\Model\OrderRepository::get(), the order gets loaded from database 3 times instead of 1. (that's actually more than 3 times, but I've created distinct issues for each concerned modules)

I think the best thing to do is to replace ->setOrderId($order->getEntityId()) by ->setOrder($order) here :

https://github.com/magento/magento2/blob/3e5be99a940ef572e1a99fee8b9258fda5be9ff4/app/code/Magento/Sales/Model/OrderRepository.php#L305

and refactor Magento\Sales\Model\Order\ShippingAssignmentBuilder accordingly :

and finally refactor Magento\Sales\Model\Order\ShippingBuilder the same way.

engcom-Hotel commented 1 year ago

Hello @Nuranto,

Thanks for the detailed explanation!

We have debugged the code via following the below steps:

  1. Login in frontend with the user which has atleast 1 order
  2. Enable DB log
  3. Add breakpoints in the below lines: https://github.com/magento/magento2/blob/9ef88b46ff666673623e3f04d969c46ecf396cad/app/code/Magento/Sales/Model/OrderRepository.php#L137

https://github.com/magento/magento2/blob/9ef88b46ff666673623e3f04d969c46ecf396cad/app/code/Magento/Sales/Model/Order/ShippingAssignmentBuilder.php#L79

https://github.com/magento/magento2/blob/9ef88b46ff666673623e3f04d969c46ecf396cad/app/code/Magento/Sales/Model/Order/ShippingBuilder.php#L79

  1. Try to access My Account page
  2. The function $this->orderFactory->create()->load($this->getOrderId()) is calling 3 times for order id 2. Below is the MySQL query fetched from db.log:
    SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
    SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
    SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')

Hence confirming the issue.

Thanks

github-jira-sync-bot commented 1 year ago

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

m2-assistant[bot] commented 1 year ago

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

o-iegorov commented 1 year ago

The internal team start working on. the issue

m2-assistant[bot] commented 1 year ago

Hi @o-iegorov. Thank you for working on this issue. Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction: