Skip to content

Email for professionals and teams

perf: covering indexes for folder counts and conversation paging

ssthormessd976136907a2
diff --git a/drizzle/migrations/0074_add_counts_conversation_indexes.sql b/drizzle/migrations/0074_add_counts_conversation_indexes.sql
new file mode 100644
index 0000000..0d5a063
--- /dev/null
+++ b/drizzle/migrations/0074_add_counts_conversation_indexes.sql
@@ -0,0 +1,12 @@
+-- Folder counts and conversation paging on large mailboxes. The counts
+-- endpoint groups by (mailbox, folder, direction, status, read, starred,
+-- snoozed_until); without a covering index that means a temp b-tree over
+-- every row.
+CREATE INDEX IF NOT EXISTS `messages_counts_cover_idx` ON `messages` (`mailbox_id`, `folder_id`, `direction`, `status`, `read`, `starred`, `snoozed_until`);--> statement-breakpoint
+-- Conversation paging partitions by coalesce(thread_id, id) and orders each
+-- partition by created_at desc. Column order matters: the thread key must
+-- come before created_at so the index both covers the query and feeds the
+-- window partition in sorted order. Without this, ANALYZE stats can steer
+-- the planner onto the non-covering messages_mailbox_thread_key_idx and
+-- pay a table lookup per scanned row.
+CREATE INDEX IF NOT EXISTS `messages_conv_cover_idx` ON `messages` (`mailbox_id`, `status`, coalesce(`thread_id`, `id`), `created_at`, `id`, `snoozed_until`);
diff --git a/drizzle/migrations/meta/_journal.json b/drizzle/migrations/meta/_journal.json
index 9181edb..8142c81 100644
--- a/drizzle/migrations/meta/_journal.json
+++ b/drizzle/migrations/meta/_journal.json
@@ -456,6 +456,13 @@
"when": 1792844400000,
"tag": "0073_add_undo_send_seconds",
"breakpoints": true
+ },
+ {
+ "idx": 66,
+ "version": "6",
+ "when": 1792844400001,
+ "tag": "0074_add_counts_conversation_indexes",
+ "breakpoints": true
}
]
}
diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts
index 39bf658..60e710a 100644
--- a/src/db/schema/index.ts
+++ b/src/db/schema/index.ts
@@ -328,6 +328,31 @@ export const messages = sqliteTable(
t.createdAt,
),
index("messages_mailbox_thread_key_idx").on(t.mailboxId, sql`coalesce(${t.threadId}, ${t.id})`),
+ // Covering index for the folder-counts GROUP BY: lets the counts
+ // endpoint stream grouped rows straight off the index instead of
+ // sorting the whole table.
+ index("messages_counts_cover_idx").on(
+ t.mailboxId,
+ t.folderId,
+ t.direction,
+ t.status,
+ t.read,
+ t.starred,
+ t.snoozedUntil,
+ ),
+ // Conversation paging partitions by coalesce(thread_id, id) and orders
+ // each partition by created_at desc, so the thread key must precede
+ // created_at for the index to feed the window in sorted order. The
+ // snoozed_until filter rides the tail column and never touches the
+ // table.
+ index("messages_conv_cover_idx").on(
+ t.mailboxId,
+ t.status,
+ sql`coalesce(${t.threadId}, ${t.id})`,
+ t.createdAt,
+ t.id,
+ t.snoozedUntil,
+ ),
],
);