日期:2014-05-16  浏览次数:20426 次

Oracle 索引与表分析几种方法

分析表与索引有几种方法,现在列举出来参考下。

1.分析表与索引(analyze 不会重建索引)

analyze table tablename compute statistics 
等同于 analyze table tablename compute statistics for table for all indexes for all columns

for table 的统计信息存在于视图:user_tables 、all_tables、dba_tables

for all indexes 的统计信息存在于视图: user_indexes 、all_indexes、dba_indexes

for all columns 的统计信息存在于视图:user_tab_columns、all_tab_columns、dba_tab_columns

注:分析表与索引见 AnalyzeAllTable存储过程

2、一般来讲可以采用以下三种方式来手工分析索引。
analyze index idx_t validate structure:
analyze index idx_t compute statistics:
analyze index idx_t estimate statistics sample 10 percent

1)analyze index idx_t validate structure:
这段分析语句是用来分析索引的block中是否有坏块儿,那么根据分析我们可以得到索引的结构数据,这些数据会保留到
index_stats中,来判断这个索引是否需要rebuild. 需要注意的是这样的分析是不会收集索引的统计信息的。

2)validate structure有二种模式: online, offline, 一般来讲默认的方式是offline。
当以offline的模式analyze索引时,会对table加一个表级共享锁,对目前table的一些实时DMl操作会产生一定的影响。
而以online模式分析时候,则不会加任何lock,但在index_stats中是看不到任何信息的。

3)analyze index idx_t compute statistics:
用来统计索引的统计信息(全分析),主要为CBO服务。

4)analyze index idx_t estimate statistics sample 10 percent
主要是用来指定比例进行抽样分析,也是为CBO服务. 例中是抽样10%

3.重建索引

alter index index_name rebuild tablespace tablespace_name 
alter index index_name rebuild tablespace tablespace_name 加入表空间名,会将指定的索引移动到指定的表空间当中。

注:
analyze 操作只是统计信息,并将统计信息存放起来供日后分析SQL使用,不进行重建之类的具体实施性操作,因此要重建索引的话
还是要用 alter index index_name rebuild

4、其他的统计方法

1)DBMS_STATS:这个当然是最强大的分析包了
--创建统计信息历史保留表
exec dbms_stats.create_stat_table(ownname => 'scott',stattab => 'stat_table');

--导出整个scheme的统计信息
exec dbms_stats.export_schema_stats(ownname => 'scott',stattab => 'stat_table');

--分析scheme
Exec dbms_stats.gather_schema_stats(ownname => 'test',options => 'GATHER AUTO',
                                       estimate_percent => dbms_stats.auto_sample_size,