# Exact vs approximate nearest neighbor: when brute force wins

> Exact vs approximate nearest neighbor search, measured: brute force against HNSW build cost, and why RushDB 3.0.15 searches up to 24,000 768-d vectors exactly.

Canonical URL: https://rushdb.com/blog/brute-force-vs-hnsw
Published: 2026-11-29T01:57:00.000Z
Updated: 2026-11-29T09:20:16.901Z
Author: RushDB
Category: Benchmarks

Exact vs approximate nearest neighbor search is a trade between query cost and build cost. Brute force scores every vector, returns the true top k and has nothing to build. An HNSW graph answers in about a millisecond, but it has to be built first and kept in memory.

In October we measured about 1.7 µs per 768-d vector for exact search on a pre-release build, and RushDB switched to HNSW above a fixed 5,000 vectors. Exact search has become much faster since, and RushDB 3.0.15 picks the path by cost on every search: an unfiltered 768-d index is searched exactly up to 24,000 vectors at the default `ef` of 128, and a selective filter keeps far larger indexes exact.

## TL;DR

- October measurement (pre-release 3.0 build, developer workstation, 2026-10-12, ANN `ef` 64): exact search cost ~1.7 µs per 768-d vector and ~2.6 µs per 1,536-d vector up to 10,000 vectors. HNSW answered in 0.24–1.1 ms per 768-d query once built; the build took 0.12 s at 1,000 vectors, 2.1 s at 5,000 and 47.6 s at 50,000.
- Exact search got faster in 3.0.7, 3.0.11 and 3.0.13 (a SIMD kernel on several threads, then vectors kept resident in memory). The 3.0.13 release notes give 11.7 ms for an exact top 10 over 100,000 resident 1,536-d vectors, against 1.37 ms for the graph at `ef` 128.
- Since 3.0.13, `auto` compares costs per search. Without a filter the switch is at `vectors × dimensions = 144,000 × ef`: 24,000 vectors at 768-d and 12,000 at 1,536-d with the default `ef` of 128. A filtered search uses `pool² × dimensions ≤ 26,000 × ef × index vectors` instead. Above that it uses HNSW once the graph is resident, and no search waits for a build.
- On random 768-d vectors, an October laptop run measured recall@10 of 0.63 at `ef` 64 and 0.84 at `ef` 256. Embeddings with cluster structure usually do better; measure yours.
- The graph has to fit in memory. Since 3.0.1 vectors have their own 256 MiB cache, which holds the graph of a 768-d space of about 1.7M vectors; before that, a 1M-vector space fell back to exact search at 4.5 s with one client.

## What is the difference between exact and approximate nearest neighbor search?

Exact nearest neighbor search (brute force, flat k-NN) computes the similarity between the query and every stored vector and keeps the top k. Its answer is correct by definition, and its cost grows linearly with the number of vectors. Approximate nearest neighbor (ANN) search walks an index, usually an HNSW graph, and visits only a small part of the data, trading some recall for latency.

[HNSW](https://arxiv.org/abs/1603.09320) (hierarchical navigable small world) builds a layered proximity graph: each vector links to its near neighbours, and a search hops greedily from a coarse layer down to a fine one. The breadth of the search, `ef`, sets how many candidates it keeps. Higher `ef` means better recall and more work.

| | Exact (brute force) | ANN (HNSW) |
|---|---|---|
| Recall | 1.0 | below 1.0; rises with `ef` |
| Query cost | linear in vectors scored | roughly flat once built |
| Build cost | none | seconds to minutes; memory for the graph |
| Filters | score only the records that pass | walk past records that fail; fall back if too few pass |
| Metrics | any | L2, inner product and cosine in most libraries; RushDB builds it for cosine only (`dot` and `euclidean` indexes search exactly) |

The argument for brute force at small sizes has been made well elsewhere. Doug Turnbull's [Just brute force your embeddings](https://softwaredoug.com/blog/2026/07/29/just-brute-force-embeddings) (July 2026) measured a NumPy dot product over 1M 384-d vectors at 12 ms average on an M4 MacBook Pro. Different hardware, dimensions and code than ours, so don't compare the numbers directly; the point holds.

## How much did brute force cost per vector in October?

In the October run, exact search cost about 1.7 µs per 768-d vector and 2.6 µs per 1,536-d vector, up to 10,000 vectors, or about 9 ms for a 5,000-vector query.

Setup: `vector_mode_crossover` from the engine's capacity bench, 2026-10-12, on a pre-release 3.0 build, run on a developer workstation with an idle development stack, a different machine from the dedicated host behind [our capacity envelope](https://rushdb.com/blog/rushdb-capacity-envelope). A fresh durable store per row, random unit vectors, cosine, top 10, 30 warm queries, ANN `ef` 64. "Cold" is the first query; for ANN it includes building the graph. Storage format v5.

| dims | vectors | exact p50 | ANN p50 | ANN cold (build + first query) |
|---:|---:|---:|---:|---:|
| 768 | 1,000 | 1.58 ms | 0.24 ms | 124 ms |
| 768 | 5,000 | 8.73 ms | 0.50 ms | 2,135 ms |
| 768 | 10,000 | 17.86 ms | 0.66 ms | 5,959 ms |
| 768 | 20,000 | 37.61 ms | 0.80 ms | 14,570 ms |
| 768 | 50,000 | 166.02 ms | 1.13 ms | 47,645 ms |
| 1,536 | 5,000 | 13.00 ms | 0.82 ms | 4,124 ms |
| 1,536 | 10,000 | 26.12 ms | 1.15 ms | 11,428 ms |
| 1,536 | 50,000 | 293.12 ms | 3.26 ms | 184,818 ms |

Divide exact p50 by vectors and the per-vector cost is 1.6–1.9 µs for 768-d up to 20,000 vectors, then 3.3 µs at 50,000. At that size the space is about 154 MB of f32, well past the CPU caches; we didn't profile the jump. No ANN search in these runs fell back to exact.

This table is a record of that build, not of 3.0.15. Three releases changed the exact path since:

- 3.0.7 scores each stored vector in place with one SIMD kernel (AVX2 where the CPU has it) and splits a large search across the server's threads.
- 3.0.11 keeps a searched space's vectors in memory as `f32` rows.
- 3.0.13 adds each vector's norm and binding to those rows and scores them in one parallel pass.

The 3.0.13 figure above, 11.7 ms for 100,000 resident 1,536-d vectors, works out to about 0.12 µs per vector, roughly a twentieth of the October figure, on a different setup.

ANN builds changed too: since 3.0.11 a build of 50,000 vectors or more inserts from several threads, and a cold build of 1M 768-d vectors went from 24.5 to 5.6 minutes on 20 threads ([changelog](https://github.com/rush-db/rushdb/blob/v3.0.15/CHANGELOG.md)). We haven't re-run the crossover bench on 3.0.15.

## When does HNSW pay for itself?

When exact search over the pool costs more than walking the graph, and the space is stable enough that the build is paid once. In the October table that point came early: HNSW answered at 1 ms or less at every 768-d size up to 20,000, while exact search passed 10 ms between 5,000 and 10,000 vectors.

Simple arithmetic on the October table, not a separate measurement: at 5,000 768-d vectors each ANN query saved about 8.2 ms, so a 2.1 s build was repaid after roughly 260 queries; at 50,000 vectors the saving was about 165 ms per query and the build paid off after about 290. With today's faster exact path the per-query saving at small sizes is close to zero, so there is little to repay a build with. That is why the default threshold moved up.

RushDB 3.0.15 builds this comparison into the router. The unfiltered rule was calibrated on 100,000 × 1,536-d cosine vectors, where exact search took 11.7 ms and a walk at `ef` 128 took 1.4 ms ([3.0.13 changelog](https://github.com/rush-db/rushdb/blob/v3.0.15/CHANGELOG.md)); at 768-d and `ef` 128 it puts the switch at 24,000 vectors. A larger `ef` makes the walk dearer, so it moves the switch up: at `ef` 512 an unfiltered 768-d index stays exact up to 96,000 vectors.

There's also memory. The HNSW graph and the resident vectors are both charged to the vector cache (`vector-cache-bytes`, 256 MiB by default).

The graph's resident part takes about 150 bytes per 768-d vector, and a build reserves about `2 × dimensions + 320` bytes per vector while it runs, in chunks sized to the free budget. The resident `f32` rows take `dimensions × 4 + 32` bytes per vector, about 3.1 KB at 768-d, so the default budget holds them for about 86,000 768-d vectors. A space whose rows don't fit is scored from storage, more slowly, with the same results ([vector internals](https://docs.rushdb.com/engine/architecture/vector-internals)).

## What does RushDB's auto mode do?

`auto`, the default, searches exactly while the pool is cheap enough to score or the metric isn't cosine. The pool is the records matching the `predicate` (at most the index's vectors), or the whole index without one. `resources.vector-exact-max-pool` sets "cheap enough"; its default, `auto`, compares the two paths' costs from the pool, the index size, the dimensions and the search's `ef` ([vector search docs](https://docs.rushdb.com/engine/indexes-and-search/vector-search)):

- without a filter, exact while `index vectors × dimensions ≤ 144,000 × ef`;
- with a filter, exact while `pool² × dimensions ≤ 26,000 × ef × index vectors`. A filtered walk slows down as the filter admits less of the index, so a narrow filter on a large index is scored exactly.

| Index | Unfiltered: exact up to | Filtered: exact up to (`ef` 128 / 512) |
|---|---|---|
| 100,000 × 1,536-d | 12,000 vectors | 14,720 / 29,439 matching records |
| 1,000,000 × 768-d | 24,000 vectors | 65,828 / 131,656 matching records |

Above that, `auto` uses HNSW once the graph is resident, and answers exactly while the graph builds in the background. Every ANN candidate is re-scored exactly against the stored vector, and with a filter the walk skips records outside it, so every gathered candidate matches.

```mermaid
flowchart TD
  Q["query.vector, mode auto"] --> M{"metric cosine?"}
  M -- no --> E["exact"]
  M -- yes --> F{"predicate?"}
  F -- no --> U{"vectors × dims ≤ 144,000 × ef?"}
  F -- yes --> P{"pool² × dims ≤<br/>26,000 × ef × vectors?"}
  U -- yes --> E
  P -- yes --> E
  U -- no --> R{"graph resident?"}
  P -- no --> R
  R -- no --> W["exact, fallback: annWarming<br/>(graph builds in background)"]
  R -- yes --> A["HNSW walk, skipping filtered-out records;<br/>exact re-score"]
  A --> C{"at least min(ef, k)<br/>candidates found?"}
  C -- no --> L["exact, fallback: lowConfidence"]
  C -- yes --> OK["results"]
```

If the graph doesn't fit the vector cache, the search runs exactly and reports `budget`. A forced `mode: "ann"` with a filter whose pool is cheap enough to score is answered exactly and reports `smallCandidateSet`. And `mode: "ann"` on a `dot` or `euclidean` index is refused, because the HNSW path is cosine-only. The filter runs first in every case: the predicate becomes a candidate set before any ranking, so a filtered search never returns fewer results because good matches were dropped after ranking. [Filtered vector search](https://rushdb.com/blog/filtered-vector-search)  covers that part.

A number fixes the threshold instead: `vector-exact-max-pool: 20000` scores at most 20,000 vectors exactly whatever the dimensions and `ef`. Every `config.yml` written before 3.0.13 holds `5000`, which 3.0.13 and later read as `auto`; write `fixed:5000` to keep the old behaviour ([configuration reference](https://docs.rushdb.com/engine/reference/configuration-reference/)).

We watched the old fixed threshold switch on a live node (pre-release build of 2026-10-27, in-memory, i9-12900H laptop, 2026-10-31, random 768-d example vectors on `Doc` records with a `lang` field, filter `lang = "en"`, top 3). The query:

```json
{"kind":"vector","query":{"labels":["Doc"],"property":"emb","index":"doc-emb",
  "vector":[0.0213, -0.0441, "… 768 numbers …"],"predicate":{"lang":"en"},"limit":3}}
```

What `vectorSearch` and the top hits reported, step by step (record `n` is a sequence field in the example data):

```text
4,000 vectors, auto   → {"mode":"exact","pool":4000}                          376, 2970, 1353
6,000 vectors, auto   → {"mode":"exact","fallback":"annWarming","pool":6000}  376, 4076, 2970
same, seconds later   → {"mode":"ann","ef":64,"pool":6000}                    376, 4076, 1279
same, mode exact      → {"mode":"exact","pool":6000}                          376, 4076, 2970
```

The ANN answer missed the true third match (2970) and returned 1279 instead. That's what recall below 1.0 looks like in one query.

On 3.0.15 the same query stays exact at both sizes: about a third of the 6,000 records match `lang = "en"`, far inside the [filtered rule](https://docs.rushdb.com/engine/indexes-and-search/vector-search) (and 6,000 × 768 is inside the unfiltered one too), and `pool` now reports those matching records, not the index's 6,000. To see the graph on a space this small today, send `mode: "ann"` without a filter. The `dot` index, asked for ANN, is refused with the same message on both builds:

```json
{"code":"invalid_request","message":"vector search mode `ann` requires a cosine index; this index uses `dot` (use `exact` or `auto`)","retryable":false}
```

## How accurate was ANN in our run?

On the same laptop node, 50 random queries against 6,000 random unit 768-d vectors, comparing `mode: "ann"` with `mode: "exact"` top 10: recall@10 was 0.63 at `ef` 64 and 0.84 at `ef` 256, with or without the `lang` filter. Server time was about 1 ms for ANN and 13 ms for exact search.

| `ef` | filter | recall@10 | exact p50 | ANN p50 |
|---:|---|---:|---:|---:|
| 64 | none | 0.63 | 13 ms | 1 ms |
| 64 | `lang = en` (~⅓) | 0.65 | 11 ms | 4 ms |
| 256 | none | 0.84 | 13 ms | 1 ms |
| 256 | `lang = en` | 0.83 | 11 ms | 5 ms |

Read this as a worst case, not as RushDB's recall. Random unit vectors in 768 dimensions have no cluster structure, which is the hardest input for any proximity graph, and real embeddings usually score higher. It's one run on a laptop that had other work on it.

The build was also older than two changes to `ef`. Before 3.0.7 a walk kept at least 128 candidates whatever `ef` asked for, so these `ef` 64 rows don't map one-to-one onto today's `ef` 64, which walks only 64. The default `ef` is now 128 (`resources.vector-default-ef`). The method is short (50 queries in both modes, compare the returned ids), so run it on your own embeddings before choosing `ef`. On 3.0.15 a 6,000-vector 768-d index is searched exactly by default, with recall 1.0, so the trade only starts to matter above the switch point.

## What happens with 1M vectors?

A 1M × 768-d index is far past the unfiltered switch (24,000 vectors at `ef` 128), so unfiltered searches use the graph. A filter matching up to about 65,800 of those records is scored exactly. The graph has to fit in memory: at about 150 bytes of vector cache per 768-d vector, the default `vector-cache-bytes` of 256 MiB holds a space of about 1.7M vectors; past that, searches run exactly and report `budget`, and raising the setting fixes it ([troubleshooting](https://docs.rushdb.com/engine/operations/troubleshooting)).

The resident `f32` rows of 1M 768-d vectors (about 3.1 GB) don't fit the default budget, so exact searches and the re-score read those vectors from storage.

A cold build of that space takes minutes: 5.6 minutes on 20 threads in the 3.0.11 release notes. Searches during the build answer exactly with `annWarming`. After a restart the graph is reopened from its persisted segments, and only vectors no segment covers are decoded again. The [capacity planning](https://docs.rushdb.com/engine/operations/capacity-planning) table for one 768-d space gives a settled search p50 of 1.8 ms at 10K vectors, 7.2 ms at 50K and 21.6 ms at 150K, slower right after a bulk bind while segments merge.

The separate vector budget is new in 3.0.1. Before it, the graph shared a 128 MiB cache with the text and planner caches, and in our capacity run (i9-12900H bench host, 2026-10-19, storage format v5) a 1M-vector space didn't fit. Every search fell back to exact, at 4.5 s with one client and 58 s p50 with 32 concurrent clients. With the graph resident (a 384 MiB cache on the same store) one-client searches took about 62 ms. Since 3.0.1 the same space answers from its index on default settings.

Trade-offs to know:

- ANN is HNSW (via [USearch](https://github.com/unum-cloud/USearch)) with cosine only; `dot` and `euclidean` indexes always search exactly.
- The graph may hold vectors at 16-bit precision; results are always re-scored with the stored 32-bit vectors. An 8-bit graph was tested in 3.0.14 and not made the default, because its recall was lower at small `ef`.
- Spaces past about 1.7M 768-d vectors need a larger `vector-cache-bytes`.
- RushDB stores and searches vectors; it never calls an embedding model. You bring the vectors.

Combining vector scores with BM25 is a separate step, covered in [hybrid search: RRF vs DBSF](https://rushdb.com/blog/hybrid-search-rrf-vs-dbsf) .

## FAQ

### When should I use exact nearest neighbor search instead of HNSW?

When the candidate set is small enough that scoring it costs less than a graph walk. On RushDB 3.0.15 that is up to 24,000 768-d or 12,000 1,536-d vectors per unfiltered index at the default `ef`, and more for a selective filter on a large index. Exact search then has perfect recall and needs no build and no graph memory.

### Where does RushDB switch from exact search to HNSW?

Since 3.0.13 there is no fixed count. `auto` searches exactly while `index vectors × dimensions ≤ 144,000 × ef` without a filter, or `pool² × dimensions ≤ 26,000 × ef × index vectors` with one. Releases before 3.0.13 used a fixed 5,000 vectors, chosen from the October measurement. Set `resources.vector-exact-max-pool` to a number, or `fixed:5000` for the old value, to pin it.

### Does RushDB return approximate results without saying so?

No. Each vector response carries `vectorSearch` with the mode used (`exact` or `ann`), `ef` for ANN, the pool the search could score, and a `fallback` reason (`annWarming`, `lowConfidence`, `budget` or `smallCandidateSet`) when a search that would have used the graph was answered exactly. `describe.explainVector` reports the same decision before you search. ANN candidates are always re-scored exactly, so scores are true cosine similarities even when the set is approximate.

## Sources

- [Vector search (RushDB engine docs)](https://docs.rushdb.com/engine/indexes-and-search/vector-search)
- [Vector internals](https://docs.rushdb.com/engine/architecture/vector-internals)
- [Configuration reference (`vector-exact-max-pool`, `vector-default-ef`, `vector-cache-bytes`)](https://docs.rushdb.com/engine/reference/configuration-reference/)
- [Capacity planning](https://docs.rushdb.com/engine/operations/capacity-planning)
- [Troubleshooting: vector search is slow or exact](https://docs.rushdb.com/engine/operations/troubleshooting)
- [RushDB engine changelog at v3.0.15 (3.0.1 vector cache, 3.0.7 `ef` and exact kernel, 3.0.11 resident vectors and parallel builds, 3.0.13 cost-based routing)](https://github.com/rush-db/rushdb/blob/v3.0.15/CHANGELOG.md)
- [Malkov and Yashunin (2016), the HNSW paper on arXiv](https://arxiv.org/abs/1603.09320)
- [USearch](https://github.com/unum-cloud/USearch)
- [Doug Turnbull: Just brute force your embeddings](https://softwaredoug.com/blog/2026/07/29/just-brute-force-embeddings)
