重建索引的前提
表上频繁发生update,delete操作;
表上发生了alter table …move操作(move操作导致了rowid变化)。
索引分析
一般来讲可以采用以下三种方式来手工分析索引。
-
analyze index idx_t validate structure:
这段分析语句是用来分析索引的block中是否有坏块儿,那么根据分析我们可以得到索引的结构数据,这些数据会保留到
index_stats中,来判断这个索引是否需要rebuild. 需要注意的是这样的分析是不会收集索引的统计信息的。 -
validate structure有二种模式: online, offline, 一般来讲默认的方式是offline。
当以offline的模式analyze索引时,会对table加一个表级共享锁,对目前table的一些实时DMl操作会产生一定的影响。
而以online模式分析时候,则不会加任何lock,但在index_stats中是看不到任何信息的。 -
analyze index idx_t compute statistics:
用来统计索引的统计信息(全分析),主要为CBO服务。 -
analyze index idx_t estimate statistics sample 10 percent
主要是用来指定比例进行抽样分析,也是为CBO服务. 例中是抽样10%主要是用来指定比例进行抽样分析,也是为CBO服务. 例中是抽样10%
--索引分析
analyze index indexName validate structure;
analyze index indexName compute statistics;
analyze index indexName estimate statistics sample 10 percent;
--dba_indexes 中的 blevel 。这列是说明索引从根块到叶快的级别,或是深度。如果级别大于等于4。该索引考虑重建。
Select indexName,blevel from dba_indexes where blevel>=4;
--当 查询出来的 height>=4 或者 DEL_LF_ROWS/LF_ROWS>0.2 的场合,当该索引中的被删除项占总的项数的百分比,该索引考虑重建 。
select height, (del_lf_rows_len/lf_rows_len) from index_stats where name='indexName';
--查询索引深度
Select indexName ,blevel from dba_indexes where blevel>=4; --大于等于4时,需要重建索引
Select indexName ,blevel from dba_indexes where index_name='indexName';
--表上有哪些索引,状态如何
select status,T.* from user_indexes T where table_name=upper('tableName');
--查询某个索引的状态
select status,indexName from user_indexes s where index_name=upper('indexName');
--查询分区索引
select status,indexName from user_ind_partitions s where index_name=upper('indexName');
重建索引
- drop 原来的索引,然后再创建索引;
此方式耗时间,无法在24*7环境中实现,不建议使用。 - 直接重建
--方法1
drop index indexName;
create index indexName on tableName(columnNmae1, ...);
--方法2
alter index indexName rebuild; --index fast full scan(or table full scan) 方式读取原索引中的数据来构建一个新的索引,有排序的操作
alter index indexName rebuild online; --执行表扫描获取数据,有排序的操作
alter index indexName rebuild tablespace tableSpaceName; --加入表空间名,最好使用tablespace字句,以保证重建工作是在现有索引相同的表空间进行,也可更换表空间
---重建期间需要额外空间,是快速重建索引的一种有效的办法,可以用来将一个索引移到新的表空间
--Rebuild 方式 (index fast full scan or table full scan 取决于统计信息的cost)
--rebuild 会阻塞 dml 操作 ,rebuild online 不会阻塞 dml 操作
explain plan for alter index indexName rebuild; --查看执行计划
select * from table(dbms_xplan.display);
--另一种直接重建索引方式
alter index indexName coalesce; --重建期间不需要额外空间,不能更换表空间
最后
以上就是背后滑板最近收集整理的关于oracle索引--重建的全部内容,更多相关oracle索引--重建内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复