溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何理解ADD和DROP分區(qū)

發(fā)布時間:2021-11-05 18:10:44 來源:億速云 閱讀:157 作者:柒染 欄目:建站服務器

如何理解ADD和DROP分區(qū),針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

add和drop分區(qū)
語法:
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;

限制:如果范圍分區(qū)使用maxvalue選項則報錯ORA-14074: 分區(qū)界限必須調(diào)整為高于最后一個分區(qū)界限
      如果LIST分區(qū)使用了default選項則報錯ORA-14323: 在 DEFAULT 分區(qū)已存在時無法添加分區(qū)
      drop 分區(qū)不能使用在HASH分區(qū)表中,報錯ORA-14255: 未按范圍, 組合范圍或列表方法對表進行分區(qū)

我們這里討論HASH、list、range 3方式下add partition和drop partition關于local索引,global索引和普通索引的狀態(tài)。
使用腳本
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;

然后查看索引狀態(tài)
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分區(qū)則不同,所有的索引均失效,添加分區(qū)后通過HASH算法重新分布了行,那么應該ROWID也受到了影響,可以DUMP出來看看。
索引進行rebuild
alter index T_PE_H_L rebuild  partition p1;
在進行HASH分區(qū)的加入分區(qū)時候最好如下:
ALTER TABLE t_pe_h
      ADD PARTITION p3 update indexes; 加上UPDATE INDEXES,同時實際上HASH的分區(qū)個數(shù)應該是2的N次方,不然會分布不均勻。
然后我們測試下DROP partition,drop partition只能用于RANGE 和LIST分區(qū)方式,HASH分區(qū)不能使用:
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
可以看到普通索引,全局索引均已經(jīng)失效,但是本地索引卻不受影響。
如果我們帶上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 就會自動重建失效的索引。
結(jié)論:
1、如果范圍分區(qū)使用maxvalue選項則報錯ORA-14074: 分區(qū)界限必須調(diào)整為高于最后一個分區(qū)界限
2、如果LIST分區(qū)使用了default選項則報錯ORA-14323: 在 DEFAULT 分區(qū)已存在時無法添加分區(qū)
3、drop 分區(qū)不能使用在HASH分區(qū)表中,報錯ORA-14255: 未按范圍, 組合范圍或列表方法對表進行分區(qū),如果要減少一個HASH分區(qū)表中的分區(qū)需要用ALTER TABLE ... COALESCE PARTITION
4、HASH分區(qū)進行ADD PARTITION操作,普通索引,本地索引,全局索引都會失效,除非使用UPDATE INDEXES,但是LIST、RANGE分區(qū)不受影響
5、LIST,RANGE分區(qū)進行DROP PARTITION操作全局索引及普通索引會失效,但是LOCAL索引不受影響。除非使用UPDATE INDEXES.
6、如果想要為全局索引增加分區(qū),那這個操作只能對HASH分區(qū)的全局有效,ORA-14640: 添加/合并索引分區(qū)操作只對散列分區(qū)的全局索引有效,但是DROP全局索引的分區(qū)對HASH\LIST\RANGE均有效。

關于如何理解ADD和DROP分區(qū)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI