跳到主要内容

Load 和 Release

对 Collection 执行 Load 操作是在 Collection 中进行 Search 和 Query 的前提条件。本节主要介绍如何 Load 和 Release Collection。

Load Collection

在加载 Collection 时,Zilliz Cloud 会将所有向量列的索引文件和所有标量列的数据加载到QueryNode,从而快速响应搜索和查询请求。在 Collection 加载后插入的数据会自动完成索引和加载。

您可以参考如下代码 Load Collection。

from pymilvus import MilvusClient

client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)

# 7. Load the collection
client.load_collection(
collection_name="customized_setup_1"
)

res = client.get_load_state(
collection_name="customized_setup_1"
)

print(res)

# Output
#
# {
# "state": "<LoadState: Loaded>"
# }

Load 部分字段

为了节约内存资源,您可以选择让 Zilliz Cloud 仅加载参与 Search 和 Query 的部分字段,提升搜索性能。

如下代码中假设名为 customized_setup_1 的 Collection 中有名为 my_idmy_vector 两个字段。

client.load_collection(
collection_name="custom_quick_setup",
# highlight-next-line
load_fields=["my_id", "my_vector"], # Load only the specified fields
skip_load_dynamic_field=True # Skip loading the dynamic field
)

res = client.get_load_state(
collection_name="custom_quick_setup"
)

# Output
#
# {
# "state": "<LoadState: Loaded>"
# }

需要注意的是,当您选择只加载部分字段时,只有在 load_fields 中的字段可以做为过滤条件(filter)和输出字段(Output Fields)使用。请务必在 load_fields 包含主键和至少一个已创建索引的向量字段。

您还可以使用 skip_load_dynamic_field 来决定是否加载 Dynamic Field。Dynamic Field 是一个名为 $meta 的预留 JSON 字段,用于以键值对的形式存放各 Entity 中所有未在 Schema 中定义的字段及其值。在加载 Dynamic Field 时,字段中所有键都会被加载,并可用于过滤和输出。如果 Dynamic Field 中的键无须参与过滤或输出,您可以将 skip_load_dynamic_field 设置为 True

如果在执行本操作后需要 Load 更多字段,请务必先对 Collection 执行 Release 操作,避免因索引结构发生变化而引发系统报错。

Release Collection

由于 Search 和 Query 操作会占用较多的内存资源。为了减少资源消耗,您可以对暂时不需要使用的 Collection 执行 Release 操作,将相关数据从内存中释放出来。

如下示例演示了如何 Release Collection。

# 8. Release the collection
client.release_collection(
collection_name="custom_quick_setup"
)

res = client.get_load_state(
collection_name="custom_quick_setup"
)

print(res)

# Output
#
# {
# "state": "<LoadState: NotLoad>"
# }