Skip to content

perf: eliminate N+1 queries in plan range and meetings#300

Open
jlunder00 wants to merge 3 commits intodevfrom
cleanup/n-plus-one-queries
Open

perf: eliminate N+1 queries in plan range and meetings#300
jlunder00 wants to merge 3 commits intodevfrom
cleanup/n-plus-one-queries

Conversation

@jlunder00
Copy link
Copy Markdown
Owner

Summary

GET /plan/range — O(days) → O(1) queries

  • Added get_plans_for_range(conn, start_date, end_date) to db/pg_queries/plans.py
  • Issues 6 queries total regardless of date range (was 5–6 per day): plan dates, tasks, recurring masters, exception rows, dependencies, acknowledgements, check-ins — all batched with BETWEEN $1 AND $2 or ANY($1) clauses
  • Updated handler in api/routes/plan.py to call get_plans_for_range directly

POST /meetings/request + POST /meetings/{id}/accept — 3 N+1 loops removed

  • Added get_users_by_usernames(conn, usernames) and get_users_by_ids(conn, ids) to db/pg_auth_queries.py
  • Added get_connections_for_caller(conn, caller_id, target_ids) to db/pg_queries/scheduling.py
  • Updated request_meeting handler: single batch lookup for usernames → ids, single batch connection check
  • Updated accept_slot handler: single batch lookup for participant usernames

Test plan

  • test_get_plan_range_uses_batch_query — patches get_plan to raise, confirms batch path used (requires DATABASE_URL on Pi)
  • test_request_meeting_uses_batch_username_resolution — patches individual lookup to raise, confirms batch used
  • test_accept_slot_uses_batch_user_lookup — patches individual lookup to raise, confirms batch used
  • All existing plan + meetings tests pass (skipped here, require DATABASE_URL)

@jlunder00 jlunder00 changed the base branch from main to dev May 6, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant