您的位置:首页 > 运维架构

Partition Tabel测试drop和truncate 分区对全局和本地索引是否有影响

2015-07-02 11:00 776 查看
创建分区表:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: