Database / pgvector basics Interview Questions
What is the HNSW index in pgvector and how do you create and tune it?
HNSW (Hierarchical Navigable Small World) is the recommended index type for most pgvector workloads. It builds a multilayer graph structure where each layer is a navigable small world graph, enabling very fast approximate nearest-neighbour search with excellent recall.
-- Create an HNSW index (choose operator class to match your distance metric) -- For L2 (Euclidean) distance <->: CREATE INDEX ON items USING hnsw (embedding vector_l2_ops); -- For cosine distance <=>: CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops); -- For inner product <#>: CREATE INDEX ON items USING hnsw (embedding vector_ip_ops); -- For L1 distance <+>: CREATE INDEX ON items USING hnsw (embedding vector_l1_ops); -- HNSW with custom build parameters: CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops) WITH ( m = 16, -- max connections per node per layer (default 16) ef_construction = 64 -- candidate list size during build (default 64) ); -- Tune QUERY performance at query time: SET hnsw.ef_search = 100; -- larger = better recall, slower query (default 40) SELECT id FROM items ORDER BY embedding <=> '[...]' LIMIT 5; RESET hnsw.ef_search; -- Speed up index BUILD with parallel workers: SET max_parallel_maintenance_workers = 7; -- match your CPU cores SET maintenance_work_mem = '2GB'; CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
| Parameter | Default | Effect of increasing |
|---|---|---|
| m | 16 | More connections = better recall, larger index, slower build |
| ef_construction | 64 | Larger candidate list during build = better recall, much slower build |
| Parameter | Default | Effect of increasing |
|---|---|---|
| hnsw.ef_search | 40 | Larger candidate set during search = better recall, slightly slower query |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
