Skip to content

perf: optimize SQLite PRAGMAs and add missing indexes#294

Merged
jalehman merged 6 commits intoMartian-Engineering:mainfrom
electricsheephq:perf/pragma-and-index-improvements
Apr 6, 2026
Merged

perf: optimize SQLite PRAGMAs and add missing indexes#294
jalehman merged 6 commits intoMartian-Engineering:mainfrom
electricsheephq:perf/pragma-and-index-improvements

Conversation

@100yenadmin
Copy link
Copy Markdown
Contributor

@100yenadmin 100yenadmin commented Apr 6, 2026

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)

PRAGMA Before After Impact
cache_size -2000 (2MB) -65536 (64MB) 32x larger page cache. Demand-allocated, released on close.
synchronous FULL (default) NORMAL ~50% write latency reduction. Officially recommended for WAL mode.
temp_store DEFAULT (disk) MEMORY Temp B-trees in RAM (helps ordinal resequencing)

Added PRAGMA optimize on connection close — updates query planner statistics for tables that changed during the session.

Missing Indexes (migration.ts)

CREATE INDEX IF NOT EXISTS summary_messages_message_idx ON summary_messages (message_id);
CREATE INDEX IF NOT EXISTS summaries_conv_kind_depth_idx ON summaries (conversation_id, kind, depth);

These eliminate full table scans on cascade deletes and condensation depth filtering.

Safety

  • synchronous = NORMAL in 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 = -65536 is 64MB per connection, demand-allocated. 5 connections = 320MB worst case — trivial on machines running Opus 4.6.
  • Indexes are additive (CREATE INDEX IF NOT EXISTS) — safe for existing databases.

Test plan

  • 197/197 tests pass locally
  • CI should pass (no logic changes)

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>
Copilot AI review requested due to automatic review settings April 6, 2026 16:00
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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 optimize on connection close.
  • Add missing indexes on summary_messages(message_id) and summaries(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.

Comment thread src/db/connection.ts
Comment thread src/db/connection.ts
Comment thread src/db/connection.ts
Comment thread src/db/migration.ts Outdated
Comment thread src/db/migration.ts Outdated
@100yenadmin
Copy link
Copy Markdown
Contributor Author

@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>
@100yenadmin
Copy link
Copy Markdown
Contributor Author

Part of the LCM Performance & Cache Optimization Sprint — see #297 for the full tracking issue linking all 5 PRs.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/db/connection.ts
Comment thread src/db/migration.ts Outdated
Comment thread src/db/migration.ts Outdated
Comment thread src/db/migration.ts Outdated
…overhead

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/db/migration.ts
Comment thread src/db/migration.ts
)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@100yenadmin
Copy link
Copy Markdown
Contributor Author

Merge order: 2nd — No dependencies. PRAGMAs + indexes — independent of all other PRs.

See #297 for the full sprint tracking issue with all 5 PRs.

Recommended merge sequence: #288#294#289#295#296

@jalehman jalehman merged commit 43342d9 into Martian-Engineering:main Apr 6, 2026
1 check passed
@jalehman
Copy link
Copy Markdown
Contributor

jalehman commented Apr 6, 2026

Thank you!

@github-actions github-actions Bot mentioned this pull request Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants