This is a late answer to a long answer, but I just wanted to add that Oracle provides a caching mechanism for functions with mutable dependencies. RESULT_CACHE is an alternative to DETERMINISTIC , which allows Oracle to DETERMINISTIC results of the cache function at any time when the referenced object changes.
In this way, it is safe to cache expensive calculations with rarely updated objects that the cached results will not return incorrect results.
Here is an example of using mythological monsters:
CREATE TABLE MONSTER ( MONSTER_NAME VARCHAR2(100) NOT NULL PRIMARY KEY ); INSERT INTO MONSTER VALUES ('Chthulu'); INSERT INTO MONSTER VALUES ('Grendel'); INSERT INTO MONSTER VALUES ('Scylla'); INSERT INTO MONSTER VALUES ('Nue'); COMMIT; CREATE OR REPLACE PACKAGE MONSTER_PKG IS FUNCTION IS_THIS_A_MONSTER(P_MONSTER_NAME IN VARCHAR2) RETURN BOOLEAN RESULT_CACHE; END MONSTER_PKG; / CREATE OR REPLACE PACKAGE BODY MONSTER_PKG IS FUNCTION IS_THIS_A_MONSTER(P_MONSTER_NAME IN VARCHAR2) RETURN BOOLEAN RESULT_CACHE RELIES_ON (MONSTER) IS V_MONSTER_COUNT NUMBER(1, 0) := 0; BEGIN SELECT COUNT(*) INTO V_MONSTER_COUNT FROM MONSTER WHERE MONSTER_NAME = P_MONSTER_NAME; RETURN (V_MONSTER_COUNT > 0); END; END MONSTER_PKG; /
When a scenario like the one below occurs, any existing cache is invalid and the new cache may be rebuilt.
BEGIN DBMS_OUTPUT.PUT_LINE('Is Kraken initially a monster?'); IF MONSTER_PKG.IS_THIS_A_MONSTER('Kraken') THEN DBMS_OUTPUT.PUT_LINE('Kraken is initially a monster'); ELSE DBMS_OUTPUT.PUT_LINE('Kraken is not initially a monster'); END IF; INSERT INTO MONSTER VALUES ('Kraken'); COMMIT; DBMS_OUTPUT.PUT_LINE('Is Kraken a monster after update?'); IF MONSTER_PKG.IS_THIS_A_MONSTER('Kraken') THEN DBMS_OUTPUT.PUT_LINE('Kraken is now a monster'); ELSE DBMS_OUTPUT.PUT_LINE('Kraken is not now a monster'); END IF; END; /
Is Kraken originally a monster?
Kraken is not originally a monster
Is Kraken a monster after upgrade?
Kraken is now a monster
source share