oracle 重新编译用户无效对象
admin
2023-05-15 01:42:10
0
oracle sys用户无效对象


select owner,object_name
, replace(object_type,' ','') object_type
,to_char(created,'yyyy-mm-dd') as created
,to_char(last_ddl_time,'yyyy-mm-dd') as last_ddl_time,
status
from dba_objects where status='INVALID' and owner='SYS';

OWNER   OBJECT_NAME           OBJECT_TYPE    CREATED     LAST_DDL_TIME  STATUS
------  --------------------- -------------  ----------- -------------- ----------
SYS     ALL_TAB_STATISTICS    VIEW           2011-09-17  2012-05-16     INVALID
SYS     USER_TAB_STATISTICS   VIEW           2011-09-17  2012-05-16     INVALID
SYS     ALL_IND_STATISTICS    VIEW           2011-09-17  2012-05-16     INVALID
SYS     USER_IND_STATISTICS   VIEW           2011-09-17  2012-05-16     INVALID
SYS     VALIDATE_ORDIM        PROCEDURE      2011-09-17  2012-05-16     INVALID
SYS     DBMS_CUBE_ADVISE      PACKAGEBODY    2011-09-17  2012-05-16     INVALID
SYS     DBMS_CUBE             PACKAGEBODY    2011-09-17  2012-05-16     INVALID

方法1:手动重新rebuilt

SQL>alter view sys.ALL_TAB_STATISTICS  compile; 

SQL>alter view sys.USER_TAB_STATISTICS compile;
 
SQL>alter view ALL_IND_STATISTICS      compile;
 
SQL>alter view sys.USER_IND_STATISTICS compile;

SQL>alter procedure sys.VALIDATE_ORDIM   compile;

SQL>alter package DBMS_CUBE_ADVISE compile body;
  
SQL>alter package DBMS_CUBE  compile body;


方法2:
oracle用户下执行

$cd $ORACLE_HOME/rdbms/admin
$sqlplus  /  as sysdba
SQL>@utlprp.sql


编译完成后,再次查看

SQL> select owner,object_name
  2  , replace(object_type,' ','') object_type
  3  ,to_char(created,'yyyy-mm-dd') as created
  4  ,to_char(last_ddl_time,'yyyy-mm-dd') as last_ddl_time,
  5  status
  6  from dba_objects where status='INVALID' and owner='SYS';

no rows selected         
                                                                                                                                                                                                                                                                                                                     方法3:
以下是一个转帖的方法
                                                                                                                           
--创建自动编译失效过程事务记录表
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;
/


相关内容

热门资讯

以军士兵集体丢掉武器抗命,大喊... 据凤凰卫视报道,以色列国防军一军事基地7月30日发生士兵抗命事件,约120名士兵抗议指挥官做法,将武...
蒋成华任商务部副部长 国务院任免国家工作人员。任命蒋成华为商务部副部长。免去蒋成华的商务部国际贸易谈判副代表职务。
伊朗驻华大使:在军事威胁下,不... 新华社北京7月31日电(记者刁慧琳) 伊朗驻华大使法兹里7月28日表示,伊美回到谈判桌的前提是美国必...
伊朗革命卫队在霍尔木兹海峡击中... 当地时间31日,伊朗伊斯兰革命卫队发布声明称,革命卫队海军当天在霍尔木兹海峡击中并扣留了两艘违反禁令...
美媒:特朗普,遇到了一个更强硬... 据《纽约时报》7月29日报道,就在特朗普总统看似放弃战事升级计划几天后,美国再次与伊朗交火。上周末,...
女子做气管镜时不幸身亡,丈夫称... 7月29日,西安刘先生反映妻子在当地医院做支气管镜检查时死亡,看监控时发现医生疑有违规操作。刘先生表...
全网“帮卖西瓜”,然后呢? 近日,河南部分地区西瓜滞销的消息在网上热度很高。很多地方也伸出援手:有景区收购千斤西瓜、免费赠予游客...
美媒:乌克兰袭击伊朗船只,险引... 据《纽约时报》7月28日报道,据伊朗和西方官员称,伊朗曾考虑攻击乌克兰的一个港口,以报复乌克兰对一艘...
20年里,他只画美女,用东方风... 迈进KIM在上海的工作室,迎面是一整墙的美女们。她们像是刚从一场时髦的沙龙里退场,或倚或立,眉宇间是...
Google在港推出AI代理G... 观点网讯:7月29日,Google在香港推出AI代理Gemini Spark,该代理可全天候在后台运...