From 494e0346bf696d36ec44c85d0438f6417bb12063 Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Fri, 6 Feb 2026 18:04:58 +0000 Subject: [PATCH] perf(db): enable WAL mode, add tuning pragmas and missing indexes - Enable WAL journal mode for concurrent reads during writes - Set synchronous=NORMAL, cache_size=8MB, temp_store=MEMORY, busy_timeout=5s - Add indexes on brews.grinder_id, brews.brewer_id, brews.filter_paper_id to speed up the 6-table brew list JOIN - Gitignore WAL sidecar files (*.db-shm, *.db-wal) --- .gitignore | 2 ++ migrations/0005_brew_gear_indexes.sql | 7 +++++++ src/infrastructure/database.rs | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 migrations/0005_brew_gear_indexes.sql diff --git a/.gitignore b/.gitignore index afe9f11..3aa9c2e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ target/ result* *.db *.db-journal +*.db-shm +*.db-wal TODO.md backup.json *.env diff --git a/migrations/0005_brew_gear_indexes.sql b/migrations/0005_brew_gear_indexes.sql new file mode 100644 index 0000000..eaa21e8 --- /dev/null +++ b/migrations/0005_brew_gear_indexes.sql @@ -0,0 +1,7 @@ +-- Add missing indexes on brew foreign keys to gear table. +-- The brew list query joins gear 3 times (grinder, brewer, filter paper); +-- these indexes avoid full table scans on the gear side of each join. + +CREATE INDEX idx_brews_grinder_id ON brews(grinder_id); +CREATE INDEX idx_brews_brewer_id ON brews(brewer_id); +CREATE INDEX idx_brews_filter_paper_id ON brews(filter_paper_id); diff --git a/src/infrastructure/database.rs b/src/infrastructure/database.rs index c181d5f..7db90d8 100644 --- a/src/infrastructure/database.rs +++ b/src/infrastructure/database.rs @@ -33,6 +33,31 @@ impl Database { .await .context("failed to enable foreign keys for sqlite")?; + sqlx::query("PRAGMA journal_mode = WAL;") + .execute(&pool) + .await + .context("failed to enable WAL journal mode for sqlite")?; + + sqlx::query("PRAGMA synchronous = NORMAL;") + .execute(&pool) + .await + .context("failed to set synchronous mode for sqlite")?; + + sqlx::query("PRAGMA cache_size = -8000;") + .execute(&pool) + .await + .context("failed to set cache size for sqlite")?; + + sqlx::query("PRAGMA temp_store = MEMORY;") + .execute(&pool) + .await + .context("failed to set temp_store for sqlite")?; + + sqlx::query("PRAGMA busy_timeout = 5000;") + .execute(&pool) + .await + .context("failed to set busy_timeout for sqlite")?; + let db = Self { pool }; db.migrate().await?; Ok(db)