HNSW is the default index for most vector databases, and it has three knobs that matter: M, the number of connections per node; ef_construction, how hard the index works while building; and ef_search, how hard it works per query. Higher values cost more memory and time but find more of the true nearest neighbours. The right values come from measuring on your own data, not from copying a default.
Before you start
A vector database that exposes HNSW parameters, and a test question set with known answer chunks.
Learn the three knobs
M sets how many links each vector keeps to its neighbours: more links mean a better-connected graph and more memory. ef_construction sets the search effort during index build: higher means a better graph and a slower build. ef_search sets the effort per query: higher means better recall and slower answers.
Write down the defaults your database ships with, so you can return to them if tuning makes things worse.
Build and measure at three settings
Build the index at the default, then at one higher-ef_search setting and one lower-M setting. Run the same 20 questions at each setting and record recall at 5 and latency.
Change one knob at a time. Changing M and ef_search together hides which one helped.
Pick the balanced set
Pick the smallest ef_search that keeps recall within one question of the best setting. Extra search effort past that point buys latency, not answers.
Record the chosen values with the vector count and dimensions, because the right values shift as the corpus grows.
Practice check: fictional Maple Corner collection, 200,000 vectors, 768 dimensions
The table below is fictional practice material, worked out by hand from the stated settings. It is the expected result for the practice collection, not a run of any database.
| Setting | Recall at 5 | Latency | Verdict |
|---|---|---|---|
| M 16, ef_search 40 | 16 of 20 | 18 ms | Fast, misses 4 |
| M 16, ef_search 100 | 18 of 20 | 35 ms | Best balance |
| M 32, ef_search 200 | 18 of 20 | 71 ms | Same recall, slower |
Raising ef_search from 40 to 100 lifts recall from 16 to 18 of 20. Raising it further to 200 with M 32 adds nothing but latency, so the middle setting wins.
Expected result. UNTESTED: the recall and latency figures in the practice table are worked out by hand, not measured by running a vector database. Build your own index at three settings, run the same questions, and pick the smallest setting that holds recall.
Limits
- HNSW parameters interact with the corpus size: a setting that is fast at 200,000 vectors may be slow at 2 million.
- Index build time also matters: a high ef_construction slows every re-index, not just queries.
