1. 程式人生 > >Partition Tabel測試drop和truncate 分割槽對全域性和本地索引是否有影響

Partition Tabel測試drop和truncate 分割槽對全域性和本地索引是否有影響

建立分割槽表:
create table test10
(sal_date date not null,
sal_id number not null,
sal_row number(12) not null
)
partition by range(sal_date)
(partition p_1 values less than(to_date(‘2010-01-01’,’yyyy-mm-dd’)),
partition p_2 values less than(to_date(‘2011-01-01’,’yyyy-mm-dd’)),
partition p_3 values less than(to_date(‘2012-01-01’,’yyyy-mm-dd’)),
partition p_4 values less than(to_date(‘2013-01-01’,’yyyy-mm-dd’)),
partition p_5 values less than(to_date(‘2014-01-01’,’yyyy-mm-dd’)),
partition p_6 values less than(to_date(‘2015-01-01’,’yyyy-mm-dd’)),
partition p_7 values less than(to_date(‘2016-01-01’,’yyyy-mm-dd’)),
partition p_8 values less than(to_date(‘2017-01-01’,’yyyy-mm-dd’)),
partition p_9 values less than(to_date(‘2018-01-01’,’yyyy-mm-dd’)),
partition p_10 values less than(to_date(‘2019-01-01’,’yyyy-mm-dd’)),
partition p_11 values less than(to_date(‘2020-01-01’,’yyyy-mm-dd’)),
partition p_12 values less than(to_date(‘2021-01-01’,’yyyy-mm-dd’)),
partition p_13 values less than(to_date(‘2022-01-01’,’yyyy-mm-dd’)),
partition p_14 values less than(to_date(‘2023-01-01’,’yyyy-mm-dd’)),
partition p_15 values less than(maxvalue)
)

插入資料:
insert into test10 SELECT TRUNC(SYSDATE)-ROWNUM, dbms_random.random,ROWNUM FROM dual CONNECT BY LEVEL<=5000;

建立全域性索引:
create index test10_sal_id on test10(sal_id) global;

建立本地索引:
create index test10_sal_date on test10(sal_date) local;

查詢分割槽表索引狀態,均有效:
SQL> select index_name,status from dba_indexes where table_name=’TEST10’;
INDEX_NAME STATUS

TEST10_SAL_DATE N/A
TEST10_SAL_ID VALID

刪除test10的一個p_2分割槽:
SQL> alter table test10 truncate partition p_2;
Table truncated.

再次查詢索引狀態 Golbal索引已經失效了而local索引沒有影響:
SQL> select index_name,status from dba_indexes where table_name=’TEST10’;
INDEX_NAME STATUS

TEST10_SAL_DATE N/A
TEST10_SAL_ID UNUSABLE

分割槽進行drop的時候:
SQL> alter table test10 drop partition p_3;
Table altered.

全域性索引也會失效:
SQL> select index_name,status from dba_indexes where table_name=’TEST10’;
INDEX_NAME STATUS

TEST10_SAL_DATE N/A
TEST10_SAL_ID UNUSABLE

如下是避免由於分割槽表進行truncate和drop操作時發生global索引失效:
SQL> alter table test10 truncate partition p_5 update global indexes;
Table truncated.

全域性索引沒有失效:
SQL> select index_name,status from dba_indexes where table_name=’TEST10’;
INDEX_NAME STATUS

TEST10_SAL_DATE N/A
TEST10_SAL_ID VALID

drop分割槽表的時候全域性索引也沒有失效:
SQL> alter table test10 drop partition p_6 update global indexes;
Table altered.

SQL> select index_name,status from dba_indexes where table_name=’TEST10’;
INDEX_NAME STATUS

TEST10_SAL_DATE N/A
TEST10_SAL_ID VALID

總結:1:local index在truncate和drop Partition table的時候不會失效
2:global index在truncate和drop Partition table的時候加上update global indexes則global index會自動更新,否則會失效需要rebuild