Sub topic 13
Measuring recall and latency and tuning index parameters
This chapter assumes you know what an approximate nearest neighbor (ANN) index is, and that you have seen HNSW, IVF and product quantization. You should be comfortable running short Python scripts with NumPy and FAISS.
What you measure
An ANN index trades accuracy for speed and memory. You need numbers for all three, measured on your own data.
- Recall@k is the fraction of the true top-k neighbors that the index returns. The true neighbors come from an exact search.
- Latency is the time per query. Report p50 and p99, not only the mean, because tail latency is what users notice.
- Memory is the index size in RAM, including graph links or codebooks.
Recall is only meaningful against ground truth. Build ground truth once with a brute-force index, then reuse it for every configuration you test.
The tuning loop
Measuring recall and latency
The script below sweeps efSearch on an HNSW index and prints recall and mean latency for each value.
import time
import numpy as np
import faiss
d, k = 64, 10
rng = np.random.default_rng(0)
xb = rng.normal(size=(10000, d)).astype("float32")
xq = rng.normal(size=(200, d)).astype("float32")
flat = faiss.IndexFlatL2(d)
flat.add(xb)
_, true_ids = flat.search(xq, k)
hnsw = faiss.IndexHNSWFlat(d, 32)
hnsw.hnsw.efConstruction = 200
hnsw.add(xb)
for ef in (8, 16, 32, 64, 128, 256):
hnsw.hnsw.efSearch = ef
start = time.perf_counter()
_, ann_ids = hnsw.search(xq, k)
ms = (time.perf_counter() - start) * 1000 / len(xq)
recall = np.mean(
[len(set(a) & set(t)) / k for a, t in zip(ann_ids, true_ids)]
)
print(f"efSearch={ef:4d} recall@{k}={recall:.3f} {ms:.3f} ms/query")Queries here are random vectors held out from the data. Using indexed vectors as queries makes recall look better than it is, because each query's nearest neighbor is itself.
Parameters and what they trade
| Index | Parameter | Raising it | Cost |
|---|---|---|---|
| HNSW | `M` (links per node) | Higher recall, more robust graph | More memory, slower build |
| HNSW | `efConstruction` | Better graph quality | Slower build |
| HNSW | `efSearch` | Higher recall | Higher latency |
| IVF | `nlist` (clusters) | Smaller lists to scan | Needs more training data, may need higher `nprobe` |
| IVF | `nprobe` | Higher recall | Higher latency |
| PQ | Bytes per vector | Higher recall | More memory |
efSearch, nprobe and similar search-time settings are the cheapest to change. They need no rebuild, so tune them first. Build-time settings such as M, efConstruction and nlist require re-indexing.
Common errors
- Testing on too small a sample. Use a few hundred to a few thousand queries, or recall estimates will be noisy.
- Evaluating only the average. Check p99 latency and the worst-case recall across queries.
- Mismatched metrics. Ground truth and the index must use the same metric. Comparing an L2 index to cosine ground truth gives misleading recall.
- Ignoring filters. Recall measured without your production metadata filter can differ a lot from recall with it.
- Tuning on synthetic data. Random vectors behave differently from real embeddings. Final tuning should use real data.
Exercise
Your HNSW index gives recall@10 of 0.82 at 1.1 ms per query. The target is 0.95, and latency may rise to 3 ms. Which parameter do you change first, and why?
Show answer
Raise efSearch. It is a search-time setting, so you can change it without rebuilding the index, and it raises recall at the cost of latency. If recall stays below target within the latency limit, then raise M or efConstruction and rebuild.
Exercise
Why is it misleading to use vectors already in the index as your evaluation queries?
Show answer
Each such query finds itself as its nearest neighbor, which is easy to retrieve. Recall then looks higher than it will for new queries. Use held-out queries instead.