Performance enhancements for order admin - Eager Loading #4196
Unanswered
justinholtweb
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Optimize Control Panel Order Detail Performance with Eager Loading
Problem
When viewing order details in the Craft Commerce Control Panel, the system generates excessive duplicate database queries due to missing eager loading. This creates significant performance issues, especially for orders with multiple line items.
Observed Performance Issues
Before optimization:
After optimization:
Root Causes
OrdersController::actionEditOrder()doesn't use eager loading - When fetching an order for the CP detail view, the controller callsgetOrderById()without eager loading relationships, causing N+1 queries for line items, transactions, adjustments, addresses, and customer data.No purchasables eager loading - While
withAll()exists for eager loading order relationships, it doesn't include purchasables for line items. Each line item'sgetPurchasable()call triggers a separate database query.Missing eager loading support in
getOrderById()- The service method doesn't accept parameters to enable eager loading, making it impossible for controllers to optimize queries.Solution
I have a PR ready that adds comprehensive eager loading support for CP order detail views:
1. Enhanced Orders::getOrderById() with Eager Loading Parameter
File:
src/services/Orders.phpAdded optional
$withAllparameter to enable eager loading:2. Updated OrdersController to Use Eager Loading
File:
src/controllers/OrdersController.phpModified
actionEditOrder()to request eager loading:3. Added Purchasables Eager Loading to OrderQuery
File:
src/elements/db/OrderQuery.phpAdded
$withPurchasablesproperty andwithPurchasables()method to enable purchasable eager loading:Updated
populate()to batch-load purchasables when enabled:4. Implemented Batch Purchasable Loading
File:
src/services/LineItems.phpAdded
eagerLoadPurchasablesForLineItems()method to batch-load purchasables by element type:Performance Impact
Query Reduction
Specific Optimizations
Backward Compatibility
100% Backward Compatible
getOrderById()defaults to$withAll = false, maintaining existing behavior->with()explicitly)Testing
Tested on:
Beta Was this translation helpful? Give feedback.
All reactions