跳到主要内容

设置 Collection TTL

数据插入到 collection 后,默认情况下会永久保留。但在某些场景下,您可能希望在一定时间后删除或清理数据。在这种情况下,您可以配置 collection 的生存时间(TTL)属性,以便 Milvus 在 TTL 过期后自动删除数据。

概述

生存时间(TTL)在数据库中通常用于数据仅应在插入或修改后的某个时间段内保持有效或可访问的场景。然后,数据可以被自动删除。

例如,如果您每天摄取数据但只需要保留14天的记录,您可以通过将 collection 的 TTL 设置为 14 × 24 × 3600 = 1209600 秒来配置 Milvus 自动删除超过该时间的任何数据。这确保了 collection 中只保留最近14天的数据。

Milvus collection 中的 TTL 属性指定为以秒为单位的整数。一旦设置,任何超过其 TTL 的数据都将自动从 collection 中删除。

由于删除过程是异步的,数据可能不会在指定的 TTL 过期后立即从搜索结果中删除。相反,可能会有延迟,因为删除依赖于垃圾回收(GC)和压缩过程,这些过程在非确定性的间隔发生。

设置 TTL

您可以在以下情况下设置 TTL 属性:

创建 collection 时设置 TTL

以下代码片段演示了如何在创建 collection 时设置 TTL 属性。

from pymilvus import MilvusClient

# With TTL
client.create_collection(
collection_name="my_collection",
schema=schema,
properties={
"collection.ttl.seconds": 1209600
}
)
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.AlterCollectionReq;
import io.milvus.param.Constant;
import java.util.HashMap;
import java.util.Map;

// With TTL
CreateCollectionReq customizedSetupReq = CreateCollectionReq.builder()
.collectionName("my_collection")
.collectionSchema(schema)
.property(Constant.TTL_SECONDS, "1209600")
.build();
client.createCollection(customizedSetupReq);
const createCollectionReq = {
collection_name: "my_collection",
schema: schema,
properties: {
"collection.ttl.seconds": 1209600
}
}
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("my_collection", schema).
WithProperty(common.CollectionTTLConfigKey, 1209600)) // TTL in seconds
if err != nil {
fmt.Println(err.Error())
// handle error
}
export params='{
"ttlSeconds": 1209600
}'

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_collection\",
\"schema\": $schema,
\"params\": $params
}"

为现有 collection 设置 TTL

以下代码片段演示了如何修改现有 collection 中的 TTL 属性。

client.alter_collection_properties(
collection_name="my_collection",
properties={"collection.ttl.seconds": 1209600}
)
Map<String, String> properties = new HashMap<>();
properties.put("collection.ttl.seconds", "1209600");

AlterCollectionReq alterCollectionReq = AlterCollectionReq.builder()
.collectionName("my_collection")
.properties(properties)
.build();

client.alterCollection(alterCollectionReq);
res = await client.alterCollection({
collection_name: "my_collection",
properties: {
"collection.ttl.seconds": 1209600
}
})
err = client.AlterCollectionProperties(ctx, milvusclient.NewAlterCollectionPropertiesOption("my_collection").
WithProperty(common.CollectionTTLConfigKey, 60))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"my_collection\",
\"properties\": {
\"collection.ttl.seconds\": 1209600
}
}"

删除 TTL 设置

如果您决定无限期保留 collection 中的数据,您可以简单地从该 collection 中删除 TTL 设置。

client.drop_collection_properties(
collection_name="my_collection",
property_keys=["collection.ttl.seconds"]
)
propertyKeys = new String[1]
propertyKeys[0] = "collection.ttl.second"

DropCollectionReq dropCollectionReq = DropCollectionReq.builder()
.collectionName("my_collection")
.propertyKeys(propertyKeys)
.build();

client.dropCollection(dropCollectionReq);
res = await client.dropCollectionProperties({
collection_name: "my_collection",
properties: ["collection.ttl.seconds"]
})
err = client.DropCollectionProperties(ctx, milvusclient.NewDropCollectionPropertiesOption("my_collection", common.CollectionTTLConfigKey))
if err != nil {
fmt.Println(err.Error())
// handle error
}
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/alter_properties" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \""my_collection"\",
\"properties\": {
\"collection.ttl.seconds\": 60
}
}"