重編譯 invalid 物件

jss001發表於2009-02-10

DBA在一些場合下,要對無效的資料庫物件進行重新編譯.如果只是對all_objects等檢視中選出來的無效物件進行重新編譯,可能需要很多次:因為各個物件之間有依賴性的.這樣並不是一個很好的辦法.Raymond 在Recompiling invalid objects提到了如何有效地重新編譯無效物件.提到了三種比較有效地方法(

  • 利 用$ORACLE_HOME/rdbms/admin下的utlrp.sql指令碼編譯.一般都是在遷移或者升級之後執行該指令碼.Raymond說該方法的 不足之處在於這個指令碼是對整個資料庫中的物件進行重新編譯的,所以不可取.有網友指出utlrp.sql 實際上是呼叫utlrcmp.sql的這樣就可以用utl_recomp包來做嘍(這樣就是比較好的方法).
  • 用DBMS_UTILITY包來進行編譯.但是也有一定的侷限性.
  • Raymond 提到了自己的解決辦法: 不過也立刻有人指出來,這樣對 View 的重新編譯是無能為力的(ALTER_COMPILE只能處理:PACKAGE, PACKAGE BODY, PROCEDURE, FUNCTION, TRIGGER).參見下面的指令碼
CREATE OR REPLACE PROCEDURE RECOMPILE_SCHEMA
IS
v_Type USER_OBJECTS.OBJECT_TYPE%TYPE;
v_Name USER_OBJECTS.OBJECT_NAME%TYPE;
v_Stat USER_OBJECTS.STATUS%TYPE;

CURSOR c_Obj
IS
SELECT BASE
FROM (SELECT A.OBJECT_ID BASE
, B.OBJECT_ID REL
FROM USER_OBJECTS A
, USER_OBJECTS B
, SYS.DEPENDENCY$ C
WHERE A.OBJECT_ID = C.D_OBJ#
AND B.OBJECT_ID = C.P_OBJ#
AND A.OBJECT_TYPE IN ('PACKAGE',
'PROCEDURE',
'FUNCTION',
'PACKAGE BODY',
-- 'VIEW',
'TRIGGER')
AND B.OBJECT_TYPE IN ('PACKAGE',
'PROCEDURE',
'FUNCTION',
'PACKAGE BODY',
-- 'VIEW',
'TRIGGER')
AND NOT A.OBJECT_NAME = B.OBJECT_NAME) OBJECTS
CONNECT BY BASE = PRIOR REL
GROUP BY BASE
ORDER BY MAX(LEVEL) DESC;
BEGIN
-- loop through all objects in order of dependancy.
FOR c_Row IN c_Obj
LOOP
-- select the objects attributes (type, name & status).
SELECT OBJECT_TYPE
, OBJECT_NAME
, STATUS
INTO v_Type
, v_Name
, v_Stat
FROM USER_OBJECTS
WHERE OBJECT_ID = c_Row.BASE;

-- if the OBJECT is INVALID, recompile it.
IF v_Stat = 'INVALID' THEN
DBMS_DDL.ALTER_COMPILE(v_Type, USER, v_Name);
END IF;
END LOOP;

-- Recompile all remaining INVALID OBJECTS (all those without dependencies).
FOR c_Row IN ( SELECT OBJECT_TYPE
, OBJECT_NAME
FROM USER_OBJECTS
WHERE STATUS = 'INVALID'
AND OBJECT_TYPE IN ('PACKAGE',
'PROCEDURE',
'FUNCTION',
'TRIGGER',
'PACKAGE BODY',
-- 'VIEW',
'TRIGGER') )
LOOP
DBMS_DDL.ALTER_COMPILE(c_Row.OBJECT_TYPE, USER, c_Row.OBJECT_NAME);
END LOOP;
END RECOMPILE_SCHEMA;
Rem   EXAMPLES
Rem 1. Recompile all objects sequentially:
Rem execute utl_recomp.recomp_serial();
Rem
Rem 2. Recompile objects in schema SCOTT sequentially:
Rem execute utl_recomp.recomp_serial('SCOTT');
Rem
Rem 3. Recompile all objects using 4 parallel threads:
Rem execute utl_recomp.recomp_parallel(4);
Rem
Rem 4. Recompile objects in schema JOE using the number of threads
Rem specified in the paramter JOB_QUEUE_PROCESSES:
Rem execute utl_recomp.recomp_parallel(NULL, 'JOE');
Rem
Rem 5. Recompile all objects using 2 parallel threads, but allow
Rem other applications to use the job queue concurrently:
Rem execute utl_recomp.recomp_parallel(2, NULL,
Rem utl_recomp.share_job_queue);
Rem
Rem 6. Restore the job queue after a failure in recomp_parallel:
Rem execute utl_recomp.restore_job_queue();
呼叫 utl_recomp 包是比較好的做法.需要 sys 許可權.[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18921899/viewspace-1017118/,如需轉載,請註明出處,否則將追究法律責任。

相關文章