Tune SQLite caches and maintain planner statistics
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import type Database from 'better-sqlite3';
|
||||
|
||||
export function tuneConnection(handle: Database.Database, reader = false): void {
|
||||
handle.pragma(`cache_size = -${reader ? 32768 : 131072}`);
|
||||
handle.pragma('temp_store = MEMORY');
|
||||
// mmap is opt-in because the controller data directory can be a network mount.
|
||||
const mmapMiB = Number(process.env.EC_SQLITE_MMAP_MB ?? 0);
|
||||
if (Number.isSafeInteger(mmapMiB) && mmapMiB >= 0 && mmapMiB <= 32768) {
|
||||
handle.pragma(`mmap_size = ${mmapMiB * 1024 * 1024}`);
|
||||
}
|
||||
if (!reader) handle.pragma('journal_size_limit = 67108864');
|
||||
}
|
||||
@@ -12,9 +12,11 @@ import { migrateServerHistory } from './server-history.js';
|
||||
import { migrateNativeInstances } from '../backends/schema.js';
|
||||
import { installPerformanceIndexes } from './performance-indexes.js';
|
||||
import { migrateVersionLayout } from './version-layout.js';
|
||||
import { tuneConnection } from './connection.js';
|
||||
import { timeStatement } from './slow-statements.js';
|
||||
|
||||
let _db: Database.Database | null = null;
|
||||
let optimizeTimer: NodeJS.Timeout | undefined;
|
||||
|
||||
// Desired shape of the `servers` table. Every column here is added to the
|
||||
// table on boot if missing — introspection via PRAGMA table_info handles
|
||||
@@ -316,6 +318,7 @@ export function db(): Database.Database {
|
||||
try {
|
||||
handle.pragma('journal_mode = WAL');
|
||||
handle.pragma('foreign_keys = ON');
|
||||
tuneConnection(handle);
|
||||
dropLegacyBetaTables(handle);
|
||||
handle.exec(schemaSql);
|
||||
migrateProvisioningActions(handle);
|
||||
@@ -409,6 +412,7 @@ export function db(): Database.Database {
|
||||
ON server_deployments(id) WHERE state NOT IN ('done','warning','cancelled');`);
|
||||
installPerformanceIndexes(handle);
|
||||
installReportRevisions(handle);
|
||||
handle.pragma('optimize = 0x10002');
|
||||
} catch (err) {
|
||||
handle.close();
|
||||
throw err;
|
||||
@@ -431,10 +435,17 @@ export function db(): Database.Database {
|
||||
}) as typeof handle.prepare;
|
||||
|
||||
_db = handle;
|
||||
optimizeTimer = setInterval(() => {
|
||||
try { handle.pragma('optimize'); }
|
||||
catch (error) { console.error('[database] optimize failed:', error); }
|
||||
}, 60 * 60_000);
|
||||
optimizeTimer.unref();
|
||||
return _db;
|
||||
}
|
||||
|
||||
export function closeDb(): void {
|
||||
clearInterval(optimizeTimer);
|
||||
optimizeTimer = undefined;
|
||||
if (_db) {
|
||||
_db.close();
|
||||
_db = null;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { workerData } from 'node:worker_threads';
|
||||
import { tuneConnection } from '../db/connection.js';
|
||||
|
||||
const handle = new Database(workerData.dbPath, { readonly: true, fileMustExist: true });
|
||||
tuneConnection(handle, true);
|
||||
export function db() { return handle; }
|
||||
|
||||
Reference in New Issue
Block a user