From d90cfb3986028382ee47a6668d50bb4ae4243273 Mon Sep 17 00:00:00 2001 From: ScreenTinker Date: Sat, 27 Jun 2026 18:54:57 -0500 Subject: [PATCH] fix(#142): index device_status_log + de-dupe its CREATE TABLE The dashboard uptime query (WHERE device_id=? AND timestamp>?) and the per-device retention prune (WHERE device_id=? AND timestamp --- server/db/database.js | 5 +++++ server/db/schema.sql | 12 +++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/server/db/database.js b/server/db/database.js index 79b9941..dae8574 100644 --- a/server/db/database.js +++ b/server/db/database.js @@ -225,6 +225,11 @@ const migrations = [ "ALTER TABLE devices ADD COLUMN ota_target_version TEXT", "ALTER TABLE devices ADD COLUMN ota_attempts INTEGER DEFAULT 0", "ALTER TABLE devices ADD COLUMN ota_updated_at INTEGER", + // #142: index device_status_log for the per-device + time-window access pattern. + // schema.sql creates this on fresh installs; this migration covers existing DBs. + // Both the dashboard uptime query and the retention prune were full scans — the + // dashboard-degradation cause once the table reached 1M+ rows. + "CREATE INDEX IF NOT EXISTS idx_device_status_log_device_ts ON device_status_log(device_id, timestamp)", ]; // Apply each ALTER idempotently. A "duplicate column name" / "already exists" // error means the column is already present (expected on a migrated DB) - benign. diff --git a/server/db/schema.sql b/server/db/schema.sql index d0bcfcb..e4e67e0 100644 --- a/server/db/schema.sql +++ b/server/db/schema.sql @@ -463,6 +463,11 @@ CREATE TABLE IF NOT EXISTS device_status_log ( status TEXT NOT NULL, timestamp INTEGER NOT NULL DEFAULT (strftime('%s','now')) ); +-- #142: index the per-device + time-window access pattern. Both the dashboard +-- uptime query (WHERE device_id=? AND timestamp>?) and the retention prune +-- (WHERE device_id=? AND timestamp