perf: N+1 query in _build_card_state_response for image_url #199
Labels
No Label
ai-changes-requested
ai-failed
ai-merged
ai-pr-opened
ai-reviewed
ai-reviewing
ai-working
autonomous
bug
enhancement
evolution
performance
phase-0
phase-1a
phase-1b
phase-1c
phase-1d
security
size:M
size:S
tech-debt
todo
type:feature
type:stability
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/paper-dynasty-database#199
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/routers_v2/refractor.pylines 71–86,_build_card_state_responsetriggers a separateCardModel.get()query to resolveimage_urlfrom the variant card row for every card state in the response.When listing cards via the
/cardsendpoint withlimit=100, this means up to 100 additional DB queries.Fix
Join the image_url lookup into the main query, or cache variant card image URLs on the
RefractorCardStatemodel itself (populated at variant creation time).Impact
Latency scales linearly with result count. Becomes noticeable at 50+ cards per team as refractor states accumulate.
PR #206 opens the fix: #206
The approach: before the response-building loop in
list_card_states, collect all variant player IDs from the page and issue at most 2 bulkSELECT … WHERE player_id IN (…)queries (one for BattingCard, one for PitchingCard). Results are stored in a(player_id, variant) → image_urldict and passed directly to_build_card_state_responsevia a newimage_urlparameter, skipping the per-rowCardModel.get()entirely. Query count goes from1 + Nto1 + 0–2regardless of page size.