Skip to content

perf(search): return the newest page of a common word without sorting every match - #144

Merged
neoromantic merged 3 commits into
mainfrom
perf/search-common-words
Nov 13, 2026
Merged

neoromantic merged 3 commits into
mainfrom
perf/search-common-words

Conversation

@neoromantic

@neoromantic neoromantic commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Problem

A newest-first tg messages search read every match, joined it with its message and sorted all of them before returning a page. A word in 30% of 10M messages took 3.0 s, --chat on the biggest chat 2.1 s, though the agent asking needs one page.

Change

The newest page comes one of two ways, with the same rows in the same order (ties in date by rowid, which is what SQLite gave before; now explicit):

  • Count, then sort. tg counts the matches in the FTS index up to T = √(900 × page × messages) (212,000 for a page of 50 at 1M, 671,000 at 10M). With fewer, it sorts them as before; a phrase or several words hand the rowids just read to the sort instead of asking FTS again. A prefix (word*) always sorts.
  • Walk. With T or more, tg walks the first page × messages / T rows of a new index idx_messages_cache_newest on messages_cache(date DESC) (or idx_messages_cache_date with --chat), from --until back, and asks FTS about each until the page is full. Scanned forward, both indexes give date DESC, rowid, so the window is a prefix of the page's order even when it ends inside a second; the outer query orders by the window's columns, so there is no temp B-tree sort (checked with EXPLAIN QUERY PLAN). If the page isn't full, it falls back to the sort.

Asking FTS about one message costs as much as sorting tens to hundreds of matches, which sets the 900: when the walk finds too few matches (a word only in old messages), counting and walking add at most about 30% to the sort.

--count joins chats and senders only for --chat @name / --sender @name (same answer, about a third faster on a common word). --sort relevance is unchanged.

The index is built on the first open of an existing cache with ensureIndex (4.3 s on the 10M bench cache), like idx_messages_cache_message_id.

Numbers (bun run bench:storage, seed 1, main at e1ebad9 vs this branch)

1M before 1M after 10M before 10M after
search, common word 315 ms 10 ms 3.0 s 21 ms
search --offset 200, common word 288 ms 296 ms 3.0 s 64 ms
search --chat, biggest chat 211 ms 9.2 ms 2.1 s 21 ms
CLI run, common word 374 ms 104 ms 3.1 s 114 ms
search, rare word 0.24 ms 0.29 ms 3.0 ms 2.8 ms
search --phrase, two common words 24 ms 24 ms 251 ms 235 ms
--count, common word (page + count) 574 ms 215 ms 5.9 s 1.9 s
History pages of 100, messages/s 8,797 6,922 8,420 6,639
Live messages, messages/s 6,315 5,762 5,632 5,378

--offset 200 at 1M sorts (its T is 474,000, above the word's 300,000 matches). Worst case, a word only in the oldest messages, main vs this branch on made-up caches written like history sync: 43 → 55 ms at 1M, 82 → 98 ms at 10M; a group of 940,000 messages sent in one second ahead of the matches: 9 → 10 ms.

Trade-off: history pages are written about a fifth slower (dates of a page land all over the new index); live writes within noise; +14 bytes per message (0.9%). Still ~70× faster than history sync receives messages from Telegram.

Tests

  • src/__tests__/messages-search-newest.test.ts: 15,000 made-up messages written like history sync (rowids don't follow dates, many ties), including 3,000 newest messages sent in one second, more than the window holds. 200 random queries and filters (--chat, @chat, --sender, --since/--until, offsets up to 1,000, deleted rows, OR, prefix) compared with a plain ORDER BY m.date DESC, m.rowid over every match, for search, count and the walk at a lower threshold; plus the page inside the one-second group and a word only in old messages.
  • bun run check; unit tests for search, schema and sql (138 pass); e2e for search, messages, sql and names (94 pass).

Fixes #118

🤖 Generated with Claude Code

neoromantic and others added 3 commits November 13, 2026 09:06
… every match

A newest-first search read every match, joined it with its message and
sorted all of them before the page: a word in 30% of 10M messages took
3.0 s. The newest page now comes one of three ways, with the same rows
in the same order (ties in date by rowid, as SQLite gave them before):

- Few matches (under sqrt(40 * page * messages)): read them from the
  index once and sort only them, as before.
- Many matches: go through the newest messages by a new index on
  messages_cache(date DESC), or the chat's date index with --chat, and
  ask the search index about each until the page is full; at most twice
  the messages expected, else sort every match.
- A prefix always sorts: asking about one message would read every word
  it starts.

--count joins chats and senders only when a filter reads them.

Refs #118

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bench:storage times the fifth page of the common word (--offset 200).
docs/storage.md has the before and after numbers at 1M and 10M, the
write cost of the new index, and the tables refreshed from the new runs.

Fixes #118

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The walk of #118 took every message from the window's last date on, so a
large group of messages sent in one second went into it whole: 10 s
instead of 9 ms for a word only in old messages. It now goes through the
first rows of the index, which give the page's order, so a page found
there is right even when the window ends inside a second.

A word above the threshold whose matches are all old walked, found
nothing and sorted everything: twice the time of the sort, seven times on
a cache written like history sync. The threshold rises from
sqrt(40 x P x N) to sqrt(900 x P x N) and the window to the messages a
page needs at that share, so counting and walking add at most about 30%.
Matches are counted in the index; a phrase or several words hand their
rowids to the sort instead of asking the index again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 479f46c into main Nov 13, 2026
5 checks passed
@neoromantic
neoromantic deleted the perf/search-common-words branch November 13, 2026 06:43
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.

Search for a common word takes seconds on a big cache

1 participant