Vector Search: HNSW, IVF, PQ and LSH

How vector databases find nearest neighbours without checking everything. Step through HNSW, escape an NSW dead end, probe IVF cells, compress with PQ and compare recall against cost.

q20 points → 20 distances

1Exact search measures everything

To find the nearest vectors for certain, compare the query with every stored vector. Fine for thousands; with millions of 768-number embeddings and many queries a second, far too slow.

q6 hops, not 12 measurements

2Graphs: hop toward the query

HNSW links every vector to a few near neighbours, plus a sparse top layer of long links. Start anywhere, keep hopping to whichever neighbour is closer, and you arrive near the query after measuring only a few vectors.

qsearch 1 cell of 4

3Cells and codes: search less, store less

IVF sorts the vectors into cells around k-means centres and searches only the cells nearest the query. Product quantisation squeezes each vector into a few bytes of codes, so millions fit in memory.

The baseline

Brute force: measure everything

RAG, recommendations and semantic search all ask the same question: which stored vectors are nearest to this one? The exact answer is easy: measure the distance to every vector and keep the k smallest. Here 120 points stand in for embeddings (real ones have hundreds of dimensions). Click anywhere to move the query.

02468100246810node 0 (2.916, 6.009)node 1 (2.507, 5.517)node 2 (6.47, 2.201)node 3 (4.228, 6.498)node 4 (5.471, 2.716)node 5 (5.118, 2.619)node 6 (6.436, 2.865)node 7 (3.969, 3.414)node 8 (1.987, 6.573)node 9 (6.905, 3.471)node 10 (5.259, 1.552)node 11 (3.182, 7.068)node 12 (2.63, 6.52)node 13 (6.995, 2.133)node 14 (3.02, 6.644)node 15 (5.682, 2.999)node 16 (1.655, 6.579)node 17 (6.43, 2.896)node 18 (3.692, 1.074)node 19 (3.166, 4.271)node 20 (2.615, 6.673)node 21 (3.048, 6.366)node 22 (4.404, 3.196)node 23 (7.27, 2.87)node 24 (4.483, 6.552)node 25 (1.206, 7.601)node 26 (1.83, 2.781)node 27 (2.736, 7.717)node 28 (4.86, 1.936)node 29 (5.669, 4.935)node 30 (3.774, 6.173)node 31 (3.522, 5.958)node 32 (6.336, 3.545)node 33 (4.963, 0.814)node 34 (5.799, 3.86)node 35 (6.133, 1.903)node 36 (7.884, 3.846)node 37 (5.891, 1.526)node 38 (7.258, 4.195)node 39 (2.741, 6.469)node 40 (1.735, 5.473)node 41 (2.503, 7.594)node 42 (2.849, 5.521)node 43 (6.335, 2.763)node 44 (4.778, 5.29)node 45 (3.037, 7.285)node 46 (3.076, 5.697)node 47 (3.396, 2.461)node 48 (2.597, 7.221)node 49 (8.752, 3.889)node 50 (1.163, 8.231)node 51 (7.408, 3.865)node 52 (6.409, 1.637)node 53 (6.761, 2.899)node 54 (0.883, 7.705)node 55 (5.109, 0.943)node 56 (7.438, 2.947)node 57 (2.475, 6.63)node 58 (4.615, 1.873)node 59 (8.981, 5.622)node 60 (6.768, 1.182)node 61 (2.785, 6.737)node 62 (2.766, 3.154)node 63 (3.254, 2.815)node 64 (7.886, 3.09)node 65 (3.366, 7.153)node 66 (2.462, 6.312)node 67 (2.032, 6.894)node 68 (3.235, 6.691)node 69 (3.334, 3.09)node 70 (8.221, 3.602)node 71 (2.837, 7.379)node 72 (3.812, 6.948)node 73 (5.051, 3.281)node 74 (3.753, 6.286)node 75 (3.688, 5.763)node 76 (2.94, 5.987)node 77 (7.385, 3.136)node 78 (7.487, 3.788)node 79 (0.683, 6.704)node 80 (1.871, 8.318)node 81 (7.738, 4.261)node 82 (2.377, 6.349)node 83 (2.123, 6.372)node 84 (3.668, 3.15)node 85 (2.901, 3.258)node 86 (3.874, 6.898)node 87 (2.569, 4.839)node 88 (2.628, 3.696)node 89 (1.573, 6.308)node 90 (2.153, 5.711)node 91 (4.051, 2.773)node 92 (5.141, 1.729)node 93 (4.623, 7.332)node 94 (2.784, 2.393)node 95 (7.658, 4.033)node 96 (5.123, 5.279)node 97 (1.392, 6.845)node 98 (4.489, 3.825)node 99 (6.352, 5.154)node 100 (3.142, 6.628)node 101 (3.54, 2.786)node 102 (7.401, 2.704)node 103 (5.389, 0.956)node 104 (2.707, 3.418)node 105 (7.074, 2.613)node 106 (2.49, 2.887)node 107 (8.343, 3.394)node 108 (2.394, 6.832)node 109 (2.714, 1.588)node 110 (2.031, 2.183)node 111 (1.236, 6.56)node 112 (2.729, 4.696)node 113 (2.099, 6.368)node 114 (5.356, 2.35)node 115 (7.127, 3.165)node 116 (1.392, 6.203)node 117 (3.236, 6.251)node 118 (3.147, 2.852)node 119 (7.588, 4.517)q
a stored vectorthe 3 nearest
Distances measured
120
Recall
100%

Always right, and the work grows in a straight line with the data: n vectors × d numbers per query. With a million 768-number embeddings that's 768 million multiply-adds for every question.

Approximate nearest neighbour (ANN) indexes give up a little accuracy, measured as recall (the share of the true top k they return), to measure far fewer vectors. Everything below is a different way to do that.

Graphs, part 1

NSW: a navigable small world

Link each point to its 3 nearest neighbours as it is added. Early points were added when the space was empty, so their links are long: highways across the map. That mix of short and long links is a “small world”: any point is a few hops from any other. To search, start at the first point and keep hopping to whichever neighbour is closer to the query.

02468100246810node 0 (2.916, 6.009)node 1 (2.507, 5.517)node 2 (6.47, 2.201)node 3 (4.228, 6.498)node 4 (5.471, 2.716)node 5 (5.118, 2.619)node 6 (6.436, 2.865)node 7 (3.969, 3.414)node 8 (1.987, 6.573)node 9 (6.905, 3.471)node 10 (5.259, 1.552)node 11 (3.182, 7.068)node 12 (2.63, 6.52)node 13 (6.995, 2.133)node 14 (3.02, 6.644)node 15 (5.682, 2.999)node 16 (1.655, 6.579)node 17 (6.43, 2.896)node 18 (3.692, 1.074)node 19 (3.166, 4.271)node 20 (2.615, 6.673)node 21 (3.048, 6.366)node 22 (4.404, 3.196)node 23 (7.27, 2.87)node 24 (4.483, 6.552)node 25 (1.206, 7.601)node 26 (1.83, 2.781)node 27 (2.736, 7.717)node 28 (4.86, 1.936)node 29 (5.669, 4.935)node 30 (3.774, 6.173)node 31 (3.522, 5.958)node 32 (6.336, 3.545)node 33 (4.963, 0.814)node 34 (5.799, 3.86)node 35 (6.133, 1.903)node 36 (7.884, 3.846)node 37 (5.891, 1.526)node 38 (7.258, 4.195)node 39 (2.741, 6.469)node 40 (1.735, 5.473)node 41 (2.503, 7.594)node 42 (2.849, 5.521)node 43 (6.335, 2.763)node 44 (4.778, 5.29)node 45 (3.037, 7.285)node 46 (3.076, 5.697)node 47 (3.396, 2.461)node 48 (2.597, 7.221)node 49 (8.752, 3.889)node 50 (1.163, 8.231)node 51 (7.408, 3.865)node 52 (6.409, 1.637)node 53 (6.761, 2.899)node 54 (0.883, 7.705)node 55 (5.109, 0.943)node 56 (7.438, 2.947)node 57 (2.475, 6.63)node 58 (4.615, 1.873)node 59 (8.981, 5.622)node 60 (6.768, 1.182)node 61 (2.785, 6.737)node 62 (2.766, 3.154)node 63 (3.254, 2.815)node 64 (7.886, 3.09)node 65 (3.366, 7.153)node 66 (2.462, 6.312)node 67 (2.032, 6.894)node 68 (3.235, 6.691)node 69 (3.334, 3.09)node 70 (8.221, 3.602)node 71 (2.837, 7.379)node 72 (3.812, 6.948)node 73 (5.051, 3.281)node 74 (3.753, 6.286)node 75 (3.688, 5.763)node 76 (2.94, 5.987)node 77 (7.385, 3.136)node 78 (7.487, 3.788)node 79 (0.683, 6.704)node 80 (1.871, 8.318)node 81 (7.738, 4.261)node 82 (2.377, 6.349)node 83 (2.123, 6.372)node 84 (3.668, 3.15)node 85 (2.901, 3.258)node 86 (3.874, 6.898)node 87 (2.569, 4.839)node 88 (2.628, 3.696)node 89 (1.573, 6.308)node 90 (2.153, 5.711)node 91 (4.051, 2.773)node 92 (5.141, 1.729)node 93 (4.623, 7.332)node 94 (2.784, 2.393)node 95 (7.658, 4.033)node 96 (5.123, 5.279)node 97 (1.392, 6.845)node 98 (4.489, 3.825)node 99 (6.352, 5.154)node 100 (3.142, 6.628)node 101 (3.54, 2.786)node 102 (7.401, 2.704)node 103 (5.389, 0.956)node 104 (2.707, 3.418)node 105 (7.074, 2.613)node 106 (2.49, 2.887)node 107 (8.343, 3.394)node 108 (2.394, 6.832)node 109 (2.714, 1.588)node 110 (2.031, 2.183)node 111 (1.236, 6.56)node 112 (2.729, 4.696)node 113 (2.099, 6.368)node 114 (5.356, 2.35)node 115 (7.127, 3.165)node 116 (1.392, 6.203)node 117 (3.236, 6.251)node 118 (3.147, 2.852)node 119 (7.588, 4.517)q
the walk, and points measuredwhere the search endedthe true nearest
Search
Measured
24 of 120
Found the nearest?
No

Stuck in a local minimum: none of this point's neighbours is closer to the query, yet the true nearest (green ring) is elsewhere. Greedy search can't see past it. Try the beam.

Click the plot to try your own query. Big NSW graphs have a second problem: early nodes collect huge numbers of links, so each hop gets expensive.

Graphs, part 2

HNSW: layers of small worlds

HNSW fixes both problems with a hierarchy, like a skip list. Every point is on layer 0, with short links. A random few also join layer 1, fewer still layer 2, and so on, each layer a sparser graph with longer links. Search starts at the top, takes big greedy hops, drops a layer, takes smaller hops, and finishes with a beam search of width ef on layer 0. Step through it, change ef, or click to move the query.

02468100246810node 0 (2.916, 6.009)node 1 (2.507, 5.517)node 2 (6.47, 2.201)node 3 (4.228, 6.498)node 4 (5.471, 2.716)node 5 (5.118, 2.619)node 6 (6.436, 2.865)node 7 (3.969, 3.414)node 8 (1.987, 6.573)node 9 (6.905, 3.471)node 10 (5.259, 1.552)node 11 (3.182, 7.068)node 12 (2.63, 6.52)node 13 (6.995, 2.133)node 14 (3.02, 6.644)node 15 (5.682, 2.999)node 16 (1.655, 6.579)node 17 (6.43, 2.896)node 18 (3.692, 1.074)node 19 (3.166, 4.271)node 20 (2.615, 6.673)node 21 (3.048, 6.366)node 22 (4.404, 3.196)node 23 (7.27, 2.87)node 24 (4.483, 6.552)node 25 (1.206, 7.601)node 26 (1.83, 2.781)node 27 (2.736, 7.717)node 28 (4.86, 1.936)node 29 (5.669, 4.935)node 30 (3.774, 6.173)node 31 (3.522, 5.958)node 32 (6.336, 3.545)node 33 (4.963, 0.814)node 34 (5.799, 3.86)node 35 (6.133, 1.903)node 36 (7.884, 3.846)node 37 (5.891, 1.526)node 38 (7.258, 4.195)node 39 (2.741, 6.469)node 40 (1.735, 5.473)node 41 (2.503, 7.594)node 42 (2.849, 5.521)node 43 (6.335, 2.763)node 44 (4.778, 5.29)node 45 (3.037, 7.285)node 46 (3.076, 5.697)node 47 (3.396, 2.461)node 48 (2.597, 7.221)node 49 (8.752, 3.889)node 50 (1.163, 8.231)node 51 (7.408, 3.865)node 52 (6.409, 1.637)node 53 (6.761, 2.899)node 54 (0.883, 7.705)node 55 (5.109, 0.943)node 56 (7.438, 2.947)node 57 (2.475, 6.63)node 58 (4.615, 1.873)node 59 (8.981, 5.622)node 60 (6.768, 1.182)node 61 (2.785, 6.737)node 62 (2.766, 3.154)node 63 (3.254, 2.815)node 64 (7.886, 3.09)node 65 (3.366, 7.153)node 66 (2.462, 6.312)node 67 (2.032, 6.894)node 68 (3.235, 6.691)node 69 (3.334, 3.09)node 70 (8.221, 3.602)node 71 (2.837, 7.379)node 72 (3.812, 6.948)node 73 (5.051, 3.281)node 74 (3.753, 6.286)node 75 (3.688, 5.763)node 76 (2.94, 5.987)node 77 (7.385, 3.136)node 78 (7.487, 3.788)node 79 (0.683, 6.704)node 80 (1.871, 8.318)node 81 (7.738, 4.261)node 82 (2.377, 6.349)node 83 (2.123, 6.372)node 84 (3.668, 3.15)node 85 (2.901, 3.258)node 86 (3.874, 6.898)node 87 (2.569, 4.839)node 88 (2.628, 3.696)node 89 (1.573, 6.308)node 90 (2.153, 5.711)node 91 (4.051, 2.773)node 92 (5.141, 1.729)node 93 (4.623, 7.332)node 94 (2.784, 2.393)node 95 (7.658, 4.033)node 96 (5.123, 5.279)node 97 (1.392, 6.845)node 98 (4.489, 3.825)node 99 (6.352, 5.154)node 100 (3.142, 6.628)node 101 (3.54, 2.786)node 102 (7.401, 2.704)node 103 (5.389, 0.956)node 104 (2.707, 3.418)node 105 (7.074, 2.613)node 106 (2.49, 2.887)node 107 (8.343, 3.394)node 108 (2.394, 6.832)node 109 (2.714, 1.588)node 110 (2.031, 2.183)node 111 (1.236, 6.56)node 112 (2.729, 4.696)node 113 (2.099, 6.368)node 114 (5.356, 2.35)node 115 (7.127, 3.165)node 116 (1.392, 6.203)node 117 (3.236, 6.251)node 118 (3.147, 2.852)node 119 (7.588, 4.517)q
path and measured pointsbest 3 so fartrue nearest (at the end)
L3: 3L2: 9L1: 34L0: 120
6
Step 0 / 21
800 ms

Press Start to search for the 3 nearest neighbours of the query, from the top layer down.

Building it

Each new point draws a random top layer: layer l with probability M−l. Then it is inserted like a search: greedy down to its layer, and on each layer below, a beam search of width efConstruction finds the M nearest to link to. Here M = 4, efConstruction = 16, and the layers hold 120, 34, 9, 3 nodes.

The three settings

M: links per node; more means better recall and more memory. efConstruction: effort at build time; better graphs, slower inserts. ef (efSearch): effort per query, the dial between recall and latency. Only ef can be changed after building.

Why it's the default

Excellent recall at low cost, and points can be added at any time. The price is memory: the full vectors plus all the links stay in RAM, and deletes are awkward. Qdrant, Weaviate, pgvector, Elasticsearch, OpenSearch, Milvus and Faiss all offer HNSW.

Partitions

IVF: search only the nearest cells

An inverted file index runs k-means once to split the vectors into cells (here 9), and stores a list of members for each cell. A query is compared with the 9 centres, then only the vectors in the nearest nprobe cells are measured.

02468100246810node 0 (2.916, 6.009)node 1 (2.507, 5.517)node 2 (6.47, 2.201)node 3 (4.228, 6.498)node 4 (5.471, 2.716)node 5 (5.118, 2.619)node 6 (6.436, 2.865)node 7 (3.969, 3.414)node 8 (1.987, 6.573)node 9 (6.905, 3.471)node 10 (5.259, 1.552)node 11 (3.182, 7.068)node 12 (2.63, 6.52)node 13 (6.995, 2.133)node 14 (3.02, 6.644)node 15 (5.682, 2.999)node 16 (1.655, 6.579)node 17 (6.43, 2.896)node 18 (3.692, 1.074)node 19 (3.166, 4.271)node 20 (2.615, 6.673)node 21 (3.048, 6.366)node 22 (4.404, 3.196)node 23 (7.27, 2.87)node 24 (4.483, 6.552)node 25 (1.206, 7.601)node 26 (1.83, 2.781)node 27 (2.736, 7.717)node 28 (4.86, 1.936)node 29 (5.669, 4.935)node 30 (3.774, 6.173)node 31 (3.522, 5.958)node 32 (6.336, 3.545)node 33 (4.963, 0.814)node 34 (5.799, 3.86)node 35 (6.133, 1.903)node 36 (7.884, 3.846)node 37 (5.891, 1.526)node 38 (7.258, 4.195)node 39 (2.741, 6.469)node 40 (1.735, 5.473)node 41 (2.503, 7.594)node 42 (2.849, 5.521)node 43 (6.335, 2.763)node 44 (4.778, 5.29)node 45 (3.037, 7.285)node 46 (3.076, 5.697)node 47 (3.396, 2.461)node 48 (2.597, 7.221)node 49 (8.752, 3.889)node 50 (1.163, 8.231)node 51 (7.408, 3.865)node 52 (6.409, 1.637)node 53 (6.761, 2.899)node 54 (0.883, 7.705)node 55 (5.109, 0.943)node 56 (7.438, 2.947)node 57 (2.475, 6.63)node 58 (4.615, 1.873)node 59 (8.981, 5.622)node 60 (6.768, 1.182)node 61 (2.785, 6.737)node 62 (2.766, 3.154)node 63 (3.254, 2.815)node 64 (7.886, 3.09)node 65 (3.366, 7.153)node 66 (2.462, 6.312)node 67 (2.032, 6.894)node 68 (3.235, 6.691)node 69 (3.334, 3.09)node 70 (8.221, 3.602)node 71 (2.837, 7.379)node 72 (3.812, 6.948)node 73 (5.051, 3.281)node 74 (3.753, 6.286)node 75 (3.688, 5.763)node 76 (2.94, 5.987)node 77 (7.385, 3.136)node 78 (7.487, 3.788)node 79 (0.683, 6.704)node 80 (1.871, 8.318)node 81 (7.738, 4.261)node 82 (2.377, 6.349)node 83 (2.123, 6.372)node 84 (3.668, 3.15)node 85 (2.901, 3.258)node 86 (3.874, 6.898)node 87 (2.569, 4.839)node 88 (2.628, 3.696)node 89 (1.573, 6.308)node 90 (2.153, 5.711)node 91 (4.051, 2.773)node 92 (5.141, 1.729)node 93 (4.623, 7.332)node 94 (2.784, 2.393)node 95 (7.658, 4.033)node 96 (5.123, 5.279)node 97 (1.392, 6.845)node 98 (4.489, 3.825)node 99 (6.352, 5.154)node 100 (3.142, 6.628)node 101 (3.54, 2.786)node 102 (7.401, 2.704)node 103 (5.389, 0.956)node 104 (2.707, 3.418)node 105 (7.074, 2.613)node 106 (2.49, 2.887)node 107 (8.343, 3.394)node 108 (2.394, 6.832)node 109 (2.714, 1.588)node 110 (2.031, 2.183)node 111 (1.236, 6.56)node 112 (2.729, 4.696)node 113 (2.099, 6.368)node 114 (5.356, 2.35)node 115 (7.127, 3.165)node 116 (1.392, 6.203)node 117 (3.236, 6.251)node 118 (3.147, 2.852)node 119 (7.588, 4.517)q
cell centre (filled: probed)measuredreturnedtrue nearest
1 of 9
Measured
18 of 120
Recall@3
33%

A true neighbour sits just across a cell wall, in a cell that wasn't searched. Raise nprobe.

Typical settings: about √n cells (a million vectors → ~1,000) and nprobe of 1–5% of them. Cheap to build and small in memory; recall drops at cell edges.

Compression

Product quantisation: vectors as a few bytes

A million 768-number vectors are 3 GB of floats. Quantisation stores each vector as the id of the nearest entry in a small codebook. In 2-D below, each point is replaced by its nearest diamond: with 16 diamonds, a 4-bit code. Distances are then computed to the diamonds, a little off, but very cheap.

02468100246810node 0 (2.916, 6.009)node 1 (2.507, 5.517)node 2 (6.47, 2.201)node 3 (4.228, 6.498)node 4 (5.471, 2.716)node 5 (5.118, 2.619)node 6 (6.436, 2.865)node 7 (3.969, 3.414)node 8 (1.987, 6.573)node 9 (6.905, 3.471)node 10 (5.259, 1.552)node 11 (3.182, 7.068)node 12 (2.63, 6.52)node 13 (6.995, 2.133)node 14 (3.02, 6.644)node 15 (5.682, 2.999)node 16 (1.655, 6.579)node 17 (6.43, 2.896)node 18 (3.692, 1.074)node 19 (3.166, 4.271)node 20 (2.615, 6.673)node 21 (3.048, 6.366)node 22 (4.404, 3.196)node 23 (7.27, 2.87)node 24 (4.483, 6.552)node 25 (1.206, 7.601)node 26 (1.83, 2.781)node 27 (2.736, 7.717)node 28 (4.86, 1.936)node 29 (5.669, 4.935)node 30 (3.774, 6.173)node 31 (3.522, 5.958)node 32 (6.336, 3.545)node 33 (4.963, 0.814)node 34 (5.799, 3.86)node 35 (6.133, 1.903)node 36 (7.884, 3.846)node 37 (5.891, 1.526)node 38 (7.258, 4.195)node 39 (2.741, 6.469)node 40 (1.735, 5.473)node 41 (2.503, 7.594)node 42 (2.849, 5.521)node 43 (6.335, 2.763)node 44 (4.778, 5.29)node 45 (3.037, 7.285)node 46 (3.076, 5.697)node 47 (3.396, 2.461)node 48 (2.597, 7.221)node 49 (8.752, 3.889)node 50 (1.163, 8.231)node 51 (7.408, 3.865)node 52 (6.409, 1.637)node 53 (6.761, 2.899)node 54 (0.883, 7.705)node 55 (5.109, 0.943)node 56 (7.438, 2.947)node 57 (2.475, 6.63)node 58 (4.615, 1.873)node 59 (8.981, 5.622)node 60 (6.768, 1.182)node 61 (2.785, 6.737)node 62 (2.766, 3.154)node 63 (3.254, 2.815)node 64 (7.886, 3.09)node 65 (3.366, 7.153)node 66 (2.462, 6.312)node 67 (2.032, 6.894)node 68 (3.235, 6.691)node 69 (3.334, 3.09)node 70 (8.221, 3.602)node 71 (2.837, 7.379)node 72 (3.812, 6.948)node 73 (5.051, 3.281)node 74 (3.753, 6.286)node 75 (3.688, 5.763)node 76 (2.94, 5.987)node 77 (7.385, 3.136)node 78 (7.487, 3.788)node 79 (0.683, 6.704)node 80 (1.871, 8.318)node 81 (7.738, 4.261)node 82 (2.377, 6.349)node 83 (2.123, 6.372)node 84 (3.668, 3.15)node 85 (2.901, 3.258)node 86 (3.874, 6.898)node 87 (2.569, 4.839)node 88 (2.628, 3.696)node 89 (1.573, 6.308)node 90 (2.153, 5.711)node 91 (4.051, 2.773)node 92 (5.141, 1.729)node 93 (4.623, 7.332)node 94 (2.784, 2.393)node 95 (7.658, 4.033)node 96 (5.123, 5.279)node 97 (1.392, 6.845)node 98 (4.489, 3.825)node 99 (6.352, 5.154)node 100 (3.142, 6.628)node 101 (3.54, 2.786)node 102 (7.401, 2.704)node 103 (5.389, 0.956)node 104 (2.707, 3.418)node 105 (7.074, 2.613)node 106 (2.49, 2.887)node 107 (8.343, 3.394)node 108 (2.394, 6.832)node 109 (2.714, 1.588)node 110 (2.031, 2.183)node 111 (1.236, 6.56)node 112 (2.729, 4.696)node 113 (2.099, 6.368)node 114 (5.356, 2.35)node 115 (7.127, 3.165)node 116 (1.392, 6.203)node 117 (3.236, 6.251)node 118 (3.147, 2.852)node 119 (7.588, 4.517)q
codebook entryeach point's code
16 (4 bits)
Bits per point
4 (not 64)
Avg error²
0.337

One codebook for a 768-dimension space would need billions of entries. The “product” trick: cut the vector into m pieces (say 96 pieces of 8 numbers) and give each piece its own 256-entry codebook. 96 one-byte codes, and 256⁹⁶ possible combinations.

One 8-number vector, cut into 4 pieces, each piece replaced by a code:

0.12 −0.80↓37
0.55 0.02↓201
−0.33 0.91↓8
0.70 −0.14↓142
32 bytes of floats → 4 bytes of codes

At query time, measure the query's pieces against each codebook once (a small table), then each stored vector's distance is just m table lookups added up.

Hashing

LSH: random planes as a hash

Locality-sensitive hashing draws random planes. Each one gives a bit: which side is the vector on? Vectors with the same bits share a bucket, and only the query's bucket is searched. Near vectors usually agree on every bit; far ones rarely do. The shaded region is the query's bucket.

02468100246810node 0 (2.916, 6.009)node 1 (2.507, 5.517)node 2 (6.47, 2.201)node 3 (4.228, 6.498)node 4 (5.471, 2.716)node 5 (5.118, 2.619)node 6 (6.436, 2.865)node 7 (3.969, 3.414)node 8 (1.987, 6.573)node 9 (6.905, 3.471)node 10 (5.259, 1.552)node 11 (3.182, 7.068)node 12 (2.63, 6.52)node 13 (6.995, 2.133)node 14 (3.02, 6.644)node 15 (5.682, 2.999)node 16 (1.655, 6.579)node 17 (6.43, 2.896)node 18 (3.692, 1.074)node 19 (3.166, 4.271)node 20 (2.615, 6.673)node 21 (3.048, 6.366)node 22 (4.404, 3.196)node 23 (7.27, 2.87)node 24 (4.483, 6.552)node 25 (1.206, 7.601)node 26 (1.83, 2.781)node 27 (2.736, 7.717)node 28 (4.86, 1.936)node 29 (5.669, 4.935)node 30 (3.774, 6.173)node 31 (3.522, 5.958)node 32 (6.336, 3.545)node 33 (4.963, 0.814)node 34 (5.799, 3.86)node 35 (6.133, 1.903)node 36 (7.884, 3.846)node 37 (5.891, 1.526)node 38 (7.258, 4.195)node 39 (2.741, 6.469)node 40 (1.735, 5.473)node 41 (2.503, 7.594)node 42 (2.849, 5.521)node 43 (6.335, 2.763)node 44 (4.778, 5.29)node 45 (3.037, 7.285)node 46 (3.076, 5.697)node 47 (3.396, 2.461)node 48 (2.597, 7.221)node 49 (8.752, 3.889)node 50 (1.163, 8.231)node 51 (7.408, 3.865)node 52 (6.409, 1.637)node 53 (6.761, 2.899)node 54 (0.883, 7.705)node 55 (5.109, 0.943)node 56 (7.438, 2.947)node 57 (2.475, 6.63)node 58 (4.615, 1.873)node 59 (8.981, 5.622)node 60 (6.768, 1.182)node 61 (2.785, 6.737)node 62 (2.766, 3.154)node 63 (3.254, 2.815)node 64 (7.886, 3.09)node 65 (3.366, 7.153)node 66 (2.462, 6.312)node 67 (2.032, 6.894)node 68 (3.235, 6.691)node 69 (3.334, 3.09)node 70 (8.221, 3.602)node 71 (2.837, 7.379)node 72 (3.812, 6.948)node 73 (5.051, 3.281)node 74 (3.753, 6.286)node 75 (3.688, 5.763)node 76 (2.94, 5.987)node 77 (7.385, 3.136)node 78 (7.487, 3.788)node 79 (0.683, 6.704)node 80 (1.871, 8.318)node 81 (7.738, 4.261)node 82 (2.377, 6.349)node 83 (2.123, 6.372)node 84 (3.668, 3.15)node 85 (2.901, 3.258)node 86 (3.874, 6.898)node 87 (2.569, 4.839)node 88 (2.628, 3.696)node 89 (1.573, 6.308)node 90 (2.153, 5.711)node 91 (4.051, 2.773)node 92 (5.141, 1.729)node 93 (4.623, 7.332)node 94 (2.784, 2.393)node 95 (7.658, 4.033)node 96 (5.123, 5.279)node 97 (1.392, 6.845)node 98 (4.489, 3.825)node 99 (6.352, 5.154)node 100 (3.142, 6.628)node 101 (3.54, 2.786)node 102 (7.401, 2.704)node 103 (5.389, 0.956)node 104 (2.707, 3.418)node 105 (7.074, 2.613)node 106 (2.49, 2.887)node 107 (8.343, 3.394)node 108 (2.394, 6.832)node 109 (2.714, 1.588)node 110 (2.031, 2.183)node 111 (1.236, 6.56)node 112 (2.729, 4.696)node 113 (2.099, 6.368)node 114 (5.356, 2.35)node 115 (7.127, 3.165)node 116 (1.392, 6.203)node 117 (3.236, 6.251)node 118 (3.147, 2.852)node 119 (7.588, 4.517)q
random planein the query's buckettrue nearest
2
Measured
54 of 120
Recall@3
100%

More planes, smaller buckets: less to measure, but a true neighbour on the wrong side of any one plane is lost. Real LSH keeps several tables with different random planes and searches the query's bucket in each, so a neighbour split off in one is caught in another.

Simple, with no training and easy updates, but it usually needs more memory and gives lower recall than HNSW or IVF on real embeddings.

Measured, not claimed

The recall–cost trade-off

Pictures in 2-D only go so far. Here are the three indexes built for real on 2,000 clustered vectors in 32 dimensions, with 40 queries. For each setting: the share of each query's true top 10 returned, and the share of vectors measured to get it.

Building three indexes over 2,000 vectors and timing 40 queries…

In practice

Choosing an index

IndexRecallMemoryBuildAdding dataGood forFound in
Flat (brute force)ExactVectors onlyNoneInstantUnder ~100k vectors; checking other indexesEvery library
HNSWVery highVectors + links (high)SlowEasy; deletes awkwardThe default for in-memory searchQdrant, Weaviate, pgvector, Elasticsearch, Milvus, Faiss
IVFGood; drops at cell edgesLowFast (k-means)Easy; retrain cells if data driftsLarge sets, tighter memoryFaiss, Milvus, pgvector (IVFFlat)
IVF-PQLower; fix with re-rankingVery lowModerateEasyHundreds of millions of vectorsFaiss, Milvus
LSHLowerHigh (many tables)InstantEasyStreaming data, theoryFaiss, older systems
DiskANN / ScaNNVery highMostly on SSD / compressedSlowVariesBillions of vectorsMilvus, Azure, Google (ScaNN)

Match the metric

Use the distance your embedding model was trained for. For cosine similarity, normalise vectors to length 1: then Euclidean distance and dot product give the same ranking. Embeddings →

Filtering is hard

“Nearest, but only from 2024” breaks graph search: filtered-out nodes still carry the links. Vector databases add filter-aware traversal or pre-filtering, and it's worth testing.

Measure your recall

Run brute force on a sample of queries and compare. Tune ef or nprobe until recall is high enough, then check latency. Don't trust defaults blindly.

Why trees don't work here

KD-trees and ball trees are exact and fast in 2-D or 3-D, but in hundreds of dimensions every branch is about equally far, so they end up checking almost everything. The curse of dimensionality →

See vector search used in a full pipeline in RAG and the Advanced RAG course.

No magic, just arithmetic

The maths, step by step

A realistic index: 1,000,000 embeddings of 768 numbers each. What does each method cost per query, and why does each one work?

  1. 1

    Brute force

    One distance per vector, and each distance is d multiply-adds.

    multiply-adds per query = 1,000,000 × 768 = 768000000.0000

    768,000,000 operations for every single query: about 3 GB of floats read each time.

  2. 2

    IVF

    1,000 cells of about 1,000 vectors. Measure the query against the cell centres, then scan only the nearest 10 cells.

    vectors measured = 1,000 centres + 10 × 1,000 = 11000.0000

    share of brute force = 11,000 ÷ 1,000,000 = 0.0110

    About 1% of the work. The risk: a true neighbour sitting just across a cell wall, in a cell you didn't probe.

  3. 3

    HNSW layers

    Each new point reaches layer l with probability M^−l (here M = 16), so each layer keeps about 1/16 of the one below. Search drops through about log_M(n) layers.

    nodes on layer 1 = 1,000,000 × 16^−1 = 62500.0000

    nodes on layer 2 = 1,000,000 × 16^−2 = 3906.2500

    nodes on layer 3 = 1,000,000 × 16^−3 = 244.1406

    nodes on layer 4 = 1,000,000 × 16^−4 = 15.2588

    nodes on layer 5 = 1,000,000 × 16^−5 = 0.9537

    layers, log₁₆(n) = ln 1,000,000 ÷ ln 16 = 4.9829

    A handful of long hops on the sparse top layers, then a local beam search at the bottom: the cost grows roughly with log n, not n.

  4. 4

    Product quantisation memory

    Cut each vector into 96 pieces of 8 numbers; replace each piece by the id (one byte) of its nearest centroid among 256.

    bytes per vector, floats = 768 × 4 = 3072.0000

    bytes per vector, PQ = 96 × 1 = 96.0000

    whole index, floats (GB) = 1,000,000 × 3072 ÷ 10⁹ = 3.0720

    whole index, PQ (GB) = 1,000,000 × 96 ÷ 10⁹ = 0.0960

  5. 5

    Why LSH finds near vectors

    A random plane separates two vectors at angle θ with probability θ / 180°. Near vectors (small θ) usually agree on every bit; far ones rarely do.

    one bit agrees, θ = 30° = 1 − 30/180 = 0.8333

    all 8 bits agree = 0.8333^8 = 0.2326

    caught by 1 of 8 tables = 1 − (1 − 0.2326)^8 = 0.8797

    same, far vector θ = 90° = 1 − (1 − 0.5^8)^8 = 0.0308

    More bits make buckets smaller (fewer candidates, but near vectors get split up); more tables catch them again.

Keep going

Resources

Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.

How it works: Vector Search (HNSW)

Step by step

  • The search path and measured vectors
  • Best found so far
  • True nearest neighbours

Press Start or Step: the step the animation is on lights up below.

  1. 1

    Enter at the top

    Start at the single entry point on the highest layer, which has only a handful of nodes and long links.

    cur = entry_point; L = top_layer
  2. 2

    Greedy hops

    On an upper layer, move to whichever neighbour is closer to the query; stop when none is.

    while some nb in N_L(cur) is closer: cur = nb
  3. 3

    Drop a layer

    Keep the same node and move down to the next, denser layer with shorter links.

    L = L - 1
  4. 4

    Beam search on layer 0

    Keep the ef best nodes found; repeatedly expand the nearest unexpanded candidate's neighbours; stop when the next candidate is worse than the worst kept.

    W = search_layer(q, cur, ef, layer=0)
  5. 5

    Return the k nearest

    The k best of the ef kept. Bigger ef means more distances measured and higher recall.

    return top_k(W)

Vector search finds the stored vectors nearest to a query vector. Exact (brute-force) search measures all n vectors of d numbers: always right, but O(n·d) per query. Approximate nearest-neighbour (ANN) indexes measure far fewer vectors and return most of the true neighbours; the share returned is the recall.

Graph indexes link each vector to nearby ones. NSW is one such graph: greedy hops toward the query, but it can stop in a local minimum. HNSW stacks sparser graphs on top (each point reaches layer l with probability M^−l), searches greedily from the top, and finishes with a beam search of width ef on the bottom layer.

Other families: IVF splits the space into k-means cells and searches only the nprobe nearest cells; product quantisation (PQ) compresses each vector into a few bytes of codebook ids; LSH hashes with random hyperplanes so near vectors tend to share a bucket. Production systems combine them, such as IVF-PQ with re-ranking.

Where you'll meet it

  • Retrieval for RAG, semantic search, recommendations and de-duplication over embeddings, once brute force is too slow (roughly beyond 100k vectors or for many queries a second).
  • HNSW when the index fits in memory and recall matters; IVF or IVF-PQ when memory is tight or the collection is huge; brute force for small sets and for measuring the recall of any index.
  • Interview favourites: “how does HNSW work?”, “what do M, efConstruction and ef do?”, “HNSW vs IVF?”, “what is product quantisation?”, “how do you measure recall?”, “why don't KD-trees work in high dimensions?”.
time about O(log n) per query (HNSW); O(n · d) brute forcespace O(n · d + n · M) for HNSW

HNSW visits a few nodes per layer across about log_M(n) layers, plus an ef-wide beam at the bottom; each visit measures one d-dimensional distance. Building costs about n · log n · efConstruction distance checks. IVF measures nlist centres plus about n · nprobe / nlist vectors; PQ stores m bytes per vector instead of 4d.