转自:http://www.cnblogs.com/einyboy/archive/2012/08/09/2630321.html

analyze table 一般可以指定分析: 表,所有字段,所有索引字段,所有索引。 若不指定则全部都分析。

SQL> analyze table my_table compute statistics;

SQL> analyze table my_table compute statistics for table for all indexes for all columns;

SQL> analyze table my_table compute statistics for table for all indexes for all indexed columns;

其中:

SQL> analyze table my_table compute statistics;

等价于:

SQL> analyze table my_table compute statistics for table for all indexes for all columns;

sample:

analyze table t1 compute statistics for table;
analyze table t2 compute statistics for all columns;
analyze table t3 compute statistics for all indexed columns;

analyze table t5 compute statistics for all indexes;

analyze table t4 compute statistics;     (不指定)

另外,可以删除分析数据:

SQL> analyze table my_table delete statistics;

SQL> analyze table my_table delete statistics for table for all indexes for all indexed columns;

http://wfly2004.blog.163.com/blog/static/1176427201042891042233/

首先创建四个临时表t1,t2,t3,t4,和他们相对应的索引
复制内容到剪贴板
代码:
create table t1 as select * from user_objects;
create table t2 as select * from user_objects;
create table t3 as select * from user_objects;
create table t4 as select * from user_objects;
create unique index pk_t1_idx on t1(object_id);
create unique index pk_t2_idx on t2(object_id);
create unique index pk_t3_idx on t3(object_id);
create unique index pk_t4_idx on t4(object_id);

查看这个时候各个表对应的数据库统计信息(表,字段,索引)
复制内容到剪贴板
代码:
--查看表的统计信息
select table_name,num_rows,blocks,empty_blocks from user_table where table_names in ('T1','T2','T3','T4');
TABLE_NAME        NUM_ROWS        BLOCKS        EMPTY_BLOCKS
T1                       
T2                       
T3                       
T4

--查看字段的统计信息
select table_name,column_name,num_distinct,low_value,high_value,density from user_tab_columns where table_name in ('T1','T2','T3','T4');
TABLE_NAME        COLUMN_NAME        NUM_DISTINCT        LOW_VALUE        HIGH_VALUE        DENSITY
T1        OBJECT_NAME                               
T1        SUBOBJECT_NAME                               
T1        OBJECT_ID                               
T1        DATA_OBJECT_ID                               
T1        OBJECT_TYPE                               
T1        CREATED                               
T1        LAST_DDL_TIME                               
T1        TIMESTAMP                               
T1        STATUS                               
T1        TEMPORARY                               
T1        GENERATED                               
T1        SECONDARY                               
T2        OBJECT_NAME                               
T2        SUBOBJECT_NAME                               
T2        OBJECT_ID                               
T2        DATA_OBJECT_ID                               
T2        OBJECT_TYPE                               
T2        CREATED                               
T2        LAST_DDL_TIME                               
T2        TIMESTAMP                               
T2        STATUS                               
T2        TEMPORARY                               
T2        GENERATED                               
T2        SECONDARY                               
T3        OBJECT_NAME                               
T3        SUBOBJECT_NAME                               
T3        OBJECT_ID                               
T3        DATA_OBJECT_ID                               
T3        OBJECT_TYPE                               
T3        CREATED                               
T3        LAST_DDL_TIME                               
T3        TIMESTAMP                               
T3        STATUS                               
T3        TEMPORARY                               
T3        GENERATED                               
T3        SECONDARY                               
T4        OBJECT_NAME                               
T4        SUBOBJECT_NAME                               
T4        OBJECT_ID                               
T4        DATA_OBJECT_ID                               
T4        OBJECT_TYPE                               
T4        CREATED                               
T4        LAST_DDL_TIME                               
T4        TIMESTAMP                               
T4        STATUS                               
T4        TEMPORARY                               
T4        GENERATED                               
T4        SECONDARY

--查看索引的统计信息
select table_name,index_name,blevel,leaf_blocks,distinct_keys,
        avg_leaf_blocks_per_key avg_leaf_blocks,avg_data_blocks_per_key avg_data_blocks,clustering_factor,num_rows
from user_indexes
where table_name in ('T1','T2','T3','T4');
TABLE_NAME        INDEX_NAME        BLEVEL        LEAF_BLOCKS        DISTINCT_KEYS        AVG_LEAF_BLOCKS        AVG_DATA_BLOCKS        CLUSTERING_FACTOR        NUM_ROWS
T1        PK_T1_IDX                                                       
T2        PK_T2_IDX                                                       
T3        PK_T3_IDX                                                       
T4        PK_T4_IDX

现在我们分别对这个表做不同形式的analyze table处理
复制内容到剪贴板

代码:
analyze table t1 compute statistics for table;
analyze table t2 compute statistics for all columns;
analyze table t3 compute statistics for all indexed columns;
analyze table t4 compute statistics;

我们再回头看看这是的oracle数据库对于各种统计信息
复制内容到剪贴板
代码:
--这是对于表的统计信息
select table_name,num_rows,blocks,empty_blocks from user_tables where table_name in ('T1','T2','T3','T4');
TABLE_NAME        NUM_ROWS        BLOCKS        EMPTY_BLOCKS
T1        3930        55        1
T2                       
T3                       
T4        3933        55        1
--我们可以据此得出结论,只有我们在analyze table命令中指定了for table或者不指定任何参数的时候,oracle数据库才会给我们统计基于表的统计信息

--这是对于表中字段的统计信息
select table_name,column_name,num_distinct,low_value,high_value,density from user_tab_columns where table_name in ('T1','T2','T3','T4');
TABLE_NAME        COLUMN_NAME        NUM_DISTINCT        LOW_VALUE        HIGH_VALUE        DENSITY
T1        OBJECT_NAME                               
T1        SUBOBJECT_NAME                               
T1        OBJECT_ID                               
T1        DATA_OBJECT_ID                               
T1        OBJECT_TYPE                               
T1        CREATED                               
T1        LAST_DDL_TIME                               
T1        TIMESTAMP                               
T1        STATUS                               
T1        TEMPORARY                               
T1        GENERATED                               
T1        SECONDARY                               
T2        OBJECT_NAME        3823        41423030        D3F1BBB736D4C2B7DDCFFABBA7C7E5B5A5        .000270447891062615
T2        SUBOBJECT_NAME        77        503031        52455354        .012987012987013
T2        OBJECT_ID        3930        C304062D        C30F4619        .000254452926208651
T2        DATA_OBJECT_ID        3662        C304062D        C30F4619        .000273074822501365
T2        OBJECT_TYPE        15        4441544142415345204C494E4B        56494557        .000127194098193844
T2        CREATED        3684        7867081E111F33        7868071211152F        .000547559423988464
T2        LAST_DDL_TIME        3574        7867081E11251B        7868071211152F        .000565522924083892
T2        TIMESTAMP        3649        323030332D30382D33303A31363A33303A3530        323030342D30372D31383A31363A32303A3436        .000559822349362313
T2        STATUS        2        494E56414C4944        56414C4944        .000127194098193844
T2        TEMPORARY        2        4E        59        .000127194098193844
T2        GENERATED        2        4E        59        .000127194098193844
T2        SECONDARY        2        4E        59        .000127194098193844
T3        OBJECT_NAME                               
T3        SUBOBJECT_NAME                               
T3        OBJECT_ID        3931        C304062D        C30F461A        .000254388196387688
T3        DATA_OBJECT_ID                               
T3        OBJECT_TYPE                               
T3        CREATED                               
T3        LAST_DDL_TIME                               
T3        TIMESTAMP                               
T3        STATUS                               
T3        TEMPORARY                               
T3        GENERATED                               
T3        SECONDARY                               
T4        OBJECT_NAME        3825        41423030        D3F1BBB736D4C2B7DDCFFABBA7C7E5B5A5        .000261437908496732
T4        SUBOBJECT_NAME        77        503031        52455354        .012987012987013
T4        OBJECT_ID        3932        C304062D        C30F461B        .000254323499491353
T4        DATA_OBJECT_ID        3664        C304062D        C30F461B        .00027292576419214
T4        OBJECT_TYPE        15        4441544142415345204C494E4B        56494557        .0666666666666667
T4        CREATED        3685        7867081E111F33        78680712111530        .000271370420624152
T4        LAST_DDL_TIME        3575        7867081E11251B        78680712111530        .00027972027972028
T4        TIMESTAMP        3650        323030332D30382D33303A31363A33303A3530        323030342D30372D31383A31363A32303A3437        .000273972602739726
T4        STATUS        2        494E56414C4944        56414C4944        .5
T4        TEMPORARY        2        4E        59        .5
T4        GENERATED        2        4E        59        .5
T4        SECONDARY        2        4E        59        .5
/*
在这个结果中我们可以看到,oracle数据库给t2,t4的所有字段都做了统计信息.
对表t3的object_id(索引字段)做了统计信息.
由此得出结论,
在指定for all columns 和不指定任何参数的时候oracle会给所有字段做统计信息,在指定for indexed columns时,oracle只给[b]有索引的字段进行字段信息统计[/b],如果我们别有必要给所有字段统计信息时,这个属性就很有用了.
*/

--这里是对于索引的统计信息
select table_name,index_name,blevel,leaf_blocks,distinct_keys,
        avg_leaf_blocks_per_key avg_leaf_blocks,avg_data_blocks_per_key avg_data_blocks,clustering_factor,num_rows
from user_indexes where table_name in ('T1','T2','T3','T4');
TABLE_NAME        INDEX_NAME        BLEVEL        LEAF_BLOCKS        DISTINCT_KEYS        AVG_LEAF_BLOCKS        AVG_DATA_BLOCKS        CLUSTERING_FACTOR        NUM_ROWS
T1        PK_T1_IDX                                                       
T2        PK_T2_IDX                                                       
T3        PK_T3_IDX                                                       
T4        PK_T4_IDX        1        9        3932        1        1        2143        3932

--从这里我们可以看出,只有表t4有索引统计信息.
--再综合前面的我们就会发现,如果在运行analyze table是我们不指定参数,oracle将收集对于特定表的所有统计信息(表,索引,表字段的统计信息)

补充,truncate命令不修改以上统计信息
复制内容到剪贴板
代码:
truncate table t1;
truncate table t2;
truncate table t3;
truncate table t4;
--我们在查看表和索引的统计信息
select table_name,num_rows,blocks,empty_blocks from user_tables where table_name in ('T1','T2','T3','T4');
TABLE_NAME        NUM_ROWS        BLOCKS        EMPTY_BLOCKS
T1        3930        55        1
T2                       
T3                       
T4        3933        55        1

--索引的统计信息
select table_name,index_name,blevel,leaf_blocks,distinct_keys,
        avg_leaf_blocks_per_key avg_leaf_blocks,avg_data_blocks_per_key avg_data_blocks,clustering_factor,num_rows
from user_indexes where table_name in ('T1','T2','T3','T4');
TABLE_NAME        INDEX_NAME        BLEVEL        LEAF_BLOCKS        DISTINCT_KEYS        AVG_LEAF_BLOCKS        AVG_DATA_BLOCKS        CLUSTERING_FACTOR        NUM_ROWS
T1        PK_T1_IDX                                                       
T2        PK_T2_IDX                                                       
T3        PK_T3_IDX                                                       
T4        PK_T4_IDX        1        9        3932        1        1        2143        3932

--我们再对以上各表做一次分析
analyze table t1 compute statistics for table;
analyze table t2 compute statistics for all columns;
analyze table t3 compute statistics for all indexed columns;
analyze table t4 compute statistics;

--现在再来查看表和索引的统计信息
select table_name,num_rows,blocks,empty_blocks,initial_extent,'8192' block_size from user_tables where table_name in ('T1','T2','T3','T4');
TABLE_NAME        NUM_ROWS        BLOCKS        EMPTY_BLOCKS        INITIAL_EXTENT        BLOCK_SIZE
T1        0        0        8        65536        8192
T2                                65536        8192
T3                                65536        8192
T4        0        0        8        65536        8192

--索引的统计信息
select table_name,index_name,blevel,leaf_blocks,distinct_keys,
        avg_leaf_blocks_per_key avg_leaf_blocks,avg_data_blocks_per_key avg_data_blocks,clustering_factor,num_rows
from user_indexes where table_name in ('T1','T2','T3','T4');
TABLE_NAME        INDEX_NAME        BLEVEL        LEAF_BLOCKS        DISTINCT_KEYS        AVG_LEAF_BLOCKS        AVG_DATA_BLOCKS        CLUSTERING_FACTOR        NUM_ROWS
T1        PK_T1_IDX                                                       
T2        PK_T2_IDX                                                       
T3        PK_T3_IDX                                                       
T4        PK_T4_IDX        0        0        0        0        0        0        0

--由此得出结论,truncate命令不会修改数据的统计信息,
--也就是如果我们想让CBO利用合理利用数据的统计信息的时候,需要我们及时的使用analyze命令或者dbms_stats重新统计数据的统计信息。

[转] Oracle analyze table 使用总结的更多相关文章

  1. 对于Oracle analyze table的使用总结 . 对于Oracle analyze table的使用总结 .

    对于Oracle analyze table的使用总结 . 对于Oracle analyze table的使用总结 . analyze table 一般可以指定分析: 表,所有字段,所有索引字段,所有 ...

  2. Oracle索引梳理系列(十)- 直方图使用技巧及analyze table操作对直方图统计的影响(谨慎使用)

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...

  3. [转] Oracle analyze 命令分析

    转自:http://blog.sina.com.cn/s/blog_682841ba0101bncp.html 1.analyze table t1 compute statistics for ta ...

  4. Oracle Analyze 命令 详解

    官网的链接如下: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_4005.htm#SQLRF01105 使用DBMS ...

  5. dbms_stats.gather_table_stats与analyze table 的区别[转贴]

    Analyze StatementThe ANALYZE statement can be used to gather statistics for a specific table, index ...

  6. Oracle Analyze

    Analyze使用场景 之前很多次都说到,对表的索引等信息进行了增删改之后,需要对表进行analyze更新统计信息,才能使数据库做出最好的执行计划,没有注意到,即使是一张很小的空表,如果进行了字段的增 ...

  7. ORACLE ANALYZE使用小结

      ANALYZE的介绍     使用ANALYZE可以收集或删除对象的统计信息.验证对象的结构.标识表或cluster中的行迁移/行链接信息等.官方文档关于ANALYZE功能介绍如下: ·      ...

  8. mysql 优化analyze table

    Analyze Table MySQL 的Optimizer(优化元件)在优化SQL语句时,首先需要收集一些相关信息,其中就包括表的cardinality(可以翻译为“散列程度”),它表示某个索引对应 ...

  9. oracle partition table 分区表详解

    Oracle partition table 分区表详解 分区表就是通过使用分区技术,将一张大表,拆分成多个表分区(独立的segment),从而提升数据访问的性能,以及日常的可维护性.分区表中,每个分 ...

随机推荐

  1. Happy New Year

    今年的元旦能明显感觉到节日的狂欢.一方面,论文的事情,压抑了好久,另一方面,把自己融入节日之中.所以才有了节日的深度参与. 早上还是按时的起床,看了朋友圈,内心却能专注于平静.因为见到了优秀的人,才发 ...

  2. 转@ManyToMany- annotation关系映射篇(下)

    原文:http://blog.sina.com.cn/s/blog_6fef491d0100obdd.html 终于要说ManyToMany了 场景:Product和Customer. 先看TestP ...

  3. java的向上转型总结

    在<think of java>中对'向上转型'有着如下的描述 看完之后很蒙圈,所以自己在网上找了一些描述,并自己做了简单的总结 简单的例子 class A{ public void A1 ...

  4. JPush API client library for C Sharp(极光推送API)

    概述 这是 JPush REST API 的 C# 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能. 对应的 REST API 文档:http://docs.jpush.io/s ...

  5. C#打开文件对话框

    OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = System.Environment.CurrentDirector ...

  6. 普通图片预览及demo(非分块)

    演示地址: http://codeman35.itongyin.com:19003/v2/image.html 功能:通过加载大图预览,这种方式无法和google art 比较.只能应用于简单的图片预 ...

  7. 从入门到精通C++需要学的10本书

    学习C++从入门到精通的的十本最经典书籍  文章来源中国IT实验室收集整理作者佚名更新时间2009-5-16 12:27:05 保存本文保存本文推荐给好友推荐给好友收藏本页收藏本页 欢迎进入C/C++ ...

  8. python基础知识---操作文件

    一.打开文件  open()函数 open函数返回一个文件对象. 用法:open('文件名','模式') 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内 ...

  9. C# ADO.NET (sql语句连接方式)(查询)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  10. ---Shell的数组遍历

    1.  一一读入:  read -a A < <(echo a b c d e f g) 2. 遍历输出