LLMs & GenAI

HNSW vs IVF vs Flat Indexes

Comparing graph based and inverted file indexing algorithms for Approximate Nearest Neighbor vector search.

🔴 advanced5 min readretrieval
Vector Search Indexing algorithms trade memory and build time for sub millisecond query retrieval speed. Flat Indexing performs exact 100 percent brute force search, scaling linearly O(N * d). Inverted File Indexing (IVF) clusters vector space into Voronoi cells to search candidate centroids. Hierarchical Navigable Small World (HNSW) builds multi layer proximity graphs, delivering state of the art sub-millisecond search speed and 99 percent recall.

Vector Indexing Algorithms

A Vector Database cannot perform brute-force search across millions of vectors for real-time production APIs.

It builds a Vector Index data structure to navigate high-dimensional space efficiently.

Three primary vector index families dominate modern AI systems:

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. FLAT INDEX (FLAT)     │ 2. INVERTED FILE (IVF)   │ 3. GRAPH-BASED (HNSW)    │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Brute-force exact search.│ Clusters space into      │ Multi-layer proximity    │
│ 100% Exact Recall.       │ Voronoi cells.           │ small-world graph.       │
│ Slow $O(N \cdot d)$      │ Fast, low RAM.           │ Industry Standard SOTA!  │
│ Best for $<10k$ vectors. │ Needs training step.     │ Highest speed & recall!  │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

1. Flat Index (Exact Brute-Force)

Measures exact distance from query vector $q$ to every single vector in the database.

2. Inverted File Index (IVF)

Divides vector space into $K$ Voronoi Cells using k-Means clustering:

  HIGH DIMENSIONAL SPACE SPLIT INTO VORONOI CELLS
  ┌──────────┬──────────┬──────────┐
  │ Cell 1   │ Cell 2   │ Cell 3   │
  │   (•)    │   (•)    │   (•)    │ ◄── Centroids!
  ├──────────┼──────────┼──────────┤
  │ Cell 4   │ Cell 5   │ Cell 6   │
  └──────────┴──────────┴──────────┘

Search Process

  1. Find nearest Voronoi centroid to query vector $q$.
  2. Search vectors ONLY inside the nprobe closest Voronoi cells, ignoring the rest of the database!

3. Hierarchical Navigable Small World (HNSW - Malkov & Yashunin, 2018)

HNSW is the state-of-the-art index for vector search (used in Qdrant, Pinecone, FAISS).

It builds a Multi-Layer Proximity Graph inspired by Skip Lists:

  Layer 2 (Top Layer - Long Jumps!):     [ Node A ] ───────────────────────► [ Node Z ]
                                             │                                  │
  Layer 1 (Middle Layer):                [ Node A ] ────────► [ Node M ] ──────► [ Node Z ]
                                             │                   │              │
  Layer 0 (Bottom Layer - Dense Graph):  [ Node A ] ──► [ B ] ──►[ M ] ──►[ P ]─►[ Z ]

HNSW Query Traversal

  1. Start at top layer (Layer 2) with sparse long-distance links $\to$ Perform fast greedy routing jumps toward target neighborhood.
  2. Drop down to middle layers $\to$ Refine routing trajectory.
  3. Drop down to Layer 0 (dense local graph) $\to$ Perform fine-grained local neighbor search.

Search complexity scales to $\mathcal{O}(\log N)$ logarithmic time, delivering sub-millisecond search speeds with $99%$ recall!

Detailed Index Comparison Matrix

Index TypeSearch SpeedRecall AccuracyRAM FootprintBuild Time
FlatSlow $\mathcal{O}(N)$100% ExactLowestInstant (0s)
IVFFastHigh (Depends on nprobe)LowModerate (k-Means)
HNSWUltra-Fast $\mathcal{O}(\log N)$98% - 99.5%Higher (Stores Graph)Slower

Say this out loud

Flat indexing performs 100 percent exact brute force search for small datasets under 10k vectors. IVF clusters vector space into Voronoi cells, searching only nprobe candidate clusters. HNSW builds multi layer proximity small world graphs, routing fast long distance jumps on top layers and fine local searches on bottom layers to deliver sub millisecond logarithmic search speed.

Followups to expect

  1. What is Product Quantization (IVF-PQ)? Combining IVF cell routing with Product Quantization, compressing 1024-d vectors into 64-byte codebook bytes to store billions of vectors in system RAM.
  2. What is the HNSW ef_construction and ef_search hyperparameter? ef_construction controls graph quality during build time. ef_search controls dynamic candidate queue size during search time (higher ef_search increases recall at the cost of higher latency).

Check yourself

Question 1 of 3

What core graph structure does HNSW (Hierarchical Navigable Small World) build to execute fast vector search?

More in LLMs & GenAI

See all →
Pretraining → SFT → RLHF5 minFine-Tune vs RAG vs Prompt: Choosing5 minRetrieval-Augmented Generation5 min