🚀 免費嘗試 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,建立一個新的集合,並插入 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 預設是在 Bounded Staleness 一致性層級下進行搜尋。因此,在資料節點和查詢節點的資料同步化之前,已刪除的實體仍然可以檢索。請嘗試在幾秒鐘之後搜尋或查詢已刪除的實體,您會發現它已不在結果中了。

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 的配置,目前的版本只能在集合創建時實現。關於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 的集合中搜尋。
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 套件中的平均搜尋延遲比在CONSISTENCY_STRONG 套件中短 200 毫秒。

如果一致性層級設定為 Strong,刪除的實體是否會立即隱形?答案是肯定的。您仍然可以自行嘗試。

交換

在處理串流資料集時,許多使用者習慣先建立索引並載入資料集,然後才插入資料。在 Milvus 以前的版本中,使用者必須在建立索引後手動載入集合,以索引取代原始資料,既慢又費力。handoff 功能可讓 Milvus 2.0 自動載入已建立索引的片段,取代達到特定索引臨界值的串流資料,大幅提升搜尋效能。

  1. 先建立索引並載入集合,再插入更多的實體。
# 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

繼續閱讀