文档

生命周期管理

更新时间:

数据生命周期(Time To Live,简称TTL)是多元索引的一个属性,即数据的保存时间。多元索引会自动清理超过保存时间的数据,减少用户的数据存储空间,降低存储成本。

前提条件

注意事项

  • 使用生命周期管理功能,必须禁用数据表的UpdateRow更新写入功能,避免一些语义上的问题:

    由于数据表TTL是属性列级别生效的,而多元索引TTL是整行生效的,如果存在UpdateRow写入操作,当系统清理数据表中数据时,数据表中部分字段值已删除而部分字段值未删除,但是多元索引中整行数据均未删除,则会造成数据表和多元索引中的数据不一致。

    如果业务有UpdateRow更新写入操作,请查看是否能改为PutRow覆盖写入操作。

  • 多元索引的TTL取值范围为-1或者int32的正整数(单位为秒),其中-1表示永久存储,int32最大值换算为年大约为68年。

  • 多元索引的TTL和数据表的TTL是独立的,多元索引的TTL值必须小于或等于数据表的TTL值。当需要同时调小多元索引TTL和数据表TTL时,请先调整多元索引TTL,再调整数据表TTL。

  • 多元索引每天会自动清理已过期的数据,过期数据的清理粒度为“天”,因此您仍然可以查询到某一时刻已过期但是还未及时清理的数据,多元索引会在下一次清理过期数据时自动清理这些过期数据。

  • 数据表和多元索引的TTL更新后,系统会在下一次清理过期数据时自动清理数据表和多元索引中的存量过期数据。

使用流程

  1. 禁用数据表UpdateRow更新写入操作。

    以下示例用于禁用数据表的UpdateRow更新写入操作。

    func disableTableUpdate(client *tablestore.TableStoreClient) {
        request := &tablestore.UpdateTableRequest{
           TableName: "TableName",
           TableOption: &tablestore.TableOption{
              TimeToAlive:               -1,    // 数据表生命周期保持默认,默认值为-1。
              MaxVersion:                1,     // 最大版本数保持默认,默认值为1。
              DeviationCellVersionInSec: 86400, // 有效版本偏差保持默认,默认值为86400。单位为秒。
              // 禁用数据表UpdateRow更新写入操作,请确保数据表无UpdateRow写入操作,避免影响业务。注意多元索引存在TTL时不能修改为允许更新。
              AllowUpdate: proto.Bool(false),
          },
       }
        resp, err := client.UpdateTable(request)
        if err != nil {
           fmt.Println("error :", err)
           return
       }
        fmt.Println("UpdateTable finished, requestId:", resp.ResponseInfo.RequestId)
    }
  2. 设置多元索引生命周期。

    禁用数据表UpdateRow更新写入操作后,您可以在创建多元索引时指定TTL或者为已有多元索引指定TTL。

    创建多元索引时指定TTL

    以下示例用于创建一个多元索引,多元索引包含col1和col2两列,类型分别设置为字符串(String)和整型(Long)。同时指定多元索引生命周期为7天。

    func createIndexWithTTL(client *tablestore.TableStoreClient) {
        request := &tablestore.CreateSearchIndexRequest{}
        request.TableName = "<TABLE_NAME>"
        request.IndexName = "<SEARCH_INDEX_NAME>"
        schemas := []*tablestore.FieldSchema{}
        field1 := &tablestore.FieldSchema{
            FieldName:        proto.String("col1"),         //设置字段名,使用proto.String用于获取字符串指针。
            FieldType:        tablestore.FieldType_KEYWORD, //设置字段类型。
            Index:            proto.Bool(true),             //设置开启索引。
            EnableSortAndAgg: proto.Bool(true),             //设置开启排序与统计聚合功能。
        }
        field2 := &tablestore.FieldSchema{
            FieldName:        proto.String("col2"),
            FieldType:        tablestore.FieldType_LONG,
            Index:            proto.Bool(true),
            EnableSortAndAgg: proto.Bool(true),
        }
        schemas = append(schemas, field1, field2)
        request.IndexSchema = &tablestore.IndexSchema{
            FieldSchemas: schemas, //设置多元索引包含的字段。
        }
        request.TimeToLive = proto.Int32(3600 * 24 * 7) // 设置多元索引TTL为7天过期。
        resp, err := client.CreateSearchIndex(request)
        if err != nil {
           fmt.Println("error :", err)
           return
       }
        fmt.Println("createIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId)
    }

    为已有多元索引指定TTL

    以下示例用于指定已有多元索引的生命周期为7天。

    func updateIndexWithTTL(client *tablestore.TableStoreClient) {
        request := &tablestore.UpdateSearchIndexRequest{}
        request.TableName = "TableName"
        request.IndexName = "IndexName"
        request.TimeToLive = proto.Int32(3600 * 24 * 7) // 设置多元索引TTL为7天过期。
        resp, err := client.UpdateSearchIndex(request)
        if err != nil {
           fmt.Println("error :", err)
           return
       }
        fmt.Println("updateIndexWithTTL finished, requestId:", resp.ResponseInfo.RequestId)
    }
  3. 多元索引的TTL和数据表的TTL是独立的。如果需要使用数据表TTL,请为数据表设置TTL。

    以下示例用于指定数据表的生命周期为7天。

    // 设置数据表的TTL为7天过期。
    func updateTableTTL(client *tablestore.TableStoreClient) {
        request := &tablestore.UpdateTableRequest{
            TableName: "TableName",
            TableOption: &tablestore.TableOption{
                TimeToAlive:               3600 * 24 * 7, // 设置数据表的TTL为7天过期,请确保数据表的TTL大于等于多元索引TTL。
                MaxVersion:                1,             // 最大版本数保持默认,默认值为1。
                DeviationCellVersionInSec: 86400,         // 有效版本偏差保持默认, 默认值为86400。单位为秒。
                // 禁用数据表Update更新写入操作,请确保数据表无Update写入操作,避免影响业务。注意多元索引存在TTL时不能修改为允许更新。
                AllowUpdate: proto.Bool(false),
            },
        }
        resp, err := client.UpdateTable(request)
        if err != nil {
            fmt.Println("error :", err)
            return
        }
        fmt.Println("UpdateTable finished, requestId:", resp.ResponseInfo.RequestId)
    }

常见问题

修改数据表生命周期时报错[table ttl] must be bigger than or equal search index ttl

相关文档

  • 如果要获取某个数据表关联的所有多元索引的列表信息,您可以使用列出多元索引列表功能实现。具体操作,请参见列出多元索引列表

  • 如果要查询多元索引的描述信息,包括多元索引的字段信息和索引配置等,您可以使用查询多元索引描述信息功能实现。具体操作,请参见查询多元索引描述信息

  • 如果要在多元索引中新增、更新或者删除索引列,您可以使用动态修改schema功能实现。具体操作,请参见动态修改schema

  • 如果不再需要使用多元索引,您可以删除多元索引。具体操作,请参见删除多元索引