關於 Oracle 分割槽索引的失效和重建
阿新 • • 發佈:2018-11-20
–建立測試表
SQL> create table t as select object_id,object_name from dba_objects;
表已建立。
SQL> select min(object_id),max(object_id) from t;
MIN(OBJECT_ID) MAX(OBJECT_ID)
-------------- --------------
2 76083
SQL> create table t_part(object_id int,object_name varchar2(1000)) partition by range(object_id)
2 (
3 partition p1 values less than (10000),
4 partition p2 values less than (20000),
5 partition p3 values less than (30000),
6 partition p4 values less than (40000),
7 partition pm values less than (maxvalue));
表已建立。
SQL> insert into t_part select * from t;
已建立72663行。
SQL> commit;
–建立本地分割槽索引
SQL> create index idx_part_local on t_part(object_name) local;
索引已建立。
–建立全域性非分割槽索引
SQL> create index idx_part_global on t_part(object_id) global;
索引已建立。
–刪除其中一個分割槽
SQL> alter table t_part drop partition p1;
表已更改。
–全域性非分割槽索引失效,本地分割槽索引沒有失效
SQL> select status,index_name from user_indexes s where index_name=‘IDX_PART_GLOBAL’;
STATUS INDEX_NAME
-------- ------------------------------
SQL> select status,index_name from user_ind_partitions s where index_name=‘IDX_PART_LOCAL’;
STATUS INDEX_NAME
-------- ------------------------------
USABLE IDX_PART_LOCAL
USABLE IDX_PART_LOCAL
USABLE IDX_PART_LOCAL
USABLE IDX_PART_LOCAL
–重建失效索引
SQL> alter index idx_part_global rebuild;
索引已更改。
–在刪除表分割槽的時候,可以通過以下命令進行索引重建
alter table t_part drop partition p2 update indexes;
–建立全域性分割槽索引
SQL> drop index idx_part_global;
索引已刪除。
SQL> CREATE INDEX idx_part_global_full ON t_part (object_id)
2 GLOBAL PARTITION BY RANGE (object_id)
3 (PARTITION p1 VALUES LESS THAN (10000),
4 PARTITION p2 VALUES LESS THAN (30000),
5 PARTITION p3 VALUES LESS THAN (MAXVALUE));
索引已建立。
–刪除其中一個分割槽
SQL> alter table t_part drop partition p3;
表已更改。
–全域性分割槽索引失效
SQL> select status,index_name from user_ind_partitions s where index_name=‘IDX_PART_GLOBAL_FULL’;
STATUS INDEX_NAME
-------- ------------------------------
UNUSABLE IDX_PART_GLOBAL_FULL
UNUSABLE IDX_PART_GLOBAL_FULL
UNUSABLE IDX_PART_GLOBAL_FULL
SQL> select /+index(t IDX_PART_LOCAL)/ * from t_part t where object_name = ‘/7f6c264c_IIOPAddress’;
OBJECT_ID OBJECT_NAME
---------- -----------------------------------
35031 /7f6c264c_IIOPAddress
35030 /7f6c264c_IIOPAddress
SQL> select /+index(t IDX_PART_GLOBAL_FULL)/ * from t_part t where object_id > 35000;
select /+index(t IDX_PART_GLOBAL_FULL)/ * from t_part t where object_id > 35000
*
第 1 行出現錯誤:
ORA-01502: 索引 ‘SCOTT.IDX_PART_GLOBAL_FULL’ 或這類索引的分割槽處於不可用狀態
當需要對分割槽表進行下面操作時,都會導致全域性索引的失效。
ADD (HASH) COALESCE (HASH) DROP EXCHANGE MERGE MOVE SPLIT TRUNCATE
之後需要對失效索引進行重建,也可以在刪除分割槽表的時候指定 UPDATE INDEXES 直接進行索引的重建。
<p class="translate">
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-1992534/,如需轉載,請註明出處,否則將追究法律責任。
</p>
</div>