Speeding Up PL/SQL

Caching for PL/SQL Performance
Tim Hall
Oracle ACE Director
Oracle ACE of the Year 2006
OCP DBA (7, 8, 8i, 9i, 10g, 11g)
OCA PL/SQL Developer
http://www.oracle-base.com
Oracle PL/SQL Tuning (Rampant)
Oracle Job Scheduling (Rampant)
http://www.oracle-base.com
Caching for PL/SQL Performance
• Using arrays for lookup tables.
• Cross-Session PL/SQL Function Result Cache
• Query Result Cache
http://www.oracle-base.com
Using arrays for lookup tables
• Many systems have lots of small lookup tables.
• Most of these lookup tables contain static data.
• Data loads/extracts often access lookup tables for each row
processed.
• The overhead of these static lookups can be reduce
significantly by caching the data in associative arrays.
• setup.sql
• lookup_api.sql
• lookup_test.sql
• Using other collection types doesn’t work so well.
• lookup_api_incorrect.sql
• lookup_test.sql
http://www.oracle-base.com
Considerations when caching data
• Data Volatility – Only cache static data, or data you expect to be
static during the lifetime of your process. *
• Memory Usage – Collections are held in memory, so lots of
cached tables will waste lots of memory. Also, the caching is
session-specific, so lots of sessions means lots of memory
used. *
• Initialization Time – The data must be cached before
processing starts, which takes time. *
• * - Clever implementation can offset some of these issues.
• Lookup Type – This method is restricted to lookups using a
single “index” column. Prior to 9i R2, it is limited to number
based indexes.
• Bad Implementation – Test to make sure you are actually
seeing improvements.
http://www.oracle-base.com
PL/SQL Function Result Cache
•
•
•
•
•
•
•
•
The cross-session PL/SQL function result cache boosts the
performance of PL/SQL functions by caching results in the SGA.
The cache is accessible to any session calling the same function with
the same parameters.
Caching clause results in significant performance improvements when
functions are called for each row in an SQL query, or within a loop in
PL/SQL.
The RESULT_CACHE clause indicates a function is available for
caching.
pfrc_setup.sql & pfrc_test.sql
The optional RELIES_ON clause is used to specify dependent tables
and views so the result cache can be invalidated if the dependent
objects are modified.
relies_on.sql & pfrc_test.sql
Performance improvements depend on the nature of the work being
done.
http://www.oracle-base.com
SQL Result Cache
•
•
•
•
•
The query result cache stores the result of queries in the SGA.
The cache is accessible to any session calling the same query.
The RESULT_CACHE hint indicates the results of a query should be
cached.
qrc_setup.sql, qrc_test_1.sql & qrc_test_2.sql
The result cache is controlled by the RESULT_CACHE_MODE
parameter. Settings:
– MANUAL – (default) Query must include the RESULT_CACHE hint to be
cached.
– FORCE – Caching can be prevented using the NO_RESULT_CACHE hint.
•
qrc_force.sql
http://www.oracle-base.com
Cache Administration
• The query result cache and PL/SQL Function result cache use
same result cache memory pool.
• The pool is configured using the RESULT_CACHE_%
parameters.
• rc_params.sql
– result_cache_mode - MANUAL or FORCE control what and how
caching takes place.
– result_cache_max_size - Amount of SGA used for the cache. Zero
disables the feature.
– result_cache_max_result - The percentage of total cache any
single query can use.
– result_cache_remote_expiration - The number of minutes remote
objects remain valid. Zero means remote objects are not cached.
http://www.oracle-base.com
Cache Administration
• The DBMS_RESULT_CACHE package provides some cache
management (invalidations, flushing, bypass, memory usage).
• Performance information about the cache is displayed using
the V$RESULT_CACHE_% views.
• rc_views.sql
– V$RESULT_CACHE_DEPENDENCY - Displays dependencies
between cached results and database objects.
– V$RESULT_CACHE_MEMORY - Displays result cache memory
usage.
– V$RESULT_CACHE_OBJECTS - Displays attributes of cached
results and dependencies.
– V$RESULT_CACHE_STATISTICS - Displays various result cache
settings and usage statistics.
• The result cache memory pool is part of the SGA, so you need
free memory to make use of it.
http://www.oracle-base.com
Summary
• Caching lookup tables in arrays can improve performance for
batch operations.
• Manually caching volatile data is dangerous.
• Caching data requires memory, so be careful.
• The query result cache and PL/SQL function cache automate
caching for us in 11g.
• Always test to make sure the improvements are worth the
effort!
• Example code:
•
http://www.oracle-base.com/workshops/caching_for_plsql_performance/scripts.zip
http://www.oracle-base.com