Skip to content

Commit 5e24480

Browse files
committed
Improved TaskRun environment indexes
### PR: Optimize **TaskRun** indexes for hot-path queries **What changed** | Object | Type | Purpose | | ------------------------------- | --------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | | `taskrun_runtime_id_desc_idx` | **BTREE** `(runtimeEnvironmentId, id DESC) INCLUDE (createdAt)` | Eliminates explicit sort for the “latest task runs” query (`ORDER BY id DESC`) while remaining index-only. | | `taskrun_runtime_createdat_idx` | **BTREE** `(runtimeEnvironmentId, createdAt DESC) INCLUDE (id)` | Accelerates the filter-only path that scans by `createdAt >= …` without any ordering requirement. | | `taskrun_createdat_brin` | **BRIN** on `createdAt` (`pages_per_range = 128`) | Lets the planner skip whole blocks older than the time window for both queries at < 100 MB cost. | | *(cleanup)* | **DROP** `TaskRun_runtimeEnvironmentId_createdAt_id_idx` | Retires the 3-column index once the new ones are built. | **Key details** * All indexes created **CONCURRENTLY** to avoid write blocking. * `fillfactor = 90` on b-trees for balanced space vs. future growth. * Net disk usage drops **≈ 15–20 GB** while each query now gets a purpose-built access path. **Why** * Remove planner Sort nodes for the top-N “latest runs” view. * Speed up environment-filtered range scans. * Shrink index bloat and improve cache efficiency.
1 parent 48e1e72 commit 5e24480

File tree

5 files changed

+9
-2
lines changed
  • internal-packages/database/prisma

5 files changed

+9
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX CONCURRENTLY IF EXISTS "TaskRun_runtimeEnvironmentId_createdAt_id_idx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "TaskRun_runtimeEnvironmentId_id_idx" ON "TaskRun"("runtimeEnvironmentId", "id" DESC) INCLUDE ("createdAt") WITH (fillfactor = 90);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "TaskRun_runtimeEnvironmentId_createdAt_idx" ON "TaskRun"("runtimeEnvironmentId", "createdAt" DESC) INCLUDE ("id") WITH (fillfactor = 90);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "TaskRun_createdAt_idx" ON "TaskRun" USING BRIN ("createdAt");

internal-packages/database/prisma/schema.prisma

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,9 +680,12 @@ model TaskRun {
680680
// Schedule list page
681681
@@index([scheduleId, createdAt(sort: Desc)])
682682
// Finding runs in a batch
683-
@@index([runtimeEnvironmentId, batchId])
684-
@@index([runtimeEnvironmentId, createdAt(sort: Desc), id(sort: Desc)])
685683
@@index([runTags(ops: ArrayOps)], type: Gin)
684+
@@index([runtimeEnvironmentId, batchId])
685+
// This will include the createdAt index to help speed up the run list page
686+
@@index([runtimeEnvironmentId, id(sort: Desc)])
687+
@@index([runtimeEnvironmentId, createdAt(sort: Desc)])
688+
@@index([createdAt], type: Brin)
686689
}
687690

688691
enum TaskRunStatus {

0 commit comments

Comments
 (0)