Archive partitioned tables in CSV or ORC format

Updated at:

The automatic cold data archiving feature, Data Lifecycle Management (DLM), periodically archives infrequently accessed cold data from PolarStore to low-cost OSS storage media in CSV or ORC format, which reduces costs and improves efficiency.

Prerequisites

  • Your cluster must run PolarDB for MySQL 8.0.2 with revision version 8.0.2.2.34.1 or later.

    Note
    • You can use Query the engine version to verify the cluster version.

    • If the cluster runs PolarDB for MySQL 8.0.2 with revision version 8.0.2.2.11.1 or later, the DLM feature does not record operations in binary logs.

  • Before you use a DLM policy, Enable cold data archiving.

    Note

    If cold data archiving is not enabled, the following error is returned:

    ERROR 8158 (HY000): [Data Lifecycle Management] DLM storage engine is not support. The value of polar_dlm_storage_mode is OFF.

Limits

  • The DLM feature supports only partitioned tables, and the partitioned tables cannot contain subpartitions. The partitioning method must be RANGE COLUMN.

  • The DLM feature is not supported for partitioned tables that were created with Global Secondary Indexes (GSIs).

  • PolarDB for MySQL does not support modifying DLM policies. Delete the existing policy before you create a policy.

  • If the current table has a DLM policy, certain DDL operations can make the schema of the archived table inconsistent with the schema of the original table. For example, adding or dropping columns or changing column types can prevent subsequently archived data from being parsed. Delete the DLM policy on the current table before you perform these DDL operations. If you later need automatic data archiving, create another DLM policy and specify a new archived table name. The new table name cannot be the same as a previously archived table name.

  • (Recommended) Use Interval range overview to automatically extend partitions, and use the DLM feature to archive data in infrequently accessed partitions to OSS.

    Note

    Only PolarDB for MySQL 8.0.2 clusters with revision version 8.0.2.2.0 or later support INTERVAL RANGE partitioning.

  • Specify the DLM feature in CREATE TABLE or ALTER TABLE statements.

  • DLM policies are not displayed by SHOW CREATE TABLE. You can view DLM policy information for all tables in mysql.dlm_policies table.

Precautions

  • After cold data archiving is complete, archived tables in OSS are read-only and have lower query performance. Test in advance whether archived data meets your query performance requirements.

  • After you archive partitions of a partitioned table to OSS, the data in the partitions archived to OSS is read-only.

  • Hybrid partitioned tables support Copy DDL and INPLACE DDL for creating or deleting secondary indexes, but do not yet support Instant DDL.

  • Backup operations do not back up data that has been archived to OSS, and data in OSS does not support point-in-time restore.

Syntax

Create a DLM policy

  • Create a DLM policy with CREATE TABLE

    CREATE TABLE [IF NOT EXISTS] tbl_name
        (create_definition,...)
        [table_options]
        [partition_options]
        [dlm_add_options]
    
    dlm_add_options:
        DLM ADD
            [(dlm_policy_definition [, dlm_policy_definition] ...)]
    
    dlm_policy_definition:
        POLICY policy_name
        [TIER TO TABLE/TIER TO PARTITION/TIER TO NONE]
        [ENGINE [=] engine_name]
        [STORAGE SCHEMA_NAME [=] storage_schema_name]
        [STORAGE TABLE_NAME [=] storage_table_name]
        [STORAGE [=] OSS]
        [READ ONLY]
        [COMMENT 'comment_string']
        [EXTRA_INFO 'extra_info']
        ON [(PARTITIONS OVER num)]           
  • Create a DLM policy with ALTER TABLE

    ALTER TABLE tbl_name
        [alter_option [, alter_option] ...]
        [partition_options]
        [dlm_add_options]
    
    dlm_add_options:
        DLM ADD
            [(dlm_policy_definition [, dlm_policy_definition] ...)]
    
    dlm_policy_definition:
        POLICY policy_name
        [TIER TO TABLE/TIER TO PARTITION/TIER TO NONE]
        [ENGINE [=] engine_name]
        [STORAGE SCHEMA_NAME [=] storage_schema_name]
        [STORAGE TABLE_NAME [=] storage_table_name]
        [STORAGE [=] OSS]
        [READ ONLY]
        [COMMENT 'comment_string']
        [EXTRA_INFO 'extra_info']
        ON [(PARTITIONS OVER num)]      

DLM policy parameters

Parameter

Required

Description

tbl_name

Yes

The table name.

policy_name

Yes

The policy name.

TIER TO TABLE

Yes

Archive data to an OSS foreign table.

Note

Only CSV format is currently supported for archiving to an OSS foreign table.

TIER TO PARTITION

Yes

Archive partitions in CSV, ORC, or X-Engine format, which converts the table into a hybrid partitioned table.

Note
  • To archive a partitioned table as a hybrid partitioned table, the following conditions must be met:

    • MySQL 8.0.2with minor version 8.0.2.2.34.1 or later, set the loose_polar_allow_create_hybrid_partition parameter to ON.

    • MySQL 8.0.2with minor version earlier than 8.0.2.2.34.1, upgrade to the required minor version.

  • When you use this feature, make sure that the total number of partitions in the partitioned table does not exceed 8192.

TIER TO NONE

Yes

Delete the data to be archived directly.

engine_name

No

The engine that stores the archived data. Supported formats: CSV, ORCand X-Engine.

storage_schema_name

No

When archiving to a table, the database that contains the table. The default is the database that contains the current table.

storage_table_name

No

When archiving to a table, the name of the table. You can specify the table name. The default is current_table_name_current_DLM_policy_name.

STORAGE [=] OSS

No

Archived data is stored in the OSS engine by default.

READ ONLY

No

Archived data is read-only by default.

comment_string

No

The comment for the DLM policy.

extra_info

No

The OSS_FILE_FILTER information for the destination OSS table.

Note
  • Only Enterprise Edition clusters that run PolarDB for MySQL 8.0.2 with revision version 8.0.2.2.25 or later support archiving partitions of partitioned tables to OSS.

  • This feature takes effect only when the destination table does not exist. In this case, based on the OSS_FILE_FILTER parameter in EXTRA_INFO, the system automatically generates the FILE_FILTER attribute when it creates the destination OSS table, and automatically generates filter data during archiving. If the destination table already exists, the existing File Filter applies.

EXTRA_INFO uses the format {"oss_file_filter":"field_filter[,field_filter]"}, where field_filter uses the following format:

field_filter := field_name[:filter_type]
filter_type := bloom

ON (PARTITIONS OVER num)

Yes

Archive data when the number of partitions exceeds num.

Modify a DLM policy

  • Enable a DLM policy.

    ALTER TABLE table_name DLM ENABLE POLICY [(dlm_policy_name [, dlm_policy_name] ...)]
  • Disable a DLM policy.

    ALTER TABLE table_name DLM DISABLE POLICY [(dlm_policy_name [, dlm_policy_name] ...)]
  • Delete a DLM policy.

    ALTER TABLE table_name DLM DROP POLICY [(dlm_policy_name [, dlm_policy_name] ...)]

In the preceding statements, table_name is the current table name, and dlm_policy_name is the name of the policy to modify. You can configure multiple policy names.

Execute a DLM policy

  • Execute the DLM policies for all tables in the current cluster.

    CALL dbms_dlm.execute_all_dlm_policies();
  • Execute the DLM policies for a single table.

    CALL dbms_dlm.execute_table_dlm_policies('database_name', 'table_name');

    In the preceding statement, database_name is the name of the database that contains the current table, and table_name is the name of the current table.

You can use the mysql event feature to execute DLM policies during your cluster maintenance window. This prevents DLM from affecting database performance during peak business hours, and it periodically moves expired data to reduce your database storage costs. The syntax for executing a DLM policy by using EVENT is as follows:

CREATE
    EVENT
    [IF NOT EXISTS]
    event_name
    ON SCHEDULE schedule
    [COMMENT 'comment']
    DO event_body;

schedule: {
  EVERY interval
  [STARTS timestamp [+ INTERVAL interval] ...]
}

interval:
    quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
              WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
              DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}

event_body: {
      CALL dbms_dlm.execute_all_dlm_policies();
    | CALL dbms_dlm.execute_table_dlm_policies('database_name', 'table_name');
}

The following table describes the parameters.

Parameter

Required

Description

event_name

Yes

The name of the EVENT.

schedule

Yes

The execution time and interval of the EVENT.

comment

No

The comment for the EVENT.

event_body

Yes

The content that the EVENT executes. Set it to a statement that executes a DLM policy by using EVENT.

Note
  • When you use CALL dbms_dlm.execute_all_dlm_policies(), the EVENT executes all DLM policies in the cluster. Therefore, create one EVENT for each cluster.

  • When you use CALL dbms_dlm.execute_table_dlm_policies('database_name', 'table_name');, the EVENT executes only the DLM policies of a specific table. Therefore, create an EVENT for each table that has a DLM policy so that the table archives data at the specified time.

interval

Yes

The execution interval of the EVENT.

timestamp

Yes

The time at which the EVENT starts to run.

database_name

Yes

The database name.

table_name

Yes

The table name.

For information about the MySQL EVENT feature, see the official MySQL EVENT documentation.

For usage examples, see Examples of archiving cold data to OSS.

Examples

Archive data in a partitioned table to an OSS foreign table

  1. Create a DLM policy

    The following example creates the sales partitioned table, which uses the order_time column as the partition key and partitions data by time interval. The table has both INTERVAL and DLM policies:

    • INTERVAL policy: When inserted data exceeds the partition range, a new partition is created automatically at a one-year interval.

    • DLM policy: The table is defined with only three partitions. When the number of partitions exceeds three and the DLM policy runs:

      • If the OSS foreign table sales_history does not exist, a new OSS foreign table named sales_history is created, and cold data is archived to the sales_history foreign table.

      • If the sales_history foreign table exists and the sales_history table is in built-in OSS storage, cold data is archived directly to the sales_history foreign table.

    Note

    Creating an INTERVAL RANGE partitioned table requires that you meet the creation conditions. For information about how to use INTERVAL, see PolarDB INTERVAL.

    1. Create the sales table with a DLM policy.

      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(YEAR, 1)
      (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB)
      DLM ADD POLICY test_policy TIER TO TABLE ENGINE=CSV STORAGE=OSS READ ONLY
      STORAGE TABLE_NAME = 'sales_history' EXTRA_INFO '{"oss_file_filter":"id,name:bloom"}' ON (PARTITIONS OVER 3);

      The DLM policy for this table is named test_policy. When the number of partitions exceeds three, cold data from the current table is archived in CSV format and stored in OSS. The archived table is named sales_history, and the data is read-only. In addition, if the archived OSS table does not exist, the OSS table is created automatically, and an OSS_FILE_FILTER is created on the id and name columns.

    2. The DLM policies for the current table are stored in the system table mysql.dlm_policies. You can use this table to view the details of the DLM policies. For information about the mysql.dlm_policies table, see Table structures. Run the following command to view the schema of the mysql.dlm_policies table.

      mysql> SELECT * FROM mysql.dlm_policies\G

      The result is as follows:

      *************************** 1. row ***************************
                         Id: 3
               Table_schema: test
                 Table_name: sales
                Policy_name: test_policy
                Policy_type: TABLE
               Archive_type: PARTITION COUNT
               Storage_mode: READ ONLY
             Storage_engine: CSV
              Storage_media: OSS
        Storage_schema_name: test
         Storage_table_name: sales_history
            Data_compressed: OFF
       Compressed_algorithm: NULL
                    Enabled: ENABLED
            Priority_number: 10300
      Tier_partition_number: 3
             Tier_condition: NULL
                 Extra_info: {"oss_file_filter": "id,name:bloom,order_time"}
                    Comment: NULL
      1 row in set (0.03 sec)      

      The sales table currently has three partitions, so data is not archived.

    3. Insert 3000 rows of test data into the sales partitioned table. Make sure that the data exceeds the partition definitions of the current table so that INTERVAL is triggered to create new partitions automatically.

      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 + 737600);
      EXECUTE stmt using @ID1, @NAME, @TIME;
      SET begin = begin + 1;
      END WHILE;
      END;
      $$
      delimiter ;
      CALL proc_batch_insert(1, 3000, 'sales');
    4. At this point, INTERVAL has created new partitions automatically, which increases the number of partitions in the sales table. The table schema becomes:

      mysql> SHOW CREATE TABLE sales\G

      The result 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(YEAR, 1) */
      /*!50500 (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20230101000000 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20240101000000 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20250101000000 VALUES LESS THAN ('2025-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
      1 row in set (0.03 sec)

      New partitions are created and the number of partitions exceeds three. The execution condition of the DLM policy is met, and data can be archived.

  2. Execute the DLM policy

    1. You can execute DLM policies directly by using SQL statements, or periodically by using the MySQL EVENT feature. Assume that from October 11, 2022, your cluster maintenance window starts at 01:00 every day. The DLM policies then start to run at 01:00 every day. Create the corresponding EVENT as follows:

      CREATE EVENT dlm_system_base_event
             ON SCHEDULE EVERY 1 DAY
          STARTS '2022-10-11 01:00:00'
          do CALL 
      dbms_dlm.execute_all_dlm_policies();

      After 01:00, this EVENT executes the DLM policies for all tables.

    2. Run the following command to view the schema of the sales table.

      mysql> SHOW CREATE TABLE sales\G

      The result 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(YEAR, 1) */
      /*!50500 (PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB,
      PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB, 
      PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
      1 row in set (0.03 sec)

      Only three partitions remain in the current table.

    3. You can use the mysql.dlm_progress table to view the execution records of DLM policies. For information about the definition of the dlm_progress table, see Table structures. Run the following command to view the schema of the mysql.dlm_progress table.

      mysql> SELECT * FROM mysql.dlm_progress\G

      The result is as follows:

      *************************** 1. row ***************************;
                        Id: 1
              Table_schema: test
                Table_name: sales
               Policy_name: test_policy
               Policy_type: TABLE
            Archive_option: PARTITIONS OVER 3
            Storage_engine: CSV
             Storage_media: OSS
           Data_compressed: OFF
      Compressed_algorithm: NULL
        Archive_partitions: p20200101000000, p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, _p20250101000000
             Archive_stage: ARCHIVE_COMPLETE
        Archive_percentage: 0
        Archived_file_info: null
                Start_time: 2024-07-26 17:56:20
                  End_time: 2024-07-26 17:56:50
                Extra_info: null
      1 row in set (0.00 sec)

      The partitions that store infrequently accessed cold data, including p20200101000000, p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, and _p20250101000000, have all been archived to the OSS foreign table.

    4. Run the following command to view the schema of the OSS foreign table.

      mysql> SHOW CREATE TABLE sales_history\G

      The result 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,name:bloom,order_time' */
      1 row in set (0.15 sec)

      The table becomes a CSV table whose data is stored in the OSS engine. You can query it in the same way as a local table. In addition, the specified columns have been added to OSS_FILE_FILTER, and because order_time is the partition key, OSS_FILE_FILTER is also created automatically.

    5. Query the data in the sales and sales_history tables separately.

      SELECT COUNT(*) FROM sales;
      +----------+
      | count(*) |
      +----------+
      |      984 |
      +----------+
      1 row in set (0.01 sec)
      
      SELECT COUNT(*) FROM sales_history;
      +----------+
      | count(*) |
      +----------+
      |     2016 |
      +----------+
      1 row in set (0.57 sec)           

      The total amount of data is exactly 3000, which is the same as the amount of data initially inserted into the sales table.

    6. Query the OSS foreign table by using OSS_FILE_FILTER. You must turn on the OSS_FILE_FILTER switch:

      mysql> explain select * from sales_history where id = 9;
      +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-----------------------------------------------------------------------------+
      | 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 | 2016 |    10.00 | Using where; With pushed engine condition (`test`.`sales_history`.`id` = 9) |
      +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-----------------------------------------------------------------------------+
      1 row in set, 1 warning (0.59 sec)
      
      mysql>  select * from sales_history where id = 9;
      +------+----------------+---------------------+
      | id   | name           | order_time          |
      +------+----------------+---------------------+
      |    9 | 2531826@stiven | 2019-07-04 00:00:00 |
      +------+----------------+---------------------+
      1 row in set (0.19 sec)

Archive partitions of a partitioned table to OSS

Archive in CSV format

  1. Create a DLM policy

    The following example creates a partitioned table named sales, which uses the order_time column as the partition key and partitions data by time interval. The table has both INTERVAL and DLM policies:

    • INTERVAL policy: When inserted data exceeds the partition range, a new partition is created automatically at a one-year interval.

    • DLM policy: The table is defined with only three partitions. When the number of partitions exceeds three, executing the DLM policy archives the earlier partitions directly to OSS.

    1. Create the sales 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(YEAR, 1)
      (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB)
      DLM ADD POLICY policy_part2part TIER TO PARTITION ENGINE=CSV STORAGE=OSS READ ONLY ON (PARTITIONS OVER 3);

      The DLM policy for this table is named policy_part2part. When the number of partitions exceeds three, the older partitions are archived to OSS.

    2. View the DLM policy in the mysql.dlm_policies table.

      SELECT * FROM mysql.dlm_policies\G

      The result is as follows:

      *************************** 1. row ***************************
                         Id: 2
               Table_schema: test
                 Table_name: sales
                Policy_name: policy_part2part
                Policy_type: PARTITION
               Archive_type: PARTITION COUNT
               Storage_mode: READ ONLY
             Storage_engine: CSV
              Storage_media: OSS
        Storage_schema_name: NULL
         Storage_table_name: NULL
            Data_compressed: OFF
       Compressed_algorithm: NULL
                    Enabled: ENABLED
            Priority_number: 10300
      Tier_partition_number: 3
             Tier_condition: NULL
                 Extra_info: null
                    Comment: NULL
      1 row in set (0.03 sec)
    3. Use the proc_batch_insert stored procedure to insert test data into the sales partitioned table, which triggers the INTERVAL policy to create new partitions automatically.

      CALL proc_batch_insert(1, 3000, 'sales');

      The following result indicates that the data was inserted:

      Query OK, 1 row affected, 1 warning (0.99 sec)
    4. Run the following command to view the schema of the sales table.

      SHOW CREATE TABLE sales \G

      The result 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 DEFAULT 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(YEAR, 1) */
      /*!50500 (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20230101000000 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20240101000000 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20250101000000 VALUES LESS THAN ('2025-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
      1 row in set (0.03 sec)
  2. Execute the DLM policy

    1. Execute the DLM policy by using the following command.

      CALL dbms_dlm.execute_all_dlm_policies();
    2. View the DLM execution records in the mysql.dlm_progress table.

      SELECT * FROM mysql.dlm_progress \G

      The result is as follows:

      *************************** 1. row ***************************
                        Id: 4
              Table_schema: test
                Table_name: sales
               Policy_name: policy_part2part
               Policy_type: PARTITION
            Archive_option: PARTITIONS OVER 3
            Storage_engine: CSV
             Storage_media: OSS
           Data_compressed: OFF
      Compressed_algorithm: NULL
        Archive_partitions: p20200101000000, p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, _p20250101000000
             Archive_stage: ARCHIVE_COMPLETE
        Archive_percentage: 100
        Archived_file_info: null
                Start_time: 2023-09-11 18:04:39
                  End_time: 2023-09-11 18:04:40
                Extra_info: null
      1 row in set (0.02 sec)
    3. Run the following command to view the schema of the sales table.

      SHOW CREATE TABLE sales \G

      The result 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 DEFAULT NULL,
         PRIMARY KEY (`order_time`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci CONNECTION='default_oss_server'
      /*!99990 800020205 PARTITION BY RANGE  COLUMNS(order_time) */ /*!99990 800020200 INTERVAL(YEAR, 1) */
      /*!99990 800020205 (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = CSV,
       PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = CSV,
       PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = CSV,
       PARTITION _p20230101000000 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = CSV,
       PARTITION _p20240101000000 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = CSV,
       PARTITION _p20250101000000 VALUES LESS THAN ('2025-01-01 00:00:00') ENGINE = CSV,
       PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
      1 row in set (0.03 sec)

      The queried schema shows that in the sales partitioned table, the p20200101000000, p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, and _p20250101000000 partitions are all archived to OSS. Only three hot data partitions remain in the InnoDB engine: _p20260101000000, _p20270101000000, and _p20280101000000. The sales table becomes a hybrid partitioned table. For information about how to query data in a hybrid partitioned table, see Query a hybrid partition.

Archive in ORC format

  1. Create a DLM policy

    The following example creates the sales partitioned table, which uses the order_time column as the partition key and partitions data by time interval. The table has both INTERVAL and DLM policies:

    • INTERVAL policy: When inserted data exceeds the partition range, a new partition is created automatically at a one-year interval.

    • DLM policy: The table is defined with only three partitions. When the number of partitions exceeds three and the DLM policy runs, the older partitions are archived directly in ORC format to OSS, and the table becomes a hybrid partitioned table.

    Create the sales table with a DLM policy. Use TIER TO PARTITION ENGINE=ORC to archive the older partitions to OSS.

    CREATE TABLE `sales_orc` (
      `id` int DEFAULT NULL,
      `name` varchar(20) DEFAULT NULL,
      `order_time` datetime NOT NULL,
       PRIMARY KEY (`order_time`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    -- Create RANGE COLUMN partitions on the order_time column. A new partition is created automatically at a one-year interval (INTERVAL policy)
    PARTITION BY RANGE  COLUMNS(order_time) INTERVAL(YEAR, 1)
    -- Pre-create three initial partitions. They store data from before 2020, 2021, and 2022, respectively
    (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB,
     PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB,
     PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB)
    -- DLM policy. When the partition count exceeds 3, the older partitions are archived directly in ORC format to OSS
    -- TIER TO PARTITION indicates partition-level archiving. The original table becomes a hybrid partitioned table (hot InnoDB partitions coexist with cold ORC partitions)
    DLM ADD POLICY policy_orc TIER TO PARTITION ENGINE=ORC STORAGE=OSS READ ONLY
    ON (PARTITIONS OVER 3);

    The DLM policy for this table is named policy_orc. When the number of partitions exceeds three, the older partitions are archived directly to OSS in ORC format, and the original table becomes a hybrid partitioned table (hot InnoDB partitions coexist with cold ORC partitions).

  2. Execute the DLM policy

    Insert test data into the sales partitioned table to trigger INTERVAL to create new partitions automatically, so that the number of partitions exceeds three and the DLM policy execution condition is met.

    CALL proc_batch_insert(1, 3000, 'sales_orc');

    Execute the DLM policy:

    CALL dbms_dlm.execute_all_dlm_policies();
  3. View the archiving result

    Run the following command to view the execution records of the DLM policy in the mysql.dlm_progress table.

    SELECT * FROM mysql.dlm_progress\G

    The partitions that store infrequently accessed cold data have all been archived to OSS. Run the following command to view the schema of the sales_orc table:

    SHOW CREATE TABLE sales_orc\G

    The result is as follows:

    *************************** 1. row ***************************
           Table: sales_orc
    CREATE TABLE `sales_orc` (
      `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 /*!99990 800020223 OSS META=1 */ CONNECTION='default_oss_server'
    /*!99990 800020205 PARTITION BY RANGE  COLUMNS(order_time) */ /*!99990 800020200 INTERVAL(YEAR, 1) */
    /*!99990 800020205 (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = ORC,
     PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = ORC,
     PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = ORC,
     PARTITION _p20230101000000 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = ORC,
     PARTITION _p20240101000000 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = ORC,
     PARTITION _p20250101000000 VALUES LESS THAN ('2025-01-01 00:00:00') ENGINE = ORC,
     PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB,
     PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB,
     PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
    1 row in set (0.03 sec)

    The queried schema shows that in the sales_orc partitioned table, the p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, and _p20250101000000 partitions are all archived to OSS. Only three hot data partitions remain in the InnoDB engine: _p20260101000000, _p20270101000000, and _p20280101000000. The sales_orc table becomes a hybrid partitioned table. For information about how to query data in a hybrid partitioned table, see Query a hybrid partition.

Delete cold data directly

  1. Create a DLM policy

    The following example creates the sales partitioned table, which uses the order_time column as the partition key and partitions data by time interval. The table has both INTERVAL and DLM policies:

    • INTERVAL policy: When inserted data exceeds the partition range, a new partition is created automatically at a one-year interval.

    • DLM policy: The table is defined with only three partitions. When the number of partitions exceeds three and the DLM policy runs, cold data is deleted directly.

    1. Create the sales table with a DLM policy.

      CREATE TABLE `sales` (
        `id` int DEFAULT NULL,
        `name` varchar(20) DEFAULT NULL,
        `order_time` datetime DEFAULT NULL,
         PRIMARY KEY (`order_time`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
      PARTITION BY RANGE  COLUMNS(order_time) INTERVAL(YEAR, 1)
      (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB)
      DLM ADD POLICY test_policy TIER TO NONE ON (PARTITIONS OVER 3);

      The DLM policy for this table is named test_policy. The execution condition is that the number of partitions exceeds three. When the DLM policy runs, it deletes cold data directly.

    2. Run the following command to view the mysql.dlm_policies table.

      SELECT * FROM mysql.dlm_policies\G

      The result is as follows:

      *************************** 1. row ***************************
                         Id: 4
               Table_schema: test
                 Table_name: sales
                Policy_name: test_policy
                Policy_type: NONE
               Archive_type: PARTITION COUNT
               Storage_mode: NULL
             Storage_engine: NULL
              Storage_media: NULL
        Storage_schema_name: NULL
         Storage_table_name: NULL
            Data_compressed: OFF
       Compressed_algorithm: NULL
                    Enabled: ENABLED
            Priority_number: 50000
      Tier_partition_number: 3
             Tier_condition: NULL
                 Extra_info: null
                    Comment: NULL
      1 row in set (0.01 sec)
    3. Insert test data into the sales partitioned table to trigger INTERVAL to create new partitions automatically. Use the proc_batch_insert stored procedure to insert the new data. The table schema is as follows:

      CALL proc_batch_insert(1, 3000, 'sales');
      Query OK, 1 row affected, 1 warning (0.99 sec)
    4. Run the following command to view the schema of the sales table.

      SHOW CREATE TABLE sales \G

      The result 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 DEFAULT 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(YEAR, 1) */
      /*!50500 (PARTITION p20200101000000 VALUES LESS THAN ('2020-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20210101000000 VALUES LESS THAN ('2021-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION p20220101000000 VALUES LESS THAN ('2022-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20230101000000 VALUES LESS THAN ('2023-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20240101000000 VALUES LESS THAN ('2024-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20250101000000 VALUES LESS THAN ('2025-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
      1 row in set (0.03 sec)
  2. Execute the DLM policy

    1. Execute the DLM policy directly by using the following command.

      CALL dbms_dlm.execute_all_dlm_policies();
    2. While the DLM policy runs, view the data in the mysql.dlm_progress table.

      SELECT * FROM mysql.dlm_progress \G

      The table contains the following result:

      *************************** 1. row ***************************
                        Id: 1
              Table_schema: test
                Table_name: sales
               Policy_name: test_policy
               Policy_type: NONE
            Archive_option: PARTITIONS OVER 3
            Storage_engine: NULL
             Storage_media: NULL
           Data_compressed: OFF
      Compressed_algorithm: NULL
        Archive_partitions: p20200101000000, p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, _p20250101000000
             Archive_stage: ARCHIVE_COMPLETE
        Archive_percentage: 100
        Archived_file_info: null
                Start_time: 2023-01-09 17:31:24
                  End_time: 2023-01-09 17:31:24
                Extra_info: null
      1 row in set (0.03 sec)

      The partitions that store infrequently accessed cold data, including p20200101000000, p20210101000000, p20220101000000, _p20230101000000, _p20240101000000, and _p20250101000000, have all been deleted.

    3. sales table schema is as follows:

      SHOW CREATE TABLE sales \G

      The result 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 DEFAULT 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(YEAR, 1) */
      /*!50500 (PARTITION _p20260101000000 VALUES LESS THAN ('2026-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20270101000000 VALUES LESS THAN ('2027-01-01 00:00:00') ENGINE = InnoDB,
       PARTITION _p20280101000000 VALUES LESS THAN ('2028-01-01 00:00:00') ENGINE = InnoDB) */
      1 row in set (0.02 sec)

Create or delete a DLM policy with ALTER TABLE

  • Create a DLM policy with ALTER TABLE.

    ALTER TABLE t DLM ADD POLICY test_policy TIER TO TABLE ENGINE=CSV STORAGE=OSS READ ONLY
    STORAGE TABLE_NAME = 'sales_history' ON (PARTITIONS OVER 3);

    The DLM policy for table t is named test_policy. The execution condition is that the number of partitions exceeds three. When this DLM policy runs and the number of partitions exceeds three, data in the older partitions of table t is archived to OSS, and the archived table in OSS is named sales_history.

  • For table t, enable the DLM policy test_policy.

    ALTER TABLE t DLM ENABLE POLICY test_policy;
  • For table t, disable the DLM policy test_policy.

    ALTER TABLE t DLM DISABLE POLICY test_policy;
  • For table t, delete the DLM policy test_policy.

    ALTER TABLE t DLM DROP POLICY test_policy;

Troubleshoot execution errors

A DLM policy can fail to run because of configuration issues. In this case, the error records are stored in the mysql.dlm_progress table. Run the following command to view the error records:

SELECT * FROM mysql.dlm_progress WHERE Archive_stage = "ARCHIVE_ERROR";

Find the error details in the Extra_info field. After you identify the cause, delete the current record, or change the Archive_stage of the current record to ARCHIVE_COMPLETE. Then run the call dbms_dlm.execute_all_dlm_policies; command to execute the DLM policy manually, or wait for the next execution cycle to run it automatically.

Note

If the execution records of the current policy contain a record in the ARCHIVE_ERROR state, the policy does not run automatically again for data security reasons. The policy continues to run only after you identify the cause of the failure and modify the corresponding record.