From 9ad315c3c7aa8ae7530ff699fc481883510cc234 Mon Sep 17 00:00:00 2001 From: Kanishk Sachdev Date: Wed, 17 Jun 2026 03:15:57 -0400 Subject: [PATCH] perf: add covering index for monitor-bars status aggregation --- ...0000_add_monitoring_data_covering_index.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 migrations/20260617120000_add_monitoring_data_covering_index.ts diff --git a/migrations/20260617120000_add_monitoring_data_covering_index.ts b/migrations/20260617120000_add_monitoring_data_covering_index.ts new file mode 100644 index 00000000..7163b59b --- /dev/null +++ b/migrations/20260617120000_add_monitoring_data_covering_index.ts @@ -0,0 +1,28 @@ +import type { Knex } from "knex"; + +export async function up(knex: Knex): Promise { + // Covering index for the grouped status-count aggregation used by the + // monitor-bars dashboard endpoint. The query filters by + // (monitor_tag, timestamp range) and reads status + latency for every row. + // Including status and latency in the index lets the database satisfy the + // query from the index alone, avoiding a heap lookup per matched row. + try { + await knex.schema.alterTable("monitoring_data", (table) => { + table.index( + ["monitor_tag", "timestamp", "status", "latency"], + "idx_monitoring_data_monitor_tag_timestamp_status_latency", + ); + }); + } catch (_e) { + /* index already exists */ + } +} + +export async function down(knex: Knex): Promise { + await knex.schema.alterTable("monitoring_data", (table) => { + table.dropIndex( + ["monitor_tag", "timestamp", "status", "latency"], + "idx_monitoring_data_monitor_tag_timestamp_status_latency", + ); + }); +}