了解如何在 AlloyDB for PostgreSQL 中对以下向量索引进行调优,以实现更快的查询性能和更高的召回率:
您还可以分析查询并查看向量索引指标,以监控和提高查询性能。
对 HNSW
索引进行调优
对您为 m
、ef_construction
和 hnsw.ef_search
参数设置的值进行调优可能会有助于优化应用性能。
调优参数 | 说明 | 参数类型 |
---|---|---|
m |
图中每个节点的连接数上限。您可以先设置默认值 16 (默认值),然后根据数据集的大小尝试更高的值。 |
索引创建 |
ef_construction |
在图构建期间维护的动态候选项列表的大小,该列表会不断更新节点最近邻的当前最佳候选项。将此值设置为大于 m 值两倍的任何值,例如 64 (默认值)。 |
索引创建 |
ef_search |
搜索期间使用的动态候选项列表的大小。您可以先将此值设置为 m 或 ef_construction ,然后在观察召回率期间进行更改。默认值为 40 。 |
查询运行时 |
请考虑以下示例,其中显示设置了调优参数的 hnsw
索引:
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);
分析查询
如以下 SQL 查询示例所示,使用 EXPLAIN ANALYZE
命令分析查询分析洞见。
EXPLAIN ANALYZE SELECT result-column
FROM my-table
ORDER BY EMBEDDING_COLUMN <=> embedding('text-embedding-005', 'What is a database?')::vector
LIMIT 1;
示例响应 QUERY PLAN
包含所用时间、扫描或返回的行数以及使用的资源等信息。
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
查看向量索引指标
您可以使用向量索引指标来查看向量索引的性能、确定需要改进的方面,以及在需要时根据指标对索引进行调优。
如需查看所有向量索引指标,请运行以下 SQL 查询,该查询使用 pg_stat_ann_indexes
视图:
SELECT * FROM pg_stat_ann_indexes;
您将看到类似如下所示的输出:
-[ 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 }}
如需详细了解指标的完整列表,请参阅向量索引指标。