首页 > 代码库 > Oracle之自动收集统计信息

Oracle之自动收集统计信息

 

一、Oracle 11g

在Oracle的11g版本中提供了统计数据自动收集的功能。在部署安装11g Oracle软件过程中,其中有一个步骤便是提示是否启动这个功能(默认是启用这个功能)。

在这里介绍一下怎么将该功能启用与禁用:

1、查看自动收集统计信息的任务及状态:

技术分享

SQL> select client_name,status from dba_autotask_client;CLIENT_NAME                 STATUS---------------------------------------- ----------------auto optimizer stats collection      ENABLEDauto space advisor             ENABLEDsql tuning advisor             ENABLED其中"auto optimizer stats collection"便是我们要寻找的自动收集统计信息的任务名称,它的状态目前是启用状态。

技术分享

2、禁止自动收集统计信息的任务

技术分享

禁用自动收集统计信息的任务可以使用DBMS_AUTO_TASK_ADMIN包完成:SQL> exec DBMS_AUTO_TASK_ADMIN.DISABLE(client_name => ‘auto optimizer stats collection‘,operation => NULL,window_name => NULL);PL/SQL 过程已成功完成。SQL> select client_name,status from dba_autotask_client;CLIENT_NAME                 STATUS---------------------------------------- ----------------auto optimizer stats collection      DISABLEDauto space advisor             ENABLEDsql tuning advisor             ENABLED此时"auto optimizer stats collection"任务已经被禁用。

技术分享

3、启用自动收集统计信息的任务

技术分享

SQL> exec DBMS_AUTO_TASK_ADMIN.ENABLE(client_name => ‘auto optimizer stats collection‘,operation => NULL,window_name => NULL);PL/SQL 过程已成功完成。SQL> select client_name,status from dba_autotask_client;CLIENT_NAME                 STATUS---------------------------------------- ----------------auto optimizer stats collection      ENABLEDauto space advisor             ENABLEDsql tuning advisor             ENABLED

技术分享

这个功能貌似带来了统计数据采集上的便捷,但是Oracle 11g自动收集统计信息的时间是22:00--2:00
这个时间段往往是业务的高峰期,给本已紧张的系统带来更大的负担。所以,应该把自动执行的时间改到空闲的时段。

4、获得当前自动收集统计信息的执行时间:

技术分享

SQL> select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2  2  where t1.window_name=t2.window_name and t2.window_group_name in (‘MAINTENANCE_WINDOW_GROUP‘,‘BSLN_MAINTAIN_STATS_SCHED‘); WINDOW_NAME                    REPEAT_INTERVAL                                                                  DURATION------------------------------ -------------------------------------------------------------------------------- -------------------------------------------------------------------------------MONDAY_WINDOW                  freq=daily;byday=MON;byhour=22;byminute=0; bysecond=0                            +000 04:00:00TUESDAY_WINDOW                 freq=daily;byday=TUE;byhour=22;byminute=0; bysecond=0                            +000 04:00:00WEDNESDAY_WINDOW               freq=daily;byday=WED;byhour=22;byminute=0; bysecond=0                            +000 04:00:00THURSDAY_WINDOW                freq=daily;byday=THU;byhour=22;byminute=0; bysecond=0                            +000 04:00:00FRIDAY_WINDOW                  freq=daily;byday=FRI;byhour=22;byminute=0; bysecond=0                            +000 04:00:00SATURDAY_WINDOW                freq=daily;byday=SAT;byhour=6;byminute=0; bysecond=0                             +000 20:00:00SUNDAY_WINDOW                  freq=daily;byday=SUN;byhour=6;byminute=0; bysecond=0                             +000 20:00:00 7 rows selected

技术分享

其中:
      WINDOW_NAME:任务名

      REPEAT_INTERVAL:任务重复间隔时间

      DURATION:持续时间

修改步骤如下:

技术分享

1.停止任务:SQL> BEGIN  2    DBMS_SCHEDULER.DISABLE(  3    name => ‘"SYS"."FRIDAY_WINDOW"‘,  4    force => TRUE);  5  END;  6  /PL/SQL 过程已成功完成。2.修改任务的持续时间,单位是分钟:SQL> BEGIN  2    DBMS_SCHEDULER.SET_ATTRIBUTE(  3    name => ‘"SYS"."FRIDAY_WINDOW"‘,  4    attribute => ‘DURATION‘,  5    value =http://www.mamicode.com/> numtodsinterval(180,‘minute‘));"SYS"."FRIDAY_WINDOW"‘,  4    attribute => ‘REPEAT_INTERVAL‘,  5    value =http://www.mamicode.com/> ‘FREQ=WEEKLY;BYDAY=MON;BYHOUR=2;BYMINUTE=0;BYSECOND=0‘);"SYS"."FRIDAY_WINDOW"‘);  4  END;  5  /PL/SQL 过程已成功完成。5.查看修改后的情况:SQL> select t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2  2  where t1.window_name=t2.window_name and t2.window_group_name in (‘MAINTENANCE_WINDOW_GROUP‘,‘BSLN_MAINTAIN_STATS_SCHED‘); WINDOW_NAME                    REPEAT_INTERVAL                                                                  DURATION------------------------------ -------------------------------------------------------------------------------- -------------------------------------------------------------------------------WEDNESDAY_WINDOW               freq=daily;byday=WED;byhour=22;byminute=0; bysecond=0                            +000 04:00:00FRIDAY_WINDOW                  FREQ=WEEKLY;BYDAY=MON;BYHOUR=2;BYMINUTE=0;BYSECOND=0                             +000 03:00:00SATURDAY_WINDOW                freq=daily;byday=SAT;byhour=6;byminute=0; bysecond=0                             +000 20:00:00THURSDAY_WINDOW                freq=daily;byday=THU;byhour=22;byminute=0; bysecond=0                            +000 04:00:00TUESDAY_WINDOW                 freq=daily;byday=TUE;byhour=22;byminute=0; bysecond=0                            +000 04:00:00SUNDAY_WINDOW                  freq=daily;byday=SUN;byhour=6;byminute=0; bysecond=0                             +000 20:00:00MONDAY_WINDOW                  freq=daily;byday=MON;byhour=22;byminute=0; bysecond=0                            +000 04:00:00 7 rows selected

技术分享

Oracle之自动收集统计信息