Speed up /coaches: partial index and database-side coach count - #2900
Merged
Merged
Conversation
The /coaches page counted all-time attended coaches by loading the grouped result into Ruby (Relation#length) and aggregating over all 1.98M workshop_invitations rows. A partial index on member_id for attended coach rows makes the aggregation an index-only scan, and the count now runs as COUNT(DISTINCT member_id) instead of materialising 3,572 rows. The index is created and dropped concurrently. Measured against the production dump: page db_runtime 112ms to 46ms. Closes #2886
mroderick
force-pushed
the
perf/coach-count-index
branch
from
September 17, 2026 10:00
cd4cb9f to
d052862
Compare
mroderick
marked this pull request as ready for review
September 17, 2026 10:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2886
Summary
The /coaches page counted all-time attended coaches by loading the grouped result into Ruby (
Relation#lengthmaterialises all 3,572 grouped rows) and aggregating over all 1.98Mworkshop_invitationsrows — the slowest of the page's five queries. This PR:workshop_invitations(member_id)WHEREattended AND role = 'Coach'(CREATE INDEX CONCURRENTLY), making the aggregation an index-only scan over the 18k matching rows, andtop_coach_query.lengthwithCOUNT(DISTINCT member_id)so the count never leaves the database as rows.Measured (production dump, in-process, warm medians)
Production context: post-#2889 the page sits at ~320-375ms total, db 252-344ms; this addresses most of what remains.
Trade-off
The index predicate must keep matching the
to_coaches/attendedscopes; if either scope changes, the index silently stops covering the count. A staging EXPLAIN after deploy is recommended. The index adds a small write cost to coach-attended rows (negligible at this table's write rate).Verification
spec/features/listing_coaches_spec.rb: new scenario pins the header count (same coach at two workshops counts once); 4/4 green.db:rollback/db:migrate), including the concurrent drop.codebar_production_dumpconfirms the index-only scan.