SlideShare a Scribd company logo
1 of 50
1
12c
2
the focus ...
the day to day stuff
Feature
inline PLSQL in SQL
most systems ...
... I've worked on
struggles with case
no correlation :-)
SQL> select vendor
2 from service_provider;
VENDOR
------------------------------
jones
brown
SMITH
sigh...
"no problem.... I'll use initcap"
SQL> select initcap(vendor)
2 from service_provider;
INITCAP(VENDOR)
------------------------------
Jones
Brown
Smith
until ...
SQL> select initcap(vendor)
2 from service_provider;
INITCAP(VENDOR)
------------------------------
Jones
Brown
Smith
Mcdonald
Johnson'S
uh oh
home grown
SQL> create or replace
2 function my_initcap(p_string varchar2) return varchar2 is
3 l_string varchar2(1000) := p_string;
4 begin
5 if regexp_like(l_string,'(Mac[A-Z]|Mc[A-Z])') then
6 null;
7 elsif l_string like '''%' then
8 null;
9 else
10 l_string := initcap(l_string);
11 if l_string like '_''S%' then
12 null;
13 else
14 l_string := replace(l_string,'''S','''s');
15 end if;
16 end if;
17
18 return l_string;
19 end;
20 /
Function created.
cool ...
SQL> select my_initcap(vendor)
2 from service_provider;
MY_INITCAP(VENDOR)
--------------------------
Jones
Brown
Smith
McDonald
Johnson's
until ...
"how dare you developer...
don't call PLSQL from SQL !!!!"
SQL> select max(x)
2 from (
3 select /*+ no_merge */ initcap(source) x
4 from large_table
5 );
...
Elapsed: 00:00:00.48
SQL> select max(x)
2 from (
3 select /*+ no_merge */ my_initcap(source) x
4 from large_table
5 );
Elapsed: 00:00:09.37
it can be done in SQL
SQL> select
2 case
3 when regexp_like(vendor,'(Mac[A-Z]|Mc[A-Z])') then vendor
4 when vendor like '''%' then vendor
5 when initcap(vendor) like '_''S%' then vendor
6 else replace(initcap(vendor),'''S','''s')
7 end ugh
8 from service_provider;
UGH
-------------------------------
Jones
Brown
Smith
McDonald
Johnson's
maintenance nightmare
12c ... user defined functions
SQL> WITH
2 function my_initcap(p_string varchar2)
3 return varchar2 is
4 l_string varchar2(1000) := p_string;
5 begin
6 if regexp_like(l_string,'(Mac[A-Z]|Mc[A-Z])') then
7 null;
8 elsif l_string like '''%' then
...
17
18 return l_string;
19 end;
20 select my_initcap(vendor)
21 from service_provider;
MY_INITCAP(VENDOR)
-----------------------------------------
Jones
Brown
Smith
McDonald
O'Brien
Johnson's
multiple routines
SQL> WITH
2 function is_scottish(p_string varchar2) return boolean is
3 begin
4 return regexp_like(p_string,'(Mac[A-Z]|Mc[A-Z])');
5 end;
6 function my_initcap(p_string varchar2) return varchar2 is
7 l_string varchar2(1000) := p_string;
8 begin
9 if is_scottish(l_string) then
10 null;
11 elsif l_string like '''%' then
12 null;
13 else
14 l_string := initcap(l_string);
15 if l_string like '_''S%' then
16 null;
17 else
18 l_string := replace(l_string,'''S','''s');
19 end if;
20 end if;
21
22 return l_string;
23 end;
24 select my_initcap(surname)
25 from names;
26 /
note: 12c client
11.2 and below
SQL> WITH
2 function my_initcap(p_string varchar2) return varchar2 is
3 l_string varchar2(1000) := p_string;
function my_initcap(p_string varchar2) return varchar2 is
*
ERROR at line 2:
ORA-06553: PLS-103: Encountered the symbol "end-of-file" when
expecting one of the following:
. ( * @ % & = - + ; < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec between || multiset member submultiset
SQL> begin
2 if regexp_like(l_string,'(Mac[A-Z]|Mc[A-Z])') then
3 null;
4 elsif l_string like '''%' then
5 null;
6 else
7 l_string := initcap(l_string);
some catch up coming #1
SQL> create or replace
2 procedure NAMES_PROCESSOR is
3 cursor C is
4 WITH
5 function my_initcap(p_string varchar2)
return varchar2 is
6 l_string varchar2(1000) := p_string;
7 begin
...
23 select my_initcap(vendor) from service_provider;
25 begin
26 null;
27 end;
28 /
Warning: Procedure created with compilation errors.
SQL> sho err
Errors for PROCEDURE NAMES_PROCESSOR:
LINE/COL ERROR
-------- ------------------------------------------------
3/7 PL/SQL: SQL Statement ignored
4/18 PL/SQL: ORA-00905: missing keyword
22/7 PLS-00103: Encountered the symbol "SELECT"
SQL> create or replace
2 procedure NAMES_PROCESSOR is
3 rc sys_refcursor;
4 begin
5 open rc for
6 q'{ WITH
7 function my_initcap(p_string varchar2)
return varchar2 is
8 l_string varchar2(1000) := p_string;
9 begin
...
23 return l_string;
24 end;
25 select my_initcap(vendor)
26 from service_provider
27 }';
28 end;
29 /
Procedure created.
some catch up coming #2
WITH_PLSQL
unrelated to previous
DML (or complex SQL)
SQL> insert into CONTRACTS
2 WITH
3 function my_initcap(p_string varchar2)
4 return varchar2 is
5 l_string varchar2(1000) := p_string;
6 begin
...
20 end;
21 select my_initcap(vendor)
22 from service_provider;
23 /
WITH
*
ERROR at line 2:
ORA-32034: unsupported use of WITH clause
SQL> insert /*+ WITH_PLSQL */ into CONTRACTS
2 WITH
3 function my_initcap(p_string varchar2)
4 return varchar2 is
5 l_string varchar2(1000) := p_string;
6 begin
...
20 end;
21 select my_initcap(surname)
22 from names;
23 /
5 rows inserted.
determinism
SQL> with
2 function f return timestamp as
3 begin
4 return systimestamp;
5 end;
6 select f
7 from dual
8 connect by level <= 10;
9 /
F
----------------------------------------
05-JAN-14 08.09.43.969000000 PM
05-JAN-14 08.09.43.970000000 PM
05-JAN-14 08.09.43.970000000 PM
05-JAN-14 08.09.43.971000000 PM
...
DETERMINISTIC keyword
SQL> with
2 function f return timestamp DETERMINISTIC as
3 begin
4 return systimestamp;
5 end;
6 select f
7 from dual
8 connect by level <= 10;
9 /
F
----------------------------------------
05-JAN-14 08.09.52.145000000 PM
05-JAN-14 08.09.52.146000000 PM
05-JAN-14 08.09.52.146000000 PM
05-JAN-14 08.09.52.147000000 PM
...
conventional scalar caching
SQL> with
2 function f return timestamp as
3 begin
4 return systimestamp;
5 end;
6 select ( select f from dual )
7 from dual
8 connect by level <= 10;
9 /
F
----------------------------------------
05-JAN-14 08.11.50.145000000 PM
05-JAN-14 08.11.50.145000000 PM
05-JAN-14 08.11.50.145000000 PM
05-JAN-14 08.11.50.145000000 PM
...
are they really faster ?
SQL> create or replace
2 function F return number is
3 begin
4 return 1;
5 end;
6 /
Function created.
SQL> select sum(f)
2 from
3 ( select level from dual
4 connect by level <= 1000 ),
5 ( select level from dual
6 connect by level <= 1000 )
7 ;
SUM(F)
----------
1000000
Elapsed: 00:00:02.04
SQL> with
2 function f1 return number is
3 begin
4 return 1;
5 end;
6 select sum(f1)
7 from
8 ( select level from dual
9 connect by level <= 1000 ),
10 ( select level from dual
11 connect by level <= 1000 )
12 /
SUM(F1)
----------
1000000
Elapsed: 00:00:00.52
"but what about all my PLSQL !!!"
SQL> create or replace
2 function F return number is
3 pragma udf;
4 begin
5 return 1;
6 end;
SQL> select sum(f)
2 from
3 ( select level from dual
4 connect by level <= 1000 ),
5 ( select level from dual
6 connect by level <= 1000 );
SUM(F)
----------
1000000
Elapsed: 00:00:00.36

More Related Content

What's hot

Dbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersDbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersRiyaj Shamsudeen
 
pstack, truss etc to understand deeper issues in Oracle database
pstack, truss etc to understand deeper issues in Oracle databasepstack, truss etc to understand deeper issues in Oracle database
pstack, truss etc to understand deeper issues in Oracle databaseRiyaj Shamsudeen
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchiesConnor McDonald
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsConnor McDonald
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestConnor McDonald
 
Oracle ERP Personalization for control master items list
Oracle ERP Personalization for control master items listOracle ERP Personalization for control master items list
Oracle ERP Personalization for control master items listAhmed Elshayeb
 
Balance pcc para 3 links adsl com modem em bridge
Balance pcc para 3 links adsl com modem em bridgeBalance pcc para 3 links adsl com modem em bridge
Balance pcc para 3 links adsl com modem em bridgejoadsoNjo
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 
12c Mini Lesson - Improved Error Handling in PLSQL
12c Mini Lesson - Improved Error Handling in PLSQL12c Mini Lesson - Improved Error Handling in PLSQL
12c Mini Lesson - Improved Error Handling in PLSQLConnor McDonald
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionSveta Smirnova
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON? Sveta Smirnova
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingSveta Smirnova
 
Software diseases: memset
Software diseases: memsetSoftware diseases: memset
Software diseases: memsetPVS-Studio
 
oracle cloud with 2 nodes processing
oracle cloud with 2 nodes processingoracle cloud with 2 nodes processing
oracle cloud with 2 nodes processingmahdi ahmadi
 
Connor McDonald Partitioning
Connor McDonald PartitioningConnor McDonald Partitioning
Connor McDonald PartitioningInSync Conference
 
Understanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-DevelopersUnderstanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-DevelopersEnkitec
 

What's hot (20)

Dbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersDbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineers
 
pstack, truss etc to understand deeper issues in Oracle database
pstack, truss etc to understand deeper issues in Oracle databasepstack, truss etc to understand deeper issues in Oracle database
pstack, truss etc to understand deeper issues in Oracle database
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tips
 
Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolest
 
Oracle ERP Personalization for control master items list
Oracle ERP Personalization for control master items listOracle ERP Personalization for control master items list
Oracle ERP Personalization for control master items list
 
Balance pcc para 3 links adsl com modem em bridge
Balance pcc para 3 links adsl com modem em bridgeBalance pcc para 3 links adsl com modem em bridge
Balance pcc para 3 links adsl com modem em bridge
 
11 things about 11gr2
11 things about 11gr211 things about 11gr2
11 things about 11gr2
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
12c Mini Lesson - Improved Error Handling in PLSQL
12c Mini Lesson - Improved Error Handling in PLSQL12c Mini Lesson - Improved Error Handling in PLSQL
12c Mini Lesson - Improved Error Handling in PLSQL
 
New features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in actionNew features in Performance Schema 5.7 in action
New features in Performance Schema 5.7 in action
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON?
 
Oracle
OracleOracle
Oracle
 
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL TroubleshootingPerformance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
 
Analytic SQL Sep 2013
Analytic SQL Sep 2013Analytic SQL Sep 2013
Analytic SQL Sep 2013
 
Software diseases: memset
Software diseases: memsetSoftware diseases: memset
Software diseases: memset
 
oracle cloud with 2 nodes processing
oracle cloud with 2 nodes processingoracle cloud with 2 nodes processing
oracle cloud with 2 nodes processing
 
Connor McDonald Partitioning
Connor McDonald PartitioningConnor McDonald Partitioning
Connor McDonald Partitioning
 
Understanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-DevelopersUnderstanding Optimizer-Statistics-for-Developers
Understanding Optimizer-Statistics-for-Developers
 

Similar to Optimize SQL performance with user-defined functions

OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersConnor McDonald
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلMohamed Moustafa
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application DevelopmentSaurabh K. Gupta
 
12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntaxConnor McDonald
 
OpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersOpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersConnor McDonald
 
12c Mini Lesson - Data Redaction
12c Mini Lesson - Data Redaction12c Mini Lesson - Data Redaction
12c Mini Lesson - Data RedactionConnor McDonald
 
12c for Developers - Feb 2014
12c for Developers - Feb 201412c for Developers - Feb 2014
12c for Developers - Feb 2014Connor McDonald
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2Umair Amjad
 
12c Mini Lesson - Better Defaults
12c Mini Lesson - Better Defaults12c Mini Lesson - Better Defaults
12c Mini Lesson - Better DefaultsConnor McDonald
 
The Hidden Face of Cost-Based Optimizer: PL/SQL Specific Statistics
The Hidden Face of Cost-Based Optimizer: PL/SQL Specific StatisticsThe Hidden Face of Cost-Based Optimizer: PL/SQL Specific Statistics
The Hidden Face of Cost-Based Optimizer: PL/SQL Specific StatisticsMichael Rosenblum
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스PgDay.Seoul
 
Latin America Tour 2019 - 10 great sql features
Latin America Tour 2019  - 10 great sql featuresLatin America Tour 2019  - 10 great sql features
Latin America Tour 2019 - 10 great sql featuresConnor McDonald
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 
Debugging Ruby
Debugging RubyDebugging Ruby
Debugging RubyAman Gupta
 
12c SQL Plan Directives
12c SQL Plan Directives12c SQL Plan Directives
12c SQL Plan DirectivesFranck Pachot
 
Oracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor formatOracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor formatFranck Pachot
 
All on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingAll on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingMohamed Houri
 

Similar to Optimize SQL performance with user-defined functions (20)

OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developers
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
 
Sql2
Sql2Sql2
Sql2
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
 
12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax
 
OpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer DisastersOpenWorld 2018 - Common Application Developer Disasters
OpenWorld 2018 - Common Application Developer Disasters
 
12c Mini Lesson - Data Redaction
12c Mini Lesson - Data Redaction12c Mini Lesson - Data Redaction
12c Mini Lesson - Data Redaction
 
12c for Developers - Feb 2014
12c for Developers - Feb 201412c for Developers - Feb 2014
12c for Developers - Feb 2014
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
Les01
Les01Les01
Les01
 
12c Mini Lesson - Better Defaults
12c Mini Lesson - Better Defaults12c Mini Lesson - Better Defaults
12c Mini Lesson - Better Defaults
 
The Hidden Face of Cost-Based Optimizer: PL/SQL Specific Statistics
The Hidden Face of Cost-Based Optimizer: PL/SQL Specific StatisticsThe Hidden Face of Cost-Based Optimizer: PL/SQL Specific Statistics
The Hidden Face of Cost-Based Optimizer: PL/SQL Specific Statistics
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
 
Latin America Tour 2019 - 10 great sql features
Latin America Tour 2019  - 10 great sql featuresLatin America Tour 2019  - 10 great sql features
Latin America Tour 2019 - 10 great sql features
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 
Debugging Ruby
Debugging RubyDebugging Ruby
Debugging Ruby
 
12c SQL Plan Directives
12c SQL Plan Directives12c SQL Plan Directives
12c SQL Plan Directives
 
Oracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor formatOracle dbms_xplan.display_cursor format
Oracle dbms_xplan.display_cursor format
 
Sqlplus
SqlplusSqlplus
Sqlplus
 
All on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingAll on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor Sharing
 

More from Connor McDonald

Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQLConnor McDonald
 
Sangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on AutonomousSangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on AutonomousConnor McDonald
 
Sangam 2019 - The Latest Features
Sangam 2019 - The Latest FeaturesSangam 2019 - The Latest Features
Sangam 2019 - The Latest FeaturesConnor McDonald
 
UKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresUKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresConnor McDonald
 
APEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomousAPEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomousConnor McDonald
 
APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne Connor McDonald
 
OOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAsOOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAsConnor McDonald
 
OOW19 - Read consistency
OOW19 - Read consistencyOOW19 - Read consistency
OOW19 - Read consistencyConnor McDonald
 
OOW19 - Slower and less secure applications
OOW19 - Slower and less secure applicationsOOW19 - Slower and less secure applications
OOW19 - Slower and less secure applicationsConnor McDonald
 
OOW19 - Killing database sessions
OOW19 - Killing database sessionsOOW19 - Killing database sessions
OOW19 - Killing database sessionsConnor McDonald
 
OOW19 - Ten Amazing SQL features
OOW19 - Ten Amazing SQL featuresOOW19 - Ten Amazing SQL features
OOW19 - Ten Amazing SQL featuresConnor McDonald
 
Latin America Tour 2019 - 18c and 19c featues
Latin America Tour 2019   - 18c and 19c featuesLatin America Tour 2019   - 18c and 19c featues
Latin America Tour 2019 - 18c and 19c featuesConnor McDonald
 
Latin America tour 2019 - Flashback
Latin America tour 2019 -  FlashbackLatin America tour 2019 -  Flashback
Latin America tour 2019 - FlashbackConnor McDonald
 
Latin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingLatin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingConnor McDonald
 
Latin America Tour 2019 - slow data and sql processing
Latin America Tour 2019  - slow data and sql processingLatin America Tour 2019  - slow data and sql processing
Latin America Tour 2019 - slow data and sql processingConnor McDonald
 
OG Yatra - upgrading to the new 12c+ optimizer
OG Yatra - upgrading to the new 12c+ optimizerOG Yatra - upgrading to the new 12c+ optimizer
OG Yatra - upgrading to the new 12c+ optimizerConnor McDonald
 
OG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tipsOG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tipsConnor McDonald
 
OG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developersOG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developersConnor McDonald
 
Kscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsKscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsConnor McDonald
 

More from Connor McDonald (20)

Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
Sangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on AutonomousSangam 19 - Successful Applications on Autonomous
Sangam 19 - Successful Applications on Autonomous
 
Sangam 2019 - The Latest Features
Sangam 2019 - The Latest FeaturesSangam 2019 - The Latest Features
Sangam 2019 - The Latest Features
 
UKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresUKOUG 2019 - SQL features
UKOUG 2019 - SQL features
 
APEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomousAPEX tour 2019 - successful development with autonomous
APEX tour 2019 - successful development with autonomous
 
APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne APAC Groundbreakers 2019 - Perth/Melbourne
APAC Groundbreakers 2019 - Perth/Melbourne
 
OOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAsOOW19 - Flashback, not just for DBAs
OOW19 - Flashback, not just for DBAs
 
OOW19 - Read consistency
OOW19 - Read consistencyOOW19 - Read consistency
OOW19 - Read consistency
 
OOW19 - Slower and less secure applications
OOW19 - Slower and less secure applicationsOOW19 - Slower and less secure applications
OOW19 - Slower and less secure applications
 
OOW19 - Killing database sessions
OOW19 - Killing database sessionsOOW19 - Killing database sessions
OOW19 - Killing database sessions
 
OOW19 - Ten Amazing SQL features
OOW19 - Ten Amazing SQL featuresOOW19 - Ten Amazing SQL features
OOW19 - Ten Amazing SQL features
 
Latin America Tour 2019 - 18c and 19c featues
Latin America Tour 2019   - 18c and 19c featuesLatin America Tour 2019   - 18c and 19c featues
Latin America Tour 2019 - 18c and 19c featues
 
Latin America tour 2019 - Flashback
Latin America tour 2019 -  FlashbackLatin America tour 2019 -  Flashback
Latin America tour 2019 - Flashback
 
Latin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingLatin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matching
 
Latin America Tour 2019 - slow data and sql processing
Latin America Tour 2019  - slow data and sql processingLatin America Tour 2019  - slow data and sql processing
Latin America Tour 2019 - slow data and sql processing
 
ANSI vs Oracle language
ANSI vs Oracle languageANSI vs Oracle language
ANSI vs Oracle language
 
OG Yatra - upgrading to the new 12c+ optimizer
OG Yatra - upgrading to the new 12c+ optimizerOG Yatra - upgrading to the new 12c+ optimizer
OG Yatra - upgrading to the new 12c+ optimizer
 
OG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tipsOG Yatra - 25 years of hints and tips
OG Yatra - 25 years of hints and tips
 
OG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developersOG Yatra - Flashback, not just for developers
OG Yatra - Flashback, not just for developers
 
Kscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsKscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAs
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

Optimize SQL performance with user-defined functions

  • 2. 2 the focus ... the day to day stuff
  • 8. SQL> select vendor 2 from service_provider; VENDOR ------------------------------ jones brown SMITH sigh...
  • 9. "no problem.... I'll use initcap"
  • 10. SQL> select initcap(vendor) 2 from service_provider; INITCAP(VENDOR) ------------------------------ Jones Brown Smith
  • 12. SQL> select initcap(vendor) 2 from service_provider; INITCAP(VENDOR) ------------------------------ Jones Brown Smith Mcdonald Johnson'S uh oh
  • 14. SQL> create or replace 2 function my_initcap(p_string varchar2) return varchar2 is 3 l_string varchar2(1000) := p_string; 4 begin 5 if regexp_like(l_string,'(Mac[A-Z]|Mc[A-Z])') then 6 null; 7 elsif l_string like '''%' then 8 null; 9 else 10 l_string := initcap(l_string); 11 if l_string like '_''S%' then 12 null; 13 else 14 l_string := replace(l_string,'''S','''s'); 15 end if; 16 end if; 17 18 return l_string; 19 end; 20 / Function created.
  • 16. SQL> select my_initcap(vendor) 2 from service_provider; MY_INITCAP(VENDOR) -------------------------- Jones Brown Smith McDonald Johnson's
  • 18.
  • 19. "how dare you developer... don't call PLSQL from SQL !!!!"
  • 20. SQL> select max(x) 2 from ( 3 select /*+ no_merge */ initcap(source) x 4 from large_table 5 ); ... Elapsed: 00:00:00.48 SQL> select max(x) 2 from ( 3 select /*+ no_merge */ my_initcap(source) x 4 from large_table 5 ); Elapsed: 00:00:09.37
  • 21. it can be done in SQL
  • 22. SQL> select 2 case 3 when regexp_like(vendor,'(Mac[A-Z]|Mc[A-Z])') then vendor 4 when vendor like '''%' then vendor 5 when initcap(vendor) like '_''S%' then vendor 6 else replace(initcap(vendor),'''S','''s') 7 end ugh 8 from service_provider; UGH ------------------------------- Jones Brown Smith McDonald Johnson's
  • 24. 12c ... user defined functions
  • 25. SQL> WITH 2 function my_initcap(p_string varchar2) 3 return varchar2 is 4 l_string varchar2(1000) := p_string; 5 begin 6 if regexp_like(l_string,'(Mac[A-Z]|Mc[A-Z])') then 7 null; 8 elsif l_string like '''%' then ... 17 18 return l_string; 19 end; 20 select my_initcap(vendor) 21 from service_provider; MY_INITCAP(VENDOR) ----------------------------------------- Jones Brown Smith McDonald O'Brien Johnson's
  • 27. SQL> WITH 2 function is_scottish(p_string varchar2) return boolean is 3 begin 4 return regexp_like(p_string,'(Mac[A-Z]|Mc[A-Z])'); 5 end; 6 function my_initcap(p_string varchar2) return varchar2 is 7 l_string varchar2(1000) := p_string; 8 begin 9 if is_scottish(l_string) then 10 null; 11 elsif l_string like '''%' then 12 null; 13 else 14 l_string := initcap(l_string); 15 if l_string like '_''S%' then 16 null; 17 else 18 l_string := replace(l_string,'''S','''s'); 19 end if; 20 end if; 21 22 return l_string; 23 end; 24 select my_initcap(surname) 25 from names; 26 /
  • 30. SQL> WITH 2 function my_initcap(p_string varchar2) return varchar2 is 3 l_string varchar2(1000) := p_string; function my_initcap(p_string varchar2) return varchar2 is * ERROR at line 2: ORA-06553: PLS-103: Encountered the symbol "end-of-file" when expecting one of the following: . ( * @ % & = - + ; < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset SQL> begin 2 if regexp_like(l_string,'(Mac[A-Z]|Mc[A-Z])') then 3 null; 4 elsif l_string like '''%' then 5 null; 6 else 7 l_string := initcap(l_string);
  • 31. some catch up coming #1
  • 32. SQL> create or replace 2 procedure NAMES_PROCESSOR is 3 cursor C is 4 WITH 5 function my_initcap(p_string varchar2) return varchar2 is 6 l_string varchar2(1000) := p_string; 7 begin ... 23 select my_initcap(vendor) from service_provider; 25 begin 26 null; 27 end; 28 / Warning: Procedure created with compilation errors. SQL> sho err Errors for PROCEDURE NAMES_PROCESSOR: LINE/COL ERROR -------- ------------------------------------------------ 3/7 PL/SQL: SQL Statement ignored 4/18 PL/SQL: ORA-00905: missing keyword 22/7 PLS-00103: Encountered the symbol "SELECT"
  • 33. SQL> create or replace 2 procedure NAMES_PROCESSOR is 3 rc sys_refcursor; 4 begin 5 open rc for 6 q'{ WITH 7 function my_initcap(p_string varchar2) return varchar2 is 8 l_string varchar2(1000) := p_string; 9 begin ... 23 return l_string; 24 end; 25 select my_initcap(vendor) 26 from service_provider 27 }'; 28 end; 29 / Procedure created.
  • 34. some catch up coming #2
  • 37. SQL> insert into CONTRACTS 2 WITH 3 function my_initcap(p_string varchar2) 4 return varchar2 is 5 l_string varchar2(1000) := p_string; 6 begin ... 20 end; 21 select my_initcap(vendor) 22 from service_provider; 23 / WITH * ERROR at line 2: ORA-32034: unsupported use of WITH clause
  • 38. SQL> insert /*+ WITH_PLSQL */ into CONTRACTS 2 WITH 3 function my_initcap(p_string varchar2) 4 return varchar2 is 5 l_string varchar2(1000) := p_string; 6 begin ... 20 end; 21 select my_initcap(surname) 22 from names; 23 / 5 rows inserted.
  • 40. SQL> with 2 function f return timestamp as 3 begin 4 return systimestamp; 5 end; 6 select f 7 from dual 8 connect by level <= 10; 9 / F ---------------------------------------- 05-JAN-14 08.09.43.969000000 PM 05-JAN-14 08.09.43.970000000 PM 05-JAN-14 08.09.43.970000000 PM 05-JAN-14 08.09.43.971000000 PM ...
  • 42. SQL> with 2 function f return timestamp DETERMINISTIC as 3 begin 4 return systimestamp; 5 end; 6 select f 7 from dual 8 connect by level <= 10; 9 / F ---------------------------------------- 05-JAN-14 08.09.52.145000000 PM 05-JAN-14 08.09.52.146000000 PM 05-JAN-14 08.09.52.146000000 PM 05-JAN-14 08.09.52.147000000 PM ...
  • 44. SQL> with 2 function f return timestamp as 3 begin 4 return systimestamp; 5 end; 6 select ( select f from dual ) 7 from dual 8 connect by level <= 10; 9 / F ---------------------------------------- 05-JAN-14 08.11.50.145000000 PM 05-JAN-14 08.11.50.145000000 PM 05-JAN-14 08.11.50.145000000 PM 05-JAN-14 08.11.50.145000000 PM ...
  • 45. are they really faster ?
  • 46. SQL> create or replace 2 function F return number is 3 begin 4 return 1; 5 end; 6 / Function created.
  • 47. SQL> select sum(f) 2 from 3 ( select level from dual 4 connect by level <= 1000 ), 5 ( select level from dual 6 connect by level <= 1000 ) 7 ; SUM(F) ---------- 1000000 Elapsed: 00:00:02.04
  • 48. SQL> with 2 function f1 return number is 3 begin 4 return 1; 5 end; 6 select sum(f1) 7 from 8 ( select level from dual 9 connect by level <= 1000 ), 10 ( select level from dual 11 connect by level <= 1000 ) 12 / SUM(F1) ---------- 1000000 Elapsed: 00:00:00.52
  • 49. "but what about all my PLSQL !!!"
  • 50. SQL> create or replace 2 function F return number is 3 pragma udf; 4 begin 5 return 1; 6 end; SQL> select sum(f) 2 from 3 ( select level from dual 4 connect by level <= 1000 ), 5 ( select level from dual 6 connect by level <= 1000 ); SUM(F) ---------- 1000000 Elapsed: 00:00:00.36