Slides · Embeddings and vector search
Vector databases
1 / 32
Vector databases
How a store finds the nearest vectors fast, what “approximate” gives up, and how to choose and measure one.
By the end you can explain the ANN trade-off, compare HNSW and IVF, tune and measure recall, choose a store, and design for filters and updates.
Concept
40,000 articles, one second
- A help centre needs the three closest articles to a question in under a second.
- Option one: compare the question with every article.
- Option two: group the articles and look only in the nearest groups.
- Option three: keep a shortcut list that is usually right.
- Interviewers probe the word “approximate”: what is traded away, which knob buys it back, how do you check.
Concept
Mental model: a floor plan for a library
- Exact search walks every shelf with the query in hand.
- An ANN index is a floor plan: walk to the right area, check the shelves around it.
- You may miss the single best book, but you finish in a fraction of the time.
- You pay for the map with memory and build time.
Worked example
Why exact search stops scaling
Illustrative corpus: 10 million vectors of 1,536 dimensions.
| Step | Calculation |
|---|---|
| Multiply-adds for one query | 10,000,000×1,536=15.36 billion |
| At 1,000 queries per second | 15.36 billion×1,000=15.36 trillion per second |
- One query is feasible in an offline job. It is too heavy for a request with a latency budget of tens of milliseconds.
- Work grows with vectors times dimensions, so doubling the corpus doubles the cost of every query.
- Exact search stays exact. Only its cost grows.
Diagram
Brute force versus ANN
Concept
Brute force still has two jobs
- Small corpora: a few thousand to tens of thousands of vectors is often fast enough with no index. Measure on your own hardware.
- Ground truth: flat indexes are the only ones the Faiss guidelines say guarantee exact results, so they define the “true” neighbours.
- Every recall number later in this deck is measured against this exact answer.
Worked example
Recall@k: what “approximate” costs
Recall@k is the share of the true top-k neighbours that the ANN search actually returned.
| Step | Value |
|---|---|
| Exact top 10 (ground truth) | 10 vectors |
| ANN top 10 contains how many of those | 9 |
| Recall@10 | 9/10=0.9 |
- It measures how close ANN is to exact search. It does not measure whether the results are useful to the user.
- Every ANN index has settings that trade recall against latency and memory. You choose a point on that curve.
Worked example
Diminishing returns on one knob
These numbers are illustrative, not measurements of any real index.
| ef_search | Recall@10 | Time per query | Gain from previous row |
|---|---|---|---|
| 10 | 0.82 | 1 ms | |
| 40 | 0.95 | 3 ms | +0.13 recall for +2 ms |
| 100 | 0.98 | 7 ms | +0.03 recall for +4 ms |
| 400 | 0.995 | 26 ms | +0.015 recall for +19 ms |
- Each step buys less recall for more time.
- Once your recall target is met, extra effort is wasted latency.
Diagram
HNSW: a layered graph
- Malkov and Yashunin describe a multi-layer set of proximity graphs where upper layers are searched first.
- Analogy: motorway, then main road, then street map.
Concept
HNSW knobs
| Knob | What it controls | When it is set |
|---|---|---|
| M | Links kept per node | Build time; changing it means rebuilding |
| ef_construction | Search width while building | Build time; slower builds when raised |
| ef_search | Candidates tracked per query | Query time; can change per query |
- Higher values raise recall at the cost of memory, build time or latency.
- pgvector defaults:
m = 16,ef_construction = 64,hnsw.ef_search = 40. - HNSW has no training step in pgvector: the index can be created on an empty table.
Source[3] pgvector: open-source vector similarity search for Postgres
Worked example
What the HNSW graph costs
The Faiss guidelines give HNSW memory as (d×4+M×2×4) bytes per vector, where d is the dimension and M the links per vector.
| Step | Calculation |
|---|---|
| Vector itself (d=1,536, float32) | 1,536×4=6,144 bytes |
| Graph links (M=16) | 16×2×4=128 bytes |
| Per vector | 6,272 bytes |
| 1 million vectors | 6.272 GB |
- At M=16 the links add about 2 percent over the raw vectors (128/6,144), and the vectors themselves dominate.
- Raising M raises the graph share. This is the Faiss formula; other products store the graph differently.
Diagram
IVF: clusters and probes
- Analogy: postal districts. Search the districts nearest the address, not the whole country.
- IVF learns its cluster centres from a sample, so the table should hold representative data before you build the index.
Sources[4] Faster search (Faiss wiki)[3] pgvector: open-source vector similarity search for Postgres
Concept
IVF knobs
| Knob | What it controls | Notes |
|---|---|---|
| nlist (pgvector: lists) | Number of clusters | Fixed at build; changing it means rebuilding |
| nprobe (pgvector: probes) | Clusters scanned per query | Search time grows roughly linearly with it |
- Probing all clusters (nprobe=nlist) matches brute force, but slower.
- Defaults: Faiss and pgvector both start at nprobe 1.
- pgvector’s starting points: lists=rows/1,000 up to 1 million rows, probes near lists. Treat them as places to start measuring.
Sources[4] Faster search (Faiss wiki)[3] pgvector: open-source vector similarity search for Postgres
Worked example
IVF cost, by hand
Illustrative: 1,000,000 vectors, nlist=1,000 clusters, assumed equal-sized (1,000 vectors each). Distance computations per query are centres plus scanned vectors.
| nprobe | Calculation | Distances | Share of brute force |
|---|---|---|---|
| 1 | 1,000+1×1,000 | 2,000 | 0.2% |
| 10 | 1,000+10×1,000 | 11,000 | 1.1% |
| 32 | 1,000+32×1,000 | 33,000 | 3.3% |
- 1,000 is about 32, the pgvector starting point for probes.
- Real clusters are uneven, so measure rather than trust this arithmetic.
- Raising nlist to 4,000 at nprobe 10 gives 4,000+10×250=6,500 distances: faster, but each query now looks at only 0.25% of the data (10 of 4,000 clusters), so recall usually falls.
Sources[4] Faster search (Faiss wiki)[3] pgvector: open-source vector similarity search for Postgres
Compare
HNSW versus IVF
| Aspect | HNSW | IVF |
|---|---|---|
| How it searches | Hops through a layered graph | Scans the nearest few clusters |
| Memory | Higher | Lower |
| Build time | Slower | Faster |
| Needs training | No: index can start empty | Yes: centres learned from existing data |
| Speed-recall trade-off | Better per the pgvector README | Slower queries at the same recall |
| Main recall knob | ef_search | nprobe |
- Faiss guidance: HNSW is a very fast and accurate choice when RAM is plentiful or the dataset is small.
- IVF is the lighter choice when memory or build time is tight.
Sources[3] pgvector: open-source vector similarity search for Postgres[1] Guidelines to choose an index (Faiss wiki)
Worked example
Quantization shrinks the vectors
1 million vectors of 1,536 dimensions:
| Storage | Per vector | Total |
|---|---|---|
| 32-bit floats | 1,536×4=6,144 bytes | about 6.1 GB |
| 8-bit integers (scalar quantization) | 1,536×1=1,536 bytes | about 1.5 GB |
| Product quantization, 64-byte codes (illustrative) | 64 bytes | 64 MB |
- Product quantization stores M-byte codes per vector (Faiss guidelines). Compression usually costs some recall.
- Common pattern: search the compressed vectors, then re-rank a short list with the originals.
Diagram
Compress, then re-rank
Process
How to measure recall
- Sample a few hundred real queries, from logs or a realistic test set.
- Get the exact top-k for each with brute force. This is the ground truth.
- Get the ANN top-k with your index and settings.
- Compute recall@k as the overlap, and record latency.
- Change one knob at a time (
ef_search,nprobe) and re-measure. - Pick the cheapest setting that meets your recall target.
- pgvector’s README says to monitor recall by comparing approximate with exact results, and shows
SET LOCAL enable_indexscan = off;to force exact search. - Check that retrieval recall is what really limits your product. Embeddings or chunking sometimes matter more.
Source[3] pgvector: open-source vector similarity search for Postgres
Worked example
A tiny recall measurement
Illustrative: 5 queries, k=10. For each, count how many of the exact top 10 the ANN search returned.
| Query | Overlap with exact top 10 | Recall@10 |
|---|---|---|
| q1 | 10 | 1.0 |
| q2 | 9 | 0.9 |
| q3 | 10 | 1.0 |
| q4 | 8 | 0.8 |
| q5 | 9 | 0.9 |
| Mean | 46/50 | 0.92 |
- Look at the worst queries, not only the mean: q4 at 0.8 is where users would notice.
- Real measurements need hundreds of queries. Five is only to show the arithmetic.
Compare
Three kinds of store
| Option | What it is | Good when | Watch out for |
|---|---|---|---|
| Postgres extension (pgvector) | Vector columns and ANN indexes inside the database you already run | Modest scale, and you want transactions and joins in one system | One node’s memory and build time, tuning, dimension limits |
| Library (Faiss) | An index you embed in your own service, not a database | Offline jobs, research, custom serving | You build persistence, updates, filtering and replication |
| Dedicated vector database (for example Qdrant, Weaviate, Pinecone) | A service built around vector indexes, filtering and scaling | Large corpora, high query volume, built-in sharding | Another system to run, pay for and keep in sync |
- Faiss describes itself as a library for similarity search and clustering of dense vectors.
- Qdrant describes itself as a vector similarity search engine and vector database.
Sources[3] pgvector: open-source vector similarity search for Postgres[5] Faiss: a library for efficient similarity search and clustering of dense vectors[6] Qdrant: vector similarity search engine and vector database
Concept
“Dedicated” does not mean “managed only”
- Qdrant is Apache 2.0 licensed, can run locally with Docker, and also has a managed Qdrant Cloud.
- Weaviate calls itself open source and lists Docker, Kubernetes and embedded options plus Weaviate Cloud.
- Pinecone’s overview describes a search engine for AI agents and applications, and a Bring Your Own Cloud option that deploys in your own cloud account.
- All checked for this deck on 2026-10-22. Products and plans change, so confirm current options before you decide.
Sources[6] Qdrant: vector similarity search engine and vector database[7] Weaviate Database | Weaviate Documentation[8] Pinecone Database
Worked example
A limit that can decide the choice
pgvector can index its vector type up to 2,000 dimensions and halfvec up to 4,000.
| Embedding dimensions | Index options |
|---|---|
| 1,536 | vector (limit 2,000) or halfvec (limit 4,000) |
| 3,072 | halfvec only: over the 2,000 limit, under 4,000 |
- A 3,072-dimension embedding needs half-precision storage or a shorter vector.
- Read the current docs for limits like this before you commit.
Source[3] pgvector: open-source vector similarity search for Postgres
Diagram
Start from what you run
- This is judgment, not a measured law: move when a measured limit forces you.
Diagram
Metadata filters can starve the result list
- pgvector’s README uses exactly this example: a condition matching 10% of rows with
hnsw.ef_searchat 40 gives about 4 matching rows on average. - Remedies: iterative index scans (pgvector 0.8.0 and later), a larger candidate list, partitioning by the filter field, or an engine that filters during the graph search.
- Permission filters are a security boundary. A missed filter is a data leak, not a ranking quirk. Test filtered queries for recall too.
Source[3] pgvector: open-source vector similarity search for Postgres
Worked example
Keeping the index fresh
Use deterministic chunk IDs, such as document ID plus chunk number.
| Event | Chunk IDs before | After re-embedding |
|---|---|---|
Document refund-policy has 5 chunks | refund-policy#0 to #4 | |
| Document edited, now 4 chunks | #0 to #3 are overwritten | |
| Chunk that no longer exists | refund-policy#4 | Delete it |
- Random IDs would leave the old chunks in the index, still matching queries.
- Some indexes need periodic rebuilds or compaction after many deletes.
Check yourself
Check: probing more clusters
IVF over 1,000,000 vectors with nlist=2,000 (assume 500 vectors per cluster). You compare nprobe=20 with nprobe=40. By how much does the scan work change, and what is the trade?
Answer
Vectors scanned go from 20×500=10,000 to 40×500=20,000, so the scan work doubles. Adding the 2,000 centre comparisons, total distances go from 12,000 to 22,000, so latency rises by less than double. You pay in latency for the higher recall you hope to gain. Re-measure recall against brute force to confirm the gain is worth it.
Check yourself
Check: does the graph fit?
A server can give the index 5 GB. You plan an HNSW index over 2,000,000 vectors of 768 dimensions (float32) with M=16, using the Faiss memory formula. Does it fit? Name two ways to get under budget, and say whether switching to IVF alone would do it.
Answer
Per vector: 768×4+16×2×4=3,072+128=3,200 bytes. For 2,000,000 vectors that is 6.4 GB, which is over the 5 GB budget, so it does not fit. The raw vectors alone are 2,000,000×3,072 bytes =6.144 GB, so the graph (0.256 GB) is not the problem. Two ways under budget: shorten the vectors (at 384 dimensions the formula gives 1,664 bytes, 3.328 GB), or store compressed codes (quantization, for example IVF with product quantization). Plain IVF with full vectors stores the same 6.144 GB, so switching to it alone does not fit. Re-measure recall after any change.
Check yourself
Check: the starved filter
Your HNSW query filters after the index search, and the filter matches 5% of rows. You need 10 results. Roughly what ef_search gives 10 matching rows on average, and why is that not a full fix?
Answer
Matching rows =ef_search×0.05, so 10=ef_search×0.05 gives ef_search=200. It is only an average, so some queries still return fewer than 10. A larger candidate list also raises latency for every query, and a rarer filter (1%) would need 1,000. That is why iterative scans or partitioning by the filter field are worth testing. Re-test filtered queries for recall either way.
Check yourself
Check: 3,072 dimensions in Postgres
A team on Postgres wants to index 800,000 embeddings of 3,072 dimensions with pgvector. What blocks a plain vector index, and what are two ways round it?
Answer
The vector type can be indexed up to 2,000 dimensions, and 3,072 is over that. Either store the vectors as halfvec, which can be indexed up to 4,000 dimensions, or use a shorter embedding if the model supports a dimensions setting. Test the retrieval quality after either change.
Summary
Recap
- Exact search costs vectors x dimensions per query; ANN trades a little recall for much lower latency.
- HNSW: layered graph, no training, more memory. Knobs: M, ef_construction, ef_search.
- IVF: clusters probed per query, less memory, needs training. Knobs: nlist, nprobe.
- Measure recall@k against brute force on real queries, one knob at a time.
- Start with what you run, often pgvector, and move when a measured limit forces it.
- Design filters and updates explicitly: post-filtering can return too few results, and deterministic chunk IDs prevent stale copies.
References
References & Sources (1 of 2)
Guidelines to choose an index (Faiss wiki) (opens in a new tab)
Meta Research (Faiss project)2026Documentation
https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index
Used for Flat indexes are the only ones that guarantee exact results, HNSW memory is (d * 4 + M * 2 * 4) bytes per vector, and a product quantizer stores M-byte codes.
Yu. A. Malkov, D. A. Yashunin2020IEEE Transactions on Pattern Analysis and Machine IntelligencePaper
DOI 10.1109/TPAMI.2018.2889473 (opens in a new tab)arXiv 1603.09320 (opens in a new tab)https://arxiv.org/abs/1603.09320
Used for HNSW is a graph-based approximate nearest-neighbour method that builds a multi-layer set of proximity graphs and searches from the upper layers down.
pgvector: open-source vector similarity search for Postgres (opens in a new tab)
pgvector2026Repository
https://github.com/pgvector/pgvector
Used for pgvector defaults (m 16, ef_construction 64, hnsw.ef_search 40, ivfflat probes 1), lists and probes starting points, IVFFlat versus HNSW trade-off, 10%-filter example giving 4 rows, iterative scans from 0.8.0, the 2,000 and 4,000 dimension limits, and checking recall against exact search.
Faster search (Faiss wiki) (opens in a new tab)
Meta Research (Faiss project)2026Documentation
https://github.com/facebookresearch/faiss/wiki/Faster-search
Used for nlist is the number of cells, nprobe the number visited, nprobe defaults to 1, nprobe equal to nlist matches brute force, and search time grows roughly linearly with nprobe.
Meta Research (Faiss project)2026Repository
https://github.com/facebookresearch/faiss
Used for Faiss describes itself as a library for efficient similarity search and clustering of dense vectors.
References
References & Sources (2 of 2)
Qdrant: vector similarity search engine and vector database (opens in a new tab)
Qdrant2026Repository
https://github.com/qdrant/qdrant
Used for Qdrant is a vector database licensed under Apache 2.0, runs locally with Docker, and has a managed Qdrant Cloud.
Weaviate Database | Weaviate Documentation (opens in a new tab)
Weaviate2026Documentation
https://docs.weaviate.io/weaviate
Used for Weaviate calls itself an open-source vector database with Docker, Kubernetes, embedded and Weaviate Cloud options.
Pinecone Database (opens in a new tab)
Pinecone2026Documentation
https://docs.pinecone.io/guides/get-started/overview
Used for Pinecone describes itself as a search engine for AI agents and applications, with a Bring Your Own Cloud deployment option.
Keys: ← → move, Home End first and last, F full screen.