בשאילתת וקטורים, הזיכרון חשוב כי הוא מודד את אחוז התוצאות הרלוונטיות שאוחזרו מחיפוש. המדד Recall עוזר להעריך את רמת הדיוק של התוצאות מחיפוש של השכן הקרוב המשוער (ANN) בהשוואה לתוצאות מחיפוש של k-השכנים הקרובים (KNN).
ANN הוא אלגוריתם שמאתר נקודות נתונים שדומות לנקודת שאילתה נתונה, והוא משפר את המהירות על ידי איתור השכנים המשוערים במקום השכנים בפועל. כשמשתמשים ב-ANN, יש איזון בין מהירות לבין היכולת לזכור.
KNN הוא אלגוריתם שמאתר את k הווקטורים הכי דומים לווקטור שאילתה נתון בתוך מערך נתונים, על סמך מדד דמיון. k הוא מספר השכנים שרוצים שהשאילתה תחזיר.
אתם יכולים למדוד את הדיוק של שאילתת החיפוש הווקטורי שלכם עבור אינדקסים וקטוריים שונים, כולל:
- Scalable Nearest Neighbors (ScaNN): אלגוריתם לחיפוש יעיל של דמיון בין וקטורים.
- Hierarchical Navigable Small World (HNSW): אלגוריתם מבוסס-גרף שמשמש לחיפוש יעיל של השכן הקרוב ביותר בקירוב במאגרי נתונים וקטוריים.
- קובץ הפוך עם דחיסה שטוחה (IVFFLAT) וקובץ הפוך שטוח (IVF):
סוגים של אינדקסים וקטוריים שמשמשים לחיפושי ANN, במיוחד במסדי נתונים כמו התוסף
pgvectorשל PostgreSQL.
בדף הזה אנחנו מניחים שאתם מכירים את PostgreSQL, AlloyDB Omni וחיפוש וקטורי.
לפני שמתחילים
מתקינים או מעדכנים את התוסף pgvector.
אם התוסף
pgvectorלא מותקן, צריך להתקין את גרסת התוסףvectorאו גרסה מתקדמת יותר כדי לאחסן את ההטמעות שנוצרו כערכים שלvector.0.8.0.google-3התוסףvectorכוללpgvectorפונקציות ואופרטורים. Google מרחיבה את הגרסה הזו שלpgvectorעם אופטימיזציות ל-AlloyDB Omni.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;
הערכת ההחזרה של שאילתות וקטוריות באינדקס וקטורי
אפשר למצוא את ה-recall של שאילתת וקטור באינדקס וקטורי להגדרה נתונה באמצעות הפונקציה evaluate_query_recall. הפונקציה הזו מאפשרת לכם לכוונן את הפרמטרים כדי להשיג את תוצאות ההחזרה של שאילתת הווקטור שאתם רוצים.
החזרה היא המדד שמשמש לאיכות החיפוש, והיא מוגדרת כאחוז התוצאות שהוחזרו והן הכי קרובות באופן אובייקטיבי לווקטורים של השאילתה. הפונקציה evaluate_query_recall מופעלת כברירת מחדל.
איך מוצאים את ה-recall של שאילתה וקטורית
- פותחים עורך SQL ב-AlloyDB Studio או פותחים לקוח
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_searchscann.pre_reordering_num_neighborsscann.pct_leaves_to_searchscann.num_search_threads
HNSW hnsw.ef_searchhnsw.iterative_scanhnsw.max_scan_tupleshnsw.scan_mem_multiplier
IVF ivf.probes
IVFFLAT ivfflat.probesivfflat.iterative_scanivfflat.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_settingsto theQUERY_TIME_CONFIGURATIONS. For example, to run a query with columnar engine scan enabled, add the following config frompg_settingsas{"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
SHOWcommand 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_timeis the time that it takes a vector query to execute using index scans.ground_truth_execution_timeis the time that it takes the query to run using a sequential scan.ann_execution_timeandground_truth_execution_timeare 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;