Repository navigation
perf(search): return the newest page of a common word without sorting every match - #144
Merged
Merged
Conversation
… 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]>
This was referenced Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A newest-first
tg messages searchread 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,--chaton 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):
word*) always sorts.idx_messages_cache_newestonmessages_cache(date DESC)(oridx_messages_cache_datewith--chat), from--untilback, and asks FTS about each until the page is full. Scanned forward, both indexes givedate 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 withEXPLAIN 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.
--countjoins chats and senders only for--chat @name/--sender @name(same answer, about a third faster on a common word).--sort relevanceis unchanged.The index is built on the first open of an existing cache with
ensureIndex(4.3 s on the 10M bench cache), likeidx_messages_cache_message_id.Numbers (
bun run bench:storage, seed 1, main at e1ebad9 vs this branch)--offset 200, common word--chat, biggest chat--phrase, two common words--count, common word (page + count)--offset 200at 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 plainORDER BY m.date DESC, m.rowidover every match, forsearch,countand 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