You can drop a partition in a partitioned table as needed. When you drop a partition, the definition and data in the partition are also dropped.
Support for deleting a partition in a partitioned table
In Oracle-compatible mode of OceanBase Database:
For a partitioned table, you can drop a RANGE partition or a LIST partition, but you cannot drop a HASH partition.
For a subpartitioned table, you can drop a RANGE or LIST subpartition.
The following table describes the support for deleting a partition in a partitioned table in Oracle-compatible mode of OceanBase Database.
| Partitioned table | Partition type | Drop a partition | Drop a subpartition |
|---|---|---|---|
| Partitioned table | RANGE / LIST | Supported | - |
| Partitioned table | HASH | Not supported | - |
| Subpartitioned table | RANGE + RANGE / RANGE + LIST | Supported | Supported |
| Subpartitioned table | RANGE + HASH | Supported | Not supported |
| Subpartitioned table | LIST + RANGE / LIST + LIST | Supported | Supported |
| Subpartitioned table | LIST + HASH | Supported | Not supported |
| Subpartitioned table | HASH + RANGE / HASH + LIST | Not supported | Supported |
| Subpartitioned table | HASH + HASH | Not supported | Not supported |
Considerations
Before you drop a partition, note the following:
When you drop a partition, make sure that no active transaction or query is running on the partition. Otherwise, an error may be returned when you execute an SQL statement, or an unexpected situation may occur. You can query the
oceanbase.GV$OB_TRANSACTION_PARTICIPANTSview in thesystenant to obtain the status of transactions that are not yet completed.In Oracle-compatible mode, when you drop a partition in a partitioned table that has global indexes, you must specify the
UPDATE GLOBAL INDEXESclause in theALTER TABLEstatement. If you do not specify this clause, the global indexes on the partitioned table will be unavailable after the partition is dropped.For a table that is partitioned by RANGE or LIST (including partitioned and subpartitioned tables) and supports partition deletion, when you specify the
UPDATE GLOBAL INDEXESclause, 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 caused by index invalidation. Note that this optimization applies only to regular global indexes. The following scenarios cannot trigger this optimization:- The partitioned table is partitioned by HASH (even if the subpartitions are RANGE or LIST). In this case, you cannot truncate a subpartition.
- The partitioning key is an expression, not a regular column.
- For special global indexes such as full-text indexes and non-structured types, even if you specify the
UPDATE GLOBAL INDEXESclause, the indexes will be unavailable.
Drop a partition
You can drop a partition in a partitioned table or subpartitioned table as needed.
Syntax
ALTER TABLE table_name DROP PARTITION partition_name_list [UPDATE GLOBAL INDEXES];
partition_name_list:
partition_name [, partition_name ...]
Note
When dropping a partition, you can drop one or more partitions, but not all partitions.
Dropping a partition also drops the data in the partition. If you want to drop only the data, you can use the
TRUNCATEstatement.
Examples
Drop the
M202005andM202006partitions in thetbl1_rpartitioned table.obclient> CREATE TABLE tbl1_r(log_id INT,log_date DATE NOT NULL DEFAULT SYSDATE) PARTITION BY RANGE(log_date) (PARTITION M202001 VALUES LESS THAN(TO_DATE('2020/02/01','YYYY/MM/DD')) , PARTITION M202002 VALUES LESS THAN(TO_DATE('2020/03/01','YYYY/MM/DD')) , PARTITION M202003 VALUES LESS THAN(TO_DATE('2020/04/01','YYYY/MM/DD')) , PARTITION M202004 VALUES LESS THAN(TO_DATE('2020/05/01','YYYY/MM/DD')) , PARTITION M202005 VALUES LESS THAN(TO_DATE('2020/06/01','YYYY/MM/DD')) , PARTITION M202006 VALUES LESS THAN(TO_DATE('2020/07/01','YYYY/MM/DD')) , PARTITION M202007 VALUES LESS THAN(TO_DATE('2020/08/01','YYYY/MM/DD')) , PARTITION M202008 VALUES LESS THAN(TO_DATE('2020/09/01','YYYY/MM/DD')) , PARTITION M202009 VALUES LESS THAN(TO_DATE('2020/10/01','YYYY/MM/DD')) , PARTITION M202010 VALUES LESS THAN(TO_DATE('2020/11/01','YYYY/MM/DD')) , PARTITION M202011 VALUES LESS THAN(TO_DATE('2020/12/01','YYYY/MM/DD')) , PARTITION M202012 VALUES LESS THAN(TO_DATE('2021/01/01','YYYY/MM/DD')) , PARTITION MMAX VALUES LESS THAN (MAXVALUE) ); Query OK, 0 rows affected obclient> ALTER TABLE tbl1_r DROP PARTITION M202005,M202006; Query OK, 0 rows affectedDrop the
p0partition in thet2_f_rrpartitioned table.obclient> CREATE TABLE t2_f_rr(col1 INT,col2 INT) PARTITION BY RANGE(col1) SUBPARTITION BY RANGE(col2) (PARTITION p0 VALUES LESS THAN(100) (SUBPARTITION sp0 VALUES LESS THAN(2020), SUBPARTITION sp1 VALUES LESS THAN(2021) ), PARTITION p1 VALUES LESS THAN(200) (SUBPARTITION sp2 VALUES LESS THAN(2020), SUBPARTITION sp3 VALUES LESS THAN(2021), SUBPARTITION sp4 VALUES LESS THAN(2022) ) ); Query OK, 0 rows affected obclient> ALTER TABLE t2_f_rr DROP PARTITION p0; Query OK, 0 rows affected
Drop a subpartition
You can drop a subpartition in a subpartitioned table as needed. Dropping a subpartition also drops the definition and data in that subpartition.
Syntax
ALTER TABLE table_name DROP SUBPARTITION subpartition_name_list [ UPDATE GLOBAL INDEXES ];
subpartition_name_list:
subpartition_name[, subpartition_name ...]
Examples
Drop the sp5 and sp6 subpartitions in the p1 partition of the t2_f_rr subpartitioned table and update the global indexes.
obclient> CREATE TABLE t2_f_rr(col1 INT,col2 INT)
PARTITION BY RANGE(col1)
SUBPARTITION BY RANGE(col2)
(PARTITION p0 VALUES LESS THAN(100)
(SUBPARTITION sp0 VALUES LESS THAN(2020),
SUBPARTITION sp1 VALUES LESS THAN(2021)
),
PARTITION p1 VALUES LESS THAN(200)
(SUBPARTITION sp2 VALUES LESS THAN(2020),
SUBPARTITION sp3 VALUES LESS THAN(2021),
SUBPARTITION sp4 VALUES LESS THAN(2022),
SUBPARTITION sp5 VALUES LESS THAN(2023),
SUBPARTITION sp6 VALUES LESS THAN(2024)
)
);
Query OK, 0 rows affected
obclient> ALTER TABLE t2_f_rr DROP SUBPARTITION sp5,sp6 UPDATE GLOBAL INDEXES;
Query OK, 0 rows affected