Sync reads the whole exchanges table once per archived file: no index on exchanges.archive_path

Open Beginner friendly
#216 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
sqlite, typescript

Research direction

Read initDatabase in src/db.ts and check how it creates the existing indexes. Add the suggested index on exchanges.archive_path, then verify that both lookups described in the issue can use it and that sync completes on an existing database. The issue reports that building the index on a large database can take several minutes.

Written by the indexing model from the issue text.

Description

What happens

On a machine with a large index, sync does not finish. For each archived conversation file, sync runs SELECT COALESCE(MAX(line_end), 0) FROM exchanges WHERE archive_path = ? (the high-water mark from #152), and getFileLastIndexed in src/db.ts filters on the same column. exchanges has no index on archive_path, so SQLite reads the whole table for every file.

Measured on v1.6.0 (Ubuntu 24.04, SQLite 3.45.1)

  • The database is 11.5 GB, with 317,412 rows in exchanges and 1,396 archived files. About 1.3 GiB of memory was free for the page cache, so each full read came from disk.
  • Before: EXPLAIN QUERY PLAN showed SEARCH exchanges with no index. One lookup ran for more than 300 s without finishing. One background sync ran for 17 hours, read 1,052 GB from disk and did not finish.
  • After CREATE INDEX idx_archive_path ON exchanges(archive_path), which took 16 minutes to build on this database:
    • the plan shows SEARCH exchanges USING INDEX idx_archive_path (archive_path=?);
    • one lookup takes 0.01 s;
    • a full sync finished in 749 s (39 files copied and indexed, 1,357 skipped).

Suggested fix

Add the index with the others in initDatabase:

CREATE INDEX IF NOT EXISTS idx_archive_path ON exchanges(archive_path);

On an existing large database, the first start after the update spends a few minutes building it.

Related, not fixed by the index

Text search (mode text or both) matches each word with LIKE '%word%' against user_message and assistant_message, so it also reads most of the table. On this database, one default search ran for more than 7 minutes and was stopped, and one --vector search took 131 s, including loading the embedding model. An FTS5 table could serve the text side.

Dominant language
TypeScript
Stars
511
Forks
154
Avg merge
14d 20h
Merged PRs (30d)
6

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from obra/episodic-memory

All issues in obra/episodic-memory

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.