共计 2207 个字符,预计需要花费 6 分钟才能阅读完成。
背景痛点:为什么需要向量数据库?
在推荐系统和图像搜索场景中,我们经常需要处理高维向量数据。比如:

- 电商推荐中商品 Embedding 向量(通常 300-1000 维)
- 人脸识别中的特征向量(512-2048 维)
传统关系型数据库遇到三大瓶颈:
- 维度灾难 :B 树索引在超过 20 维后效率急剧下降
- 计算密集型 :余弦相似度计算无法利用索引加速
- 规模限制 :千万级向量全表扫描耗时超过 1 秒
主流方案技术对比
我们测试了三款主流工具在 AWS c5.4xlarge 上的表现:
| 特性 | Faiss (CPU) | Milvus (分布式) | Weaviate (云原生) |
|---|---|---|---|
| 索引类型 | IVF+PQ | HNSW | HNSW+Quantization |
| 10M 向量构建耗时 | 32 分钟 | 41 分钟 | 38 分钟 |
| QPS@P99<50ms | 12,000 | 8,500 | 6,200 |
| 内存占用 | 8GB | 14GB | 11GB |
Faiss 实战:从入门到生产
基础索引构建
import faiss
import numpy as np
# 生成随机测试数据
d = 512 # 向量维度
nb = 1000000 # 数据库大小
np.random.seed(1234)
xb = np.random.random((nb, d)).astype('float32')
# 构建 IVF2048+PQ16 索引
quantizer = faiss.IndexFlatL2(d)
index = faiss.IndexIVFPQ(quantizer, d, 2048, 16, 8)
assert not index.is_trained
index.train(xb) # 训练索引
index.add(xb) # 添加向量
# 查询示例
k = 5 # 返回 top5
xq = np.random.random((1, d)).astype('float32')
D, I = index.search(xq, k) # D 是距离,I 是索引
GPU 加速配置
res = faiss.StandardGpuResources()
# 转换到 GPU 版本
gpu_index = faiss.index_cpu_to_gpu(res, 0, index)
# 多 GPU 配置
# gpu_index = faiss.index_cpu_to_all_gpus(index)
Milvus 分布式部署要点
docker-compose.yml 关键配置
version: '3'
services:
etcd:
image: quay.io/coreos/etcd:v3.5.0
ports:
- "2379:2379"
environment:
- ETCD_AUTO_COMPACTION_MODE=revision
- ETCD_AUTO_COMPACTION_RETENTION=1000
minio:
image: minio/minio:RELEASE.2021-06-17T00-10-46Z
ports:
- "9000:9000"
environment:
- MINIO_ACCESS_KEY=minioadmin
- MINIO_SECRET_KEY=minioadmin
command: server /data
pulsar:
image: apachepulsar/pulsar:2.8.0
ports:
- "6650:6650"
command: >
bin/pulsar standalone
--no-functions-worker
-nss
性能压测方案设计
使用 JMeter 进行阶梯式压测:
- 测试场景
- 并发用户:50/100/200
- 向量维度:512
-
数据集:10M 向量
-
关键指标
- 吞吐量 (QPS)
- 95 分位延迟
-
内存增长曲线
-
测试脚本要点
// JMeter Groovy 脚本片段 import io.milvus.client.* def client = new MilvusGrpcClient("localhost", 19530) // 构建查询请求 List<List<Float>> queryVectors = generateRandomVectors(512, 100) SearchParam param = SearchParam.create(collectionName) .setFloatVectors(queryVectors) .setTopK(10) .setMetricType(MetricType.IP) // 执行压测 SearchResult result = client.search(param)
避坑实践指南
内存优化方案
- mmap 模式配置 (Faiss)
# 创建时启用 mmap index = faiss.read_index("trained.index", faiss.IO_FLAG_MMAP) # 定期检查内存 if os.path.getsize("index.file") > 0.8 * RAM_SIZE: trigger_compaction()
分布式一致性策略
Milvus 采用改进版一致性哈希:
- 虚拟节点数 = 物理节点数×200
- 动态负载均衡阈值:±15% 节点负载差异
- 数据迁移时限制带宽占用≤30%
延伸优化方向
- RDMA 网络 :使用 RoCEv2 协议降低节点间通信延迟
- 持久化内存 :Intel Optane PMem 存储热索引
- 混合精度量化 :FP16 存储 +FP32 计算平衡精度 / 性能
总结建议
根据我们的测试经验:
- 单机研发环境 :优先选择 Faiss + mmap 模式
- 中小规模生产 :Milvus 单机版 + 本地 SSD
- 超大规模集群 :Milvus 分布式 + 对象存储
未来可以关注向量数据库与 LLM 结合的创新应用,比如通过向量检索实现大模型的记忆增强。
正文完
发表至: 未分类
近一天内
