Skip to content

Eager load variant stock on the products API index to avoid an N+1#6501

Open
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/api-products-stock-n-plus-one
Open

Eager load variant stock on the products API index to avoid an N+1#6501
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/api-products-stock-n-plus-one

Conversation

@ikraamg

@ikraamg ikraamg commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

The products API index renders each variant's stock on hand, but did not eager load stock items, so the stock queries grew with the number of variants across the listed products. The page also loaded the master and the variants-including-master per product.

  • eager loads stock items and stock locations for the master, the variants, and the variants including master
  • stock queries stay constant regardless of how many products and variants are listed
  • adds a spec asserting the index loads variant stock without an N+1

The products API index rendered each variant's stock on hand without
eager loading stock items, so the query count grew with the number of
variants across the listed products.

Eager loading stock items for the master, the variants, and the
variants including master keeps the stock queries constant regardless
of how many products and variants are listed.
Comment thread api/app/controllers/spree/api/products_controller.rb
Comment thread api/app/controllers/spree/api/products_controller.rb
Comment thread api/app/controllers/spree/api/products_controller.rb
Comment thread api/app/controllers/spree/api/products_controller.rb
@github-actions github-actions Bot added the changelog:solidus_api Changes to the solidus_api gem label Jun 26, 2026
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.68%. Comparing base (8d781ac) to head (b32c208).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6501   +/-   ##
=======================================
  Coverage   89.68%   89.68%           
=======================================
  Files         993      993           
  Lines       20863    20863           
=======================================
  Hits        18712    18712           
  Misses       2151     2151           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ikraamg ikraamg marked this pull request as ready for review June 26, 2026 06:35
@ikraamg ikraamg requested a review from a team as a code owner June 26, 2026 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_api Changes to the solidus_api gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants