Vector indexing is the algorithmic foundation of modern vector databases, semantic search engines, and Retrieval-Augmented Generation (RAG) systems. While exact flat search (\text{Flat L2} or Cosine) calculates pairwise distances across all N vectors with O(N \cdot d) computational complexity, approximate nearest neighbor (ANN) indexes trade a marginal fraction of recall (typically 95\% - 99\% Recall@K) for orders-of-magnitude faster queries (O(\log N) or O(1)) and reduced RAM footprints.
This article details the mathematical foundations, data structures, and hardware trade-offs of the three primary vector indexing paradigms: Hierarchical Navigable Small World (HNSW), Inverted File (IVF), and Product Quantization (PQ).
+-----------------------------------------------------------------------------------------+
| ANN VECTOR INDEX TRADEOFF MATRIX |
+-----------------------------------------------------------------------------------------+
| Index Type | Query Latency (QPS) | Memory Footprint (RAM) | Index Build Time | Recall@10 |
+--------------------+---------------------+------------------------+------------------+-----------+
| Flat (Exhaustive) | Very Slow (< 50 QPS)| High (100% Float32) | Instant (0 ms) | 100% |
| IVF-Flat | Fast (~ 1,500 QPS) | High (100% Float32) | Fast (K-Means) | 90 - 95% |
| HNSW | Ultra-Fast (>5,000) | Very High (+30% Graph) | Slow (Multi-pass)| 98 - 99.5%|
| IVF-PQ | Fast (~ 3,000 QPS) | Ultra-Low (4x-16x comp)| Moderate (Train) | 85 - 93% |
| HNSW-PQ | Ultra-Fast (>4,000) | Low (Quantized nodes) | Slow (Training) | 92 - 96% |
+-----------------------------------------------------------------------------------------+
HNSW is currently the state-of-the-art graph-based ANN algorithm. It structures vector space into a multi-layer graph hierarchy analogous to a Skip List:
HNSW Multi-Layer Search Traversal:
Layer 2 (Sparse Graph): (Node A) -----------------------------------> (Node D)
| |
v v
Layer 1 (Medium Density): (Node A) ------------> (Node B) ------------> (Node D)
| | |
v v v
Layer 0 (Dense Base): (Node A) -> (Node E) -> (Node B) -> (Node F) -> (Node D)
IVF partitions high-dimensional vector space into C Voronoi cells using K-Means clustering:
IVF Voronoi Partitioning:
[ High-Dimensional Space ]
|
v (K-Means Clustering: C Centroids)
+-------------+-------------+
| Cell C1 | Cell C2 |
| * Centroid | * Centroid|
| [vec1,vec2]| [vec3,vec4|
+-------------+-------------+
|
Query Search: Identify closest `nprobe` centroids -> Scan only vectors in those cells!
nprobe Latency vs. Recall DialHigh-dimensional float32 vectors consume massive RAM. A 1,536-dimensional vector (OpenAI text-embedding-3-small) requires 1,536 \times 4\,\text{bytes} = 6,144\,\text{bytes} per vector. One million vectors require \approx 6.14\,\text{GB} of uncompressed RAM.
Product Quantization (PQ) compresses vectors by splitting high-dimensional spaces into lower-dimensional orthogonal subspaces:
Product Quantization 4x Compression Pipeline:
Original 128-dim Float32 Vector (512 Bytes):
[ v_1, ..., v_32 | v_33, ..., v_64 | v_65, ..., v_96 | v_97, ..., v_128 ]
| | | |
v (Subspace 1) v (Subspace 2) v (Subspace 3) v (Subspace 4)
[ Codebook 1 ] [ Codebook 2 ] [ Codebook 3 ] [ Codebook 4 ]
(256 Centroids) (256 Centroids) (256 Centroids) (256 Centroids)
| | | |
v (Byte Index) v (Byte Index) v (Byte Index) v (Byte Index)
Compressed 4-Byte Quantized Code: [ 0x4A, 0x1F, 0x9B, 0x03 ] (4 Bytes total! 128x compression)
In Asymmetric Distance Computation, the query vector q remains uncompressed float32, while stored database vectors are quantized bytes. The engine precomputes a lookup table of distances from q's sub-vectors to all 256 centroids per codebook:
Evaluating distance requires only M table lookups and additions per vector, completely eliminating expensive floating-point multiplications from inner loops.
Production vector engines (Qdrant, Milvus, pgvector, Lucene) combine techniques: