C.W.K.
Stream
Lesson 03 of 05 · published

HNSW vs IVFFlat: Picking the Index

~22 min · pgvector, indexes, performance

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Two indexes ship with pgvector

  • HNSW (Hierarchical Navigable Small World) — graph-based ANN. Higher recall, slower build, supports incremental inserts. The 2026 default.
  • IVFFlat (Inverted File with Flat compression) — partition-based. Faster to build, lower recall, requires periodic rebuild as data grows.

Choosing parameters

For HNSW the two knobs are m (graph degree, default 16) and ef_construction (build effort, default 64). For queries, SET hnsw.ef_search = 40 trades recall for latency at runtime. For IVFFlat, lists at create time and ivfflat.probes at query time play the same role.

What 'recall' actually means here

ANN indexes are approximate. Recall is the fraction of true top-k results the index actually returns. HNSW typically delivers 0.95–0.99 with default settings; IVFFlat with low probes can drop to 0.7. Always measure recall against an exact scan on a held-out query set before trusting numbers.

Code

Create an HNSW index·sql
-- Cosine
CREATE INDEX chunks_embedding_hnsw_cos
    ON chunks USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);

-- L2
-- CREATE INDEX ... USING hnsw (embedding vector_l2_ops)    WITH (...);
-- Inner product
-- CREATE INDEX ... USING hnsw (embedding vector_ip_ops)    WITH (...);

SET hnsw.ef_search = 40;   -- raise for higher recall, slower queries
Top-k cosine query·sql
-- The <=> operator is cosine DISTANCE for vector_cosine_ops.
SELECT id, source, chunk_index, text, 1 - (embedding <=> $1) AS similarity
FROM   chunks
ORDER BY embedding <=> $1
LIMIT  10;

External links

Exercise

Load 100k rows. Create HNSW with default settings; time 100 queries. Drop the index and try IVFFlat with lists=100, probes=10; time 100 queries. Compare median latency and approximate recall (use ORDER BY without an index as ground truth on a sample).

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.