You can drop partitions from a partitioned table as needed.
Support for dropping partitions from partitioned tables
The following table describes the support for dropping partitions from partitioned and subpartitioned tables in MySQL-compatible mode of OceanBase Database.
Partitioned table type |
Partitioning type |
Drop partitions |
Drop subpartitions |
|---|---|---|---|
| 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 |
Instructions for dropping a partition
Impact of dropping a partition on global indexes
For tables using RANGE/RANGE COLUMNS or LIST/LIST COLUMNS partitions (including both partitions and subpartitions) that support partition deletion, the system does not rebuild global indexes. Instead, it maintains index validity via a lazy (deferred) maintenance strategy, merging and deleting data in the background. This approach prevents query disruptions or performance degradation caused by index invalidation. Note that this optimization only applies to regular global indexes. The following scenarios do not trigger this optimization:
- The partition is a HASH partition (even if the subpartition is RANGE/RANGE COLUMNS or LIST/LIST COLUMNS), and a drop partition operation is performed on the subpartition.
- The partitioning key is an expression, not a regular column.
Hidden tenant-level configuration item to control global index behavior during partition DDL
Starting from V4.3.5 BP2, OceanBase Database V4.3.5 introduces a tenant-level hidden configuration item _ob_enable_truncate_partition_preserve_global_index to control whether global indexes remain valid during TRUNCATE/DROP operations on a partition (referred to as partition DDL). For upgraded tenants, the default value of this hidden configuration item is False, while for newly created tenants, it 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 is DML-inhibited; other partitions can perform DML operations. |
Global indexes remain valid: DML operations can be performed on the main table and all index tables, and global indexes related to the main table remain valid. |
| False | Only the TRUNCATE/DROP partition is DML-inhibited; other partitions can perform DML operations. |
Global indexes become invalid: DML operations can be performed on the main table and all index tables, but global indexes related to the main table become invalid or are rebuilt. |
Examples:
Index remains valid.
obclient> ALTER SYSTEM SET _ob_enable_truncate_partition_preserve_global_index = True;Index becomes invalid.
obclient> ALTER SYSTEM SET _ob_enable_truncate_partition_preserve_global_index = False;
Impact of dropping a partition on concurrent DDL/DML/queries
Dropping a partition is an offline DDL operation. During the partition drop, 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 queries (SELECT) |
Unaffected and can proceed normally. |
Drop a partition
You can drop a partition from a RANGE-, RANGE COLUMNS-, LIST, or LIST COLUMNS-partitioned table. You cannot drop partitions from HASH- or KEY-partitioned tables.
The SQL syntax for dropping a partition is as follows:
ALTER TABLE table_name DROP PARTITION partition_name[, partition_name ...];
Note
You can drop multiple partitions at a time, but you cannot drop all partitions in a table.
Before you drop a partition, ensure that no active transactions or queries exist in this partition. Otherwise, SQL statement errors or exceptions may occur. You can query the
oceanbase.GV$OB_TRANSACTION_PARTICIPANTSview for the context status of unfinished transactions in thesystenant.When you drop a partition, the data in the partition is also dropped. If you want to drop only the data, use the
TRUNCATEstatement.When you drop a partition from a subpartitioned table, its definition, data, and subpartitions are also dropped.
Here are some examples:
Create a partitioned table named
tbl1_rand drop theM202011andM202012partitions from it.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 affectedCreate a template-based RANGE COLUMNS-RANGE COLUMNS-subpartitioned table named
t_m_rcrcand drop thep1 and p2partitions from it.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 affectedCreate a RANGE-RANGE-subpartitioned table named
t_f_rrand drop thep1partition from it.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 you drop a subpartition from a subpartitioned table, ensure that no active transactions or queries exist in this subpartition. Otherwise, SQL statement errors or exceptions may occur. You can query the
oceanbase.GV$OB_TRANSACTION_PARTICIPANTSview for the context status of unfinished transactions in thesystenant.If you drop a subpartition, its definition and data are also dropped.
If you want to drop multiple subpartitions at a time, they must belong to the same partition.
Here is an example of creating a non-template-based RANGE-RANGE-partitioned table named t_f_rr and dropping the sp6 and sp7 subpartitions from it:
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
