solidus
solidus copied to clipboard
Check if stock_items are loaded before doing a SUM()
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:
- [x] I have written a thorough PR description.
- [x] I have kept my commits small and atomic.
- [x] I have used clear, explanatory commit messages.
The following are not always needed:
@BenMorganIO failure seems actually related to the change, can you please take a look?