You can delete partitions from a partitioned table as needed.
Support for dropping partitions in partitioned tables
In MySQL mode of OceanBase Database, the support for dropping partitions in partitioned tables is as follows:
Partitioned table |
Partitioning type |
Drop partition |
Drop subpartition |
|---|---|---|---|
| Partitioned table |
|
Supported | - |
| Partitioned table |
|
Not supported | - |
| Subpartitioned table |
|
Supported | Supported |
| Subpartitioned table |
|
Supported | Not supported |
| Subpartitioned table |
|
Supported | Supported |
| Subpartitioned table |
|
Supported | Not supported |
| Subpartitioned table |
|
Supported | Supported |
| Subpartitioned table |
|
Supported | Not supported |
| Subpartitioned table |
|
Supported | Supported |
| Subpartitioned table |
|
Supported | Not supported |
| Subpartitioned table |
|
Not supported | Supported |
| Subpartitioned table |
|
Not supported | Not supported |
| Subpartitioned table |
|
Not supported | Supported |
| Subpartitioned table |
|
Not supported | Not supported |
Considerations
Impact on global indexes
For V4.3.5, starting from V4.3.5 BP2, when you drop a partition of a table that is partitioned by RANGE/RANGE COLUMNS, LIST/LIST COLUMNS (including partitioned tables and subpartitioned tables), the system does not rebuild the global indexes. Instead, it uses a lazy maintenance strategy to keep the indexes valid by deleting data in the background. This avoids query interruptions or performance degradation due to index invalidation. Note that this optimization applies only to regular global indexes. The following scenarios do not trigger this optimization:
- The primary partition is a HASH partition (even if the subpartition is RANGE/RANGE COLUMNS or LIST/LIST COLUMNS). In this case, dropping a subpartition is not allowed.
- The partitioning key is an expression, not a regular column.
- For full-text indexes or non-structured data types, the global indexes are unavailable.
Hidden tenant-level configuration item to control global index behavior during partition DDL
For V4.3.5, OceanBase Database introduces the tenant-level hidden configuration item _ob_enable_truncate_partition_preserve_global_index starting from V4.3.5 BP2. This configuration item controls whether global indexes remain valid during TRUNCATE or DROP operations on a partition (referred to as partition DDL). For upgraded tenants, the default value is False, and for newly created tenants, the default value is True. The behavior is as follows:
Configuration item value |
Behavior during partition DDL execution |
Result after partition DDL execution |
|---|---|---|
| True | Only the TRUNCATE/DROP partition does not allow DML operations. Other partitions of the table can perform DML operations. |
Global indexes remain valid: DML operations can be performed on the table and all index tables, and global indexes related to the table remain valid. |
| False | Only the TRUNCATE/DROP partition does not allow DML operations. Other partitions of the table can perform DML operations. |
Global indexes become invalid or are rebuilt: DML operations can be performed on the table and all index tables, but global indexes related to the table become invalid or are rebuilt. |
Examples:
Global index remains valid.
obclient> ALTER SYSTEM SET _ob_enable_truncate_partition_preserve_global_index = True;Global index becomes invalid.
obclient> ALTER SYSTEM SET _ob_enable_truncate_partition_preserve_global_index = False;
Impact on concurrent DDL/DML/queries
Dropping a partition is an offline DDL operation. During the operation, OceanBase Database adds an exclusive lock at the partition level on the target partition.
Operation type |
Impact during partition drop |
|---|---|
| Concurrent DDL | Prohibited or blocked. No other DDL operations on the same table can be executed concurrently. |
| Concurrent DML |
|
Concurrent query (SELECT) |
Not affected. Can be executed normally. |
Drop a partition
You can drop a partition from a partitioned table of the RANGE, RANGE COLUMNS, LIST, or LIST COLUMNS type. However, you cannot drop a partition from a partitioned table of the HASH or KEY type.
The SQL syntax for dropping a partition is as follows:
ALTER TABLE table_name DROP PARTITION partition_name[, partition_name ...];
Note
You can drop one or more partitions from a partitioned table, but you cannot drop all partitions in the table.
Before dropping a partition, make sure that there are no active transactions or queries on the partition. Otherwise, an error may be returned when you execute the SQL statement, or some unexpected situations may occur. In the
systenant, query theoceanbase.GV$OB_TRANSACTION_PARTICIPANTSview to obtain the status of ongoing transactions.When you drop a partition, the data in the partition is also dropped. If you want to drop only the data in the partition, you can execute the
TRUNCATEstatement.When you drop a partition from a subpartitioned table, the definition of the partition and the corresponding subpartitions and data in the subpartitions are also dropped.
Here are some examples:
Drop the
M202011andM202012partitions from thetbl1_rpartitioned table.obclient> CREATE TABLE tbl1_r (log_id BIGINT NOT NULL,log_value VARCHAR(50),log_date TIMESTAMP NOT NULL) PARTITION BY RANGE(UNIX_TIMESTAMP(log_date)) (PARTITION M202001 VALUES LESS THAN(UNIX_TIMESTAMP('2020/02/01')) , PARTITION M202002 VALUES LESS THAN(UNIX_TIMESTAMP('2020/03/01')) , PARTITION M202003 VALUES LESS THAN(UNIX_TIMESTAMP('2020/04/01')) , PARTITION M202004 VALUES LESS THAN(UNIX_TIMESTAMP('2020/05/01')) , PARTITION M202005 VALUES LESS THAN(UNIX_TIMESTAMP('2020/06/01')) , PARTITION M202006 VALUES LESS THAN(UNIX_TIMESTAMP('2020/07/01')) , PARTITION M202007 VALUES LESS THAN(UNIX_TIMESTAMP('2020/08/01')) , PARTITION M202008 VALUES LESS THAN(UNIX_TIMESTAMP('2020/09/01')) , PARTITION M202009 VALUES LESS THAN(UNIX_TIMESTAMP('2020/10/01')) , PARTITION M202010 VALUES LESS THAN(UNIX_TIMESTAMP('2020/11/01')) , PARTITION M202011 VALUES LESS THAN(UNIX_TIMESTAMP('2020/12/01')) , PARTITION M202012 VALUES LESS THAN(UNIX_TIMESTAMP('2021/01/01')) ); Query OK, 0 rows affected obclient> ALTER TABLE tbl1_r DROP PARTITION M202011,M202012; Query OK, 0 rows affectedDrop the
p1andp2partitions from thet_m_rcrcpartitioned table, which is a RANGE COLUMNS + RANGE COLUMNS partitioned table with a partitioning template.obclient> CREATE TABLE t_m_rcrc(col1 INT,col2 INT) PARTITION BY RANGE COLUMNS(col1) SUBPARTITION BY RANGE COLUMNS(col2) SUBPARTITION TEMPLATE (SUBPARTITION mp0 VALUES LESS THAN(1000), SUBPARTITION mp1 VALUES LESS THAN(2000), SUBPARTITION mp2 VALUES LESS THAN(3000) ) (PARTITION p0 VALUES LESS THAN(100), PARTITION p1 VALUES LESS THAN(200), PARTITION p2 VALUES LESS THAN(300) ); Query OK, 0 rows affected obclient> ALTER TABLE t_m_rcrc DROP PARTITION p1,p2; Query OK, 0 rows affectedDrop the
p1partition from thet_f_rrpartitioned table, which is a RANGE + RANGE partitioned table without a partitioning template.obclient> CREATE TABLE t_f_rr(col1 INT,col2 TIMESTAMP) PARTITION BY RANGE(col1) SUBPARTITION BY RANGE(UNIX_TIMESTAMP(col2)) (PARTITION p0 VALUES LESS THAN(100) (SUBPARTITION sp0 VALUES LESS THAN(UNIX_TIMESTAMP('2021/04/01')), SUBPARTITION sp1 VALUES LESS THAN(UNIX_TIMESTAMP('2021/07/01')), SUBPARTITION sp2 VALUES LESS THAN(UNIX_TIMESTAMP('2021/10/01')), SUBPARTITION sp3 VALUES LESS THAN(UNIX_TIMESTAMP('2022/01/01')) ), PARTITION p1 VALUES LESS THAN(200) (SUBPARTITION sp4 VALUES LESS THAN(UNIX_TIMESTAMP('2021/04/01')), SUBPARTITION sp5 VALUES LESS THAN(UNIX_TIMESTAMP('2021/07/01')), SUBPARTITION sp6 VALUES LESS THAN(UNIX_TIMESTAMP('2021/10/01')), SUBPARTITION sp7 VALUES LESS THAN(UNIX_TIMESTAMP('2022/01/01')) ) ); Query OK, 0 rows affected obclient> ALTER TABLE t_f_rr DROP PARTITION p1; Query OK, 0 rows affected
Drop a subpartition
The SQL syntax for dropping a subpartition is as follows:
ALTER TABLE table_name DROP SUBPARTITION subpartition_name[, subpartition_name ...];
Note
Before dropping a subpartition, make sure that there are no active transactions or queries on the subpartition. Otherwise, an error may be returned when you execute the SQL statement, or some unexpected situations may occur. In the
systenant, query theoceanbase.GV$OB_TRANSACTION_PARTICIPANTSview to obtain the status of ongoing transactions.When you drop a subpartition, the definition of the subpartition and the data in the subpartition are also dropped.
When you drop multiple subpartitions, they must belong to the same partition.
Here is an example of dropping the sp6 and sp7 subpartitions from the t_f_rr partitioned table, which is a RANGE + RANGE partitioned table without a partitioning template:
obclient> CREATE TABLE t_f_rr(col1 INT,col2 TIMESTAMP)
PARTITION BY RANGE(col1)
SUBPARTITION BY RANGE(UNIX_TIMESTAMP(col2))
(PARTITION p0 VALUES LESS THAN(100)
(SUBPARTITION sp0 VALUES LESS THAN(UNIX_TIMESTAMP('2021/04/01')),
SUBPARTITION sp1 VALUES LESS THAN(UNIX_TIMESTAMP('2021/07/01')),
SUBPARTITION sp2 VALUES LESS THAN(UNIX_TIMESTAMP('2021/10/01')),
SUBPARTITION sp3 VALUES LESS THAN(UNIX_TIMESTAMP('2022/01/01'))
),
PARTITION p1 VALUES LESS THAN(200)
(SUBPARTITION sp4 VALUES LESS THAN(UNIX_TIMESTAMP('2021/04/01')),
SUBPARTITION sp5 VALUES LESS THAN(UNIX_TIMESTAMP('2021/07/01')),
SUBPARTITION sp6 VALUES LESS THAN(UNIX_TIMESTAMP('2021/10/01')),
SUBPARTITION sp7 VALUES LESS THAN(UNIX_TIMESTAMP('2022/01/01'))
)
);
Query OK, 0 rows affected
obclient> ALTER TABLE t_f_rr DROP SUBPARTITION sp6,sp7;
Query OK, 0 rows affected
