RAG and vector searchLoading progress…

Tuning and updates

Trace IVF and HNSW search and explain how candidate exploration trades latency for recall.

Tuning and updates

Tuning means choosing a point on the recall–latency–memory curve that meets your requirement on representative data. In the toy table the smallest efSearch with recall ≥ 0.97 within 10 ms is 64 (9 ms). Indexes also age: heavy inserts and deletes erode quality, and a new embedding model changes the space, so the index must be re-embedded, rebuilt and re-measured.

minimum recall0.97maximum latency (ms)10
efSearchrecalllatency (ms)meets both?
160.93no
320.955no
640.989yes
1280.9917no
chosen setting: ef = 64 (9 ms → 111.1 queries/s per worker)

A value that suits one corpus is not a universal recommendation: measure on representative data. Re-measure after major changes, and rebuild if the embedding model changes, because new vectors live in a different space.

Set the requirement, read off the setting.

Predict first. Should index quality be rechecked after a major embedding-model change?

Chapter context

An approximate index organizes vectors so a query can avoid most comparisons. Clustering and graph navigation use different mechanisms to choose candidates, and their build settings influence later search quality.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

Under the simplifying assumption of equally sized IVF lists, N vectors, C cells and p probes produce approximately

Ncandidates≈Np/C.N_{candidates}\approx Np/C.

Add coarse scoring, uneven list sizes and any reranking costs separately. HNSW has no equivalent universal fixed candidate-count formula.

Worked example

For 102,400 vectors evenly divided among 1024 cells, a cell has 100 vectors. Probing eight cells scans about 800 candidates, compared with 102,400 in a full scan. This excludes coarse-centroid comparisons and says nothing by itself about recall.

Reference code

The following example uses the Python standard library and does not call a remote service.

total,cells,probes=102400,1024,8
assert total/cells == 100
assert total*probes/cells == 800
assert total/(total*probes/cells) == 128

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

An average candidate count is not an actual worst-case bound. More graph edges consume memory. Approximate search settings must be evaluated against the current corpus and metric.