Archive tables store archived data. A one-to-one relationship exists between TTL tables and archive tables. This means that you can create only one archive table for each TTL table. This topic describes the statements concerning archive tables.
Prerequisites
A PolarDB-X instance that meets one of the following requirements is created:
The engine version of the instance is MySQL 5.7 and the instance version is polardb-2.4.0_5.4.19-20240927_xcluster5.4.19-20240920 or later.
The engine version of the instance is MySQL 8.0 and the instance version is polardb-2.4.0_5.4.19-20240927_xcluster8.4.19-20240924 or later.
NoteFor more information about version number, see Release notes.
For more information about how to check the instance version, see View and update the version of an instance.
A partitioned table that belongs to a database in AUTO mode and is not partitioned by using the
LOCAL PARTITION BYclause is used.
Terms
Term | Description |
Cold data | The data that is infrequently updated and rarely queried in specific database tables in an instance. |
Online table | The business data table that processes online traffic. |
Archive table | The table that stores archived data in storage mediums such as OSS that is featured by high compression and cost-effectiveness . |
TTL | Time To Live. TTL in adatabase context refers to the retention period of data in a database. The data is automatically deleted after it reaches the time limit. |
TTL table | The online tables that contain TTL definitions. For more information, see Definitions of TTL tables. |
TTL column | The time column in a TTL definition that is used for data validation calculation. For more information, see Definitions of TTL tables. |
Create an archive table
After you create a TTL table by using the CREATE TABLE or ALTER TABLE statement, if you want to archive historical data of the TTL table, you must create an archive table for the TTL table. Archive tables store data in TTL tables to OSS, which is featured by high compression and cost-effectiveness. This reduces storage costs.
Syntax
CREATE TABLE #archiveTableName
LIKE #ttlTableName
ENGINE = 'Columnar' ARCHIVE_MODE = 'TTL'The
ENGINEparameter must be set to'Columnar'.The
ARCHIVE_MODEparameter must be set to'TLL'.The source table for the
CREATE TABLE LIKEstatement must be a table with TTL defined. In addition, you must setENGINE = 'Columnar'andARCHIVE_MODE = 'TTL'.Before you create an archive table, we recommend that you set
TTL_ENABLEtoOFFto disable automatic cleanup tasks. For more information, see Definitions of TTL tables.
Example
For example, the TTL table is my_ttl_tbl. If you want to create an archive table named my_arc_tbl for the TTL table, you can execute the following statement:
/*+TDDL:cmd_extra(ENABLE_ASYNC_DDL=true, PURE_ASYNC_DDL_MODE=true)*/
CREATE TABLE `my_arc_tbl`
LIKE `my_ttl_tbl`
ENGINE = 'Columnar' ARCHIVE_MODE = 'TTL';The
CREATE TABLE LIKEstatement that is used to create an archive table triggers the archiving process for all data of the TTL table. The process essentially creates a CCI and uploads the CCI to OSS. It is usually time-consuming.The hint
/*+TDDL:cmd_extra(ENABLE_ASYNC_DDL=true, PURE_ASYNC_DDL_MODE=true)*/in the preceding statement allows asynchronous background execution of the DDL statement to create the archive table. When the archive table is created, the original TTL table can still be read and written.For more information about cold data archiving and cleanup of PolarDB-X TTL tables, see Overview.
Partition an archive table
Partitioning scheme
By default, an archive table is range-partitioned based on the
ttl_columnvalue inTTL_EXPRof the TTL definition. The range-partitioning interval is thettl_interval_definitionvalue inTTL_EXPRof the TTL definition.For example, in a TTL table, the TTL column is
date_field. The data TTL interval is defined by month, and the data of the last three months is retained. The range-partitioning interval is one month, and the data of one month corresponds to one range partition. Example:TTL_EXPR = `date_field` EXPIRE AFTER 3 MONTH TIMEZONE '+08:00'When you create an archive table, the number of automatically generated range partitions and their definitions are determined by the two parameters.
ARCHIVE_TABLE_PRE_ALLOCATE: determines the maximum number of partitions to be created for the future when you create an archive table.ARCHIVE_TABLE_POST_ALLOCATE: determines the maximum number of partitions that can be created for the past when you create an archive table.
For example, the current time is 2024-10-02, the
ARCHIVE_TABLE_PRE_ALLOCATEvalue is 4 (four partitions are created for the future), and theARCHIVE_TABLE_POST_ALLOCATEvalue is 3 (three partitions are created for the past). Therefore, the following partitioning scheme can be used for the archive table:PARTITION BY RANGE COLUMNS(`date_field`) ( PARTITION `pstart` VALUES LESS THAN ('1970-01-02 00:00:00'), PARTITION `p202407` VALUES LESS THAN ('2024-08-01 00:00:00'), PARTITION `p202408` VALUES LESS THAN ('2024-09-01 00:00:00'), PARTITION `p202409` VALUES LESS THAN ('2024-10-01 00:00:00'), PARTITION 'p202410' VALUES LESS THAN ('2024-11-01 00:00:00')/*The partition for the current time 2024-10-02*/, PARTITION `p202411` VALUES LESS THAN ('2024-12-01 00:00:00'), PARTITION `p202412` VALUES LESS THAN ('2025-01-01 00:00:00'), PARTITION `p202501` VALUES LESS THAN ('2025-02-01 00:00:00'), PARTITION `pm` VALUES LESS THAN (MAXVALUE) )
Modify partition parameters
If you want to modify the ARCHIVE_TABLE_PRE_ALLOCATE or ARCHIVE_TABLE_POST_ALLOCATE value to create more partitions for the future and for the past, you can execute the following statement:
/**
*Create 12 partitions for the future
*Create 24 partitions for the past
*/
ALTER TABLE `my_ttl_tbl`
MODIFY TTL
SET
ARCHIVE_TABLE_PRE_ALLOCATE=12,
ARCHIVE_TABLE_POST_ALLOCATE=24;Query the archive table
Query the archive table definitions
Creating an archive table is essentially a process of creating a CCI. When the archive table is created, the CCI (named in the format of arctmp_Archive table name) is added to the original TTL table. The CCI dedicated to the archive table. For more information, see Overview.
Therefore, you can execute the SHOW CREATE TABLE statement on the TTL table to query the CCI definitions and partitions of the archive table.
For example, if the TTL table is my_ttl_tbl and the archive table is my_arc_tbl, the CCI is arctmp_my_arc_tbl. The archive table my_arc_tbl is essentially a query view that explicitly specifies the CCI arctmp_my_arc_tbl.
Query all tables.
show tables;+------------------+ | Tables_in_ttldb | +------------------+ | my_ttl_tbl | | my_arc_tbl | +------------------+ 2 rows in set (0.00 sec)Execute the
SHOW CREATE TABLEstatement on the TTL table.show create table my_ttl_tbl\G;*************************** 1. row *************************** Table: my_ttl_tbl Create Table: CREATE TABLE `my_ttl_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT, `date_field` datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), CLUSTERED COLUMNAR INDEX `arctmp_my_arc_tbl` (`date_field`) PARTITION BY RANGE COLUMNS(`date_field`) (PARTITION `pstart` VALUES LESS THAN ('1970-01-02 00:00:00') ENGINE = Columnar, PARTITION `p201903` VALUES LESS THAN ('2019-04-01 00:00:00') ENGINE = Columnar, ... PARTITION `p202506` VALUES LESS THAN ('2025-07-01 00:00:00') ENGINE = Columnar, PARTITION `p202507` VALUES LESS THAN ('2025-08-01 00:00:00') ENGINE = Columnar) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 TTL = TTL_DEFINITION( TTL_ENABLE = 'OFF', TTL_EXPR = `date_field` EXPIRE AFTER 2 MONTH TIMEZONE '+08:00', TTL_JOB = CRON '0 0 1 * * ? *' TIMEZONE '+08:00', ARCHIVE_TYPE = 'COLUMNAR', ARCHIVE_TABLE_NAME = 'my_arc_tbl', ARCHIVE_TABLE_PRE_ALLOCATE = 12, ARCHIVE_TABLE_POST_ALLOCATE = 64 ) PARTITION BY KEY(`id`) PARTITIONS 8 1 row in set (0.00 sec)Execute the
SHOW CREATE TABLEstatement on the archive table.show create table my_arc_tbl\G;*************************** 1. row *************************** View: my_arc_tbl Create View: CREATE VIEW `my_arc_tbl` AS SELECT `my_ttl_tbl`.`id`, `my_ttl_tbl`.`date_field` FROM `ttldb`.`my_ttl_tbl` FORCE INDEX(`ARCTMP_MY_ARC_TBL`) character_set_client: utf8 collation_connection: utf8_general_ci 1 row in set (0.00 sec)
Query the archive table data
Using the query view of the archive table my_arc_tbl, you can easily query all archived historical data in the TTL table. Example:
select count(1) from my_arc_tbl;Querying data in an archive table pulls data from the OSS bucket to the local disk. This operation consumes a lot of I/O resources. If the local OSS cache is not hit, the query may be very slow. Therefore, we recommend that you do not perform the operation frequently.
Delete the archive table
A one-to-one relationship exists between TTL tables and archive tables. This means that you can create only one archive table for each TTL table. Take note of the following points when you delete the archive table:
If a TTL table is deleted, its archive table is also deleted.
If an archive table is deleted, its TTL table is not deleted. However, the cleanup job simply clears data, and the cleared data is no longer archived.
By default, the DROP TABLE operation is restricted on archive tables to prevent accidentally deleting archived historical data.
For example, if the TTL table my_ttl_tbl has the archive table my_arc_tbl, the following error is returned when you delete the my_arc_tbl table.
Query all tables.
show tables;+------------------+ | Tables_in_ttldb | +------------------+ | my_ttl_tbl | | my_arc_tbl | +------------------+ 2 rows in set (0.00 sec)Execute the
DROP TABLEstatement on the archive table.DROP TABLE my_arc_tbl;The following error message is returned:
ERROR 4601 (HY000): [189b8f1eb7cxxxxx][10.57.xxx.xxx:xxxx][ttldb]ERR-CODE: [TDDL-4601][ERR_EXECUTOR] Forbid to drop the ttl-defined table `ttldb`.`my_arc_tbl` with archive cci, please use the hint /*TDDL:cmd_extra(TTL_FORBID_DROP_TTL_TBL_WITH_ARC_CCI=false)*/ to drop this table
If you really want to delete the archive table, you can execute the DROP TABLE statement included in the error message. For example, you can execute the following statement to delete the archive table my_arc_tbl:
/*+TDDL:CMD_EXTRA(TTL_FORBID_DROP_TTL_TBL_WITH_ARC_CCI=false)*/
DROP TABLE my_arc_tbl;The /*+TDDL:CMD_EXTRA(TTL_FORBID_DROP_TTL_TBL_WITH_ARC_CCI=false)*/ hint before DROP TABLE is for removing the deletion restriction.
Restrictions on DDL statements
A one-to-one relationship exists between TTL tables and archive tables. This means that you can create only one archive table for each TTL table. Therefore, after an archive table is created for a TTL table, DDL statements are subject to the following restrictions:
By default, you cannot execute
CREATE TABLE LIKEstatement that do not containENGINE = 'Columnar' ARCHIVE_MODE = 'TTL'. To execute the precedingCREATE TABLE LIKEstatement on the TTL table, you must add the hint/*+TDDL:CMD_EXTRA(ALLOW_CREATE_TABLE_LIKE_IGNORE_ARCHIVE_CCI=true)*/to automatically remove the archive table definitions.Example:
/*+TDDL:CMD_EXTRA(ALLOW_CREATE_TABLE_LIKE_IGNORE_ARCHIVE_CCI=true)*/ CREATE TABLE my_tbl2 like my_ttl_tbl;By default, you cannot execute the DDL statements for modifying column names or column types of TTL columns,
MODIFY COLUMNS,CHANGE COLUMNS, orRENAME COLUMNS. This is because modifying TTL columns may trigger repartitioning of the archive table.