TTL usage

更新时间:
复制 MD 格式

This topic describes the Time to Live (TTL) feature, which automatically deletes expired data in PolarDB for MySQL.

Version restrictions

PolarDB for MySQL supports the following database engine versions:

  • MySQL 8.0.1 with revision version 8.0.1.1.49.2 or later.

  • MySQL 8.0.2 with revision version 8.0.2.2.29.2 or later.

For more information, see Kernel version description.

Notes

  • You cannot set TTL properties on temporary tables, including local temporary tables and global temporary tables.

  • Tables with TTL properties do not support features such as global secondary index (GSI) and partitioned table.

  • A table with TTL properties cannot be referenced by other tables as a primary table in a foreign key constraint.

  • A table with TTL properties cannot contain triggers.

  • Expired data may not be immediately deleted. The deletion time of expired data depends on the scheduling cycle of the background cleanup task.

  • When restoring from a database table or a backup, you need to set loose_innodb_enable_ttl_purge to OFF to disable the feature that purges expired data based on TTL. This is because all data may be expired after the restoration.

  • A column with TTL properties must be of the TIMESTAMP or DATETIME type.

  • Data deleted by the TTL feature does not generate binary logging records. Therefore, if you use binary logging for data synchronization and a table in the source database has the TTL feature enabled, key value conflicts may occur in the secondary database.

Syntax

You can use the CREATE TABLE or ALTER TABLE statement to configure the TTL feature for a table.

Create a table with TTL properties

You can use one of the following methods to create a table with TTL properties:

A table named t1 is created, where created_at is the time to live (TTL) column that stores the creation time of the data. You can set a TTL value based on the created_at column to determine when the data is deleted.

  • Set the time to live of created_at to 100 seconds.

    CREATE TABLE `t1` (
      `a` INT PRIMARY KEY,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      KEY idx_created_at (`created_at`)
    )ENGINE=InnoDB  TTL='created_at@100';
    Note

    TTL='created_at@100' sets the time to live for rows in the table to 100 seconds. Rows older than this duration are marked as expired and will be deleted later.

  • Set the time to live of created_at to 3 hours.

    CREATE TABLE `t1` (
      `a` INT PRIMARY KEY,
      `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      KEY idx_created_at (`created_at`)
    )ENGINE=InnoDB  TTL='created_at' + INTERVAL 3 HOUR;
    Note
    • The expression TTL ='created_at'+ INTERVAL 3 HOUR sets the time to live for rows in the table to 3 hours. Expired data is automatically deleted.

    • The TTL setting supports multiple time units, such as YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, and SECOND. This flexibility lets you select an appropriate time unit for efficient data management based on your requirements.

Modify the TTL properties of a table

You can use one of the following methods to modify the TTL properties of a table:

The following examples modify the time to live of the created_at column in the t1 table.

  • Modify the time to live of created_at to 10,000 seconds.

    ALTER TABLE `t1` TTL='created_at@10000';
    Note

    This setting ensures that data is automatically deleted 10,000 seconds after its creation time, which is based on the value in the created_at field.

  • Modify the time to live of created_at to 3 days.

    ALTER TABLE `t1` TTL='created_at' + INTERVAL 3 DAY;
    Note

    This sets the data expiration time to 3 days from the value in the created_at column. Data older than 3 days is automatically deleted.

Clear the TTL properties of a table

ALTER TABLE t1 TTL = '';

Query the TTL properties of a table

SHOW CREATE TABLE `t1` FULL;

CREATE TABLE `t1` (  
`a` INT PRIMARY KEY,  
`created_at` TIMESTAMP DEFAULT 
CURRENT_TIMESTAMP,  
 KEY idx_created_at (`created_at`)
)ENGINE=InnoDB  TTL='created_at@259200';
Note

You can use this statement to query whether a table has TTL properties.

Parameter description

The following global parameters control the cleanup of expired data:

Note

You can modify the following global parameters in the PolarDB console to control the cleanup of expired data.

Parameter name

Description

loose_innodb_enable_ttl_purge

Specifies whether to enable the cleanup of TTL-expired data.

Valid values:

  • ON: enables the cleanup of TTL-expired data.

  • OFF (default): disables the cleanup of TTL-expired data.

Note

You need to go to the console to enable the TTL expired data cleanup feature.

loose_innodb_ttl_min_interval

The minimum allowed time when you set the data expiration time. The default value is 100. The default unit is second.

loose_innodb_ttl_purge_thread

The number of threads for cleaning up TTL-expired data. After you modify this parameter, you must reset the innodb_enable_ttl_purge parameter for the modification to take effect. The default value is 4.

loose_innodb_ttl_cluster_index_purge_batch_size

If the specified TTL column does not have an index, the primary key is scanned for TTL-expired data. The number of rows scanned from the primary key at a time is 10,000 by default.

loose_innodb_ttl_index_purge_batch_size

If the specified TTL column is indexed, this index is scanned for TTL-expired data. The number of rows scanned from this index at a time is 500 by default.

loose_innodb_ttl_purge_start_hour

The start time of the TTL cleanup task. The default value is 0. The value ranges from 0 to 23. The value cannot be greater than innodb_ttl_purge_end_hour.

loose_innodb_ttl_purge_end_hour

The end time of the TTL cleanup task. The default value is 0. The value ranges from 0 to 23. The value cannot be less than innodb_ttl_purge_start_hour.

loose_innodb_ttl_finished_job_expired_days

Controls the expiration time of data in the mysql. ttl_job_history table. The default value is 90. The value ranges from 1 to 365. The default unit is day.

Observe TTL

The system periodically collects TTL runtime information. You can view the execution status of TTL cleanup tasks in the mysql.ttl_job_history system table. The following table describes the fields in this table:

Column name

Description

job_id

The ID of the TTL cleanup task, which is typically a timestamp in milliseconds.

table_name

The name of the table corresponding to the task executed by this TTL job.

state

The execution status of the TTL task, including pending, executing, and completed.

start_time

The start time of the task.

finished_time

The completion time of the task.

expire_time

The expiration time of the data cleaned up by this TTL task.

scan_cost

The time consumed to scan this batch.

purge_cost

The time consumed to clean up this batch.

purge_rows

The number of data rows cleaned up by this TTL task.