Oracle失效对象自动重新编译

--创建自动编译失效过程事务记录表
declare
  tabcnt integer := 0;
begin
  select count(*) into tabcnt from dba_tables where table_name='RECOMPILE_LOG';
  if tabcnt = 0 then
    execute immediate 'create table recompile_log(rdate date,errmsg varchar2(200))';
  end if;
end;
/


--创建编译失效对象的存储过程
create or replace procedure recompile_invalid_objects  
as
  str_sql varchar2(200);  --中间用到的sql语句
  p_owner varchar2(20);   --所有者名称,即SCHEMA
  errm varchar2(200);     --中间错误信息
begin
  /*****************************************************/
  p_owner := 'owner';/***用户名*************************/
  /*****************************************************/ 
  insert into recompile_log(rdate, errmsg) values(sysdate,'time to recompile invalid objects'); 
  
  --编译失效存储过程
  for invalid_procedures in (select object_name from all_objects
    where status = 'INVALID' and object_type = 'PROCEDURE' and owner=upper(p_owner))
  loop
    str_sql := 'alter procedure ' ||invalid_procedures.object_name || ' compile';
    begin
      execute immediate str_sql;
    exception
      When Others Then
      begin
        errm := 'error by obj:'||invalid_procedures.object_name||' '||sqlerrm;
        insert into recompile_log(rdate, errmsg) values(sysdate,errm);
      end;
    end;
  end loop;
  
  --编译失效函数
  for invalid_functions in (select object_name from all_objects
    where status = 'INVALID' and object_type = 'FUNCTION' and owner=upper(p_owner))
  loop
    str_sql := 'alter function ' ||invalid_functions.object_name || ' compile';
    begin
      execute immediate str_sql;
    exception
      When Others Then
      begin
        errm := 'error by obj:'||invalid_functions.object_name||' '||sqlerrm;
        insert into recompile_log(rdate, errmsg) values(sysdate,errm);
      end;
    end;
  end loop;


  --编译失效包
  for invalid_packages in (select object_name from all_objects
    where status = 'INVALID' and object_type = 'PACKAGE' and owner=upper(p_owner))
  loop
    str_sql := 'alter package ' ||invalid_packages.object_name || ' compile';
    begin
      execute immediate str_sql;
    exception
      When Others Then
      begin
        errm := 'error by obj:'||invalid_packages.object_name||' '||sqlerrm;
        insert into recompile_log(rdate, errmsg) values(sysdate,errm);
      end;
    end;
  end loop;
  
  --编译失效类型
  for invalid_types in (select object_name from all_objects
    where status = 'INVALID' and object_type = 'TYPE' and owner=upper(p_owner))
  loop
    str_sql := 'alter type ' ||invalid_types.object_name || ' compile';
    begin
      execute immediate str_sql;
    exception
      When Others Then
      begin
        errm := 'error by obj:'||invalid_types.object_name||' '||sqlerrm;
        insert into recompile_log(rdate, errmsg) values(sysdate,errm);
      end;
    end;
  end loop;


  --编译失效索引
  for invalid_indexs in (select object_name from all_objects
    where status = 'INVALID' and object_type = 'INDEX' and owner=upper(p_owner))
  loop
    str_sql := 'alter index ' ||invalid_indexs.object_name || ' rebuild';
    begin
      execute immediate str_sql;
    exception
      When Others Then
      begin
        errm := 'error by obj:'||invalid_indexs.object_name||' '||sqlerrm;
        insert into recompile_log(rdate, errmsg) values(sysdate,errm);
      end;
    end;
  end loop;


  --编译失效触发器
  for invalid_triggers in (select object_name from all_objects
    where status = 'INVALID' and object_type = 'TRIGGER' and owner=upper(p_owner))
  loop
    str_sql := 'alter trigger ' ||invalid_triggers.object_name || ' compile';
    begin
      execute immediate str_sql;
    exception
      When Others Then
      begin
        errm := 'error by obj:'||invalid_triggers.object_name||' '||sqlerrm;
        insert into recompile_log(rdate, errmsg) values(sysdate,errm);
      end;
    end;
  end loop;
 
end;
/


--创建任务计划,每天早上8点整执行该任务,且保证此任务有且只有一个
declare 
  jobcnt integer :=0;
  job_recompile number := 0;
  str_sql varchar2(200);
begin 
  select count(*) into jobcnt from all_jobs where what = 'recompile_invalid_objects;' and broken = 'N';
  if jobcnt > 0 then
    for jobs in (select job from all_jobs where what = 'recompile_invalid_objects;' and broken = 'N')
    loop
      str_sql := 'begin dbms_job.remove('||jobs.job||'); end;';
      begin
        execute immediate str_sql;
      exception
        When Others Then null;
      end;
    end loop; 
  end if;
  --创建任务计划
  dbms_job.submit(job_recompile,'recompile_invalid_objects;',sysdate,'TRUNC(SYSDATE + 1) + 8/24');
  --启动任务计划
  dbms_job.run(job_recompile);
end;
/
日常管理维护一个oracle数据库服务器的时,经常会碰到修改view,table结构的情况,而且由于oracle view,函数,存储过程对象的相互关联的关系,经常会由于一个view,table,fun,proc的修改而导致相关的对象失效。而实施的时候,经常只会注意要修改的对象是否修改完成,往往忽略相关对象失效问题,所以做了一个自动重新编译的脚本程序,目的是定期(10分钟)完成一次对所有对象的检查,如果有失效对象,则对其进行重新编译。 机制是:基于linux的crontab,定期执行下述脚本,对失效对象,执行alter object_type object_name compile;语句,达到重新编译。 如果扩展该脚本,可以完成对失效对象进行告警等管理的需要。 该脚本运行于oracle9i,linux 环境下面 #!/bin/sh nowdir=`pwd` #配置文件的生成日期 nowtime=`date '+%Y%m%d'` nowtime_h=`date '+%Y%m%d%H%M'` #脚本执行的目录 dmpdir=/oracle_script/auto_recompile #初始化参数 cd #执行oracle 相关的环境变量, . .bash_profile cd $dmpdir #下面取出失效对象('TRIGGER','PROCEDURE','FUNCTION','VIEW), #并且去掉系统的对象,只针对用户自己部署的。 #如果有失效对象则完成alter sql语句。 rm -rf param.temp $ORACLE_HOME/bin/sqlplus -SILENT "/ as sysdba" <<eof set pagesize 1000 set linesize 100 set heading off set feedback off column table_name format a30 spool param.temp select 'alter '||a.object_type||' '||a.owner||'.'||a.object_name ||' compile;' from dba_objects a where a.object_type in('TRIGGER','PROCEDURE','FUNCTION','VIEW') and a.status='INVALID' and a.owner not in ('SYS' ,'SYSTEM','OUTLN','U_SYSTEM'); spool off exit; eof # Start to backup oracle database param echo "-------------- Complete obj compile Started on `date` -------------" >>$dmpdir/do.log dofilelog="obj_compile"$nowtime_h'.log' $ORACLE_HOME/bin/sqlplus -SILENT "/ as sysdba" <<eof SET serveroutput on long 999999 set pagesize 0 set linesize 300 spool $dofilelog @param.temp spool off exit; eof cat param.temp >> $dofilelog echo "-------------- Complete export End on `date` -------------" >>$dmpdir/do.log
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值