1. 程式人生 > >對分區表添加子分區

對分區表添加子分區

sql 提前 round global lec lis 再次 p s quest

一、需求

開發人員提出需求,有一個財務表,需要插入2019年新的數據進行報銷,但是分區表中沒有容納的分區,因此需要添加一個分區。 經過get_ddl語法,得到該表的創建語法。 現有情況:財務表A: 地市列表,報銷時間字段。 第一個分區part_a,第一個列表1,報銷時間2018年1月前的數據; 第二個分區Part_b,第一個列表2,報銷時間2018年1月前的數據; 第二個分區part_b,第一個列表2,報銷時間2018年9月份數據; ???此時,需要將2019年的報銷數據,插入第二個分區中,無法插入,報錯 ORA
-14400: inserted partition key does not map to any partition

二、模擬測試環境
1)在11.2.0.4版本中,模擬創建列表-列表,復合分區
2)創建不同類型的索引(全局索引,全局分區索引,本地索引),為了模擬添加分區後,是否會對索引造成影響

2.1測試表

create table range_list_part_tab(id number,deal_date date,area_code varchar2(12),contents varchar2(4000))
partition by range(area_code)
subpartition by range(deal_date)
(
partition p1 values less than(‘952‘)

(subpartition "part1_20181205" values less than (to_date(‘2018-12-16 00:00:00‘,‘YYYY-MM-DD HH24:MI:SS‘,‘NLS_CALENDAR=GREGORIAN‘)),
subpartition "part1_20190105" values less than (to_date(‘2019-01-05 00:00:00‘,‘YYYY-MM-DD HH24:MI:SS‘,‘NLS_CALENDAR=GREGORIAN‘))),
partition p2 values less than(‘970‘)
(subpartition "part2_20181205" values less than (to_date(‘2018-12-16 00:00:00‘,‘YYYY-MM-DD HH24:MI:SS‘,‘NLS_CALENDAR=GREGORIAN‘)))
)
;

select PARTITION_NAME,SUBPARTITION_COUNT,HIGH_VALUE,HIGH_VALUE_LENGTH,PARTITION_POSITION,
TABLESPACE_NAME from dba_tab_partitions where table_owner=‘SYS‘ and table_name=upper(‘range_list_part_tab‘)

PARTITION_ SUBPARTITION_COUNT HIGH_VALUE HIGH_VALUE_LENGTH PARTITION_POSITION TABLESPACE_NAME
---------- ------------------ -------------------- ----------------- ------------------ ------------------------------------------------------------
P1 3 ‘952‘ 5 1 SYSTEM
P2 1 ‘970‘ 5 2 SYSTEM

SYS@a11204>select PARTITION_NAME,SUBPARTITION_NAME,HIGH_VALUE,HIGH_VALUE_LENGTH,SUBPARTITION_POSITION,TABLESPACE_NAME from dba_tab_subpartitions where table_owner=‘SYS‘

and table_name=upper(‘range_list_part_tab‘);

PARTITION_ SUBPARTITION_NAME HIGH_VALUE HIGH_VALUE_LENGTH SUBPARTITION_POSITION TABLESPACE_NAME
---------- ------------------------------------------------------------ -------------------- ----------------- -------------------
P1 part1_20181205 TO_DATE(‘ 2018-12-16 83 1 SYSTEM

P1 part1_20190105 TO_DATE(‘ 2019-01-05 83 2 SYSTEM

P1 PART1_MAX MAXVALUE 8 3 SYSTEM
P2 part2_20181205 TO_DATE(‘ 2018-12-16 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) 83 1 SYSTEM

insert into range_list_part_tab values(20,sysdate,‘952‘,‘qq‘);
ERROR at line 1:
ORA-14400: inserted partition key does not map to any partition

insert into range_list_part_tab values(10,sysdate-365,‘953‘,‘weixin‘);

2.2 創建索引

create index IND_GLOBAL_A on range_list_part_tab(id);
create index IND_PART_GLOBAL_B on range_list_part_tab(deal_date) global partition by range(deal_date)
(partition p1 values less than (to_date(‘2018-12-16 00:00:00‘,‘YYYY-MM-DD HH24:MI:SS‘,‘NLS_CALENDAR=GREGORIAN‘)) tablespace system,
partition p2 values less than (to_date(‘2019-01-05 00:00:00‘,‘YYYY-MM-DD HH24:MI:SS‘,‘NLS_CALENDAR=GREGORIAN‘)) tablespace users,
partition p3 values less than (maxvalue) tablespace sysaux);
create index IND_PART_LOCAL_C on range_list_part_tab(area_code) local;

全局索引

SQL> select index_name,STATUS,GLOBAL_STATS,TABLESPACE_NAME from dba_indexes where owner=‘SYS‘ and index_name=‘IND_GLOBAL_A‘;
INDEX_NAME STATUS GLOBAL TABLESPACE_NAME
------------------------------ ---------------- ------ ------------------------------------------------------------
IND_GLOBAL_A VALID YES SYSTEM

全局本地索引

SYS@a11204>select index_name,PARTITIONING_TYPE,SUBPARTITIONING_TYPE,PARTITION_COUNT,LOCALITY,DEF_TABLESPACE_NAME from DBA_PART_INDEXES where index_name=‘IND_PART_GLOBAL_B‘;
INDEX_NAME PARTITIONING_TYPE SUBPARTITIONING_TY PARTITION_COUNT LOCALITY DEF_TABLESPACE_NAME
-------------------- ------------------ ------------------ --------------- ------------ ------------------------------------------------------------
IND_PART_GLOBAL_B RANGE NONE 3 GLOBAL SYSTEM

select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_PART_GLOBAL_B‘
INDEX_NAME PARTITION_ HIGH_VALUE STATUS TABLESPACE_NAME
-------------------- ---------- -------------------------------------------------------------------------------- ---------------- ------------------------------------------------------------
IND_PART_GLOBAL_B P1 TO_DATE(‘ 2018-12-16 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE SYSTEM
IND_PART_GLOBAL_B P2 TO_DATE(‘ 2019-01-05 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIAN‘) USABLE USERS
IND_PART_GLOBAL_B P3 MAXVALUE

select index_name,PARTITION_NAME,HIGH_VALUE,STATUS,TABLESPACE_NAME from dba_ind_partitions where index_owner=‘SYS‘ and index_name=‘IND_PART_LOCAL_C‘;
INDEX_NAME PARTITION_ HIGH_VALUE STATUS TABLESPACE_NAME
-------------------- ---------- -------------------- ---------------- ------------------------------------------------------------
IND_PART_LOCAL_C P1 ‘952‘ N/A
IND_PART_LOCAL_C P2 ‘970‘ N/A

本地索引

SELECT INDEX_NAME,PARTITION_NAME,SUBPARTITION_NAME,HIGH_VALUE,HIGH_VALUE_LENGTH,SUBPARTITION_POSITION,STATUS,TABLESPACE_NAME FROM DBA_IND_SUBPARTITIONS WHERE INDEX_OWNER=‘SYS‘ and INDEX_NAME=‘IND_PART_LOCAL_C‘
INDEX_NAME PARTITION_ SUBPARTITION_NAME HIGH_VALUE HIGH_VALUE_LENGTH SUBPARTITION_POSITION STATUS TABLESPACE_NAME
-------------------- ---------- -------------------- -------------------- ----------------- --------------------- ---------------- -----------------------------------------------------------
IND_PART_LOCAL_C P1 part1_20181205 TO_DATE(‘ 2018-12-16 83 1 USABLE SYSTEM
IND_PART_LOCAL_C P1 part1_20190105 TO_DATE(‘ 2019-01-05 83 2 USABLE SYSTEM
IND_PART_LOCAL_C P2 part2_20181205 TO_DATE(‘ 2018-12-16 83 1 USABLE SYSTEM

SYS@ceshi>select index_name,table_name,PARTITIONING_TYPE,SUBPARTITIONING_TYPE,PARTITION_COUNT,LOCALITY,DEF_TABLESPACE_NAME from DBA_PART_INDEXES where index_name=‘IND_PART_LOCAL_C‘;
INDEX_NAME TABLE_NAME PARTITIONING_TYPE SUBPARTITIONING_TY PARTITION_COUNT LOCALITY DEF_TABLESPACE_NAME
-------------------- ------------------------------ ------------------ ------------------ --------------- ------------ ------------------------------------------------------------
IND_PART_LOCAL_C RANGE_LIST_PART_TAB RANGE RANGE 2 LOCAL

三、添加子分區操作

ALTER TABLE range_list_part_tab
MODIFY PARTITION p1
ADD SUBPARTITION part1_max VALUES LESS THAN (MAXVALUE) ;

insert into range_list_part_tab values(20,sysdate,‘950‘,‘qq‘);
09:28:42 SYS@a11204>COMMIT;

再次查詢上述三個索引視圖:均為有效,因此可忽略。添加子分區對索引無影響。

***其它**************

刪除子分區

SYS@a11204>ALTER TABLE range_list_part_tab drop SUBPARTITION part1_max ;

Table altered.

刪除子分區,子分區數據均自動刪除

select * from range_list_part_tab

ID DEAL_DATE AREA_CODE CONTENTS
---------- ------------------- ------------------------ --------------------
10 2018-02-26 09:17:19 953 weixin

ALTER TABLE range_list_part_tab MODIFY PARTITION p1 ADD SUBPARTITION part1_max VALUES LESS THAN (MAXVALUE) update global index ;

***添加分區,申請的鎖資源*******************

session_1

insert into range_list_part_tab values(30,sysdate-90,‘940‘,‘en‘);

insert into range_list_part_tab values(80,‘2018-12-01‘,‘960‘,‘ha‘);

session_2

SQL> alter session set ddl_lock_timeout=600;

ALTER TABLE range_list_part_tab MODIFY PARTITION p1 ADD SUBPARTITION part1_max VALUES LESS THAN (MAXVALUE) ;

不受影響,並沒有對表申請很嚴重的鎖。

具體分區表添加子分區,申請的鎖資源,繼續測試。

session_1

10:57:33 SYS@a11204>LOCK TABLE range_list_part_tab IN EXCLUSIVE MODE;

session_2,被阻塞

SQL> SQL> ALTER TABLE range_list_part_tab MODIFY PARTITION p1 ADD SUBPARTITION part1_max VALUES LESS THAN (MAXVALUE) ;

查詢鎖模式類型

SQL> select sid,type,id1,lmode,request,ctime,block from v$Lock where type in(‘TX‘,‘TM‘);

SID TYPE ID1 LMODE REQUEST CTIME BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
135 TM 18 3 0 47 0
135 TM 98881 0 3 47 0
130 TM 98881 6 0 72 1
135 TX 262159 6 0 47 0

select owner,object_name,object_type,object_id from dba_objects where object_id in(‘18‘,‘98881‘)

OWNER OBJECT_NAME OBJECT_TYPE OBJECT_ID
-------------------- -------------------- -------------------- ----------
SYS RANGE_LIST_PART_TAB TABLE 98881
SYS OBJ$ TABLE 18

添加子分區的操作,需要申請RANGE_LIST_PART_TAB 表的TM3號鎖,測試表提前添加TM6號鎖,被阻塞。因此添加子分區的操作,等同於dml申請TM鎖需求,影響很小。

***********對列表子分區操作,無法執行update global維護索引操作,還好之前測試索引不受影響,添加子分區對索引無影響。

ALTER TABLE range_list_part_tab MODIFY PARTITION p1 ADD SUBPARTITION part1_max VALUES LESS THAN (MAXVALUE) update global indexes ;

*
ERROR at line 1:
ORA-14633: Index maintainence clause not allowed for ADD list subpartition to a
Composite partitioned table

  

對分區表添加子分區