בדף הזה מוסבר איך לשפר את האינדקסים כדי להשיג ביצועים מהירים יותר של שאילתות ודיוק גבוה יותר ב-AlloyDB ל-PostgreSQL.
ניתוח השאילתות
כדי לנתח את התובנות לגבי השאילתה, משתמשים בפקודה EXPLAIN ANALYZE, כמו בדוגמה הבאה של שאילתת SQL.
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 אפשר להבין את מצב הניצול של האינדקס, ובתצוגה pg_stat_ann_index_creation אפשר לראות מידע על השורות שנוצרו בזמן יצירת האינדקס.
כדי לראות את מדדי הניצול של האינדקס, מריצים את הפקודה הבאה:
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 }}
כדי לראות את מספר השורות שנוצרו בזמן יצירת האינדקס, מריצים את הפקודה הבאה:
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
למידע נוסף על הרשימה המלאה של המדדים, אפשר לעיין במאמר מדדים של אינדקס וקטורי.