벡터 쿼리에서는 검색에서 검색된 관련 결과의 비율을 측정하므로 재현율이 중요합니다. 재현율은 k-최근접 이웃 (KNN) 검색 결과와 비교하여 근사 최근접 이웃 (ANN) 검색 결과의 정확도를 평가하는 데 도움이 됩니다.
ANN은 주어진 쿼리 포인트와 유사한 데이터 포인트를 찾는 알고리즘으로, 실제 이웃이 아닌 근사 이웃을 찾아 속도를 개선합니다. ANN을 사용하면 속도와 검색을 절충할 수 있습니다.
KNN은 유사성 측정항목을 기반으로 데이터 세트 내에서 주어진 쿼리 벡터와 가장 유사한 k개의 벡터를 찾는 알고리즘입니다. k는 쿼리에서 반환할 이웃의 수입니다.
다음과 같은 다양한 벡터 색인에 대해 벡터 검색 쿼리의 재현율을 측정할 수 있습니다.
- 확장 가능한 최근접 이웃 (ScaNN): 효율적인 벡터 유사성 검색을 위한 알고리즘입니다.
- HNSW (Hierarchical Navigable Small World): 벡터 데이터베이스에서 효율적인 근사 최근접 이웃 검색에 사용되는 그래프 기반 알고리즘입니다.
- IVFFLAT (Inverted File with Flat Compression) 및 IVF (Inverted File Flat): ANN 검색에 사용되는 벡터 색인 유형으로, 특히 PostgreSQL
pgvector
확장 프로그램과 같은 데이터베이스에서 사용됩니다.
이 페이지에서는 PostgreSQL, AlloyDB Omni, 벡터 검색에 익숙하다고 가정합니다.
시작하기 전에
pgvector 확장 프로그램을 설치하거나 업데이트합니다.
pgvector
확장 프로그램이 설치되어 있지 않으면vector
확장 프로그램 버전0.8.0.google-3
이상을 설치하여 생성된 임베딩을vector
값으로 저장합니다.vector
확장 프로그램에는pgvector
함수와 연산자가 포함되어 있습니다. Google에서는 AlloyDB Omni에 맞게 최적화된 이 버전의pgvector
를 확장합니다.CREATE EXTENSION IF NOT EXISTS vector WITH VERSION '0.8.0.google-3';
자세한 내용은 벡터 저장, 색인 생성, 쿼리를 참고하세요.
pgvector
확장 프로그램이 이미 설치되어 있는 경우vector
확장 프로그램을 버전 0.8.0.google-3 이상으로 업그레이드하여 재현 평가 도구 기능을 사용하세요.ALTER EXTENSION vector UPDATE TO '0.8.0.google-3';
ScaNN 색인을 만들려면
alloydb_scann
확장 프로그램을 설치하세요.CREATE EXTENSION IF NOT EXISTS alloydb_scann;
벡터 색인에서 벡터 쿼리의 재현율 평가
evaluate_query_recall
함수를 사용하여 특정 구성의 벡터 색인에 대한 벡터 쿼리의 재현율을 확인할 수 있습니다. 이 함수를 사용하면 원하는 벡터 쿼리 리콜 결과를 얻을 수 있도록 매개변수를 조정할 수 있습니다.
재현율은 검색 품질에 사용되는 측정항목으로, 객관적으로 쿼리 벡터에 가장 가까운 반환된 결과의 비율로 정의됩니다. evaluate_query_recall
함수는 기본적으로 사용 설정되어 있습니다.
벡터 쿼리의 재현율 찾기
- AlloyDB Studio에서 SQL 편집기를 열거나
psql
클라이언트를 엽니다. enable_indexscan
플래그가 사용 설정되어 있는지 확인합니다. 플래그가 사용 중지되어 있으면 색인 검색이 선택되지 않으며 모든 색인의 재현율이 1입니다.질문을 매개변수로 사용하고 다음 재현율을 반환하는
evaluate_query_recall
함수를 실행합니다.SELECT * FROM evaluate_query_recall( QUERY_TEXT, QUERY_TIME_CONFIGURATIONS, INDEX_METHODS )
이 명령어를 실행하기 전에 다음을 바꿉니다.
QUERY_TEXT
: SQL 쿼리($$
.QUERY_TIME_CONFIGURATIONS
: Optional: the configuration that you can set for the ANN query. This must be in JSON format. The default value isNULL
.INDEX_METHODS
: Optional: a text array that contains different vector index methods for which you want to calculate the recall. If you set an index method for which a corresponding index doesn't exist, the recall is1
. The input must be a subset of{scann, hnsw, ivf, ivfflat}
. If no value is provided, the ScaNN method is used.To view differences between query recall and execution time, change the query time parameters for your index.
The following table lists query time parameters for ScaNN, HNSW, and IVF/IVFFLAT index methods. The parameters are formatted as
{"scann.num_leaves_to_search":1, "scann.pre_reordering_num_neighbors":10, "hnsw.ef_search": 1}
.Index type Parameters ScaNN scann.num_leaves_to_search
scann.pre_reordering_num_neighbors
scann.pct_leaves_to_search
scann.num_search_threads
HNSW hnsw.ef_search
hnsw.iterative_scan
hnsw.max_scan_tuples
hnsw.scan_mem_multiplier
IVF ivf.probes
IVFFLAT ivfflat.probes
ivfflat.iterative_scan
ivfflat.max_probes
For more information about ScaNN index methods, see AlloyDB Omni ScaNN Index reference. For more information about HNSW and IVF/IVFFLAT index methods, see
pgvector
.
Optional: You can also add configurations from
pg_settings
to theQUERY_TIME_CONFIGURATIONS
. For example, to run a query with columnar engine scan enabled, add the following config frompg_settings
as{"google_columnar_engine.enable_columnar_scan" : on}
.The configurations are set locally in the function. Adding these configurations doesn't impact the configurations that you set in your session. If you don't set any configurations, AlloyDB uses all of the configurations that you set in the session. You can also set only those configurations that are best suited for your use case.
Optional: To view the default configuration settings, run the
SHOW
command or view thepg_settings
.Optional: If you have a ScaNN index for which you want to tune the recall, see the tuning parameters in ScaNN index reference.
The following is an example output, where
ann_execution_time
is the time that it takes a vector query to execute using index scans.ground_truth_execution_time
is the time that it takes the query to run using a sequential scan.ann_execution_time
andground_truth_execution_time
are different from but directly dependent on Execution time in the query plan. Execution time is the total time to execute the query from the client.t=# SELECT * FROM evaluate_query_recall( $$ SELECT id FROM t1 ORDER BY val <=> '[1000,1000,49000]' LIMIT 10 $$, '{"scann.num_leaves_to_search":1, "scann.pre_reordering_num_neighbors":10, "hnsw.ef_search": 1}', ARRAY['scann', 'hnsw']); NOTICE: Recall is 1. This might mean that the vector index is not present on the table or index scan not chosen during query execution. id| query | configurations | recall |ann_execution_time | ground_truth_execution_time | index_type ----+-------------------------------------------------------------------+------------------------------------------------------------------------------------------------+--------+--------------------+-----------------------------+------------ 1 | SELECT id FROM t1 ORDER BY val <=> '[1000,1000,49000]' LIMIT 10 | {"scann.num_leaves_to_search":1, "scann.pre_reordering_num_neighbors":10, "hnsw.ef_search": 1} | 0.5 | 4.23 | 118.211 | scann 2 | SELECT id FROM t1 ORDER BY val <=> '[1000,1000,49000]' LIMIT 10 | {"scann.num_leaves_to_search":1, "scann.pre_reordering_num_neighbors":10, "hnsw.ef_search": 1} | 1 | 107.198 | 118.211 | hnsw (2 rows)
로 묶임)결과가
Recall is 1
(쿼리의 재현율이1
)인 경우 테이블에 벡터 색인이 없거나 쿼리 실행 중에 벡터 색인이 선택되지 않았음을 나타낼 수 있습니다. 이 상황은 테이블에 벡터 색인이 없거나 플래너가 벡터 색인 검색을 선택하지 않는 경우에 발생합니다.쿼리가
select id, name from table order by embedding <->'[1,2,3]' LIMIT 10;.
이고 열 이름의 예상 값이
NULL
인 경우 쿼리를 다음 중 하나로 변경합니다.select id, COALESCE(name, 'NULL') as name from table order by embedding <-> '[1,2,3]' LIMIT 10;
또는
select id from table order by embedding <-> '[1,2,3]' LIMIT 10;