Archive data in CSV or ORC format
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. Cold data archiving has the following cluster version limits:
-
Archiving to CSV format
-
The cluster edition is Standard Edition or Cluster Edition:
-
Archive a standard table:
-
MySQL 8.0.1 with a revision of 8.0.1.1.47 or later.
-
MySQL 8.0.2 with a revision of 8.0.2.2.10 or later.
-
-
Archive a partitioned table:
-
MySQL 8.0.2 with a revision of 8.0.2.2.34.1 or later.
-
-
-
The cluster edition is Multi-master Cluster (Limitless) Edition:
-
MySQL 8.0.1 with a revision of 8.0.1.0.13 or later.
-
-
-
Archiving to ORC format
-
The cluster edition is Cluster Edition:
-
Archive a standard table: MySQL 8.0.2 with a revision of 8.0.2.2.30 or later.
-
Archive a partitioned table: MySQL 8.0.2 with a revision of 8.0.2.2.34.1 or later.
-
-
The cluster edition is Multi-master Cluster (Limitless) Edition: MySQL 8.0.2 with a revision of 8.0.2.2.30 or later.
-
Manually archiving cold data on clusters running the following versions does not record binary logs.
PolarDB for MySQL version 8.0.1 with a minor engine version of 8.0.1.1.33 or later.
PolarDB for MySQL version 8.0.2 with a minor engine version of 8.0.2.2.11.1 or later.
Usage notes
Archive a standard table
Cold data archiving is a table archiving operation. A table that has undergone cold data archiving is called an archived table (read-only). The archived table uses the OSS engine, and the data files of the archived table are stored in OSS. After cold data archiving is complete, the space that the original local table occupied in PolarStore is released.
Syntax
CSV format
Format 1:
ALTER TABLE table_name ENGINE = CSV CONNECTION = 'default_oss_server';Format 2: Your cluster version must meet one of the following conditions.
Cluster Edition:
MySQL 8.0.1 with a revision of 8.0.1.1.33 or later.
MySQL 8.0.2 with a revision of 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 OSS contains a data file whose name conflicts with the name of the data file to archive, the system returns a file-exists error. For example:
Target file for archived table exists on oss.When your cluster version is MySQL 8.0.2 and the 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 preceding syntax to delete the table structure together with 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
Cold data archiving is supported for tables that use the InnoDB engine and the X-Engine engine.
You cannot modify a table during cold data archiving (DDL and DML operations are not supported).
Cold data archiving does not support archiving data files to a user-created OSS Server.
To use cold data archiving on a table that uses the InnoDB engine, the table to archive must have a primary key.
After cold data archiving is complete, archived tables in OSS are read-only and have poor query performance. Test whether query performance after data archiving meets your requirements.
If a table has In-Memory Column Index (IMCI), the table cannot be archived in CSV format and can only be archived in ORC format.
Examples
Archive data in table t to OSS in CSV or ORC format.
In database
oss_test, create an InnoDB tablet.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 cold data by using the
ALTERcommand.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 log on to the PolarDB console to view information about the databases and tables archived in OSS, or use an SQL statement to view data in the archived table:
View information about the databases and tables archived in OSS: Log in to the PolarDB console。 For the target cluster, choose to view information about the databases and tables archived in OSS.
View data in the archived table: Use an SQL statement to view data in the archived table without changing how the table is accessed. For example:
SELECT * FROM t;
Archive a partitioned table to an OSS external table
This feature applies only to PolarDB for MySQL version 8.0.2.
If the minor engine version is earlier than 8.0.2.2.33, adjust 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 database name of the source table. |
source_tb | The name of the source table. |
source_part | The partitions of the source table to archive. Separate multiple partitions with commas. |
archive_db | The database name of the destination table. |
archive_table | The name of the destination table. |
oss_file_filter | Specifies whether a new FILE FILTER must be created for the destination table. For more information, see Use the OSS file filter feature to accelerate cold data queries. |
Considerations
If the destination OSS table does not exist, the system automatically creates the destination OSS table and, on the columns specified by
oss_file_filter, the system automatically creates anoss_file_filteron the destination table to provide query acceleration. In addition, theoss_file_filterautomatically includes the primary key and the partition key to improve query speed.If the destination OSS table exists, the system compares the column names or column type definitions of the two tables. If they are consistent, the data can be archived. If they are inconsistent, an error occurs. You can use a DDL statement to make the two table definitions consistent. For more information, see DDL for cold data. In addition, if the destination table has an
oss_file_filterwhose definition differs from the definition incall dbms_dlm.archive_partition2table, theoss_file_filteron the destination table takes precedence.If the destination OSS table does not exist but OSS contains a file with the same name, an error occurs during 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 no longer 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 cold data only in CSV format.
After a partitioned table is archived, it must retain at least one InnoDB partition.
Archived data loses its partition information and cannot be restored directly. However, you can restore it by using
insert select.You cannot archive individual second-level partitions. You can archive all second-level partitions under an entire first-level partition.
The following table lists the partition types supported by the partition function of PolarDB for MySQL.
First-level partition
Second-level partition
Whether archiving to an OSS external table is supported
HASH
Any type
Archiving HASH first-level partitions 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.
Examples
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 output is as follows:
*************************** 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 id-column OSS_FILE_FILTER; 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 output is as follows:
*************************** 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
To archive a partitioned table as a hybrid partitioned table, the following conditions must be met:
-
MySQL 8.0.2 and the minor version is 8.0.2.2.34.1 or later. You must set the
loose_polar_allow_create_hybrid_partitionparameter toON. -
MySQL 8.0.2 and the minor version is earlier than 8.0.2.2.34.1. Upgrade to a later minor version.
Archiving a partitioned table archives the partitions of the table. The archived table is a hybrid partitioned table, and the data files of the archived partitions are stored in OSS. After the archiving operation is complete, the space that the partitions occupied in PolarStore is automatically released.
Syntax
Archive files in CSV format:
ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = CSV;Archive files in ORC format:
ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = ORC;
If OSS contains a data file whose name conflicts with the name of the data file to archive, the system returns a file-exists error. For example:
Target file for archived table exists on oss.When your cluster version is MySQL 8.0.2 and the 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 preceding three syntaxes to delete the table structure together with the corresponding OSS files. For example, to archive a partitioned table in CSV format, use the following syntax with theFORCE STORAGE OSSoption: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
Cold data archiving is supported only for partitioned tables that use the InnoDB engine.
The archived table is a hybrid partitioned table. A hybrid partitioned table contains ORC or CSV partitions. Online DDL operations are not supported on hybrid partitioned tables, except for adding or dropping columns and indexes. For usage considerations, see Create a hybrid partition.
You cannot modify data in archived partitions.
Cold data archiving is not supported for second-level partitions in partitioned tables.
Cold data archiving is not supported for the
LIST DEFAULT HASHpartition table'sDEFAULTpartition.Cold data archiving is not supported for partitioned tables of the
HASHorKEYtype.You cannot manually archive cold data from an entire partitioned table.
If OSS contains a file with the same name when you archive data in a partition, the operation fails and returns an error message similar to the following:
Target file for archived table exists on oss.Run the following command to overwrite the existing files in OSS. Replace
table_nameandpart_namewith actual values.ALTER TABLE table_name CHANGE PARTITION part_name ENGINE = CSV/ORC FORCE STORAGE OSS;
Examples
Archive data in table t from partitions p1 and p2 to OSS in CSV format.
Create an InnoDB table
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 command to archive data in partitions
p1andp2to 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 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:
View information about the databases and tables archived in OSS: Log in to the PolarDB console。 For the target cluster, choose to view information about the databases and tables archived in OSS.
Query data in the hybrid partitioned table: For more information, see Query a hybrid partition.
TDE encryption for archived data
Only manual archiving in CSV or ORC format is supported.
Only PolarDB for MySQL clusters that meet the following version requirements support data encryption when the corresponding files in OSS are archived:
Version 8.0.1 with a minor engine version of 8.0.1.1.47 or later.
Version 8.0.2 with a minor engine version of 8.0.2.2.27 or later.
If the minor engine version of your cluster does not meet the preceding version requirements, upgrade the minor engine version of your cluster. For more information, see Minor version management.
To meet your data security requirements, files archived to OSS by cold data archiving support transparent data encryption (TDE). The basic principle is that, during the archiving operation, you select the files that require TDE encryption and the OSS server performs the encryption. For more information about encryption, see Data encryption. After encryption, you can use SQL statements in PolarDB for MySQL to query archived data. The entire encryption and decryption process runs in the background and is transparent to you, with no additional action required.
Syntax
During manual archiving, you can add the ENCRYPTION="Y" syntax to enable TDE encryption.
ALTER TABLE t1 engine = CSV ENCRYPTION="Y" STORAGE OSS;Restore data from OSS to PolarStore
Restore data from a standard table archived in OSS
If you need to modify cold data archived in OSS infrequently, you can use the ALTER ENGINE syntax to restore OSS data to PolarStore for modification. Restoring data to PolarStore also deletes the cold data from OSS. After modifying the data, you can archive the modified table as an OSS table 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
An OSS table in the read-only state does not support modification operations (INSERT, UPDATE, and DELETE). To modify archived cold data, you must convert the OSS table to a read-write table, such as an InnoDB table. Modifying a read-only OSS table returns the following error:
1036 - Table 't1' is read onlyExamples
In database oss_test, restore OSS table t to PolarStore.
ALTER TABLE `oss_test`.`t` ENGINE = InnoDB;Modify data in the InnoDB table t. After modification, archive table t in 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 data from a partitioned table archived in OSS
If you need to restore data from an archived partitioned table to PolarStore, you can use the ALTER statement to restore the data in OSS to PolarStore. After restoration, the cold data in OSS is deleted.
The cluster version must be PolarDB for MySQL version 8.0.2 with a minor engine version of 8.0.2.2.34 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 match the |
Examples
In the database, restore data in the partitioned table t, which is archived in OSS, from partition p1 to PolarStore.
ALTER TABLE t REORGANIZE PARTITION p1 INTO(PARTITION p1 values less than(100));Delete corresponding files in OSS
Only PolarDB for MySQL clusters that meet the following version requirements support deleting corresponding files in OSS:
Version 8.0.1 with a minor engine version of 8.0.1.1.42 or later.
Version 8.0.2 with a minor engine version of 8.0.2.2.23 or later.
If the minor engine version of your cluster does not meet the preceding version requirements, deleting corresponding files in OSS is not supported. Upgrade the minor engine version of your cluster. For more information, see Minor version management.
When you delete a table in OSS or restore it to PolarStore, the files in OSS are not deleted automatically. After you confirm that the data is no longer needed, you can use the following syntax to delete the corresponding files in OSS:
CALL dbms_oss.delete_table_file('database_name', 'table_name');Deleting the corresponding files in OSS is asynchronous. The files can be completely deleted only after all nodes in the cluster no longer depend on them, and high traffic can cause delays. Therefore, if the preceding command fails and returns the error message OSS files are still in use, wait for a while and then run the command again.