Skip to content

Vector Search HNSW vs IVFFlat Indexing Mechanics

CoreConceptAugust 1, 20263 min read

Approximate Nearest Neighbor (ANN) search is the engine behind Retrieval-Augmented Generation (RAG) and semantic search. Performing exact k-Nearest Neighbors (k-NN) queries over millions of high-dimensional vectors requires $O(N \cdot D)$ calculations per query, making brute-force search impractically slow for production applications.

To achieve sub-10ms query latencies, vector databases and extensions like pgvector build approximate vector indexes. The two leading indexing algorithms are HNSW (Hierarchical Navigable Small World) and IVFFlat (Inverted File Flat). This guide compares their indexing build times, VRAM footprints, query recall rates, and throughput benchmarks.

Four lanes of vector storage for AI builders
Four lanes of vector storage for AI builders

HNSW Graph Indexing: Multi-Layer Proximity Graphs

HNSW (Hierarchical Navigable Small World) constructs a multi-layer graph hierarchy inspired by skip lists. The top layers contain sparse long-range connections for fast coarse navigation across vector space, while lower layers contain dense short-range connections for high-precision local refinement.

During search, HNSW traverses top layers rapidly before dropping into lower layers to find nearest neighbor nodes, delivering outstanding query recall (95%+ precision) at sub-5ms query latencies.

Quick reference

  • HNSW: Graph-based indexing offering high query recall (95–99%) and low search latency.
  • HNSW Trade-off: Higher RAM footprint and slower index build times during bulk ingestion.
  • IVFFlat: Inverted file index that partitions vector space into Voronoi cells using k-means clustering.
Building an HNSW Vector Index in pgvector (PostgreSQL)
1-- Enable pgvector extension2CREATE EXTENSION IF NOT EXISTS vector;3 4-- Create table with 1536-dimensional embeddings (OpenAI text-embedding-3-small)5CREATE TABLE document_embeddings (6    id UUID PRIMARY KEY,7    content TEXT,8    embedding vector(1536)9);10 11-- Build HNSW index with custom m (max connections) and ef_construction parameters12CREATE INDEX idx_embeddings_hnsw ON document_embeddings 13USING hnsw (embedding vector_cosine_ops)14WITH (m = 16, ef_construction = 64);
Building an IVFFlat Vector Index in pgvector
1-- IVFFlat requires data to exist first to compute cluster centroids2CREATE INDEX idx_embeddings_ivfflat ON document_embeddings 3USING ivfflat (embedding vector_cosine_ops)4WITH (lists = 100);5 6-- Set search probes for IVFFlat query execution7SET ivfflat.probes = 10;

Remember this

HNSW delivers maximum query recall and low search latency, while IVFFlat uses less memory for fast build times.

IVFFlat Clustering: Voronoi Cell Partitions

IVFFlat (Inverted File Flat) divides vector space into $N$ Voronoi clusters (defined by centroids created via k-means clustering). During search, queries compare distance against cluster centroids, scanning only vectors inside the top matching probes clusters.

Because IVFFlat must compute centroids during index creation, the target table should be populated with representative data before creating the index.

Quick reference

  • IVFFlat requires tuning lists (number of clusters) and probes (number of clusters to search per query).
  • Higher probes values increase search recall at the expense of higher query latency.
  • Compare with Agentic RAG vs GraphRAG and Hybrid Search.

Remember this

IVFFlat is memory-efficient and fast to build, making it suitable for resource-constrained environments.

Vector Index Selection Matrix

Selecting between HNSW and IVFFlat depends on your priority: memory budget versus search recall accuracy.

For related guides, see our articles on Database Indexing Mechanics and RAG Evaluation.

Quick reference

  • Choose HNSW for production RAG and semantic search where high recall (>95%) and sub-10ms response times are critical.
  • Choose IVFFlat when RAM budget is limited and dataset vector insertions occur in bulk batches.
  • Use pgvector inside PostgreSQL to maintain transactional ACID consistency between application data and embeddings.

Remember this

Use HNSW for production RAG performance; use IVFFlat when conserving VRAM/RAM is paramount.

Key takeaway

Mastering HNSW graph traversal and IVFFlat Voronoi partitioning enables developers to tune vector indexes for optimal speed, recall, and RAM consumption.

Share:

Related Articles

First-generation Retrieval-Augmented Generation (RAG) systems relied exclusively on naive Vector Search (semantic simila

Read

Building enterprise AI applications requires selecting the right software framework for prompt chaining, document retrie

Read

Jul 30, 2026 · 9 min read

RAG taxonomy gets confusing because people mix three different ideas: architecture levels, retrieval tricks, and product

Read

Explore this topic

Keep learning

Follow a structured path or browse all courses to go deeper.