HNSW (Hierarchical Navigable Small World) — graph-based ANN algorithm, the most popular for vector DB. Builds a multi-layer graph: top layer sparse, bottom dense. Search: greedy descent from top to bottom. O(log N) complexity. Used in Qdrant, Pinecone, Weaviate, pgvector (opt-in). Parameters: M (connections per node, 16-64), ef_construction (build quality), ef (search quality).
Below: details, example, related terms, FAQ.
# pgvector HNSW
CREATE INDEX ON docs USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
-- Query tuning
SET hnsw.ef_search = 100; -- runtime param
SELECT * FROM docs ORDER BY embedding <=> query_vec LIMIT 5;HNSW: best recall + speed, but all in RAM. IVF: cheaper RAM (centroids + buckets), slower recall. For huge datasets (>100M) — IVF + re-ranking.
Prefilter can cut graph connectivity. Quality vector DBs (Qdrant, Weaviate) have filter-aware HNSW. pgvector added index filtering in 2024.
DiskANN — SSD-based ANN. 10× cheaper memory, 2-3× slower. For billion-scale. Milvus, MyScale support it.