Skip to content

Add missing game_id index to speed up per-game status - #251

Merged
dblock merged 1 commit into
masterfrom
optimize-team-game-index
Aug 30, 2026
Merged

Add missing game_id index to speed up per-game status#251
dblock merged 1 commit into
masterfrom
optimize-team-game-index

Conversation

@dblock

@dblock dblock commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Description

StatusPresenter#games (used by /api/status) filters Team by game_id via game.teams (has_many), and Game#users/#matches/#challenges/#seasons all start with teams.distinct(:_id) on the same relation. Team.belongs_to :game had no index, so every one of these was a full collection scan on teams.

Added belongs_to :game, index: true (matching the pattern already used for belongs_to :team on User/Match/Season/Challenge).

Verification

Confirmed via MongoDB explain() on a seeded teams collection:

  • Without index: COLLSCAN
  • With index: COUNT_SCAN using the new game_id_1 index

Testing

  • bundle exec rubocop -a
  • bundle exec rspec (590 examples, 0 failures)

StatusPresenter#games (used by /api/status) filters Team by game_id
via game.teams (has_many) and Game#users/matches/challenges/seasons
(which start with teams.distinct(:_id)), but Team.belongs_to :game
had no index, causing a full collection scan on every status request
(confirmed via explain(): COLLSCAN without the index, COUNT_SCAN
using the new game_id_1 index with it).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dblock
dblock merged commit 92065d0 into master Aug 30, 2026
4 checks passed
@dblock
dblock deleted the optimize-team-game-index branch August 30, 2026 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant