Skip to content

Preload adjustments in promotion recalculation to avoid an N+1#6496

Open
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/promotion-adjustments-preload-n-plus-one
Open

Preload adjustments in promotion recalculation to avoid an N+1#6496
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/promotion-adjustments-preload-n-plus-one

Conversation

@ikraamg

@ikraamg ikraamg commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Recalculating promotions loaded each line item's and shipment's adjustments with its own query, so the adjustment query count grew with the number of items in the order. This runs on every order recalculation.

  • preloads adjustments for all line items and shipments in one batch before the recalculation loop
  • applies the same fix to both the legacy and the new promotion adjusters
  • adjustment queries stay constant regardless of order size
  • adds a spec to each engine asserting line item adjustments load in a single query

Comment thread promotions/app/models/solidus_promotions/order_adjuster.rb
Comment thread promotions/spec/models/solidus_promotions/order_adjuster_spec.rb Outdated
Comment thread promotions/spec/models/solidus_promotions/order_adjuster_spec.rb
Comment thread promotions/spec/models/solidus_promotions/order_adjuster_spec.rb
Comment thread promotions/spec/models/solidus_promotions/order_adjuster_spec.rb
@github-actions github-actions Bot added changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Jun 26, 2026
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.40%. Comparing base (8d781ac) to head (1eca826).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6496      +/-   ##
==========================================
- Coverage   89.68%   89.40%   -0.29%     
==========================================
  Files         993      906      -87     
  Lines       20863    19208    -1655     
==========================================
- Hits        18712    17172    -1540     
+ Misses       2151     2036     -115     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Recalculating promotions loaded each line item's and shipment's
adjustments with a separate query, so the adjustment query count grew
with the number of items in the order. This runs on every order
recalculation.

Preloading the adjustments for all items in one batch keeps the query
count constant regardless of order size. The same fix applies to both
the legacy and the new promotion adjusters.
@ikraamg ikraamg force-pushed the perf/promotion-adjustments-preload-n-plus-one branch from a1d1c34 to 1eca826 Compare June 26, 2026 03:30
end

it "loads line item adjustments in a single query" do
expect { order_adjuster.call }.to make_database_queries(matching: /from .spree_adjustments..*adjustable_id. IN \(/im, count: 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [133/80]

@ikraamg ikraamg marked this pull request as ready for review June 26, 2026 06:34
@ikraamg ikraamg requested a review from a team as a code owner June 26, 2026 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_legacy_promotions Changes to the solidus_legacy_promotions gem changelog:solidus_promotions Changes to the solidus_promotions gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants