コレクションTTLの設定

一度データがコレクションに挿入されると、デフォルトではそのまま残ります。しかし、シナリオによっては、一定期間後にデータを削除またはクリーンアップしたい場合があります。そのような場合、コレクションのTTL(Time-to-Live)プロパティを設定することで、TTLが切れるとMilvusは自動的にデータを削除します。

概要

Time-to-Live (TTL)は、挿入または変更後、一定期間だけデータが有効またはアクセス可能である必要がある場合にデータベースで一般的に使用されます。その後、データは自動的に削除されます。

例えば、毎日データを取り込むが、14日間だけレコードを保持する必要がある場合、コレクションのTTLを14×24×3600=1209600秒に設定することで、それより古いデータを自動的に削除するようにMilvusを設定することができます。これにより、最新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
    }
}"

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
フィードバック

このページは役に立ちましたか ?