Skip to content

messages search --count with --chat and --since/--until runs one full-text match per message of the chat #227

Open

Description

@neoromantic

MessagesSearch.count() in src/db/messages-search.ts joins message_search with messages_cache. When the filters hold a chat ID and a date bound (--since or --until), SQLite starts from idx_messages_cache_date (chat_id, date DESC) and runs the FTS5 MATCH once for each message of the chat in that range. The cost grows with the size of the chat, not with the number of matches, so a rare word costs as much as a common one.

So tg messages search --count --chat <id> --until <date> is slow on a big chat. These keep the fast plan, which reads the FTS5 index first: --chat alone, a date bound alone, --chat @username, --sender and the allowedChats filter.

Plan

--chat alone, or --since/--until alone:
  SCAN message_search VIRTUAL TABLE INDEX 0:M4
  SEARCH m USING INTEGER PRIMARY KEY (rowid=?)

--chat <id> with --since or --until:
  SEARCH m USING INDEX idx_messages_cache_date (chat_id=? AND date<?)
  SCAN message_search VIRTUAL TABLE INDEX 0:=M4

The plan is the same on an empty cache and with either search index (contentless or regular). It depends on the shape of the filters, not on the data. Caches never run ANALYZE, so SQLite has no sqlite_stat1 to tell it that chat_id isn't selective.

Numbers

I built a made-up cache with the repo's own schema (initCacheSchema, initSyncSchema) and MessagesCache.upsertBatch. It holds 20,000 messages in one supergroup and 20,000 more in 20 other chats, all random filler words. "meeting" is in 30% of the messages and "zephyr" in 0.2%. Each figure is count() called directly. The runs used Bun 1.4.3 and SQLite 3.53.4 on a busy shared Linux machine, so the absolute times are noisy; the gap between the columns is what matters.

query (matches: cache / chat) no filter --chat --since or --until --chat + --until --chat + --since (newer half)
meeting (11,383 / 5,703) 8–13 ms 9–10 ms 9–10 ms 1.4–1.9 s 0.6–1.3 s
zephyr (255 / 125) 0.3 ms 0.2 ms 0.2 ms 1.4–1.5 s 0.5 s
comm* (11,369 / 5,691) 7–8 ms 6 ms 6–11 ms 13–23 s 6.7 s

The whole CLI on the same kind of cache:

  • tg messages search --query 'comm*' --count --chat=-1001000000001 --until 2024-01-01 took 19.7–20.2 s.
  • The same command without --count took 0.8–1.4 s.
  • --count and --chat without --until took 1.7–2.4 s. Most of that is Bun starting up on the busy machine.

--sort relevance with the same filters gets the same plan, plus a temp B-tree for the order. One 21-row page took 9.1 s for meeting and 34 s for comm*. The default newest-first walk uses its own INDEXED BY queries and isn't affected. Its sorted fallback is affected: a prefix query is always sorted, and a newest page of the rare prefix zeph* with --chat + --until took 0.9 s. With the FTS5 index first it takes 0.7 ms.

Options

Each option costs something, so the fix is a product decision:

  • Make FTS5 drive the join (CROSS JOIN in MATCHES_FROM, or only in count()). On this cache the count with --chat + --until went from 22.9 s to 9 ms for comm*, from 1.9 s to 9 ms for meeting and from 1.5 s to 0.7 ms for zephyr. The cost is that a dense sorted page then sorts every match: the newest page of meeting with --chat + --until went from 23 ms to 45 ms here. An earlier probe on a test fixture saw count time fall from 22.6 s to 3.2 s, while some dense sorted pages got slower. On a big cache, sorting every match is the cost Search for a common word takes seconds on a big cache #118 describes. Even for count alone, starting from FTS5 reads every match in the whole cache. For a small chat in a big cache, the current plan can be cheaper.
  • Run ANALYZE (or PRAGMA optimize) on caches. On this cache, after ANALYZE, every count plan read FTS5 first and every count took at most 13 ms. I haven't checked what it does to other queries' plans or how long it takes on a big cache.
  • Choose the plan in code, for example by the chat's message count from chat_sync_state against the number of matches.

Related: #118 (newest-first walk, and the cost of --count on big caches), #85 (added --count, --since and --until).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions