Sub topic 12
Tuning m, ef_construction, ef_search, lists, and probes
This chapter assumes that you know basic PostgreSQL, SQL ORDER BY, and LIMIT. It also assumes that you have a table with an embedding vector(n) column and understand the difference between exact and approximate nearest-neighbor search.
Approximate indexes reduce query time by examining only part of the possible search space. Their settings control the balance between speed, memory, index size, build time, and recall. Recall is the percentage of the true nearest neighbors that the index returns.
HNSW parameters
HNSW builds a graph whose vectors are connected to nearby vectors. During a search, PostgreSQL follows graph links instead of scanning every row.
The m parameter controls the maximum number of connections made for each vector. A larger value usually improves recall because the graph has more paths between vectors. It also increases index size, memory use, and build time.
The ef_construction parameter controls how broadly PostgreSQL searches while building the graph. A larger value usually produces a better graph, but index creation takes longer. It does not directly control the number of results examined during each query.
CREATE INDEX items_embedding_hnsw
ON items USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
SET hnsw.ef_search = 100;
SELECT id, content
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;The hnsw.ef_search setting controls the search breadth at query time. Increasing it generally improves recall and increases latency. It can also increase memory use for an individual query.
You can change this setting for one transaction:
BEGIN;
SET LOCAL hnsw.ef_search = 200;
SELECT id, content
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;
COMMIT;A practical tuning process is to build the index with a reasonable m and ef_construction, then adjust ef_search when queries need higher recall.
IVFFlat parameters
IVFFlat divides vectors into lists, or clusters. A query examines only selected lists.
The lists parameter controls the number of clusters created in the index. More lists can reduce the work per query, but each list contains fewer vectors and the index needs enough data to form useful clusters. IVFFlat indexes should generally be created after the table contains representative data.
CREATE INDEX items_embedding_ivfflat
ON items USING ivfflat (embedding vector_l2_ops)
WITH (lists = 100);
SET ivfflat.probes = 10;
SELECT id, content
FROM items
ORDER BY embedding <-> '[3,1,2]'
LIMIT 5;The ivfflat.probes setting controls how many lists a query examines. A value of 1 is fast but can miss many true neighbors. Increasing it improves recall and increases query time. Setting probes close to the total number of lists approaches an exact scan.
| Index | Build-time setting | Query-time setting | Main trade-off |
|---|---|---|---|
| HNSW | `m`, `ef_construction` | `hnsw.ef_search` | Recall versus memory and latency |
| IVFFlat | `lists` | `ivfflat.probes` | Recall versus scanned clusters and latency |
A tuning workflow
- Keep an exact-search query as a reference.
- Choose a target recall and maximum acceptable latency.
- Adjust the query-time setting first:
ef_searchfor HNSW orprobesfor IVFFlat. - Rebuild the index only when build-time settings such as
m,ef_construction, orlistsneed to change. - Use
EXPLAINand production-like data to confirm the expected index is used.
Exercise
Which setting should you increase first when an HNSW query is fast but misses too many neighbors?
Show answer
Increase hnsw.ef_search. It broadens the query-time graph search without requiring the index to be rebuilt.