【动手实践】Oracle 12.2 新特性:自动的列表分区创建

时间:2022-05-03
本文章向大家介绍【动手实践】Oracle 12.2 新特性:自动的列表分区创建,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

2017年来了,我们要启动新的学习征程了。在过去我们一直思考,什么样的内容能够更帮助大家了解和学习到有用的知识?

这个『动手实践』栏目就是这样一个改进和尝试吧,一个小小的范例,几分钟的线上实践(感谢Oracle),就能帮助大家熟悉一个知识点,几个重要的命令。如此是否会有不一样的体验?试一试吧。


在Oracle Database 12.2 之前,如果使用列表分区,当插入的数据超过了分区列表值设定,则会抛出异常;而如果存在大量的列表值需要定义,则可能需要一一设置。

在12.2引入的新特性中 - Auto-List Partitioning 可以针对新的列表值,进行自动的分区创建,从而减少了维护的复杂性。

在文档中这样描述:

Partitioning: Auto-List Partitioning

The database automatically creates a separate (new) partition for every distinct partition key value of the table. Auto-list partitioning removes the management burden from the DBAs to manually maintain a list of partitioned tables for a large number of distinct key values that require individual partitions. It also automatically copes with the unplanned partition key values without the need of a DEFAULT partition.

通过以下测试来简单验证一下这个特性的表征,如果是常规的列表分区,在分区缺失时会遇到ORA-14400错误:

SQL> CREATE TABLE enmotech (
  2    PartID	     integer	     not null,
  3    CretTm	     date	     not null,
  4    PartCD	     varchar2(2)     not null
  5  ) partition by list (partcd) (
  6    partition pBJ values ('BJ'),
  7    partition pCD values ('CD'),
  8    partition pGZ values ('GZ'),
  9    partition pSH values ('SH')
 10  );

Table created.

SQL> insert into enmotech values (1, sysdate, 'KM');
insert into enmotech values (1, sysdate, 'KM')
            *
ERROR at line 1:
ORA-14400: inserted partition key does not map to any partition

当设置了automatic关键字之后,分区变更为自动管理:

drop table enmotech purge;

CREATE TABLE enmotech (
  PartID	integer		not null,
  CretTm	date		not null,
  PartCD	varchar2(2)	not null
) partition by list (partcd) automatic (
  partition pBJ values ('BJ'),
  partition pCD values ('CD'),
  partition pGZ values ('GZ'),
  partition pSH values ('SH')
);

当插入一条未定义的分区数据时,新的分区被自动创建:

SQL> insert into enmotech values (1, sysdate, 'KM');

1 row created.

SQL> select partition_name from   user_tab_partitions
  2  where  table_name = 'ENMOTECH';

PARTITION_NAME
----------------------------------------------------
PBJ
PCD
PGZ
PSH
SYS_P290

如果这个自动分片的分区名不符合你的命名规则,可以通过DDL语句去修改变更:

SQL> alter table enmotech rename partition SYS_P290 to pKM;

Table altered.

SQL> select partition_name from   user_tab_partitions
  2  where  table_name = 'ENMOTECH';

PARTITION_NAME
---------------------------------------------------
PBJ
PCD
PGZ
PKM
PSH

对于已有的分区定义,可以通过关键字 automatic 和 manual 来进行分区定义的调整:

alter table PEOPLE set partitioning automatic;

alter table PEOPLE set partitioning manual;

这是Oracle Database 12.2 分区特性的众多增强之一。

更为重要的是,在今天,虽然你还可能下载不到12.2的安装盘,但是在LiveSQL ( https://livesql.oracle.com )站点,你可以毫无障碍的测试这个新特性,以下是以上脚本在网站上的测试输出: