Senior LevelPostgreSQL
Compare IVFFlat vs HNSW vector index build times, recall rates, and RAM usage.
❓ Interview Prompt: Compare `IVFFlat` (Inverted File Flat - requires training lists, fast build, lower RAM, lower recall) vs `HNSW` (Hierarchical Navigable Small World - slow build, high RAM, 99%+ recall).
pgvector supports two vector index algorithms:
1. IVFFlat (Inverted File Flat):
•
Divides vector space into lists ('lists = 100'). Requires training data!
•
Fast Build, Low RAM usage, but lower recall accuracy under data updates.
2. HNSW (Hierarchical Navigable Small World):
•
Builds a multi-layer graph network.
•
Slower Build, High RAM usage, but delivers 99%+ recall accuracy and fast query performance!
Code Implementation Example
sql
-- IVFFlat Index (Requires lists parameter and training data)
CREATE INDEX idx_ivfflat ON document_chunks USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
-- HNSW Index (Recommended for production accuracy!)
CREATE INDEX idx_hnsw ON document_chunks USING hnsw (embedding vector_cosine_ops);💡Comparing IVFFlat and HNSW vector index creation queries.
🎯 Interview Key Takeaway
Prefer HNSW indexes for production pgvector deployments to guarantee high search recall accuracy.
#IVFFlat#HNSW#pgvector#Vector Search#Senior
Back to all PostgreSQL Questions→