ClickHouse vector search: HNSW index vs brute force, and why WHERE can return fewer rows than LIMIT

ClickHouse's HNSW index cut a 100M-vector search from 4.6 s to 0.019 s. With a WHERE that keeps 1% of rows, a published test got recall@10 of just 0.10.

The ClickHouse docs ran one nearest-neighbour query over about 100 million 512-dimension LAION embeddings twice. A brute-force cosineDistance scan took 4.605 s and read 309.98 GB. With an HNSW vector_similarity index it took 0.019 s and read 24.42 MB. Both runs returned the same top 10 (LAION-400M example).

That’s the case everyone shows. The case that bites in production looks like this:

SELECT id, title
FROM books
WHERE price < 2
ORDER BY cosineDistance(embedding, {q:Array(Float32)})
LIMIT 10;

On a table with a vector index, this can return three rows, or zero, even when thousands of books under $2 exist. The docs say so themselves. A benchmark posted with a ClickHouse pull request puts numbers on it: with a filter that keeps 1% of 1 M rows, the default path returned the true top 10 with a recall of 0.10, and all 1,000 test queries came back with fewer than 10 rows (PR #112406). So Day 19 isn’t another “index fast, scan slow” chart. It’s about how the index actually answers a query, when you get exact results and when you don’t, and what each knob costs. I read the docs, the release posts and the 26.11 source (tag v26.11.9.22-stable). Nothing was run for this post. Every number links to whoever measured it.

The short answer

You run What ClickHouse does Result
ORDER BY L2Distance(v, q) LIMIT 10, no index Reads every vector, computes every distance Exact, cost grows with rows
Same, with a matching vector_similarity index Asks the HNSW graph of each part for its 10 nearest, merges Approximate, usually the same top 10
+ any WHERE (default auto) Index first, then the filter (postfilter) Can return fewer than 10; recall 0.10 at a 1% filter
+ SET vector_search_filter_strategy = 'prefilter' Filter first, then exact distances over what’s left Exact, but ~45-90× fewer queries/s in the PR test
+ WHERE on the primary key that cuts into a part That part skips the index: exact brute force on the surviving ranges Exact, slower
+ WHERE on the partition key Partitions pruned first, then the index on what’s left Approximate
Distance function differs from the index, LIMIT … WITH TIES, or LIMIT above max_limit_for_vector_search_queries Index not used Exact, slow
Part has lightweight-deleted rows That part is scanned exactly Exact, slower

The rest of the post shows where each row comes from.

How the index answers a query

A vector_similarity index is an HNSW graph (the usearch library) built per part. Its default GRANULARITY is 100 million, so in practice one graph per part (docs). A query must look exactly like this to use it:

ORDER BY <same distance function as the index>(vec, <constant vector>) LIMIT N

L2Distance and cosineDistance indexes serve ASC, dotProduct serves DESC. Ask for L2Distance on a cosineDistance index and the query quietly becomes a full scan, so check with EXPLAIN indexes = 1. N may not exceed max_limit_for_vector_search_queries. The docs say the default is 100; the 26.11 source says 1,000 (Settings.cpp). Above it, the index is simply not used.

For each part, ClickHouse asks the graph for its N nearest neighbours, walking it with a candidate list of hnsw_candidate_list_size_for_search = 256 (HNSW’s ef_search). Then it reads only the granules that hold those rows and merges the per-part winners into one top N. That’s why the LAION query touched 137 thousand rows instead of 100 million: a few granules per part, not the part.

Docs example Brute force HNSW index Speedup
LAION, ~100 M × 512 dims, LIMIT 10 4.605 s, 100.38 M rows, 309.98 GB 0.019 s, 137.27 K rows, 24.42 MB ~240×
dbpedia, 1 M × 1536 dims, LIMIT 20 0.261 s, 1.00 M rows, 6.22 GB 0.025 s, 32.03 K rows, 2.10 MB ~10×

Both indexes were cosineDistance, bf16, M = 64. The docs don’t give hardware or version, and the two dbpedia runs used different query vectors.

Two things in the code matter later. First, results are per part: each part’s graph returns its own top N, and SQL then picks the global top N (MergeTreeDataSelectExecutor.cpp). Second, without filters or rescoring the index skips the distance math entirely. It doesn’t read the vector column at all and hands back the distance it computed in the graph as a virtual _distance column (useVectorSearch.cpp). That’s why the read volume collapses.

What neither docs page publishes is recall: the share of the true top 10 the index returns. The LAION page says the top 10 matched in that one run, and the main page says HNSW “can potentially achieve a recall close to 1” with careful parameters. The only ClickHouse recall numbers I found are in the filter benchmark below.

Why WHERE can return fewer rows than LIMIT

ClickHouse has two ways to combine a vector search with a filter, picked by vector_search_filter_strategy (settings):

  • postfilter: ask the index for the nearest rows, then apply WHERE.
  • prefilter: apply WHERE, then compute exact distances for every surviving row. In ClickHouse “pre-filtering” means brute force. It doesn’t walk the graph with a filter.
ORDER BY cosineDistance(vec, q) LIMIT 10 WHERE price < 2 (rare)postfilterdefault (auto)HNSW: 10 nearestper part (× multiplier)WHERE drops 9 of 10 → 1 row, not 10prefilterexact, no HNSWWHERE first:rows with price < 2brute-force distances → exactly 10Postfilter is fast but can return fewer than LIMIT (even 0). Prefilter is exact but scans every surviving row.
Schematic of the two strategies in the ClickHouse vector search docs (setting vector_search_filter_strategy; in 26.11 auto takes the postfilter path). The docs' own example is books under $2 returning zero rows. The 1-of-10 outcome is illustrative.

Postfilter is fast, but the index only returns LIMIT × vector_search_index_fetch_multiplier rows per part, and the multiplier defaults to 1. If 9 of those 10 fail price < 2, you get 1 row. The docs’ own example is books under $2: postfiltering “may return zero rows”. Raising the multiplier helps (3.0 → 30 candidates per part for LIMIT 10), but the docs warn that with a very selective WHERE “it is still possible that less than N requested rows returned”.

How bad it gets: the published numbers

The best measurement I found comes from PR #112406, an open pull request (July 2026, on master) that proposes a third strategy. Its author ran 1,000 queries over 1 M Hacker News embeddings (cosineDistance, bf16, M = 32, ef_search 128, LIMIT 10, 8 concurrent) with WHERE id % 100 < ratio, so the filter keeps 1%, 50% or 99% of rows.

keeps 1%: postfilter ×11000 of 1000 short · 111.6 QPSkeeps 1%: postfilter ×1: 0.10 0.10 keeps 1%: postfilter ×1000342 short · 101.1 QPSkeeps 1%: postfilter ×1000: 0.90 0.90 keeps 1%: prefilter0 short · 2.4 QPSkeeps 1%: prefilter: 1.00 1.00 keeps 50%: postfilter ×1998 short · 108.9 QPSkeeps 50%: postfilter ×1: 0.54 0.54 keeps 50%: postfilter ×10000 short · 101.3 QPSkeeps 50%: postfilter ×1000: 0.98 0.98 keeps 50%: prefilter0 short · 1.5 QPSkeeps 50%: prefilter: 1.00 1.00 keeps 99%: postfilter ×188 short · 109.8 QPSkeeps 99%: postfilter ×1: 0.96 0.96 keeps 99%: postfilter ×10000 short · 101.0 QPSkeeps 99%: postfilter ×1000: 0.98 0.98 keeps 99%: prefilter0 short · 1.2 QPSkeeps 99%: prefilter: 1.00 1.00
Recall@10 (distance tie-aware) per route, from the benchmark in open pull request ClickHouse #112406 (contributor's run on master, July 2026; 1,000 queries, 8 concurrent, cosineDistance, bf16, M = 32, ef_construction 256, ef_search 128, LIMIT 10; hardware not stated). “Short” = queries that returned fewer than 10 rows. ×N = vector_search_index_fetch_multiplier. Nothing was run for this post. Bars to scale.
Filter keeps Route Recall@10 Queries with < 10 rows Queries/s
1% postfilter, multiplier 1 (default) 0.1041 1000 of 1000 111.6
1% postfilter, multiplier 1000 0.8959 342 101.1
1% prefilter 1.0000 0 2.4
50% postfilter, multiplier 1 0.5442 998 108.9
50% postfilter, multiplier 1000 0.9768 0 101.3
50% prefilter 1.0000 0 1.5
99% postfilter, multiplier 1 0.9552 88 109.8
99% postfilter, multiplier 1000 0.9803 0 101.0
99% prefilter 1.0000 0 1.2

Three things stand out, and each has a plain explanation:

  • Even a filter that keeps 50% breaks the default. With multiplier 1, the index fetches 10 candidates and half fail the filter, so about 5 survive. 998 of 1,000 queries came back short.
  • Multiplier 1000 still misses at 1%. In 26.11 the fetch is capped at max_limit_for_vector_search_queries = 1,000 rows (MergeTreeIndexVectorSimilarity.cpp). 1% of 1,000 candidates from a part is about 10 matching rows on average, just enough for LIMIT 10, so roughly a third of the queries got fewer. The author: postfilter “does not reach the target recall, even with vector_search_index_fetch_multiplier = 1000”.
  • Prefilter is exact and 45-90× slower here. It’s brute force over the filtered rows, and id % 100 < ratio can’t use any index, so every row is still read. The more rows the filter keeps, the slower it gets (2.4 → 1.2 queries/s).

The PR’s own route, exact filtering inside the graph walk (in_traversal), got recall 1.0 at 15.5-98.8 queries/s. It isn’t merged, so it isn’t in any release. Hardware isn’t stated, so compare the routes with each other, not with your server.

What auto really does

The default is auto. The settings page describes it as “Postfiltering (the exact semantics may change in future)”. The main docs page describes heuristics (partition pruning, primary-key ranges, skip indexes). In the 26.11 code, the only place the setting is read is one line:

if (additional_filters_present && settings.vector_search_filter_strategy == VectorSearchFilterStrategy::PREFILTER)
    return no_layers_updated; /// user explicitly wanted exact (brute-force) vector search

(useVectorSearch.cpp#L138). So auto and postfilter are the same code path. There is no selectivity estimate. The “heuristics” are side effects of how parts are read:

  • A filter on the partition key prunes whole partitions before any index runs.
  • A filter on the primary key that removes some ranges inside a part turns the index off for that part, because “the vector index is built on the entire part” (MergeTreeDataSelectExecutor.cpp#L3480). That part gets an exact scan of the surviving ranges. That’s how a selective primary-key filter ends up exact: by accident, not by cost estimate.
  • Any other filter, including one a skip index can evaluate, is applied after the vector index.

Rule of thumb from the numbers above:

  • Filter keeps most rows, approximate is fine: stay on postfilter but raise vector_search_index_fetch_multiplier. In the PR test, ×1000 left no short queries at 50% and 99%, still at ~100 queries/s.
  • Filter keeps a few percent or less, or you need exact results: SET vector_search_filter_strategy = 'prefilter', and make the filter cheap. Put the filtered column in the partition key or first in the primary key, so the brute-force scan reads only the slice it needs.

What the index costs

Memory: it must fit in RAM

“The vector similarity index must be fully loaded from disk into main memory to perform searches” (docs). Loaded indexes live in vector_similarity_index_cache_size, 5 GiB by default in open-source ClickHouse (SLRU policy; ServerSettings.cpp). An index bigger than the cache isn’t cached at all, so every query reloads it from disk. The docs’ log shows one 1.8 M-vector index (M = 64, 4,096 bytes per vector) taking about 75 s to load and using 8.00 GiB. If vector queries are suddenly slow, check this cache first (VectorSimilarityIndexCacheMisses in system.events).

The docs give a sizing formula: vectors × dimensions × bytes per value, plus vectors × M × 4 bytes × 2 for the graph.

Float32 columnon disk, CODEC(NONE)Float32 column: 6,144 MB6,144 MBindex, f326,144 + 256 MB graphindex, f32: 6,400 MB6,400 MBindex, bf16 (default)3,072 + 256 MB graphindex, bf16 (default): 3,328 MB3,328 MBindex, i81,536 + 256 MB graphindex, i8: 1,792 MB1,792 MBindex, b1192 + 256 MB graphindex, b1: 448 MB448 MB
Estimate from the sizing formula in the ClickHouse vector search docs: vectors × dimensions × bytes per value + vectors × M × 4 bytes × 2 (graph), with the default M = 32 (the docs' example uses M = 64, which doubles the graph to 512 MB). MB = 10⁶ bytes; runtime buffers not included. Arithmetic, not a measurement.

That’s why the default quantization is bf16, which halves the vector part with “negligible precision loss” on most datasets. i8 and b1 shrink it further with “appreciable precision loss”. Use them only when the index is much bigger than RAM, and turn on vector_search_with_rescoring = 1 so the final distances come from the full-precision column. b1 (1 bit per dimension, Hamming distance) is only recommended for normalized embeddings with cosine distance.

Inserts and merges: the graph is rebuilt every time

An index belongs to its part. Every insert builds a graph, and every merge builds a new one for the merged part. The docs warn that HNSW “can significantly slow down inserts and merges”, and that the index fits data that is rarely changed. ClickHouse’s 25.10 release post shows it at small scale: inserting 38.46 K dbpedia vectors (1536 dims) took 6.161 s while building the index and 0.522 s with the build excluded (25.10 release). That’s about 12× on the insert, before any merge.

The escape hatches, all documented:

Setting Effect
exclude_materialize_skip_indexes_on_insert = 'vec_idx' (25.10+) Insert without building this index; those parts are searched exactly
materialize_skip_indexes_on_merge = 0 / exclude_materialize_skip_indexes_on_merge Don’t rebuild on merge
ALTER TABLE t MATERIALIZE INDEX vec_idx Build later, e.g. off-hours
max_build_vector_similarity_index_thread_pool_size (server) More threads for builds

The trade-off is plain: every part without an index is a brute-force part at query time.

Deletes switch the index off for that part

A part with lightweight-deleted rows, or a pending DELETE mutation, doesn’t use the index and is scanned exactly, until a merge or mutation rewrites it (or you set apply_deleted_mask = 0 and accept deleted rows as candidates). The reason is the same LIMIT problem: rows removed after the index answered would leave fewer than N.

When brute force is the better choice

Brute force in ClickHouse is not a toy. It’s a parallel columnar scan, and it’s exact. The dbpedia scan of 1 M × 1536-dim vectors took 0.261 s. A few cases where I’d skip the index:

  • Under a few million vectors, or queries that always filter down to a small slice by primary key (prefilter does brute force on that slice anyway).
  • Frequent inserts or deletes: index builds on every merge, and deleted rows disable it per part.
  • When you can’t afford “fewer than LIMIT”: prefilter or no index.
  • When the index won’t fit in RAM: then a smaller exact scan helps more. QBit (25.10+, experimental) stores vectors bit-sliced and lets you choose how many bits to read at query time. In ClickHouse’s dbpedia demo, reading 5 bits per value cut a top-1000 query from 1.157 s / 32.76 GB / 6.05 GiB peak to 0.271 s / 4.54 GB / 739.82 MiB, with the top 10 still relevant but reordered (QBit post, “not a controlled benchmark – just my machine”).

Use the index when the table is large, mostly append-only, fits the cache, and your filters keep most rows or hit the partition key.

Rough edges still open in 26.11

These are open GitHub issues as of today, each with the reporter’s own numbers. Worth knowing before you size a cluster:

  • Cache accounting. #116917 reports the index cache charging ~16 MiB per cached granule where the real cost is “a few hundred kilobytes”, so the cache can fall to a 0% hit rate without any error.
  • First query after a merge or restart. #134259: the index is decompressed on one thread. On a 1 M × 768-dim bf16 part (1.26 GB index file, 16 vCPU), that load took 2,680 ms, against 8.3-13.2 ms for warm searches. The issue also says maintainers plan to deprecate HNSW in favour of QBit, quantized codecs and IVF. I found no official statement of that.
  • Rescoring reads whole granules. #133484: with vector_search_with_rescoring = 1, a 768-dim bf16 candidate costs a full granule of about 12.6 MB to read. 10 M LAION rows with multiplier 10 meant ~5.8 GB per query: 235 ms warm, 10 s cold from object storage. index_granularity = 256 brought one test from 172 ms to 26 ms.
  • SAMPLE ignores both prefilter and the multiplier, and loses rows (#126668).
  • A huge LIMIT on a small index could reserve absurd memory (“64.19 GiB” on 26.10). Fixed in 26.12 (#135179), not in 26.11.

What I couldn’t verify

  • Recall without filters at the default ef_search = 256 on a public dataset. The only ClickHouse recall numbers I found are PR #112406’s (with ef_search 128), and its 99% row suggests about 0.96-0.98 at that setting. The docs only say the top results matched.
  • Hardware and version behind the LAION, dbpedia, 25.10 and PR numbers. All are someone else’s runs, and the PR’s runs are on an unreleased master.
  • Whether the docs’ default of 100 for max_limit_for_vector_search_queries was ever true in a release. The 26.11 source and its change history say 1,000 since 25.5.

FAQ

Does ClickHouse have a vector index? Yes. INDEX i vec TYPE vector_similarity('hnsw', 'cosineDistance', 1536) on an Array(Float32), Array(Float64) or Array(BFloat16) column, available since 25.8. HNSW is the only method.

Why does my ClickHouse vector search with WHERE return fewer rows than LIMIT? Because the default auto strategy post-filters: the index returns LIMIT × vector_search_index_fetch_multiplier candidates per part (default 1×, capped at 1,000), then WHERE removes some. In a published test, a filter keeping 50% of rows left 998 of 1,000 queries short. Raise the multiplier, or SET vector_search_filter_strategy = 'prefilter' for exact results.

Is ClickHouse vector search exact? Without an index, and with prefilter, yes. With the HNSW index it’s approximate; the default bf16 quantization adds a little error, i8/b1 more, which vector_search_with_rescoring = 1 corrects for the final distances.

How much memory does a ClickHouse vector index need? About vectors × dims × 2 bytes (bf16) plus vectors × M × 8 bytes. 1 M × 1536-dim vectors: about 3.3 GB at the default M = 32. The whole index must fit in RAM, and the cache defaults to 5 GiB.

Why isn’t my query using the vector index? Usual reasons: the distance function differs from the index’s, LIMIT is missing, uses WITH TIES or is above max_limit_for_vector_search_queries, the reference vector isn’t constant, the part has deleted rows, or the part was inserted with index builds excluded. EXPLAIN indexes = 1 shows it.

Tomorrow, Day 20: high-cardinality GROUP BY, memory limits and spilling to disk.