调整索引构建级别
Zilliz Cloud 引入了一个名为 build_level 的参数,该参数允许用户为目标 Collection 平衡存储容量和搜索召回率。对于不常使用或需要更多存储空间的 Collection,您可以牺牲 2% 的召回率,以换取 30% 至 40% 的存储容量增加,反之亦然。本指南介绍了可用选项以及如何使用它们为 Collection 构建索引。
此功能为公测版特性,仅在满足以下情况下适用于 Dedicated 集群:
-
集群类型为 性能型 或 容量型,并且
-
集群与 Milvus v2.6.x 兼容。
您可以升级集群来测试此功能。如果您遇到任何需要进一步说明的问题,请联系我们。
概述
不同类型的 Zilliz Cloud 集群在其宣称的存储容量方面存在显著差异。如果性能型集群中的某个 Collection 使用频率不高或需要额外存储容量,在为 Collection 中浮点向量类型(如 FLOAT_VECTOR、FLOAT16_VECTOR 或 BFLOAT16_VECTOR)的向量字段创建索引时,考虑将 build_level 设置为高容量选项。虽然这可能会略微降低召回率,但可以将存储容量提高 30% 至 40%。
build_level 参数有三个选项:高召回率 (2)、平衡型 (1)和高容量 (0)。
-
平衡型 (1)
这是默认选项,在大多数场景下平衡搜索精度和存储容量。
-
精度优先 (2)
此选项优先考虑搜索性能和高召回率,适用于需要高精度的集合。
-
容量优先 (0)
此选项强调存储容量,非常适合需要额外存储空间的收藏。
内部基准测试显示,默认选项可增加所有集群的存储容量,无论其类型如何。对于性能型集群,默认选项甚至可使存储容量提升 60%,性能(QPS)提升 17%。
性能型集群
下表比较了性能型集群在引入 build_level 前后的容量、QPS 和召回率。可以看到,默认选项在保持召回率的同时,提高了 QPS 和存储容量。
| 构建级别选项 | 容量 | QPS | 召回率 |
|---|---|---|---|
| 引入 Build Level 之前(基线) | 150万个768维向量 | ~ 3,000 | 91% - 97% |
| 容量优先(0) | 210万个768维向量(↑250%) | ~ 3,000 | 90% - 95% |
| 平衡型(1) | 150万个768维向量(↑ 60%) | ~ 3,500 (↑ 17%) | 91% - 97% |
| 精度优先 (2) | 100万个768维向量 | ~ 2,850 (↓ ~5%) | 92% - 98% (↑) |
容量型集群
下表比较了容量型集群在引入 build_level 前后的容量、QPS 和召回率。可以看到,默认选项在保持召回率的同时,提高了 QPS 和存储容量。
| 构建级别选项 | 容量 | QPS | 召回率 |
|---|---|---|---|
| 引入 Build Level 之前(基线) | 500万个768维向量 | ~ 340 | 93% - 98% |
| 容量优先(0) | 700万个768维向量(↑ 100%) | ~ 300 | 89% - 97% |
| 平衡型(1) | 500万个768维向量(↑ 50%) | ~ 350 (↑ 3%) | 92% - 97% |
| 精度优先 (2) | 300万个768维向量 | ~ 345 | 94% - 98% (↑) |
约束与限制
在开始操作之前,请了解以下限制条件:
-
在对 Collection 进行索引时,您需要在浮点向量类型的向量字段上设置此参数,可用的向量类型包括FLOAT_VECTOR、FLOAT16_VECTOR 和 BFLOAT16_VECTOR。
-
一旦设置,此参数将无法修改。不过,如果有必要,您可以删除该索引并使用所需设置创建另一个索引。
-
迁移或备份将移除
build_level设置。迁移或恢复完成后,如果需要,您可以删除该索引并使用所需设置创建另一个集合。
操作步骤
在大多数情况下,您不需要设置 build_level。默认设置可帮助您在搜索性能、精度和存储容量之间取得平衡。
Zilliz Cloud 允许您以编程方式或在Zilliz Cloud控制台中设置 build_level。
以编程方式设置 build_level
要设置 build_level,你需要在为浮点类型的向量字段创建索引时进行,例如 FLOAT_VECTOR、FLOAT16_VECTOR 和 BFLOAT16_VECTOR。
以下示例假设您已完成准备工作中的步骤。将 build_level 设置为 1 表示使用平衡型选项。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
# 4. Set up index
# 4.1. Set up the index parameters
index_params = MilvusClient.prepare_index_params()
# 4.2. Add an index on the vector field
index_params.add_index(
field_name="vector",
metric_type="COSINE",
index_type="AUTOINDEX",
index_name="vector_index",
build_level=1
)
# 4.3. Create the index
client.create_index(
collection_name="customized_setup",
index_params=index_params
)
# 5. List indexes
res = client.list_indexes(
collection_name="customized_setup"
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
import io.milvus.v2.service.index.request.ListIndexesReq;
import java.util.Collections;
import java.util.List;
IndexParam indexParam = IndexParam.builder()
.fieldName("vector")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.COSINE)
.indexName("vector_index")
.extraParams(Collections.singletonMap("build_level", 1))
.build();
client.createIndex(CreateIndexReq.builder()
.collectionName("customized_setup")
.indexParams(Collections.singletonList(indexParam))
.build());
// 5. List indexes
List<String> indexNames = client.listIndexes(ListIndexesReq.builder()
.collectionName("customized_setup")
.build());
import (
"log"
"github.com/milvus-io/milvus/client/v3/entity"
"github.com/milvus-io/milvus/client/v3/index"
milvusclient "github.com/milvus-io/milvus/client/v3/milvusclient"
)
idx := index.WithExtraIndexParams(
index.NewAutoIndex(entity.COSINE),
map[string]string{"build_level": "1"},
)
_, err := client.CreateIndex(ctx, milvusclient.NewCreateIndexOption("customized_setup", "vector", idx).WithIndexName("vector_index"))
if err != nil {
log.Fatal(err)
}
// 5. List indexes
indexes, err := client.ListIndexes(ctx, milvusclient.NewListIndexOption("customized_setup"))
if err != nil {
log.Fatal(err)
}
log.Println(indexes)
use milvus::v2::prelude::*;
use std::collections::HashMap;
const COLLECTION_NAME: &str = "customized_setup";
client
.create_index(
CreateIndexRequest::builder()
.collection_name(COLLECTION_NAME)
.index_params(vec![
IndexParam::new()
.field_name("vector")
.index_name("vector_index")
.index_type(IndexType::AutoIndex)
.metric_type(MetricType::Cosine)
.extra_params(HashMap::from([("build_level".into(), "1".into())])),
])
.build()?,
)
.await?;
let res = client
.list_indexes(
ListIndexesRequest::builder()
.collection_name(COLLECTION_NAME)
.build()?,
)
.await?;
println!("{:?}", res.index_names());
#include <iostream>
#include <string>
const std::string collection_name = "customized_setup";
milvus::IndexDesc index_vector("vector", "vector_index", milvus::IndexType::AUTOINDEX, milvus::MetricType::COSINE);
index_vector.AddExtraParam("build_level", "1");
auto status = client->CreateIndex(milvus::CreateIndexRequest()
.WithCollectionName(collection_name)
.AddIndex(std::move(index_vector)));
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
milvus::ListIndexesResponse list_resp;
status = client->ListIndexes(milvus::ListIndexesRequest().WithCollectionName(collection_name), list_resp);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
import { MilvusClient } from '@zilliz/milvus2-sdk-node';
const collectionName = 'customized_setup';
// 4. Create the index with build_level
await client.createIndex({
collection_name: collectionName,
field_name: 'vector',
index_type: 'AUTOINDEX',
index_name: 'vector_index',
metric_type: 'COSINE',
params: { build_level: 1 },
});
// 5. List indexes
const res = await client.listIndexes({ collection_name: collectionName });
console.log(res);
curl --request POST \
--url "YOUR_CLUSTER_ENDPOINT/v2/vectordb/indexes/create" \
--header "Authorization: Bearer YOUR_CLUSTER_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"collectionName": "customized_setup",
"indexParams": [
{
"fieldName": "vector",
"indexName": "vector_index",
"metricType": "COSINE",
"indexType": "AUTOINDEX",
"params": { "build_level": 1 }
}
]
}'
curl --request POST \
--url "YOUR_CLUSTER_ENDPOINT/v2/vectordb/indexes/list" \
--header "Authorization: Bearer YOUR_CLUSTER_TOKEN" \
--header "Content-Type: application/json" \
--data '{"collectionName": "customized_setup"}'
在 Zilliz Cloud 控制台设置 build_level
除了以编程方式设置 build_level 之外,您还可以在创建 Collection 时在 Zilliz Cloud 控制台中设置它。
-
点击 + Collection 在目标集群的集合选项卡上。
-
在创建 Collection 页面上设置模式。
确保向量字段的数据类型是有效选项之一:FLOAT_VECTOR、FLOAT16_VECTOR 或 BFLOAT16_VECTOR。
-
在创建索引部分,点击编辑索引。
-
在提示的编辑向量索引字段中,您可以设置度量类型和索引构建级别。