מדידת הזיכרון של שאילתות וקטוריות

בחירת גרסה של מאמר העזרה:

בדף הזה מוסבר איך למדוד את היכולת של שאילתות וקטוריות לאחזר מידע ב-AlloyDB Omni. בהקשר של חיפוש וקטורי, recall הוא אחוז הווקטורים שמוחזרים מהאינדקס שהם השכנים הקרובים האמיתיים. לדוגמה, אם שאילתת השכנים הקרובים של 20 השכנים הקרובים ביותר מחזירה 19 מתוך השכנים הקרובים ביותר של נתוני האמת, אז ה-recall הוא 19/20x100 = 95%.

בשאילתת וקטורים, הזיכרון חשוב כי הוא מודד את אחוז התוצאות הרלוונטיות שאוחזרו מחיפוש. המדד 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 וחיפוש וקטורי.

לפני שמתחילים

  1. מתקינים או מעדכנים את התוסף pgvector.

    1. אם התוסף 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';
      

      מידע נוסף מופיע במאמר אחסון, יצירת אינדקס ושאילתות של וקטורים.

    2. אם התוסף pgvector כבר מותקן, צריך לשדרג את התוסף vector לגרסה 0.8.0.google-3 או לגרסה מתקדמת יותר כדי לקבל יכולות של כלי להערכת היזכרות.

      ALTER EXTENSION vector UPDATE TO '0.8.0.google-3';
      
  2. כדי ליצור אינדקסים של ScaNN, צריך להתקין את alloydb_scannהתוסף.

    CREATE EXTENSION IF NOT EXISTS alloydb_scann;
    

הערכת ההחזרה של שאילתות וקטוריות באינדקס וקטורי

אפשר למצוא את ה-recall של שאילתת וקטור באינדקס וקטורי להגדרה נתונה באמצעות הפונקציה evaluate_query_recall. הפונקציה הזו מאפשרת לכם לכוונן את הפרמטרים כדי להשיג את תוצאות ההחזרה של שאילתת הווקטור שאתם רוצים. החזרה היא המדד שמשמש לאיכות החיפוש, והיא מוגדרת כאחוז התוצאות שהוחזרו והן הכי קרובות באופן אובייקטיבי לווקטורים של השאילתה. הפונקציה evaluate_query_recall מופעלת כברירת מחדל.

איך מוצאים את ה-recall של שאילתה וקטורית

  1. פותחים עורך SQL ב-AlloyDB Studio או פותחים לקוח psql.
  2. יצירת אינדקס וקטורי מסוג ScaNN,‏ HNSW או IVFFLAT

  3. מוודאים שהדגל enable_indexscan מופעל. אם הדגל מושבת, לא נבחר סריקת אינדקס וההחזרה לכל האינדקסים היא 1.

  4. מריצים את הפונקציה 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 is NULL.
    • 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 is 1. 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.

  5. Optional: You can also add configurations from pg_settings to the QUERY_TIME_CONFIGURATIONS. For example, to run a query with columnar engine scan enabled, add the following config from pg_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.

  6. Optional: To view the default configuration settings, run the SHOW command or view the pg_settings.

  7. 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 and ground_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;
    

המאמרים הבאים