From f0996686e9e8825f1229f9df9953980d2819845e Mon Sep 17 00:00:00 2001 From: Matt Aitken Date: Wed, 6 Nov 2024 11:56:28 +0000 Subject: [PATCH] Added a db index to make the schedule list page faster There was a slow query to get the latest run for a list of schedules --- .../migration.sql | 2 ++ internal-packages/database/prisma/schema.prisma | 2 ++ references/hello-world/src/trigger/schedule.ts | 11 +++++++++++ 3 files changed, 15 insertions(+) create mode 100644 internal-packages/database/prisma/migrations/20241106112827_task_run_add_schedule_id_created_at_index/migration.sql create mode 100644 references/hello-world/src/trigger/schedule.ts diff --git a/internal-packages/database/prisma/migrations/20241106112827_task_run_add_schedule_id_created_at_index/migration.sql b/internal-packages/database/prisma/migrations/20241106112827_task_run_add_schedule_id_created_at_index/migration.sql new file mode 100644 index 0000000000..89c6278bcf --- /dev/null +++ b/internal-packages/database/prisma/migrations/20241106112827_task_run_add_schedule_id_created_at_index/migration.sql @@ -0,0 +1,2 @@ +-- CreateIndex +CREATE INDEX CONCURRENTLY IF NOT EXISTS "TaskRun_scheduleId_createdAt_idx" ON "TaskRun"("scheduleId", "createdAt" DESC); diff --git a/internal-packages/database/prisma/schema.prisma b/internal-packages/database/prisma/schema.prisma index cceb5572f4..d263409b52 100644 --- a/internal-packages/database/prisma/schema.prisma +++ b/internal-packages/database/prisma/schema.prisma @@ -1799,6 +1799,8 @@ model TaskRun { @@index([parentSpanId]) // Finding completed runs @@index([completedAt]) + // Schedule list page + @@index([scheduleId, createdAt(sort: Desc)]) } enum TaskRunStatus { diff --git a/references/hello-world/src/trigger/schedule.ts b/references/hello-world/src/trigger/schedule.ts new file mode 100644 index 0000000000..29c363d64c --- /dev/null +++ b/references/hello-world/src/trigger/schedule.ts @@ -0,0 +1,11 @@ +import { schedules } from "@trigger.dev/sdk/v3"; + +export const simpleSchedule = schedules.task({ + id: "simple-schedule", + cron: "0 0 * * *", + run: async (payload, { ctx }) => { + return { + message: "Hello, world!", + }; + }, +});