solidusio / solidusio/solidus_subscriptions

N+1 query on subscribable in admin subscriptions index

Open
#331 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.