CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
The Algorithm Inside Every Vector Database: Understand and Tune HNSW in 20 Minutes

The Algorithm Inside Every Vector Database: Understand and Tune HNSW in 20 Minutes

Chris Harper

3 min read

Jul 26, 2026 · 12:06 UTC

AI
Tutorial
Embeddings
Vectors
Best Practices

HNSW is the graph index inside pgvector, Qdrant, Weaviate, ChromaDB, and FAISS — three knobs control all of them: m (connectivity), ef_construction (build quality), and ef (query depth).

What you'll be able to do after this:

  • Explain why HNSW finds approximate nearest neighbors in sub-millisecond time across millions of vectors — and why brute-force can't
  • Tune m, ef_construction, and ef for the right recall/latency tradeoff in any vector database
  • Benchmark your own collection and find the performance knee that fits your workload

Resource: Qdrant Essentials Course, Day 2: HNSW Indexing + YouTube walkthrough

How HNSW works

HNSW builds a multi-layer graph. Higher layers contain fewer nodes connected by long-range "highway" edges — they let a search jump quickly across the vector space. Lower layers are dense with local connections for fine-grained lookup.

At query time, search enters at the top (coarsest) layer, follows the closest edges downward through increasingly dense layers, and returns approximate nearest neighbors. This is typically 10–100× faster than brute-force scan, with 95–99% recall — which is why every major vector database uses it as the default index.

All the databases covered in this learning track use HNSW: pgvector, Qdrant, Weaviate, ChromaDB, and FAISS (IndexHNSWFlat).

The three parameters you control

ParameterControlsWhen set
mEdges per node per layer — higher = better recall, more memory and build timeIndex creation
ef_constructionCandidates explored during build — higher = better index quality, slower buildIndex creation
ef (or ef_search)Candidates explored per query — higher = better recall, slower queriesQuery time (tunable live)

Starting values: m=16, ef_construction=200, ef=128. Raise m when recall matters more than memory. Raise ef at query time for a recall boost without rebuilding the index.

Hands-on walk-through (Qdrant course)

The Qdrant Essentials Day 2 course runs you through a 100K-vector benchmark:

  1. Upload vectors with HNSW disabled — measure brute-force baseline latency
  2. Enable HNSW with default params — observe the latency drop
  3. Tune m and ef_construction, rebuild — measure recall against a ground-truth set
  4. Use the HNSW Performance Benchmarking project to find the recall/latency knee for your collection

The YouTube video is the clearest visual walkthrough of the layer traversal before you run the code.

The same parameters exist in every major vector DB — they just use different names:

Databasemef_constructionef at query time
Qdrantmef_constructef
pgvectormef_constructionhnsw.ef_search
WeaviatemaxConnectionsefConstructionef
ChromaDBMef_constructionef

Once you understand the three knobs, you're fluent across all of them.

Sources: HNSW Indexing Fundamentals — Qdrant · HNSW Performance Tuning Demo · YouTube: Fast Vector Search with HNSW Indexing