Field Notes

SQLite FTS5 in practice

28 October 2026 · 6 min read

Search does not need a separate service until it does. For a few million rows of text, FTS5 inside SQLite will answer queries in single-digit milliseconds and save you an entire deployment. The cost is that you have to understand two things: how the index is kept in sync, and which queries still fall back to a scan.

Start with an external content table

The naive approach duplicates every row into the index. The better one points the index at the source table:

CREATE VIRTUAL TABLE notes_fts USING fts5(
  title, body,
  content='notes', content_rowid='id',
  tokenize='porter unicode61'
);

Now the index stores only the postings, and reads for display come from the original table. You are responsible for keeping it current, which means three triggers or one rebuild after every batch load.

Triggers, briefly

CREATE TRIGGER notes_ai AFTER INSERT ON notes BEGIN
  INSERT INTO notes_fts(rowid, title, body)
  VALUES (new.id, new.title, new.body);
END;

The delete case is the one people get wrong. An external-content table needs the special 'delete' command row, not a plain DELETE, or the postings are left behind and your index slowly fills with ghosts.

The query that scans

Ranking with bm25() is fine. Filtering on a column that is not part of the index is not:

-- index used
SELECT rowid FROM notes_fts WHERE notes_fts MATCH 'sqlite';

-- full scan over the joined table
SELECT n.* FROM notes_fts f JOIN notes n ON n.id = f.rowid
WHERE f MATCH 'sqlite' AND n.published = 1;

Put the filter in the source table's own index, or denormalise it into an unindexed column of the FTS table. Either way, measure with EXPLAIN QUERY PLAN before you ship.


← Back to all writing