---
title: "Oracle表与索引的分析及索引重建"
author: "luweifeng1983"
date: "2014-09-25T12:09:47Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2014-09-25T12:09:47Z"
id: 2121526
url: "https://quicker.iteye.com/blog/2121526"
---

转自：[http://www.cnblogs.com/lanzi/archive/2010/12/31/1923270.html](http://www.cnblogs.com/lanzi/archive/2010/12/31/1923270.html)

**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 =&gt; 'scott',stattab =&gt; 'stat\_table');

--导出整个scheme的统计信息  
exec dbms\_stats.export\_schema\_stats(ownname =&gt; 'scott',stattab =&gt; 'stat\_table');

--分析scheme  
Exec dbms\_stats.gather\_schema\_stats(ownname =&gt; 'test',options =&gt; 'GATHER AUTO',  
                                       estimate\_percent =&gt; dbms\_stats.auto\_sample\_size,  
                                       method\_opt =&gt; 'for all indexed columns',  
                                       degree =&gt; 6 );

--分析表  
exec dbms\_stats.gather\_table\_stats(ownname =&gt; 'TEST',tabname =&gt; 'sm\_user',estimate\_percent =&gt; 10,method\_opt=&gt; 'for all indexed columns') ;

--分析索引  
exec dbms\_stats.gather\_index\_stats(ownname =&gt; 'TEST',indname =&gt; 'pk\_user\_index',estimate\_percent =&gt; '10',degree =&gt; '4') ;

--如果发现执行计划走错，删除表的统计信息  
exec dbms\_stats.delete\_table\_stats(ownname =&gt; 'TEST',tabname =&gt; 'SM\_USER') ;

--导入表的历史统计信息  
exec dbms\_stats.import\_table\_stats(ownname =&gt; 'TEST',tabname =&gt; 'SM\_USER',stattab =&gt; 'stat\_table') ;

--如果进行分析后，大部分表的执行计划都走错，需要导回整个scheme的统计信息  
exec dbms\_stats.import\_schema\_stats(ownname =&gt; 'TEST',stattab =&gt; 'SM\_USER');

--导入索引的统计信息  
exec dbms\_stats.import\_index\_stats(ownname =&gt; 'TEST',indname =&gt; 'PK\_USER\_INDEX',stattab =&gt; 'stat\_table')

analyze和dbms\_stats不同的地方:  
analyze是同时更新表和索引的统计信息，而dbms\_stats会先更新表的统计信息，然后再更新索引的统计信息，  
这里就有一个问题，就是当表的统计信息更新后，而索引的统计信息没有被更新，这时候cbo就有可能选择错误的plan

2）DBMS\_UTILITY.ANALYZE\_SCHEMA：可直接分析SCHEMA中所有对象  
   如：EXEC DBMS\_UTILITY.ANALYZE\_SCHEMA ('LTTFM','COMPUTE');

3）DBMS\_DDL.ANALYZE\_OBJECT：收集对象的的统计信息