Skip to content

Avoid an N+1 loading stock on the admin products index#6495

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

Avoid an N+1 loading stock on the admin products index#6495
ikraamg wants to merge 1 commit into
solidusio:mainfrom
ikraamg:perf/admin-products-stock-n-plus-one

Conversation

@ikraamg

@ikraamg ikraamg commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

The solidus_admin products index renders each product's stock via total_on_hand, which loaded stock_items for every variant because variants_including_master and their stock items weren't eager-loaded. The page's query count grew with the number of products and variants.

  • eager-loads variants_including_master with their stock items and stock locations
  • the index issues a single stock_items query regardless of page size
  • adds a request spec asserting the index renders and has no stock N+1

Comment thread admin/spec/requests/solidus_admin/products_spec.rb
Comment thread admin/spec/requests/solidus_admin/products_spec.rb
Comment thread admin/spec/requests/solidus_admin/products_spec.rb
Comment thread admin/spec/requests/solidus_admin/products_spec.rb
@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 (10f869c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6495   +/-   ##
=======================================
  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.

The products index renders each product's stock via total_on_hand, which loaded stock_items per variant because variants_including_master and their stock_items were not eager-loaded. Eager-loading them keeps the index query count flat regardless of page size.
@ikraamg ikraamg force-pushed the perf/admin-products-stock-n-plus-one branch from 6498a45 to 10f869c Compare June 26, 2026 03:32
@ikraamg ikraamg marked this pull request as ready for review June 26, 2026 06:34
@ikraamg ikraamg requested a review from a team as a code owner June 26, 2026 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants