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 the MySQL mode of OceanBase Database.
| Partitioned table | Partitioning type | Drop a partition | Drop a subpartition |
|---|---|---|---|
| Partitioned table | RANGE, RANGE COLUMNS, LIST, and LIST COLUMNS | Supported | - |
| Partitioned table | HASH and KEY | Not supported | - |
| Subpartitioned table | RANGE-RANGE, RANGE-RANGE COLUMNS, RANGE-LIST, and RANGE-LIST COLUMNS | Supported | Supported |
| Subpartitioned table | RANGE-HASH and RANGE-KEY | Supported | Not supported |
| Subpartitioned table | RANGE COLUMNS-RANGE, RANGE COLUMNS-RANGE COLUMNS, RANGE COLUMNS-LIST, and RANGE COLUMNS-LIST COLUMNS | Supported | Supported |
| Subpartitioned table | RANGE COLUMNS-HASH and RANGE COLUMNS-KEY | Supported | Not supported |
| Subpartitioned table | LIST-RANGE, LIST-RANGE COLUMNS, LIST-LIST, LIST-LIST COLUMNS, LIST-HASH, and LIST-KEY | Supported | Supported |
| Subpartitioned table | LIST-HASH and LIST-KEY | Supported | Not supported |
| Subpartitioned table | LIST COLUMNS-RANGE, LIST COLUMNS-RANGE COLUMNS, LIST COLUMNS-LIST, and LIST COLUMNS-LIST COLUMNS | Supported | Supported |
| Subpartitioned table | LIST COLUMNS-HASH and LIST COLUMNS-KEY | Supported | Not supported |
| Subpartitioned table | HASH-RANGE, HASH-RANGE COLUMNS, HASH-LIST, and HASH-LIST COLUMNS | Not supported | Supported |
| Subpartitioned table | HASH-HASH and HASH-KEY | Not supported | Not supported |
| Subpartitioned table | KEY-RANGE, KEY-RANGE COLUMNS, KEY-LIST, and KEY-LIST COLUMNS | Not supported | Supported |
| Subpartitioned table | KEY-HASH and KEY-KEY | Not supported | Not supported |
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- and 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:
Drop partitions
M202011andM202012from 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 affectedCreate a template-based RANGE COLUMNS-RANGE COLUMNS-subpartitioned table named
t_m_rcrcand drop partitionsp1andp2from 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.
Create a non-template-based RANGE-RANGE-partitioned table named t_f_rr and drop 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