Eliminate N+1 queries in post_transactions #25

Closed
opened 2026-02-20 06:50:04 +00:00 by cal · 1 comment
Owner

app/routers_v3/transactions.py:126-133 — For each transaction in a batch, 3 individual DB queries validate teams and players. For a 50-move batch = 150 queries. Same pattern in post_results, post_schedules, post_batstats. All should use Team.id << [list] to fetch in a single query.

Priority: medium | Labels: performance

`app/routers_v3/transactions.py:126-133` — For each transaction in a batch, 3 individual DB queries validate teams and players. For a 50-move batch = 150 queries. Same pattern in `post_results`, `post_schedules`, `post_batstats`. All should use `Team.id << [list]` to fetch in a single query. **Priority**: medium | **Labels**: performance
cal added the
ai-working
label 2026-03-05 23:31:03 +00:00
cal removed the
ai-working
label 2026-03-05 23:34:04 +00:00
Author
Owner

PR #52 addresses this: #52

Fix approach: Before the validation loop in each endpoint, all required IDs are collected into sets and fetched with a single WHERE id IN (...) query. The loop then checks membership in the resulting Python sets instead of issuing a DB query per row.

  • post_transactions: 3N → 2 queries (team IDs + player IDs)
  • post_results / post_schedules: 2N → 1 query (combined away+home team IDs)
  • post_batstats: 2N → 2 queries (team IDs + player IDs)
PR #52 addresses this: https://git.manticorum.com/cal/major-domo-database/pulls/52 **Fix approach:** Before the validation loop in each endpoint, all required IDs are collected into sets and fetched with a single `WHERE id IN (...)` query. The loop then checks membership in the resulting Python sets instead of issuing a DB query per row. - `post_transactions`: 3N → 2 queries (team IDs + player IDs) - `post_results` / `post_schedules`: 2N → 1 query (combined away+home team IDs) - `post_batstats`: 2N → 2 queries (team IDs + player IDs)
cal added the
ai-pr-opened
label 2026-03-05 23:34:12 +00:00
cal closed this issue 2026-03-10 18:26:16 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: cal/major-domo-database#25
No description provided.