-
Notifications
You must be signed in to change notification settings - Fork 2
perf: SQL-based conversation pagination + missing DB indexes #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * Migration 004: Performance Indexes | ||
| * | ||
| * Adds missing composite indexes and a pg_trgm GIN index to resolve: | ||
| * | ||
| * 1. ai_feedback trend queries β getFeedbackTrend() filters by guild_id AND | ||
| * created_at but only had a single-column guild_id index, forcing a full | ||
| * guild scan + sort for every trend call. | ||
| * | ||
| * 2. conversations ILIKE search β content ILIKE '%...%' is a seq-scan | ||
| * without pg_trgm. Installing the extension + GIN index reduces search from | ||
| * O(n) to O(log n * trigram matches). | ||
| * | ||
| * 3. conversations(guild_id, created_at) β The default 30-day listing query | ||
| * (WHERE guild_id = $1 AND created_at >= $2 ORDER BY created_at DESC) | ||
| * benefits from a dedicated 2-column index over the existing 3-column | ||
| * (guild_id, channel_id, created_at) composite when channel_id is not filtered. | ||
| * | ||
| * 4. flagged_messages(guild_id, message_id) β POST /flag and the detail | ||
| * endpoint both do WHERE guild_id = $1 AND message_id = ANY($2) which | ||
| * the existing (guild_id, status) index cannot serve efficiently. | ||
| */ | ||
|
|
||
| /** @param {import('node-pg-migrate').MigrationBuilder} pgm */ | ||
| exports.up = (pgm) => { | ||
| // ai_feedback: composite for trend + recent queries | ||
| // getFeedbackTrend: WHERE guild_id = $1 AND created_at >= NOW() - INTERVAL ... | ||
| // getRecentFeedback: WHERE guild_id = $1 ORDER BY created_at DESC LIMIT $2 | ||
| pgm.sql(` | ||
| CREATE INDEX IF NOT EXISTS idx_ai_feedback_guild_created | ||
| ON ai_feedback(guild_id, created_at DESC) | ||
| `); | ||
|
|
||
| // conversations: pg_trgm for ILIKE searches | ||
| // Enable the extension first (idempotent) | ||
| pgm.sql(`CREATE EXTENSION IF NOT EXISTS pg_trgm`); | ||
|
|
||
| // GIN index over content column -- supports col ILIKE '%term%' and col ~ 'pattern' | ||
| pgm.sql(` | ||
| CREATE INDEX IF NOT EXISTS idx_conversations_content_trgm | ||
| ON conversations USING gin(content gin_trgm_ops) | ||
| `); | ||
|
|
||
| // conversations: (guild_id, created_at) for default 30-day listing | ||
| // The existing idx_conversations_guild_channel_created covers (guild_id, channel_id, created_at) | ||
| // but queries that filter only by guild_id + date range skip the channel_id column, | ||
| // making this 2-column index cheaper to scan. | ||
| pgm.sql(` | ||
| CREATE INDEX IF NOT EXISTS idx_conversations_guild_created | ||
| ON conversations(guild_id, created_at DESC) | ||
| `); | ||
|
|
||
| // flagged_messages: (guild_id, message_id) for detail + flag endpoints | ||
| // Used by: | ||
| // GET /:conversationId -> WHERE guild_id = $1 AND message_id = ANY($2) | ||
| // POST /:conversationId/flag -> msgCheck + anchorCheck in parallel | ||
| pgm.sql(` | ||
| CREATE INDEX IF NOT EXISTS idx_flagged_messages_guild_message | ||
| ON flagged_messages(guild_id, message_id) | ||
| `); | ||
| }; | ||
|
|
||
| /** @param {import('node-pg-migrate').MigrationBuilder} pgm */ | ||
| exports.down = (pgm) => { | ||
| pgm.sql(`DROP INDEX IF EXISTS idx_flagged_messages_guild_message`); | ||
| pgm.sql(`DROP INDEX IF EXISTS idx_conversations_guild_created`); | ||
| pgm.sql(`DROP INDEX IF EXISTS idx_conversations_content_trgm`); | ||
| pgm.sql(`DROP INDEX IF EXISTS idx_ai_feedback_guild_created`); | ||
| // Note: do NOT drop pg_trgm extension on down -- it may be used elsewhere. | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.