首页 > 代码库 > Oracle11g 统计信息——统计信息自动收集任务

Oracle11g 统计信息——统计信息自动收集任务

参考文献:

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%;
       CLIENT_NAME    WINDOW_NAME    JOBS_CREATED    JOBS_STARTED    JOBS_COMPLETED1    auto optimizer stats collection    FRIDAY_WINDOW    1    1    12    auto optimizer stats collection    FRIDAY_WINDOW    1    1    13    auto optimizer stats collection    FRIDAY_WINDOW    1    1    14    auto optimizer stats collection    FRIDAY_WINDOW    1    1    15    auto optimizer stats collection    MONDAY_WINDOW    1    1    16    auto optimizer stats collection    MONDAY_WINDOW    1    1    17    auto optimizer stats collection    MONDAY_WINDOW    1    1    18    auto optimizer stats collection    MONDAY_WINDOW    1    1    19    auto optimizer stats collection    SATURDAY_WINDOW    5    5    510    auto optimizer stats collection    SATURDAY_WINDOW    5    5    511    auto optimizer stats collection    SATURDAY_WINDOW    5    5    512    auto optimizer stats collection    SATURDAY_WINDOW    5    5    513    auto optimizer stats collection    SUNDAY_WINDOW    5    5    514    auto optimizer stats collection    SUNDAY_WINDOW    5    5    515    auto optimizer stats collection    SUNDAY_WINDOW    5    5    516    auto optimizer stats collection    SUNDAY_WINDOW    5    5    517    auto optimizer stats collection    THURSDAY_WINDOW    1    1    118    auto optimizer stats collection    THURSDAY_WINDOW    1    1    119    auto optimizer stats collection    THURSDAY_WINDOW    1    1    120    auto optimizer stats collection    THURSDAY_WINDOW    1    1    121    auto optimizer stats collection    TUESDAY_WINDOW    1    1    122    auto optimizer stats collection    TUESDAY_WINDOW    1    1    123    auto optimizer stats collection    TUESDAY_WINDOW    1    1    124    auto optimizer stats collection    TUESDAY_WINDOW    1    1    125    auto optimizer stats collection    TUESDAY_WINDOW    1    1    126    auto optimizer stats collection    WEDNESDAY_WINDOW    1    1    127    auto optimizer stats collection    WEDNESDAY_WINDOW    1    1    128    auto optimizer stats collection    WEDNESDAY_WINDOW    1    1    129    auto optimizer stats collection    WEDNESDAY_WINDOW    1    1    130    auto optimizer stats collection    WEDNESDAY_WINDOW    1    1    1
View Code

通过时间窗口名称可以看出是周几执行的,在时间窗口内创建了几次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_STATS1    THURSDAY_WINDOW    07-AUG-14 10.00.00.000000 PM +08:00    FALSE    ENABLED2    FRIDAY_WINDOW    08-AUG-14 10.00.00.000000 PM +08:00    FALSE    ENABLED3    SATURDAY_WINDOW    09-AUG-14 06.00.00.000000 AM +08:00    FALSE    ENABLED4    SUNDAY_WINDOW    10-AUG-14 06.00.00.000000 AM +08:00    FALSE    ENABLED5    MONDAY_WINDOW    11-AUG-14 10.00.00.000000 PM +08:00    FALSE    ENABLED6    TUESDAY_WINDOW    12-AUG-14 10.00.00.000000 PM +08:00    FALSE    ENABLED7    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_GROUP1    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    COMMENTS1    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_NAME1    ORA$AT_WGRP_OS    MONDAY_WINDOW2    ORA$AT_WGRP_OS    TUESDAY_WINDOW3    ORA$AT_WGRP_OS    WEDNESDAY_WINDOW4    ORA$AT_WGRP_OS    THURSDAY_WINDOW5    ORA$AT_WGRP_OS    FRIDAY_WINDOW6    ORA$AT_WGRP_OS    SATURDAY_WINDOW7    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    ACTIVE1    MONDAY_WINDOW    11-AUG-14 10.00.00.000000 PM +08:00    FALSE2    TUESDAY_WINDOW    12-AUG-14 10.00.00.000000 PM +08:00    FALSE3    WEDNESDAY_WINDOW    13-AUG-14 10.00.00.000000 PM +08:00    FALSE4    THURSDAY_WINDOW    07-AUG-14 10.00.00.000000 PM +08:00    FALSE5    FRIDAY_WINDOW    08-AUG-14 10.00.00.000000 PM +08:00    FALSE6    SATURDAY_WINDOW    09-AUG-14 06.00.00.000000 AM +08:00    FALSE7    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]