This topic describes how to archive cold data in CSV or ORC format and how to restore data from an OSS table to PolarStore.
Applicability
You must first enable cold data archiving. This feature requires one of the following cluster revisions:
-
Archiving to CSV format
-
For Cluster Edition clusters:
-
Archive a standard table:
-
MySQL 8.0.1, revision 8.0.1.1.47 or later.
-
MySQL 8.0.2, revision 8.0.2.2.10 or later.
-
-
Archive a partitioned table:
-
MySQL 8.0.2, revision 8.0.2.2.34.1 or later.
-
-
-
For Multi-master Cluster (Limitless) Edition clusters:
-
MySQL 8.0.1, revision 8.0.1.0.13 or later.
-
-
-
Archiving to ORC format
-
For Cluster Edition clusters:
-
Archive a standard table: MySQL 8.0.2, revision 8.0.2.2.30 or later.
-
Archive a partitioned table: MySQL 8.0.2, revision 8.0.2.2.34.1 or later.
-
-
For Multi-master Cluster (Limitless) Edition clusters: MySQL 8.0.2, revision 8.0.2.2.30 or later.
-
When you manually archive cold data on a cluster of one of the following versions, the operation is not recorded in binary logs.
PolarDB for MySQL 8.0.1 with revision 8.0.1.1.33 or later.
PolarDB for MySQL 8.0.2 with revision 8.0.2.2.11.1 or later.
Procedures
Archive a standard table
Cold data archiving is a table-level operation. After you archive a table, it becomes a read-only archived table. The engine of the archived table is the OSS engine, and its data files are stored in Object Storage Service (OSS). After the archiving operation is complete, PolarDB releases the space that the original table occupied in PolarStore.
Syntax
CSV format
Format 1:
ALTER TABLE table_name ENGINE = CSV CONNECTION = 'default_oss_server';Format 2: Your cluster must meet one of the following revision requirements.
Cluster Edition:
MySQL 8.0.1 with revision 8.0.1.1.33 or later.
MySQL 8.0.2 with revision 8.0.2.2.13 or later.
Multi-master Cluster (Limitless) Edition: The revision must be 8.0.1.1.15 or later.
ALTER TABLE table_name ENGINE = CSV STORAGE OSS;
ORC format
ALTER TABLE table_name ENGINE = ORC STORAGE OSS;If a data file with the same name as the target archive file already exists in OSS, the operation fails with an error. For example:
Target file for archived table exists on oss.The
FORCE STORAGE OSSoption is supported to forcibly delete OSS files when your cluster version is MySQL 8.0.2 and the revision is 8.0.2.2.29 or later. You can add theFORCE STORAGE OSSoption to the preceding syntax to delete the table structure and the corresponding OSS files. For example:DROP TABLE table_name FORCE STORAGE OSS;
Parameters
Parameter | Description |
table_name | The name of the table to archive to OSS. |
Considerations
You can use the cold data archiving feature for tables that use the InnoDB engine or the X-Engine engine.
You cannot use DDL or DML statements to modify the table during the archiving process.
You cannot archive data to a user-created OSS server.
An InnoDB table must have a primary key to be archived.
After archiving, the table in OSS becomes read-only and may exhibit slower query performance. You should test the performance to ensure it meets your requirements.
A table with an In-Memory Column Index (IMCI) cannot be archived in CSV format; it must be archived in ORC format.
Example
Archive the data in table t to OSS in CSV or ORC format.
Create an InnoDB table named
tin theoss_testdatabase.CREATE TABLE t(a int, b int, c int, primary key(a)) ENGINE = INNODB;Insert data into table
t.INSERT INTO t VALUES (1,2,3);Archive the table by using the
ALTER TABLEcommand.Archive in CSV format:
ALTER TABLE t ENGINE = CSV CONNECTION = 'default_oss_server';Archive in ORC format:
ALTER TABLE t ENGINE = ORC STORAGE OSS;
After archiving is complete, you can view the archived table information in the PolarDB console or query the data directly by using SQL:
To view information about databases and tables archived in OSS: Log on to the PolarDB console. In the left-side navigation pane of the target cluster, choose to view the information.
Query the data in the archived table as you would with a standard table. For example:
SELECT * FROM t;
Archive a partition to an OSS external table
This section applies only to PolarDB for MySQL 8.0.2.
If the minor version of the kernel is earlier than 8.0.2.2.33, configure the following parameters:
Syntax
CALL dbms_dlm.archive_partition2table('source_db', 'source_tb', 'source_part', 'archive_db', 'archive_table', ' oss_file_filter');Parameters
Parameter | Description |
source_db | The name of the source table database. |
source_tb | The name of the source table. |
source_part | The names of the source table's partitions to archive. To specify multiple partitions, separate the names with commas. |
archive_db | The name of the destination table database. |
archive_table | The name of the destination table. |
oss_file_filter | A comma-separated list of columns to be included in the oss_file_filter for the destination table. For more information, see OSS_FILE_FILTER query acceleration. |
Considerations
If the destination OSS table does not exist, it is automatically created. To accelerate queries, an
oss_file_filteris also automatically created on the columns specified by theoss_file_filterparameter. The primary key and partition key are also automatically added to theoss_file_filter.If the target OSS table exists, you must compare the column names or column type definitions of the two tables. The data can be archived only if the definitions are consistent. Otherwise, an error occurs. You can use DDL statements to make the two table definitions consistent. For more information, see Cold Data DDL. Additionally, if an
oss_file_filteris defined on the target table and its definition is inconsistent with the one in thecall dbms_dlm.archive_partition2tablecommand, theoss_file_filterdefinition on the target table takes precedence.If the destination OSS table does not exist but a file with the same name exists in OSS, an error message is reported when you perform the archiving operation. The error message is as follows:
mysql> CALL dbms_dlm.archive_partition2table('test', 'sales', 'p0', 'test', 'sales_history', 'id'); ERROR 8181 (HY000): [Data Lifecycle Management] errmsg: Target file for archived table exists on oss, please remove it first, or use flag 'FORCE' to overwrite on existing files.If you confirm that the residual files in OSS are not needed, you can use a stored procedure to delete the data in OSS and then perform the archiving operation.
-- Delete the OSS data. mysql> CALL dbms_oss.delete_table_file('test', 'sales_history'); Query OK, 0 rows affected (0.76 sec) -- Perform the archiving operation. mysql> CALL dbms_dlm.archive_partition2table('test', 'sales', 'p0', 'test', 'sales_history', 'id'); Query OK, 0 rows affected (4.24 sec)The destination OSS table supports only cold data in CSV format.
After a partition is archived, the table must contain at least one InnoDB partition.
Archived data loses its partition information and cannot be directly restored to a partition. However, you can restore the data into a table by using an
INSERT ... SELECTstatement.You cannot archive individual second-level partitions. You can only archive an entire first-level partition, which includes all of its second-level partitions.
The following table lists the partition types supported by the partition function of PolarDB for MySQL.
First-level partition
Second-level partition
OSS archiving support
HASH
Any type
Archiving a HASH first-level partition to an OSS external table is not supported.
LIST
Any type
Supported.
RANGE
Any type
Supported.
KEY
Any type
Supported.
Any type
Archiving the DEFAULT partition is not supported.
Example
Create an InnoDB partitioned table and insert data.
DROP TABLE IF EXISTS `sales`; -- Create a partitioned table. CREATE TABLE `sales` ( `id` int DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `order_time` datetime NOT NULL, primary key (order_time) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 PARTITION BY RANGE COLUMNS(order_time) INTERVAL(month, 1) (PARTITION p0 VALUES LESS THAN ('2022-01-01') ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = InnoDB, PARTITION p2 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = InnoDB); DROP PROCEDURE IF EXISTS proc_batch_insert; delimiter $$ CREATE PROCEDURE proc_batch_insert(IN begin INT, IN end INT, IN name VARCHAR(20)) BEGIN SET @insert_stmt = concat('INSERT INTO ', name, ' VALUES(? , ?, ?);'); PREPARE stmt from @insert_stmt; WHILE begin <= end DO SET @ID1 = begin; SET @NAME = CONCAT(begin+begin*281313, '@stiven'); SET @TIME = from_days(begin + 738368); EXECUTE stmt using @ID1, @NAME, @TIME; SET begin = begin + 1; END WHILE; END; $$ delimiter ; CALL proc_batch_insert(1, 1000, 'sales');Archive the p0 partition of the partitioned table to a new OSS table.
Run the following command to view the schema of the sales table.
-- View the status of the current InnoDB table. mysql> SHOW CREATE TABLE sales;The following result is returned:
*************************** 1. row *************************** Table: sales Create Table: CREATE TABLE `sales` ( `id` int(11) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `order_time` datetime NOT NULL, PRIMARY KEY (`order_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci /*!50500 PARTITION BY RANGE COLUMNS(order_time) */ /*!99990 800020200 INTERVAL(MONTH, 1) */ /*!50500 (PARTITION p0 VALUES LESS THAN ('2022-01-01') ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = InnoDB, PARTITION p2 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = InnoDB, PARTITION _p20240201000000 VALUES LESS THAN ('2024-02-01 00:00:00') ENGINE = InnoDB, PARTITION _p20240301000000 VALUES LESS THAN ('2024-03-01 00:00:00') ENGINE = InnoDB, PARTITION _p20240401000000 VALUES LESS THAN ('2024-04-01 00:00:00') ENGINE = InnoDB, PARTITION _p20240501000000 VALUES LESS THAN ('2024-05-01 00:00:00') ENGINE = InnoDB) */ 1 row in set (0.03 sec)Run the following command to archive the p0 partition to the OSS table sales_history.
-- Archive the p0 partition to the OSS table sales_history and create an OSS_FILE_FILTER on the id column. mysql> CALL dbms_dlm.archive_partition2table('test', 'sales', 'p0', 'test', 'sales_history', 'id'); Query OK, 0 rows affected (1.86 sec)Run the following command to view the schema of the sales_history table.
SHOW CREATE TABLE sales_history;The following result is returned:
*************************** 1. row ***************************; Table: sales_history Create Table: CREATE TABLE `sales_history` ( `id` int(11) DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `order_time` datetime DEFAULT NULL, PRIMARY KEY (`order_time`) ) /*!99990 800020213 STORAGE OSS */ ENGINE=CSV DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci /*!99990 800020204 NULL_MARKER='NULL' */ /*!99990 800020223 OSS META=1 */ /*!99990 800020224 OSS_FILE_FILTER='id,order_time' */ 1 row in set (0.00 sec)
Query the new OSS table.
NoteYou can enable the OSS_FILE_FILTER query acceleration feature to accelerate queries.
mysql> explain SELECT * FROM sales_history WHERE id = 100; +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-------------------------------------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-------------------------------------------------------------------------------+ | 1 | SIMPLE | sales_history | NULL | ALL | NULL | NULL | NULL | NULL | 152 | 10.00 | Using where; With pushed engine condition (`test`.`sales_history`.`id` = 100) | +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-------------------------------------------------------------------------------+ 1 row in set, 1 warning (0.00 sec) mysql> SELECT * FROM sales_history WHERE id = 100; +------+-----------------+---------------------+ | id | name | order_time | +------+-----------------+---------------------+ | 100 | 28131400@stiven | 2021-11-09 00:00:00 | +------+-----------------+---------------------+ 1 row in set (0.24 sec)
Archive a partitioned table
You can archive a partitioned table only if your PolarDB for MySQL cluster is version 8.0.2 and the kernel minor version is 8.0.2.2.34.1 or later.
If your kernel minor version is earlier than 8.0.2.2.33, go to Quota Center. Find the quota by its Quota ID
polardb_mysql_hybrid_partition, and click Request in the Actions column to enable the feature.
When you archive a partitioned table, you archive its partitions. The table becomes a hybrid partitioned table, and the data files of the archived partitions are stored in OSS. After the archiving operation completes, PolarDB automatically releases the space that the partitions occupied in PolarStore.
Syntax
Archive as a file in CSV format:
ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = CSV;Archive as a file in ORC format:
ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = ORC;
If a data file that has the same name as the archived data file already exists in OSS, the system reports an error that the file already exists. For example:
Target file for archived table exists on oss.When your cluster version is MySQL 8.0.2 and its revision is 8.0.2.2.29 or later, you can use the
FORCE STORAGE OSSoption to forcibly delete OSS files. You can add theFORCE STORAGE OSSoption to the three syntaxes mentioned above to delete the table structure and the corresponding OSS files. For example, to archive a partitioned table in CSV format, the syntax for adding theFORCE STORAGE OSSoption is as follows:ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = CSV FORCE STORAGE OSS;
Parameters
Parameter | Description |
table_name | The name of the table to archive to OSS. |
part_name | The name of the partition to archive to OSS. |
Considerations
You can use the cold data archiving feature only for InnoDB partitioned tables.
The table becomes a hybrid partitioned table after archiving. A hybrid partitioned table can contain ORC or CSV partitions. You cannot perform online DDL operations on a hybrid partitioned table, except for adding or dropping columns and indexes. For usage considerations, see Create a hybrid partition.
You cannot modify the data of an archived partition.
You cannot use cold data archiving for second-level partitions in a partitioned table.
You cannot use cold data archiving for the
DEFAULTpartition of aLIST DEFAULT HASHpartitioned table.You cannot use cold data archiving for
HASHorKEYpartitioned tables.You cannot manually archive an entire partitioned table.
When you archive data in a partition, if a file with the same name exists in OSS, the operation fails and an error message similar to the following is reported:
Target file for archived table exists on oss.You must run the following command to overwrite the existing file in OSS. Replace
table_nameandpart_namewith your actual values.ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = CSV/ORC FORCE STORAGE OSS;
Example
Archive the data in the p1 and p2 partitions of table t to OSS in CSV format.
Create an InnoDB table named
tin the database.CREATE TABLE t(a int, b int, c int, primary key(a)) PARTITION BY RANGE(a) (PARTITION p1 values less than(100), PARTITION p2 values less than(200), PARTITION p3 values less than MAXVALUE );Insert data into table
t.INSERT INTO t VALUES(1,1,1); INSERT INTO t VALUES(10,10,10); INSERT INTO t VALUES(100,100,100); INSERT INTO t VALUES(150,150,150); INSERT INTO t VALUES(200,200,200); INSERT INTO t VALUES(1000,1000,1000);Run the following commands to archive the data in the
p1andp2partitions to the OSS engine.Archive in CSV format:
ALTER TABLE t CHANGE PARTITION p1 ENGINE = csv; ALTER TABLE t CHANGE PARTITION p2 ENGINE = csv;Archive in ORC format:
ALTER TABLE t CHANGE PARTITION p1 ENGINE = ORC; ALTER TABLE t CHANGE PARTITION p2 ENGINE = ORC;
After the archiving is complete, you can log on to the PolarDB console to view information about the databases and tables archived in OSS, or use an SQL statement to query data in the hybrid partitioned table:
To view information about databases and tables archived in OSS: Log on to the PolarDB console. In the left-side navigation pane of the target cluster, choose to view the information.
To query data in a hybrid partitioned table, see Querying hybrid partitions.
TDE encryption for archived data
Only manual archiving in CSV or ORC format is supported.
Only PolarDB for MySQL clusters with the following versions support encrypting data files in OSS during archiving:
8.0.1 with minor version 8.0.1.1.47 or later.
8.0.2 with minor version 8.0.2.2.27 or later.
If your cluster's minor version does not meet the requirements, we recommend that you upgrade the minor version. For more information, see minor version management.
To meet your data security requirements, you can enable transparent data encryption (TDE) for cold data files that are archived to OSS. The basic principle is that you select the files that you want to encrypt during the archiving operation, and the OSS server performs the encryption. For more information, see Data Encryption. After the data is encrypted, you can use SQL statements in PolarDB for MySQL to query the archived data. The entire encryption and decryption process is performed in the background and is transparent to you, requiring no additional operations.
Syntax
During a manual archiving operation, you can add the ENCRYPTION="Y" syntax to enable TDE.
ALTER TABLE t1 engine = CSV ENCRYPTION="Y" STORAGE OSS;Restore data from OSS to PolarStore
Restore a standard archived table
To modify cold data archived in OSS, use the ALTER ENGINE syntax to restore the data to PolarStore. Restoring data to PolarStore also deletes the corresponding data from OSS. After you modify the data, you can archive the modified table to OSS again.
Syntax
ALTER TABLE table_name ENGINE[=]engine_name;Parameters
Parameter | Description |
table_name | The name of the OSS table to restore. |
engine_name | The engine type after restoration. |
Considerations
Archived OSS tables are read-only and do not allow INSERT, UPDATE, or DELETE operations. To modify the data, you must first restore the table to a read-write engine, like InnoDB. Attempting to modify a read-only OSS table results in the following error:
1036 - Table 't1' is read onlyExample
Restore the OSS table t to PolarStore in the oss_test database.
ALTER TABLE `oss_test`.`t` ENGINE = InnoDB;Modify the data in the InnoDB table t. After you modify the data, you can archive table t from the InnoDB engine to OSS again. For example:
ALTER TABLE t ENGINE = CSV CONNECTION = 'default_oss_server';or
ALTER TABLE t ENGINE = CSV STORAGE OSS;Restore an archived partition
If you need to restore data from an archived partitioned table to PolarStore, you can use the ALTER statement to restore the data from OSS to PolarStore. After the data is restored, the cold data in OSS is also deleted.
Your cluster must be PolarDB for MySQL 8.0.2 with kernel revision 8.0.2.2.34.1 or later.
Syntax
ALTER TABLE table_name REORGANIZE PARTITION part_name INTO (partition_definition);Parameters
Parameter | Description |
table_name | The name of the OSS table to restore. |
part_name | The name of the partition to restore. |
partition_definition | Must be the same as the |
Example
Restore the data from the p1 partition of the archived partitioned table t from OSS to PolarStore.
ALTER TABLE t REORGANIZE PARTITION p1 INTO(PARTITION p1 values less than(100));Delete the corresponding files on OSS
This feature is supported only on PolarDB for MySQL clusters that run the following versions:
Version 8.0.1, and the minor version must be 8.0.1.1.42 or later.
Version 8.0.2, and the minor version must be 8.0.2.2.23 or later.
If the minor version of your cluster does not meet the preceding requirements, you cannot delete the corresponding files on OSS. Upgrade the minor version of your cluster. For more information, see minor version management.
When you delete a table on OSS or import it back to PolarStore, the files on OSS are not deleted automatically. After you confirm that the data is no longer needed, use the following syntax to delete the corresponding files on OSS:
CALL dbms_oss.delete_table_file('database_name', 'table_name');The operation to delete files on OSS is asynchronous. The files are completely deleted only after all nodes in the cluster no longer depend on them. High traffic can cause delays. If the command fails and returns the OSS files are still in use error, wait for a while and run the command again.