perf: optimize SQLite PRAGMAs and add missing indexes#294
Conversation
Zero-logic-change performance improvements for multi-GB databases with concurrent agent sessions. PRAGMAs added to configureConnection(): - cache_size = -65536 (64MB page cache, up from 2MB default) Demand-allocated, released on close. 5 connections = 320MB max. - synchronous = NORMAL (officially recommended for WAL mode) Crash-safe for app crashes; only risks power-failure data loss. Bootstrap re-ingests any lost transactions from session files. - temp_store = MEMORY (keeps temp B-trees in RAM) Added PRAGMA optimize on connection close to update query planner statistics for tables that changed during the session. Missing indexes (cause full table scans on large databases): - summary_messages(message_id) — needed for cascade delete lookups - summaries(conversation_id, kind, depth) — needed for condensation depth filtering queries Fixes Martian-Engineering#291 (partial — PRAGMA + index portion) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The summaries(conversation_id, kind, depth) index references the depth column which is added by ensureSummaryDepthColumn(). The index was in the initial schema creation (too early). Moved it to run right after the depth column migration. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR tunes SQLite connection settings and adds a couple of indexes to improve performance on large (multi-GB) LCM databases, targeting reduced latency and fewer table scans under concurrent agent sessions (partial fix for #291).
Changes:
- Adjust SQLite connection PRAGMAs (cache size, synchronous mode, temp storage) and run
PRAGMA optimizeon connection close. - Add missing indexes on
summary_messages(message_id)andsummaries(conversation_id, kind, depth).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/db/connection.ts |
Sets additional SQLite PRAGMAs for performance and runs PRAGMA optimize during connection close. |
src/db/migration.ts |
Adds two new indexes intended to speed FK checks/deletes and depth-related summary queries. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@jalehman Ready for merge — CI green, zero logic changes (PRAGMAs + indexes only). |
…ing, index order, comments 1. PRAGMA optimize in separate try block so SQLITE_BUSY doesn't skip db.close() (handle leak prevention). 2. Index column order: (conversation_id, depth, kind) instead of (conversation_id, kind, depth) — matches getDistinctDepthsInContext query pattern which filters by conversation_id + depth. 3. Fixed misleading comment on summary_messages index. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Part of the LCM Performance & Cache Optimization Sprint — see #297 for the full tracking issue linking all 5 PRs. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…overhead Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Thank you! |
Summary
Zero-logic-change SQLite tuning for multi-GB databases with concurrent agent sessions. 16 lines changed, no behavioral differences.
Partial fix for #291
Changes
PRAGMAs (connection.ts)
cache_sizesynchronoustemp_storeAdded
PRAGMA optimizeon connection close — updates query planner statistics for tables that changed during the session.Missing Indexes (migration.ts)
These eliminate full table scans on cascade deletes and condensation depth filtering.
Safety
synchronous = NORMALin WAL mode is crash-safe for application crashes. Only risks data loss on power failure (OS/kernel crash). LCM's bootstrap process re-ingests any lost transactions from session files (the source of truth).cache_size = -65536is 64MB per connection, demand-allocated. 5 connections = 320MB worst case — trivial on machines running Opus 4.6.CREATE INDEX IF NOT EXISTS) — safe for existing databases.Test plan