From b75771a445970a331e1fb67fd87a0f2ac7808b54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sat, 18 Jul 2026 18:04:26 +0200 Subject: [PATCH] perf(medications): index the dashboard meds-today read shapes Two additive indexes on medication_intake_events: (user_id, scheduled_for) for the sibling reads that filter without medication_id, and (user_id, medication_id, taken_at) for the per-medication last-taken group-by that scanned the whole intake history on every dashboard load. Additive migration 0253, no data change, no behaviour change. --- .../0253_perf_medication_intake_indexes/migration.sql | 11 +++++++++++ prisma/schema.prisma | 6 ++++++ 2 files changed, 17 insertions(+) create mode 100644 prisma/migrations/0253_perf_medication_intake_indexes/migration.sql diff --git a/prisma/migrations/0253_perf_medication_intake_indexes/migration.sql b/prisma/migrations/0253_perf_medication_intake_indexes/migration.sql new file mode 100644 index 000000000..217b1b8c3 --- /dev/null +++ b/prisma/migrations/0253_perf_medication_intake_indexes/migration.sql @@ -0,0 +1,11 @@ +-- v1.30.12 perf — index the two medication-intake read shapes the dashboard +-- "meds today" builder fires that could not index-seek before: the sibling +-- reads filtering (user_id, scheduled_for) without medication_id, and the +-- per-medication last-taken group-by that scanned the whole intake history. +-- Additive; no data change. + +-- CreateIndex +CREATE INDEX "medication_intake_events_user_id_scheduled_for_idx" ON "medication_intake_events"("user_id", "scheduled_for"); + +-- CreateIndex +CREATE INDEX "medication_intake_events_user_id_medication_id_taken_at_idx" ON "medication_intake_events"("user_id", "medication_id", "taken_at"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 31162b780..ca42d421f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -2733,6 +2733,12 @@ model MedicationIntakeEvent { // v1.7.0 sync — keyset support for the `/api/sync/changes` intake feed // (`where userId` ordered by `(updatedAt asc, id asc)`). @@index([userId, updatedAt, id]) + // v1.30.12 perf — the dashboard "meds today" read fires sibling queries that + // filter `(userId, scheduledFor)` WITHOUT `medicationId` (the composite above + // cannot range-seek those) and a per-medication `_max(takenAt)` group-by that + // scanned the whole intake history. These two let both index-seek instead. + @@index([userId, scheduledFor]) + @@index([userId, medicationId, takenAt]) @@map("medication_intake_events") }