oracle数据库中有一个表,用pl/sql查看该表的索引没有被drop掉,, 但是表上的数据查询起来很慢(查询时间大概是原来的3倍),后来重建了一下索引就好了, 请问这是为什么, 在什么情况下会出现类似的索引丢失的情况?
可能是你的表经常被更新,碎片太多,索引占用空间太大,优化器觉得没有必要用索引了就直接全表扫描了啊,你重新建立索引,就整理了碎片了啊,当然就又用索引了.
索引失效的情况很多,比如左边使用了函数 ,表没有分析 ,导致索引扫描的cost高于全表扫描,表很小 ,等等。需要具体分析。你可以根据执行计划来判断.
以下情况会导致索引失效:
1) 直接导入:
imp with skip_unusable_indexes=y
or sqlldr with skip_index_maintenance
2) 在索引维护过程中出现ora-1652/1653错误:
sqlldr direct=y failes with ora-1652 or 1653
3) 分区维护导致rowid发生改变:
alter table move partition
alter table truncate partition
alter table split partition
索引失效问题解决方法:
1)导致的原因:
在sql*loader 加载过程中会维护索引,由于数据量比较大,在sql*loader 加载过程中出现异常情况,导致oracle 来不及维护索引,导致索引处于失效状态,影响查询和加载。
异常情况主要有:在加载过程中杀掉sql*loader 进程,重启,表空间不够等。
2)解决方法:
重建索引
3)如何重建索引
a) 查看索引类型
select t1.index_name,t1.partitioned from dba_indexes t1
where t1.table_name=upper('ccb_cognos_prod_balance_aa')
索引名称 是否分区索引
gnos_prod_balance_aa_n1 no
b)非分区索引
重建索引:alter index cin.ccb_cognos_prod_balance_aa_n1 rebuild nologging
c)分区索引
找出失效的分区索引:
select t.index_name, t.partition_name, t.tablespace_name, t.status
from dba_ind_partitions t
where t.index_name = 'cmz_local_idx_2'
重建所有状态为unusable的索引
alter index 索引名
rebuild partition 分区名
tablespace 表空间名
nologging
oracle索引失效解决方案
最近碰到这样一个问题:在prod_parts表中新添加了一个索引:
create index idx_pt_dv_id on prod_parts (device_id);
但是在使用device_id字段进行查询时,发现该索引并没有被利用到:
select * from prod_parts where device_id =122511619;
执行计划:
table access full
之后请教dba后,发现是数据统计的问题,具体的解决办法是执行下面的语句:
analyze table prod_parts compute statistics;
analyze table prod_parts compute statistics for all indexed columns;
analyze table prod_parts compute statistics for table for all indexes for all indexed columns;
