SlideShare una empresa de Scribd logo
1 de 60
1 of 60
#589: Expanding the SQL Horizons:
PL/SQL User-Defined Functions
in the Real World
Michael Rosenblum
Dulcian, Inc.
www.dulcian.com
2 of 60
Who Am I? – “Misha”
 Oracle ACE
 Co-author of 3 books
 PL/SQL for Dummies
 Expert PL/SQL Practices
 Oracle PL/SQL Performance Tuning Tips & Techniques
(Rosenblum & Dorsey, Oracle Press, July 2014)
 Won ODTUG 2009 Speaker of the Year
 Known for:
 SQL and PL/SQL tuning
 Complex functionality
 Code generators
 Repository-based development
3 of 60
© Tom Kyte
If you have a thing to do in the database, do it in
SQL.
If you cannot do it in SQL, do it in PL/SQL.
If you cannot do it in either SQL or PL/SQL, do
it in Java.
If you cannot do it even inJava, do it in C.
If you cannot do it in C, are you sure that it
needs to be done?
4 of 60
So…
The rule:
 Working in the same environment is better than
jumping between multiple ones.
Reasoning:
 You pay a price for unnecessary context switches,
especially in terms of CPU cost.
5 of 60
But…
The reality is more complicated, because it has
lots of dimensions:
 Performance
 Maintainability
 Available expertise, etc.
If context switches are necessary, it is OK to use
multiple environments.
6 of 60
Switching to PL/SQL
7 of 60
Why PL/SQL?
Flexibility:
 PL/SQL can utilize Dynamic SQL to build SQL
statements on the fly that precisely match the
situation
 … while hardcoded SQL query has to support all possible
permutations.
Functionality:
 PL/SQL can do whatever you can code
 … while SQL features are provided to you by Oracle
8 of 60
Generic Search Problem
 Use Case:
 Complex generic search
 Common approach:
 Build a single complex SQL statement that takes all
possible filters.
 Common problem #1:
 Some condition permutations cause major performance
issues.
 Common problem #2:
 Optimizing condition A worsens condition B…
 Common problem #3:
 Optimizing condition B worsens…
9 of 60
Generic Search Solution
Proposal:
 Build SQL statements on the fly.
 Only some filters (and some tables) can be used at
any point in time.
 Always use bind variables.
 Use object collection to bring back the result:
CREATE TYPE emp_search_ot AS OBJECT
(empno_nr NUMBER,
empno_dsp VARCHAR2(256),
comp_nr NUMBER);
CREATE TYPE emp_search_nt IS TABLE OF emp_search_ot;
10 of 60
Generic Search (1)
create function f_search_tt
(i_empno number:=null,
i_ename_tx varchar2:=null,
i_loc_tx varchar2:=null,
i_limit_nr number:=50)
return emp_search_nt
is
v_out_tt emp_search_nt:=emp_search_nt();
v_from_tx varchar2(32767):='emp';
v_where_tx varchar2(32767):=
'rownum<=v_limit_nr';
v_plsql_tx varchar2(32767);
begin
...
11 of 60
Generic Search (2)
if i_empno is not null then
v_where_tx:=v_where_tx||chr(10)||
'and emp.empno=v_empno_nr';
end if;
if i_ename_tx is not null then
v_where_tx:=v_where_tx||chr(10)||
'and emp.ename like ''%''||v_ename_tx||''%''';
end if;
if i_loc_tx is not null then
v_from_tx:=v_from_tx||chr(10)||
'join dept on (emp.deptno=dept.deptno)';
v_where_tx:=v_where_tx||chr(10)||
'and dept.loc=v_loc_tx';
end if;
...
12 of 60
Generic Search (3)
v_plsql_tx:=
'declare '||chr(10)||
'v_limit_nr number:=:1;'||chr(10)||
'v_empno_nr number:=:2;'||chr(10)||
'v_ename_tx varchar2(256):=:3;'||chr(10)||
'v_loc_tx varchar2(256):=:4;'||chr(10)||
'begin '||chr(10)||
'select emp_search_ot('||
'emp.empno,emp.ename||''(''||emp.job||'')'','||
'emp.sal+nvl(emp.comm,0))'||chr(10)||
'bulk collect into :5'||chr(10)||
'from '||v_from_tx||chr(10)||
'where '||v_where_tx||';'||chr(10)||
'end;';
execute immediate v_plsql_tx using
i_limit_nr, i_empno, i_ename_tx, i_loc_tx,
out v_out_tt;
return v_out_tt;
end;
All possible binds
are variables
Type constructor
Bulk output
13 of 60
Generic Search(3)
Key points:
 Table DEPT is joined only when needed.
 ANSI SQL comes in handy when you need to split
conditions and joins!
 Building anonymous blocks instead of PL/SQL
simplifies passing bind variables.
 Otherwise you need to cover all possible permutations.
 EMP_SEARCH_OT constructor must be inside
 Because the output is an object collection, not a record
14 of 60
Create your own SQL
functionality
 Use Case:
 Built-in function LISTAGG is limited to 4000 characters
 … but it would be nice if it could return a CLOB!
 Solution:
 Build your own aggregate function using Oracle Extensibility
Framework
 … if you can survive ODCI syntax 
 Although…
 Oracle normally does not allow aggregate functions to have
multiple parameters (LISTAGG is the exception).
 Your own aggregate parameter object:
CREATE TYPE listAggParam_ot IS OBJECT
(value_tx VARCHAR2(4000),
separator_tx VARCHAR2(10))
15 of 60
ODCI Object Type (1)
create or replace type ListAggCLImpl as object (
v_out_cl clob,
v_defaultseparator_tx varchar2(10),
static function ODCIAggregateInitialize
(sctx in out listaggclimpl) return number,
member function ODCIAggregateIterate
(self in out listaggclimpl,
value_ot in listaggparam_ot) return number,
member function ODCIAggregateTerminate
(self in listaggclimpl,
returnvalue out clob,
flags in number) return number,
member function ODCIAggregateMerge
(self in out listaggclimpl,
ctx2 in listaggclimpl) return number
)
OUT-Type
IN-Type
16 of 60
ODCI Object Type (2)
 Type elements (mandatory):
 Type attributes
 Intermediate data storage
 method ODCIAggregateInitialize
 Called once per group to initialize all required settings
 method ODCIAggregateIterate
 Called once for every processed value
 The second parameter’s datatype - input to be aggregated.
 method ODCIAggregateTerminate
 Called once at the end of each group
 The second parameter’s datatype - output of aggregate
function.
 method ODCIAggregateMerge
 Called if your aggregate function is running in parallel
 Used to put together the results of different threads.
17 of 60
ODCI Object Type Body (1)
create or replace type body listaggclimpl is
static function ODCIAggregateInitialize
(sctx in out listaggclimpl)
return number is
begin
sctx :=listaggclimpl(null,',');
return odciconst.success;
end;
member function ODCIAggregateTerminate
(self in listaggclimpl,
returnvalue out clob,
flags in number)
return number is
begin
returnvalue := self.v_out_cl;
return odciconst.success;
end;
Default constructor
Return final value
18 of 60
ODCI Object Type Body (2)
member function ODCIAggregateIterate
(self in out listaggclimpl, value_ot in listaggparam_ot)
return number is
begin
if self.v_out_cl is null then
self.v_defaultseparator_tx:=value_ot.separator_tx;
dbms_lob.createtemporary
(self.v_out_cl,true,dbms_lob.call);
dbms_lob.writeappend (self.v_out_cl,
length(value_ot.value_tx),value_ot.value_tx);
else
dbms_lob.writeappend(self.v_out_cl,
length(value_ot.separator_tx||value_ot.value_tx),
value_ot.separator_tx||value_ot.value_tx);
end if;
return odciconst.success;
end;
Add separators
for consecutive writes
Initial write
19 of 60
ODCI Object Type Body (3)
member function ODCIAggregateMerge
(self in out listaggclimpl, ctx2 in listaggclimpl)
return number is
begin
if ctx2.v_out_cl is not null then
if self.v_out_cl is null then
self.v_out_cl:=ctx2.v_out_cl;
else
dbms_lob.writeappend(self.v_out_cl,
length(self.v_defaultseparator_tx),
self.v_defaultseparator_tx);
dbms_lob.append(self.v_out_cl,ctx2.v_out_cl);
end if;
end if;
return odciconst.success;
end;
end;
Merge parallel processes
in pairs
20 of 60
ODCI Object Type
Explanation
 Key points:
 Method ODCIAggregateInitialize
 Has a default constructor to specify the initial values of two attributes of
ListAggCLImpl type
 Method ODCIAggregateIterate
 Adds new values to the existing temporary storage V_OUT_CL via
DBMS_LOB APIs
 Method ODCIAggregateTerminate
 Returns the final value of V_OUT_CL as a formal result
 Method ODCIAggregateMerge
 Used in case there are parallel executions merging two different
V_OUT_CL values into a single output .
 Putting everything together:
CREATE FUNCTION ListAggCL (value_ot listAggParam_ot)
RETURN CLOB
PARALLEL_ENABLE
AGGREGATE USING ListAggCLImpl;
21 of 60
ListAggCL Usage (1)
SQL> SELECT deptno,
2 ListAggCL(listAggParam_ot(ename,',')) list_cl
3 FROM emp
4 GROUP BY deptno;
DEPTNO LIST_cl
---------- ------------------------------
10 CLARK,MILLER,KING
20 SMITH,FORD,ADAMS,SCOTT,JONES
30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD
22 of 60
ListAggCL Usage (2)
SQL> SELECT empno, ename,
2 ListAggCL(listAggParam_ot(ename,','))
3 over(partition by deptno
4 order by ename
5 rows between unbounded preceding
6 and unbounded following
7 ) list_cl
8 FROM emp
9 WHERE job = 'CLERK';
EMPNO ENAME LIST_CL
---------- ---------- -----------------------------------
7934 MILLER MILLER
7876 ADAMS ADAMS,SMITH
7369 SMITH ADAMS,SMITH
7900 JAMES JAMES
Also works as analytical function!
Important
By default, if you have ORDER BY
it is “… AND CURRENT ROW”
23 of 60
Overview
Switching from SQL to PL/SQL
 Can:
 Make your code more flexible and understandable
 Improve performance in cases when the CBO is confused
 Expand SQL horizons
 Cannot:
 Do it for free
 There will always be a cost of context switching between
language environments.
24 of 60
Calling User-Defined Functions
within SQL
25 of 60
THE problem
Question:
 Do you know how many times your function is
being executed within a SQL statement?
Issue:
 Few developers know how to properly ask this
question
 … and even fewer know how to correctly interpret
the answer!
26 of 60
Reminder!
SQL statement execution order:
 1. JOIN
 2. WHERE
 3. GROUP BY
 4. SELECT (including analytics functions)
 5. HAVING
 6. ORDER BY
27 of 60
Testing Framework
create package counter_pkg is
v_nr number:=0;
procedure p_check;
end;
create package body counter_pkg is
procedure p_check is
begin
dbms_output.put_line ('fired:'||counter_pkg.v_nr);
counter_pkg.v_nr:=0;
end;
end;
create function f_change_nr (i_nr number) return
number is
begin
counter_pkg.v_nr:=counter_pkg.v_nr+1;
return return i_nr*(-1);;
end;
28 of 60
SELECT and WHERE
 Output:
SQL> SELECT empno, ename, f_change_nr(empno) change_nr
2 FROM emp
3 WHERE f_change_nr(empno) IS NOT NULL
4 AND deptno = 20;
...
5 rows selected.
SQL> exec counter_pkg.p_check;
Fired:10
 Explanation:
 CBO orders predicates to decrease the total cost
 DEPNO=20 is applied first to get 5 rows back
 CBO needs correct info (statistics, indexes, constraints etc.) to
make that decision
 The same functions in SELECT and WHERE clauses
are being fired independently.
Twice the number
of returned rows
29 of 60
SELECT and ORDER BY (1)
 Output:
SQL> SELECT empno, ename, f_change_nr(empno) change_nr
2 FROM emp WHERE deptno = 20
3 ORDER BY 3;
5 rows selected.
SQL> exec counter_pkg.p_check;
Fired:5
SQL> SELECT empno, change_nr
2 FROM (SELECT empno, ename, f_change_nr(empno) change
3 FROM emp WHERE deptno = 20
4 ORDER BY 3);
5 rows selected.
SQL> exec counter_pkg.p_check;
Fired:10
 Problem:
 Why does the inline view cause a double fire?
In-line view causes
double-firing
30 of 60
SELECT and ORDER BY (2)
 Research of 10053 trace:
Order-by elimination (OBYE)
***************************
OBYE: OBYE performed.
OBYE: OBYE bypassed: no order by to eliminate.
Final query after transformations:*** UNPARSED QUERY IS ***
SELECT "EMP"."EMPNO" "EMPNO","EMP"."ENAME" "ENAME",
"SCOTT"."F_CHANGE_NR"("EMP"."EMPNO") "CHANGE_NR"
FROM "SCOTT"."EMP" "EMP"
WHERE "EMP"."DEPTNO"=20
ORDER BY "SCOTT"."F_CHANGE_NR"("EMP"."EMPNO")
 Explanation:
 Order-by elimination is fired before the query transformation.
 It does not find an ORDER BY clause in the root SELECT
and stops.
 After query transformation, ORDER BY suddenly appears.
 It is not removed now.
31 of 60
SELECT and ORDER BY (3)
 /*+ NO_MERGE */ hint solves the problem:
SQL> select empno, change_nr
2 from (select /*+ no_merge */
3 empno, ename, f_change_nr(empno) change_nr
4 from emp where deptno = 20 order by 3);
5 rows selected.
SQL> exec counter_pkg.p_check;
fired:5
 Why bother? Because of views:
SQL> create or replace view v_emp as
2 select empno, ename, f_change_nr(empno) change_nr
3 from emp where deptno = 20 order by 3;
view created.
SQL> select empno, change_nr from v_emp;
5 rows selected.
SQL> exec counter_pkg.p_check;
fired:10
Query rewrite is skipped. 
Order-by elimination
is applied directly.
Same effect
as in-line view
32 of 60
Multi-Table Example
 Output:
SQL> SELECT empno, f_change_nr(empno) change_nr, dname
2 FROM emp,
3 dept
4 WHERE emp.deptno(+) = dept.deptno;
EMPNO CHANGE_NR DNAME
7782 7782 ACCOUNTING
...
7654 7654 SALES
OPERATIONS
15 rows selected.
SQL> exec counter_pkg.p_check;
Fired:15
 Explanation
 JOIN is being applied first. It results in 15 rows.
 Function in SELECT is fired as many times as the number of rows.
No employees!
33 of 60
Section Summary
Calling user-defined functions within SQL:
 Depends on the SQL internal execution order
 Remember: Joins always happen first!
 Is impacted by Cost-Based Optimizer decisions:
 Query transformation
 Predicate transformations
 ORDER-BY elimination, etc.
34 of 60
New Cost-Management Features
(Oracle 12c)
35 of 60
PRAGMA UDF (1)
Meaning:
 PL/SQL function is compiled in the way that is
optimized for SQL usage (different memory
management).
Example:
CREATE FUNCTION f_change_udf_nr (i_nr NUMBER)
RETURN NUMBER IS
PRAGMA UDF;
BEGIN
counter_pkg.v_nr:=counter_pkg.v_nr+1;
RETURN i_nr+1;
END;
36 of 60
PRAGMA UDF (2)
Much faster in SQL:
SQL> SELECT MAX(f_change_nr(object_id))
2 FROM TEST_TAB;
MAX(F_CHANGE_NR(OBJECT_ID))
---------------------------
51485
Elapsed: 00:00:00.48
SQL> SELECT MAX(f_change_udf_nr(object_id))
2 FROM TEST_TAB;
MAX(F_CHANGE_UDF_NR(OBJECT_ID))
-------------------------------
51485
Elapsed: 00:00:00.06
37 of 60
PRAGMA UDF (3)
 Somewhat slower in PL/SQL (1 million iterations):
SQL> DECLARE
2 v_out_nr NUMBER;
3 BEGIN
4 FOR i IN 1..1000000 loop
5 v_out_nr:=f_change_nr(i)+f_change_nr(i+1);
6 END LOOP;
7 END;
8 /
Elapsed: 00:00:01.39
SQL> DECLARE
2 v_out_nr NUMBER;
3 BEGIN
4 FOR i IN 1..1000000 LOOP
5 v_out_nr:=f_change_udf_nr(i)+f_change_udf_nr(i+1);
6 END LOOP;
7 END;
8 /
Elapsed: 00:00:01.89
38 of 60
PRAGMA UDF Overview
Looks very promising:
 Significant performance gains when used in SQL.
 Minimal performance loss when used in PL/SQL.
Although…
 It is a new feature and has not been extensively
tested.
39 of 60
Functions in WITH clause (1)
 Meaning:
 Functions with the visibility scope of just one SQL query
 Example (the same 50000 row table):
SQL> WITH FUNCTION f_changeWith_nr (i_nr number)
2 RETURN NUMBER IS
3 BEGIN
4 RETURN i_nr+1;
5 END;
6 SELECT max(f_changeWith_nr(object_id))
7 FROM test_tab
8 /
MAX(F_CHANGEWITH_NR(OBJECT_ID))
-------------------------------
51485
Elapsed: 00:00:00.07
40 of 60
Functions in WITH clause
Overview
 Advantages:
 Significantly decreases SQL-to-PL/SQL context switches
 Drawbacks:
 Code fragmentation and duplication
 PL/SQL limitations – It is not even supported directly (only via
Dynamic SQL).
 SQL limitations - If you want to include it anywhere other than a
top-level query, you need a hint /*+ WITH_PLSQL*/
 Optimization limitations: DETERMINISTIC hint is being ignored.
 Remark:
 PRAGMA UDF constantly outperforms WITH-clause functions in
a number of research papers.
 Even here it is 0.06 vs 0.07 sec.
 Summary:
 Usability of this feature is questionable for now.
41 of 60
Query-Level Caching
42 of 60
Reasoning
Question:
 Why do you need to do something that can be done
once and preserved for future use multiple times?
Answer:
 You don’t!
43 of 60
Same OUT for the same IN
 DETERMINISTIC clause:
 Definition: If function does the same thing for exactly the same IN, it
can be defined as DETERMINISTIC.
 Impact: Oracle may reuse already calculated values.
 Warning: Oracle does not check to see whether the function is
deterministic or not.
 Example:
CREATE FUNCTION f_change_det_tx (i_tx VARCHAR2) RETURN VARCHAR2
DETERMINISTIC IS
BEGIN
counter_pkg.v_nr:=counter_pkg.v_nr+1;
RETURN lower(i_tx);
END;
CREATE FUNCTION f_change_tx (i_tx VARCHAR2) RETURN VARCHAR2 IS
BEGIN
counter_pkg.v_nr:=counter_pkg.v_nr+1;
return lower(i_tx);
END;
Regular counterpart
to see the impact
44 of 60
DETERMINISTIC (1)
 Basic Output:
SQL> select empno, f_change_tx(job) from emp;
SQL> exec counter_pkg.p_check;
Fired:14
SQL> select empno, f_change_det_tx(job) from emp;
SQL> exec counter_pkg.p_check;
Fired:5
 Problem:
 DETERMINISTIC is a hint, not a directive and can be ignored.
 Alternate test - increase the scope (50000 rows table):
alter table test_tab add (obj3_tx varchar2(3),
obj1_tx varchar2(1));
update test_tab set
obj3_tx = upper(substr(object_name,-3)), -- 3442 distinct
obj1_tx = upper(substr(object_name,1,1)); -- 26 distinct
create type stringlist_tt is table of varchar2(256);
45 of 60
DETERMINISTIC (2)
SQL> declare
2 v_obj_tt stringlist_tt;
3 v_count_nr number;
4 cursor c_rec is
5 select f_change_det_tx(obj1_tx) obj_tx from test_tab;
6 begin
7 open c_rec;
8 for i in 1..5 loop
9 fetch c_rec bulk collect into v_obj_tt limit 200;
10 select count(distinct column_value) into v_count_nr
11 from table(cast (v_obj_tt as stringlist_tt));
12 counter_pkg.p_check;
13 dbms_output.put_line('-real count:'||v_count_nr);
14 end loop;
15 close c_rec;
16 end;
Fired:41 - real count:18
Fired:42 - real count:19
Fired:25 - real count:17
Fired:22 - real count:11
Fired:26 - real count:10
46 of 60
DETERMINISTIC(3)
Problem:
 Number of function calls is lower than 200, but not
as low and the number of distinct values.
Explanation:
 The scope of the DETERMINISTIC clause is one
fetch operation (not the whole SQL statement!)
 Internally, Oracle has a hash table that preserves
IN/OUT pairs.
 By default, this hash table has limited size (65536
bytes) and fills up very quickly.
 … And you can adjust this size 
47 of 60
DETERMINISTIC (4)
 Example (increase hash table size 4 times):
SQL> ALTER SESSION
2 SET "_query_execution_cache_max_size"=262144;
SQL> ... rerun the last example …
Fired:31 - real count:18
Fired:32 - real count:19
Fired:22 - real count:17
Fired:22 - real count:11
Fired:10 - real count:10
 Remarks:
 It is underscore-parameter, so no Oracle support on this one!
 Hash collisions between multiple values of IN-parameters can occur
 That’s why you rarely get the number of calls matching the number of
distinct values.
Originally was:
42
42
25
22
26
48 of 60
DETERMINISTIC Overview
Good:
 Can improve performance
 Very light in terms of system impact and management
Bad:
 It is a HINT – so you cannot rely on it 100%
 Adding it inappropriately can cause data problems.
Ugly:
 You can adjust internal hash table size, but only at
your own risk.
 Data order may impact the level of optimization.
49 of 60
Side Effect of SELECT from
DUAL
 Definitions:
 Scalar sub-query returns a single column of a single row (or from
the empty rowset)
 Scalar sub-query caching is an Oracle internal mechanism to
preserve results of such queries while processing more complex
ones.
 Implemented as in-memory hash table
 Cache size is defined by “_query_execution_cache_max_size”
 Cache is preserved for the duration of the query.
 Last value is preserved even if hash table is full.
 Example:
SQL> SELECT empno, f_change_tx(job) FROM emp;
SQL> exec counter_pkg.p_check;
Fired:14
SQL> SELECT empno, (SELECT f_change_tx(job) FROM dual)
2 FROM emp;
SQL> exec counter_pkg.p_check;
Fired:5
Only 5 distinct jobs
50 of 60
Cache Durationsql> declare
2 v_obj_tt stringlist_tt;
3 v_count_nr number;
4 cursor c_rec is select
5 (select f_change_tx(obj1_tx) from dual) obj_tx from test_tab;
6 begin
7 open c_rec;
8 for i in 1..5 loop
9 fetch c_rec bulk collect into v_obj_tt limit 100;
10 select count(distinct column_value)
11 into v_count_nr from table(cast (v_obj_tt as stringlist_tt));
12 counter_pkg.p_check;
13 dbms_output.put_line('-real count:'||v_count_nr);
14 end loop;
15 close c_rec;
16 end;
Fired:17 - real count:14
Fired:16 - real count:14
Fired:10 - real count:16
Fired:18 - real count:14
Fired:5 - real count:15
Count is less than number of distinct values:
Cache duration is query.
51 of 60
Scalar Sub-Query Overview
Good:
 Can improve performance
 No changes to PL/SQL code!
 More reliable than DETERMINISTIC hint
Bad:
 Can be impacted by query transformation
 Confusing syntax
Ugly:
 You can adjust internal hash table size, but only at
your own risk.
 Data order may impact the level of optimization.
52 of 60
PL/SQL Result Cache
53 of 60
The Idea
 PL/SQL Function Result Cache
 Database-level cache (cross-session)
 Stored in SGA
 Automatic cache monitoring and invalidation (starting
with Oracle 11g R2)
 Example:
create function f_getdept_dsp (i_deptno number)
return varchar2 result_cache is
v_out_tx varchar2(256);
begin
if i_deptno is null then return null; end if;
select initcap(dname) into v_out_tx
from dept where deptno=i_deptno;
counter_pkg.v_nr:=counter_pkg.v_nr+1;
return v_out_tx;
end;
54 of 60
Result Cache Basics
SQL> SELECT empno, f_getDept_dsp(deptno) dept_dsp
2 FROM emp;
EMPNO DEPT_DSP
----------- ------------------------
7369 Research
...
14 rows selected.
SQL> exec counter_pkg.p_check;
Fired:3
SQL> SELECT empno, f_getDept_dsp(deptno) dept_dsp
2 FROM emp;
EMPNO DEPT_DSP
---------- ----------
7369 Research
...
14 rows selected.
SQL> exec counter_pkg.p_check;
Fired:0
No calls at all!
55 of 60
Result Cache Stats
SQL> SELECT * FROM v$result_cache_statistics;
ID NAME VALUE
----- ------------------------------ ----------
1 Block Size (Bytes) 1024
2 Block Count Maximum 15360
3 Block Count Current 32
4 Result Size Maximum (Blocks) 768
5 Create Count Success 3
6 Create Count Failure 0
7 Find Count 25
8 Invalidation Count 0
9 Delete Count Invalid 0
10 Delete Count Valid 0
11 Hash Chain Length 1
12 Find Copy Count 25
3 distinct INs
56 of 60
Result Cache Dependencies
SQL> SELECT rco.id, rco.name, ao.object_name object_name
2 FROM v$result_cache_objects rco,
3 v$result_cache_dependency rcd,
4 all_objects ao
5 WHERE rco.id = rcd.result_id
6 AND rcd.object_no = ao.object_id
7 order by 1;
ID NAME OBJECT_NAME
-- ------------------------------------------- -------------
1 "SCOTT"."F_GETDEPT_DSP"::8."F_ GETDEPT_DSP" DEPT
1 "SCOTT"."F_GETDEPT_DSP"::8.”F_ GETDEPT_DSP" F_GETDEPT_DSP
3 "SCOTT"."F_GETDEPT_DSP"::8."F_ GETDEPT_DSP" DEPT
3 "SCOTT"."F_GETDEPT_DSP"::8.”F_ GETDEPT_DSP" F_GETDEPT_DSP
4 "SCOTT"."F_GETDEPT_DSP"::8."F_ GETDEPT_DSP" DEPT
4 "SCOTT"."F_GETDEPT_DSP"::8.”F_ GETDEPT_DSP" F_GETDEPT_DSP
57 of 60
Result Cache Cost
 It is not free:
SQL> exec runstats_pkg.rs_start
SQL> SELECT MAX(f_change_cache_tx(obj1_tx)) FROM test_tab;
SQL> exec counter_pkg.p_check;
Fired:26
SQL> exec runstats_pkg.rs_middle
SQL> SELECT MAX(f_change_det_tx(obj1_tx)) FROM test_tab;
SQL> exec counter_pkg.p_check;
Fired:7627
SQL> exec runstats_pkg.rs_stop
Run1 ran in 65 cpu hsecs
Run2 ran in 16 cpu hsecs
Name Run1 Run2 Diff
STAT...CPU used by this session 64 16 -48
LATCH.Result Cache: RC Latch 50,079 0 -50,079
Run1 latches total versus runs -- difference and pct
Run1 Run2 Diff Pct
52,388 2,057 -50,331 2,546.82%
DETERMINISTIC is faster
because of latching.
58 of 60
PL/SQL Result Cache
Overview
Good:
 Database-level caching
 Automatic dependency tracking
 Extended statistics
 Very informative data dictionary views
Bad:
 Significant memory utilization
Ugly:
 Noticeable CPU impact when retrieving values from
cache (because of latching)
 Too easy to add without real understanding
59 of 60
Summary
PL/SQL can significantly extend SQL
functionality.
You must worry about the total number of user-
defined function calls.
PRAGMA UDF can decrease the cost of
function calls within SQL.
Different caching techniques can improve the
overall system performance.
60 of 60
Contact Information
 Michael Rosenblum – mrosenblum@dulcian.com
 Blog – wonderingmisha.blogspot.com
 Website – www.dulcian.com
Coming in July 2014
Oracle PL/SQL Performance
Tuning Tips & Techniques

Más contenido relacionado

La actualidad más candente

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
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321maclean liu
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowKaren Morton
 
你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能maclean liu
 
Database administration commands
Database administration commands Database administration commands
Database administration commands Varsha Ajith
 
Oracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New FeaturesOracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New FeaturesAlex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
MySQL Stored Procedures: Building High Performance Web Applications
MySQL Stored Procedures: Building High Performance Web ApplicationsMySQL Stored Procedures: Building High Performance Web Applications
MySQL Stored Procedures: Building High Performance Web ApplicationsOSSCube
 
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_wormDefcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_wormguest785f78
 
Oracle : Monitoring and Diagnostics without OEM
Oracle : Monitoring and Diagnostics without OEMOracle : Monitoring and Diagnostics without OEM
Oracle : Monitoring and Diagnostics without OEMHemant K Chitale
 
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014Alex Zaballa
 
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsScott Wesley
 
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015Alex Zaballa
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assertfangjiafu
 
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbmsjain.pralabh
 

La actualidad más candente (20)

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
 
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
【Maclean liu技术分享】拨开oracle cbo优化器迷雾,探究histogram直方图之秘 0321
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, How
 
你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能你所不知道的Oracle后台进程Smon功能
你所不知道的Oracle后台进程Smon功能
 
Database administration commands
Database administration commands Database administration commands
Database administration commands
 
Oracle ORA Errors
Oracle ORA ErrorsOracle ORA Errors
Oracle ORA Errors
 
Oracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New FeaturesOracle Database 12.1.0.2 New Features
Oracle Database 12.1.0.2 New Features
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
MySQL Stored Procedures: Building High Performance Web Applications
MySQL Stored Procedures: Building High Performance Web ApplicationsMySQL Stored Procedures: Building High Performance Web Applications
MySQL Stored Procedures: Building High Performance Web Applications
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_wormDefcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
 
Oracle : Monitoring and Diagnostics without OEM
Oracle : Monitoring and Diagnostics without OEMOracle : Monitoring and Diagnostics without OEM
Oracle : Monitoring and Diagnostics without OEM
 
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
Flex Cluster e Flex ASM - GUOB Tech Day - OTN TOUR LA Brazil 2014
 
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
 
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015Oracle Data redaction - GUOB - OTN TOUR LA - 2015
Oracle Data redaction - GUOB - OTN TOUR LA - 2015
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assert
 
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
#29.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 

Destacado

ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure
ISAS On SQL Features like Trigger, Transaction,Batches, Stored ProcedureISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure
ISAS On SQL Features like Trigger, Transaction,Batches, Stored ProcedureShubham Choudahry
 
My sql presentation
My sql presentationMy sql presentation
My sql presentationNikhil Jain
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developersScott Wesley
 
Database Development Mistakes
Database Development MistakesDatabase Development Mistakes
Database Development MistakesMichael Findling
 
Why is the application running so slowly?
Why is the application running so slowly?Why is the application running so slowly?
Why is the application running so slowly?Michael Rosenblum
 
A green solution to solve a race condition problem
A green solution to solve a race condition problemA green solution to solve a race condition problem
A green solution to solve a race condition problemKai Zhou
 
Generic collection types in PLSQL
Generic collection types in PLSQLGeneric collection types in PLSQL
Generic collection types in PLSQLArnold Reuser
 
Striving for Perfection: The Ultimate APEX Application Architecture
Striving for Perfection: The Ultimate APEX Application ArchitectureStriving for Perfection: The Ultimate APEX Application Architecture
Striving for Perfection: The Ultimate APEX Application ArchitectureRoel Hartman
 
utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLSteven Feuerstein
 
MySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs AcademyMySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs Academythewebsacademy
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Take Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL CompilerTake Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL CompilerSteven Feuerstein
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projectsjwjablonski
 

Destacado (20)

ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure
ISAS On SQL Features like Trigger, Transaction,Batches, Stored ProcedureISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure
ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
My sql presentation
My sql presentationMy sql presentation
My sql presentation
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
 
Views Oracle Database
Views Oracle DatabaseViews Oracle Database
Views Oracle Database
 
Oracle 11g new features for developers
Oracle 11g new features for developersOracle 11g new features for developers
Oracle 11g new features for developers
 
Database Development Mistakes
Database Development MistakesDatabase Development Mistakes
Database Development Mistakes
 
Why is the application running so slowly?
Why is the application running so slowly?Why is the application running so slowly?
Why is the application running so slowly?
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Sql presentation 1 by chandan
Sql presentation 1 by chandanSql presentation 1 by chandan
Sql presentation 1 by chandan
 
A green solution to solve a race condition problem
A green solution to solve a race condition problemA green solution to solve a race condition problem
A green solution to solve a race condition problem
 
PLSQL Advanced
PLSQL AdvancedPLSQL Advanced
PLSQL Advanced
 
Generic collection types in PLSQL
Generic collection types in PLSQLGeneric collection types in PLSQL
Generic collection types in PLSQL
 
Striving for Perfection: The Ultimate APEX Application Architecture
Striving for Perfection: The Ultimate APEX Application ArchitectureStriving for Perfection: The Ultimate APEX Application Architecture
Striving for Perfection: The Ultimate APEX Application Architecture
 
utPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQLutPLSQL: Unit Testing for Oracle PL/SQL
utPLSQL: Unit Testing for Oracle PL/SQL
 
user defined function
user defined functionuser defined function
user defined function
 
MySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs AcademyMySql Triggers Tutorial - The Webs Academy
MySql Triggers Tutorial - The Webs Academy
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Take Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL CompilerTake Full Advantage of the Oracle PL/SQL Compiler
Take Full Advantage of the Oracle PL/SQL Compiler
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
 

Similar a fired:14FROM emp;EMPNO ENAME CHAN---------- ---------- ----------7369 SMITH -73697499 ALLEN -74997521 WARD -75217566 JONES -75667654 MARTIN -76547698 BLAKE -76987782 CLARK -77827788 SCOTT -77887839 KING -78397844 TURNER -78447876 ADAMS -78767900 JAMES -79007902 FORD -79027934 MILLER -7934Fired 14 times - once for each row

Performance tuning
Performance tuningPerformance tuning
Performance tuningami111
 
ClojureScript: The Good Parts
ClojureScript: The Good PartsClojureScript: The Good Parts
ClojureScript: The Good PartsKent Ohashi
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueriessiavosh kaviani
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactionstepsum
 
Building a Generic Search Screen using Dynamic SQL
Building a Generic Search Screen using Dynamic SQLBuilding a Generic Search Screen using Dynamic SQL
Building a Generic Search Screen using Dynamic SQLMichael Rosenblum
 
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
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3Belal Arfa
 
Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)Valeriy Kravchuk
 
ORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORMORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORMMikhail Egorov
 
EdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaEdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaLisa Hua
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Alex Zaballa
 

Similar a fired:14FROM emp;EMPNO ENAME CHAN---------- ---------- ----------7369 SMITH -73697499 ALLEN -74997521 WARD -75217566 JONES -75667654 MARTIN -76547698 BLAKE -76987782 CLARK -77827788 SCOTT -77887839 KING -78397844 TURNER -78447876 ADAMS -78767900 JAMES -79007902 FORD -79027934 MILLER -7934Fired 14 times - once for each row (20)

Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
ClojureScript: The Good Parts
ClojureScript: The Good PartsClojureScript: The Good Parts
ClojureScript: The Good Parts
 
Usertracing
UsertracingUsertracing
Usertracing
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
Scala to assembly
Scala to assemblyScala to assembly
Scala to assembly
 
Les07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column SubqueriesLes07[1]Multiple-Column Subqueries
Les07[1]Multiple-Column Subqueries
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactions
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Building a Generic Search Screen using Dynamic SQL
Building a Generic Search Screen using Dynamic SQLBuilding a Generic Search Screen using Dynamic SQL
Building a Generic Search Screen using Dynamic SQL
 
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
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)Applying profilers to my sql (fosdem 2017)
Applying profilers to my sql (fosdem 2017)
 
Using AWR for SQL Analysis
Using AWR for SQL AnalysisUsing AWR for SQL Analysis
Using AWR for SQL Analysis
 
ORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORMORM2Pwn: Exploiting injections in Hibernate ORM
ORM2Pwn: Exploiting injections in Hibernate ORM
 
Pl sql using_xml
Pl sql using_xmlPl sql using_xml
Pl sql using_xml
 
EdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for JavaEdSketch: Execution-Driven Sketching for Java
EdSketch: Execution-Driven Sketching for Java
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
Sge
SgeSge
Sge
 

Último

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Último (20)

Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

fired:14FROM emp;EMPNO ENAME CHAN---------- ---------- ----------7369 SMITH -73697499 ALLEN -74997521 WARD -75217566 JONES -75667654 MARTIN -76547698 BLAKE -76987782 CLARK -77827788 SCOTT -77887839 KING -78397844 TURNER -78447876 ADAMS -78767900 JAMES -79007902 FORD -79027934 MILLER -7934Fired 14 times - once for each row

  • 1. 1 of 60 #589: Expanding the SQL Horizons: PL/SQL User-Defined Functions in the Real World Michael Rosenblum Dulcian, Inc. www.dulcian.com
  • 2. 2 of 60 Who Am I? – “Misha”  Oracle ACE  Co-author of 3 books  PL/SQL for Dummies  Expert PL/SQL Practices  Oracle PL/SQL Performance Tuning Tips & Techniques (Rosenblum & Dorsey, Oracle Press, July 2014)  Won ODTUG 2009 Speaker of the Year  Known for:  SQL and PL/SQL tuning  Complex functionality  Code generators  Repository-based development
  • 3. 3 of 60 © Tom Kyte If you have a thing to do in the database, do it in SQL. If you cannot do it in SQL, do it in PL/SQL. If you cannot do it in either SQL or PL/SQL, do it in Java. If you cannot do it even inJava, do it in C. If you cannot do it in C, are you sure that it needs to be done?
  • 4. 4 of 60 So… The rule:  Working in the same environment is better than jumping between multiple ones. Reasoning:  You pay a price for unnecessary context switches, especially in terms of CPU cost.
  • 5. 5 of 60 But… The reality is more complicated, because it has lots of dimensions:  Performance  Maintainability  Available expertise, etc. If context switches are necessary, it is OK to use multiple environments.
  • 6. 6 of 60 Switching to PL/SQL
  • 7. 7 of 60 Why PL/SQL? Flexibility:  PL/SQL can utilize Dynamic SQL to build SQL statements on the fly that precisely match the situation  … while hardcoded SQL query has to support all possible permutations. Functionality:  PL/SQL can do whatever you can code  … while SQL features are provided to you by Oracle
  • 8. 8 of 60 Generic Search Problem  Use Case:  Complex generic search  Common approach:  Build a single complex SQL statement that takes all possible filters.  Common problem #1:  Some condition permutations cause major performance issues.  Common problem #2:  Optimizing condition A worsens condition B…  Common problem #3:  Optimizing condition B worsens…
  • 9. 9 of 60 Generic Search Solution Proposal:  Build SQL statements on the fly.  Only some filters (and some tables) can be used at any point in time.  Always use bind variables.  Use object collection to bring back the result: CREATE TYPE emp_search_ot AS OBJECT (empno_nr NUMBER, empno_dsp VARCHAR2(256), comp_nr NUMBER); CREATE TYPE emp_search_nt IS TABLE OF emp_search_ot;
  • 10. 10 of 60 Generic Search (1) create function f_search_tt (i_empno number:=null, i_ename_tx varchar2:=null, i_loc_tx varchar2:=null, i_limit_nr number:=50) return emp_search_nt is v_out_tt emp_search_nt:=emp_search_nt(); v_from_tx varchar2(32767):='emp'; v_where_tx varchar2(32767):= 'rownum<=v_limit_nr'; v_plsql_tx varchar2(32767); begin ...
  • 11. 11 of 60 Generic Search (2) if i_empno is not null then v_where_tx:=v_where_tx||chr(10)|| 'and emp.empno=v_empno_nr'; end if; if i_ename_tx is not null then v_where_tx:=v_where_tx||chr(10)|| 'and emp.ename like ''%''||v_ename_tx||''%'''; end if; if i_loc_tx is not null then v_from_tx:=v_from_tx||chr(10)|| 'join dept on (emp.deptno=dept.deptno)'; v_where_tx:=v_where_tx||chr(10)|| 'and dept.loc=v_loc_tx'; end if; ...
  • 12. 12 of 60 Generic Search (3) v_plsql_tx:= 'declare '||chr(10)|| 'v_limit_nr number:=:1;'||chr(10)|| 'v_empno_nr number:=:2;'||chr(10)|| 'v_ename_tx varchar2(256):=:3;'||chr(10)|| 'v_loc_tx varchar2(256):=:4;'||chr(10)|| 'begin '||chr(10)|| 'select emp_search_ot('|| 'emp.empno,emp.ename||''(''||emp.job||'')'','|| 'emp.sal+nvl(emp.comm,0))'||chr(10)|| 'bulk collect into :5'||chr(10)|| 'from '||v_from_tx||chr(10)|| 'where '||v_where_tx||';'||chr(10)|| 'end;'; execute immediate v_plsql_tx using i_limit_nr, i_empno, i_ename_tx, i_loc_tx, out v_out_tt; return v_out_tt; end; All possible binds are variables Type constructor Bulk output
  • 13. 13 of 60 Generic Search(3) Key points:  Table DEPT is joined only when needed.  ANSI SQL comes in handy when you need to split conditions and joins!  Building anonymous blocks instead of PL/SQL simplifies passing bind variables.  Otherwise you need to cover all possible permutations.  EMP_SEARCH_OT constructor must be inside  Because the output is an object collection, not a record
  • 14. 14 of 60 Create your own SQL functionality  Use Case:  Built-in function LISTAGG is limited to 4000 characters  … but it would be nice if it could return a CLOB!  Solution:  Build your own aggregate function using Oracle Extensibility Framework  … if you can survive ODCI syntax   Although…  Oracle normally does not allow aggregate functions to have multiple parameters (LISTAGG is the exception).  Your own aggregate parameter object: CREATE TYPE listAggParam_ot IS OBJECT (value_tx VARCHAR2(4000), separator_tx VARCHAR2(10))
  • 15. 15 of 60 ODCI Object Type (1) create or replace type ListAggCLImpl as object ( v_out_cl clob, v_defaultseparator_tx varchar2(10), static function ODCIAggregateInitialize (sctx in out listaggclimpl) return number, member function ODCIAggregateIterate (self in out listaggclimpl, value_ot in listaggparam_ot) return number, member function ODCIAggregateTerminate (self in listaggclimpl, returnvalue out clob, flags in number) return number, member function ODCIAggregateMerge (self in out listaggclimpl, ctx2 in listaggclimpl) return number ) OUT-Type IN-Type
  • 16. 16 of 60 ODCI Object Type (2)  Type elements (mandatory):  Type attributes  Intermediate data storage  method ODCIAggregateInitialize  Called once per group to initialize all required settings  method ODCIAggregateIterate  Called once for every processed value  The second parameter’s datatype - input to be aggregated.  method ODCIAggregateTerminate  Called once at the end of each group  The second parameter’s datatype - output of aggregate function.  method ODCIAggregateMerge  Called if your aggregate function is running in parallel  Used to put together the results of different threads.
  • 17. 17 of 60 ODCI Object Type Body (1) create or replace type body listaggclimpl is static function ODCIAggregateInitialize (sctx in out listaggclimpl) return number is begin sctx :=listaggclimpl(null,','); return odciconst.success; end; member function ODCIAggregateTerminate (self in listaggclimpl, returnvalue out clob, flags in number) return number is begin returnvalue := self.v_out_cl; return odciconst.success; end; Default constructor Return final value
  • 18. 18 of 60 ODCI Object Type Body (2) member function ODCIAggregateIterate (self in out listaggclimpl, value_ot in listaggparam_ot) return number is begin if self.v_out_cl is null then self.v_defaultseparator_tx:=value_ot.separator_tx; dbms_lob.createtemporary (self.v_out_cl,true,dbms_lob.call); dbms_lob.writeappend (self.v_out_cl, length(value_ot.value_tx),value_ot.value_tx); else dbms_lob.writeappend(self.v_out_cl, length(value_ot.separator_tx||value_ot.value_tx), value_ot.separator_tx||value_ot.value_tx); end if; return odciconst.success; end; Add separators for consecutive writes Initial write
  • 19. 19 of 60 ODCI Object Type Body (3) member function ODCIAggregateMerge (self in out listaggclimpl, ctx2 in listaggclimpl) return number is begin if ctx2.v_out_cl is not null then if self.v_out_cl is null then self.v_out_cl:=ctx2.v_out_cl; else dbms_lob.writeappend(self.v_out_cl, length(self.v_defaultseparator_tx), self.v_defaultseparator_tx); dbms_lob.append(self.v_out_cl,ctx2.v_out_cl); end if; end if; return odciconst.success; end; end; Merge parallel processes in pairs
  • 20. 20 of 60 ODCI Object Type Explanation  Key points:  Method ODCIAggregateInitialize  Has a default constructor to specify the initial values of two attributes of ListAggCLImpl type  Method ODCIAggregateIterate  Adds new values to the existing temporary storage V_OUT_CL via DBMS_LOB APIs  Method ODCIAggregateTerminate  Returns the final value of V_OUT_CL as a formal result  Method ODCIAggregateMerge  Used in case there are parallel executions merging two different V_OUT_CL values into a single output .  Putting everything together: CREATE FUNCTION ListAggCL (value_ot listAggParam_ot) RETURN CLOB PARALLEL_ENABLE AGGREGATE USING ListAggCLImpl;
  • 21. 21 of 60 ListAggCL Usage (1) SQL> SELECT deptno, 2 ListAggCL(listAggParam_ot(ename,',')) list_cl 3 FROM emp 4 GROUP BY deptno; DEPTNO LIST_cl ---------- ------------------------------ 10 CLARK,MILLER,KING 20 SMITH,FORD,ADAMS,SCOTT,JONES 30 ALLEN,JAMES,TURNER,BLAKE,MARTIN,WARD
  • 22. 22 of 60 ListAggCL Usage (2) SQL> SELECT empno, ename, 2 ListAggCL(listAggParam_ot(ename,',')) 3 over(partition by deptno 4 order by ename 5 rows between unbounded preceding 6 and unbounded following 7 ) list_cl 8 FROM emp 9 WHERE job = 'CLERK'; EMPNO ENAME LIST_CL ---------- ---------- ----------------------------------- 7934 MILLER MILLER 7876 ADAMS ADAMS,SMITH 7369 SMITH ADAMS,SMITH 7900 JAMES JAMES Also works as analytical function! Important By default, if you have ORDER BY it is “… AND CURRENT ROW”
  • 23. 23 of 60 Overview Switching from SQL to PL/SQL  Can:  Make your code more flexible and understandable  Improve performance in cases when the CBO is confused  Expand SQL horizons  Cannot:  Do it for free  There will always be a cost of context switching between language environments.
  • 24. 24 of 60 Calling User-Defined Functions within SQL
  • 25. 25 of 60 THE problem Question:  Do you know how many times your function is being executed within a SQL statement? Issue:  Few developers know how to properly ask this question  … and even fewer know how to correctly interpret the answer!
  • 26. 26 of 60 Reminder! SQL statement execution order:  1. JOIN  2. WHERE  3. GROUP BY  4. SELECT (including analytics functions)  5. HAVING  6. ORDER BY
  • 27. 27 of 60 Testing Framework create package counter_pkg is v_nr number:=0; procedure p_check; end; create package body counter_pkg is procedure p_check is begin dbms_output.put_line ('fired:'||counter_pkg.v_nr); counter_pkg.v_nr:=0; end; end; create function f_change_nr (i_nr number) return number is begin counter_pkg.v_nr:=counter_pkg.v_nr+1; return return i_nr*(-1);; end;
  • 28. 28 of 60 SELECT and WHERE  Output: SQL> SELECT empno, ename, f_change_nr(empno) change_nr 2 FROM emp 3 WHERE f_change_nr(empno) IS NOT NULL 4 AND deptno = 20; ... 5 rows selected. SQL> exec counter_pkg.p_check; Fired:10  Explanation:  CBO orders predicates to decrease the total cost  DEPNO=20 is applied first to get 5 rows back  CBO needs correct info (statistics, indexes, constraints etc.) to make that decision  The same functions in SELECT and WHERE clauses are being fired independently. Twice the number of returned rows
  • 29. 29 of 60 SELECT and ORDER BY (1)  Output: SQL> SELECT empno, ename, f_change_nr(empno) change_nr 2 FROM emp WHERE deptno = 20 3 ORDER BY 3; 5 rows selected. SQL> exec counter_pkg.p_check; Fired:5 SQL> SELECT empno, change_nr 2 FROM (SELECT empno, ename, f_change_nr(empno) change 3 FROM emp WHERE deptno = 20 4 ORDER BY 3); 5 rows selected. SQL> exec counter_pkg.p_check; Fired:10  Problem:  Why does the inline view cause a double fire? In-line view causes double-firing
  • 30. 30 of 60 SELECT and ORDER BY (2)  Research of 10053 trace: Order-by elimination (OBYE) *************************** OBYE: OBYE performed. OBYE: OBYE bypassed: no order by to eliminate. Final query after transformations:*** UNPARSED QUERY IS *** SELECT "EMP"."EMPNO" "EMPNO","EMP"."ENAME" "ENAME", "SCOTT"."F_CHANGE_NR"("EMP"."EMPNO") "CHANGE_NR" FROM "SCOTT"."EMP" "EMP" WHERE "EMP"."DEPTNO"=20 ORDER BY "SCOTT"."F_CHANGE_NR"("EMP"."EMPNO")  Explanation:  Order-by elimination is fired before the query transformation.  It does not find an ORDER BY clause in the root SELECT and stops.  After query transformation, ORDER BY suddenly appears.  It is not removed now.
  • 31. 31 of 60 SELECT and ORDER BY (3)  /*+ NO_MERGE */ hint solves the problem: SQL> select empno, change_nr 2 from (select /*+ no_merge */ 3 empno, ename, f_change_nr(empno) change_nr 4 from emp where deptno = 20 order by 3); 5 rows selected. SQL> exec counter_pkg.p_check; fired:5  Why bother? Because of views: SQL> create or replace view v_emp as 2 select empno, ename, f_change_nr(empno) change_nr 3 from emp where deptno = 20 order by 3; view created. SQL> select empno, change_nr from v_emp; 5 rows selected. SQL> exec counter_pkg.p_check; fired:10 Query rewrite is skipped.  Order-by elimination is applied directly. Same effect as in-line view
  • 32. 32 of 60 Multi-Table Example  Output: SQL> SELECT empno, f_change_nr(empno) change_nr, dname 2 FROM emp, 3 dept 4 WHERE emp.deptno(+) = dept.deptno; EMPNO CHANGE_NR DNAME 7782 7782 ACCOUNTING ... 7654 7654 SALES OPERATIONS 15 rows selected. SQL> exec counter_pkg.p_check; Fired:15  Explanation  JOIN is being applied first. It results in 15 rows.  Function in SELECT is fired as many times as the number of rows. No employees!
  • 33. 33 of 60 Section Summary Calling user-defined functions within SQL:  Depends on the SQL internal execution order  Remember: Joins always happen first!  Is impacted by Cost-Based Optimizer decisions:  Query transformation  Predicate transformations  ORDER-BY elimination, etc.
  • 34. 34 of 60 New Cost-Management Features (Oracle 12c)
  • 35. 35 of 60 PRAGMA UDF (1) Meaning:  PL/SQL function is compiled in the way that is optimized for SQL usage (different memory management). Example: CREATE FUNCTION f_change_udf_nr (i_nr NUMBER) RETURN NUMBER IS PRAGMA UDF; BEGIN counter_pkg.v_nr:=counter_pkg.v_nr+1; RETURN i_nr+1; END;
  • 36. 36 of 60 PRAGMA UDF (2) Much faster in SQL: SQL> SELECT MAX(f_change_nr(object_id)) 2 FROM TEST_TAB; MAX(F_CHANGE_NR(OBJECT_ID)) --------------------------- 51485 Elapsed: 00:00:00.48 SQL> SELECT MAX(f_change_udf_nr(object_id)) 2 FROM TEST_TAB; MAX(F_CHANGE_UDF_NR(OBJECT_ID)) ------------------------------- 51485 Elapsed: 00:00:00.06
  • 37. 37 of 60 PRAGMA UDF (3)  Somewhat slower in PL/SQL (1 million iterations): SQL> DECLARE 2 v_out_nr NUMBER; 3 BEGIN 4 FOR i IN 1..1000000 loop 5 v_out_nr:=f_change_nr(i)+f_change_nr(i+1); 6 END LOOP; 7 END; 8 / Elapsed: 00:00:01.39 SQL> DECLARE 2 v_out_nr NUMBER; 3 BEGIN 4 FOR i IN 1..1000000 LOOP 5 v_out_nr:=f_change_udf_nr(i)+f_change_udf_nr(i+1); 6 END LOOP; 7 END; 8 / Elapsed: 00:00:01.89
  • 38. 38 of 60 PRAGMA UDF Overview Looks very promising:  Significant performance gains when used in SQL.  Minimal performance loss when used in PL/SQL. Although…  It is a new feature and has not been extensively tested.
  • 39. 39 of 60 Functions in WITH clause (1)  Meaning:  Functions with the visibility scope of just one SQL query  Example (the same 50000 row table): SQL> WITH FUNCTION f_changeWith_nr (i_nr number) 2 RETURN NUMBER IS 3 BEGIN 4 RETURN i_nr+1; 5 END; 6 SELECT max(f_changeWith_nr(object_id)) 7 FROM test_tab 8 / MAX(F_CHANGEWITH_NR(OBJECT_ID)) ------------------------------- 51485 Elapsed: 00:00:00.07
  • 40. 40 of 60 Functions in WITH clause Overview  Advantages:  Significantly decreases SQL-to-PL/SQL context switches  Drawbacks:  Code fragmentation and duplication  PL/SQL limitations – It is not even supported directly (only via Dynamic SQL).  SQL limitations - If you want to include it anywhere other than a top-level query, you need a hint /*+ WITH_PLSQL*/  Optimization limitations: DETERMINISTIC hint is being ignored.  Remark:  PRAGMA UDF constantly outperforms WITH-clause functions in a number of research papers.  Even here it is 0.06 vs 0.07 sec.  Summary:  Usability of this feature is questionable for now.
  • 42. 42 of 60 Reasoning Question:  Why do you need to do something that can be done once and preserved for future use multiple times? Answer:  You don’t!
  • 43. 43 of 60 Same OUT for the same IN  DETERMINISTIC clause:  Definition: If function does the same thing for exactly the same IN, it can be defined as DETERMINISTIC.  Impact: Oracle may reuse already calculated values.  Warning: Oracle does not check to see whether the function is deterministic or not.  Example: CREATE FUNCTION f_change_det_tx (i_tx VARCHAR2) RETURN VARCHAR2 DETERMINISTIC IS BEGIN counter_pkg.v_nr:=counter_pkg.v_nr+1; RETURN lower(i_tx); END; CREATE FUNCTION f_change_tx (i_tx VARCHAR2) RETURN VARCHAR2 IS BEGIN counter_pkg.v_nr:=counter_pkg.v_nr+1; return lower(i_tx); END; Regular counterpart to see the impact
  • 44. 44 of 60 DETERMINISTIC (1)  Basic Output: SQL> select empno, f_change_tx(job) from emp; SQL> exec counter_pkg.p_check; Fired:14 SQL> select empno, f_change_det_tx(job) from emp; SQL> exec counter_pkg.p_check; Fired:5  Problem:  DETERMINISTIC is a hint, not a directive and can be ignored.  Alternate test - increase the scope (50000 rows table): alter table test_tab add (obj3_tx varchar2(3), obj1_tx varchar2(1)); update test_tab set obj3_tx = upper(substr(object_name,-3)), -- 3442 distinct obj1_tx = upper(substr(object_name,1,1)); -- 26 distinct create type stringlist_tt is table of varchar2(256);
  • 45. 45 of 60 DETERMINISTIC (2) SQL> declare 2 v_obj_tt stringlist_tt; 3 v_count_nr number; 4 cursor c_rec is 5 select f_change_det_tx(obj1_tx) obj_tx from test_tab; 6 begin 7 open c_rec; 8 for i in 1..5 loop 9 fetch c_rec bulk collect into v_obj_tt limit 200; 10 select count(distinct column_value) into v_count_nr 11 from table(cast (v_obj_tt as stringlist_tt)); 12 counter_pkg.p_check; 13 dbms_output.put_line('-real count:'||v_count_nr); 14 end loop; 15 close c_rec; 16 end; Fired:41 - real count:18 Fired:42 - real count:19 Fired:25 - real count:17 Fired:22 - real count:11 Fired:26 - real count:10
  • 46. 46 of 60 DETERMINISTIC(3) Problem:  Number of function calls is lower than 200, but not as low and the number of distinct values. Explanation:  The scope of the DETERMINISTIC clause is one fetch operation (not the whole SQL statement!)  Internally, Oracle has a hash table that preserves IN/OUT pairs.  By default, this hash table has limited size (65536 bytes) and fills up very quickly.  … And you can adjust this size 
  • 47. 47 of 60 DETERMINISTIC (4)  Example (increase hash table size 4 times): SQL> ALTER SESSION 2 SET "_query_execution_cache_max_size"=262144; SQL> ... rerun the last example … Fired:31 - real count:18 Fired:32 - real count:19 Fired:22 - real count:17 Fired:22 - real count:11 Fired:10 - real count:10  Remarks:  It is underscore-parameter, so no Oracle support on this one!  Hash collisions between multiple values of IN-parameters can occur  That’s why you rarely get the number of calls matching the number of distinct values. Originally was: 42 42 25 22 26
  • 48. 48 of 60 DETERMINISTIC Overview Good:  Can improve performance  Very light in terms of system impact and management Bad:  It is a HINT – so you cannot rely on it 100%  Adding it inappropriately can cause data problems. Ugly:  You can adjust internal hash table size, but only at your own risk.  Data order may impact the level of optimization.
  • 49. 49 of 60 Side Effect of SELECT from DUAL  Definitions:  Scalar sub-query returns a single column of a single row (or from the empty rowset)  Scalar sub-query caching is an Oracle internal mechanism to preserve results of such queries while processing more complex ones.  Implemented as in-memory hash table  Cache size is defined by “_query_execution_cache_max_size”  Cache is preserved for the duration of the query.  Last value is preserved even if hash table is full.  Example: SQL> SELECT empno, f_change_tx(job) FROM emp; SQL> exec counter_pkg.p_check; Fired:14 SQL> SELECT empno, (SELECT f_change_tx(job) FROM dual) 2 FROM emp; SQL> exec counter_pkg.p_check; Fired:5 Only 5 distinct jobs
  • 50. 50 of 60 Cache Durationsql> declare 2 v_obj_tt stringlist_tt; 3 v_count_nr number; 4 cursor c_rec is select 5 (select f_change_tx(obj1_tx) from dual) obj_tx from test_tab; 6 begin 7 open c_rec; 8 for i in 1..5 loop 9 fetch c_rec bulk collect into v_obj_tt limit 100; 10 select count(distinct column_value) 11 into v_count_nr from table(cast (v_obj_tt as stringlist_tt)); 12 counter_pkg.p_check; 13 dbms_output.put_line('-real count:'||v_count_nr); 14 end loop; 15 close c_rec; 16 end; Fired:17 - real count:14 Fired:16 - real count:14 Fired:10 - real count:16 Fired:18 - real count:14 Fired:5 - real count:15 Count is less than number of distinct values: Cache duration is query.
  • 51. 51 of 60 Scalar Sub-Query Overview Good:  Can improve performance  No changes to PL/SQL code!  More reliable than DETERMINISTIC hint Bad:  Can be impacted by query transformation  Confusing syntax Ugly:  You can adjust internal hash table size, but only at your own risk.  Data order may impact the level of optimization.
  • 52. 52 of 60 PL/SQL Result Cache
  • 53. 53 of 60 The Idea  PL/SQL Function Result Cache  Database-level cache (cross-session)  Stored in SGA  Automatic cache monitoring and invalidation (starting with Oracle 11g R2)  Example: create function f_getdept_dsp (i_deptno number) return varchar2 result_cache is v_out_tx varchar2(256); begin if i_deptno is null then return null; end if; select initcap(dname) into v_out_tx from dept where deptno=i_deptno; counter_pkg.v_nr:=counter_pkg.v_nr+1; return v_out_tx; end;
  • 54. 54 of 60 Result Cache Basics SQL> SELECT empno, f_getDept_dsp(deptno) dept_dsp 2 FROM emp; EMPNO DEPT_DSP ----------- ------------------------ 7369 Research ... 14 rows selected. SQL> exec counter_pkg.p_check; Fired:3 SQL> SELECT empno, f_getDept_dsp(deptno) dept_dsp 2 FROM emp; EMPNO DEPT_DSP ---------- ---------- 7369 Research ... 14 rows selected. SQL> exec counter_pkg.p_check; Fired:0 No calls at all!
  • 55. 55 of 60 Result Cache Stats SQL> SELECT * FROM v$result_cache_statistics; ID NAME VALUE ----- ------------------------------ ---------- 1 Block Size (Bytes) 1024 2 Block Count Maximum 15360 3 Block Count Current 32 4 Result Size Maximum (Blocks) 768 5 Create Count Success 3 6 Create Count Failure 0 7 Find Count 25 8 Invalidation Count 0 9 Delete Count Invalid 0 10 Delete Count Valid 0 11 Hash Chain Length 1 12 Find Copy Count 25 3 distinct INs
  • 56. 56 of 60 Result Cache Dependencies SQL> SELECT rco.id, rco.name, ao.object_name object_name 2 FROM v$result_cache_objects rco, 3 v$result_cache_dependency rcd, 4 all_objects ao 5 WHERE rco.id = rcd.result_id 6 AND rcd.object_no = ao.object_id 7 order by 1; ID NAME OBJECT_NAME -- ------------------------------------------- ------------- 1 "SCOTT"."F_GETDEPT_DSP"::8."F_ GETDEPT_DSP" DEPT 1 "SCOTT"."F_GETDEPT_DSP"::8.”F_ GETDEPT_DSP" F_GETDEPT_DSP 3 "SCOTT"."F_GETDEPT_DSP"::8."F_ GETDEPT_DSP" DEPT 3 "SCOTT"."F_GETDEPT_DSP"::8.”F_ GETDEPT_DSP" F_GETDEPT_DSP 4 "SCOTT"."F_GETDEPT_DSP"::8."F_ GETDEPT_DSP" DEPT 4 "SCOTT"."F_GETDEPT_DSP"::8.”F_ GETDEPT_DSP" F_GETDEPT_DSP
  • 57. 57 of 60 Result Cache Cost  It is not free: SQL> exec runstats_pkg.rs_start SQL> SELECT MAX(f_change_cache_tx(obj1_tx)) FROM test_tab; SQL> exec counter_pkg.p_check; Fired:26 SQL> exec runstats_pkg.rs_middle SQL> SELECT MAX(f_change_det_tx(obj1_tx)) FROM test_tab; SQL> exec counter_pkg.p_check; Fired:7627 SQL> exec runstats_pkg.rs_stop Run1 ran in 65 cpu hsecs Run2 ran in 16 cpu hsecs Name Run1 Run2 Diff STAT...CPU used by this session 64 16 -48 LATCH.Result Cache: RC Latch 50,079 0 -50,079 Run1 latches total versus runs -- difference and pct Run1 Run2 Diff Pct 52,388 2,057 -50,331 2,546.82% DETERMINISTIC is faster because of latching.
  • 58. 58 of 60 PL/SQL Result Cache Overview Good:  Database-level caching  Automatic dependency tracking  Extended statistics  Very informative data dictionary views Bad:  Significant memory utilization Ugly:  Noticeable CPU impact when retrieving values from cache (because of latching)  Too easy to add without real understanding
  • 59. 59 of 60 Summary PL/SQL can significantly extend SQL functionality. You must worry about the total number of user- defined function calls. PRAGMA UDF can decrease the cost of function calls within SQL. Different caching techniques can improve the overall system performance.
  • 60. 60 of 60 Contact Information  Michael Rosenblum – mrosenblum@dulcian.com  Blog – wonderingmisha.blogspot.com  Website – www.dulcian.com Coming in July 2014 Oracle PL/SQL Performance Tuning Tips & Techniques

Notas del editor

  1. 60