本頁說明如何在 AlloyDB for PostgreSQL 中調整索引,以加快查詢效能並提升召回率。
事前準備
建立 ScaNN 索引前,請先完成下列步驟:
建立資料表,並填入資料。
為避免建立 ScaNN 索引時發生記憶體不足問題,請確保
maintenance_work_mem和shared_buffers資料庫旗標設為小於機器總記憶體的值。如要使用四層索引,必須先為 AlloyDB 執行個體啟用預覽功能。如要啟用預覽功能,請選擇下列其中一種方法:
啟用
scann.enable_preview_features資料庫旗標。如要進一步瞭解如何設定資料庫旗標,請參閱「設定資料庫旗標」。
將工作階段層級的
scann.max_allowed_num_levels資料庫旗標設為3。SET scann.max_allowed_num_levels = 3;
調整 ScaNN 索引
如要判斷 ScaNN 索引所需的層級數量,請參閱下表。
| 資料表中的向量資料列數 | ScaNN 索引的層級數量 |
|---|---|
| [0..10 million] | 二 |
| [10 million..100 million] |
選擇下列其中一種指標做為優先考量:
|
| [1 億..10 億] |
選擇下列其中一種指標做為優先考量: |
| [10 億..100 億] | 四個 (預先發布版) |
請參考下列 ScaNN 索引,為含有 100 萬列的資料表調整參數。
兩層樹狀結構索引
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)]);
三層樹狀結構索引
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);
四層樹狀結構索引
SET LOCAL scann.num_leaves_to_search = 100; 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, 3/4)], max_num_levels = 3);
如要進一步瞭解 ScaNN 索引,請參閱下列頁面:
處理因資料欄引擎加速而導致的 DML 失效
如果您選擇使用資料欄引擎加速向量搜尋,請注意,基礎資料表上的 DML 和 DDL 失效可能會影響向量查詢效能。如果 DML 輸送量偏高,請考慮調整 google_columnar_engine.refresh_threshold_percentage 資料庫旗標,或使用 google_columnar_engine_refresh_index 指令手動重新整理索引。
分析查詢
使用 EXPLAIN ANALYZE 指令分析查詢洞察,如下列 SQL 查詢範例所示。
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
查看向量索引指標
您可以運用向量索引指標查看向量索引的成效、找出可改進的部分,並視需要根據指標調整索引。pg_stat_ann_indexes 檢視畫面可協助您瞭解索引使用率的狀態,pg_stat_ann_index_creation 檢視畫面則提供索引建立時所建立的資料列相關資訊。
如要查看索引使用率指標,請執行下列指令:
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,max_num_levels=1,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 }}
如要查看建立索引時建立的資料列數,請執行下列指令:
SELECT * FROM pg_stat_ann_index_creation;
畫面會顯示類似以下內容的輸出:
-[ RECORD 1 ]----------+---------------------------------------------------------------------------
relid | 271236
indexrelid | 271242
schemaname | public
relname | t1
indexrelname | t1_ix1
index_rows_at_creation_time | 262144
如要進一步瞭解完整的指標清單,請參閱「向量索引指標」。