fix: eliminate N+1 queries in batch POST endpoints (#25)

Replace per-row Team/Player lookups with bulk IN-list queries before
the validation loop in post_transactions, post_results, post_schedules,
and post_batstats. A 50-move batch now uses 2 queries instead of 150.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2026-03-05 17:33:32 -06:00
parent 5ac9cce7f0
commit b0fd1d89ea
4 changed files with 47 additions and 11 deletions

View File

@ -381,14 +381,21 @@ async def post_batstats(s_list: BatStatList, token: str = Depends(oauth2_scheme)
all_stats = []
all_team_ids = list(set(x.team_id for x in s_list.stats))
all_player_ids = list(set(x.player_id for x in s_list.stats))
found_team_ids = set(
t.id for t in Team.select(Team.id).where(Team.id << all_team_ids)
)
found_player_ids = set(
p.id for p in Player.select(Player.id).where(Player.id << all_player_ids)
)
for x in s_list.stats:
team = Team.get_or_none(Team.id == x.team_id)
this_player = Player.get_or_none(Player.id == x.player_id)
if team is None:
if x.team_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.team_id} not found"
)
if this_player is None:
if x.player_id not in found_player_ids:
raise HTTPException(
status_code=404, detail=f"Player ID {x.player_id} not found"
)

View File

@ -159,12 +159,21 @@ async def post_results(result_list: ResultList, token: str = Depends(oauth2_sche
raise HTTPException(status_code=401, detail="Unauthorized")
new_results = []
all_team_ids = list(
set(x.awayteam_id for x in result_list.results)
| set(x.hometeam_id for x in result_list.results)
)
found_team_ids = set(
t.id for t in Team.select(Team.id).where(Team.id << all_team_ids)
)
for x in result_list.results:
if Team.get_or_none(Team.id == x.awayteam_id) is None:
if x.awayteam_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.awayteam_id} not found"
)
if Team.get_or_none(Team.id == x.hometeam_id) is None:
if x.hometeam_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.hometeam_id} not found"
)

View File

@ -144,12 +144,21 @@ async def post_schedules(sched_list: ScheduleList, token: str = Depends(oauth2_s
raise HTTPException(status_code=401, detail="Unauthorized")
new_sched = []
all_team_ids = list(
set(x.awayteam_id for x in sched_list.schedules)
| set(x.hometeam_id for x in sched_list.schedules)
)
found_team_ids = set(
t.id for t in Team.select(Team.id).where(Team.id << all_team_ids)
)
for x in sched_list.schedules:
if Team.get_or_none(Team.id == x.awayteam_id) is None:
if x.awayteam_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.awayteam_id} not found"
)
if Team.get_or_none(Team.id == x.hometeam_id) is None:
if x.hometeam_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.hometeam_id} not found"
)

View File

@ -143,16 +143,27 @@ async def post_transactions(
all_moves = []
all_team_ids = list(
set(x.oldteam_id for x in moves.moves) | set(x.newteam_id for x in moves.moves)
)
all_player_ids = list(set(x.player_id for x in moves.moves))
found_team_ids = set(
t.id for t in Team.select(Team.id).where(Team.id << all_team_ids)
)
found_player_ids = set(
p.id for p in Player.select(Player.id).where(Player.id << all_player_ids)
)
for x in moves.moves:
if Team.get_or_none(Team.id == x.oldteam_id) is None:
if x.oldteam_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.oldteam_id} not found"
)
if Team.get_or_none(Team.id == x.newteam_id) is None:
if x.newteam_id not in found_team_ids:
raise HTTPException(
status_code=404, detail=f"Team ID {x.newteam_id} not found"
)
if Player.get_or_none(Player.id == x.player_id) is None:
if x.player_id not in found_player_ids:
raise HTTPException(
status_code=404, detail=f"Player ID {x.player_id} not found"
)