Learn how to tune the following vector indexes to achieve faster query performance and better recall in AlloyDB for PostgreSQL:
You can also analyze your queries and view vector index metrics to monitor and improve query performance.
Tune an HNSW
index
Tuning the values you set for the m
, ef_construction
, and the hnsw.ef_search
parameters can
help optimize application performance.
Tuning parameter | Description | Parameter type |
---|---|---|
m |
The maximum number of connections per from a node in the graph. You can start with the default value as 16 (default) and experiment with higher values based on the size of your dataset. |
Index creation |
ef_construction |
The size of the dynamic candidate list maintained during graph construction, which constantly updates the current best candidates for nearest neighbors for a node. Set this value to any value higher than twice of the m value—for example, 64 (default). |
Index creation |
ef_search |
The size of the dynamic candidate list used during search. You can start setting this value to either m or ef_construction , and then change it while observing the recall. The default value is 40 . |
Query runtime |
Consider the following example that shows an hnsw
index with the tuning parameters set:
SET LOCAL hnsw.ef_search = 40;
CREATE INDEX my-hnsw-index ON my-table
USING hnsw (vector_column cosine)
WITH (m = 16, ef_construction = 200);
Analyze your queries
Use the EXPLAIN ANALYZE
command to analyze your query insights as shown in the following example SQL query.
EXPLAIN ANALYZE SELECT result-column
FROM my-table
ORDER BY EMBEDDING_COLUMN <=> embedding('text-embedding-005', 'What is a database?')::vector
LIMIT 1;
The example response QUERY PLAN
includes information such as the time taken, the number of rows scanned or returned, and the resources used.
Limit (cost=0.42..15.27 rows=1 width=32) (actual time=0.106..0.132 rows=1 loops=1)
-> Index Scan using my-scann-index on my-table (cost=0.42..858027.93 rows=100000 width=32) (actual time=0.105..0.129 rows=1 loops=1)
Order By: (embedding_column <=> embedding('text-embedding-005', 'What is a database?')::vector(768))
Limit value: 1
Planning Time: 0.354 ms
Execution Time: 0.141 ms
View vector index metrics
You can use vector index metrics to review performance of your vector index, identify areas for improvement, and tune your index based on the metrics, if needed.
To view all vector index metrics, run the following SQL query, which uses the
pg_stat_ann_indexes
view:
SELECT * FROM pg_stat_ann_indexes;
You see output similar to the following:
-[ RECORD 1 ]----------+---------------------------------------------------------------------------
relid | 271236
indexrelid | 271242
schemaname | public
relname | t1
indexrelname | t1_ix1
indextype | scann
indexconfig | {num_leaves=100,quantizer=SQ8}
indexsize | 832 kB
indexscan | 0
insertcount | 250
deletecount | 0
updatecount | 0
partitioncount | 100
distribution | {"average": 3.54, "maximum": 37, "minimum": 0, "outliers": [37, 12, 11, 10, 10, 9, 9, 9, 9, 9]}
distributionpercentile |{"10": { "num_vectors": 0, "num_partitions": 0 }, "25": { "num_vectors": 0, "num_partitions": 30 }, "50": { "num_vectors": 3, "num_partitions": 30 }, "75": { "num_vectors": 5, "num_partitions": 19 }, "90": { "num_vectors": 7, "num_partitions": 11 }, "95": { "num_vectors": 9, "num_partitions": 5 }, "99": { "num_vectors": 12, "num_partitions": 4 }, "100": { "num_vectors": 37, "num_partitions": 1 }}
For more information about the complete list of metrics, see Vector index metrics.
What's next
- Maintain vector indexes.
- Learn about an example embedding workflow.