Download presentation
Presentation is loading. Please wait.
Published byChristine Johnson Modified over 9 years ago
1
http://www.oracle-base.com From Zero to Hero : Using an assortment of caching techniques to improve performance of SQL containing PL/SQL calls. Tim Hall Oracle ACE Director Oracle ACE of the Year 2006 OakTable Network OCP DBA (7, 8, 8i, 9i, 10g, 11g) OCP Advanced PL/SQL Developer Oracle Database: SQL Certified Expert http://www.oracle-base.com Books Oracle PL/SQL Tuning Oracle Job Scheduling
2
http://www.oracle-base.com What’s the problem? We sometimes need to call PL/SQL functions in the select list of queries.We sometimes need to call PL/SQL functions in the select list of queries. By default, the function may be called for each row returned.By default, the function may be called for each row returned. If the function is called repeatedly with the same input parameters, this can represent a massive waste of resources.If the function is called repeatedly with the same input parameters, this can represent a massive waste of resources. Sometimes we are able to change the query, but not the function. Sometimes we can change the function, but not the query. (setup.sql)Sometimes we are able to change the query, but not the function. Sometimes we can change the function, but not the query. (setup.sql)setup.sql
3
http://www.oracle-base.com Scalar Subquery Caching Rewriting function calls as scalar subqueries allows Oracle to cache the results. SELECT (SELECT slow_function(id) FROM dual) FROM func_test;Rewriting function calls as scalar subqueries allows Oracle to cache the results. SELECT (SELECT slow_function(id) FROM dual) FROM func_test; Oracle sets up an in-memory hash table to cache results of scalar subqueries.Oracle sets up an in-memory hash table to cache results of scalar subqueries. The cache only last for the lifetime of the query.The cache only last for the lifetime of the query. The cached values are not reusable in the current session or in other sessions.The cached values are not reusable in the current session or in other sessions. (scalar.sql)(scalar.sql)scalar.sql
4
http://www.oracle-base.com DETERMINISTIC Hint The DETERMINISTIC hint has been available for a long time, but didn’t seem to do much until 10g.The DETERMINISTIC hint has been available for a long time, but didn’t seem to do much until 10g. Oracle will optimize calls to functions marked as DETERMINISTIC to improve query performance.Oracle will optimize calls to functions marked as DETERMINISTIC to improve query performance. The caching is based on the array size of fetch, so mileage can vary.The caching is based on the array size of fetch, so mileage can vary. Cached return values only last for the lifetime of the call.Cached return values only last for the lifetime of the call. The cached values are not reusable in the current session or in other sessions.The cached values are not reusable in the current session or in other sessions. (deterministic.sql)(deterministic.sql)deterministic.sql
5
http://www.oracle-base.com 11g Result Cache 11g introduced two new caching features.11g introduced two new caching features. Both share the same pool of memory in the SGA, controlled using:Both share the same pool of memory in the SGA, controlled using: –RESULT_CACHE_% parameters. –DBMS_RESULT_CACHE package. –V$RESULT_CACHE_% views. ( result_cache.sql) result_cache.sql The Query Result Cache improves performance of complex queries that return small number of rows. (query_result_cache.sql)The Query Result Cache improves performance of complex queries that return small number of rows. (query_result_cache.sql)query_result_cache.sql The Cross-Session PL/SQL Function Result Cache improves performance of function calls by caching the return values. (plsql_result_cache.sql)The Cross-Session PL/SQL Function Result Cache improves performance of function calls by caching the return values. (plsql_result_cache.sql)plsql_result_cache.sql Cached results can be reused in the same session and in other sessions.Cached results can be reused in the same session and in other sessions.
6
http://www.oracle-base.com Manual Caching Using PL/SQL Collections Caching of function calls is nothing new.Caching of function calls is nothing new. Caching using PL/SQL collections has been done for many years.Caching using PL/SQL collections has been done for many years. Collections are session-specific, but cached values can be reused in multiple queries.Collections are session-specific, but cached values can be reused in multiple queries. (collection.sql)(collection.sql)collection.sql Manually caching can cause problems for volatile data.Manually caching can cause problems for volatile data. Remember, collections use memory. Don’t go nuts!Remember, collections use memory. Don’t go nuts!
7
http://www.oracle-base.com Manual Caching Using Contexts Manual caching using contexts is similar to using collections.Manual caching using contexts is similar to using collections. It shares many of the same drawbacks, but can allow a shared cache between sessions.It shares many of the same drawbacks, but can allow a shared cache between sessions. (context.sql)(context.sql)context.sql
8
http://www.oracle-base.com Scalar Subquery Caching (Revisited) Q: Do other caching methods make scalar subquery caching irrelevant?Q: Do other caching methods make scalar subquery caching irrelevant? A: No. Alternative caching mechanisms don’t reduce context switching between SQL and PL/SQL.A: No. Alternative caching mechanisms don’t reduce context switching between SQL and PL/SQL. (plsql_result_cache_2.sql)(plsql_result_cache_2.sql)plsql_result_cache_2.sql You should always use scalar subquery caching, even when using other caching mechanisms.You should always use scalar subquery caching, even when using other caching mechanisms. Scalar Subquery Caching reduces context switches, but other methods have added benefits reuse between queries and sessions.Scalar Subquery Caching reduces context switches, but other methods have added benefits reuse between queries and sessions.
9
http://www.oracle-base.com Is there anything relevant in 12c? Functions (and procedures) in the WITH clause. Reduces the context switching associated with PL/SQL calls, which improves performance. WITH FUNCTION slow_function(p_id IN NUMBER) IS BEGIN RETURN p_id; END slow_function; SELECT slow_function(id) FROM test_func;Functions (and procedures) in the WITH clause. Reduces the context switching associated with PL/SQL calls, which improves performance. WITH FUNCTION slow_function(p_id IN NUMBER) IS BEGIN RETURN p_id; END slow_function; SELECT slow_function(id) FROM test_func;
10
http://www.oracle-base.com What about the FROM and WHERE clause? Function calls in the select list of an inline view follow the same rules as for the main select list.Function calls in the select list of an inline view follow the same rules as for the main select list. If you are using table functions, consider switching to pipelined table functions.If you are using table functions, consider switching to pipelined table functions. Use Scalar Subquery Caching for functions in the WHERE clause if possible.Use Scalar Subquery Caching for functions in the WHERE clause if possible. Avoid functions on indexed columns, or consider function-based indexes. (fbi.sql)Avoid functions on indexed columns, or consider function-based indexes. (fbi.sql)fbi.sql
11
http://www.oracle-base.com What did we cover? Scalar Subquery CachingScalar Subquery Caching DETERMINISTIC HintDETERMINISTIC Hint Cross-Session PL/SQL Function Result CacheCross-Session PL/SQL Function Result Cache Manual Caching Using PL/SQL CollectionsManual Caching Using PL/SQL Collections Manual Caching Using ContextsManual Caching Using Contexts Function calls in the FROM and WHERE clauseFunction calls in the FROM and WHERE clause
12
http://www.oracle-base.com The End… Slides and Demos: http://www.oracle-base.com/workshopsSlides and Demos: http://www.oracle-base.com/workshops http://www.oracle-base.com/workshops Questions?Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.