原因: 出现这个问题,可能有人move过表,或者disable 过索引。
1. alter table xxxxxx move tablespace xxxxxxx 命令后,索引就会失效。
2. alter index index_name unusable,命令使索引失效。
解决办法:
1. 重建索引才是解决这类问题的完全的方法。
alter index index_name rebuild (online);
或者alter index index_name rebuild;
2. 如果是分区索引只需要重建那个失效的分区 。
alter index index_name rebuild partition partition_name (online);
或者alter index index_name rebuild partition partition_name ;
3. 或者改变当前索引的名字。
说明:
1. alter session set skip_unusable_indexes=true;就可以在session级别跳过无效索引作查询。
2. 分区索引应适用user_ind_partitions。
3. 状态分4种:
N/A说明这个是分区索引需要查user_ind_partitions或者user_ind_subpartitions来确定每个分区是否可用;
VAILD说明这个索引可用;
UNUSABLE说明这个索引不可用;
USABLE 说明这个索引的分区是可用的。
4. 查询当前索引的状态:select distinct status from user_indexes;
5. 查询那个索引无效:select index_name from user_indexes where status <> 'valid';
6. 详细讲解可参考:http://www.sudu.cn/info/html/edu/20071225/20526.html。
====================================
Oracle删除分区的命令
alter table table_name drop partition partition_name;
删除06年相关分区后,发现存储过程运行速度特别慢。查看了相关SQL执行计划,发觉没有使用索引,而执行全表扫描。
查看索引状况
select * from user_indexes t where t.status!='VALID'
发觉分区表使用的是全局索引,删除分区会导致索引失效,状态变成UNUSABLE,注意分区索引的状态是N/A,这个是正常的。
我觉得既然是分区表还是本地索引管理比较方便,就重建索引
alter table table_name drop primary key;
drop index pk_table_name;
create unique index pk_table_name on table_name (columns) local tablespace tablespace_name;
alter table table_name add constraint pk_table_name primary key(columns);
建立索引后查看user_part_indexes,user_ind_partitions两个字典表,索引正常建立。建立本地索引后系统运行速度恢复正常。
相关数据字典
user_tables
user_part_tables
user_tab_partitions
user_indexes
user_part_indexes
user_ind_partitions