From e33cb70decd58997d820e6c357afbb803bc06afd Mon Sep 17 00:00:00 2001 From: 4gray Date: Fri, 1 May 2026 21:02:52 +0200 Subject: [PATCH] perf(database): add SQLite performance pragmas (synchronous, cache, mmap) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pair the existing journal_mode=WAL with the companion pragmas it needs to actually pay off, and reduce read latency on hot query paths. - synchronous=NORMAL: ~2-10x faster writes when paired with WAL. Safe — only risks losing the last committed txn on power loss; DB stays consistent. - cache_size=-64000: 64MB page cache (default is 2MB). - temp_store=MEMORY: keep sort/group temp tables in RAM. - mmap_size=268435456: 256MB memory-mapped I/O for reads. Applied to both connection sites: the main read-write/read-only connection in libs/shared/database and the EPG worker connection in electron-backend. The per-connection pragmas (cache_size, temp_store, mmap_size) apply to the read-only agent-backend connection too. Inspired by matracey/iptvnator@4ad8f88; ported manually since the file has diverged significantly and the worker connection didn't exist in that fork. Co-Authored-By: Claude Opus 4.7 (1M context) Entire-Checkpoint: 3d6901072049 --- .../src/app/workers/database.worker-connection.ts | 4 ++++ libs/shared/database/src/lib/connection.ts | 5 +++++ 2 files changed, 9 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 8e6d79d78..6c883560a 100644 --- a/apps/electron-backend/src/app/workers/database.worker-connection.ts +++ b/apps/electron-backend/src/app/workers/database.worker-connection.ts @@ -80,6 +80,10 @@ export async function getWorkerDatabase(): Promise { sqlite.pragma('foreign_keys = ON'); sqlite.pragma('journal_mode = WAL'); sqlite.pragma('busy_timeout = 5000'); + sqlite.pragma('synchronous = NORMAL'); + sqlite.pragma('cache_size = -64000'); + sqlite.pragma('temp_store = MEMORY'); + sqlite.pragma('mmap_size = 268435456'); if (isSqlTraceEnabled()) { trace('sql-worker', 'open', { diff --git a/libs/shared/database/src/lib/connection.ts b/libs/shared/database/src/lib/connection.ts index 9ca1e003b..6c632ec3f 100644 --- a/libs/shared/database/src/lib/connection.ts +++ b/libs/shared/database/src/lib/connection.ts @@ -527,8 +527,13 @@ export async function initDatabase( if (!readonly) { sqlite.pragma('journal_mode = WAL'); + sqlite.pragma('synchronous = NORMAL'); } + sqlite.pragma('cache_size = -64000'); + sqlite.pragma('temp_store = MEMORY'); + sqlite.pragma('mmap_size = 268435456'); + // Create tables only for read-write connections if (!readonly && !skipTableCreation) { createTables(sqlite);