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 <oyi77@users.noreply.github.com>
This commit is contained in:
Paijo
2026-07-18 21:33:49 +07:00
committed by GitHub
parent e79de5c294
commit a0cff84339
2 changed files with 22 additions and 0 deletions

View File

@@ -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);

View File

@@ -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");
});