Using Server Result Cache
By Ahmed Baraka
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Objectives
In this lecture, you will learn how to perform the following:
• Use server result cache with SQL Statements
• Use result cache materialized views (RCMV) for query rewrite
• Use server result cache with PL/SQL Functions
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Server Result Cache
SELECT E.SALES_MAN,
SUM(O.ORDER_TOTAL) SALES SGA
FROM EMP E, ORDERS O
WHERE ...
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Server Result Cache
SELECT E.SALES_MAN,
SUM(O.ORDER_TOTAL) SALES SGA
FROM EMP E, ORDERS O
WHERE ...
SALES_MAN TOTAL
PATRICK 120,550
JOHN 90,1500
KUMAR 450,000
RASHID 320,540
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
About Server Result Cache
• SQL and PL/SQL function results are cached in the memory
• Benefit:
- Boost the performance of the frequently executed queries and PL/SQL
functions
• If the underlying objects are updated, the query is re-executed
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Server Result Cache in SQL Statement
• To use it in a query:
SELECT /*+ RESULT_CACHE */ DEPT_NO, AVG(SALARY)
FROM EMP GROUP BY DEPT_NO;
...
----------------------------------------------------------
| Id | Operation | Name |
----------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | RESULT CACHE | 7qh2cc0uxgcjg7p88f1j4qmum7 |
| 2 | HASH GROUP BY | |
| 3 | TABLE ACCESS FULL| EMP |
----------------------------------------------------------
...
1 - column-count=2; dependencies=(HR.EMP); name="SELECT /*+
RESULT_CACHE */ DEPT_NO, AVG(SALARY)
FROM EMP GROUP BY DEPT_NO"
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Server Result Cache in SQL Statement
• To use it in a query:
SELECT /*+ RESULT_CACHE(NAME=CACHED_QUERY) */ DEPT_NO, AVG(SALARY)
FROM EMP GROUP BY DEPT_NO;
...
----------------------------------------------------------
| Id | Operation | Name |
----------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | RESULT CACHE | 7qh2cc0uxgcjg7p88f1j4qmum7 |
| 2 | HASH GROUP BY | |
| 3 | TABLE ACCESS FULL| EMP |
----------------------------------------------------------
...
Result Cache Information (identified by operation id):
1 - column-count=2; dependencies=(HR.EMP); name="CACHED_QUERY"
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Server Result Cache in WITH View
• To use it in a WITH view within a query:
WITH SUMMARY AS
( SELECT /*+ RESULT_CACHE */ DEPT_NO, AVG(SALARY) AVG_SAL
FROM EMP GROUP BY DEPT_NO )
SELECT D.*, S.AVG_SAL
FROM DEPT D, SUMMARY S
WHERE D.DEPT_NO = S.DEPT_NO;
...
...
|* 4 | SORT JOIN | |
| 5 | VIEW | |
| 6 | RESULT CACHE | 2g47yrth4v1a6fqts8hcj39gs7 |
| 7 | HASH GROUP BY | |
| 8 | TABLE ACCESS FULL | EMP |
--------------------------------------------------------------------
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Server Result Cache in SQL Statement
• To obtain details of result cache area contents:
SELECT ID, TYPE, BLOCK_COUNT, COLUMN_COUNT, PIN_COUNT, ROW_COUNT
FROM V$RESULT_CACHE_OBJECTS
WHERE CACHE_ID = '7qh2cc0uxgcjg7p88f1j4qmum7';
ID TYPE BLOCK_COUNT COLUMN_COUNT PIN_COUNT ROW_COUNT
--- ---------- ----------- ------------ ---------- ----------
6 Result 1 2 0 6
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Result Cache in Query Rewrite
• You can use result cache in running query rewrite
• Is called a result cache materialized view (RCMV)
• Benefit:
- When you cannot modify queries to apply the RESULT_CACHE hint
- A replacement to the materialize views
• Manage it using DBMS_ADVANCED_REWRITE
• Required privileges:
GRANT CREATE MATERIALIZED VIEW TO hr;
GRANT EXECUTE ON DBMS_ADVANCED_REWRITE TO hr;
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Result Cache Materialized View (RCMV)
BEGIN
sys.DBMS_ADVANCED_REWRITE.DECLARE_REWRITE_EQUIVALENCE
(NAME => 'RCMV_SALARY',
SOURCE_STMT =>'SELECT DEPT_NO, AVG(SALARY) FROM EMP GROUP BY
DEPT_NO',
DESTINATION_STMT =>'select * from
(SELECT /*+ RESULT_CACHE(NAME=CACHED_QUERY) */ DEPT_NO,
AVG(SALARY) FROM EMP GROUP BY DEPT_NO)',
VALIDATE => FALSE,
REWRITE_MODE => 'GENERAL'
);
END;
/
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Result Cache Materialized View (RCMV)
ALTER SESSION SET QUERY_REWRITE_INTEGRITY = STALE_TOLERATED;
SELECT DEPT_NO, AVG(SALARY) FROM EMP GROUP BY DEPT_NO;
...
------------------------------------------------------------
| Id | Operation | Name |
------------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | VIEW | |
| 2 | RESULT CACHE | 7qh2cc0uxgcjg7p88f1j4qmum7 |
| 3 | HASH GROUP BY | |
| 4 | TABLE ACCESS FULL| EMP |
------------------------------------------------------------
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Dropping RCMV query equivalence
begin
sys.DBMS_ADVANCED_REWRITE.DROP_REWRITE_EQUIVALENCE('RCMV_SALARY');
end;
/
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Using Result Cache with PL/SQL Functions
CREATE OR REPLACE FUNCTION GET_RSALES ( P_SALES_REP_ID NUMBER, P_YEAR
NUMBER ) RETURN NUMBER
RESULT_CACHE RELIES_ON (ORDERS)
IS
N NUMBER;
BEGIN
SELECT SUM(ORDER_TOTAL)
INTO N
FROM ORDERS
WHERE SALES_REP_ID=P_SALES_REP_ID
AND ORDER_DATE BETWEEN TO_DATE('01-01-' || P_YEAR,'DD-MM-YYYY')
AND TO_DATE('31-12-' || P_YEAR || ' 23:59:59','DD-MM-YYYY HH24:MI:SS');
RETURN (N);
END;
/
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka
Summary
In this lecture, you should have learnt how to perform the following:
• Use server result cache with SQL Statements
• Use result cache materialized views (RCMV) for query rewrite
• Use server result cache with PL/SQL Functions
Oracle© Database 12c SQL Tuning - a course by Ahmed Baraka