PostgreSQL용 AlloyDB에서 벡터 쿼리 성능 조정

PostgreSQL용 AlloyDB에서 더 빠른 쿼리 성능과 더 나은 리콜을 달성하기 위해 다음 벡터 인덱스를 조정하는 방법을 알아보세요.

쿼리를 분석하고 벡터 색인 측정항목을 확인하여 쿼리 성능을 모니터링하고 개선할 수도 있습니다.

IVFFlat 색인 조정

애플리케이션 성능 최적화에는 listsivfflat.probes 파라미터 값을 조정하는 것이 도움이 될 수 있습니다.

조정 파라미터 설명 매개변수 유형
lists 색인 빌드 중에 생성된 목록 수입니다. 이 값을 설정하는 시작점은 행 수가 100만 개 이하일 경우 (rows)/1000, 100만 개를 초과할 경우 sqrt(rows)입니다. 색인 생성
ivfflat.probes 검색 중에 탐색할 최근접 목록 수입니다. 이 값의 시작점은
sqrt(lists)입니다.
쿼리 런타임

IVFFlat 색인을 빌드하기 전에 데이터베이스의 max_parallel_maintenance_workers 플래그가 대규모 테이블에서 색인 생성을 촉진할 수 있는 값으로 설정되어 있는지 확인하세요.

다음은 조정 파라미터가 설정된 IVFFlat 색인을 보여주는 예시입니다.

SET LOCAL ivfflat.probes = 10;

CREATE INDEX my-ivfflat-index ON my-table
  USING ivfflat (vector_column cosine)
  WITH (lists = 100);

쿼리 분석

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

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

다음 단계