SlideShare a Scribd company logo
http://www.oracle-base.com
Improving the Performance of PL/SQL Function
Calls from SQL
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
http://www.oracle-base.com
What’s the problem?
 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.
 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)
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;
 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 cached values are not reusable in the current
session or in other sessions.
 (scalar.sql)
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.
 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.
 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.
 (deterministic.sql)
http://www.oracle-base.com
11g Result Cache
 11g introduced two new caching features.
 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)
 The Query Result Cache improves performance of complex queries that
return small number of rows. (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)
 Cached results can be reused in the same session and
in other sessions.
http://www.oracle-base.com
Manual Caching Using PL/SQL Collections
 Caching of function calls is nothing new.
 Caching using PL/SQL collections has been done for many years.
 Collections are session-specific, but cached values can be reused in
multiple queries.
 (collection.sql)
 Manually caching can cause problems for volatile data.
 Remember, collections use memory. Don’t go nuts!
http://www.oracle-base.com
Manual Caching Using Contexts
 Manual caching using contexts is similar to using collections.
 It shares many of the same drawbacks, but can allow a shared cache
between sessions.
 (context.sql)
http://www.oracle-base.com
Scalar Subquery Caching (Revisited)
 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.
 (plsql_result_cache_2.sql)
 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.
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;
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.
 If you are using table functions, consider switching to pipelined table
functions.
 Use Scalar Subquery Caching for functions in the WHERE clause if
possible.
 Avoid functions on indexed columns, or consider
function-based indexes.
(fbi.sql)
http://www.oracle-base.com
What did we cover?
 Scalar Subquery Caching
 DETERMINISTIC Hint
 Cross-Session PL/SQL Function Result Cache
 Manual Caching Using PL/SQL Collections
 Manual Caching Using Contexts
 Function calls in the FROM and WHERE clause
http://www.oracle-base.com
The End…
 Slides and Demos:
http://www.oracle-base.com/workshops
 Questions?

More Related Content

PPT
01 oracle architecture
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPTX
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
PPTX
5. stored procedure and functions
PPT
Oracle-L11 using Oracle flashback technology-Mazenet solution
PPT
Oracle SQL, PL/SQL Performance tuning
PPTX
Oracle Database 12c Release 2 - New Features On Oracle Database Exadata Expre...
DOCX
Oracle Database 12c "New features"
01 oracle architecture
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
5. stored procedure and functions
Oracle-L11 using Oracle flashback technology-Mazenet solution
Oracle SQL, PL/SQL Performance tuning
Oracle Database 12c Release 2 - New Features On Oracle Database Exadata Expre...
Oracle Database 12c "New features"

What's hot (18)

PDF
Flashback - The Time Machine..
PPTX
DBA Commands and Concepts That Every Developer Should Know
PPTX
Oracle flashback
PDF
New Stuff in the Oracle PL/SQL Language
PDF
Oracle 12c New Features for Developers
PPTX
Stored procedure in sql server
PPTX
High Performance Plsql
PPT
07 Using Oracle-Supported Package in Application Development
PPT
05 Creating Stored Procedures
PPTX
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
PPT
09 Managing Dependencies
PPT
Oracle Flashback Query 3
PPTX
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
PPTX
Oracle Data Redaction - EOUC
PPTX
Oracle’ın parallel execution yetenekleri ve performans
PPT
Less17 flashback tb3
PPT
Less08 managing data and concurrency
PPTX
Oracle Basics and Architecture
Flashback - The Time Machine..
DBA Commands and Concepts That Every Developer Should Know
Oracle flashback
New Stuff in the Oracle PL/SQL Language
Oracle 12c New Features for Developers
Stored procedure in sql server
High Performance Plsql
07 Using Oracle-Supported Package in Application Development
05 Creating Stored Procedures
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
09 Managing Dependencies
Oracle Flashback Query 3
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Oracle Data Redaction - EOUC
Oracle’ın parallel execution yetenekleri ve performans
Less17 flashback tb3
Less08 managing data and concurrency
Oracle Basics and Architecture
Ad

Viewers also liked (19)

PDF
Gestión de infraestructura tomcat/Tom EE con tfactory
PDF
It's raining data! Oracle databases in the cloud
PDF
Why to Upgrade to Oracle 12c
PDF
How Oracle Single/Multitenant will change a DBA's life
PDF
Best Features of Multitenant 12c
PDF
What's next after Upgrade to 12c
PDF
Pluggable Databases: What they will break and why you should use them anyway!
PDF
SOA y Microservices Diferencias y Aplicaciones
PDF
Integracion Continua en Oracle ADF
PDF
The best Oracle Database 12c Tuning Features for Developers
PDF
Deep Dive: More Oracle Data Pump Performance Tips and Tricks
PDF
Introduction to Oracle Clusterware 12c
PDF
How to Upgrade Hundreds or Thousands of Databases
PDF
Integration Cloud Service vs SOA
PDF
Upgrade/Migrate to Oracle 12c: Live and Uncensored!
PDF
ADF 12c como Backend
PPTX
Cloud Integration for Human Resources: Connect with Your talent in the Cloud
PPTX
La transformacion digital en nuestra vida cotidiana. Un vistazo a las APIs
PDF
Oracle 12c New Features
Gestión de infraestructura tomcat/Tom EE con tfactory
It's raining data! Oracle databases in the cloud
Why to Upgrade to Oracle 12c
How Oracle Single/Multitenant will change a DBA's life
Best Features of Multitenant 12c
What's next after Upgrade to 12c
Pluggable Databases: What they will break and why you should use them anyway!
SOA y Microservices Diferencias y Aplicaciones
Integracion Continua en Oracle ADF
The best Oracle Database 12c Tuning Features for Developers
Deep Dive: More Oracle Data Pump Performance Tips and Tricks
Introduction to Oracle Clusterware 12c
How to Upgrade Hundreds or Thousands of Databases
Integration Cloud Service vs SOA
Upgrade/Migrate to Oracle 12c: Live and Uncensored!
ADF 12c como Backend
Cloud Integration for Human Resources: Connect with Your talent in the Cloud
La transformacion digital en nuestra vida cotidiana. Un vistazo a las APIs
Oracle 12c New Features
Ad

Similar to Improving the Performance of PL/SQL function calls from SQL (20)

PPTX
The Amazing and Elegant PL/SQL Function Result Cache
PDF
Exploring plsql new features best practices september 2013
PDF
PDF
Meet the CBO in Version 11g
PPT
Myth busters - performance tuning 102 2008
PDF
11g Function Result Cache
PPT
Remote DBA Experts 11g Features
PPT
Oracle SQL, PL/SQL best practices
PPT
PL/SQL11g Question #1
PPTX
How to tune a query - ODTUG 2012
PPTX
OpenWorld 2018 - Common Application Developer Disasters
PDF
Performance improvements in PostgreSQL 9.5 and beyond
PPTX
Oracle result cache highload 2017
PPTX
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
ODP
Performance tuning
PPTX
Managing Unstructured Data: Lobs in the World of JSON
PPTX
PL/SQL User-Defined Functions in the Read World
PPTX
High Performance Plsql
ODP
SQL Tunning
PPTX
PLSQL Advanced
The Amazing and Elegant PL/SQL Function Result Cache
Exploring plsql new features best practices september 2013
Meet the CBO in Version 11g
Myth busters - performance tuning 102 2008
11g Function Result Cache
Remote DBA Experts 11g Features
Oracle SQL, PL/SQL best practices
PL/SQL11g Question #1
How to tune a query - ODTUG 2012
OpenWorld 2018 - Common Application Developer Disasters
Performance improvements in PostgreSQL 9.5 and beyond
Oracle result cache highload 2017
100500 способов кэширования в Oracle Database или как достичь максимальной ск...
Performance tuning
Managing Unstructured Data: Lobs in the World of JSON
PL/SQL User-Defined Functions in the Read World
High Performance Plsql
SQL Tunning
PLSQL Advanced

More from Guatemala User Group (11)

PPTX
More than 12 More things about Oracle Database 12c
PPTX
Oracle Optimizer: 12c New Capabilities
PPTX
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
PPTX
Monitoreo del performance de linux con sar
PPTX
Diagnóstico de problemas de red para DBAs
PDF
Cutting edge Essbase
PDF
Gestión de grandes volúmenes de información
PDF
RMAN – The Pocket Knife of a DBA
PDF
Oracle Linux and Oracle Database - A Trusted Combination
PDF
Re-­Think Mobile… Beyond Mobile­‐First: Oracle Mobile Strategy and Overview
PDF
Building Better Mobile Backends with Oracle Mobile Cloud Service
More than 12 More things about Oracle Database 12c
Oracle Optimizer: 12c New Capabilities
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Monitoreo del performance de linux con sar
Diagnóstico de problemas de red para DBAs
Cutting edge Essbase
Gestión de grandes volúmenes de información
RMAN – The Pocket Knife of a DBA
Oracle Linux and Oracle Database - A Trusted Combination
Re-­Think Mobile… Beyond Mobile­‐First: Oracle Mobile Strategy and Overview
Building Better Mobile Backends with Oracle Mobile Cloud Service

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Spectroscopy.pptx food analysis technology
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Advanced IT Governance
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Spectroscopy.pptx food analysis technology
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
Sensors and Actuators in IoT Systems using pdf
Advanced IT Governance
Cloud computing and distributed systems.
NewMind AI Monthly Chronicles - July 2025
GamePlan Trading System Review: Professional Trader's Honest Take

Improving the Performance of PL/SQL function calls from SQL

  • 1. http://www.oracle-base.com Improving the Performance of PL/SQL Function Calls from SQL 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.  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.  Sometimes we are able to change the query, but not the function. Sometimes we can change the function, but not the query. (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;  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 cached values are not reusable in the current session or in other sessions.  (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.  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.  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.  (deterministic.sql)
  • 5. http://www.oracle-base.com 11g Result Cache  11g introduced two new caching features.  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)  The Query Result Cache improves performance of complex queries that return small number of rows. (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)  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 using PL/SQL collections has been done for many years.  Collections are session-specific, but cached values can be reused in multiple queries.  (collection.sql)  Manually caching can cause problems for volatile data.  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.  It shares many of the same drawbacks, but can allow a shared cache between sessions.  (context.sql)
  • 8. http://www.oracle-base.com Scalar Subquery Caching (Revisited)  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.  (plsql_result_cache_2.sql)  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.
  • 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;
  • 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.  If you are using table functions, consider switching to pipelined table functions.  Use Scalar Subquery Caching for functions in the WHERE clause if possible.  Avoid functions on indexed columns, or consider function-based indexes. (fbi.sql)
  • 11. http://www.oracle-base.com What did we cover?  Scalar Subquery Caching  DETERMINISTIC Hint  Cross-Session PL/SQL Function Result Cache  Manual Caching Using PL/SQL Collections  Manual Caching Using Contexts  Function calls in the FROM and WHERE clause
  • 12. http://www.oracle-base.com The End…  Slides and Demos: http://www.oracle-base.com/workshops  Questions?