Dani Schnider-Function Calls in SQL
Dani Schnider-Function Calls in SQL
@dani_schnider danischnider.wordpress.com
3 FUNCTION CALLS IN SQL
Nested
SQL Calls
Cardinality
Estimation
Index
Usage
Context
Switch
SQL FUNCTION CALLS
IN WHERE CONDITION
6 SIMPLE EXAMPLE
De
mo
Cardinality Index
Estimation Usage
0.01
Cardinality = Selectivity * TotalNumberOfRows
SELECT dbms_stats.create_extended_stats
(USER, 'addresses', '(UPPER(ctr_code))')
FROM dual
Cardinality Estimation
Index Usage
! Function-based Index
Index
o Function must be DETERMINISTIC Usage
o Index creation takes time and disk space
o Index is not optimal choice for all type of queries
Context
Switch
19 CONTEXT SWITCH BETWEEN PL/SQL AND SQL
De
mo
Source: Oracle News Connect, PL/SQL 101 series by Steven Feuerstein, https://www.oracle.com/news/connect/plsql-101-part-9.html
20 SOLUTION 1: USER DEFINED FUNCTION
! PRAGMA UDF
! Useful for PL/SQL functions mainly used in SQL statements
! Scalar expression that can be used in SELECT, WHERE, GROUP BY and ORDER BY clauses
! Processed in SQL statement directly, no context switch to PL/SQL
Any Ideas?
25 WHAT IS THE PROBLEM?
For queries with strong selectivity: Never use PL/SQL function calls with embedded
• create Function-based Index SQL queries!
(or index on virtual column) • Try to rewrite the SQL statement
danischnider.wordpress.com
https://danischnider.wordpress.com/2022/05/15/
performance-tips-pl-sql-functions-in-sql-queries/
https://danischnider.wordpress.com/2022/02/28/
performance-tips-function-calls-in-where-
conditions/
https://asktom.oracle.com/pls/apex/f?p=100:551::::551:P551_CLASS_ID,P551_INVITED:17183