参考文献:
Oracle11g 统计信息(一)-----统计信息自动收集任务
背景:
在使用cacti监控oracle数据库IO的时候发现每天晚上10点钟的时候oracle数据库读写明显增加,如下图所示:
对这个问题,后来查了一下是因为oracle在运行一个信息自动收集任务。oracle 11g中统计信息自动收集任务的名称是auto optimizer stats collection。11g中自动任务默认的执行时间窗口(oracle时间窗口介绍)为:
- 周一到周五是晚上10点开始到2点结束
- 周末是早上六点,持续20个小时。
1、查看自动收集任务及状态
select client_name,status from Dba_Autotask_Client where client_name='auto optimizer stats collection';
2、停止自动收集任务
--关闭信息自动收集任务 BEGIN DBMS_AUTO_TASK_ADMIN.DISABLE( client_name => 'auto optimizer stats collection', operation => NULL, window_name => NULL); END; / PL/SQL procedure successfully completed. --再次查询 select client_name,status from Dba_Autotask_Client where client_name='auto optimizer stats collection'; CLIENT_NAME STATUS ---------------------------------------------------------------- -------- auto optimizer stats collection DISABLED
但是此时再查询DBA_ATUOTASK_TASK视图时,显示该任务状态还是ENABLED
select client_name,status from dba_autotask_task where client_name='auto optimizer stats collection'; CLIENT_NAME STATUS ---------------------------------------------------------------- -------- auto optimizer stats collection ENABLED
Oracle给出的解释是在现在的版本中(11.1 to 11.2)一个client对应一个task,但是在将来的版本中会出现多个client会对应一个task,所以一个client被disabled了,不会改变task的状态。[ID 858852.1]
3、启动自动收集任务
BEGIN DBMS_AUTO_TASK_ADMIN.ENABLE( client_name => 'auto optimizer stats collection', operation => NULL, window_name => NULL); END; / PL/SQL procedure successfully completed. --再次查询
select client_name,status from Dba_Autotask_Client where client_name='auto optimizer stats collection'; CLIENT_NAME STATUS ---------------------------------------------------------------- -------- auto optimizer stats collection ENABLED
4、查看自动收集任务历史执行状态
SELECT client_name, window_name, jobs_created, jobs_started, jobs_completed FROM dba_autotask_client_history WHERE client_name like '%stats%';
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
CLIENT_NAME WINDOW_NAME JOBS_CREATED JOBS_STARTED JOBS_COMPLETED 1 auto optimizer stats collection FRIDAY_WINDOW 1 1 1 2 auto optimizer stats collection FRIDAY_WINDOW 1 1 1 3 auto optimizer stats collection FRIDAY_WINDOW 1 1 1 4 auto optimizer stats collection FRIDAY_WINDOW 1 1 1 5 auto optimizer stats collection MONDAY_WINDOW 1 1 1 6 auto optimizer stats collection MONDAY_WINDOW 1 1 1 7 auto optimizer stats collection MONDAY_WINDOW 1 1 1 8 auto optimizer stats collection MONDAY_WINDOW 1 1 1 9 auto optimizer stats collection SATURDAY_WINDOW 5 5 5 10 auto optimizer stats collection SATURDAY_WINDOW 5 5 5 11 auto optimizer stats collection SATURDAY_WINDOW 5 5 5 12 auto optimizer stats collection SATURDAY_WINDOW 5 5 5 13 auto optimizer stats collection SUNDAY_WINDOW 5 5 5 14 auto optimizer stats collection SUNDAY_WINDOW 5 5 5 15 auto optimizer stats collection SUNDAY_WINDOW 5 5 5 16 auto optimizer stats collection SUNDAY_WINDOW 5 5 5 17 auto optimizer stats collection THURSDAY_WINDOW 1 1 1 18 auto optimizer stats collection THURSDAY_WINDOW 1 1 1 19 auto optimizer stats collection THURSDAY_WINDOW 1 1 1 20 auto optimizer stats collection THURSDAY_WINDOW 1 1 1 21 auto optimizer stats collection TUESDAY_WINDOW 1 1 1 22 auto optimizer stats collection TUESDAY_WINDOW 1 1 1 23 auto optimizer stats collection TUESDAY_WINDOW 1 1 1 24 auto optimizer stats collection TUESDAY_WINDOW 1 1 1 25 auto optimizer stats collection TUESDAY_WINDOW 1 1 1 26 auto optimizer stats collection WEDNESDAY_WINDOW 1 1 1 27 auto optimizer stats collection WEDNESDAY_WINDOW 1 1 1 28 auto optimizer stats collection WEDNESDAY_WINDOW 1 1 1 29 auto optimizer stats collection WEDNESDAY_WINDOW 1 1 1 30 auto optimizer stats collection WEDNESDAY_WINDOW 1 1 1
通过时间窗口名称可以看出是周几执行的,在时间窗口内创建了几次job,执行了几次job,当然可以加上window_start_time来查看具体执行的日期。
5、查看自动收集任务执行时间窗口
select WINDOW_NAME, WINDOW_NEXT_TIME , WINDOW_ACTIVE,OPTIMIZER_STATS from DBA_AUTOTASK_WINDOW_CLIENTS order by WINDOW_NEXT_TIME ;
WINDOW_NAME WINDOW_NEXT_TIME WINDOW_ACTIVE OPTIMIZER_STATS 1 THURSDAY_WINDOW 07-AUG-14 10.00.00.000000 PM +08:00 FALSE ENABLED 2 FRIDAY_WINDOW 08-AUG-14 10.00.00.000000 PM +08:00 FALSE ENABLED 3 SATURDAY_WINDOW 09-AUG-14 06.00.00.000000 AM +08:00 FALSE ENABLED 4 SUNDAY_WINDOW 10-AUG-14 06.00.00.000000 AM +08:00 FALSE ENABLED 5 MONDAY_WINDOW 11-AUG-14 10.00.00.000000 PM +08:00 FALSE ENABLED 6 TUESDAY_WINDOW 12-AUG-14 10.00.00.000000 PM +08:00 FALSE ENABLED 7 WEDNESDAY_WINDOW 13-AUG-14 10.00.00.000000 PM +08:00 FALSE ENABLED
6、查询自动收集任务正在执行的JOB
select client_name, JOB_SCHEDULER_STATUS from DBA_AUTOTASK_CLIENT_JOB where client_name='auto optimizer stats collection';
这个查询没有结果也很正常,只有job正在运行时,该查询才有结果。
7、与时间窗口相关的视图
查询自动收集任务所属时间窗口组
select client_name,window_group from dba_autotask_client where client_name='auto optimizer stats collection';
CLIENT_NAME WINDOW_GROUP 1 auto optimizer stats collection ORA$AT_WGRP_OS
查询自动收集任务所属时间窗口组详细信息
select * from dba_scheduler_window_groups where window_group_name='ORA$AT_WGRP_OS';
WINDOW_GROUP_NAME ENABLED NUMBER_OF_WINDOWS NEXT_START_DATE COMMENTS 1 ORA$AT_WGRP_OS TRUE 7 07-AUG-14 10.00.00.000000 PM +08:00 auto optimizer stats collection
查看自动收集任务所属时间窗口组包含的子时间窗口
select * from dba_scheduler_wingroup_members where window_group_name='ORA$AT_WGRP_OS';
WINDOW_GROUP_NAME WINDOW_NAME 1 ORA$AT_WGRP_OS MONDAY_WINDOW 2 ORA$AT_WGRP_OS TUESDAY_WINDOW 3 ORA$AT_WGRP_OS WEDNESDAY_WINDOW 4 ORA$AT_WGRP_OS THURSDAY_WINDOW 5 ORA$AT_WGRP_OS FRIDAY_WINDOW 6 ORA$AT_WGRP_OS SATURDAY_WINDOW 7 ORA$AT_WGRP_OS SUNDAY_WINDOW
查看子时间窗口信息
select a.window_name,a.next_start_date,a.active from dba_scheduler_windows a inner join dba_scheduler_wingroup_members b on a.window_name = b.window_name where b.window_group_name='ORA$AT_WGRP_OS';
WINDOW_NAME NEXT_START_DATE ACTIVE 1 MONDAY_WINDOW 11-AUG-14 10.00.00.000000 PM +08:00 FALSE 2 TUESDAY_WINDOW 12-AUG-14 10.00.00.000000 PM +08:00 FALSE 3 WEDNESDAY_WINDOW 13-AUG-14 10.00.00.000000 PM +08:00 FALSE 4 THURSDAY_WINDOW 07-AUG-14 10.00.00.000000 PM +08:00 FALSE 5 FRIDAY_WINDOW 08-AUG-14 10.00.00.000000 PM +08:00 FALSE 6 SATURDAY_WINDOW 09-AUG-14 06.00.00.000000 AM +08:00 FALSE 7 SUNDAY_WINDOW 10-AUG-14 06.00.00.000000 AM +08:00 FALSE
通过这个查询可以看出DBA_AUTOTASK_WINDOW_CLIENTS视图的信息其实和上面的结果一样。
总结一下统计信息自动收集任务运行的步骤:
首先是dba_autotask_task-->dba_autotask_client建立自动执行任务
再根据时间窗口及资源组建立自动执行作业
dba_autotask_client-->dba_scheduler_window_groups-->dba_scheduler_windows-->dba_scheduler_jobs
dba_autotask_client-->dba_scheduler_job_classes
相关视图:
dba_autotask_task
dba_autotask_client
dba_autotask_client_job
dba_autotask_window_clients
dba_autotask_client_history
dba_scheduler_jobs
dba_scheduler_job_classes
dba_scheduler_window_groups
dba_scheduler_windows
dba_scheduler_wingroup_members
自定义自动收集任务时间窗口:
How to use an own Maintenance Window for the Statistics Collection in 11g [ID 1300313.1]
MOS相关文档: FAQ: Automatic Statistics Collection [ID 1233203.1]