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.
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_searchtrade 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
| Option | Choose 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 database | Tens 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
HNSW in one line
pgvector vs vector database
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.