AlloyDB Omni에서 벡터 쿼리 성능 조정

문서 버전을 선택합니다.

이 문서에서는 색인을 조정하여 AlloyDB Omni에서 더 빠른 쿼리 성능과 더 나은 재현율을 달성하는 방법을 설명합니다.

시작하기 전에

ScaNN 색인을 빌드하기 전에 다음을 완료합니다.

  • 데이터가 포함된 테이블이 이미 생성되었는지 확인합니다.
  • 색인을 생성하는 동안 문제가 발생하지 않도록 maintenance_work_memshared_buffers 플래그에 설정한 값이 총 머신 메모리보다 작은지 확인합니다.
  • 4단계 색인을 사용하려면 먼저 인스턴스에 미리보기 기능을 사용 설정해야 합니다. 미리보기 기능을 사용 설정하려면 다음 두 가지 방법 중 하나를 선택하세요.

ScaNN 색인 조정

다음 안내에 따라 ScaNN 색인에 필요한 수준 수를 결정하세요.

  • 0~1,000만 개 행: 2단계 색인을 선택합니다.
  • 1,000만~1억 개의 행의 경우 다음을 실행합니다.
    • 검색 재현율을 우선시하려면 2단계 색인을 선택하세요.
    • 색인 빌드 시간에 우선순위를 두려면 3단계 색인을 선택합니다.
  • 1억~10억 개의 행의 경우:
    • 검색 재현율을 우선시하려면 3단계 색인을 선택하세요.
    • 색인 빌드 시간을 우선시하려면 4단계 색인 (미리보기)을 선택합니다.
  • 10억~100억 개 행: 4단계 색인을 선택합니다 (미리보기).

다음은 100만 행을 가진 테이블에서 2단계, 3단계, 4단계 ScaNN 색인의 조정 파라미터를 설정하는 방법을 보여주는 예시입니다.

2단계 색인

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)]);

3단계 색인

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);

4단계 색인

(미리보기)

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);

열 기반 엔진을 사용한 가속으로 인한 DML 무효화 처리

열 기반 엔진으로 벡터 검색을 가속화하도록 선택한 경우 기본 테이블의 DML 및 DDL 무효화가 벡터 쿼리 성능에 영향을 줄 수 있습니다. DML 처리량이 많은 경우 google_columnar_engine.refresh_threshold_percentage 데이터베이스 플래그를 조정하거나 google_columnar_engine_refresh_index 명령어를 사용하여 색인을 수동으로 새로고침하는 것이 좋습니다.

쿼리 분석

다음 예시 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

벡터 색인 측정항목 보기

벡터 색인 측정항목을 사용하면 벡터 색인의 성능을 검토하고, 개선이 필요한 영역을 식별하며, 필요할 경우 해당 측정항목에 따라 색인을 조정할 수 있습니다.

모든 벡터 색인 측정항목을 보려면 pg_stat_ann_indexes 뷰를 사용하는 다음 SQL 쿼리를 실행하세요.

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

전체 측정항목 목록에 대한 자세한 내용은 벡터 색인 측정항목을 참조하세요.

다음 단계