fix: add missing indexes on FK columns in stratplay and stratgame (#74) #95
No reviewers
Labels
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/major-domo-database#95
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "issue/74-add-missing-indexes-on-foreign-key-columns-in-high"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #74
Changes
app/db_engine.py— AddedMetaclasses withindexesdeclarations toStratGameandStratPlay:StratGame.Meta.indexes: single-column index onseason; composite index on(season, week, game_num)StratPlay.Meta.indexes: single-column indexes ongame(FK→stratgame),batter(FK→player),pitcher(FK→player),runner(FK→player)migrations/2026-03-27_add_stratplay_stratgame_indexes.sql— New migration usingCREATE INDEX IF NOT EXISTSfor all 6 indexes. Safe to run on existing databases.Why
PostgreSQL does not auto-index foreign key columns.
stratplayandstratgameare the highest-volume tables and are filtered/joined on these columns in batting/pitching/running stats aggregation and standings recalculation. Missing indexes cause sequential scans as data grows.Other observations
app/db_engine.pyhad pre-existing ruff/black reformatting in the working tree before this work began (single→double quotes, trailing commas, line wrapping). This noise is included in the diff — consistent with the pattern across previous PRs in this repo.AI Code Review
Files Reviewed
app/db_engine.py(modified — Meta.indexes added to StratGame and StratPlay, plus ruff/black noise)migrations/2026-03-27_add_stratplay_stratgame_indexes.sql(added)Findings
Correctness
((("field_name",), is_unique), ...)withFalsefor non-unique indexes is right."game","batter") — not the column name ("game_id") — inMeta.indexes. Peewee's metaclass resolves FK field names to<field>_idcolumn names automatically.game_id,batter_id,pitcher_id,runner_idare the actual PostgreSQL column names for FK fields, matching what Peewee generates.stratgame,stratplay).StratGamenorStratPlayhad aMetaclass. The newMetawith onlyindexescorrectly inheritsdatabase = dbfromBaseModel.Metavia Peewee's metaclass.StratGame→stratgameandStratPlay→stratplay, matching the migration SQL — notable_namedeclaration needed for correctness.Security
Style & Conventions
CREATE INDEX IF NOT EXISTS— idempotent, safe on existing databases. Good practice.Meta.table_name". The new Meta classes omittable_namesince the implicit names are correct — functionally fine, but addingtable_name = "stratgame"/table_name = "stratplay"would make the convention explicit.Suggestions
(season, week, game_num)covers the single-column(season)prefix in most query planners. For workloads where the composite index is always present, the standaloneseasonindex may be redundant. That said, having the standalone index is harmless and ensures the optimizer uses it for simpleWHERE season = Xwithout a covering scan on the composite — reasonable choice.Verdict: APPROVED
Clean, correct implementation. ORM declarations and migration SQL are aligned. Peewee field-name/column-name convention for FK indexes is correctly handled.
IF NOT EXISTSmakes the migration safe to replay.Automated review by Claude PR Reviewer
Checkout
From your project repository, check out a new branch and test the changes.