---
title: 管理二级分区表-V3.2.4-OceanBase 数据库文档-分布式数据库使用文档
description: 分布式数据库OceanBase 企业版文档中心，提供关于管理二级分区表的相关内容，方便用户更好的应用分布式数据库OceanBaseOceanBase 数据库
image: https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*OSPzQ6GUQF4AAAAAQHAAAAgAeiGDAQ/original
---
[OceanBase AI 数据大会](https://www.oceanbase.com/conference2026 "OceanBase AI 数据大会")文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 3.2.4 企业版 LTS

# 管理二级分区表

更新时间：2026-03-09 20:25:39

二级分区表创建成功后，您可以对二级分区表进行添加、删除或 Truncate 操作。

## 添加一级分区

### 语法

二级分区表添加一级分区主要有为模板化二级分区表和非模板化二级分区添加一级分区表两种场景。

- 模板化二级分区表

  ```sql
  ALTER TABLE table_name ADD PARTITION partition_options;

  partition_options:
    {PARTITION partition_name VALUES LESS THAN range_partition_expr
       [,PARTITION partition_name VALUES LESS THAN range_partition_expr]... }
   |{PARTITION partition_name VALUES IN list_partition_expr
       [,PARTITION partition_name VALUES IN list_partition_expr]...}

  ```

  > **说明**
  >
  >  
  >
  > 对于模板化二级分区表，添加一级分区时只需要指定一级分区的定义即可，二级分区的定义会⾃动按照模板填充。
 - 非模板化二级分区表

  partition_options:
    {PARTITION partition_name VALUES LESS THAN range_partition_expr (subpartition_option)
       [,PARTITION partition_name VALUES LESS THAN range_partition_expr (subpartition_option)]... }
   |{PARTITION partition_name VALUES IN list_partition_expr (subpartition_option)
       [,PARTITION partition_name VALUES IN list_partition_expr (subpartition_option)]...}

  subpartition_option：
    {SUBPARTITION subpartition_name VALUES LESS THAN range_partition_expr, ...}
   |{SUBPARTITION subpartition_name VALUES IN list_partition_expr, ....}
   |{SUBPARTITION subpartition_name, ....}

  ```

  > **说明**
  >
  >  
  >
  > 对于非模板化二级分区表，添加一级分区时，需要同时指定一级分区的定义和该一级分区下的二级分区定义。

### 示例

- 向 Range Columns + Range Columns 模板化分区表 `t_m_rcrc` 中添加一级分区 `p3` 和 `p4`。

  ```sql
  obclient> ALTER TABLE t_m_rcrc ADD PARTITION
         (PARTITION p3 VALUES LESS THAN(400),
          PARTITION p4 VALUES LESS THAN(500)
         );
  Query OK, 0 rows affected

  ```

  查看分区表 `t_m_rcrc` 中添加一级分区 `p3` 和 `p4`。

  ```sql
  obclient> show create table t_m_rcrc\G
  *************************** 1. row ***************************
         Table: t_m_rcrc
  Create Table: CREATE TABLE `t_m_rcrc` (
    `col1` int(11) DEFAULT NULL,
    `col2` int(11) DEFAULT NULL
  ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
   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),
  partition p3 values less than (400),
  partition p4 values less than (500))
  1 row in set

  ```

  插入数据到 `t_m_rcrc` 表的 `p3` 和 `p4` 分区。

  ```unknow
  obclient> INSERT INTO t_m_rcrc VALUES (399,399),(499,499);
  Query OK, 2 row affected

  ```

  查看已新建的分区表 `t_m_rcrc` 的 `p3` 和 `p4` 分区。

  ```unknow
  obclient>  SELECT * FROM t_m_rcrc partition(p3,p4);
  +------+------+
  | col1 | col2 |
  +------+------+
  |  399 |  399 |
  |  499 |  499 |
  +------+------+
  2 rows in set

  ```
 - 向 Range + Range 非模板化分区表 `t2_f_rclc` 中添加一级分区 `p2`。

  ```sql
  obclient> ALTER TABLE t2_f_rclc ADD PARTITION
         (PARTITION p2 VALUES LESS THAN(300)
           (SUBPARTITION sp6 VALUES IN(1,3),
            SUBPARTITION sp7 VALUES IN(4,6),
            SUBPARTITION sp8 VALUES IN(7,9))
         );
  Query OK, 0 rows affected

  ```

  查看分区表 `t2_f_rclc` 中添加一级分区 `p6` 和 `p7` 、`p8`。

  ```sql
  obclient> show create table t2_f_rclc\G
  *************************** 1. row ***************************
         Table: t2_f_rclc
  Create Table: CREATE TABLE `t2_f_rclc` (
    `col1` int(11) DEFAULT NULL,
    `col2` int(11) DEFAULT NULL
  ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
   partition by range columns(col1) subpartition by list columns(col2)
  (partition p0 values less than (100) (
  subpartition sp0 values in (1,3),
  subpartition sp1 values in (4,6),
  subpartition sp2 values in (7,9)),
  partition p1 values less than (200) (
  subpartition sp3 values in (1,3),
  subpartition sp4 values in (4,6),
  subpartition sp5 values in (7,9)),
  partition p2 values less than (300) (
  subpartition sp6 values in (1,3),
  subpartition sp7 values in (4,6),
  subpartition sp8 values in (7,9)))
  1 row in set

  ```

  插入数据到 `t2_f_rclc` 表的 `p6` 和 `p7`、`p8` 分区。

  ```unknow
  obclient> INSERT INTO t2_f_rclc VALUES (299,3),(299,6),(299,9);
  Query OK, 1 row affected

  ```

  查看已新建的分区表 `t2_f_rclc` 表的 `p6` 和 `p7`、`p8` 分区。

  ```unknow
  obclient> SELECT * FROM t2_f_rclc partition(sp6,sp7,sp8);
  +------+------+
  | col1 | col2 |
  +------+------+
  |  299 |    3 |
  |  299 |    6 |
  |  299 |    9 |
  +------+------+
  3 rows in set

  ```

## 添加二级分区

当前 MySQL 模式暂不支持向表中添加二级分区。

## 删除一级分区

### 语法

```sql
ALTER TABLE table_name DROP PARTITION partition_name[, partition_name ...];

```

> **说明**
>
>  
>
> - 删除二级分区表中的一级分区时，请尽量避免该分区上存在活动的事务或查询，否则可能会导致 SQL 语句报错，或者出现一些异常情况。在 `sys` 租户下，通过事务状态表 `__all_virtual_trans_stat` 可以查询到当前还未结束的事务上下文状态。
>  - 删除一级分区会同时删除该一级分区的定义和其对应的二级分区及数据。

### 示例

1. 删除 Range Columns + Range Columns 模板化分区表 `t_m_rcrc` 中的一级分区 `p3` 和 `p4`。

      1. 查看 `t_m_rcrc` 分区表。

        ```sql
        obclient> show create table t_m_rcrc\G
        *************************** 1. row ***************************
               Table: t_m_rcrc
        Create Table: CREATE TABLE `t_m_rcrc` (
          `col1` int(11) DEFAULT NULL,
          `col2` int(11) DEFAULT NULL
        ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
         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),
        partition p3 values less than (400),
        partition p4 values less than (500))
        1 row in set (0.00 sec)

        ```
      2. 删除 `t_m_rcrc` 中的一级分区 `p3` 和 `p4`。

        ```unknow
        obclient> ALTER TABLE t_m_rcrc DROP PARTITION p3,p4;
        Query OK, 0 rows affected

        ```
      3. 查看运行结果。

        ```unknow
        obclient> show create table t_m_rcrc\G
        *************************** 1. row ***************************
               Table: t_m_rcrc
        Create Table: CREATE TABLE `t_m_rcrc` (
          `col1` int(11) DEFAULT NULL,
          `col2` int(11) DEFAULT NULL
        ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
         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),
        partition p3 values less than (400),
        partition p4 values less than (500))
        1 row in set (0.00 sec)

        ```
 2. 删除 Range + Range 非模板化分区表 `t2_f_rr` 中的一级分区 `p2`。

        ```unknow
        obclient> show create table t2_f_rr\G
        *************************** 1. row ***************************
               Table: t2_f_rr
        Create Table: CREATE TABLE `t2_f_rr` (
          `col1` int(11) DEFAULT NULL,
          `col2` timestamp NULL DEFAULT NULL
        ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
         partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2))
        (partition p0 values less than (100) (
        subpartition sp0 values less than (1617206400),
        subpartition sp1 values less than (1625068800),
        subpartition sp2 values less than (1633017600),
        subpartition sp3 values less than (1640966400)),
        partition p1 values less than (200) (
        subpartition sp4 values less than (1617206400),
        subpartition sp5 values less than (1625068800),
        subpartition sp6 values less than (1633017600),
        subpartition sp7 values less than (1643644800)),
        partition p2 values less than (300) (
        subpartition sp8 values less than (1617206400),
        subpartition sp9 values less than (1625068800),
        subpartition sp10 values less than (1633017600),
        subpartition sp11 values less than (1643644800)))
        1 row in set (0.00 sec)

        ```
      2. 删除 `t2_f_rr` 中的一级分区 `p2`。

        ```unknow
        obclient> ALTER TABLE t2_f_rr DROP PARTITION P2;
        Query OK, 0 rows affected

        ```sql
        obclient> show create table t2_f_rr\G
        *************************** 1. row ***************************
               Table: t2_f_rr
        Create Table: CREATE TABLE `t2_f_rr` (
          `col1` int(11) DEFAULT NULL,
          `col2` timestamp NULL DEFAULT NULL
        ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
         partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2))
        (partition p0 values less than (100) (
        subpartition sp0 values less than (1617206400),
        subpartition sp1 values less than (1625068800),
        subpartition sp2 values less than (1633017600),
        subpartition sp3 values less than (1640966400)),
        partition p1 values less than (200) (
        subpartition sp4 values less than (1617206400),
        subpartition sp5 values less than (1625068800),
        subpartition sp6 values less than (1633017600),
        subpartition sp7 values less than (1643644800)))
        1 row in set (0.00 sec)

        ```

## 删除二级分区

### 语法

```sql
ALTER TABLE table_name DROP SUBPARTITION subpartition_name[, subpartition_name ...];

```

> **说明**
>
>  
>
> - 删除二级分区表中的二级分区时，请尽量避免该分区上存在活动的事务或查询，否则可能会导致 SQL 语句报错，或者出现一些异常情况。在 `sys` 租户下，通过事务状态表 `__all_virtual_trans_stat` 可以查询到当前还未结束的事务上下文状态。
>  - 删除二级分区会同时删除该分区的定义和其中的数据。
>  - 当删除多个二级分区时，这些二级分区必须属于同一个一级分区。

### 示例

删除 Range + Range 非模板化分区表 `t2_f_rr` 中的二级分区 `sp6` 和 `sp7`。

1. 查看 `t2_f_rr` 分区表。

   ```unknow
   obclient> show create table t2_f_rr\G
   *************************** 1. row ***************************
          Table: t2_f_rr
   Create Table: CREATE TABLE `t2_f_rr` (
     `col1` int(11) DEFAULT NULL,
     `col2` timestamp NULL DEFAULT NULL
   ) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
    partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2))
   (partition p0 values less than (100) (
   subpartition sp0 values less than (1617206400),
   subpartition sp1 values less than (1625068800),
   subpartition sp2 values less than (1633017600),
   subpartition sp3 values less than (1640966400)),
   partition p1 values less than (200) (
   subpartition sp4 values less than (1617206400),
   subpartition sp5 values less than (1625068800),
   subpartition sp6 values less than (1633017600),
   subpartition sp7 values less than (1643644800)))
   1 row in set (0.00 sec)

   ```
 2. `t2_f_rr` 中的二级分区 `sp6` 和 `sp7`。

   ```unknow
   obclient> ALTER TABLE t2_f_rr DROP SUBPARTITION sp6,sp7;
   Query OK, 0 rows affected

```sql
obclient> show create table t2_f_rr\G
*************************** 1. row ***************************
       Table: t2_f_rr
Create Table: CREATE TABLE `t2_f_rr` (
  `col1` int(11) DEFAULT NULL,
  `col2` timestamp NULL DEFAULT NULL
) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = 'zstd_1.3.8' REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
 partition by range(col1) subpartition by range(UNIX_TIMESTAMP(col2))
(partition p0 values less than (100) (
subpartition sp0 values less than (39283200),
subpartition sp1 values less than (47145600),
subpartition sp2 values less than (55094400),
subpartition sp3 values less than (63043200)),
partition p1 values less than (200) (
subpartition sp4 values less than (39283200),
subpartition sp5 values less than (47145600)))
1 row in set

```

## Truncate 一级分区

### 语法

```sql
ALTER TABLE table_name TRUNCATE PARTITION partition_name[, partition_name ...];

```

> **说明**
>
>  
>
> - Truncate 二级分区表中的一级分区时，请尽量避免该分区上存在活动的事务或查询，否则可能会导致 SQL 语句报错，或者出现一些异常情况。在 `sys` 租户下，通过事务状态表 `__all_virtual_trans_stat` 可以查询到当前还未结束的事务上下文状态。
>  - OceanBases 数据库当前支持对 Range/List [Columns] 类型（组合）的二级分区表中的一级分区执行 Truncate 操作，将一个或多个一级分区中对应的二级分区的数据全部移除。

### 示例

清空 Range Columns + List Columns 分区表 `t2_f_rclc` 的一级分区 `p0` 中的数据。

1. `t2_f_rclc` 的一级分区 `p0` 中的数据。

   ```unknow
   obclient> select * from t2_f_rclc partition(p0);
   +------+------+
   | col1 | col2 |
   +------+------+
   |    1 |    6 |
   +------+------+
   1 row in set (0.01 sec)

   ```
 2. 删除 `t2_f_rclc` 的一级分区 `p0` 中的数据。

   ```unknow
   obclient> ALTER TABLE t2_f_rclc TRUNCATE PARTITION p0;
   Query OK, 0 rows affected

   ```unknow
   obclient> select * from t2_f_rclc partition(p0);
   Empty set (0.01 sec)

   ```

## Truncate 二级分区

### 语法

```sql
ALTER TABLE table_name TRUNCATE SUBPARTITION subpartition_name[, subpartition_name ...];

```

> **说明**
>
>  
>
> - Truncate 二级分区表中的二级分区时，请尽量避免该分区上存在活动的事务或查询，否则可能会导致 SQL 语句报错，或者出现一些异常情况。在 `sys` 租户下，通过事务状态表 `__all_virtual_trans_stat` 可以查询到当前还未结束的事务上下文状态。
>  - OceanBases 数据库支持对 Range/List [Columns] 类型（组合）的二级分区表中的二级分区执行 Truncate 操作，将一个或多个二级分区中的数据全部移除。
>  - 当清除多个二级分区时，这些二级分区必须属于同一个一级分区。

### 示例

清空 Range + Range 分区表 `t2_f_rr` 的二级分区 `sp1` 和 `sp2` 中的数据。

1. 查看 `t2_f_rr` 的二级分区 sp1 和 sp2 中的数据。

   ```unknow
   obclient> select * from t2_f_rr partition(sp1,sp2);
   +------+---------------------+
   | col1 | col2                |
   +------+---------------------+
   |   10 | 2021-07-01 00:00:00 |
   |   11 | 2021-09-30 00:00:00 |
   +------+---------------------+
   2 rows in set (0.00 sec)

   ```
 2. 清空 `t2_f_rr` 的二级分区 sp1 和 sp2 中的数据。

   ```unknow
   obclient> ALTER TABLE t2_f_rr TRUNCATE SUBPARTITION sp1,sp2;
   Query OK, 0 rows affected

   ```unknow
   obclient> select * from t2_f_rr partition(sp1,sp2);
   Empty set (0.01 sec)

   ```

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ) 咨询
