Database / pgvector basics Interview Questions
What are common performance tuning techniques for pgvector at scale?
As vector tables grow to millions of rows, several tuning techniques help maintain good query performance and manageable index build times.
| Technique | When to apply | How |
|---|---|---|
| HNSW index | > ~100k rows or when speed needed | CREATE INDEX USING hnsw with appropriate ops class |
| ef_search tuning | Recall too low after adding filter | SET hnsw.ef_search = 100 (or higher) |
| Partial indexes | Heavy filtering on specific value | CREATE INDEX ... WHERE category = 'X' |
| halfvec type | Storage cost is a concern | Use HALFVEC(n) instead of VECTOR(n) |
| Parallel index build | Slow index creation | SET max_parallel_maintenance_workers = CPU_CORES |
| maintenance_work_mem | Slow HNSW build | SET maintenance_work_mem = '4GB' |
| EXPLAIN ANALYZE | Diagnose slow queries | EXPLAIN ANALYZE SELECT ... |
| Context caching | Same filter repeated | Partial index on that filter value |
-- Full performance-tuned setup for large tables: -- 1. Create table with efficient type CREATE TABLE items ( id BIGSERIAL PRIMARY KEY, content TEXT, category TEXT, embedding HALFVEC(1536) -- 2x storage savings ); -- 2. Add composite B-tree index for filter columns: CREATE INDEX ON items (category); -- speeds up WHERE category = '...' -- 3. Configure for fast HNSW build: SET maintenance_work_mem = '4GB'; SET max_parallel_maintenance_workers = 7; -- 4. Build HNSW index AFTER bulk loading data: CREATE INDEX ON items USING hnsw (embedding halfvec_cosine_ops) WITH (m = 16, ef_construction = 64); -- 5. At query time, tune recall if needed: SET hnsw.ef_search = 100; -- increase if recall is insufficient -- 6. Check query plan: EXPLAIN (ANALYZE, BUFFERS) SELECT id, content FROM items WHERE category = 'tech' ORDER BY embedding <=> '[...]'::halfvec LIMIT 10; -- Verify: 'Index Scan using items_embedding_idx' appears
More Related questions...