Skip to content

Commit 84365dd

Browse files
committed
feat: add new index, constraints on isQueued
Our queuing logic will be slow if we dont use an index. isQueued is most of the time true, since emailoutbox table accounts for historical data and all sent emails have isQueued set to true. So the index will actually be quite selective. We can't just use the partial index on isQueued because its build on tenancyId, and the queuing query doesnt check tenancy id.
1 parent 9dad2d8 commit 84365dd

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- SPLIT_STATEMENT_SENTINEL
2+
-- SINGLE_STATEMENT_SENTINEL
3+
-- RUN_OUTSIDE_TRANSACTION_SENTINEL
4+
-- Index on isQueued for efficient queueReadyEmails() queries.
5+
-- Most emails have isQueued=TRUE (already processed), so filtering for FALSE is highly selective.
6+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "EmailOutbox_isQueued_idx" ON /* SCHEMA_NAME_SENTINEL */."EmailOutbox" ("isQueued");

apps/backend/prisma/schema.prisma

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ model EmailOutbox {
833833
// The scheduled time of when the email should be added to the queue. Can be edited, but only if the email has not yet started sending. Doing so should set isQueued to false.
834834
scheduledAt DateTime
835835
836-
// The scheduled time of the email if it is in the future.
836+
// Whether the email has been queued for sending. Once queued, this stays TRUE.
837837
isQueued Boolean @default(false)
838838
839839
// A generated column that is equal to scheduledAt if isQueued is false, otherwise null. See the note above on EmailOutboxStatus.status for more details on dbgenerated values.
@@ -886,6 +886,7 @@ model EmailOutbox {
886886
@@index([tenancyId, finishedSendingAt(sort: Desc), scheduledAtIfNotYetQueued(sort: Desc), priority, id], map: "EmailOutbox_ordering_idx")
887887
@@index([tenancyId, simpleStatus], map: "EmailOutbox_simple_status_tenancy_idx")
888888
@@index([tenancyId, status], map: "EmailOutbox_status_tenancy_idx")
889+
@@index([isQueued], map: "EmailOutbox_isQueued_idx")
889890
}
890891

891892
model EmailOutboxProcessingMetadata {

0 commit comments

Comments
 (0)