HNSW index

Hierarchical Navigable Small World — the most popular ANN index algorithm. Offers excellent query speed/recall tradeoff. Used by Pinecone, Weaviate, and pgvector.

Syntax

vector-databases
CREATE INDEX USING hnsw (embedding vector_cosine_ops) WITH (m=16, ef_construction=64)

Example

vector-databases
-- pgvector HNSW index:
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)
WITH (m = 16, ef_construction = 64);

-- At query time, set ef (search depth):
SET hnsw.ef_search = 100;

SELECT id, content
FROM items
ORDER BY embedding <-> $1::vector
LIMIT 10;

-- Parameters:
-- m: edges per node (higher = better recall, more memory)
-- ef_construction: build quality (higher = slower build, better index)