8000 Search: read the field clock once per query, and don't look up each row's hash twice in FT.AGGREGATE KNN · Issue #670 · kividbio/kividb · GitHub
[go: up one dir, main page]

Skip to content

Search: read the field clock once per query, and don't look up each row's hash twice in FT.AGGREGATE KNN #670

Closed

Description

@murtazayusuf

Follow-ups from the review of #643 (kept out of it so it stays a correctness fix).

  1. field_now() (a clock read) runs once per document in a query. Most of the +3% on queries over hashes with field TTLs comes from that, and one query can judge two documents at different moments. Read it once per query and pass it down.
  2. FT.AGGREGATE with KNN looks up each row's hash a second time (src/commands/vector.rs around 826-828), +10.3% when indexed hashes have field TTLs. Reuse the first lookup.
  3. nearest_shown (src/commands/vector.rs ~1572-1592) keeps doubling the KNN search size up to the whole index and ignores the query TIMEOUT. Cap it at 2-3 rounds, then fall back to the exact filtered path.

Measure with callgrind (scripts/perf/run.sh or a search workload), with and without field TTLs.

Activity

  1. added this to the 1.3.0 milestone on Nov 12, 2026
  2. murtazayusuf commented on Nov 14, 2026

    @murtazayusuf
    ContributorAuthor

    Tagged good first issue for items 1 and 2 (item 3 needs more design, leave it out or do it in a separate PR). Both are local changes: read the field clock once per query and pass it down; reuse the first hash lookup in FT.AGGREGATE with KNN. A good PR includes before/after instruction counts from scripts/perf/run.sh (or a small search workload) with and without field TTLs, and the existing search tests passing. Comment here before starting.

  3. magic-peach commented on Nov 18, 2026

    @magic-peach
    Collaborator

    @murtazayusuf items 1 and 2 went in with #830. I'd like to take item 3 as its own PR. My plan:

    • nearest_shown gets at most 3 rounds (k, then 2k, then 4k, plus whatever was hidden). If it still has fewer than k visible hits, it returns that it ran out instead of growing to the whole index.
    • On that, the caller (FT.SEARCH at search.rs:2299, FT.HYBRID at hybrid.rs:676) falls back to the exact filtered path (FilteredKnn) with "field not hidden" as the filter. That gives the same k results the doubling would have found.
    • Each round and the fallback check the query deadline. A timeout goes through the existing ON_TIMEOUT handling: fail errors, and return gives what was found so far.
    • Tests: a query where most of the nearest hits have an expired vector field returns the same results as today after 3 rounds, and a small TIMEOUT on a large index with mostly hidden hits stops instead of scanning everything. Callgrind numbers with and without field TTLs, as in perf(search): read the field clock once per query, and each aggregate row's hash once #830.

    Does that match what you had in mind for the fallback and ON_TIMEOUT?

  4. murtazayusuf commented on Nov 18, 2026

    @murtazayusuf
    ContributorAuthor

    @magic-peach you're right, 1 and 2 went in with #830; sorry for pointing you at a half-done issue. Item 3 is yours, and the plan matches what I had in mind. A few points:

    • The fallback's results need not equal what the doubling would have found: the doubled search is approximate and FilteredKnn is exact, so where they differ the fallback is the better answer. Test for "the k nearest visible documents", not for equality with today's output.
    • Cover every caller of nearest_shown, not only FT.SEARCH and FT.HYBRID: check FT.AGGREGATE with KNN and any path through vector sets, and say in the PR which ones there are.
    • Deadline: check it between rounds and inside the fallback's scan at the same granularity the other search paths use, not once per document if that reads the clock (item 1's lesson). With no TIMEOUT given the default applies as elsewhere.
    • ON_TIMEOUT return: what was found so far must still be only visible documents, in distance order.
    • Numbers: callgrind for a plain KNN with no field TTLs (must not move), with TTLs but nothing hidden (must not move), and the mostly-hidden case before and after.
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

    good first issueGood for newcomersperformanceThroughput and latencysearchSearch, FT.*, vector search

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0