You can clear the data in a partition of a partitioned table but retain the partition structure.
Support for truncating a partition from a partitioned table
Take note of the following points in the Oracle mode of OceanBase Database:
You can truncate partitions only for RANGE- and LIST-partitioned tables. You cannot truncate partitions for HASH partitioned tables.
In the case of subpartitioned tables, you can truncate partitions only for RANGE-LIST partitioned tables.
The following table describes the support for truncating partitions from partitioned and subpartitioned tables in the Oracle mode of OceanBase Database.
| Partitioned table type | Partitioning type | Truncate partitions | Truncate subpartitions |
|---|---|---|---|
| Partitioned table | RANGE and LIST | Supported | - |
| Partitioned table | HASH | Not supported | - |
| Subpartitioned table | RANGE-RANGE and RANGE-LIST | Supported | Supported |
| Subpartitioned table | RANGE-HASH | Supported | Not supported |
| Subpartitioned table | LIST-RANGE and LIST-LIST | Supported | Supported |
| Subpartitioned table | LIST-HASH | Supported | Not supported |
| Subpartitioned table | HASH-RANGE and HASH-LIST | Not supported | Supported |
| Subpartitioned table | HASH-HASH | Not supported | Not supported |
Considerations
Before you truncate a partition, note that:
Before you truncate 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.In the Oracle mode of OceanBase Database, when you truncate a partition from a partitioned or subpartitioned table with global indexes, you must add the
UPDATE GLOBAL INDEXESkeyword to theALTER TABLEstatement to update global indexes. If theUPDATE GLOBAL INDEXESkeyword is not added, the global indexes of this partitioned table become unavailable after the partition is truncated.
Truncate a partition
You can truncate a partition from a partitioned or subpartitioned table as needed.
Syntax
ALTER TABLE table_name TRUNCATE PARTITION partition_name_list [UPDATE GLOBAL INDEXES];
partition_name_list:
partition_name [, partition_name ...]
Note
You can clear the data in one or more partitions by using the
TRUNCATEstatement.When you truncate a partition from a subpartitioned table, all data in this partition and its subpartitions is cleared.
Examples
Clear the data in the
M202001andM202002partitions of thetbl1_rpartitioned table, and update the global indexes.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 TRUNCATE PARTITION M202001,M202002 UPDATE GLOBAL INDEXES; Query OK, 0 rows affectedTruncate the
p0andp1partitions of the RANGE-LIST-subpartitioned tablet2_f_rl.obclient> ALTER TABLE t2_f_rl TRUNCATE PARTITION p0,p1;
Truncate a subpartition
You can truncate a partition from a subpartitioned table as needed.
Syntax
ALTER TABLE table_name TRUNCATE SUBPARTITION subpartition_name_list[ UPDATE GLOBAL INDEXES ];
subpartition_name_list:
subpartition_name[, subpartition_name ...]
Note
You can clear the data in one or more subpartitions by using the TRUNCATE statement.
Example
Truncate the sp3 and sp4 subpartitions from the p1 partition of the t2_f_lr subpartitioned table, and then update the global indexes.
obclient> CREATE TABLE t2_f_rl(col1 INT,col2 VARCHAR2(50))
PARTITION BY RANGE(col1)
SUBPARTITION BY LIST(col2)
(PARTITION p0 VALUES LESS THAN(100)
(SUBPARTITION sp0 VALUES('01'),
SUBPARTITION sp1 VALUES('02')
),
PARTITION p1 VALUES LESS THAN(200)
(SUBPARTITION sp2 VALUES('01'),
SUBPARTITION sp3 VALUES('02'),
SUBPARTITION sp4 VALUES('03')
)
);
Query OK, 0 rows affected
obclient> ALTER TABLE t2_f_rl TRUNCATE SUBPARTITION sp3,sp4 UPDATE GLOBAL INDEXES;
Query OK, 0 rows affected