solidusio / solidusio/solidus_subscriptions
N+1 query on subscribable in admin subscriptions index
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 49
- Forks
- 53
- Avg merge
- 4h 54m
- Merged PRs (30d)
- 1
Description
Problem
The admin subscriptions index controller eager loads line_items and user:
@subscriptions = @search.result(distinct: true).
includes(:line_items, :user).
page(page).per(per_page)
However, the index view iterates over subscription.line_items and accesses line_item.subscribable.name to display the line items column. Since :subscribable is not included in the eager load, this causes an N+1 query — one extra query per subscription row to load the subscribable variant.
For a page with 25 subscriptions, this adds ~25 unnecessary SQL queries. For the export path (which sets per_page to 100,000), this could be significantly worse.
Suggested Fix
Change the includes in the index action from:
includes(:line_items, :user)
to:
includes(line_items: [:subscribable]).includes(:user)
This ensures the subscribable association is eager loaded along with line items, eliminating the N+1.
Environment
- solidus_subscriptions (styrken fork, commit 9318faa)
- File:
app/controllers/spree/admin/subscriptions_controller.rb
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Open app/controllers/spree/admin/subscriptions_controller.rb and inspect the index action's eager-loading chain. Check the index and export paths described in the issue, then verify that subscribable data is loaded without one query per subscription row. Done means the suggested association is eager loaded for both normal pagination and the large export path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- backend, performance
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100