ORACLE FUNCTION函式中DETERMINISTIC測試

wisdomone1發表於2012-09-13
如果函式經常對於上述情況產生確定的記錄,可以在
   函式宣告中用DETERMINISTIC,這樣ORACLE會自動在記憶體中快取這些記錄集,如果不確定,
   產生的結果就會不確定了
          1,概念及含義
              1,語法
                  CREATE OR REPLACE FUNCTION SCHEMA.FUNTION_NAME
                  (ARGUMENT IN NOC0PY DATATYPE)
                  RETURN DATETYPE
                  DETERMINISTIC
                  IS
                  BEGIN
                  END;
               2,指定DETERMINISTIC用於,對於呼叫多次同一引數相同值的過程,返回相同的結果時
               3,如果你在基於函式的索引相關的表示式或者對REFRESH FAST及ENABLE QUERY REWRITE的物化檢視相關的查詢
                 ,必須要指定DETERMINISTIC關鍵字。
               4,如果以後變更了上述表示式的定義,必須手工重建物化檢視或者基於函式的索引
               5,如果一個函式使用包變數,或者訪問資料庫的方能會影響函式的結果集,則不要用DETERMINISTIC
               6,使用DETERMINISTIC語句的語義規則,此處皆指是定義或宣告而非使用
                     1,可以在最頂級的子程式中,指包中
                     2,可以在包規範即包級的子程式中,但不能在包體中;指包的子程式宣告中
                     3,不能在一個私有子程式中(子程式:另一個子程式內部或一個包體內部),即包體或另一個子程式中的子程式
                     4,一個有DETERMINISTIC的子程式能呼叫另一個子程式,
                        不管被呼叫的子程式是否宣告DETERMINISTIC
          2,示例
              1,CREATE OR REPLACE FUNCTION text_length(a CLOB)
                 RETURN NUMBER DETERMINISTIC IS
                 BEGIN
                    RETURN DBMS_LOB.GETLENGTH(a);
                 END;
 
              2,自己編寫的示例
                  1,SQL> desc t_deterministic;
                     名稱                                      是否為空? 型別
                     ----------------------------------------- -------- -----------------------
                   
                     A                                                  CHAR(2)
                   
                   
                    SQL> select * from t_deterministic;
                   
                    A
                    --
                    12
                    ab
             
             
                   2,  create or replace function func_deterministic(a_len t_deterministic.a%type
                      2  return number deterministic is
                      3  v_len number;
                      4  begin
                      5  select length(a) into v_len from t_deterministic where a=a_len;
                      6  return v_len;
                      7* end;
                    SQL> /
                   
                    函式已建立。
                   
                    3,SQL> select func_deterministic('ab') from t_deterministic;--查詢是NULL
                   
                    FUNC_DETERMINISTIC('AB')
                    ------------------------
                   
                   
                    SQL> select func_deterministic('12') from t_deterministic;--查詢有結果
                   
                    FUNC_DETERMINISTIC('12')
                    ------------------------
                                          12
                   
                   
                   
          3,加與不加它的區別
          4,哪些語句可以有DETERMINISTIC,此處指FUNCTION,PACKAGE,PACKAGE BODY,PROCEDURE,VIEW
                1,create procedure過程中不能有DETERMINISTIC
                2,CRREATE OR REPLACE PACKAGE的語法參考:Oracle? Database PL/SQL User's Guide and Reference
                     測試包規範
                        --經測試可在包規範中建立有DETERMINISTIC的函式宣告
                       CREATE OR REPLACE PACKAGE PKG_DETERMINISTIC
                       AS
                        FUNCTION FUNC_TEST
                         RETURN NUMBER DETERMINISTIC;
                        
                       END;
                      
                       --續上測試,包體也可以建立有DETERMINISTIC的函式宣告
                       CREATE OR REPLACE PACKAGE BODY PKG_DETERMINISTIC
                       IS
                       function FUNC_TEST
                       return number deterministic is
                       v_len number;
                        begin
                       select length(a) into v_len from t_deterministic;
                        return v_len;
                       end;
                       END PKG_DETERMINISTIC;

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

相關文章