目標
- 在 AlloyDB Omni 中安裝 AlloyDB AI。
- 連線至資料庫並安裝必要擴充功能。
- 建立
product和product inventory資料表。 - 將資料插入
product和product inventory資料表,並執行基本向量搜尋。 - 在產品資料表上建立 ScaNN 索引。
- 執行向量搜尋。
- 使用篩選器和聯結執行複雜的向量搜尋。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator 根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」一節。
必要條件
執行向量搜尋前,請先完成下列先決條件。
根據運算環境,在 AlloyDB Omni 中安裝 AlloyDB AI
根據您使用的運算環境,完成「在 AlloyDB Omni 中安裝 AlloyDB AI」中的操作說明,安裝 AlloyDB Omni。
使用 psql 連線至資料庫
使用 psql 連線至資料庫:
export DBPOD=`kubectl get pod --selector=alloydbomni.internal.dbadmin.goog/dbcluster=my-db-cluster,alloydbomni.internal.dbadmin.goog/task-type=database -o jsonpath='{.items[0].metadata.name}'`kubectl exec -ti $DBPOD -c database -- psql -h localhost -U postgres
安裝必要擴充功能
執行下列查詢,安裝 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_inventory 和 product 資料表執行複雜的向量搜尋查詢。
執行下列陳述式,建立可執行下列作業的
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-004', description)) STORED );執行下列查詢,建立
product_inventory資料表,儲存可用商品目錄和相應價格的相關資訊。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;如果您在未建立索引的情況下執行基本向量搜尋,AlloyDB AI 會使用 KNN,這項技術可有效召回資料;不過,大規模使用 KNN 可能會影響效能。為提升查詢效能,建議您使用 ScaNN 索引進行 ANN 搜尋,這類索引可提供高召回率和低延遲。
如果您未建立索引,AlloyDB Omni 預設會使用 KNN。
在產品資料表上建立 ScaNN 索引
執行下列查詢,在 product 資料表上建立 product_index ScaNN 索引:
CREATE INDEX product_index ON product
USING scann (embedding cosine)
WITH (num_leaves=5);
num_leaves 參數表示樹狀結構索引用來建構索引的葉節點數量。如要進一步瞭解如何調整這個參數,請參閱「調整向量查詢效能」。
執行向量搜尋
執行下列向量搜尋查詢,嘗試找出與自然語言查詢 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 參數值有助於平衡查詢效能和喚回率。
執行使用篩選器和聯結的向量搜尋
執行下列複雜的向量搜尋查詢,即使有篩選條件,也會傳回符合查詢條件的相關結果:
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;
清除所用資源
刪除資料庫叢集
kubectl patch dbclusters.alloydbomni.dbadmin.goog my-db-cluster -p '{"spec":{"isDeleted":true}}' --type=merge解除安裝 AlloyDB Omni 運算子
如要從 Kubernetes 叢集解除安裝 AlloyDB Omni Kubernetes 運算子,請完成下列步驟:
刪除所有資料庫叢集:
for ns in $(kubectl get dbclusters.alloydbomni.dbadmin.goog --all-namespaces -o=jsonpath='{range .items[*]}{.metadata.namespace}{"\n"}{end}'); do for cr in $(kubectl get dbclusters.alloydbomni.dbadmin.goog -n $ns -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'); do kubectl patch dbclusters.alloydbomni.dbadmin.goog $cr -n $ns --type=merge -p '{"spec":{"isDeleted":true}}' done done ```執行下列指令,確認 AlloyDB Omni Kubernetes 運算子已刪除所有資料庫叢集:
kubectl get dbclusters.alloydbomni.dbadmin.goog --all-namespaces刪除 AlloyDB Omni Kubernetes 運算子建立的其他資源:
kubectl delete failovers.alloydbomni.dbadmin.goog --all --all-namespaceskubectl delete restores.alloydbomni.dbadmin.goog --all --all-namespaceskubectl delete switchovers.alloydbomni.dbadmin.goog --all --all-namespaces解除安裝 AlloyDB Omni Kubernetes 運算子:
helm uninstall alloydbomni-operator --namespace alloydb-omni-system清除與 AlloyDB Omni Kubernetes 運算子相關的密鑰、自訂資源說明和命名空間:
kubectl delete certificate -n alloydb-omni-system --allkubectl get secrets --all-namespaces -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,ANNOTATION:.metadata.annotations.cert-manager\.io/issuer-name | grep -E 'alloydbomni|dbs-al' | awk '{print $1 " " $2}' | xargs -n 2 kubectl delete secret -nkubectl delete crd -l alloydb-omni=truekubectl delete ns alloydb-omni-system