您好,登錄后才能下訂單哦!
如何理解ADD和DROP分區,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
add和drop分區
語法:
ALTER TABLE t_pe_r
ADD PARTITION p4 VALUES LESS THAN (30 );
ALTER TABLE t_pe_l
ADD PARTITION p4 VALUES (30 );
ALTER TABLE t_pe_h
ADD PARTITION p3;
alter table t_pe_r drop partition p3;
限制:如果范圍分區使用maxvalue選項則報錯ORA-14074: 分區界限必須調整為高于最后一個分區界限
如果LIST分區使用了default選項則報錯ORA-14323: 在 DEFAULT 分區已存在時無法添加分區
drop 分區不能使用在HASH分區表中,報錯ORA-14255: 未按范圍, 組合范圍或列表方法對表進行分區
我們這里討論HASH、list、range 3方式下add partition和drop partition關于local索引,global索引和普通索引的狀態。
使用腳本
drop table t_pe_r ;
drop table t_pe_l;
drop table t_pe_h;
CREATE TABLE t_pe_r (i NUMBER, j NUMBER , f varchar2(20),k varchar2(20))
PARTITION BY RANGE(j)
(PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (20));
create index t_pe_r_n on t_pe_r(i);
create index t_pe_r_l on t_pe_r(j) local;
create index t_pe_r_g on t_pe_r(f)
GLOBAL PARTITION BY hash (f)
(partition pg1 ,
partition pg2);
CREATE TABLE t_pe_l (i NUMBER, j NUMBER , f varchar2(20),k varchar2(20))
PARTITION BY list (j)
(PARTITION p1 VALUES (10),
PARTITION p2 VALUES (20));
create index t_pe_l_n on t_pe_l(i);
create index t_pe_l_l on t_pe_l(j) local;
create index t_pe_l_g on t_pe_l(f)
GLOBAL PARTITION BY hash (f)
(partition pg1 ,
partition pg2);
CREATE TABLE t_pe_h (i NUMBER, j NUMBER , f varchar2(20),k varchar2(20))
PARTITION BY hash(j)
(PARTITION p1 ,
PARTITION p2 );
create index t_pe_h_n on t_pe_h(i);
create index t_pe_h_l on t_pe_h(j) local;
create index t_pe_h_g on t_pe_h(f)
GLOBAL PARTITION BY hash (f)
(partition pg1 ,
partition pg2);
insert into t_pe_r
values(2,5,'a','A');
insert into t_pe_r
values(1,15,'b','B');
insert into t_pe_l
values(2,10,'a','A');
insert into t_pe_l
values(1,20,'b','B');
insert into t_pe_h
values(2,5,'a','A');
insert into t_pe_h
values(1,15,'b','B');
insert into t_pe_h
values(3,25,'c','C');
select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_l';
select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_g';
select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_r_n';
select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_l';
select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_g';
select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_l_n';
select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_h_l';
select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_h_g';
select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_h_n';
下面進行添加
ALTER TABLE t_pe_r
ADD PARTITION p4 VALUES LESS THAN (30 );
ALTER TABLE t_pe_l
ADD PARTITION p4 VALUES (30 );
ALTER TABLE t_pe_h
ADD PARTITION p3;
然后查看索引狀態
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_R_L USABLE P2
T_PE_R_L USABLE P1
T_PE_R_L USABLE P4
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_R_G USABLE PG2
T_PE_R_G USABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_r_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_R_N VALID
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_L_L USABLE P2
T_PE_L_L USABLE P1
T_PE_L_L USABLE P4
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_L_G USABLE PG2
T_PE_L_G USABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_l_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_L_N VALID
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_h_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_H_L USABLE P2
T_PE_H_L UNUSABLE P1
T_PE_H_L UNUSABLE P3
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_h_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_H_G UNUSABLE PG2
T_PE_H_G UNUSABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_h_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_H_N UNUSABLE
可以看到實際上LIST和HASH的所有索引都沒有受到影響,而HASH分區則不同,所有的索引均失效,添加分區后通過HASH算法重新分布了行,那么應該ROWID也受到了影響,可以DUMP出來看看。
索引進行rebuild
alter index T_PE_H_L rebuild partition p1;
在進行HASH分區的加入分區時候最好如下:
ALTER TABLE t_pe_h
ADD PARTITION p3 update indexes; 加上UPDATE INDEXES,同時實際上HASH的分區個數應該是2的N次方,不然會分布不均勻。
然后我們測試下DROP partition,drop partition只能用于RANGE 和LIST分區方式,HASH分區不能使用:
You can drop partitions from range, list, or composite range-list partitioned tables. For hash-partitioned tables, or hash subpartitions of range-hash partitioned
tables, you must perform. a coalesce operation instead.
使用腳本:
alter table t_pe_r drop partition p1;
alter table t_pe_l drop partition p1;
然后觀察:
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_R_L USABLE P2
T_PE_R_L USABLE P4
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_R_G UNUSABLE PG2
T_PE_R_G UNUSABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_r_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_R_N UNUSABLE
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_L_L USABLE P2
T_PE_L_L USABLE P4
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_L_G UNUSABLE PG2
T_PE_L_G UNUSABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_l_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_L_N UNUSABLE
可以看到普通索引,全局索引均已經失效,但是本地索引卻不受影響。
如果我們帶上UPDATE INDEXES會怎么樣?
SQL> alter table t_pe_r drop partition p1 update indexes;
Table altered
SQL> alter table t_pe_l drop partition p1 update indexes;
Table altered
SQL>
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_R_L USABLE P2
T_PE_R_L USABLE P4
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_r_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_R_G USABLE PG2
T_PE_R_G USABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_r_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_R_N VALID
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_l';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_L_L USABLE P2
T_PE_L_L USABLE P4
SQL> select INDEX_NAME ,STATUS,a.partition_name from dba_ind_partitions a where lower(index_name)='t_pe_l_g';
INDEX_NAME STATUS PARTITION_NAME
------------------------------ -------- ------------------------------
T_PE_L_G USABLE PG2
T_PE_L_G USABLE PG1
SQL> select INDEX_NAME ,STATUS from dba_indexes where lower(index_name)='t_pe_l_n';
INDEX_NAME STATUS
------------------------------ --------
T_PE_L_N VALID
可以看到加上UPDATE INDEXES 就會自動重建失效的索引。
結論:
1、如果范圍分區使用maxvalue選項則報錯ORA-14074: 分區界限必須調整為高于最后一個分區界限
2、如果LIST分區使用了default選項則報錯ORA-14323: 在 DEFAULT 分區已存在時無法添加分區
3、drop 分區不能使用在HASH分區表中,報錯ORA-14255: 未按范圍, 組合范圍或列表方法對表進行分區,如果要減少一個HASH分區表中的分區需要用ALTER TABLE ... COALESCE PARTITION
4、HASH分區進行ADD PARTITION操作,普通索引,本地索引,全局索引都會失效,除非使用UPDATE INDEXES,但是LIST、RANGE分區不受影響
5、LIST,RANGE分區進行DROP PARTITION操作全局索引及普通索引會失效,但是LOCAL索引不受影響。除非使用UPDATE INDEXES.
6、如果想要為全局索引增加分區,那這個操作只能對HASH分區的全局有效,ORA-14640: 添加/合并索引分區操作只對散列分區的全局索引有效,但是DROP全局索引的分區對HASH\LIST\RANGE均有效。
關于如何理解ADD和DROP分區問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。