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

Check if stock_items are loaded before doing a SUM() #5687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BenMorganIO
Copy link
Contributor

@BenMorganIO BenMorganIO commented Mar 9, 2024

Summary

This PR is about trying to reduce n+1's in stores. If you have a product listing page that also displays the total_on_hand then you can get an n+1 with:

Spree::StockItem Sum (1.0ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 32]]
Spree::StockItem Sum (0.4ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 30]]
Spree::StockItem Sum (0.3ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 35]]
Spree::StockItem Sum (0.3ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 33]]
Spree::StockItem Sum (0.4ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 31]]
Spree::StockItem Sum (0.3ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 36]]
Spree::StockItem Sum (0.4ms)  SELECT SUM("spree_stock_items"."count_on_hand") FROM "spree_stock_items" INNER JOIN "spree_variants" ON "spree_stock_items"."variant_id" = "spree_variants"."id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = $1  [["product_id", 34]]

Now, if you've done Spree::Product.includes(:stock_items) you won't have to worry about this.

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.

@BenMorganIO BenMorganIO requested a review from a team as a code owner March 9, 2024 18:39
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Mar 9, 2024
@kennyadsl
Copy link
Member

@BenMorganIO failure seems actually related to the change, can you please take a look?

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.

None yet

2 participants