Time to live (TTL) is an attribute of search indexes that specifies the retention period of data in search indexes. When data in a search index is retained for a period of time that exceeds the TTL value, Tablestore automatically deletes the data from the search index to free up storage space and reduce costs.

Usage notes

  • To use the TTL feature of a search index, you must prohibit the UpdateRow operation on the data table for which the search index is created due to the following reasons:

    The TTL feature of data tables takes effect on attribute columns, and the TTL feature of search indexes takes effect on the entire rows. If the UpdateRow operation is performed on a data table, when the system clears data in the data table, the values of some fields are deleted and the values of some fields are retained in the data table. However, the entire rows in the search index that is created for the data table are not deleted. As a result, data in the data table and search index is inconsistent.

    If the UpdateRow operation is required, check whether the UpdateRow operation can be changed to the PutRow operation.

  • The TTL value of search indexes can be -1 or a positive int32 in seconds. The value of -1 indicates that data in the search index never expires and the maximum int32 value is equivalent to approximately 68 years.
  • The TTL value of a search index is independent of and must be smaller than or equal to the TTL value of the data table for which the search index is created. If you need to change TTL values of search indexes and data tables for which the search indexes are created to smaller values, you must change the TTL values of the search indexes before you change the TTL values of the data tables.
  • Tablestore automatically deletes expired data from search indexes every day. In some cases, you can still query expired data in search indexes. Tablestore automatically deletes the expired data in the next cycle.
  • After you change the TTL values of data tables and search indexes, the system automatically deletes legacy expired data from the data tables and search indexes in the next cycle.

Procedure

You can configure the TTL of a search index by using the Tablestore console or Tablestore SDKs. To use the TTL feature of a search index, you must prohibit the UpdateRow operation on the data table for which the search index is created.

Use the Tablestore console

  1. Prohibit the UpdateRow operation on a data table.
    1. On the Basic information tab of the data table, click Modify Attributes.
    2. In the Modify Attributes dialog box, select No for Allow Updates and click OK. fig_tableupdate
  2. Specify the TTL for a search index.

    After the UpdateRow operation on a data table is prohibited, you can specify the TTL of a search index when you create the search index or modify the TTL of existing search indexes.

    • Specify the TTL when you create a search index
      1. On the Indexes tab of the data table for which you want to create a search index and on which the UpdateRow operation is prohibited, click Create Search Index.
      2. In the Create Index dialog box, configure the Index Name, Time to Live, and Schema Generation Type parameters, and click OK. fig_ttlcreate
    • Modify the TTL for an existing search index
      1. On the Indexes tab of the data table on which the UpdateRow operation is prohibited, find the search index for which you want to modify the TTL and click Index Details in the Actions column. fig_indexdetailsenter
      2. In the Index Details dialog box, click the fig_indexdetail icon, modify the value of the Time to Live parameter, and then click Modify. fig_modifyttl
      3. Click OK.
  3. The TTL of a data table is independent of the TTL of the search index that is created for the data table. If you want to use the TTL of a data table, configure the TTL for the data table.
    1. In the Description section of the Basic Information tab, click Modify Attributes.
    2. In the Modify Attributes dialog box, configure the Time to Live parameter based on your business requirements, and click OK. fig_ttltablemodify

Use Tablestore SDKs

  1. Prohibit the UpdateRow operation on a data table.
    public void disableTableUpdate(SyncClient client) {
        UpdateTableRequest updateTableRequest = new UpdateTableRequest(tableName);
        TableOptions options = new TableOptions();
        // Prohibit the UpdateRow operation on a data table to prevent impacts on your business. 
        options.setAllowUpdate(false);
        updateTableRequest.setTableOptionsForUpdate(options);
        client.updateTable(updateTableRequest);
    }
  2. Specify the TTL for search indexes.

    After the UpdateRow operation on a data table is prohibited, you can specify the TTL of a search index when you create the search index or modify the TTL of existing search indexes.

    • Specify the TTL when you create a search index
      // Use Tablestore SDK for Java V5.12.0 or later to create a search index. 
      public void createIndexWithTTL(SyncClient client) {
          int days = 7;
          CreateSearchIndexRequest createRequest = new CreateSearchIndexRequest();
          createRequest.setTableName(tableName);
          createRequest.setIndexName(indexName);
          createRequest.setIndexSchema(indexSchema);
          // Specify the TTL for the search index. 
          createRequest.setTimeToLiveInDays(days);
          client.createSearchIndex(createRequest);
      }
    • Modify the TTL for an existing search index
      // Use Tablestore SDK for Java V5.12.0 or later. 
      public void updateIndexWithTTL(SyncClient client) {
          int days = 7;
          UpdateSearchIndexRequest updateSearchIndexRequest = new UpdateSearchIndexRequest(tableName, indexName);
          // Modify the TTL for the search index. 
          updateSearchIndexRequest.setTimeToLiveInDays(days);
          client.updateSearchIndex(updateSearchIndexRequest);
      }
  3. The TTL of a data table is independent of the TTL of the search index that is created for the data table. If you want to use the TTL of a data table, configure the TTL for the data table.
    public void updateTableTTL(SyncClient client) {
        int days = 7;
        UpdateTableRequest updateTableRequest = new UpdateTableRequest(tableName);
        TableOptions options = new TableOptions();
        options.setTimeToLiveInDays(days);
        updateTableRequest.setTableOptionsForUpdate(options);
        client.updateTable(updateTableRequest);
    }