Fix N+1 query in get_custom_commands #26

Open
opened 2026-02-20 06:50:10 +00:00 by cal · 1 comment
Owner

app/routers_v3/custom_commands.py:294-296 — For each command, a separate SELECT for creator is issued. With 25 commands per page = 26 queries. The initial join already fetches creator fields.

Priority: medium | Labels: performance

`app/routers_v3/custom_commands.py:294-296` — For each command, a separate SELECT for creator is issued. With 25 commands per page = 26 queries. The initial join already fetches creator fields. **Priority**: medium | **Labels**: performance
cal added the
ai-working
label 2026-03-05 22:00:47 +00:00
Author
Owner

Fixed in PR #51.

Approach: Expanded the JOIN SELECT in get_custom_commands to also fetch creator.created_at, creator.total_commands, and creator.active_commands (in addition to the already-joined discord_id, username, display_name). Replaced the per-row SELECT * FROM custom_command_creators WHERE id = %s with building CustomCommandCreatorModel directly from the joined data. Result: 26 queries → 2 queries for a 25-command page.

Fixed in PR #51. **Approach**: Expanded the JOIN SELECT in `get_custom_commands` to also fetch `creator.created_at`, `creator.total_commands`, and `creator.active_commands` (in addition to the already-joined `discord_id`, `username`, `display_name`). Replaced the per-row `SELECT * FROM custom_command_creators WHERE id = %s` with building `CustomCommandCreatorModel` directly from the joined data. Result: 26 queries → 2 queries for a 25-command page.
cal added
ai-pr-opened
and removed
ai-working
labels 2026-03-05 22:04:06 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: cal/major-domo-database#26
No description provided.