Skip to content

Vector search stays an exact scan, with the measurements that rule out an HNSW index - #131

Merged
zlareb1 merged 12 commits into
mainfrom
feat/vector-similarity-index
Oct 21, 2026
Merged

zlareb1 merged 12 commits into
mainfrom
feat/vector-similarity-index

Conversation

@zlareb1

@zlareb1 zlareb1 commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Related: #17

Changelog category (leave one):

  • Documentation (changelog entry is not required)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

...

Why

Semantic search is an exact cosineDistance scan over a tenant's rows, so its cost grows with namespace size. This evaluated a vector_similarity (HNSW) index on memory_embeddings and on the knowledge and chunk tables, which share the read shape: every vector table's sort key starts with (org, namespace), and every read filters on it. The index does not hold up, so no migration or setting ships. docs/architecture/clickhouse.md gains "Why there is no vector index" with the numbers below, and bench/vector_index_vs_exact.py reproduces them.

Setup.

  • ClickHouse 26.9.19.9.
  • 1M synthetic 1536-dimension BFloat16 vectors: 500 clusters around a 16-dimensional latent space.
  • Four tables shaped like memory_embeddings (its sort key, ReplacingMergeTree, index_granularity = 256, one merged part per partition):
    • plain;
    • Quantized-coded;
    • HNSW ('hnsw', 'cosineDistance', 1536, 'bf16', 16, 128: M 16, ef_construction 128, GRANULARITY 100000000);
    • HNSW with one partition per tenant.
  • Tenants hold 50%, 10%, 1% and 0.1% of the rows; the rest spread over 1000 namespaces.
  • 30 queries per filter. Each query is a noisy copy of a row the filter admits, with seeds sampled by hash.
  • recall@10 is measured against the exact top 10 under the same filter.
  • "Index used" and cache misses come from system.query_log ProfileEvents (USearchSearchCount, VectorSimilarityIndexCacheMisses), with tags unique to the run.

1. A tenant read never searches the index. ClickHouse searches the graph only when the primary-key condition keeps every granule of a part. In every vector table but code_chunks a part holds many tenants, so a (org, namespace) filter prunes to the tenant's granules and the read sorts them exactly. code_chunks is partitioned by (org, namespace), so an index there would run: points 2 and 3 are what rule it out. Index searches were zero for every tenant, and rows and bytes read matched the plain table, under every vector_search_filter_strategy, multiplier and ef tried. MiB read and p50, every read under FINAL:

Tenant Rows Exact With HNSW Codes-first, LIMIT 60
50% 500k 1,478 MiB, 141 ms 1,478 MiB, 140 ms 134 MiB, 55 ms
10% 100k 295 MiB, 491 ms 295 MiB, 476 ms 49 MiB, 44 ms
1% 10k 30 MiB, 57 ms 30 MiB, 55 ms 21 MiB, 18 ms
0.1% 1k 3.2 MiB, 12 ms 3.2 MiB, 13 ms 3.4 MiB, 9 ms

The codes-first column uses the fact leg's shape: 60 rows, codes multiplier 1. recall@10 of those 60 rows against the exact top 10 was 0.95-1.00. The 10% tenant's exact read is slower than the 50% one in both runs, so compare bytes rather than milliseconds.

2. Where the index does run, a selective filter loses recall. With a predicate outside the sort key (cityHash64(memory_key) % 1000 < n), the graph runs first and the predicate post-filters its candidates. recall@10 by vector_search_index_fetch_multiplier:

Filter admits x1 x10 x100
all 0.783 0.783 0.783
10% 0.200 0.820 0.933
1% 0.080 0.163 0.723
  • The unfiltered row is the graph's own recall on this data at ef = 256; it is 0.847 at ef = 1024.
  • With one partition per tenant the index does run for tenant reads:
    • the 50% tenant takes 76 ms instead of 141 ms under FINAL, at recall 0.757;
    • the 10% tenant gets 0.747 and the 1% tenant 0.843.
  • That layout needs one partition per namespace, which a table with thousands of namespaces cannot hold.

3. Under FINAL it serves superseded versions. bench/ann_final_staleness.py (30k keys x 3 versions, 18 parts, 128 dimensions) on 26.9:

  • final: recall@10 0.38, with 6.2 of 10 rows stale;
  • prefilter, which skips the index: 1.00 with no stale rows, 20 ms;
  • reread, which re-reads the index's candidate keys under FINAL: 0.90-0.98 at 37-39 ms against 20 ms.

4. Write, storage and memory cost.

  • The graph is 2.27 GiB beside 2.25 GiB of table data.
  • Copying 1M rows into the indexed table took 135-166 s over two runs, against 17-51 s into a coded table without an index.
  • After the partitioned table's graphs were loaded, every read of the single-part graph missed the cache (30 of 30 misses; vector_similarity_index_cache_size 5 GiB, SLRU) and took 4.8 s instead of 9 ms. The harness now prints the cache settings.

So an index would cost writes, storage and cache memory, would never serve a tenant read, and would serve stale rows wherever it did run. The codes-first read already cuts the bytes of a 100k-row-or-larger tenant's scan 6-11x.

The doc names when to look again: when ClickHouse searches the graph within the primary-key ranges a read selects, and resolves versions under FINAL.

Reproduce (a ClickHouse you started; no provider, no Loom server; about 40 minutes and about 15 GiB of disk, plus room to rewrite the largest table):

LOOM_CH_CONTAINER=ch-vbench LOOM_CH_HOST_PORT=18433 LOOM_CH_NATIVE_HOST_PORT=19433 bash dev/ci_start_clickhouse.sh
PYTHONPATH=$PWD .venv/bin/python bench/vector_index_vs_exact.py \
    --port 18433 --user loom --password loomdev --rows 1000000 --dim 1536 --build
.venv/bin/python bench/ann_final_staleness.py --url http://localhost:18433 --user loom --password loomdev
docker rm -f ch-vbench

Scope and risk

This PR changes documentation and adds one offline harness. No server code, migration, setting, op or stored data changes. The harness writes only to its own database (loom_vbench by default) and drops its tables at the end unless given --keep. bench/vector_tenant_recall.py now has a docstring that matches the table as it is.

Left alone on purpose:

  • LOOM_VECTOR_SEARCH_EF_SEARCH and LOOM_VECTOR_SEARCH_INDEX_FETCH_MULTIPLIER only tune an index that no table has. Whether to remove them is the open question from the related PR, and it needs its own decision.
  • The same applies to the embedding_hnsw_idx entry in memory_embeddings' exclude_materialize_skip_indexes_on_merge.
  • The same applies to the HNSW comments in server/memory/app/retrieval/fact_sql.py. The SQL comment there says the identity PREWHERE makes ClickHouse post-filter, but the measurement shows it keeps the read off the graph. Fixing it belongs with the settings decision, and this PR leaves the server untouched.

Seen on the way, not changed here: at LIMIT 10 with multiplier 3, codes-first scored recall@10 0.73-0.88 on the 1M-row and 500k-row filters. The comment in server/core/vectors/search_settings.py says that "x3 held recall 1.000". The fact leg's own shape (LIMIT 60, x1) is in the table above.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: reverting removes the doc section and the harness; nothing else is left behind.

Tests

  • make format lint typecheck: pass. The codegen checks inside make lint all report current.
  • No unit or integration suites were run locally (by request); CI runs them. No server behaviour changed.
  • The measurements above come from the commands above, on a ClickHouse started for these runs and removed afterwards.
  • origin/main is merged in, which brings migrations up to 071; this PR adds no migration.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (Claude Code): I checked every figure in the doc against the harness output. I checked the index-use column against EXPLAIN indexes = 1 and against the query_log counters.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed. (format, lint and typecheck ran locally; unit tests are left to CI)
  • Generated files are regenerated in this PR, not by hand (none affected).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. (no migration)
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version. (none)
  • The frontend builds, if frontend/ changed. (unchanged)
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s).

🤖 Generated with Claude Code

zlareb1 and others added 12 commits October 21, 2026 14:27
On ClickHouse 26.9 a vector_similarity index is never searched by a read
whose primary-key condition prunes granules, and every Loom vector read
filters on the (org, namespace) sort-key prefix. Where the index does run,
a selective filter drops recall@10 to 0.10-0.82, and under FINAL it
serves superseded versions. docs/architecture/clickhouse.md records the
numbers; bench/vector_index_vs_exact.py reproduces them on synthetic
vectors against any ClickHouse.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…and drops its tables

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Codes-first at the fact leg's LIMIT 60, the partition-per-tenant win at
50%, the FINAL reread arm, per-run query tags and the cache-miss count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…d the harness drops its tables on any exit

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…he cache observation as measured

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…rt its ClickHouse, and what the health probe can still find

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…k and the health probe's table shape

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…e every part and probe output

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 131455a into main Oct 21, 2026
15 of 18 checks passed
@zlareb1
zlareb1 deleted the feat/vector-similarity-index branch October 21, 2026 15:59
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.

1 participant