Skip to content

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

@Nuranto

Description

@Nuranto

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:
    $entity = $this->metadata->getNewInstance()->load($id);

$order = $this->orderFactory->create()->load($this->getOrderId());

$this->order = $this->orderFactory->create()->load($this->getOrderId());

4. Try to access My Account page
5. 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

  • 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.
  • 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”.

Metadata

Metadata

Labels

Area: FrameworkIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: doneReported on 2.4.5-p1Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions