Академический Документы
Профессиональный Документы
Культура Документы
Older code that does not take advantage of recent PL/SQL language features. Code that does many mathematical calculations Functions that are called from queries, which might run millions of times. Code that spends much time processing INSERT, UPDATE, or DELETE statements, or looping through query results. Investigate the FORALL statement for issuing DML and the BULK COLLECT clause Consider using a function-based index to precompute the results for each row and save query time. When a result-cached function is invoked, caching the results of PL/SQL functions in a shared global area (SGA), which is available to every session that runs your application the system checks the cache. If the cache contains the result from a previous call to the function with the same parameter values, the system returns the cached result to the invoker and does not reexecute the function body. If the cache does not contain the result, the system runs the function body and adds the result (for these parameter values) to the cache before returning control to the invoker.If the system needs more memory, it ages out (deletes) one or more cached results.
CREATE OR REPLACE PACKAGE department_pkg IS TYPE dept_info_record IS RECORD (average_salary NUMBER, number_of_employees NUMBER); -- Function declaration FUNCTION get_dept_info (dept_id NUMBER) RETURN dept_info_record RESULT_CACHE; END department_pkg; / CREATE OR REPLACE PACKAGE BODY department_pkg AS FUNCTION get_dept_info (dept_id NUMBER) RETURN dept_info_record RESULT_CACHE; IS rec dept_info_record; BEGIN SELECT AVG(SALARY), COUNT(*) INTO rec FROM EMPLOYEES WHERE DEPARTMENT_ID = dept_id; RETURN rec; END get_dept_info; END department_pkg; / DECLARE dept_id NUMBER := 50; avg_sal NUMBER; no_of_emp NUMBER; BEGIN avg_sal := department_pkg.get_dept_info(50).average_salary; no_of_emp := department_pkg.get_dept_info(50).number_of_employees; DBMS_OUTPUT.PUT_LINE('dept_id = ' ||dept_id); DBMS_OUTPUT.PUT_LINE('average_salary = '|| avg_sal); DBMS_OUTPUT.PUT_LINE('number_of_employees = ' ||no_of_emp); END; /
Some situations in which the body of a result-cached function runs are: The first time a session on this database instance invokes the function with these parameter values When the cached result for these parameter values is invalid When a change to any data source on which the function depends is committed, the cached result becomes invalid. When the cached results for these parameter values have aged out If the system needs memory, it might discard the oldest cached values. When the function bypasses the cache
BLOB CLOB NCLOB REF CURSOR Collection Object Record The return type is none of these: BLOB CLOB NCLOB REF CURSOR Object Record or PL/SQL collection that contains an unsupported return type The function computes the result instead of retrieving it from the cache. The result that the function computes is not added to the cache. Some examples of situations in which the cache is bypassed are: A session is performing a DML statement on a table or view on which a result-cached function depends. The session bypasses the result cache for that function until the DML statement is completedeither committed or rolled back. If the statement is rolled back, the session resumes using the cache for that function.