From c837e5494cff3ebcccf2f63354f46fc45b0febd6 Mon Sep 17 00:00:00 2001 From: KM Koushik Date: Tue, 17 Nov 2026 11:12:13 +1100 Subject: [PATCH] perf(web): index Email by team and date, so team email lists and counts stop scanning the whole table Every query that lists or counts a team's emails filtered on teamId, which no Email index covered: the dashboard email list, search and its status/domain filters, the public API's GET /emails and its count, and the email export. Postgres read the whole Email table (1.5 GB) for them, about 55 times a day, which kept ~2.2 GB of it in the page cache and made these requests take seconds (pg_stat_statements: dashboard list 4.1 s mean over 5,187 calls; search 9.5 s; API counts 0.5-2 s). Adds (teamId, createdAt DESC) and (teamId, domainId, createdAt DESC). Each migration is a single CREATE INDEX CONCURRENTLY IF NOT EXISTS, so writes aren't blocked while it builds, and it is a no-op where the index already exists (useSend cloud builds them by hand first). Tested on a local Postgres 16 with 1.38M synthetic rows shaped like production: the dashboard and API queries go from reading the whole table (161k pages) to tens or hundreds of pages; a big team's full count becomes an index-only scan after vacuum. --- .../migration.sql | 6 ++++++ .../migration.sql | 6 ++++++ apps/web/prisma/schema.prisma | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 apps/web/prisma/migrations/20261117010000_email_team_created_at_index/migration.sql create mode 100644 apps/web/prisma/migrations/20261117010100_email_team_domain_created_at_index/migration.sql diff --git a/apps/web/prisma/migrations/20261117010000_email_team_created_at_index/migration.sql b/apps/web/prisma/migrations/20261117010000_email_team_created_at_index/migration.sql new file mode 100644 index 000000000..bbbc2512d --- /dev/null +++ b/apps/web/prisma/migrations/20261117010000_email_team_created_at_index/migration.sql @@ -0,0 +1,6 @@ +-- CreateIndex +-- Built CONCURRENTLY so sending isn't blocked while it builds on a large +-- "Email" table. CONCURRENTLY can't run inside a transaction, so this must be +-- the only statement in the file. IF NOT EXISTS: on useSend cloud the index was +-- created by hand before this shipped. +CREATE INDEX CONCURRENTLY IF NOT EXISTS "Email_teamId_createdAt_idx" ON "Email"("teamId", "createdAt" DESC); diff --git a/apps/web/prisma/migrations/20261117010100_email_team_domain_created_at_index/migration.sql b/apps/web/prisma/migrations/20261117010100_email_team_domain_created_at_index/migration.sql new file mode 100644 index 000000000..8c595775a --- /dev/null +++ b/apps/web/prisma/migrations/20261117010100_email_team_domain_created_at_index/migration.sql @@ -0,0 +1,6 @@ +-- CreateIndex +-- Built CONCURRENTLY so sending isn't blocked while it builds on a large +-- "Email" table. CONCURRENTLY can't run inside a transaction, so this must be +-- the only statement in the file. IF NOT EXISTS: on useSend cloud the index was +-- created by hand before this shipped. +CREATE INDEX CONCURRENTLY IF NOT EXISTS "Email_teamId_domainId_createdAt_idx" ON "Email"("teamId", "domainId", "createdAt" DESC); diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma index 67e44c428..49e4c59ea 100644 --- a/apps/web/prisma/schema.prisma +++ b/apps/web/prisma/schema.prisma @@ -300,6 +300,11 @@ model Email { @@index([campaignId, contactId]) @@index([createdAt(sort: Desc)]) + // A team's emails, newest first: the dashboard email list and search, the + // public API's GET /emails (and its count), and the email export. + @@index([teamId, createdAt(sort: Desc)]) + // The same, filtered by domain (domain-restricted API keys, domain filter). + @@index([teamId, domainId, createdAt(sort: Desc)]) } model CampaignEmail {