- Atomic overwrite operation If you need to rewrite the data in a partition while ensuring that the data can be queried during the rewriting process, you can first create a temporary partition based on the original formal partition, and load the new data into the temporary partition. Then you can use the replace operation to atomically replace the original formal partition with the temporary partition. For atomic overwrite operations on non-partitioned tables, see ALTER TABLE - SWAP.
-
Adjust partition data query concurrency
If you need to modify the number of buckets for a partition, you can first create a temporary partition with the same partition range as the original formal partition and specify the new number of buckets. Then, you can use the
INSERT INTOcommand to load the data of the original formal partition into the temporary partition. Finally, you can use the replace operation to atomically replace the original formal partition with the temporary partition. -
Modify partitioning rules
If you want to modify a partitioning strategy, such as merging partitions or splitting a large partition into multiple smaller partitions, you can first create temporary partitions with the expected merged or split ranges. Then, you can use the
INSERT INTOcommand to load the data of the original formal partitions into the temporary partitions. Finally, you can use the replace operation to atomically replace the original formal partitions with the temporary partitions.
Create temporary partitions
You can create one or more partitions at a time by using the ALTER TABLE command.Syntax
Create a single temporary partition
Create multiple partitions at a time
Parameters
partition_desc: specifies the number of buckets and properties for temporary partitions, such as the number of replicas and the storage medium.
Examples
Create a temporary partitiontp1 in the table site_access and specify its range as [2020-01-01, 2020-02-01) by using the VALUES [(...), (...)] syntax.
tp2 in the table site_access and specify its upper bound as 2020-03-01 by using the VALUES LESS THAN (...) syntax. The database uses the upper bound of the previous temporary partition as the lower bound of this temporary partition, generating a temporary partition with the left-closed and right-open range of [2020-02-01, 2020-03-01).
tp3 in the table site_access, specify its upper bound as 2020-04-01 by using the VALUES LESS THAN (...) syntax, and specify the number of replicas as 1.
site_access by using the START (...) END (...) EVERY (...) syntax, and specify the range of these partitions as [2020-04-01, 2021-01-01) with a monthly partition granularity.
Usage notes
- The partition column for a temporary partition must be the same as the partition column for the original formal partition based on which you create the temporary partition and cannot be changed.
- The name of a temporary partition cannot be the same as the name of any formal partition or other temporary partition.
- The ranges of all temporary partitions in a table cannot overlap, but the ranges of a temporary partition and a formal partition can overlap.
Show temporary partitions
You can view the temporary partitions by using the SHOW TEMPORARY PARTITIONS command.Load data into temporary partitions
You can load data into one or more temporary partitions by using theINSERT INTO command, STREAM LOAD, or BROKER LOAD.
Load data by using INSERT INTO command
Example:
Load data by using STREAM LOAD
Example:Load data by using BROKER LOAD
Example:StorageCredentialParams represents a group of authentication parameters which vary depending on the authentication method you choose. For detailed syntax and parameter descriptions, see Broker Load.
Load data by using ROUTINE LOAD
Example:Query data in temporary partitions
You can use the SELECT statement to query data in specified temporary partitions.Replace original formal partitions with temporary partitions
You can use the ALTER TABLE statement to replace the original formal partition with a temporary partition, thereby creating a new formal partition.NOTE The original formal partition and temporary partition that you operated in the ALTER TABLE statement are deleted and cannot be recovered.
Syntax
Parameters
-
strict_range
Default value:
true. When this parameter is set totrue, the unions of ranges of all original formal partitions must be exactly the same as the unions of ranges of the temporary partitions that are used for replacement. When this parameter is set tofalse, you only need to make sure that the ranges of the new formal partitions do not overlap with other formal partitions after replacement.-
Example 1:
In the following example, the unions of the original formal partitions
p1,p2, andp3are the same as the unions of the temporary partitionstp1andtp2, and you can usetp1andtp2to replacep1,p2, andp3. -
Example 2:
In the following example, the union of ranges of the original formal partition differs from the union of ranges of the temporary partitions. If the value of the parameter
strict_rangeis set totrue, the temporary partitionstp1andtp2cannot replace the original formal partitionp1. If the value is set tofalse, and the ranges [10, 30) and [40, 50) of the temporary partitions do not overlap with other formal partitions, the temporary partitions can replace the original formal partition.
-
Example 1:
In the following example, the unions of the original formal partitions
-
use_temp_partition_name
Default value:
false. If the number of original formal partitions is the same as the number of temporary partitions used for replacement, the names of the new formal partitions remain unchanged after replacement when this parameter is set tofalse. When this parameter is set totrue, the names of the temporary partitions are used as the names of the new formal partitions after replacement. In the following example, the partition name of the new formal partition remainsp1after replacement when this parameter is set tofalse. However, its related data and properties are replaced with the data and properties of the temporary partitiontp1. When this parameter is set totrue, the partition name of the new formal partition is changed totp1after replacement. The original formal partitionp1no longer exists.If the number of formal partitions to be replaced is different from the number of temporary partitions used for replacement, and this parameter remains the default valuefalse, the valuefalseof this parameter is invalid. In the following example, after replacement, the name for the new formal partition is changed totp1, and the original formal partitionsp1andp2no longer exist.
Examples
Replace the original formal partitionp1 with the temporary partition tp1.
p2 and p3 with the temporary partitions tp2 and tp3.
p4 and p5 with the temporary partitions tp4 and tp5, and specify the parameters strict_range as false and use_temp_partition_name as true.
Usage notes
- When a table has temporary partitions, you cannot use the
ALTERcommand to perform Schema Change operations on the table. - When performing Schema Change operations on a table, you cannot add temporary partitions to the table.
Delete temporary partitions
Use the following command to delete the temporary partitiontp1.
- If you use the
DROPcommand to directly delete a database or table, you can recover the database or table within a limited time period by using theRECOVERcommand. However, temporary partitions cannot be recovered. - After using the
ALTERcommand to delete a formal partition, you can recover it within a limited time period by using theRECOVERcommand. Temporary partitions are not bound with formal partitions, so operations on temporary partitions do not affect formal partitions. - After using the
ALTERcommand to delete a temporary partition, you cannot recover it by using theRECOVERcommand. - When using the
TRUNCATEcommand to delete data in a table, the temporary partitions of the table are deleted and cannot be recovered. - When using the
TRUNCATEcommand to delete data in a formal partition, the temporary partitions are not affected. - The TRUNCATE command cannot be used to delete data in a temporary partition.