From eedfc498e736d0937c79901fed025f3cab3eefb7 Mon Sep 17 00:00:00 2001 From: 4gray Date: Fri, 1 May 2026 21:09:24 +0200 Subject: [PATCH] perf(database): run PRAGMA optimize before closing connections SQLite's recommended approach for keeping query plans current: cheap when nothing needs analyzing, runs incremental ANALYZE on tables/indexes that have grown significantly since the last run. Wrapped in try/catch since optimize is advisory and must never block connection close. Applied to both the main connection and the EPG worker connection. Co-Authored-By: Claude Opus 4.7 (1M context) Entire-Checkpoint: 3d6901072049 --- .../src/app/workers/database.worker-connection.ts | 6 ++++++ libs/shared/database/src/lib/connection.ts | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/apps/electron-backend/src/app/workers/database.worker-connection.ts b/apps/electron-backend/src/app/workers/database.worker-connection.ts index 6c883560a..de81d9781 100644 --- a/apps/electron-backend/src/app/workers/database.worker-connection.ts +++ b/apps/electron-backend/src/app/workers/database.worker-connection.ts @@ -100,6 +100,12 @@ export function closeWorkerDatabase(): void { return; } + try { + sqlite.pragma('optimize'); + } catch { + // Optimize is advisory; never block close on it. + } + sqlite.close(); if (isSqlTraceEnabled()) { diff --git a/libs/shared/database/src/lib/connection.ts b/libs/shared/database/src/lib/connection.ts index 6c632ec3f..e3fc06d4d 100644 --- a/libs/shared/database/src/lib/connection.ts +++ b/libs/shared/database/src/lib/connection.ts @@ -573,6 +573,12 @@ export async function getReadOnlyDatabase(): Promise { */ export function closeDatabase(): void { if (sqlite) { + try { + sqlite.pragma('optimize'); + } catch { + // Optimize is advisory; never block close on it. + } + sqlite.close(); if (isSqlTraceEnabled()) {