🚀 免费试用 Zilliz Cloud,完全托管的 Milvus,体验 10 倍的性能提升!立即试用>

milvus-logo
LFAI

Milvus 2.0 - 新功能一瞥

  • Engineering
January 27, 2022
Yanliang Qiao

Milvus 2.0 第一个候选版本发布至今已有半年时间。现在,我们自豪地宣布 Milvus 2.0 正式发布。请跟随我一步步了解 Milvus 支持的一些新功能。

实体删除

Milvus 2.0 支持实体删除,允许用户根据向量的主键(ID)删除向量。他们再也不用担心过期或无效数据了。让我们试试看。

  1. 连接到 Milvus,创建一个新的 Collections,然后插入 300 行随机生成的 128 维向量。
from pymilvus import connections, utility, Collection, DataType, FieldSchema, CollectionSchema
# connect to milvus
host = 'x.x.x.x'
connections.add_connection(default={"host": host, "port": 19530})
connections.connect(alias='default')
# create a collection with customized primary field: id_field
dim = 128
id_field = FieldSchema(name="cus_id", dtype=DataType.INT64, is_primary=True)
age_field = FieldSchema(name="age", dtype=DataType.INT64, description="age")
embedding_field = FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=dim)
schema = CollectionSchema(fields=[id_field, age_field, embedding_field],
                          auto_id=False, description="hello MilMil")
collection_name = "hello_milmil"
collection = Collection(name=collection_name, schema=schema)
import random
# insert data with customized ids
nb = 300
ids = [i for i in range(nb)]
ages = [random.randint(20, 40) for i in range(nb)]
embeddings = [[random.random() for _ in range(dim)] for _ in range(nb)]
entities = [ids, ages, embeddings]
ins_res = collection.insert(entities)
print(f"insert entities primary keys: {ins_res.primary_keys}")
insert entities primary keys: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299]
  1. 在删除之前,先通过搜索或查询检查要删除的实体是否存在,并执行两次以确保结果可靠。
# search
nq = 10
search_vec = [[random.random() for _ in range(dim)] for _ in range(nq)]
search_params = {"metric_type": "L2", "params": {"nprobe": 16}}
limit = 3
# search 2 times to verify the vector persists
for i in range(2):
    results = collection.search(search_vec, embedding_field.name, search_params, limit)
    ids = results[0].ids
    print(f"search result ids: {ids}")
    expr = f"cus_id in {ids}"
    # query to verify the ids exist
    query_res = collection.query(expr)
    print(f"query results: {query_res}")
search result ids: [76, 2, 246]
query results: [{'cus_id': 246}, {'cus_id': 2}, {'cus_id': 76}]
search result ids: [76, 2, 246]
query results: [{'cus_id': 246}, {'cus_id': 2}, {'cus_id': 76}]
  1. 删除 cus_id 为 76 的实体,然后搜索和查询该实体。
print(f"trying to delete one vector: id={ids[0]}")
collection.delete(expr=f"cus_id in {[ids[0]]}")
results = collection.search(search_vec, embedding_field.name, search_params, limit)
ids = results[0].ids
print(f"after deleted: search result ids: {ids}")
expr = f"cus_id in {ids}"
# query to verify the id exists
query_res = collection.query(expr)
print(f"after deleted: query res: {query_res}")
print("completed")
trying to delete one vector: id=76
after deleted: search result ids: [76, 2, 246]
after deleted: query res: [{'cus_id': 246}, {'cus_id': 2}, {'cus_id': 76}]
completed

为什么已删除的实体仍可检索?如果你查看过 Milvus 的源代码,就会发现 Milvus 内部的删除是异步和逻辑的,这意味着实体不会被物理删除。相反,它们会被附加一个 "已删除 "标记,这样搜索或查询请求就不会检索到它们。此外,Milvus 默认在 "有界滞后 "一致性级别下进行搜索。因此,在数据节点和查询节点同步数据之前,已删除的实体仍可检索。尝试在几秒钟后搜索或查询已删除的实体,你会发现它已不在结果中了。

expr = f"cus_id in {[76, 2, 246]}"
# query to verify the id exists
query_res = collection.query(expr)
print(f"after deleted: query res: {query_res}")
print("completed")
after deleted: query res: [{'cus_id': 246}, {'cus_id': 2}]
completed

一致性水平

上述实验向我们展示了一致性级别如何影响新删除数据的即时可见性。 用户可以灵活调整 Milvus 的一致性级别,使其适应各种服务场景。Milvus 2.0 支持四种一致性级别:

  • CONSISTENCY_STRONG:GuaranteeTs 设置为与最新的系统时间戳相同,查询节点等待服务时间前进到最新的系统时间戳,然后处理搜索或查询请求。
  • CONSISTENCY_EVENTUALLYGuaranteeTs 设置为小于最新系统时间戳,以跳过一致性检查。查询节点立即在现有数据视图上进行搜索。
  • CONSISTENCY_BOUNDED:GuaranteeTs 设置为比最新系统时间戳相对较小,查询节点在可容忍的、更新较少的数据视图上进行搜索。
  • CONSISTENCY_SESSION:客户端使用最后一次写操作的时间戳作为GuaranteeTs ,这样每个客户端至少可以检索到自己插入的数据。

在之前的 RC 版本中,Milvus 采用 Strong 作为默认一致性。但考虑到大多数用户对一致性的要求低于对性能的要求,Milvus 将默认一致性改为 Bounded Staleness,这样可以在更大程度上平衡用户的要求。未来,我们将进一步优化 GuaranteeTs 的配置,目前的版本只能在创建 Collections 时实现。有关GuaranteeTs 的更多信息,请参阅搜索请求中的保证时间戳

降低一致性会带来更好的性能吗?不试试永远找不到答案。

  1. 修改上面的代码,记录搜索延迟。
for i in range(5):
    start = time.time()
    results = collection.search(search_vec, embedding_field.name, search_params, limit)
    end = time.time()
    print(f"search latency: {round(end-start, 4)}")
    ids = results[0].ids
    print(f"search result ids: {ids}")
  1. 使用相同的数据规模和参数进行搜索,但将consistency_level 设置为CONSISTENCY_STRONG
collection_name = "hello_milmil_consist_strong"
collection = Collection(name=collection_name, schema=schema,
                        consistency_level=CONSISTENCY_STRONG)
search latency: 0.3293
search latency: 0.1949
search latency: 0.1998
search latency: 0.2016
search latency: 0.198
completed
  1. 在将consistency_level 设置为CONSISTENCY_BOUNDED 的 Collections 中搜索。
collection_name = "hello_milmil_consist_bounded"
collection = Collection(name=collection_name, schema=schema,
                        consistency_level=CONSISTENCY_BOUNDED)
search latency: 0.0144
search latency: 0.0104
search latency: 0.0107
search latency: 0.0104
search latency: 0.0102
completed
  1. 显然,在CONSISTENCY_BOUNDED Collections 中的平均搜索延迟比在CONSISTENCY_STRONG Collections 中短 200ms。

如果一致性级别设置为 "强",删除的实体是否会立即不可见?答案是肯定的。您仍然可以自行尝试。

切换

在处理流数据集时,很多用户习惯先建立索引并加载 Collections,然后再向其中插入数据。在 Milvus 以前的版本中,用户必须在建立索引后手动加载 Collections,用索引替换原始数据,这样既慢又费力。交接功能允许 Milvus 2.0 自动加载索引段,替换达到一定索引阈值的流数据,大大提高了搜索性能。

  1. 在插入更多实体之前,先建立索引并加载 Collections。
# index
index_params = {"index_type": "IVF_SQ8", "metric_type": "L2", "params": {"nlist": 64}}
collection.create_index(field_name=embedding_field.name, index_params=index_params)
# load
collection.load()
  1. 插入 50,000 行实体 200 次(为方便起见,使用相同批次的向量,但这不会影响结果)。
import random
# insert data with customized ids
nb = 50000
ids = [i for i in range(nb)]
ages = [random.randint(20, 40) for i in range(nb)]
embeddings = [[random.random() for _ in range(dim)] for _ in range(nb)]
entities = [ids, ages, embeddings]
for i in range(200):
    ins_res = collection.insert(entities)
    print(f"insert entities primary keys: {ins_res.primary_keys}")
  1. 在插入过程中和插入后检查查询节点中的加载段信息。
# did this in another python console
utility.get_query_segment_info("hello_milmil_handoff")
  1. 你会发现所有加载到查询节点的封存段都有索引。
[segmentID: 430640405514551298
collectionID: 430640403705757697
partitionID: 430640403705757698
mem_size: 394463520
num_rows: 747090
index_name: "_default_idx"
indexID: 430640403745079297
nodeID: 7
state: Sealed
, segmentID: 430640405514551297
collectionID: 430640403705757697
partitionID: 430640403705757698
mem_size: 397536480
num_rows: 752910
index_name: "_default_idx"
indexID: 430640403745079297
nodeID: 7
state: Sealed
...

其他功能

除上述功能外,Milvus 2.0 还引入了数据压缩、动态负载平衡等新功能。请尽情享受与 Milvus 的探索之旅!

在不久的将来,我们将与您分享一系列介绍 Milvus 2.0 新功能设计的博客。

查找我们

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started

Like the article? Spread the word

扩展阅读