From a0cff84339ff31ecf0f1ba59d65dd1e728320f0a Mon Sep 17 00:00:00 2001 From: Paijo <14921983+oyi77@users.noreply.github.com> Date: Sat, 18 Jul 2026 21:33:49 +0700 Subject: [PATCH] perf(db): add temp_store=MEMORY pragma to SQLite init (#6921) * perf(db): add temp_store=MEMORY pragma to SQLite init Store temp tables/indices in memory instead of disk for faster query execution (GROUP BY, ORDER BY, subquery materialization). The two other optimized PRAGMAs (synchronous=NORMAL, cache_size=-16384) were already set. * test(db): add temp_store MEMORY pragma test Verifies PRAGMA temp_store = 2 (MEMORY) after initDb() runs. --------- Co-authored-by: oyi77 --- src/lib/db/core.ts | 1 + tests/unit/db-core-temp-store-pragma.test.ts | 21 ++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/unit/db-core-temp-store-pragma.test.ts diff --git a/src/lib/db/core.ts b/src/lib/db/core.ts index 69bd022ca5..f4e80e1754 100644 --- a/src/lib/db/core.ts +++ b/src/lib/db/core.ts @@ -1163,6 +1163,7 @@ export function getDbInstance(): SqliteDatabase { db.pragma("busy_timeout = 2000"); db.pragma("synchronous = NORMAL"); db.pragma(`cache_size = -${DEFAULT_DATABASE_SETTINGS.optimization.cacheSize}`); + db.pragma("temp_store = MEMORY"); db.exec(SCHEMA_SQL); ensureProviderConnectionsColumns(db); ensureUsageHistoryColumns(db); diff --git a/tests/unit/db-core-temp-store-pragma.test.ts b/tests/unit/db-core-temp-store-pragma.test.ts new file mode 100644 index 0000000000..3b23f67049 --- /dev/null +++ b/tests/unit/db-core-temp-store-pragma.test.ts @@ -0,0 +1,21 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-temp-store-")); +process.env.DATA_DIR = TEST_DATA_DIR; +process.env.API_KEY_SECRET = "test-temp-store-secret"; + +const { getDbInstance } = await import("../../src/lib/db/core.ts"); + +test("temp_store pragma is 2 (MEMORY) after initDb", () => { + // The database singleton should already have the pragma set by initDb(). + // This test confirms the runtime respects the setting. + const db = getDbInstance(); + const val: unknown = db.pragma("temp_store", { simple: true }); + assert.ok(typeof val === "number"); + // 2 = MEMORY (set by the new PRAGMA in initDb) + assert.equal(val, 2, "expected temp_store=2 (MEMORY) after initDb"); +});