Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch adjustments efficiently from Spree::Order #5790

Closed
wants to merge 3 commits into from

Conversation

vl3
Copy link
Contributor

@vl3 vl3 commented Jun 13, 2024

Summary

This commit updates the Spree::Order model to improve the efficiency of fetching adjustments. The approach is to use includes(:adjustments) in the association queries. This tells ActiveRecord to preload the adjustments along with the line items or shipments, resulting in a more efficient single database query for each table.

» o.item_total_before_tax
  Spree::LineItem Load (0.8ms)  SELECT "spree_line_items".* FROM "spree_line_items" WHERE "spree_line_items"."order_id" = $1 ORDER BY "spree_line_items"."created_at" ASC, "spree_line_items"."id" ASC  [["order_id", 8701508]]
  Spree::Adjustment Load (11.7ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_id" = $1 AND "spree_adjustments"."adjustable_type" = $2  [["adjustable_id", 43746834], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.5ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_id" = $1 AND "spree_adjustments"."adjustable_type" = $2  [["adjustable_id", 43746835], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.3ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_id" = $1 AND "spree_adjustments"."adjustable_type" = $2  [["adjustable_id", 43746836], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.3ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_id" = $1 AND "spree_adjustments"."adjustable_type" = $2  [["adjustable_id", 43746837], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.3ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_id" = $1 AND "spree_adjustments"."adjustable_type" = $2  [["adjustable_id", 43746838], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.2ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_id" = $1 AND "spree_adjustments"."adjustable_type" = $2  [["adjustable_id", 43746839], ["adjustable_type", "Spree::LineItem"]]
=> 89.94
» o.item_total_before_tax
  Spree::LineItem Load (0.8ms)  SELECT "spree_line_items".* FROM "spree_line_items" WHERE "spree_line_items"."order_id" = $1 ORDER BY "spree_line_items"."created_at" ASC, "spree_line_items"."id" ASC  [["order_id", 8701508]]
  Spree::Adjustment Load (0.7ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_type" = $1 AND "spree_adjustments"."adjustable_id" IN ($2, $3, $4, $5, $6, $7)  [["adjustable_type", "Spree::LineItem"], ["adjustable_id", 43746834], ["adjustable_id", 43746835], ["adjustable_id", 43746836], ["adjustable_id", 43746837], ["adjustable_id", 43746838], ["adjustable_id", 43746839]]
=> 89.94

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@vl3 vl3 requested a review from a team as a code owner June 13, 2024 17:08
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Jun 13, 2024
@vl3 vl3 closed this Jun 13, 2024
@kennyadsl
Copy link
Member

@vl3 why did you close this?

@vl3
Copy link
Contributor Author

vl3 commented Jun 14, 2024

@kennyadsl there were a few failing tests related, I didn't check them because I had no time at that moment, I will do it soon and reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants