2.1 KiB
2.1 KiB
| id | type | title | tags | importance | confidence | created | updated | relations | |||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| e014f59b-60ff-4602-91bc-076b3a73f0e8 | fix | Fix: batch PitchingCardRatings lookup in pitcher sort (paper-dynasty-database #19) |
|
0.65 | 0.8 | 2026-03-03T23:04:16.395675+00:00 | 2026-03-04T02:04:45.819294+00:00 |
|
Problem
sort_pitchers() and sort_starters() in app/routers_v2/teams.py called PitchingCardRatings.get_or_none() twice per row inside a DataFrame.apply() — once for vs_hand="L" and once for "R". With 30 pitchers this was 60 queries.
Solution
Before the apply, batch-fetch all ratings for all card IDs in one query and build a (pitchingcard_id, vs_hand) → rating dict. The closure does O(1) dict lookups.
card_ids = pitcher_df["id"].tolist()
ratings_map = {
(r.pitchingcard_id, r.vs_hand): r
for r in PitchingCardRatings.select().where(
(PitchingCardRatings.pitchingcard_id << card_ids)
& (PitchingCardRatings.vs_hand << ["L", "R"])
)
}
def get_total_ops(df_data):
vlval = ratings_map.get((df_data["id"], "L"))
vrval = ratings_map.get((df_data["id"], "R"))
...
Files Changed
app/routers_v2/teams.py— bothsort_pitchersand nestedsort_starters
Pattern
General pattern for Peewee + pandas: never do model lookups inside DataFrame.apply. Batch fetch with model << id_list, build a dict, use dict lookup in the closure.