Professional Documents
Culture Documents
Improving Performance With Caching
Improving Performance With Caching
Improving Performance With Caching
SGA
Shared pool
Default
result
cache
Increased
result
cache
execute dbms_result_cache.memory_report
R e s u l t C a c h e M e m o r y R e p o r t
[Parameters]
Block Size = 1K bytes
Maximum Cache Size = 1056K bytes (1056 blocks)
Maximum Result Size = 52K bytes (52 blocks)
[Memory]
Total Memory = 5140 bytes [0.003% of the Shared Pool]
... Fixed Memory = 5140 bytes [0.003% of the Shared Pool]
... Dynamic Memory = 0 bytes [0.000% of the Shared Pool]
• Definition:
– Cache the results of the current query or query fragment in
memory, and then use the cached results in future executions
of the query or query fragments.
– Cached results reside in the result cache memory portion of
the SGA.
• Benefits:
– Improved performance
• Scenario:
– You need to find the greatest average value of credit limit
grouped by state over the whole population.
– The query results in a large number of rows being analyzed to
yield a few or one row.
– In your query, the data changes fairly slowly (say every hour)
but the query is repeated fairly often (say every second).
• Solution:
– Use the new optimizer hint /*+ result_cache */ in your
query:
SELECT /*+ result_cache */
AVG(cust_credit_limit), cust_state_province
FROM sh.customers
GROUP BY cust_state_province;
--- flush.sql
--- Start with a clean slate. Flush the cache and shared pool.
--- Verify that memory was released.
connect / as sysdba
SET ECHO ON
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 150
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 1000
SET SERVEROUTPUT ON
execute dbms_result_cache.flush
alter system flush shared_pool
/
execute dbms_result_cache.memory_report
R e s u l t C a c h e M e m o r y R e p o r t
[Parameters]
Block Size = 1K bytes
Maximum Cache Size = 1056K bytes (1056 blocks)
Maximum Result Size = 52K bytes (52 blocks)
[Memory]
Total Memory = 5140 bytes [0.003% of the Shared Pool]
... Fixed Memory = 5140 bytes [0.003% of the Shared Pool]
... Dynamic Memory = 0 bytes [0.000% of the Shared Pool]
PL/SQL procedure successfully completed.
--- query3.sql
--- Cache result of both queries, then use the cached result.
. . . .
connect oe
set echo on 2453 04-OCT-99 06.53.34.362632 PM
direct 116 0 129 153
select /*+ result_cache q_name(Q1) */ * from orders
/ 2456 07-NOV-98 06.53.25.989889 PM
direct 117 0 3878.4 163
--- query3.sql
--- Cache result of both queries, then use the cached result.
. . . .
connect oe
set echo on 2453 04-OCT-99 06.53.34.362632 PM
direct 116 0 129 153
select /*+ result_cache q_name(Q1) */ * from orders
/ 2456 07-NOV-98 06.53.25.989889 PM
direct 117 0 3878.4 163
• Definition:
– Enables data that is stored in cache to be shared across
sessions
– Stores the function result cache in an SGA, making it available
to any session that runs your application
• Benefits:
– Improved performance
– Improved scalability
• Scenario:
– You need a PL/SQL function that derives a complex metric.
– The data that your function calculates changes slowly, but the
function is frequently called.
• Solution:
– Use the new RESULT_CACHE clause in your function
definition.
– You can also have the cache purged when a dependent table
experiences a DML operation, by using the RELIES_ON
clause.
--- flush.sql
--- Start with a clean slate. Flush the cache and shared pool.
--- Verify that memory was released.
connect / as sysdba
SET ECHO ON
SET FEEDBACK 1
SET NUMWIDTH 10
SET LINESIZE 150
SET TRIMSPOOL ON
SET TAB OFF
SET PAGESIZE 1000
SET SERVEROUTPUT ON
execute dbms_result_cache.flush
alter system flush shared_pool
/
execute dbms_result_cache.memory_report
R e s u l t C a c h e M e m o r y R e p o r t
[Parameters]
Block Size = 1K bytes
Maximum Cache Size = 1056K bytes (1056 blocks)
Maximum Result Size = 52K bytes (52 blocks)
[Memory]
Total Memory = 5140 bytes [0.003% of the Shared Pool]
... Fixed Memory = 5140 bytes [0.003% of the Shared Pool]
... Dynamic Memory = 0 bytes [0.000% of the Shared Pool]
return v_count;
end;
SQL> connect oe
Connected.
SQL> select cust_last_name, ord_count(customer_id) no_of_orders
2 from customers
3 where cust_last_name = 'MacGraw';
CUST_LAST_NAME NO_OF_ORDERS
-------------------- ------------
MacGraw 4
1 row selected.
SQL> connect oe
Connected.
SQL> select cust_last_name, ord_count(customer_id) no_of_orders
2 from customers
3 where cust_last_name = 'MacGraw';
CUST_LAST_NAME NO_OF_ORDERS
-------------------- ------------
MacGraw 4
1 row selected.