链载Ai

标题: 索引选不对,成本贵十倍!一文读懂向量索引选型 [打印本页]

作者: 链载Ai    时间: 3 小时前
标题: 索引选不对,成本贵十倍!一文读懂向量索引选型

在使用 Milvus 构建向量检索系统的过程中,很多开发者常常会在“该选哪种索引”这一步卡壳。

是要精度优先的 FLAT,还是速度更快的 IVF_PQ?是适合实时检索的 HNSW,还是适合离线大规模数据的 DiskANN?

不同索引适配的场景差异很大,选错了不仅影响性能,还可能造成资源浪费。

那么该如何对其进行选择呢?本篇文章,带你十分钟读懂不同条件下的索引选型逻辑。

在思考如何挑选索引之前,我们需要知道什么是索引。索引的本质是基于数据的规律,所构建的一种用于加快查找速度的数据结构。

在向量检索中,索引能显著提升查询效率,但也会带来一定的预处理时间开销、额外的存储空间占用,以及在搜索过程中更高的内存消耗。此外,使用索引相对于暴搜而言,通常召回率会出现一定的下降,虽然影响有限,但在对精度有要求的场景中仍需关注。

在 Milvus 中,索引是以字段为单位进行构建的,不同数据类型支持的索引类型也不同。作为一款面向向量检索场景设计的专业数据库,Milvus 在向量搜索与标量过滤两个方面均提供了丰富的索引选项,以提升整体检索性能。

下表展示了字段类型与可用索引类型的对应关系:

Field Data Type

Applicable Index Types

  • FLOAT_VECTOR

  • FLOAT16_VECTOR

  • BFLOAT16_VECTOR

  • INT8_VECTOR

  • FLAT

  • IVF_FLAT

  • IVF_SQ8

  • IVF_PQ

  • GPU_IVF_FLAT

  • GPU_IVF_PQ

  • HNSW

  • DISKANN

  • IVF_RABITQ

BINARY_VECTOR

  • BIN_FLAT

  • BIN_IVF_FLAT

SPARSE_FLOAT_VECTOR

SPARSE_INVERTED_INDEX

VARCHAR

  • INVERTED (Reommended)

  • BITMAP

  • Trie

BOOL

  • BITMAP (Recommended)

  • INVERTED

  • INT8

  • INT16

  • INT32

  • INT64

  • INVERTED

  • STL_SORT

  • FLOAT

  • DOUBLE

INVERTED

ARRAY <sup>(elements of the BOOL, INT8/16/32/64, and VARCHAR types)</sup>

BITMAP (Recommended)

ARRAY <sup>(elements of the BOOL, INT8/16/32/64, FLOAT, DOUBLE, and VARCHAR types)</sup>

INVERTED

JSON

INVERTED


首先,我们必须明白一个前提,做索引选型时,我们必须综合考虑底层数据结构、内存占用和性能需求等因素。

如图所示,Milvus 中的每种索引类型都由三部分组成:数据结构(用于粗过滤)、量化器(用于提升计算效率)和精化器(用于提升结果精度)。其中,量化器和精化器虽然是可选项,但整体性价比较高。

接下来,我们会对这三部分做出依次解读:

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">数据结构

数据结构是索引的基础层,常见结构包括:

(1)倒排文件(IVF)

IVF 系列索引通过质心聚类将向量划分为多个桶。若某桶的质心与查询向量接近,可合理假设该桶内的向量也可能接近查询向量。基于此前提,Milvus 仅扫描质心接近查询向量的桶内向量,而非整个数据集,从而在保持可接受精度的同时,降低计算成本。

适合需要高吞吐量的大规模数据集。

(2)图结构(Graph-based)

例如 HNSW(Hierarchical Navigable Small World),构建了多层图结构,每个向量与其近邻相连。查询过程从上层粗粒度图开始逐层向下,最终定位最邻近向量,实现对数级搜索复杂度。

适合高维空间和低延迟场景。

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">量化(Quantization)

量化通过对向量进行粗略表示来减少内存与计算开销:

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">精化器(Refiner)

由于量化会导致信息损失,为保持召回率,量化通常会多返回候选结果,供精化器以更高精度重新筛选出 topK。例如,FP32 精化器会使用 32 位浮点数重新计算距离,以替代使用量化向量计算出来的距离。

这种机制对语义搜索、推荐系统等要求高精度的场景至关重要。

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">性能

评估索引性能时,应平衡建索时间、QPS 与召回率,通常遵循以下规律:

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">容量

容量涉及数据量与可用内存的关系,建议如下:

注意:mmap 并非万能,若大部分数据在磁盘,DiskANN 延迟表现更优。

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">召回率

召回率与过滤比例密切相关:

提示:以上为经验规律,实际推荐测试不同索引类型以调优召回率。

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">性能(按 top-K)

top-K 指每次查询返回的结果数量:

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">内存占用估算

注:本节包含大量技术细节,若无相关需求可跳过。

索引的内存占用与其数据结构、量化压缩率及是否使用精化器有关。一般来说:

IVF 内存估算(100 万条 128 维向量):


图索引内存估算(HNSW)

ingFang SC", system-ui, -apple-system, "system-ui", "Helvetica Neue", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: 0.544px;orphans: 2;text-align: left;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;clear: both;min-height: 1em;display: block;background-color: rgb(255, 255, 255);visibility: visible;box-sizing: border-box !important;overflow-wrap: break-word !important;">其他注意事项

其中,DiskANN是基于 Vamana 图结构,通过 PQ 压缩向量,在硬盘上建立可导航索引,适用于十亿级数据集。mmap(内存映射)则支持将磁盘文件映射到内存空间,无需完全加载字段数据即可访问,有效减少 I/O 开销,提升系统容量并保持良好搜索性能。

最后,如果上文读完还是记不住,以下两张表存起来,帮你立刻搞懂索引选型逻辑!要是还是没学会,欢迎使用zilliz cloud,用AUTO INDEX功能让AI智能帮你选择最合适的索引。






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5