This document shows you how to tune your indexes to achieve faster query performance and better recall.
Before you build a ScaNN
index, complete the following:
- Make sure that a table with your data is already created.
- Make sure that the value you set for the
maintenance_work_mem
and theshared_buffers
flag is less than total machine memory to avoid issues while generating the index.
Tune a ScaNN
index
Use the following guidance to choose between a two-level and three-level ScaNN index:
- Choose a two-level index if the number of vector rows is less than 10 million rows.
- Choose a three-level index if the number of vector rows exceeds 100 million rows.
- Choose a three-level index to optimize for index build time or choose a two-level index to optimize for search recall if the number of vector rows lies between 10 million and 100 million rows.
Consider the following examples for two-level and three-level ScaNN
indexes that show how tuning parameters are set for a table with 1000000 rows:
Two-level index
SET LOCAL scann.num_leaves_to_search = 1;
SET LOCAL scann.pre_reordering_num_neighbors=50;
CREATE INDEX my-scann-index ON my-table
USING scann (vector_column cosine)
WITH (num_leaves = [power(1000000, 1/2)]);
Three-level index
SET LOCAL scann.num_leaves_to_search = 10;
SET LOCAL scann.pre_reordering_num_neighbors=50;
CREATE INDEX my-scann-index ON my-table
USING scann (vector_column cosine)
WITH (num_leaves = [power(1000000, 2/3)], max_num_levels = 2);
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 ::vector
USING INDEX my-scann-index
<-> embedding('textembedding-gecko@003', 'What is a database?')
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('textembedding-gecko@003', '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 the 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;
For more information about the complete list of metrics, see Vector index metrics.