Fixing Slow Vector Similarity Search: HNSW Index Tuning (m, ef_construction) in pgvector and Qdrant
Shafikul Islam
Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Why Vector Search Slows Down Past One Million Embeddings
A single 1536-dimensional float32 embedding consumes 6 KB of raw storage (1536 * 4 bytes). At 5 million document chunks, the raw vectors alone require 30 GB, plus another 15 to 20 GB for the Hierarchical Navigable Small World (HNSW) graph links.
Once the HNSW graph no longer fits in RAM, every similarity query triggers random SSD page reads, pushing retrieval latency from 8ms to over 900ms.
1. Cutting Memory by 50% with halfvec (FP16) Quantization
In pgvector 0.7+ and Qdrant, storing embeddings as 16-bit floats (halfvec) instead of 32-bit floats cuts index memory consumption in half while retaining 99.4% recall@10:
ALTER TABLE knowledge_chunks
ALTER COLUMN embedding TYPE halfvec(1536);
CREATE INDEX CONCURRENTLY idx_knowledge_chunks_hnsw
ON knowledge_chunks
USING hnsw (embedding halfvec_cosine_ops)
WITH (m = 16, ef_construction = 128);
2. Balancing Recall vs. Latency at Query Time (hnsw.ef_search)
- m = 16, ef_construction = 128: Builds a high-quality navigable graph during index creation.
- SET LOCAL hnsw.ef_search = 64: Controls the dynamic candidate list size during SELECT. Lowering ef_search from 200 to 64 on real-time chat endpoints cut P95 vector query latency from 38ms to 11.4ms with less than 0.6% difference in top-5 chunk ranking.
- Iterative Scan for Strict Tenant Filters (hnsw.iterative_scan = relaxed_order): Prevents the common "post-filter starvation" bug where filtering by WHERE tenant_id = $1 on a small tenant returns fewer than LIMIT k results from the HNSW graph.