How does HNSW improve vector retrieval performance?

Created: Updated: 5 min read

HNSW improves vector retrieval performance by replacing the need to compare a query against every single vector in a dataset with a much smaller number of targeted comparisons, guided by a purpose-built graph structure that steers a search toward the right answer instead of checking everything along the way. The performance gain isn’t a single trick — it comes from several design decisions working together, each addressing a different part of what makes exhaustive comparison so slow at scale, and understanding how they compound is the clearest way to see why HNSW delivers such a large practical speedup over brute-force search.

How much work does HNSW actually save compared to checking every vector directly?

An exhaustive, brute-force search against a dataset of a million vectors requires computing a million distances for every single query, with that cost growing directly in proportion to how many vectors the dataset contains. HNSW’s greedy, best-first traversal, by contrast, typically only needs to examine a small fraction of the dataset — often just a few hundred vectors, even against a dataset of millions — because the graph structure it searches was specifically built so that following a handful of well-chosen edges reliably leads toward the query’s actual neighborhood. This is the single biggest source of HNSW’s performance improvement: theoretical analysis of HNSW-like structures suggests search cost scales only logarithmically with dataset size under reasonable assumptions, meaning a dataset a thousand times larger might only require a modestly larger number of comparisons per query, rather than a thousand times as many.

This efficiency doesn’t come from any shortcut that skips checking relevant data — it comes from the graph’s structure making it possible to rule out large portions of the dataset implicitly, simply by never having a reason to visit them, rather than needing to explicitly examine and discard them the way a brute-force scan would.

Why does the layered hierarchy specifically contribute to this speedup, beyond what a single flat graph could achieve?

A single proximity graph, however well connected, still requires a search to traverse enough edges to get from an arbitrary starting point to the right neighborhood, and that number of hops can add up even on a well-built graph. HNSW’s hierarchy, covered in depth elsewhere in this documentation, addresses this by giving the search a fast way to cover large distances early, using the sparse upper layers’ long-range connections to jump across much of the vector space in relatively few steps, before descending to the dense base layer for the careful, fine-grained work of actually pinning down the true nearest neighbors. This coarse-to-fine strategy means the expensive, thorough part of the search only has to operate on the base layer, and only after the search has already been guided into approximately the right region — a meaningfully smaller and more targeted task than searching the full graph carefully from an arbitrary starting point.

How does the neighbor-selection heuristic contribute to retrieval performance, separately from the hierarchy itself?

Fast traversal alone isn’t enough if the graph being traversed is poorly connected — a search can only move as well as the edges available to it allow. HNSW’s neighbor-selection heuristic, by favoring a diversified spread of connections over simply the closest available candidates, produces a graph where a node’s limited set of edges actually leads in several genuinely useful directions rather than clustering redundantly in one. This directly improves the odds that a greedy search finds a productive next step at every point along its path, which translates into both better recall — actually finding the true nearest neighbors more reliably — and better speed, since a well-connected graph tends to reach the right answer in fewer hops than a poorly connected one covering the same data.

These two contributions work together rather than independently: the hierarchy determines how quickly a search can cover distance, while the neighbor-selection heuristic determines how reliably each individual step within any given layer actually leads somewhere useful. A well-built hierarchy searched with a poorly connected graph at each layer would still struggle, just as a well-connected single-layer graph without any hierarchy would still require more hops to get started than a layered structure would.

What trade-off does this performance improvement actually cost, and why is that trade-off usually worth making?

The speedup comes at the cost of the exactness guarantee that brute-force search provides — HNSW’s greedy traversal has no formal proof that it always finds the true nearest neighbors, which is exactly the approximate-versus-exact trade-off covered at length elsewhere in this documentation. In practice, on real, well-behaved embedding data, this trade generally pays off overwhelmingly: the recall achieved is very high, often comfortably above ninety-five percent with reasonable tuning, while the actual computational savings compared to brute-force search can be several orders of magnitude on large datasets, which is precisely why HNSW has become one of the standard choices for production vector retrieval systems operating at real-world scale.

Having covered the combined effect of the layered hierarchy and diversified neighbor selection on retrieval speed, the HNSW-hierarchy and neighbor-selection-heuristic glossary pages are worth revisiting together with this fuller performance picture in mind. From there, the recall-based-approximation and approximate-nearest-neighbor-search glossary pages pick up the trade-off side of this improvement, covering exactly what gets given up in exchange for the speed gains described here.