跳到主要内容

向量数据库速查表

快速查阅向量数据库的核心概念、操作和最佳实践。

核心概念速查

向量数据库对比

数据库类型最佳场景数据规模
Pinecone全托管云服务快速上线、无运维任意规模
Milvus开源/云原生大规模生产十亿级+
Weaviate开源/托管复杂查询、GraphQL千万级
Qdrant开源/托管高性能、资源受限千万级
Chroma开源快速原型、本地开发百万级
pgvectorPG 扩展已有 PG 基础设施千万级

相似度度量

度量符号范围适用场景
余弦相似度<=>[-1, 1]文本语义搜索
欧几里得距离<->[0, +∞)几何/物理数据
点积<#>无限制已归一化向量

ANN 索引算法

算法时间复杂度特点适用场景
HNSWO(log n)搜索快、内存高通用首选
IVFO(√n)平衡速度/内存大规模数据
PQO(1)压缩存储内存受限
FlatO(n)100%精确小规模数据

嵌入模型速查

常用模型

模型维度语言特点
text-embedding-3-small1536多语言OpenAI,性价比高
text-embedding-3-large3072多语言OpenAI,高精度
all-MiniLM-L6-v2384英文轻量快速
paraphrase-multilingual384多语言支持中文
BAAI/bge-large-zh1024中文中文最优
M3E-base768中文开源中文

模型选择指南

英文通用 → text-embedding-3-small
英文高精度 → text-embedding-3-large
中文场景 → BAAI/bge-large-zh
资源受限 → all-MiniLM-L6-v2
多语言 → paraphrase-multilingual

代码速查

Pinecone

from pinecone import Pinecone, ServerlessSpec

# 连接
pc = Pinecone(api_key="your-api-key")

# 创建索引
pc.create_index(
name="my-index",
dimension=1536,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)

# 连接索引
index = pc.Index("my-index")

# 插入
index.upsert(vectors=[
{"id": "vec1", "values": [0.1, 0.2, ...], "metadata": {"key": "value"}}
])

# 查询
results = index.query(
vector=[0.1, 0.2, ...],
top_k=10,
filter={"category": {"$eq": "tech"}}
)

Milvus

from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection

# 连接
connections.connect("default", host="localhost", port="19530")

# 创建集合
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True),
FieldSchema(name="text", dtype=DataType.VARCHAR, max_length=512),
FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=1536)
]
schema = CollectionSchema(fields, "my_collection")
collection = Collection("my_collection", schema)

# 创建索引
index_params = {
"metric_type": "COSINE",
"index_type": "HNSW",
"params": {"M": 16, "efConstruction": 200}
}
collection.create_index("embedding", index_params)

# 插入
collection.insert([["text1", "text2"], [vec1, vec2]])

# 加载并搜索
collection.load()
results = collection.search(
data=[query_vector],
anns_field="embedding",
param={"metric_type": "COSINE", "params": {"ef": 64}},
limit=10
)

Chroma

import chromadb

# 客户端
client = chromadb.PersistentClient(path="./chroma_db")

# 创建集合
collection = client.create_collection(name="my_collection")

# 添加
collection.add(
documents=["doc1", "doc2"],
metadatas=[{"category": "tech"}, {"category": "life"}],
ids=["id1", "id2"]
)

# 查询
results = collection.query(
query_texts=["query"],
n_results=10,
where={"category": "tech"}
)

Qdrant

from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct

# 连接
client = QdrantClient(host="localhost", port=6333)

# 创建集合
client.create_collection(
collection_name="my_collection",
vectors_config=VectorParams(size=1536, distance=Distance.COSINE)
)

# 插入
client.upsert(
collection_name="my_collection",
points=[
PointStruct(id=1, vector=[0.1, 0.2, ...], payload={"text": "doc"})
]
)

# 搜索
results = client.search(
collection_name="my_collection",
query_vector=[0.1, 0.2, ...],
query_filter=Filter(must=[FieldCondition(key="category", match=MatchValue(value="tech"))]),
limit=10
)

pgvector (SQL)

-- 创建扩展
CREATE EXTENSION vector;

-- 创建表
CREATE TABLE items (
id SERIAL PRIMARY KEY,
content TEXT,
embedding VECTOR(1536)
);

-- 创建索引
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);

-- 插入
INSERT INTO items (content, embedding)
VALUES ('text', '[0.1, 0.2, ...]');

-- 查询
SELECT id, content, embedding <=> '[0.1, 0.2, ...]' AS distance
FROM items
ORDER BY embedding <=> '[0.1, 0.2, ...]'
LIMIT 10;

RAG 流程速查

1. 文档加载 → 2. 文本切分 → 3. 生成嵌入 → 4. 存入向量库

5. 生成回答 ← 6. 调用 LLM ← 7. 构建上下文 ← 8. 检索相关文档

文档切分策略

策略适用场景代码示例
按段落通用场景text.split('\n\n')
按句子短文本text.split('。')
固定长度长文档text[i:i+chunk_size]
重叠切分保持上下文text[i:i+chunk_size] + overlap

提示词模板

RAG_PROMPT = """基于以下文档回答问题。如果文档中没有相关信息,请说明。

文档:
{context}

问题:{question}

回答:"""

性能优化速查

批量大小建议

操作推荐批量大小说明
插入100-1000平衡速度和内存
查询10-100根据延迟要求调整
嵌入生成100-500取决于 API 限制

索引参数

HNSW 索引

{
"M": 16, # 2-100,越大精度越高
"efConstruction": 200, # 100-500,越大构建越慢
"ef": 64 # 搜索时,> top_k
}

IVF 索引

{
"nlist": 128, # 聚类中心数 ≈ 4×√数据量
"nprobe": 10 # 搜索时,10-100
}

数据规模建议

数据量推荐索引部署方式
< 10万Flat / 无索引单机
10万-1000万HNSW / IVF单机
1000万-1亿HNSW分布式
> 1亿IVF_PQ分布式集群

故障排查速查

常见问题

问题可能原因解决方案
查询结果为空未加载集合collection.load()
搜索结果不准确索引参数不当调整 M/efConstruction
内存不足数据量过大使用量化索引
查询速度慢未创建索引创建 HNSW/IVF 索引
维度不匹配模型维度错误检查向量维度

调试命令

# 检查集合信息
collection.get_stats()

# 查看索引进度
collection.index().progress

# 验证数据
collection.query(expr="id >= 0", output_fields=["*"])

选型决策树

是否需要事务/Join?
├── 是 → pgvector
└── 否 → 继续

是否有运维团队?
├── 否 → Pinecone
└── 是 → 继续

数据规模?
├── < 100万 → Chroma
├── 100万-1000万 → Qdrant / Weaviate
└── > 1000万 → Milvus

资源链接

官方文档

嵌入模型