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)
This commit is contained in:
Jon Seager 2026-02-06 18:04:58 +00:00
parent 1bcadf1d3e
commit 494e0346bf
No known key found for this signature in database
3 changed files with 34 additions and 0 deletions

2
.gitignore vendored
View file

@ -2,6 +2,8 @@ target/
result* result*
*.db *.db
*.db-journal *.db-journal
*.db-shm
*.db-wal
TODO.md TODO.md
backup.json backup.json
*.env *.env

View file

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

View file

@ -33,6 +33,31 @@ impl Database {
.await .await
.context("failed to enable foreign keys for sqlite")?; .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 }; let db = Self { pool };
db.migrate().await?; db.migrate().await?;
Ok(db) Ok(db)