Milvus 和 pgvector 对比:数据持续增长时该怎么选
当向量检索成为多个应用共用的生产服务,Milvus 的优势集中在三件事:专用索引和服务端混合检索、写入与索引及查询资源独立扩缩、冷热数据分层存储。数据持续增长时,可把内存和本地盘优先用于活跃检索数据,让历史语料保留在对象存储中按需访问。Postgres 可以继续承担事务、JOIN 和业务记录,Milvus 负责规模化检索。
- Apache-2.0 · GitHub 40K+ stars
- 专用向量与混合检索
- 查询、写入、索引独立扩缩
- 冷热分离 · 对象存储持久化
- Salesforce
- Walmart
- NVIDIA
- eBay
- IBM
- Cisco
- Roblox
- Exa.ai
- OpenEvidence
- Read.ai
- Plaud
- Filevine
Milvus 和 pgvector 扩展性对比:向量表超过一台 Postgres 之后怎么办?
Milvus 是存算分离架构:segment 和 WAL 都在对象存储里,新加的 query 节点直接加载分配给它的 segment,不用从别的节点拷盘;写入、检索、建索引由不同类型的节点各自扩缩,十亿级向量检索靠加节点解决。tiered storage 还能让单个节点服务远超自身内存的数据量。
pgvector 的扩展性就是 Postgres 的扩展性:README 写明,纵向靠给单实例加资源,横向靠 hot standby 只读副本(每个都是全量拷贝,严格只读),再大就用 Citus 或 PgDog 分片。所有写入和 HNSW 建索引都落在主库。文档记录的几条扩展行为:
- HNSW 建索引时间与内存:图放得进 maintenance_work_mem 才快,超出后 Postgres 提示 "hnsw graph no longer fits into maintenance_work_mem",建索引明显变慢。并行构建(0.6.0+)默认 2 个 worker;Jonathan Katz 2023 年在 r7gd.16xlarge 上建 1M × 1,536 维 HNSW 用了 60.10 分钟,索引 7,734 MB。
- 可建索引的维度:vector 最高 2,000 维,halfvec 4,000 维,bit 64,000 维。
- 后过滤召回悬崖:默认 hnsw.ef_search 为 40 时,命中 10% 行的 WHERE 平均只返回约 4 行;0.8.0+ 的迭代索引扫描会继续扫到凑够 k 行,上限 hnsw.max_scan_tuples(默认 20,000)。
- 索引大小与内存:README 说索引放进内存性能更好;AWS 写明 HNSW 索引必须常驻内存,否则延迟劣化。
冷热分离:让检索资源跟着活跃数据扩展
Milvus Tiered Storage(2.6.4+)将 QueryNode 缓存与数据加载解耦:持久数据留在对象存储,字段数据按 chunk、索引按 segment 按需加载;低频缓存可淘汰,关键字段和索引可提前预热。
对于持续累积的知识库、历史文档和 Agent 记忆,这提供了独立控制语料容量与活跃检索资源的空间,减少低频数据长期占用昂贵内存和本地盘的需要。pgvector 也使用 Postgres 缓存并可访问磁盘索引;区别在于 Milvus 原生提供面向分布式向量检索的对象存储与 QueryNode 分层机制。
- 历史数据继续可检索,无需让每份数据都长期驻留 QueryNode 本地。
- 检索流量上升时扩查询资源,语料容量增长时评估对象存储与活跃缓存需求。
冷数据首次访问可能增加延迟;低延迟负载应预热关键数据,或选择全量加载。节省多少资源取决于访问热度、索引、缓存命中率和查询目标。
Milvus、Zilliz Cloud 与 pgvector 性能数据
Cohere 1M,768 维。下表并排列出无过滤及过滤后保留 1% 数据的公开结果。
| 指标 | Milvus | Zilliz Cloud | pgvector |
|---|---|---|---|
| 实例 | 16 vCPU / 64 GB | 8 CU | 2 vCPU / 8 GB |
| 结果来源 | VectorDBBench v2 | VectorDBBench v2 | VectorDBBench v1 · 2023-07 |
| 无过滤:配置 | Milvus-16c64g-sq4u-fp16-force_merge | ZillizCloud-8cu-perf | PgVector-2c8g (Jul 2023, IVF_FLAT, filter=0) |
| 无过滤:QPS | 9,575.69 | 13,316.23 | 10.63 |
| 无过滤:P99 延迟(ms) | 2.3 | 2.0 | 730.7 |
| 无过滤:召回率 | 0.9189 | 0.9383 | 0.8898 |
| 过滤后保留 1%:配置 | Milvus-16c64g-sq8-partition_key | ZillizCloud-8cu-perf-partition_key | PgVector-2c8g (Jul 2023, IVF_FLAT, filter=0.99) |
| 过滤后保留 1%:QPS | 11,280.08 | 10,020.53 | 75.71 |
| 过滤后保留 1%:P99 延迟(ms) | 1.6 | 2.6 | 121.2 |
| 过滤后保留 1%:召回率 | 0.9507 | 0.9788 | 0.9999 |
实例、索引和测试时期不同,以上为公开结果并列展示,非同条件对测。pgvector 为 2023 年 IVFFlat 测试,延迟为 serial P99;来源链接见下方资料列表。
为什么为生产检索选择 Milvus
Milvus 把专用检索、独立扩缩和冷热分层放在同一套服务中。优势在于可以按查询、写入和索引负载分别配置资源,并按访问热度管理缓存;当检索成为业务主路径时,这些能力决定持续增长的服务如何部署与运维。
| 能力 | Milvus | pgvector |
|---|---|---|
| 索引广度 | HNSW 及 SQ、PQ、PRQ 变体;IVF_FLAT、IVF_SQ8、IVF_PQ、IVF_RABITQ;SCANN;超出内存用 DiskANN;GPU_CAGRA、GPU_IVF;最高 32,768 维 | HNSW 和 IVFFlat;vector 可索引 2,000 维,halfvec 4,000 维;二值量化加重排 |
| 内置 BM25 混合检索 | BM25 函数在服务端把文本转成稀疏向量;一次 hybrid_search 用 RRFRanker 或 WeightedRanker 融合稠密与稀疏结果 | tsvector 全文检索加向量查询,在 SQL 或应用层用 RRF 或 cross-encoder 合并 |
| Function Chain 重排 | Decay、Model(TEI、vLLM、Cohere、Voyage)和 Boost ranker 在服务端执行;Milvus 3.0+ 的混合检索支持 Function Chain | 在应用层重排 |
| 百万级多租户 | partition key 用于裁剪检索范围,每次请求仍须带正确的租户过滤;也可按 database、collection、partition 组织租户 | list 分区或每租户一张表 |
| 存算分离与冷热分层 | segment 和 WAL 在对象存储;query 节点独立扩缩;tiered storage 服务远超节点内存和磁盘的数据 | 堆表和索引存放在 Postgres 存储中;HNSW 超出缓存时可能因 I/O 增加而变慢,并非必须完全驻留内存 |
| 边服务边流式写入 | Streaming Node 追加写 WAL,growing 数据实时可查,data 节点同时给 sealed segment 建索引 | 写入和 HNSW 维护都在单个主库 |
| 标量索引与丰富字段 | VARCHAR、JSON、ARRAY、TEXT(Milvus 3.0+)、TIMESTAMPTZ 上建 INVERTED、BITMAP、STL_SORT、Trie、JSON path 索引;过滤在 ANN 检索之前执行,或用 iterative_filter hint | 过滤在索引扫描之后执行;iterative_scan 最多重扫到 hnsw.max_scan_tuples |
pgvector 能做、Milvus 做不到的事
Milvus 一定比 pgvector 好吗?不是。pgvector 的优势不在向量索引,而在 Postgres 围绕这一列向量已经具备的一切。数据本来就在 Postgres 里的团队,这些都是留下来的理由,PGSQL 和 Milvus 的取舍从这里开始。
| 能力 | pgvector | Milvus |
|---|---|---|
| ACID 事务 | 行、向量和审计记录在一个事务里写入,一起回滚 | 按请求设置一致性级别(Strong、Bounded、Session、Eventually);跨请求的 upsert 不保证原子 |
| 与关系数据 JOIN | 一条 SQL 就能把向量表和用户、订单、权限表 JOIN | 不支持跨 collection JOIN;在应用层按主键关联 |
| SQL 与 Postgres 工具链 | 视图、CTE、触发器、EXPLAIN、pg_dump、时间点恢复 | MilvusClient API 加过滤表达式;备份用 milvus-backup |
| 行级安全(RLS) | 按表 CREATE POLICY,覆盖 SELECT、INSERT、UPDATE、DELETE,租户隔离由数据库强制执行 | RBAC 角色与权限;租户范围靠 partition key 和过滤条件 |
| 只运维一个数据库、沿用现有工具 | 在已有 Postgres 上 CREATE EXTENSION vector;任意 ORM 或驱动;RDS、Aurora、Supabase、Neon、Cloud SQL、AlloyDB、Azure 均托管 | 独立服务(Milvus Lite、Standalone 或 Kubernetes),自带 SDK |
pgvector 和 Milvus 哪个好?什么情况下该选 pgvector,一张选型清单
pgvector 维护得很好,把向量放在已有数据库里本身就是优势。向量表一台主库放得下、又需要向量和关系行之间的事务、JOIN 或行级安全时,pgvector 向量数据库就是合适的选择;需要事务更新的 Agent 记忆是典型例子,大语料检索再交给 Milvus。搜 pgvector 替代方案的团队多半撞上了上面那些单主库行为。向量数据库选型按下表逐项核对,运维复杂度也在其中。
| 问题 | 偏向 pgvector | 偏向 Milvus |
|---|---|---|
| 向量表会长到多大? | 一台主库放得下,HNSW 索引放得进内存 | 数亿到数百亿 |
| 有多少租户? | 几十到几千,靠分区或分表 | 百万级,靠 partition key |
| 过滤条件有多挑剔? | 少量宽泛过滤,迭代扫描够用 | 高选择率或多列过滤,标量索引先过滤 |
| 需要 BM25 混合检索吗? | tsvector 加 SQL 里的 RRF 够用 | 服务端 BM25、一次 hybrid_search、Function Chain 重排 |
| 谁来运维? | 现有 Postgres 团队或托管 Postgres | Zilliz Cloud,或 Kubernetes 上的 Milvus Operator |
哪些 AI 公司和大型企业在生产环境用 Milvus?
从 AI 原生公司到大型企业都在生产环境运行 Milvus 或 Zilliz Cloud:milvus.io 用户墙上有 Salesforce、Walmart、NVIDIA、eBay、IBM、Cisco、Roblox、Reddit,也有 Exa.ai、OpenEvidence、Read.ai、Plaud、Filevine。
- OpenEvidence:基于 Zilliz Cloud,检索延迟低于 10 ms,峰值 4,000 QPS。
- Plaud:每个区域 100 亿+ 向量块,平均召回延迟低于 200 ms。
- Exa:借助混合检索,神经搜索延迟从秒级降到 200 ms 以内。
常见问题
Milvus 和 pgvector 谁更快?
表中列出了三者的公开结果,但实例、索引和测试时期不同,不能据此计算同条件性能倍数。选型时应使用相同数据、硬件、过滤和召回目标复测。
pgvector 支持多少条向量?
README 没有条数上限,容量就是主库的容量。AWS 公开的 LAION 100M(768 维)HNSW 索引 367 GB,建了 16.1 小时,128 GB 缓存上冷查询 3.4 QPS。Milvus Standalone 标称约 1 亿向量,Distributed 到数百亿。
pgvector 支持多少维度?
vector 类型最多存 16,000 维,但 HNSW 和 IVFFlat 只能给 2,000 维以内的 vector 建索引;halfvec 可索引到 4,000 维,bit 到 64,000 维。Milvus 单个向量字段最高 32,768 维,可直接建索引。
pgvector 支持混合检索吗?
支持:向量查询配合 Postgres 全文检索(tsvector),再用 RRF 或 cross-encoder 合并。Milvus 的 BM25 在服务端运行,一次 hybrid_search 就能融合稠密和稀疏结果。
什么情况下该选 pgvector?
向量和需要事务更新、JOIN 的关系行放在一起,表一台 Postgres 实例装得下,又想用同一个数据库的行级安全和 SQL 时,选 pgvector;规模、过滤吞吐、租户数或持续写入成为瓶颈时,选 Milvus。
什么时候该从 pgvector 迁移到 Milvus?
HNSW 建索引超出 maintenance_work_mem、读扩展只能靠全量副本、或租户数和过滤吞吐成为瓶颈时。迁移是拷贝:VTS 的 pg_to_milvus 原样搬向量和列,建好索引后双写切流,关系表留在 Postgres。
资料来源与技术文档 (75)
- https://milvus.io
- https://github.com/milvus-io/milvus
- https://milvus.io/docs/architecture_overview.md
- https://milvus.io/docs/install-overview.md
- https://milvus.io/docs/scaleout.md
- https://milvus.io/docs/tiered-storage-overview.md
- https://milvus.io/docs/index-explained.md
- https://milvus.io/docs/ivf-rabitq.md
- https://milvus.io/docs/diskann.md
- https://milvus.io/docs/gpu_index.md
- https://milvus.io/docs/limitations.md
- https://milvus.io/docs/schema.md
- https://milvus.io/docs/text.md
- https://milvus.io/docs/use-json-fields.md
- https://milvus.io/docs/index-scalar-fields.md
- https://milvus.io/docs/filtered-search.md
- https://milvus.io/docs/use-partition-key.md
- https://milvus.io/docs/multi_tenancy.md
- https://milvus.io/docs/full-text-search.md
- https://milvus.io/docs/multi-vector-search.md
- https://milvus.io/docs/reranking.md
- https://milvus.io/docs/decay-ranker-overview.md
- https://milvus.io/docs/model-ranker-overview.md
- https://milvus.io/docs/boost-ranker.md
- https://milvus.io/docs/release_notes.md
- https://milvus.io/docs/embedding-function-overview.md
- https://milvus.io/docs/consistency.md
- https://milvus.io/docs/upsert-entities.md
- https://milvus.io/docs/rbac.md
- https://milvus.io/docs/quickstart.md
- https://milvus.io/docs/install_standalone-docker.md
- https://milvus.io/docs/integrations_overview.md
- https://github.com/zilliztech/milvus-backup
- https://github.com/zilliztech/vts
- https://github.com/zilliztech/vts/blob/master/seatunnel-examples/seatunnel-engine-examples/src/main/resources/examples/pg_to_milvus.conf
- https://github.com/pgvector/pgvector
- https://github.com/pgvector/pgvector/blob/master/CHANGELOG.md
- https://github.com/pgvector/pgvector/blob/master/LICENSE
- https://www.postgresql.org/docs/current/high-availability.html
- https://www.postgresql.org/docs/current/hot-standby.html
- https://www.postgresql.org/docs/current/ddl-rowsecurity.html
- https://docs.aws.amazon.com/AmazonRDS/latest/PostgreSQLReleaseNotes/postgresql-extensions.html
- https://supabase.com/docs/guides/database/extensions/pgvector
- https://neon.com/docs/extensions/pgvector
- https://docs.cloud.google.com/sql/docs/postgres/extensions
- https://docs.cloud.google.com/alloydb/docs/reference/extensions
- https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/how-to-use-pgvector
- https://zilliz.com/vdbbench-leaderboard
- https://github.com/zilliztech/VectorDBBench
- https://github.com/zilliztech/VectorDBBench/blob/main/vectordb_bench/results/leaderboard_v2.json
- https://github.com/zilliztech/VectorDBBench/blob/main/vectordb_bench/results/PgVector/result_20230727_standard_pgvector.json
- https://github.com/zilliztech/VectorDBBench/tree/main/vectordb_bench/backend/clients
- https://jkatz05.com/post/postgres/pgvector-hnsw-performance/
- https://aws.amazon.com/blogs/database/accelerate-hnsw-indexing-and-searching-with-pgvector-on-amazon-aurora-postgresql-compatible-edition-and-amazon-rds-for-postgresql/
- https://supabase.com/blog/pgvector-vs-pinecone
- https://supabase.com/blog/increase-performance-pgvector-hnsw
- https://supabase.com/blog/pgvector-fast-builds
- https://www.tigerdata.com/blog/pgvector-is-now-as-fast-as-pinecone-at-75-less-cost
- https://www.tigerdata.com/blog/pgvector-vs-pinecone
- https://aws.amazon.com/blogs/database/supercharging-vector-search-performance-and-relevance-with-pgvector-0-8-0-on-amazon-aurora-postgresql/
- https://aws.amazon.com/blogs/database/scale-pgvector-with-binary-quantization-on-amazon-aurora-postgresql/
- https://aws.amazon.com/blogs/database/running-pgvector-in-production-on-amazon-aurora-postgresql/
- https://arxiv.org/abs/2603.23710
- https://docs.langchain.com/oss/python/integrations/vectorstores/pgvector
- https://docs.langchain.com/oss/python/integrations/vectorstores/milvus
- https://zilliz.com/customers/filevine
- https://zilliz.com/customers/openevidence
- https://zilliz.com/customers/exa
- https://zilliz.com/customers/airtable
- https://zilliz.com/customers/plaud
- https://zilliz.com/customers/read-ai
- https://zilliz.com/customers/meituan
- https://lfaidata.foundation/projects/
- https://milvus.io/docs/zh/quickstart.md
- https://cloud.zilliz.com.cn/signup