From afe280cfc86096f447c9ace92d69e4fa31aad0cd Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Fri, 24 Jul 2026 11:27:59 +0100 Subject: [PATCH] perf(database): index BatchTaskRun on (runtimeEnvironmentId, createdAt, id) for the batches list The batches list page orders by createdAt DESC, id DESC filtered by environment and a created-at window, but the only matching index was (runtimeEnvironmentId, id), which cannot satisfy the createdAt ordering. On environments with many batches the query fell back to a full table scan and in-memory sort, which could run long enough to hit the statement timeout. Adds (runtimeEnvironmentId, createdAt DESC, id DESC) so the query reads straight from the index in order. Created with CONCURRENTLY IF NOT EXISTS, so it takes no table lock and is a no-op if already present. --- .server-changes/batch-list-created-at-index.md | 6 ++++++ .../migration.sql | 2 ++ internal-packages/database/prisma/schema.prisma | 1 + 3 files changed, 9 insertions(+) create mode 100644 .server-changes/batch-list-created-at-index.md create mode 100644 internal-packages/database/prisma/migrations/20260724120000_add_batch_task_run_created_at_dashboard_index/migration.sql diff --git a/.server-changes/batch-list-created-at-index.md b/.server-changes/batch-list-created-at-index.md new file mode 100644 index 0000000000..b5a2438661 --- /dev/null +++ b/.server-changes/batch-list-created-at-index.md @@ -0,0 +1,6 @@ +--- +area: webapp +type: fix +--- + +Speed up the Batches list page for environments with a large number of batches, which could previously time out while loading. diff --git a/internal-packages/database/prisma/migrations/20260724120000_add_batch_task_run_created_at_dashboard_index/migration.sql b/internal-packages/database/prisma/migrations/20260724120000_add_batch_task_run_created_at_dashboard_index/migration.sql new file mode 100644 index 0000000000..4ff6ed34bc --- /dev/null +++ b/internal-packages/database/prisma/migrations/20260724120000_add_batch_task_run_created_at_dashboard_index/migration.sql @@ -0,0 +1,2 @@ +-- CreateIndex +CREATE INDEX CONCURRENTLY IF NOT EXISTS "BatchTaskRun_runtimeEnvironmentId_createdAt_id_idx" ON "public"."BatchTaskRun"("runtimeEnvironmentId", "createdAt" DESC, "id" DESC); diff --git a/internal-packages/database/prisma/schema.prisma b/internal-packages/database/prisma/schema.prisma index 2800dd5dd2..7d6f4ac549 100644 --- a/internal-packages/database/prisma/schema.prisma +++ b/internal-packages/database/prisma/schema.prisma @@ -1935,6 +1935,7 @@ model BatchTaskRun { @@index([dependentTaskAttemptId]) // This is for the batch list dashboard page @@index([runtimeEnvironmentId, id(sort: Desc)]) + @@index([runtimeEnvironmentId, createdAt(sort: Desc), id(sort: Desc)]) } enum BatchTaskRunStatus {