运行 作业的 PLSQL 块使用 dbms_scheduler 收集 30 个模式的统计信息

PLSQL block for run the job using dbms_scheduler for gather statistics of 30 schema

我想创建一个 PL/SQL 块,尝试使用 dbms_scheduler 包 运行 作业,我想收集所有 30 个模式的模式统计信息。例如:

begin
     dbms_scheduler_create_job(
     job_name => ....,
     job_type = > 'PL/SQL BLOCK',
     job_action => 'declare
      sch_lst dbms_stats.objecttab := dbms_stats.objecttab()
      begin
      sch_lst.extend(10);
      sch_lst(1).ownname := "ab";   --ab is the Schema name
      sch_lst(2).ownname := "cd";
      .........
      sch_lst(30).ownname := "xy";
      dbms_stats.gather_schema_stats( ......)
      end;
      /
      ',
     start_date => sysdate,
     ..........);
     end;
     /

首先,您可以创建这样一个程序:

create or replace procedure pr_schema_stats is
  sch_lst owa.vc_arr;
begin
  sch_lst(1) := 'ab';
  sch_lst(2) := 'cd';  
  sch_lst(3) := 'ef';
  sch_lst(4) := 'gh';  
  sch_lst(5) := 'ij';
  sch_lst(6) := 'kl';  
  sch_lst(7) := 'mn';
  sch_lst(8) := 'op';  
  sch_lst(9) := 'rs';  
  sch_lst(10):= 'tu';  

 for i in 1..10
 loop    
   dbms_stats.gather_schema_stats(upper(sch_lst(i)),degree => 4, cascade => true );      
 end loop;
end; 

然后从调度程序调用:

declare
    v_job_name varchar2(70) := 'jb_gather_stats';
begin  
    dbms_scheduler.create_job(
        job_name => v_job_name,
        job_type => 'STORED_PROCEDURE',
        job_action => 'pr_schema_stats', 
        start_date => to_date('04-12-2018 19:00:00', 'dd-mm-yyyy hh24:mi:ss'),
        repeat_interval => 'FREQ=MONTHLY;INTERVAL=1;',
        auto_drop => false,
        comments => 'Produces statistics for Cost based SQL statements');

    dbms_scheduler.enable(v_job_name);    
end;

编辑: 您可以将程序代码替换为:

create or replace procedure pr_schema_stats is   
begin

 for c in (   
           select u.username,
                 row_number() over (order by u.username) as rn
            from dba_users u
           where u.account_status = 'OPEN' 
             and u.username not like 'SYS%' 
           )
 loop
  begin
    dbms_stats.gather_schema_stats(c.username,degree => 4, cascade => true );      
   exception when others then 
    dbms_output.put_line(sqlerrm);
  end;
 end loop;
end;

在分析任务中包含所有普通模式。

start_date => sysdate, 之前删除 / 并在架构名称中使用 "(双引号)代替 " =]''(双单引号),因为它在已经在单引号内的声明语句中。

begin
 dbms_scheduler_create_job(
 job_name => ....,
 job_type = > 'PL/SQL BLOCK',
 job_action => 'declare
  sch_lst dbms_stats.objecttab := dbms_stats.objecttab()
  begin
  sch_lst.extend(10);
  sch_lst(1).ownname := ''ab'';   --ab is the Schema name
  sch_lst(2).ownname := ''cd'';
  .........
  sch_lst(10).ownname := ''kl'';
  dbms_stats.gather_schema_stats( ......)
  end;
  ',
 start_date => sysdate,
 ..........);
 end;
 /

然后编译这个。您可以使用 execute dbms_schedule.run_job('<job_name>');

检查作业