Topic 33 / 40

pgvector Indexing & Vector Search: HNSW vs IVFFlat

~17 min read  //  Django Series  //  Coding India

1. Deep Architecture

IVFFlat (Inverted File Flat) groups vectors into clusters, searching only the closest clusters. HNSW (Hierarchical Navigable Small World) builds a multi-layered graph of vectors. HNSW uses more memory and takes longer to build, but provides faster queries and better search recall.

2. The Feynman Gatekeeper

[KNOWLEDGE CHECK] Contrast the index creation times and query latency profiles of HNSW and IVFFlat indexes in pgvector.

3. The Code

# Migration run SQL to add HNSW index on pgvector column
# CREATE INDEX embedding_hnsw_idx ON sanjaya_embedding USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);

4. The Funnel

Stat Level-Up: Index Architect (Lvl 1).
Sanjaya Integration: Speed up viral hook matching lookups across thousands of template files.