Add missing game_id index to speed up per-game status - #251
Merged
Conversation
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>
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.
Description
StatusPresenter#games(used by/api/status) filtersTeambygame_idviagame.teams(has_many), andGame#users/#matches/#challenges/#seasonsall start withteams.distinct(:_id)on the same relation.Team.belongs_to :gamehad no index, so every one of these was a full collection scan onteams.Added
belongs_to :game, index: true(matching the pattern already used forbelongs_to :teamonUser/Match/Season/Challenge).Verification
Confirmed via MongoDB
explain()on a seededteamscollection:COLLSCANCOUNT_SCANusing the newgame_id_1indexTesting
bundle exec rubocop -abundle exec rspec(590 examples, 0 failures)