ANN Search (Approximate Nearest Neighbours)

Last updated: Jun 21, 2026 Author: Aspirant Edu Team

Comparing a query to every stored vector (exact search) is accurate but far too slow at scale. Approximate nearest neighbour (ANN) search fixes this: it uses clever indexes to find the nearest vectors almost as well, but dramatically faster. ANN is what makes vector databases practical at millions or billions of vectors — it's the engine under the hood of every production similarity search.

💡 In one line: ANN search finds the nearly-closest vectors without checking them all — trading a tiny bit of accuracy for a huge speed-up.

What is ANN Search?

Approximate nearest neighbour search finds the approximately closest vectors to a query without comparing against all of them. It relies on a prebuilt index that lets the search skip most vectors and still land on the right ones almost every time.

Why Not Exact Search?

Exact (brute-force) KNN compares the query to every vector — its cost grows with the dataset. That's fine for thousands of vectors, but impossible in real time for millions or billions. ANN is what lets vector search scale.

The Accuracy-Speed Trade-off

  • Exact → 100% recall, but slow.
  • ANN → roughly 95–99% recall, but much faster.

Recall here means the fraction of the true nearest neighbours the search actually finds. ANN gives up a sliver of recall for an enormous speed gain — and the trade-off is tunable.

How ANN Works

At query time, ANN navigates its index to a small promising region instead of scanning everything. 

Whiteboard
Whiteboard diagram


Common ANN Index Types

IndexIdeaNotes
HNSWNavigable graphMost popular — high recall & fast, but memory-heavy
IVFCluster into cellsSearch only the nearest cells (nprobe)
PQCompress vectorsSaves memory; often combined as IVF-PQ
LSHHashingOlder hashing-based approach

(For very large or disk-based scale, there's also DiskANN and ScaNN.)

Recall & Tuning

ANN exposes knobs that trade recall vs. speed vs. memory:

  • Query-time (e.g. ef_search, nprobe) — higher = better recall, slower.
  • Build-time (e.g. M, ef_construction, nlist) — higher = better quality, more memory/build time.

You tune these to hit your target recall at an acceptable latency.

ANN in Vector Databases

Every production vector database uses ANN under the hood — HNSW is the common default in Qdrant, Weaviate, Milvus, pgvector, and Pinecone, and FAISS offers many index types. Usually you just pick an index type and metric, and the database handles the rest.

Exact vs. ANN: When to Use

  • Exact — small datasets, or when you truly need 100% recall.
  • ANN — production scale (the default choice).

Code Example


Summary

  • ANN search finds the approximately nearest vectors without checking them all.
  • It trades a little recall (~95–99%) for a huge speed gain — essential at scale.
  • At query time it navigates an index to a small candidate set instead of scanning everything.
  • Common indexes: HNSW (graph), IVF (clusters), PQ (compression), LSH (hashing).
  • Tune recall vs. speed with knobs like ef_search / nprobe; ANN powers every production vector DB. EOF echo created