HNSW index
- Category
- Retrieval & RAG
- Also known as
- Hierarchical Navigable Small World
- ANN index
- Related terms
- Vector search
- Embeddings
- pgvector
One-line definition
A graph-based index that finds approximately nearest vectors quickly, without comparing the query to every row.
Plain-English explanation
HNSW (Hierarchical Navigable Small World) builds layers of links between similar vectors. A search starts at the sparse top layer and walks down towards the closest matches. It trades a little accuracy for a lot of speed. In Postgres with pgvector, it only helps when the query is ORDER BY embedding <=> $1 LIMIT n directly on the indexed table.
Real example from my work
My assistant's HNSW index was never used, because a DISTINCT ON above the search made Postgres sort by id first and compute the distance for every row.
Why it matters
If retrieval is slow, the first question is whether the index is being used at all.