本文主要是介绍pgvector docker部署测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
docker pull pgvector/pgvector:pg16
运行
docker run --name pgvector --restart=always -e POSTGRES_USER=pgvector -e POSTGRES_PASSWORD=pgvector -v /srv/tlw/pgvectordata:/var/lib/postgresql/data -p 54333:5432 -d pgvector/pgvector:pg16
CREATE EXTENSION vector;
--创建表
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
--添加字段
ALTER TABLE items ADD COLUMN embedding1 vector(30);
--插入数据
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
--查询
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
SELECT * FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
SELECT * FROM items ORDER BY embedding <=> '[3,1,2]' LIMIT 5;
--插入数据
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
--修改记录
UPDATE items SET embedding = '[1,2,3]' WHERE id = 1;
--删除记录
DELETE FROM items WHERE id = 1;
--Get the nearest neighbors to a row
SELECT * FROM items WHERE id != 1 ORDER BY embedding <-> (SELECT embedding FROM items WHERE id = 1) LIMIT 5;
--Get rows within a certain distance
SELECT * FROM items WHERE embedding <-> '[3,1,2]' < 5;
--embedding 创建索引hnsw
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);
--embedding 创建索引ivfflat
CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100);SELECT * FROM items WHERE id = 1 ORDER BY embedding <-> '[3,1,2]' LIMIT 5;--Use EXPLAIN ANALYZE to debug performance.性能分析
EXPLAIN ANALYZE SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
参考:GitHub - pgvector/pgvector: Open-source vector similarity search for Postgres
这篇关于pgvector docker部署测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!