Lesson 2.3 · 7 min read · about 50 min to master at a very slow pace

Vector Indexes and Nearest-neighbour Search

How a vector database finds the closest chunks quickly: exact search versus approximate nearest neighbours (HNSW), the storage a corpus needs, and how to choose between pgvector and a dedicated vector database.

Not startedJump to quiz

The problem

Given a query vector, return the chunks with the highest similarity: top-k search. The exact way compares the query with every stored vector. With 10,000 chunks that is instant. With 50 million, it is too slow for a chat interface.

Approximate nearest neighbours

ANN indexes trade a little accuracy for a lot of speed. The most common is HNSW (Hierarchical Navigable Small World): a layered graph in which each vector links to its near neighbours. A search starts at a coarse top layer, hops greedily towards the query, and refines on lower layers, visiting a tiny fraction of all vectors.

  • It may miss a true nearest neighbour now and then; the share it finds is its recall (typically 95–99%).
  • Settings such as ef_search trade speed for recall: higher is slower and more accurate.
  • The graph lives in memory, so RAM is the main cost.

How much storage?

Each dimension is usually stored as a 4-byte float:

Worked example

A client has 200,000 chunks embedded with : bytes ≈ 1.2 GB for the raw vectors, plus index overhead (often 1.5–2× more for HNSW). A modest database server handles it. At 50 million chunks it would be about 300 GB, and design choices start to matter: smaller dimensions, quantisation (storing 1 byte per dimension), or a dedicated service.

Where to store vectors

OptionChoose it when
pgvector (Postgres extension)You already run Postgres; up to a few million chunks; you want vectors, metadata, permissions and transactions in one place.
Dedicated vector databaseTens of millions of vectors, very high query rates, or built-in hybrid search and multi-tenancy you would otherwise build.
In-memory library (e.g. FAISS)Prototypes, offline experiments, batch jobs.

The practical default

For most client projects, Postgres with pgvector is enough and keeps the system simple: one database to secure, back up and explain. Move to a dedicated service when measurements say so, not before.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passIf ANN can miss results, why not always use exact search?

For small corpora, do. Exact search over tens of thousands of vectors is fast and has perfect recall. ANN matters once exact search is too slow for your latency budget. Then measure the ANN recall on real queries and raise its search setting if it misses too much.

Going deeperHow do I apply a metadata filter together with vector search?

Most stores support filtering in the same query ("top 5 similar chunks where year = 2026 and team = 'sales'"). Watch out for very selective filters with ANN: if the filter removes most vectors, the index may return fewer than results. Check the store's guidance on filtered search, or pre-filter to a smaller candidate set.

Cheatsheet

Vector storage

Raw bytes = vectors × dimensions × 4 (float32). Add 1.5–2× for an HNSW index.

HNSW in one line

A layered neighbour graph searched greedily from coarse to fine. Fast, in-memory, ~95–99% recall.

pgvector vs vector database

pgvector: already on Postgres, up to millions of chunks, one system. Dedicated DB: tens of millions, high QPS.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1calculateHow much memory do the raw vectors take for 1,000,000 chunks with 1,024 dimensions stored as 4-byte floats?
Q2conceptWhat do approximate nearest-neighbour (ANN) indexes such as HNSW trade away?
Q3applyA client runs Postgres and has 300,000 chunks with per-user permissions. Where should the vectors live?
Q4interpretYou raise HNSW's ef_search setting from 40 to 200. What should you expect?
Q5diagnoseA search with the filter "department = legal" (1% of chunks) often returns only 1 or 2 results instead of 5. What is the likely cause?