컬렉션 TTL 설정
데이터가 컬렉션에 삽입되면 기본적으로 해당 데이터는 그대로 유지됩니다. 그러나 일부 시나리오에서는 일정 기간이 지나면 데이터를 제거하거나 정리하고 싶을 수 있습니다. 이러한 경우, 컬렉션의 TTL(Time-to-Live) 속성을 구성하여 TTL이 만료되면 Milvus가 자동으로 데이터를 삭제하도록 할 수 있습니다.
개요
TTL(Time-to-Live)은 일반적으로 데이터베이스에서 데이터를 삽입하거나 수정한 후 일정 기간 동안만 유효하거나 액세스할 수 있어야 하는 시나리오에 사용됩니다. 그러면 데이터가 자동으로 제거될 수 있습니다.
예를 들어, 매일 데이터를 수집하지만 14일 동안만 기록을 보관해야 하는 경우, 컬렉션의 TTL을 14 × 24 × 3600 = 1209600초로 설정하여 그보다 오래된 데이터를 자동으로 제거하도록 Milvus를 구성할 수 있습니다. 이렇게 하면 가장 최근 14일 분량의 데이터만 컬렉션에 남게 됩니다.
Milvus 컬렉션의 TTL 속성은 초 단위의 정수로 지정됩니다. 설정이 완료되면 해당 TTL을 초과하는 모든 데이터는 컬렉션에서 자동으로 삭제됩니다.
삭제 프로세스는 비동기식이기 때문에 지정된 TTL이 경과한 후 데이터가 검색 결과에서 정확히 제거되지 않을 수 있습니다. 대신, 비결정적 간격으로 발생하는 가비지 컬렉션(GC) 및 압축 프로세스에 따라 제거가 달라지므로 지연이 발생할 수 있습니다.
TTL 설정
다음과 같은 경우 TTL 속성을 설정할 수 있습니다.
컬렉션을 만들 때 TTL 설정하기
다음 코드 스니펫은 컬렉션을 만들 때 TTL 속성을 설정하는 방법을 보여줍니다.
from pymilvus import MilvusClient
# With TTL
client.create_collection(
collection_name="my_collection",
schema=schema,
# highlight-start
properties={
"collection.ttl.seconds": 1209600
}
# highlight-end
)
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)
// highlight-next-line
.property(Constant.TTL_SECONDS, "1209600")
.build();
client.createCollection(customizedSetupReq);
const createCollectionReq = {
collection_name: "my_collection",
schema: schema,
// highlight-start
properties: {
"collection.ttl.seconds": 1209600
}
// highlight-end
}
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
}"
기존 컬렉션의 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" \
-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" \
-d "{
\"collectionName\": \""my_collection"\",
\"properties\": {
\"collection.ttl.seconds\": 60
}
}"