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?

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

A rebase with latest main should fix the unrelated spec failures also I think we should make it simpler.

core/app/models/spree/product.rb Show resolved Hide resolved
@tvdeyen tvdeyen force-pushed the total-count-when-stock-items-already-loaded branch from 280048c to 93f2419 Compare October 31, 2024 21:40
@tvdeyen tvdeyen force-pushed the total-count-when-stock-items-already-loaded branch from 93f2419 to 51df6ca Compare November 4, 2024 13:35
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.

4 participants