使用VectorDBBench测试PostgreSQL协议向量检索性能

更新时间:
复制 MD 格式

向量检索类业务(如 RAG 与语义检索)在选型阶段需要评估数据库在目标召回率下的延迟与吞吐表现。本文介绍如何使用开源基准测试工具VectorDBBench端到端测试PolarDB PostgreSQL版的向量检索性能:以 Recall@100 高于 95% 为召回目标调优检索参数,并给出三个公开数据集在不同并发度下的实测结果。

测试环境

本轮测试的被测端为包含 1 个读写节点的PolarDB PostgreSQL版集群,测试客户端为与集群位于同一可用区的 ECS 实例。

PolarDB集群规格

节点角色

规格

配置

数量

读写节点(RW)

polar.pg.x4.4xlarge

32 核 128 GB

1

数据库版本与关键配置:

项目

配置

数据库版本

PostgreSQL 16.14(PolarDB 2.0.16.14.21.0)

向量引擎

pgvector 扩展 0.8.3.1(含PolarDB自研优化)

向量索引

HNSW(m=16),RaBitQ 8-bit 量化

maintenance_work_mem

24 GB(构建索引时)

max_parallel_maintenance_workers

64

说明

向量数据通过标准 PostgreSQL 协议写入,索引由 CREATE INDEX ... USING hnsw 构建,无需部署任何额外组件。

测试客户端(ECS)

项目

配置

实例规格

ecs.g9i.12xlarge

vCPU / 内存

48 核(vCPU)/ 192 GiB

网络

与数据库集群相同可用区

测试工具与数据集

VectorDBBench 是 Zilliz 开源的向量数据库基准测试工具,支持数据导入、索引构建、串行召回率测量与并发吞吐测量。PolarDB-PG 使用其 polardbpghnsw 子命令,通过标准 PostgreSQL 协议连接。本轮测试选用三个不同规模与维度的公开数据集,覆盖典型 RAG / 语义检索场景:

数据集

向量数量

向量维度

--case-type 参数

Cohere 768D 1M

1,000,000

768

Performance768D1M

Cohere 768D 10M

10,000,000

768

Performance768D10M

OpenAI 1536D 5M

5,000,000

1536

Performance1536D5M

三个数据集的距离度量均为 COSINE,召回率以官方 Ground Truth 的 Top-100 为基准计算(--k 100)。

测试步骤

步骤一:安装 VectorDBBench

获取 VectorDBBench 源码后,执行以下命令创建 conda 环境、完成安装并指定数据集本地目录:

conda create -n polardb-pg-hnsw python=3.11 -y
conda activate polardb-pg-hnsw
cd VectorDBBench && pip install -e .
export DATASET_LOCAL_DIR=/data/dataset/     # 数据集本地目录

步骤二:执行测试

以向量规模最小的 Cohere 768D 1M 数据集为例:

DATASET_LOCAL_DIR=/data/dataset/ \
vectordbbench polardbpghnsw \
    --case-type Performance768D1M \
    --user-name <user> --password <password> \
    --host <host> --port 5432 --db-name vec \
    --k 100 \
    --m 16 --ef-construction 256 --ef-search 185 \
    --quantization rabitq --quantization-nbits 8 \
    --skip-graph-cache \
    --iterative-scan off \
    --maintenance-work-mem 24GB --max-parallel-workers 32 \
    --num-concurrency 20,40,60,80,100 \
    --concurrency-duration 60

执行该命令后,VectorDBBench 完成数据导入、索引构建、串行召回率测量与并发吞吐测量。并发测量按 --num-concurrency 指定的各并发度依次进行,每个并发度持续 --concurrency-duration 指定的时长(本命令为 60 s)。

关键参数说明:

参数

说明

--case-type

选择测试数据集。可选值:Performance768D1M、Performance768D10M、Performance1536D5M。

--m / --ef-construction

HNSW 索引构图参数。--m 控制每个节点的最大邻居数,--ef-construction 控制索引构建时的搜索宽度。

--ef-search

检索时的搜索宽度,值越大召回率越高、延迟也越大。本测试调优至满足召回目标的最低值。

--quantization rabitq --quantization-nbits 8

使用 RaBitQ 8-bit 量化构建 HNSW 索引。

--skip-graph-cache

不启用图缓存,建索引 DDL 中不含 cache 选项(等价 cache=off)。

--k

返回最近邻个数,本测试固定 100(即 Recall@100)。

--maintenance-work-mem / --max-parallel-workers

构建索引阶段的内存与并行度。

--num-concurrency

检索阶段使用的并发度列表,以英文逗号分隔。

--concurrency-duration

每个并发度的持续时间,单位为秒。

说明

测试其他数据集时,将 --case-type 修改为对应的值,并根据数据集规模调整 --ef-construction 与 --ef-search。本轮测试中,Cohere 768D 10M 数据集的索引构建耗时约 30 分钟,为三个数据集中最长。

参考测试结果

Cohere 768D 1M 数据集

  • 测试参数:

    --case-type Performance768D1M --k 100 \
    --m 16 --ef-construction 256 --ef-search 185 \
    --quantization rabitq --quantization-nbits 8 --skip-graph-cache \
    --num-concurrency 20,40,60,80,100 --concurrency-duration 60
  • 不同并发度下的性能表现如下:

    并发度

    QPS

    平均延迟(ms)

    P95 延迟(ms)

    P99 延迟(ms)

    20

    9230.53

    2.16

    2.66

    2.82

    40

    13988.80

    2.85

    4.87

    7.21

    60

    14569.61

    4.10

    8.07

    13.41

    80

    14578.75

    5.45

    11.29

    17.69

    100

    14388.42

    6.89

    16.20

    46.91

Cohere 768D 10M 数据集

  • 测试参数:

    --case-type Performance768D10M --k 100 \
    --m 16 --ef-construction 500 --ef-search 250 \
    --quantization rabitq --quantization-nbits 8 --skip-graph-cache \
    --num-concurrency 20,40,60,80,100 --concurrency-duration 60
  • 不同并发度下的性能表现如下:

    并发度

    QPS

    平均延迟(ms)

    P95 延迟(ms)

    P99 延迟(ms)

    20

    6582.46

    3.03

    3.81

    4.14

    40

    9535.74

    4.18

    7.20

    10.97

    60

    9650.82

    6.20

    15.35

    31.39

    80

    9688.14

    8.22

    24.73

    45.33

    100

    9470.46

    10.51

    25.53

    43.54

OpenAI 1536D 5M 数据集

  • 测试参数:

    --case-type Performance1536D5M --k 100 \
    --m 16 --ef-construction 256 --ef-search 200 \
    --quantization rabitq --quantization-nbits 8 --skip-graph-cache \
    --num-concurrency 20,40,60,80,100 --concurrency-duration 60
  • 不同并发度下的性能表现如下:

    并发度

    QPS

    平均延迟(ms)

    P95 延迟(ms)

    P99 延迟(ms)

    20

    7754.77

    2.57

    3.38

    3.67

    40

    10896.55

    3.66

    6.24

    8.38

    60

    11028.25

    5.42

    13.21

    21.54

    80

    10974.92

    7.25

    16.18

    25.62

    100

    10876.94

    9.14

    20.14

    29.89

汇总对比

三个数据集的关键指标汇总如下:

数据集

向量数量

向量维度

Recall@100

单线程 P50 延迟(ms)

单线程 P95 延迟(ms)

单线程 P99 延迟(ms)

峰值 QPS

峰值并发度

索引构建耗时(optimize,s)

索引大小(MB)

Cohere 768D 1M

1,000,000

768

0.9559

1.8

2.1

2.1

14578.75

80

85.50

1116

Cohere 768D 10M

10,000,000

768

0.9516

2.6

3.1

3.4

9688.14

80

1791.77(约 30 分钟)

11160

OpenAI 1536D 5M

5,000,000

1536

0.9606

2.1

2.6

2.7

11028.25

60

660.67

9766

测试结果分析

  • 召回率与延迟:三个数据集的 Recall@100 均高于 0.95(0.9516~0.9606),单线程 P99 延迟为 2.1~3.4 ms。在保证高召回的同时实现了毫秒级响应。

  • 并发表现:并发度从 20 提升至 40 时 QPS 增长明显,此后趋于饱和,峰值出现在并发度 60~80,并发度提升至 100 时小幅回落。以 Cohere 768D 1M 为例,并发度 80 时达到峰值 14578.75 QPS,平均延迟 5.45 ms。由于PolarDB PostgreSQL版集群支持多节点读,所以随着Replica节点扩展,性能TPS也会线性增长。

  • 高维数据集表现:OpenAI 1536D 5M(1536 维、500 万向量)在并发度 60 下达到 11028.25 QPS,单线程 P99 延迟 2.7 ms,说明 RaBitQ 8-bit 量化在高维场景下能有效压缩距离计算开销,检索性能不随维度显著劣化。

  • 规模扩展:Cohere 768D 10M 的向量数量为 Cohere 768D 1M 的 10 倍,峰值 QPS 为 9688.14,约为后者的 66%。单线程 P99 延迟从 2.1 ms 升至 3.4 ms,Recall@100 保持在 0.95 以上,规模扩展表现平稳。

  • 索引构建耗时:100 万向量的索引构建耗时 85.50 s,1000 万向量耗时 1791.77 s(约 30 分钟),1536 维的 500 万向量耗时 660.67 s。构建速度满足生产环境需求。

  • 索引存储占用:RaBitQ 8-bit 量化下,768 维数据集的索引约为 1.1 KB/向量(1M:1116 MB;10M:11160 MB),1536 维数据集约为 2.0 KB/向量(5M:9766 MB),约为全精度 float32 存储的 1/3。

  • 架构与生态:PolarDB PostgreSQL版集群使用 CREATE INDEX 语句构建向量索引,兼容既有 PostgreSQL 生态与 SQL 语法,无需引入额外的向量数据库组件。其采用共享存储、一写多读架构,只读节点与主节点共享同一份数据与索引,新增只读节点无需重建索引、无需数据迁移,读吞吐可随只读节点数量扩展。