Skip to Content

When do you pick HNSW vs IVFFlat for a vector column?

Category: Database & RAG

Answer

HNSW: graph-based, sub-linear latency, O(N log M) build memory, default-accurate. Use for most production middle-ground datasets (1M-100M). IVFFlat: cluster-based, must be trained on a sample, slightly less recall but uses less memory. Pre-2020 default. Pick HNSW unless RAM is the constraint.

Concrete examples from the fca project context

Example 1

HNSW: create index … m=16 ef_construction=64, query with ef_search=100.

Example 2

IVFFlat: lists=sqrt(rows), probes=4-16, train on 100k representative vectors.

Example 3

HNSW dominates on recall@K vs build cost until you hit billions of rows or memory pressure.

Data flow / flow chart

data size, latency target, recall@K target
  HNSW for most mid-size prod
  IVFFlat when memory pressure is the constraint
  Flat (brute force) for < 100k rows ground truth

Takeaway

Default HNSW. Reach for IVFFlat only when memory bites.