【動手實踐】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 )站點,你可以毫無障礙的測試這個新特性,以下是以上腳本在網站上的測試輸出:
快來一起體驗吧。
文章轉自數據和雲公眾號,原文鏈接
最後更新:2017-07-18 10:33:10