Lifecycle management
Time to Live (TTL) is a property of a search index that defines how long data is stored. A search index automatically deletes expired data, which reduces your data storage space and lowers storage costs.
Prerequisites
The OTSClient is initialized. For more information, see Initialize the Tablestore Client.
A data table is created. For more information, see Create a data table.
Notes
To use the lifecycle management feature, you must disable the UpdateRow write operation for the data table. This prevents potential semantic issues.
The TTL for a data table applies at the attribute column level, whereas the TTL for a search index applies to the entire row. If you use UpdateRow operations, the system might delete some field values in the data table but not others, while the entire row in the search index remains. This causes data inconsistency between the data table and the search index.
If your business requires UpdateRow write operations, consider using PutRow overwrite operations instead.
The TTL for a search index can be set to -1 or a positive 32-bit integer in seconds. A value of -1 indicates that the data is stored permanently. The maximum 32-bit integer value is equivalent to approximately 68 years.
The TTL for a search index is independent of the TTL for its data table. The search index TTL must be less than or equal to the data table TTL. To decrease both TTL values, decrease the search index TTL before you decrease the data table TTL.
A search index automatically deletes expired data daily. The cleanup granularity for expired data is one day. You might still be able to query data that has expired but has not yet been deleted. This data is automatically deleted during the next cleanup cycle.
After you update the TTL for a data table and its search index, the system automatically deletes any existing expired data during the next cleanup cycle.
Procedure
Disable the UpdateRow write operation for the data table.
The following example shows how to disable the UpdateRow write operation for the data table.
def disable_table_update(client): # Disable the UpdateRow write operation for the data table. Make sure that no UpdateRow write operations are performed on the data table to avoid affecting services. table_options = TableOptions(time_to_live=None, max_version=None, max_time_deviation=None, allow_update=False) client.update_table('<TABLE_NAME>', table_options, None)Set the lifecycle for the search index.
After you disable the UpdateRow write operation for the data table, you can specify a TTL when you create a search index or for an existing search index.
Specify a TTL when you create a search index
The following example shows how to create a search index that contains two columns: Col_Keyword and Col_Long. Their data types are string (KEYWORD) and integer (LONG), respectively. The lifecycle of the search index is set to 7 days.
def create_index_with_ttl(client): # A field of the KEYWORD type. field_keyword = FieldSchema('Col_Keyword', FieldType.KEYWORD) # A field of the LONG type. field_long = FieldSchema('Col_Long', FieldType.LONG) fields = [field_keyword, field_long] index_meta = SearchIndexMeta(fields, time_to_live=24 * 3600 * 7) client.create_search_index('<TABLE_NAME>', '<SEARCH_INDEX_NAME>', index_meta)Specify a TTL for an existing search index
The following example shows how to set the lifecycle of an existing search index to 7 days.
def update_index_with_ttl(client): index_meta = SearchIndexMeta(fields=None, time_to_live=24 * 3600 * 7) client.update_search_index('<TABLE_NAME>', '<SEARCH_INDEX_NAME>', index_meta)The TTL for a search index is independent of the TTL for its data table. To use the data table TTL, you must also set a TTL for the data table.
The following example shows how to set the lifecycle of the data table to 7 days.
def update_table_ttl(client): table_options = TableOptions(time_to_live=24 * 3600 * 7, max_version=None, max_time_deviation=None) client.update_table('<TABLE_NAME>', table_options, None)
FAQ
Error when modifying the data table lifecycle:
must be greater than or equal to search index ttlReferences
To retrieve a list of all search indexes associated with a data table, you can list the search indexes. For more information, see List search indexes.
To query the description of a search index, including its source table columns and index configuration, you can query the search index description. For more information, see Query search index description.
To add, update, or delete index columns in a search index, you can dynamically modify the schema. For more information, see Dynamically modify a schema.
If a search index is no longer needed, you can delete it. For more information, see Delete a search index.