Oracle统计信息收集维护

时间:2024-03-10 17:54:35

GATHER_STATS_JOB自动统计信息收集

对象之前从未收集过统计信息,或由于某些原因没有统计信息
对象的统计信息相对陈旧(stale)

收集周期:
工作日:22点开始,持续4小时
周末:6点开始,持续20小时

11g及以上:
查看自动收集任务及状态:(11g及以上)

select client_name,status from Dba_Autotask_Client where client_name='auto optimizer stats collection'; 

查看自动收集任务执行时间窗口:(11g及以上)

select WINDOW_NAME, WINDOW_NEXT_TIME, WINDOW_ACTIVE, OPTIMIZER_STATS 
  from DBA_AUTOTASK_WINDOW_CLIENTS 
 order by WINDOW_NEXT_TIME; 

关闭自动收集任务:

BEGIN 
      DBMS_AUTO_TASK_ADMIN.DISABLE( 
                     client_name => 'auto optimizer stats collection', 
                     operation => NULL, window_name => NULL); 
  END; 
 / 

开启自动收集任务:

BEGIN 
        DBMS_AUTO_TASK_ADMIN.ENABLE( 
                       client_name => 'auto optimizer stats collection', 
                       operation => NULL, window_name => NULL); 
    END; 
    / 

10g:
查看自动收集任务及状态:

select job_name,schedule_name,enabled,last_start_date,last_run_duration,next_run_date
from dba_scheduler_jobs a
where job_name = 'GATHER_STATS_JOB';

开启/关闭自动收集任务:
方法一:

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;

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

col WINDOW_NAME format a20
col DURATION format a20
col REPEAT_INTERVAL format a75
select t2.window_group_name,t1.window_name,t1.repeat_interval,t1.duration from dba_scheduler_windows t1,dba_scheduler_wingroup_members t2 where t1.window_name=t2.window_name;

修改统计信息执行的时间
–修改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;
/

查看统计信息执行的历史记录
–JOB运行历史记录

select * from dba_scheduler_job_log
where  job_name = 'GATHER_STATS_JOB';

–正在运行的job

select * from dba_scheduler_running_jobs;