Remove N+1 queries in org, schedule, user and tag code paths #79
Labels
No labels
bug
chore
data
feature
frontend
good first issue
intermediate
needs/docs
needs/research
needs/upstream
type/bug
type/external
type/feature
type/refactor
urgency
high
urgency
immediate
urgency
low
urgency
medium
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ScottyLabs/cal#79
Loading…
Reference in a new issue
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?
What's wrong
Several routes run one or more queries per row of a previous query. With remote Supabase every round trip costs tens of ms, so large orgs and schedules load slowly.
How to reproduce
Turn on SQL logging (
echo=Trueinapi/app/services/db.py, or a SQLAlchemybefore_cursor_executecounter in a test), callGET /api/organizations/org/<id>for an org with many categories, and count the statements. The count grows with the number of categories.Expected
A fixed number of queries per request, independent of row count: one query per table with
IN (...)or a join, then grouping in Python.Where to look
api/app/api/organizations.pyget_organization_data()L99-130: events plus occurrences per category.api/app/api/schedule.pyget_schedule_route()L60-160: categories per org, then events plus occurrences per category, in both the course and the club branches.api/app/api/organizations.pyget_admins_in_org()L623-625: a user lookup and an org lookup per admin (the org is the same every time).api/app/api/users.pyget_admin_categories()L184:join_org_and_to_dict()per category;get_user_schedules()L158-171 lazy-loadsschedule_orgsandorgper row.api/app/api/events.pytag lookups per tag:create_event_record()L197-207 andupdate_event()L831-842.2c5f2e9.Done when
api/tests/api/test_query_counts.pyasserting the statement count does not grow with the number of categories, admins or tags