Oracle 收集统计数据

查看最新用户表统计信息

select owner,table_name,last_analyzed from dba_tables where owner not like '%SYS%' order by 1,3;


SELECT Table_Name,Num_Rows,Blocks,Empty_Blocks,Avg_Space,Chain_Cnt,Avg_Row_Len,Sample_Size,Last_Analyzed
FROM Dba_Tables WHERE owner = 'SCOTT' ;

oracle是通过维护窗口来完成系统的自动job的,系统的维护窗口通过视图dba_scheduler_windows

SQL> select window_name,resource_plan,comments from dba_scheduler_windows;

自从上一次分析统计数据以来,发生了多少次插入、更新和删除操作。
SELECT * FROM Sys.Dba_Tab_Modifications WHERE Table_Owner = 'SCOTT';

查看历史JOB执行情况
select LOG_ID,LOG_DATE,OWNER,JOB_NAME,STATUS,REQ_START_DATE,ACTUAL_START_DATE from DBA_SCHEDULER_JOB_RUN_DETAILS where job_name='BSLN_MAINTAIN_STATS_JOB'


查看JOB执行情况
select OWNER,JOB_NAME,STATE,FAILURE_COUNT from dba_scheduler_jobs;

BSLN_MAINTAIN_STATS_JOB

手动执行全库统计信息
exec dbms_stats.GATHER_DATABASE_STATS(99,CASCADE=>TRUE);

手工收集统计信息:
SQL> exec dbms_stats.gather_schema_stats('detail',options=>'gather stale',estimate_percent =>10);


--收集没有分析过的表的统计信息
begin
dbms_stats.gather_schema_stats(ownname=>scott,options=>'gather empty');
end;

--重新分析修改量超过10%的表(这些修改包括插入、更新和删除)
begin
dbms_stats.gather_schema_stats(ownname=>scott,options=>'gather stale');
end;

[@more@]

Oracle10g增加了一个新的任务,用来自动分析数据库,用于增加CBO执行的正确性。这个任务就是GATHER_STATS_JOB,
任务定义周一到周五的夜晚执行,和周六日全天执行。在oracle11g中job的名称有些不同。

 oracle这个自动收集信息的出发点很好,但是,大部分系统晚上的负载也很高,或者执行计划改变,会引起新的性能问题。
而且oracle这个自动收集信息的job非常耗性能。

 

oracle10g的系统自动job
SQL> select job_name,comments from dba_scheduler_jobs;

JOB_NAME COMMENTS
------------------------------ --------------------------------------------------------------------------------
AUTO_SPACE_ADVISOR_JOB auto space advisor maintenance job
GATHER_STATS_JOB Oracle defined automatic optimizer statistics collection job
FGR$AUTOPURGE_JOB file group auto-purge job
PURGE_LOG purge log job
MGMT_STATS_CONFIG_JOB OCM Statistics collection job.
MGMT_CONFIG_JOB Configuration collection job.
RLM$SCHDNEGACTION 
RLM$EVTCLEANUP 

8 rows selected

SQL>

oracle11g的自动job
SQL> select job_name,comments from dba_scheduler_jobs;

JOB_NAME COMMENTS
------------------------------ --------------------------------------------------------------------------------
SM$CLEAN_AUTO_SPLIT_MERGE auto clean job for auto split merge
RSE$CLEAN_RECOVERABLE_SCRIPT auto clean job for recoverable script
FGR$AUTOPURGE_JOB file group auto-purge job
BSLN_MAINTAIN_STATS_JOB Oracle defined automatic moving window baseline statistics computation job
DRA_REEVALUATE_OPEN_FAILURES Reevaluate open failures for DRA
HM_CREATE_OFFLINE_DICTIONARY Create offline dictionary in ADR for DRA name translation
ORA$AUTOTASK_CLEAN Delete obsolete AUTOTASK repository data
FILE_WATCHER File watcher job
PURGE_LOG purge log job
MGMT_STATS_CONFIG_JOB OCM Statistics collection job.
MGMT_CONFIG_JOB Configuration collection job.

11 rows selected

SQL>

oracle是通过维护窗口来完成系统的自动job的,系统的维护窗口通过视图dba_scheduler_windows

SQL> select window_name,resource_plan,comments from dba_scheduler_windows;

WINDOW_NAME RESOURCE_PLAN COMMENTS
------------------------------ ------------------------------ --------------------------------------------------------------------------------
MONDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Monday window for maintenance tasks
TUESDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Tuesday window for maintenance tasks
WEDNESDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Wednesday window for maintenance tasks
THURSDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Thursday window for maintenance tasks
FRIDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Friday window for maintenance tasks
SATURDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Saturday window for maintenance tasks
SUNDAY_WINDOW DEFAULT_MAINTENANCE_PLAN Sunday window for maintenance tasks
WEEKNIGHT_WINDOW Weeknight window - for compatibility only
WEEKEND_WINDOW Weekend window - for compatibility only

9 rows selected

SQL>


维护窗口组有哪些维护组
select * from dba_scheduler_wingroup_members;


select t1.window_name,
t1.repeat_interval,
t1.duration,
t2.window_group_name
from dba_scheduler_windows t1, dba_scheduler_wingroup_members t2
where t1.window_name = t2.window_name
and t2.window_group_name in
('MAINTENANCE_WINDOW_GROUP', 'BSLN_MAINTAIN_STATS_SCHED');


常用视图:
select * from dba_scheduler_programs
select * from dba_scheduler_jobs
select * from dba_scheduler_running_jobs
select * from dba_scheduler_job_run_details
select * from dba_scheduler_schedules
select * from dba_scheduler_wingroup_members

select * from DBA_AUTOTASK_CLIENT_JOB;
select * from DBA_AUTOTASK_CLIENT;
select * from DBA_AUTOTASK_JOB_HISTORY;
select * from DBA_AUTOTASK_WINDOW_CLIENTS;
select * from DBA_AUTOTASK_CLIENT_HISTORY;


10g关闭自动收集job

select * from Dba_Scheduler_Jobs where JOB_NAME ='GATHER_STATS_JOB'

所以建议最好关闭自动统计信息收集功能:
exec DBMS_SCHEDULER.DISABLE('GATHER_STATS_JOB');

启动自动统计信息收集功能
exec DBMS_SCHEDULER.ENABLE('GATHER_STATS_JOB');


手工收集统计信息:
SQL> exec dbms_stats.gather_schema_stats('detail',options=>'gather stale',estimate_percent =>10);


--收集没有分析过的表的统计信息
begin
dbms_stats.gather_schema_stats(ownname=>scott,options=>'gather empty');
end;

--重新分析修改量超过10%的表(这些修改包括插入、更新和删除)
begin
dbms_stats.gather_schema_stats(ownname=>scott,options=>'gather stale');
end;


11g关闭自动收集job
The automated maintenance tasks infrastructure (known as AutoTask) schedules tasks to
run automatically in Oracle Scheduler windows known as maintenance windows. By default,
one window is scheduled for each day of the week. Automatic optimizer statistics collection
runs as part of AutoTask and is enabled by default to run in all predefined maintenance windows.


If for some reason automatic optimizer statistics collection is disabled, then you can enable
it using the ENABLE procedure in the DBMS_AUTO_TASK_ADMIN package:

BEGIN
DBMS_AUTO_TASK_ADMIN.ENABLE(
client_name => 'auto optimizer stats collection', 
operation => NULL, 
window_name => NULL);
END;
/
When you want to disable automatic optimizer statistics collection, you can disable it using the DISABLE procedure in the DBMS_AUTO_TASK_ADMIN package:

BEGIN
DBMS_AUTO_TASK_ADMIN.DISABLE(
client_name => 'auto optimizer stats collection', 
operation => NULL, 
window_name => NULL);
END;
/

 

手工收集统计信息:
SQL> exec dbms_stats.gather_schema_stats('detail',options=>'gather stale',estimate_percent =>10);

参考文档 
http://download.oracle.com/docs/cd/E11882_01/server.112/e16638/stats.htm#PFGRF94713

这是对命令与工具包的一些总结
1、对于分区表,建议使用DBMS_STATS,而不是使用Analyze语句。
a) 可以并行进行,对多个用户,多个Table
b) 可以得到整个分区表的数据和单个分区的数据。
c) 可以在不同级别上Compute Statistics:单个分区,子分区,全表,所有分区 ,但不收集聚簇统计
d) 可以倒出统计信息
e) 可以用户自动收集统计信息
2、DBMS_STATS的缺点
a) 不能Validate Structure
b) 不能收集CHAINED ROWS, 不能收集CLUSTER TABLE的信息,这两个仍旧需要使用Analyze语句。
c) DBMS_STATS 默认不对索引进行Analyze,因为默认Cascade是False,需要手工指定为True
3、对于External Table,Analyze不能使用,只能使用DBMS_STATS来收集信息。

 

收集oracle统计信息(1)优化器统计范围:

表统计; --行数,块数,行平均长度;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中;

-------------
analyze
-------------
需要使用ANALYZE统计的统计:
使用LIST CHAINED ROWS和VALIDATE子句;
收集空闲列表块的统计;
Analyze table tablename compute statistics;
Analyze index|cluster indexname estimate statistics;
ANALYZE TABLE tablename COMPUTE STATISTICS
FOR TABLE
FOR ALL [LOCAL] INDEXES
FOR ALL [INDEXED] COLUMNS;
ANALYZE TABLE tablename DELETE STATISTICS
ANALYZE TABLE tablename VALIDATE REF UPDATE
ANALYZE TABLE tablename VALIDATE STRUCTURE [CASCADE]|[INTO TableName]
ANALYZE TABLE tablename LIST CHAINED ROWS [INTO TableName]
ANALYZE 不适合做的分析
----------------------
dbms_stats
----------------------
dbms_stats能良好地估计统计数据(尤其是针对较大的分区表),并能获得更好的统计结果,最终制定出速度更快的执行计划。
这个包的下面四个存储过程分别收集index、table、schema、database的统计信息:
dbms_stats.gather_table_stats 收集表、列和索引的统计信息;
dbms_stats.gather_schema_stats 收集SCHEMA下所有对象的统计信息;
dbms_stats.gather_index_stats 收集索引的统计信息;
dbms_stats.gather_system_stats 收集系统统计信息
dbms_stats.GATHER_DICTIONARY_STATS: 所有字典对象的统计;
DBMS_STATS.GATHER_DICTIONARY_STATS 其收集所有系统模式的统计

dbms_stats.delete_table_stats 删除表的统计信息
dbms_stats.delete_index_stats 删除索引的统计信息
dbms_stats.export_table_stats 输出表的统计信息
dbms_stats.create_state_table
dbms_stats.set_table_stats 设置表的统计
dbms_stats.auto_sample_size

 

DBMS_STATS.GATHER_TABLE_STATS详解


作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息.

DBMS_STATS.GATHER_TABLE_STATS的语法如下:
DBMS_STATS.GATHER_TABLE_STATS ( ownname VARCHAR2, tabname VARCHAR2, partname VARCHAR2, estimate_percent NUMBER, block_sample BOOLEAN, method_opt VARCHAR2, degree NUMBER, granularity VARCHAR2, cascade BOOLEAN, stattab VARCHAR2, statid VARCHAR2, statown VARCHAR2, no_invalidate BOOLEAN, force BOOLEAN);

参数说明:
ownname:要分析表的拥有者
tabname:要分析的表名.
partname:分区的名字,只对分区表或分区索引有用.
estimate_percent:采样行的百分比,取值范围[0.000001,100],null为全部分析,不采样. 常量:DBMS_STATS.AUTO_SAMPLE_SIZE是默认值,由Oracle决定最佳取采样值.
block_sapmple:是否用块采样代替行采样.
method_opt:决定histograms信息是怎样被统计的.method_opt的取值如下:
for all columns:统计所有列的histograms.
for all indexed columns:统计所有indexed列的histograms.
for all hidden columns:统计你看不到列的histograms
for columns SIZE | REPEAT | AUTO | SKEWONLY:统计指定列的histograms.N的取值范围[1,254]; REPEAT上次统计过的histograms;AUTO由Oracle决定N的大小;SKEWONLY multiple end-points with the same value which is what we define by "there is skew in the data
degree:决定并行度.默认值为null.
granularity:Granularity of statistics to collect ,only pertinent if the table is partitioned.
cascace:是收集索引的信息.默认为falase.
stattab指定要存储统计信息的表,statid如果多个表的统计信息存储在同一个stattab中用于进行区分.statown存储统计信息表的拥有者.以上三个参数若不指定,统计信息会直接更新到数据字典.
no_invalidate: Does not invalidate the dependent cursors if set to TRUE. The procedure invalidates the dependent cursors immediately if set to FALSE.
force:即使表锁住了也收集统计信息.

例子:
execute dbms_stats.gather_table_stats(ownname => 'owner',tabname => 'table_name' ,estimate_percent => null ,method_opt => 'for all indexed columns' ,cascade => true);

GATHER_INDEX_STATS
==========================
BEGIN
SYS.DBMS_STATS.GATHER_INDEX_STATS (OwnName => 'ABC',
IndName => 'IDX_FUNC_ABC',
Estimate_Percent => 10,
Degree => SYS.DBMS_STATS.DEFAULT_DEGREE,
No_Invalidate => FALSE);
END;

posted @ 2017-01-12 09:49  兔小灰385  阅读(1462)  评论(0编辑  收藏  举报