统计信息作用

统计信息是优化器进行工作的一个主要依据。但是一个问题在于收集统计数据的时候时间是比较长的,并且重复的对静态的数据进行收集是没有用了,所以需要确定那些对象需要收集统计数据。

收集统计信息

Oracle 10g以上会自动收集统计信息,默认情况下,周1-周5晚上10点开始收集,周末是白天收集。由一个scheduler控制。
并且默认收集一天来修改量超过10%的。
从Oracle Database 10g,Oracle在建库后就默认创建了一个名为GATHER_STATS_JOB的定时任务,用于自动收集CBO的统计信息。
从Oracle Database 11g,Oracle在建库后就默认创建了一个名为BSLN_MAINTAIN_STATS_JOB的定时任务,用于自动收集CBO的统计信息。
这个自动任务默认情况下在工作日晚上10:00-6:00和周末全天开启。
10g
SELECT owner,job_name,state,last_start_date,last_run_duration,failure_count FROM dba_scheduler_jobs WHERE job_name = 'GATHER_STATS_JOB';
11g
SELECT owner,job_name,state,last_start_date,last_run_duration,failure_count FROM dba_scheduler_jobs WHERE job_name = 'BSLN_MAINTAIN_STATS_JOB';
统计信息生成技术包括三种:

  • 1 基于数据采样的估计方式
  • 2 精确计算方式
  • 3 用户自定义的统计信息收集方式
    我们可以在 USER_TAB_MODIFICATIONS视图中找到本OWNER的对象更改情况,每15分钟SONM会去更行这个视图。
    或者可以执行exec dbms_stats.FLUSH_DATABASE_MONITORING_INFO();显示进行刷性。
    并且这个功能必须开启表的MONITORING,10g以来如果设置
    SQL> show parameter statistics_level
    NAME TYPE VALUE

statistics_level string TYPICAL
就可以启动表监控

优化器统计范围

表统计; --行数,块数,行平均长度;all_tables:NUM_ROWS,BLOCKS,AVG_ROW_LEN;
列统计; --列中唯一值的数量(NDV),NULL值的数量,数据分布;
--DBA_TAB_COLUMNS:NUM_DISTINCT,NUM_NULLS,HISTOGRAM;
索引统计;--叶块数量,等级,聚簇因子;
--DBA_INDEXES:LEAF_BLOCKS,CLUSTERING_FACTOR,BLEVEL;
系统统计;--I/O性能与使用率;
--CPU性能与使用率;
--存储在aux_stats$中,需要使用dbms_stats收集,I/O统计在X$KCFIO中;

收集统计信息

  • 1.analyze table t1 compute statistics for table; -->user_tables
    (只对表的总体信息进行统计,比如行数多少等,不涉及到表字段)
  • 2.analyze table t2 compute statistics for all columns; -->user_tab_columns
    (只会收集表字段信息)
  • 3.analyze table t3 compute statistics for all indexed columns; -->user_tab_columns
    (只会收集表中索引所在的字段信息)
  • 4.analyze table t4 compute statistics for all indexes;à user_indexes
    (只收集表索引的信息)
  • 5.analyze table t5 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;
例子:
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 table t5 as select * from user_objects;
create table t6 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);
create unique index pk_t5_idx on t5(object_id);
create unique index pk_t6_idx on t6(object_id);
<刚建完表的时候>
(1) 查看表的统计信息
select table_name, num_rows, blocks, empty_blocks
from user_tables
where table_name in ('T1', 'T2', 'T3', 'T4', 'T5','T6');
(2) 查看字段的统计信息
select table_name,
column_name,
num_distinct,
low_value,
high_value,
density
from user_tab_columns
where table_name in ('T1', 'T2', 'T3', 'T4','T5','T6');
(3) 查看索引的统计信息
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', 'T5', 'T6');
二.执行analyze命令
analyze table t1 compute statistics for table; --针对表收集信息
analyze table t2 compute statistics for all columns; --针对表字段收集信息
analyze table t3 compute statistics for all indexes columns; --收集索引字段信息
analyze table t4 compute statistics; --收集表,表字段,索引信息
analyze table t5 compute statistics for all indexes; --收集索引信息
analyze table t6 compute statistics for table for all indexes for all columns;
--收集表,索引,表字段信息
(1) 表的统计信息
select table_name, num_rows, blocks, empty_blocks
from user_tables
where table_name in ('T1', 'T2', 'T3', 'T4', 'T5','T6');
(2) 表中字段的统计信息
select table_name,
column_name,
num_distinct,
low_value,
high_value,
density
from user_tab_columns
where table_name in ('T1', 'T2', 'T3', 'T4','T5','T6');
<其中会收集T2的表字段信息,T3是索引所在字段信息,T4表字段信息,T6表字段信息>
(3) 索引的统计信息
没有变化,说明在创建索引的时候就ORACLE就已经收集相关信息
相关文章 Oracle 统计信息命令汇总

转载自chenoracle

最后修改:2022 年 02 月 27 日
如果觉得我的文章对你有用,请随意赞赏