fix(server): collect SQLite planner statistics when the database opens (#18471)
This commit is contained in:
1 parent
c616e16c8f
commit
fb8d0fc19c
2 files changed
+34
No files matched your search
@@ -84,6 +84,36 @@ it.effect("shrinks the WAL file back to the size limit after a large write", ()
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("collects planner statistics for an existing database on open", () => {
|
||||
const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-stats-"));
|
||||
const dbPath = NodePath.join(tempDir, "state.sqlite");
|
||||
const persistence = () =>
|
||||
SqlitePersistence.layerFromPath(dbPath).pipe(Layer.provide(NodeServices.layer));
|
||||
|
||||
return Effect.gen(function* () {
|
||||
yield* Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* sql`CREATE TABLE stats_probe(id INTEGER PRIMARY KEY, thread_id TEXT NOT NULL)`;
|
||||
yield* sql`CREATE INDEX stats_probe_thread_idx ON stats_probe(thread_id)`;
|
||||
yield* sql`
|
||||
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 1000)
|
||||
INSERT INTO stats_probe(thread_id) SELECT 'thread-' || (i % 10) FROM n
|
||||
`;
|
||||
}).pipe(Effect.provide(persistence()));
|
||||
|
||||
// A database written before statistics existed gets them on the next open.
|
||||
yield* Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const rows = yield* sql<{ readonly stat: string }>`
|
||||
SELECT stat FROM sqlite_stat1 WHERE idx = 'stats_probe_thread_idx'
|
||||
`;
|
||||
assert.deepEqual([...rows], [{ stat: "1000 100" }]);
|
||||
}).pipe(Effect.provide(persistence()));
|
||||
}).pipe(
|
||||
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("applies busy_timeout in the shared persistence setup", () =>
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
|
||||
@@ -23,6 +23,10 @@ const layerSetup = Layer.effectDiscard(
|
||||
// largest size until the last connection closes.
|
||||
yield* sql.unsafe(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES};`);
|
||||
yield* runMigrations();
|
||||
// Without sqlite_stat1 the planner guesses at index selectivity. 0x10012
|
||||
// analyzes tables whose indexes lack stats or whose row count changed 10x,
|
||||
// under a temporary analysis_limit. Otherwise it only checks table sizes.
|
||||
yield* sql`PRAGMA optimize = 0x10012;`;
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user