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
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...
