solidus icon indicating copy to clipboard operation
solidus copied to clipboard

Check if stock_items are loaded before doing a SUM()

Open BenMorganIO opened this issue 2 years ago • 2 comments

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 avatar Mar 09 '24 18:03 BenMorganIO

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

kennyadsl avatar Mar 11 '24 17:03 kennyadsl