設定集合 TTL

一旦資料插入到集合中,預設會保留在集合中。然而,在某些情況下,您可能想要在某段時間後移除或清理資料。在這種情況下,您可以設定資料集的 Time-to-Live (TTL) 屬性,讓 Milvus 在 TTL 過期後自動刪除資料。

概述

Time-to-Live (TTL) 通常用於資料庫中,在插入或修改資料後,資料只能維持有效或可存取一段時間。之後,資料就會自動移除。

舉例來說,如果您每天擷取資料,但只需要保留 14 天的記錄,您可以設定 Milvus,將資料集的 TTL 設定為14 × 24 × 3600 = 1209600秒,以自動移除任何超過此期限的資料。這可確保只有最近 14 天的資料保留在資料集中。

過期的實體不會出現在任何搜尋或查詢結果中。不過,它們可能會留在儲存中,直到隨後的資料壓縮為止,而資料壓縮應該在未來 24 小時內進行。

您可以在 Milvus 配置檔中設定dataCoord.compaction.expiry.tolerance 配置項目,以控制何時觸發資料壓縮。

此配置項的預設值為-1 ,表示現有的資料壓縮間隔適用。但是,當您將其值變更為正整數,如12 時,資料壓縮將在任何實體過期後的指定小時數觸發。

Milvus 集合中的 TTL 屬性指定為以秒為單位的整數。一旦設定,任何超過其 TTL 的資料都會自動從資料集中刪除。

由於刪除過程是非同步的,資料可能不會在指定的 TTL 過後才從搜尋結果中移除。相反,可能會有延遲,因為刪除取決於垃圾收集 (GC) 和壓縮過程,而這些過程會以非確定的間隔發生。

設定 TTL

您可以在以下情況設定 TTL 屬性

建立集合時設定 TTL

以下程式碼片段示範如何在建立集合時設定 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" \
--header "Request-Timeout: 10" \
-d "{
    \"collectionName\": \"my_collection\",
    \"schema\": $schema,
    \"params\": $params
}"

設定現有集合的 TTL

以下程式碼片段示範如何在現有的集合中變更 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" \
--header "Request-Timeout: 10" \
-d "{
    \"collectionName\": \"my_collection\",
    \"properties\": {
        \"collection.ttl.seconds\": 1209600
    }
}"

刪除 TTL 設定

如果您決定無限期地保留集合中的資料,您可以簡單地從該集合中刪除 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" \
--header "Request-Timeout: 10" \
-d "{
    \"collectionName\": \""my_collection"\",
    \"properties\": {
        \"collection.ttl.seconds\": 60
    }
}"

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?