Oracle数据库里提供了自动维护任务,以保证Oracle数据库可以更高效的运行。在不同的Oracle版本中,自动任务的种类和执行方式也有区别。本文以10g和11g为主来认识Oracle里的自动任务。
1、种类
10g里的自动任务有两种:
AUTO_SPACE_ADVISOR_JOB,自动空间顾问JOB
GATHER_STATS_JOB,自动收集统计信息JOB
11g里的自动任务有三种:
auto optimizer stats collection,自动统计信息收集顾问
auto space advisor,自动空间顾问
sql tuning advisor,sql调优顾问
2、实现方式
10g里的自动任务是以SCHEDULER的JOB形式运行的,可以在dba_scheduler_jobs视图里查到相关的JOB。
SQL> set linesize 200
SQL> col owner for a30
SQL> col job_name for a30
SQL> select owner,job_name from dba_scheduler_jobs where job_class='AUTO_TASKS_JOB_CLASS';
OWNER JOB_NAME
------------------------------ ------------------------------
SYS AUTO_SPACE_ADVISOR_JOB
SYS GATHER_STATS_JOB
11g里的自动任务使用的是11g的新特性AUTOTASK的方式实现的。可以从dba_autotask_client视图中查到相关信息。
sys@TEST>SELECT client_name,status FROM dba_autotask_client;
CLIENT_NAME STATUS
---------------------------------------- ------------------------
auto optimizer stats collection ENABLED
auto space advisor ENABLED
sql tuning advisor ENABLED
3、维护窗口时间
两个版本中自动任务的执行时间是不同的。
10g中分为两个时间段WEEKNIGHT_WINDOW和WEEKEND_WINDOW,可以从dba_scheduler_windows视图中查到。它们分别的运行开始时间和持续时间为:
WEEKNIGHT_WINDOW,每周一至周五晚22:00:00开始,持续8小时
WEEKEND_WINDOW,第周六00:00:00,持续2天
11g中维护窗口分得更细,分为7个,即第一天个维护窗口,可以针对不同的需求做出更细粒度的调整,dba_scheduler_windows视图中查到,分别为MONDAY_WINDOW,TUESDAY_WINDOW,WEDNESDAY_WINDOW,THURSDAY_WINDOW,FRIDAY_WINDOW,SATURDAY_WINDOW,SUNDAY_WINDOW。开始时间和持续时间分别为:
周一到周五晚22:00:00开始,持续4小时,周六和周日早6:00:00开始,执行20小时。
4、维护方式
由于10g和11g中实现方式的不同,维护方式自然就不一样了。
10g的维护方式与普通的SCHEDULER的JOB一样。
--停用JOB
SQL> exec dbms_scheduler.disable('AUTO_SPACE_ADVISOR_JOB');
PL/SQL procedure successfully completed.
SQL> select enabled from dba_scheduler_jobs where job_name='AUTO_SPACE_ADVISOR_JOB';
ENABLED
---------------
FALSE
--启用JOB
SQL> exec dbms_scheduler.ENABLE('AUTO_SPACE_ADVISOR_JOB');
PL/SQL procedure successfully completed.
SQL> select enabled from dba_scheduler_jobs where job_name='AUTO_SPACE_ADVISOR_JOB';
ENABLED
---------------
TRUE
下面重点介绍11g里自动任务的维护。
1)停用和启用自动维护任务
--停用sql tuning advisor
sys@TEST>BEGIN
2 dbms_auto_task_admin.disable(
3 client_name => 'sql tuning advisor',
4 operation => NULL,
5 window_name => NULL);
6 END;
7 /
PL/SQL procedure successfully completed.
sys@TEST>select client_name,status from dba_autotask_client;
CLIENT_NAME STATUS
---------------------------------------- ------------------------
auto optimizer stats collection ENABLED
auto space advisor ENABLED
sql tuning advisor DISABLED
--启用sql tuning advisor
sys@TEST>BEGIN
2 dbms_auto_task_admin.enable(
3 client_name => 'sql tuning advisor',
4 operation => NULL,
5 window_name => NULL);
6 END;
7 /
PL/SQL procedure successfully completed.
sys@TEST>select client_name,status from dba_autotask_client;
CLIENT_NAME STATUS
---------------------------------------- ------------------------
auto optimizer stats collection ENABLED
auto space advisor ENABLED
sql tuning advisor ENABLED
--停用所有维护窗口下的所有的自动任务
sys@TEST>exec dbms_auto_task_admin.disable;
PL/SQL procedure successfully completed.
sys@TEST>select window_name,autotask_status from dba_autotask_window_clients;
WINDOW_NAME AUTOTASK_STATUS
------------------------------ ------------------------
MONDAY_WINDOW DISABLED
TUESDAY_WINDOW DISABLED
WEDNESDAY_WINDOW DISABLED
THURSDAY_WINDOW DISABLED
FRIDAY_WINDOW DISABLED
SATURDAY_WINDOW DISABLED
SUNDAY_WINDOW DISABLED
7 rows selected.
--启用所有维护窗口下的所有自动任务
sys@TEST>exec dbms_auto_task_admin.enable;
PL/SQL procedure successfully completed.
sys@TEST>select window_name,autotask_status from dba_autotask_window_clients;
WINDOW_NAME AUTOTASK_STATUS
------------------------------ ------------------------
WEDNESDAY_WINDOW ENABLED
FRIDAY_WINDOW ENABLED
SATURDAY_WINDOW ENABLED
THURSDAY_WINDOW ENABLED
TUESDAY_WINDOW ENABLED
SUNDAY_WINDOW ENABLED
MONDAY_WINDOW ENABLED
7 rows selected.
--停用针对某个维护窗口的自动任务
sys@TEST>BEGIN
2 dbms_auto_task_admin.disable(
3 client_name => 'sql tuning advisor',
4 operation => NULL,
5 window_name => 'MONDAY_WINDOW');
6 END;
7 /
PL/SQL procedure successfully completed.
sys@TEST>select window_name,autotask_status,sql_tune_advisor from dba_autotask_window_clients;
WINDOW_NAME AUTOTASK_STATUS SQL_TUNE_ADVISOR
------------------------------ ------------------------ ------------------------
MONDAY_WINDOW ENABLED DISABLED
TUESDAY_WINDOW ENABLED ENABLED
WEDNESDAY_WINDOW ENABLED ENABLED
THURSDAY_WINDOW ENABLED ENABLED
FRIDAY_WINDOW ENABLED ENABLED
SATURDAY_WINDOW ENABLED ENABLED
SUNDAY_WINDOW ENABLED ENABLED
7 rows selected.
2、配置维护窗口
这个与10g的类似,都是使用dbms_scheduler包来操作
--修改维护窗口的属性,把SATURDAY_WINDOW的持续时间修改为4小时
sys@TEST>BEGIN
2 dbms_scheduler.disable(
3 name => 'SATURDAY_WINDOW');
4 dbms_scheduler.set_attribute(
5 name => 'SATURDAY_WINDOW',
6 attribute => 'DURATION',
7 value => numtodsinterval(4, 'hour'));
8 dbms_scheduler.enable(
9 name => 'SATURDAY_WINDOW');
10 END;
11 /
PL/SQL procedure successfully completed.
sys@TEST>SELECT duration FROM dba_scheduler_windows WHERE window_name='SATURDAY_WINDOW';
DURATION
---------------------------------------------------------------------------
+000 04:00:00