deep · 6 min
Vector index
A structure that finds the nearest stored vectors without comparing against all of them, trading away some recall for speed.
updated 27 Sept 2026
We've cut our documents into chunks and turned each one into an embedding. A question comes in, we embed it, and we want the five chunks whose vectors sit closest to it. That's semantic search. How do we find those five?
The obvious way: measure the distance from the question to every chunk and keep the five smallest. That's called exact, brute-force or flat search, and it's always right. The five we get back really are the five nearest. In the jargon, perfect recall.
Now count the work. Each comparison touches every number in both vectors. With 300-number vectors and 10 million chunks, one question costs 300 × 10 million, 3 billion multiplications. Double the chunks and the work doubles. Grow the collection a hundredfold and every query gets a hundred times slower.
A vector index is the way out. It finds the nearest vectors without comparing the question to all of them. But before we build one, there's an earlier question.
Do we even need one?
Run the same sum on a small collection. 5,000 chunks at 300 numbers is 1.5 million multiplications per question, about 2,000 times less work. Bigger vectors scale that up, but the gap stays huge.
Small collections have other things going for them, too. In Postgres, pgvector searches exactly unless you create an index, so perfect recall comes with no setup. When the whole collection fits in memory many times over, a flat search that stores just the vectors costs nothing extra. And there's nothing to tune.
So the honest first step is to time the exact query on your real data. If it's fast enough, stop. An index would give up recall for speed you don't need.
What an index gives up
When exact search does get too slow, we switch to approximate nearest neighbour (ANN) search. An ANN index only looks at a small part of the collection for each question. That's why it's fast, and it's also why it can miss: a true neighbour in the part it didn't look at never comes back.
We score that with recall: of the true five nearest, how many did the index return? It's the same idea as recall@k, except the answer key is the exact search's result, not human labels.
Every ANN index has a knob that trades recall for speed. Here's a real one, pgvector's HNSW index on a standard set of a million vectors with 1,536 numbers each. The knob is how many candidates the search keeps while it explores:
Going from 85% to 97% recall halves the speed. The last steps cost the most: 99.6% to 99.9% cuts it by about two thirds again. The slowest 1% of queries go from 1.4 ms at the fast end to 30.5 ms at the careful end.
A good way to read a chart like this: only settings on the outer edge are worth using. Anything that's both slower and less accurate than another setting can be thrown out. Picking a setting means picking a point on that edge that fits your speed budget and the lowest recall you'll accept.
Two ways to skip most of the vectors
People have tried trees, hashing, clustering and graphs. Trees and hashing stop helping past about 10 dimensions, and embeddings have hundreds or thousands, so the two families that matter are clustering and graphs.
Clustering (IVF, short for inverted file). Before any search, group the vectors into clusters with k-means, each with a centre. At query time, find the few centres nearest the question and scan only those clusters. The knob is how many clusters to scan. Scan one and it's fast, but a neighbour sitting just over a cluster's border gets missed. Scan them all and you're back to exact search. Since the clusters are learned from the data, you need data before you build, and if the data drifts a lot later, the clusters fit it worse.
Graphs. Link each vector to some of its near neighbours, so the collection becomes a graph. To search, start somewhere and keep stepping to whichever neighbour is closer to the question. The knob is how much exploring the walk does. The popular one is HNSW, which promotes some vectors to hubs the search visits first. It needs no training step and takes new vectors one at a time.
The other bills
Recall and speed get the attention, but an index also costs memory and build time.
A flat index stores the vectors and nothing else. A graph index stores each vector plus its links. In the Faiss library's HNSW, a 1,536-number vector with 16 links is 6,144 bytes of vector plus 128 bytes of links. Faiss keeps every index in RAM, and at scale even the raw vectors may not fit, so you compress them by storing smaller numbers or even single bits and accept some loss (quantization).
Some indexes need a training step (the k-means for clustering) before any vectors go in, and all of them spend time per vector added. In pgvector, the graph index builds more slowly than the clustering one. Deleting and changing vectors is awkward too: Faiss's HNSW can't do it at all, and libraries that patch the graph pay for it in index quality.
The Faiss team's rough guide by size: graph indexes are a good choice when memory isn't tight, typically under a million vectors. Past 10 million, build time gets in the way.
Traps
Filters break the simple picture. Real queries often add a condition, like only this user's documents. An approximate index often finds its candidates first and filters after. In pgvector, if the filter matches 10% of rows and the search keeps its default 40 candidates, about 4 pass. Ask for 10, get 4. (Newer versions can keep scanning until enough pass.) The other order works too: filter first, then compare the question with every row that passed. Slow if many pass, but exact, and fine when few do.
Benchmarks from standalone libraries don't always carry over to databases. A 2026 study inside a Postgres-compatible database found that page reads and filter checks changed which index won; graph indexes could make so many filter checks that clustering beat them. It didn't test pgvector's indexes, so take it as a reason to measure on your own setup, not a verdict.
An approximate index also changes your results. A normal database index only makes a query faster. This one changes which rows come back, so a test that passed with exact search can fail after you add it. Measure recall yourself by running the same queries with and without the index.
And the one people most often mix up: index recall isn't answer quality. At 99% recall, the index returns 99% of what exact search would. But if the embedding puts the wrong chunks closest, exact search returns the wrong chunks too, perfectly. Whether the nearest chunks are the right ones is a separate question, measured against questions with known good answers. That's retrieval evaluation.
Further reading
- The Faiss library, Douze et al. (Meta), 2024 (revised 2025). The whole trade-off space, from the people who built the most-used library. Sections 3 and 5 are the useful ones.
- Guidelines to choose an index, Faiss team, undated. A short decision guide. Good to have open when you pick.
- Vector Search Explained, Victoria Slocum (Weaviate), 2024. A plain explainer of exact versus approximate, with the 3 billion example.
- Scalar and Binary Quantization for Pgvector Vector Search and Storage, Jonathan Katz, 2024. Where the recall and speed numbers above come from.
- pgvector, Andrew Kane and contributors, 0.8.6 (2026). Exact by default, the index options, and the filter arithmetic.
- An In-Depth Study of Filter-Agnostic Vector Search on a PostgreSQL Database System, Lu et al. (Google), SIGMOD 2026. For when you filter heavily and want to know why it behaves differently in a database.