如要瞭解如何使用經過篩選的向量搜尋功能來修正相似內容搜尋,請參閱「AlloyDB Omni 中的經過篩選的向量搜尋」。
如要瞭解如何使用 Vertex AI 嵌入功能執行向量搜尋,請參閱「開始透過 AlloyDB Omni AI 使用向量嵌入」。
目標
- 建立 AlloyDB Omni 叢集和主要執行個體。
- 連線至資料庫並安裝必要擴充功能。
- 建立
product和product inventory資料表。 - 將資料插入
product和product inventory資料表,並執行基本向量搜尋。 - 在產品資料表上建立 ScaNN 索引。
- 執行基本向量搜尋。
- 使用篩選器和聯結執行複雜的向量搜尋。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator 根據預測用量估算費用。
初次使用 Google Cloud 的使用者可能符合免費試用期資格。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清理」一節。
必要條件
執行向量搜尋前,請先完成下列先決條件:
- 在 AlloyDB Omni 中安裝 AlloyDB AI
-
以
postgres使用者身分執行並連線至 AlloyDB Omni。 -
安裝
vector、alloydb_scann和google_ml_integration擴充功能。CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS alloydb_scann; CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
插入商品目錄資料,並執行基本向量搜尋
執行下列陳述式,建立可執行下列作業的
product資料表:- 儲存產品基本資訊。
- 包含
embedding向量資料欄,可計算及儲存每項產品的產品說明嵌入向量。
CREATE TABLE product ( id INT PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, category VARCHAR(255), color VARCHAR(255), embedding vector(768) GENERATED ALWAYS AS (embedding('text-embedding-005', description)) STORED );如有需要,您可以查看記錄並排解錯誤。
執行下列查詢,建立
product_inventory資料表,儲存可用庫存和對應價格的相關資訊。本教學課程會使用product_inventory和product資料表執行複雜的向量搜尋查詢。CREATE TABLE product_inventory ( id INT PRIMARY KEY, product_id INT REFERENCES product(id), inventory INT, price DECIMAL(10,2) );執行下列查詢,將產品資料插入
product資料表:INSERT INTO product (id, name, description,category, color) VALUES (1, 'Stuffed Elephant', 'Soft plush elephant with floppy ears.', 'Plush Toys', 'Gray'), (2, 'Remote Control Airplane', 'Easy-to-fly remote control airplane.', 'Vehicles', 'Red'), (3, 'Wooden Train Set', 'Classic wooden train set with tracks and trains.', 'Vehicles', 'Multicolor'), (4, 'Kids Tool Set', 'Toy tool set with realistic tools.', 'Pretend Play', 'Multicolor'), (5, 'Play Food Set', 'Set of realistic play food items.', 'Pretend Play', 'Multicolor'), (6, 'Magnetic Tiles', 'Set of colorful magnetic tiles for building.', 'Construction Toys', 'Multicolor'), (7, 'Kids Microscope', 'Microscope for kids with different magnification levels.', 'Educational Toys', 'White'), (8, 'Telescope for Kids', 'Telescope designed for kids to explore the night sky.', 'Educational Toys', 'Blue'), (9, 'Coding Robot', 'Robot that teaches kids basic coding concepts.', 'Educational Toys', 'White'), (10, 'Kids Camera', 'Durable camera for kids to take pictures and videos.', 'Electronics', 'Pink'), (11, 'Walkie Talkies', 'Set of walkie talkies for kids to communicate.', 'Electronics', 'Blue'), (12, 'Karaoke Machine', 'Karaoke machine with built-in microphone and speaker.', 'Electronics', 'Black'), (13, 'Kids Drum Set', 'Drum set designed for kids with adjustable height.', 'Musical Instruments', 'Blue'), (14, 'Kids Guitar', 'Acoustic guitar for kids with nylon strings.', 'Musical Instruments', 'Brown'), (15, 'Kids Keyboard', 'Electronic keyboard with different instrument sounds.', 'Musical Instruments', 'Black'), (16, 'Art Easel', 'Double-sided art easel with chalkboard and whiteboard.', 'Arts & Crafts', 'White'), (17, 'Finger Paints', 'Set of non-toxic finger paints for kids.', 'Arts & Crafts', 'Multicolor'), (18, 'Modeling Clay', 'Set of colorful modeling clay.', 'Arts & Crafts', 'Multicolor'), (19, 'Watercolor Paint Set', 'Watercolor paint set with brushes and palette.', 'Arts & Crafts', 'Multicolor'), (20, 'Beading Kit', 'Kit for making bracelets and necklaces with beads.', 'Arts & Crafts', 'Multicolor'), (21, '3D Puzzle', '3D puzzle of a famous landmark.', 'Puzzles', 'Multicolor'), (22, 'Race Car Track Set', 'Race car track set with cars and accessories.', 'Vehicles', 'Multicolor'), (23, 'RC Monster Truck', 'Remote control monster truck with oversized tires.', 'Vehicles', 'Green'), (24, 'Train Track Expansion Set', 'Expansion set for wooden train tracks.', 'Vehicles', 'Multicolor');選用:執行下列查詢,確認資料已插入
product資料表:SELECT * FROM product;執行下列查詢,將庫存資料插入
product_inventory資料表:INSERT INTO product_inventory (id, product_id, inventory, price) VALUES (1, 1, 9, 13.09), (2, 2, 40, 79.82), (3, 3, 34, 52.49), (4, 4, 9, 12.03), (5, 5, 36, 71.29), (6, 6, 10, 51.49), (7, 7, 7, 37.35), (8, 8, 6, 10.87), (9, 9, 7, 42.47), (10, 10, 3, 24.35), (11, 11, 4, 10.20), (12, 12, 47, 74.57), (13, 13, 5, 28.54), (14, 14, 11, 25.58), (15, 15, 21, 69.84), (16, 16, 6, 47.73), (17, 17, 26, 81.00), (18, 18, 11, 91.60), (19, 19, 8, 78.53), (20, 20, 43, 84.33), (21, 21, 46, 90.01), (22, 22, 6, 49.82), (23, 23, 37, 50.20), (24, 24, 27, 99.27);執行下列向量搜尋查詢,嘗試找出與字詞
music相似的產品。這表示即使產品說明中未明確提及字詞music,結果仍會顯示與查詢相關的產品:SELECT * FROM product ORDER BY embedding <=> embedding('text-embedding-005', 'music')::vector LIMIT 3;查詢結果如下:

執行基本向量搜尋時,不需要建立索引,系統會使用 K 近鄰 (KNN) 搜尋,有效召回結果。大規模使用 KNN 可能會影響效能。為提升查詢效能,建議您使用 ScaNN 索引進行近似最鄰近 (ANN) 搜尋,這類索引可提供高召回率和低延遲。
如果沒有建立索引,AlloyDB Omni 預設會使用精確的最鄰近搜尋 (KNN)。
如要進一步瞭解如何大規模使用 ScaNN,請參閱「開始透過 AlloyDB AI 使用向量嵌入」。
在產品資料表上建立手動調整的 ScaNN 索引
執行下列查詢,在 product
資料表上建立 product_index ScaNN 索引:
CREATE INDEX product_index ON product
USING scann (embedding cosine)
WITH (mode='MANUAL', num_leaves=4);
如要進一步瞭解如何建立 ScaNN 索引,請參閱「建立 ScaNN 索引」。
執行向量搜尋
執行下列向量搜尋查詢,嘗試找出與自然語言查詢 music 相似的產品。即使產品說明中未包含「music」一詞,結果仍會顯示與查詢相關的產品:
SET LOCAL scann.num_leaves_to_search = 2;
SELECT * FROM product
ORDER BY embedding <=> embedding('text-embedding-005', 'music')::vector
LIMIT 3;
查詢結果如下:

scann.num_leaves_to_search 查詢參數可控制相似度搜尋期間搜尋的葉節點數量。num_leaves 和 scann.num_leaves_to_search 參數值有助於平衡效能和召回率。
執行使用篩選器和聯結的向量搜尋
即使使用 ScaNN 索引,您也能有效率地執行經過篩選的向量搜尋查詢。執行下列複雜的向量搜尋查詢,即使有篩選條件,也會傳回符合查詢條件的相關結果:
SET LOCAL scann.num_leaves_to_search = 2;
SELECT * FROM product p
JOIN product_inventory pi ON p.id = pi.product_id
WHERE pi.price < 80.00
ORDER BY embedding <=> embedding('text-embedding-005', 'music')::vector
LIMIT 3;
加快篩選後的向量搜尋速度
搭配高選擇性述詞篩選 (例如在資料庫中使用 LIKE) 時,您可以使用直欄式引擎內容存放區,提升向量相似度搜尋 (特別是 K 近鄰 (KNN) 搜尋) 的效能。在本節中,您會使用 vector 擴充功能和 AlloyDB Omni google_columnar_engine 擴充功能。如要進一步瞭解資料欄引擎的運作方式,請參閱「直欄式引擎總覽」。
欄式引擎內建效率,可掃描大型資料集並套用篩選器 (例如述詞),加上使用向量支援功能預先篩選資料列,因此能提升效能。這項功能可減少後續 KNN 向量距離計算所需的資料子集數量,並協助最佳化涉及標準篩選和向量搜尋的複雜分析查詢。LIKE
直欄式儲存庫提供兩種管理內容的選項:
- 自動管理資料欄存放區內容:新的 AlloyDB Omni 執行個體預設會使用自動資料欄化功能。或者,您也可以手動執行自動直欄化功能。
- 手動管理資料行存放區內容: 如要手動管理工作負載的資料行存放區中的資料行,可以停用自動分欄。
如要比較啟用資料欄引擎前後,以述詞篩選的 KNN 向量搜尋執行時間,請按照下列步驟操作:LIKE
-
啟用
vector擴充功能,支援向量資料類型和作業。執行下列陳述式,建立含有 ID、文字說明和 512 維度向量嵌入資料欄的範例資料表 (項目)。CREATE EXTENSION IF NOT EXISTS vector; CREATE TABLE items ( id SERIAL PRIMARY KEY, description TEXT, embedding VECTOR(512) );
-
執行下列陳述式,將 100 萬列資料插入範例
items資料表,藉此填入資料。-- Simplified example of inserting matching (~0.1%) and non-matching data INSERT INTO items (description, embedding) SELECT CASE WHEN g % 1000 = 0 THEN 'product_' || md5(random()::text) || '_common' -- ~0.1% match ELSE 'generic_item_' || g || '_' || md5(random()::text) -- ~99.9% don't match END, (SELECT array_agg(random()) FROM generate_series(1, 512))::vector FROM generate_series(1, 999999) g;
-
測量未啟用資料欄引擎時,向量相似度搜尋的基準效能。
SELECT id, description, embedding <-> '[...]' AS distance FROM items WHERE description LIKE '%product_%_common%' ORDER BY embedding <-> '[...]' LIMIT 100;
-
啟用資料欄引擎和向量支援。
-
啟用
google_columnar_engine.enabled和google_columnar_engine.enable_vector_support資料庫旗標。ALTER SYSTEM SET google_columnar_engine.enabled = 'on'; ALTER SYSTEM SET google_columnar_engine.enable_vector_support = 'on';
-
重新啟動 AlloyDB Omni
systemctl restart alloydbomni18
-
-
將
items表格新增至資料欄引擎:SELECT google_columnar_engine_add('items');
-
使用資料欄引擎評估向量相似度搜尋的效能。重新執行先前用於測量基準效能的查詢。
SELECT id, description, embedding <-> '[...]' AS distance FROM items WHERE description LIKE '%product_%_common%' ORDER BY embedding <-> '[...]' LIMIT 100;
-
如要檢查查詢是否使用資料欄引擎執行,請執行下列指令:
explain (analyze) SELECT id, description, embedding <-> '[...]' AS distance FROM items WHERE description LIKE '%product_%_common%' ORDER BY embedding <-> '[...]' LIMIT 100;
清除所用資源
如要解除安裝 AlloyDB Omni,請參閱「管理及監控 AlloyDB Omni」。