Fix N+1 query pattern in standings recalculation #75
Labels
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/major-domo-database#75
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
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?
Problem
In
app/db_engine.pylines 1304-1343, the standings recalculation loops through every game and callsgame.update_standings(), which issues ~4 queries each. For a full season (~288 games), this generates 1,100+ queries.On PostgreSQL with network latency between containers, this is significantly slower than on SQLite (in-process).
Fix
Batch the standings updates — prefetch related data and use bulk updates instead of row-by-row processing.
Severity
Medium — performance issue that worsens with data volume and network latency.
PR #93 opened: #93
Approach: Pre-fetch all standings, teams, and divisions for the season into dicts before the game loop, inline the win/loss tallying logic using dict lookups (zero DB queries per game), then save all modified standings in a single
bulk_updatecall. Full-season recalculation drops from ~1,100+ queries to ~5 queries.StratGame.update_standings()is left intact for single-game use cases.