从10g开始,Oracle在建库后就默认创建了一个名为GATHER_STATS_JOB的定时任务,用于自动收集CBO的统计信息。这个自动任务默认情况下在工作日晚上10:00-6:00和周末全天开启。
1、查看自动收集统计信息的任务及状态:
SQL> select job_name,schedule_name,enabled,last_start_date,last_run_duration,next_run_date 2 from dba_scheduler_jobs a 3 where job_name = 'GATHER_STATS_JOB'
2、启用/禁止自动收集统计信息的任务
方法一: exec dbms_scheduler.disable('SYS.GATHER_STATS_JOB'); exec dbms_scheduler.enable('SYS.GATHER_STATS_JOB'); 方法二: alter system set "_optimizer_autostats_job"=false scope=spfile; alter system set "_optimizer_autostats_job"=true scope=spfile;
3、获得当前自动收集统计信息的执行时间
SQL> col WINDOW_NAME format a20 SQL> col DURATION format a20 SQL> col REPEAT_INTERVAL format a75 SQL> select t2.window_group_name,t1.window_name,t1.repeat_interval,t1.duration 2 from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2 3 where t1.window_name=t2.window_name; WINDOW_GROUP_NAME WINDOW_NAME REPEAT_INTERVAL DURATION ------------------------------ -------------------- --------------------------------------------------------------------------- -------------------- MAINTENANCE_WINDOW_GROUP WEEKNIGHT_WINDOW freq=daily;byday=MON,TUE,WED,THU,FRI;byhour=22;byminute=0; bysecond=0 +000 08:00:00 MAINTENANCE_WINDOW_GROUP WEEKEND_WINDOW freq=daily;byday=SAT;byhour=0;byminute=0;bysecond=0 +002 00:00:00 SQL>
4、修改统计信息执行的时间
--修改WEEKEND_WINDOW的配置 (改成和WEEKNIGHT_WINDOW相同,即周一~周五,每日的22:00向后8小时,至次日凌晨6点) begin dbms_scheduler.set_attribute('WEEKEND_WINDOW','REPEAT_INTERVAL','freq=daily;byday=SAT,SUN;byhour=22;byminute=0;bysecond=0'); dbms_scheduler.set_attribute('WEEKEND_WINDOW','DURATION','+000 08:00:00'); end; / --若要还原成以前默认设置,可执行如下 --周末两天都是全天: begin dbms_scheduler.set_attribute('WEEKEND_WINDOW','REPEAT_INTERVAL','freq=daily;byday=SAT;byhour=0;byminute=0;bysecond=0'); dbms_scheduler.set_attribute('WEEKEND_WINDOW','DURATION','+002 00:00:00'); end; /
5、查看统计信息执行的历史记录
--JOB运行历史记录 select * from dba_scheduler_job_log where job_name = 'GATHER_STATS_JOB' --正在运行的job select * from dba_scheduler_running_jobs