SlideShare a Scribd company logo
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		|
High Performance PL/SQL
Bulk Processing, Function Result Cache and More
1
Steven	Feuerstein
Oracle	Developer	Advocate	for	PL/SQL
Oracle Corporation
Email:	steven.feuerstein@oracle.com
Twitter:	@sfonplsql
Blog:	stevenfeuersteinonplsql.blogspot.com
YouTube:	Practically	Perfect	PL/SQL
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	2
Deepen	Your	PL/SQL	and	SQL	Expertise
• Take	advantage	of	our	community	websites.
• Oracle	AskTOM	– https://asktom.oracle.com
–Q&A	site,	Office	Hours	with	database	experts,	and	much	more
• Oracle	Dev	Gym	– https://devgym.oracle.com
–Quizzes,	workouts	and	classes	for	an	active	learning	experience
• Oracle	LiveSQL	– https://livesql.oracle.com
–24x7	access	to	the	latest	release	of	Oracle	Database,	plus	a	script	library	and	
tutorials
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	3
Key	Performance	Features
• Bulk	processing	with	FORALL	and	BULK	COLLECT
• Function	Result	Cache
• Improved	performance	of	PL/SQL	functions	from	SQL
• NOCOPY
• Automatic	optimization
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	4
What’s	the	problem	with	this	code?
• We	have,	on	average,	10,000	employees	per	department.
CREATE OR REPLACE PROCEDURE upd_for_dept (
dept_in IN employees.department_id%TYPE
,newsal_in IN employees.salary%TYPE)
IS
CURSOR emp_cur IS
SELECT employee_id,salary,hire_date
FROM employees WHERE department_id = dept_in;
BEGIN
FOR rec IN emp_cur
LOOP
adjust_compensation (rec, newsal_in);
UPDATE employee SET salary = rec.salary
WHERE employee_id = rec.employee_id;
END LOOP;
END upd_for_dept;
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	5
Row-by-row	=	Slow-by-slow?	
• Many	PL/SQL	blocks	execute	the	same	SQL	statement	repeatedly,	with	
different	bind	values.
–Retrieves	data	one	row	at	a	time.
–Performs	same	DML	operation	for	each	row	retrieved.
• The	SQL	engine	does	a	lot	to	optimize	performance,	but	row-by-row	
processing	is	inherently	slow.
– But,	but...aren't	SQL	and	PL/SQL	supposed	to	be	very	tightly	integrated?	Let's	take	a	
look	"under	the	covers.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	6
Oracle server
PL/SQL Runtime Engine SQL Engine
PL/SQL block
Procedural
statement
executor SQL statement
executor
FOR rec IN emp_cur LOOP
UPDATE employee
SET salary = ...
WHERE employee_id =
rec.employee_id;
END LOOP;
Performance penalty for many
“context switches”
Repetitive	statement	processing	from	PL/SQL
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	7
Bulk	Processing	in	PL/SQL
• The	goal	is	straightforward:	reduce	the	number	of	context	switches	and	
you	improve	performance.
• To	do	this,	Oracle	"bundles	up"	the	requests	for	data	(or	to	change	data)	
and	then	passes	them	with	a	single	context	switch.	
• FORALL	speeds	up	non-query	DML.
–Use	with	inserts,	updates,	deletes	and	merges.
–Move	data	from	collections	to	tables.
• BULK	COLLECT	speeds	up	queries.
–Can	be	used	with	all	kinds	of	queries:	implicit,	explicit,	static	and	dynamic.	
–Move	data	from	tables	into	collections.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	8
Bulk	processing	with	FORALL
Oracle server
PL/SQL Runtime Engine SQL Engine
PL/SQL block
Procedural
statement
executor SQL statement
executor
FORALL indx IN
list_of_emps.FIRST ..
list_of_emps.LAST
UPDATE employee
SET salary = ...
WHERE employee_id =
list_of_emps(indx);
Fewer context switches,
same SQL behavior
Update...
Update...
Update...
Update...
Update...
Update...
Update...
Update...
Update...
Update...
Update...
Update...
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	9
Impact	of	Bulk	Processing	in	SQL	layer
• The	bulk	processing	features	of	PL/SQL	change	the	way	the	PL/SQL	engine	
communicates	with	the	SQL	layer.
• For	both	FORALL	and	BULK	COLLECT,	the	processing	in	the	SQL	engine	is	
almost	completely	unchanged.
–Same	transaction	and	rollback	segment	management
–Same	number	of	individual	SQL	statements	will	be	executed.
• Only	one	difference:	BEFORE	and	AFTER	statement-level	triggers	only	
fire	once per	FORALL	INSERT	statements.
–Not	for	each	INSERT	statement	passed	to	the	SQL	engine	from	the	FORALL	
statement.
statement_trigger_and_forall.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	10
BULK	COLLECT	for	multi-row	querying	
• Retrieve	multiple	rows	into	a	collection	with	a	single	fetch	(context	switch	to	the	SQL	
engine).
– Deposit	the	multiple	rows	of	data	into	one	or	more	collections.
• NO_DATA_FOUND	is	not raised	when	no	rows	are	fetched;	instead,	the	collection	is	
empty.
• The	"INTO"	collections	are	filled	sequentially	from	index	value	1.
– There	are	no	"gaps"	between	1	and	the	index	value	returned	by	the	COUNT	method.
• Only	integer-indexed	collections	may	be	used.
• No	need	to	initialize	or	extend	nested	tables	and	varrays.	Done	automatically	by	
Oracle.
SELECT * BULK COLLECT INTO collection(s) FROM table;
FETCH cur BULK COLLECT INTO collection(s) [ LIMIT number_of_rows ];
EXECUTE IMMEDIATE query BULK COLLECT INTO collection(s);
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	11
BULK	COLLECT	with	Implicit	Cursor
• An	"unlimited"	bulk	collect	operation	that	populates	the	collection	with	all	
rows	found.	Which	can	lead	to	PGA	memory	errors!
DECLARE
TYPE employees_aat IS TABLE OF
employees%ROWTYPE;
l_employees employees_aat;
BEGIN
SELECT *
BULK COLLECT INTO l_employees
FROM employees;
FOR indx IN 1 .. l_employees.COUNT
LOOP
process_employee (l_employees(indx));
END LOOP;
END;
bulkcoll.sql
bulkcollect.tst
Declare a nested table of records
to hold the queried data.
Fetch all rows into collection
sequentially, starting with 1.
Iterate through the collection
contents with a loop.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	12
Limit	rows	returned	by	BULK		COLLECT
CREATE OR REPLACE PROCEDURE bulk_with_limit
(deptno_in IN dept.deptno%TYPE)
IS
CURSOR emps_in_dept_cur IS
SELECT * FROM emp
WHERE deptno = deptno_in;
TYPE emp_tt IS TABLE OF emps_in_dept_cur%ROWTYPE;
emps emp_tt;
BEGIN
OPEN emps_in_dept_cur;
LOOP
FETCH emps_in_dept_cur
BULK COLLECT INTO emps LIMIT 1000;
EXIT WHEN emps.COUNT = 0;
process_emps (emps);
END LOOP;
CLOSE emps_in_dept_cur;
END bulk_with_limit;
Use the LIMIT clause with the INTO to
manage the amount of memory used
with the BULK COLLECT operation.
Definitely the preferred approach in
production applications with large or
varying datasets.
bulklimit.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	13
Details	on	that	LIMIT	clause
• The	limit	value	can	be	a	literal	or	a	variable.
– I	suggest	passing	the	limit	as	a	parameter to	give	you	maximum	flexibility.
• A	limit	of	100	seems	like	a	good	default	value.
– Setting	it	to	500	or	1000	doesn't	seem	to	make	much	difference	in	performance.
• With	very	large	volumes	of	data	and	small	numbers	of	batch	processes,	
however,	a	larger	LIMIT	could	help.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	14
When	to	convert	to	BULK	COLLECT
• Prior	to	Oracle10g,	you	should	convert	all multiple	row	fetch	code	to	BULK	
COLLECTs.
• On	10.1	and	higher,	the	optimizer	will	automatically	optimize	cursor	FOR	
loops	to	run	at	performance	levels	similar	to	BULK	COLLECT.
• So	leave	your	cursor	for	loops	in	place	if	they...
– contain	no DML	operations.
– seem	to	be	running	fast	enough.
• Explicit	BULK	COLLECTs	may	run	a	little	faster	than	cursor	for	loops	
optimized	to	return	100	rows	with	each	fetch.
10g_optimize_cfl.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	15
BULK	COLLECT	Conclusions	
• BULK	COLLECT	improves	performance	of	queries	that	retrieve	more	than	
one	row.
• Use	the	LIMIT	clause	to	avoid	excessive	PGA	memory	consumption.
• Leave	it	to	the	optimizer	to	speed	up	"read	only"	cursor	FOR	loops.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	16
FORALL
• Introduction	to	FORALL
• Using	the	SQL%BULK_ROWCOUNT
• Referencing	fields	of	collections	of	records
• Using	FORALL	with	sparsely-filled	collections
• Handling	errors	raised	during	execution	of	FORALL
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	17
Use	FORALL	for	repeated	DML	operations
• Convert	loops	that	contain	inserts,	updates,	deletes	or	merges	to	FORALL	
statements.
• Header	(usually)	looks	identical	to	a	numeric	FOR	loop.
– Implicitly	declared	integer	iterator
– At	least	one	bind	array	that	uses	the	iterator	as	its	index	value.
– Use	INDICES	OF	and	VALUES	OF	when	bind	arrays	are	sparse
PROCEDURE upd_for_dept (...) IS
BEGIN
FORALL indx IN low_value .. high_value
UPDATE employee
SET salary = newsal_in
WHERE employee_id = list_of_emps (indx);
END;
forall_timing.sql
forall_examples.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	18
More	on	FORALL
• Use	any	type	of	collection	with	FORALL.
• Only	one	DML	statement	is	allowed	per	FORALL.
–Each	FORALL	is	its	own	"extended"	DML	statement.
• The	collection	must	be	indexed	by	integer.
• The	bind	array	must	be	sequentially	filled.
–Unless	you	use	the	INDICES	OF	or	VALUES	OF	clause.
• Indexes	cannot	be	expressions.
forall_restrictions.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	19
How	many	rows	were	modified?
• SQL%ROWCOUNT	returns	total	number	of	rows	modified	by	entire	
FORALL.
– Not	to	be	relied	on	when	used	with	LOG	ERRORS.
• Use	the	SQL%BULK_ROWCOUNT	cursor	attribute	to	determine	how	
many	rows	are	modified	by	each	statement.
– A	"pseudo-collection"	of	integers;	no	methods	are	defined	for	this	element.
bulk_rowcount.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	20
Using	FORALL	with	Sparse	Collections
• Prior	to	10.2,	the	binding	arrays	in	a	FORALL	statement	must be	
sequentially	filled.
– Using	the	IN	low	..	high	syntax.
• Now,	however,	you	can	bind	sparse	collections	by	using	INDICES	OF	and	
VALUES	OF	in	the	FORALL	header.
10g_indices_of*.sql
10g_values_of*.sql
PROCEDURE upd_for_dept (...) IS
BEGIN
FORALL indx IN INDICES OF list_of_emps
UPDATE employee
SET salary = newsal_in
WHERE employee_id = list_of_emps (indx);
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	21
FORALL	and	DML	Errors
• FORALLs	typically	execute	a	large	number	of	DML	statements.
• When	an	exception	occurs	in	one	of	those	DML	statement,	the	default	
behavior	is:
– That statement	is	rolled	back	and	the	FORALL	stops.
– All	(previous)	successful	statements	are	not rolled	back.
• What	if	you	want	the	FORALL	processing	to	continue,	even	if	an	error	
occurs	in	one	of	the	statements?
• Just	add	the	SAVE	EXCEPTIONS	clause!
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	22
Example:	FORALL	with	SAVE	EXCEPTIONS
• Add SAVE EXCEPTIONS to enable FORALL to suppress
errors at the statement level.
CREATE OR REPLACE PROCEDURE load_books (books_in IN book_obj_list_t)
IS
bulk_errors EXCEPTION;
PRAGMA EXCEPTION_INIT ( bulk_errors, -24381 );
BEGIN
FORALL indx IN books_in.FIRST .. books_in.LAST
SAVE EXCEPTIONS
INSERT INTO book values (books_in(indx));
EXCEPTION
WHEN bulk_errors THEN
FOR indx in 1..SQL%BULK_EXCEPTIONS.COUNT
LOOP
log_error (SQL%BULK_EXCEPTIONS(indx).ERROR_INDEX
, SQL%BULK_EXCEPTIONS(indx).ERROR_CODE);
END LOOP;
END;
Allows processing of all
statements, even after an error
occurs.
Iterate through "pseudo-
collection" of errors.
bulkexc.sql
If any exception is
encountered, Oracle raises
-24381 when done.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	23
SAVE	EXCEPTIONS	and	FORALL
• The	SAVE	EXCEPTIONS	clause	tells	Oracle	to	save exception	information	and	
continue	processing	all	of	the	DML	statements.
• When	the	FORALL	statement	completes,	if	at	least	one	exception	occurred,	
Oracle	then	raises	ORA-24381.
• You	then	check	the	contents	of	SQL%BULK_EXCEPTIONS.
PROCEDURE upd_for_dept (newsal_in IN NUMBER,
list_of_emps_in IN DBMS_SQL.NUMBER_TABLE) IS
BEGIN
FORALL indx IN list_of_emps_in.FIRST .. list_of_emps_in.LAST
SAVE EXCEPTIONS
UPDATE employees
SET salary = newsal_in
WHERE employee_id = list_of_emps_in (indx);
END;
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	24
SAVE	EXCEPTIONS	in	Detail
• For	each	exception	raised,	Oracle	populates	the	SQL%BULK_EXCEPTIONS	
pseudo-collection	of	records.
– The	record	has	two	fields	:	ERROR_INDEX	and	ERROR_CODE.
– ERROR_INDEX:	the	index	in	the	bind	array	for	which	the	error	occurred.
– ERROR_CODE:	the	number	(positive)	for	the	error	that	was	raised
• It's	a	pseudo-collection and	only	supports	a	single	method:	COUNT.
• So	you	iterate	from	1	to	SQL%BULK_EXCEPTIONS.COUNT	to	get	
information	about	each	error.
• Unfortunately,	it	does	not	store	the	error	message.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	25
Converting	to	Bulk	Processing
• Let's	take	a	look	at	the	process	by	which	you	go	from	"old-fashioned"	code	
to	a	bulk	processing-based	solution.
• From	integrated	row-by-row	to	phased processing
• Challenges	include:
– With	multiple	DML	statements	in	loop,	how	do	you	"communicate"	from	one	to	the	
other?
– Avoid	excessive	PGA	consumption
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	26
The	"Old	Fashioned"	Approach
• Cursor	FOR	loop	with	two	DML	statements,	trap	exception,	and	keep	on	
going.
CREATE OR REPLACE PROCEDURE upd_for_dept (
dept_in IN employees.department_id%TYPE
, newsal_in IN employees.salary%TYPE)
IS
CURSOR emp_cur ...;
BEGIN
FOR rec IN emp_cur
LOOP
BEGIN
INSERT INTO employee_history ...
adjust_compensation (rec.employee_id, rec.salary);
UPDATE employees SET salary = rec.salary ...
EXCEPTION
WHEN OTHERS THEN log_error;
END;
END LOOP;
END upd_for_dept;
cfl_to_bulk_0.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	27
A	phased	approach	with	bulk	processing
• Change	from	integrated,	row-by-row	approach	to	a	phased	approach.
Relational	
Table(s)
Relational	
Table
Phase	1:	Bulk	collect	from	table(s)	to	collection
Phase	3:	FORALL	from	collection	to	table
Phase	2:	Modify	contents	of	collection	
according	to	requirements
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	28
Translating	phases	into	code
• The	cfl_to_bulk_5.sql	file	contains	the	converted	program,	following	the	
phased	approach.
cfl_to_bulk_0.sql
cfl_to_bulk_5_11g.sql
BEGIN
OPEN employees_cur;
LOOP
fetch_next_set_of_rows (
bulk_limit_in, employee_ids, salaries, hire_dates);
EXIT WHEN employee_ids.COUNT = 0;
insert_history;
adj_comp_for_arrays (employee_ids, salaries);
update_employee;
END LOOP;
END;
Phase	1:
Get	Data
Phase	3:
Push	Data
Phase	2:
Massage	Data
Phase	3:
Push	Data
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	29
Conclusions	– Bulk	Processing	
• FORALL	is	the	most	important	performance	tuning	feature	in	PL/SQL.
– Almost	always	the	fastest	way	to	execute	repeated	SQL	operations	in	PL/SQL.
– Look	that	for	prime	anti-pattern:	Loops	containing	non-query	DML.
– Then	ask	yourself:	can	I	do	it	entirely	within	SQL?	If	not...bulk	it	up!
• You	trade	off	increased	complexity	of	code	for	dramatically	faster	
execution.
– But	remember	that	Oracle	will	automatically	optimize	cursor	FOR	loops	to	BULK	
COLLECT	efficiency.
– No	need	to	convert	unless	the	loop	contains	DML	or	you	want	to	maximally	
optimize	your	code.
• Watch	out	for	the	impact	on	PGA	memory!
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	30
What's	wrong	with	this	picture?
• .
SELECT * FROM materialized_view
WHERE pky = 12345;
What's	Taking So	Long?
SELECT * FROM materialized_view
WHERE pky = 12345;
What's	Taking So	Long?
SELECT * FROM materialized_view
WHERE pky = 12345;
What's	Taking So	Long?
SELECT * FROM materialized_view
WHERE pky = 12345;
What's	Taking So	Long?
SELECT * FROM materialized_view
WHERE pky = 12345;
What's	Taking So	Long?
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	31
Here's	the	question	to	ask	oneself:
• But	they	need	the	data!	And	it	needs	to	be	the	right	data!	What's	a	
developer	supposed	to	do?
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	32
The	Wonderful	Function	Result	Cache
• The	Function	Result	Cache	should	be	the	caching	mechanism	of	choice	
for	PL/SQL	developers.
– Others:	DETERMINISTIC,	PGA	(session-specific)	caching
• This	cache	is stored	in	the	SGA	;	shared	across	sessions;	purged	of	dirty	
data	automatically
• You	can	and	should	use	it	to	retrieve	data	from	any	table	that	is	queried	
more	frequently	than	updated.
– Static	datasets	like	materialized	views	(the	"extreme	case")
– Same	rows	fetched	multiple	times	(parameter	values	serve	as	unique	index	into	
cache)
• Enterprise	Edition	only.
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	33
How	the	Function	Result	Cache	Works
• Add	the	RESULT_CACHE	clause	to	your	function's	header.
– Yes,	that's	the	only	change	you	have	to	make	to	your	code!
• When	a	call	is	made	to	function,	Oracle	compares	IN	argument	values	to	
the	cache.
• If	no	match,	the	function	is	executed	and	the	inputs	and	return	data	are	
cached.
• If	a	match	is	found,	the	function	is	not	executed;	cached	data	is	returned.
• If	changes	to	a	"relies	on"	table	are	committed,	the	cache	is	marked	
invalid	and	will	be	re-built.
11g_frc_demo.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	34
Performance	Impact	of	Result	Cache
• The	result	cache	is	stored	in	the	SGA.
• So	we	should	expect	it	be	slower	than	a	PGA-based	cache.
• But	accessing	result	cache	data	does	not	require	going	through	the	SQL	
engine.
• So	it	should	be	much	faster	than	executing	a	query.
– Even	if	the	statement	is	parsed	and	the	data	blocks	are	already	in	the	SGA.
• Let's	find	out!
11g_emplu*.*
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	35
Result	Cache	– Things	to	Keep	in	Mind	- 1
• If	you	have	uncommitted	changes	in	your	session,	dependent	caches	are	
ignored.
– The	cache	will	not override	your	own	changed	data.
• Caching	is	not	performed	for	complex	types:	records	with	CLOBs,	
collections,	etc.
– But	Oracle	is	optimistic!
• The	cache	is	not related	to	SQL	statements	in	your	function.
– It	only	keeps	track	of	the	input	values	and	the	RETURN	clause	data.
11g_frc_demo.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	36
Result	Cache	– Things	to	Keep	in	Mind	- 2
• You	cannot	use	the	result	cache	with	invoker	rights	program	units	until	12.1.
– Bypass	execution	of	function	body,	Oracle	cannot	resolve	references	to	objects	- the	
whole	point of	IR.
• Functions	with	session-specific	dependencies	must	be		"result-cached"	with	
great	care.
– Virtual	private	database	configurations
– References	to	SYSDATE,	reliance	on	NLS_DATE_FORMAT,	time	zone	changes
– Application	contexts	(calls	to	SYS_CONTEXT)
• Solution:	move	all	dependencies	into	parameter	list.
11g_frc_vpd.sql
11g_frc_vpd2.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	37
Managing	the	Result	Cache
• Oracle	offers	a	number	of	ways	to	manage	the	result	cache	and	tune	it	to	
your	specific	application	needs:
• RESULT_CACHE_MAX_SIZE	initialization	parameter
– If	the	cache	is	too	small,	then	the	LRU	algorithm	negates	the	point	of	the	cache.
• DBMS_RESULT_CACHE	management	package
• v$RESULT_CACHE_*	performance	views
• Warning:	use	of	the	function	result	cache	can	result	in	latch	contention	that	
can	cause	database	performance	degradation.
show_frc_dependencies.sp
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	38
Fine	Grained	Dependencies	in	11.2
• Oracle	keeps	track	of	table	dependencies	on	a	per-result	level.
– Each	result	cached	could	have	a	different	set	of	dependencies.
• A	change	to	a	table	could	invalidate	just	a	subset	of	the	results	in	the	cache.
– It's	not all	or	nothing	- when	your	function's	different	logic	paths	could	"hit"	different	
tables.	
11g_frc_dependencies.sql
11g_frc_dependencies2.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	39
Make	It	Easy	on	Yourself
• I	hope	you	will	agree	that	the	result	cache	is	a	great	feature.
• But	how	easy	will	it	be	for	you	to	apply	it?
• If	you	write/duplicate	queries	throughout	your	code,	upgrading	will	be	
expensive	and	slow.
• If	you	hide	your	queries	behind	functions,	you	have	a	single	point	of	
definition,	and	you	can	upgrade	"instantly."
11g_frc_encapsulation.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	40
The	NOCOPY	Parameter	Hint
• By	default,	Oracle	passes	all	OUT	and	IN	OUT	arguments	by	value,	not	
reference.
– This	means	that	OUT	and	IN	OUT	arguments	always	involve	some	copying of	data.
– All	IN	arguments	are	always	passed	by	reference	(no	copying).
• With	NOCOPY,	you	turn	off	the	copy	process.
– For	"large"	formal	parameters,	such	as	collections,	you	may	see	a	performance	
benefit.
• Compile-time	warnings	will	notify	you	of	opportunities	to	use	this	hint.
nocopy*.*
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	41
Optimizing	Function	Execution	in	SQL
• That	seems	like	an	awfully	good	idea!
• Two	methods:
– WITH	clause	that	defines a	function
– UDF	pragma
• WITH	FUNCTION:	define	a	function	directly	within	your	SQL	statement.
– Most	helpful	for	avoiding	redundancy	in	long	SQL	statements
• UDF	pragma	declaratively	tells	the	compiler	to	"prepare"	the	function	for	
execution	from	SQL.
– Reduces	the	cost	of	the	context	switch
12c_with_function*.sql			
12c_udf*.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	42
Automatic	Optimization	– Set	to	Level	3
• The	default	optimization	level	is	2.
– It's	been	2	since	10.1,	and	it's	a	fine	optimization	level.
• But	11.1	introduced	automatic	subprogram	inlining	– level	3
– Works	with	nested	subprograms	only!
• Recommendation	from	PL/SQL	dev team:	always	move	optimization	level	
up	to	3.	
– Only	downside	you	are	likely	to	see	is	increased	compiled	code	size.	That	should	not	
be	an	issue.
– You	can	also	use	INLINE	pragma	for	selective	inlining.
11g_inline*.sql
Copyright	©	2015	Oracle	and/or	its	affiliates.	All	rights	reserved.		| Page	43
PL/SQL	Optimization
• Make	sure	you	integrate	the	"big	ticket"	performance	features	as	you	
develop.
– Native	SQL,	bulk	processing,	FORALL,	NOCOPY,	UDF,	opt	level	3...
• Focus	primarily	on	correctness	and	maintainability.
• Then	identify	bottlenecks.
– DBMS_HPROF,	DBMS_PROFILER
• Then	perform	more	granular	optimization	within	identified	subprograms.
44

More Related Content

PDF
Same plan different performance
PDF
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
PDF
Oracle Performance Tuning Fundamentals
PPT
Ash masters : advanced ash analytics on Oracle
PDF
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
PDF
Oracle Real Application Clusters (RAC) 12c Rel. 2 - Operational Best Practices
PDF
Advanced MySQL Query Tuning
PPTX
Explain the explain_plan
Same plan different performance
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Oracle Performance Tuning Fundamentals
Ash masters : advanced ash analytics on Oracle
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Oracle Real Application Clusters (RAC) 12c Rel. 2 - Operational Best Practices
Advanced MySQL Query Tuning
Explain the explain_plan

What's hot (20)

PDF
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
PPTX
The Amazing and Elegant PL/SQL Function Result Cache
PDF
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 1
PPTX
AWR and ASH Deep Dive
PDF
Linux tuning to improve PostgreSQL performance
PPTX
What’s New in Oracle Database 19c - Part 1
PDF
Practical Partitioning in Production with Postgres
 
PPT
OOUG: Oracle transaction locking
PPTX
Why oracle data guard new features in oracle 18c, 19c
PPTX
Oracle sql high performance tuning
PDF
DB12c: All You Need to Know About the Resource Manager
PDF
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
PDF
PostgreSQL Deep Internal
PPTX
PostgreSQL Database Slides
PDF
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
PPT
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
PDF
Real-time Data Ingestion from Kafka to ClickHouse with Deterministic Re-tries...
PPTX
re:Invent 2022 DAT326 Deep dive into Amazon Aurora and its innovations
PDF
Oracle db performance tuning
PPSX
Oracle Performance Tuning Fundamentals
DB Time, Average Active Sessions, and ASH Math - Oracle performance fundamentals
The Amazing and Elegant PL/SQL Function Result Cache
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 1
AWR and ASH Deep Dive
Linux tuning to improve PostgreSQL performance
What’s New in Oracle Database 19c - Part 1
Practical Partitioning in Production with Postgres
 
OOUG: Oracle transaction locking
Why oracle data guard new features in oracle 18c, 19c
Oracle sql high performance tuning
DB12c: All You Need to Know About the Resource Manager
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
PostgreSQL Deep Internal
PostgreSQL Database Slides
Query Optimization with MySQL 5.6: Old and New Tricks - Percona Live London 2013
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
Real-time Data Ingestion from Kafka to ClickHouse with Deterministic Re-tries...
re:Invent 2022 DAT326 Deep dive into Amazon Aurora and its innovations
Oracle db performance tuning
Oracle Performance Tuning Fundamentals
Ad

Similar to High Performance PL/SQL (20)

PPTX
Turbocharge SQL Performance in PL/SQL with Bulk Processing
PPT
Oracle - SQL-PL/SQL context switching
PPT
oracle pl-sql lec 7 oracle pl-sql lec 7 plsql Lec07.ppt
PPT
Oracle PL/SQL Bulk binds
PPTX
PLSQL Advanced
PPT
08 Dynamic SQL and Metadata
PPTX
How to tune a query - ODTUG 2012
DOCX
Trig
DOCX
PLSQL.docx
PDF
ILOUG 2019 - SQL features for Developers
PDF
SQL & Adv SQL - Basics and Advanced for Beginners
DOCX
Top MNC'S Interview questions and answers
PPTX
Sangam 18 - Great Applications with Great SQL
PPT
Les06- Subqueries.ppt
PPTX
PLSQL Practices
PPTX
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
PPTX
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
PPTX
Tom Kyte at Hotsos 2015
PPTX
Introduction Oracle Database 11g Release 2 for developers
PPT
11 Things About 11gr2
Turbocharge SQL Performance in PL/SQL with Bulk Processing
Oracle - SQL-PL/SQL context switching
oracle pl-sql lec 7 oracle pl-sql lec 7 plsql Lec07.ppt
Oracle PL/SQL Bulk binds
PLSQL Advanced
08 Dynamic SQL and Metadata
How to tune a query - ODTUG 2012
Trig
PLSQL.docx
ILOUG 2019 - SQL features for Developers
SQL & Adv SQL - Basics and Advanced for Beginners
Top MNC'S Interview questions and answers
Sangam 18 - Great Applications with Great SQL
Les06- Subqueries.ppt
PLSQL Practices
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
Tom Kyte at Hotsos 2015
Introduction Oracle Database 11g Release 2 for developers
11 Things About 11gr2
Ad

More from Steven Feuerstein (18)

PDF
New(er) Stuff in PL/SQL
PDF
Six simple steps to unit testing happiness
PDF
Speakers at Nov 2019 PL/SQL Office Hours session
PDF
New Stuff in the Oracle PL/SQL Language
PDF
AskTOM Office Hours on Database Triggers
PDF
PL/SQL Guilty Pleasures
PDF
Oracle Application Express and PL/SQL: a world-class combo
PDF
Error Management Features of PL/SQL
PDF
JSON and PL/SQL: A Match Made in Database
PDF
OLD APEX and PL/SQL
PDF
Oracle PL/SQL 12c and 18c New Features + RADstack + Community Sites
PDF
AskTOM Office Hours - Dynamic SQL in PL/SQL
PPT
Database Developers: the most important developers on earth?
PDF
Unit Testing Oracle PL/SQL Code: utPLSQL, Excel and More
PDF
Impact Analysis with PL/Scope
PDF
All About PL/SQL Collections
PPTX
Take Full Advantage of the Oracle PL/SQL Compiler
PPTX
utPLSQL: Unit Testing for Oracle PL/SQL
New(er) Stuff in PL/SQL
Six simple steps to unit testing happiness
Speakers at Nov 2019 PL/SQL Office Hours session
New Stuff in the Oracle PL/SQL Language
AskTOM Office Hours on Database Triggers
PL/SQL Guilty Pleasures
Oracle Application Express and PL/SQL: a world-class combo
Error Management Features of PL/SQL
JSON and PL/SQL: A Match Made in Database
OLD APEX and PL/SQL
Oracle PL/SQL 12c and 18c New Features + RADstack + Community Sites
AskTOM Office Hours - Dynamic SQL in PL/SQL
Database Developers: the most important developers on earth?
Unit Testing Oracle PL/SQL Code: utPLSQL, Excel and More
Impact Analysis with PL/Scope
All About PL/SQL Collections
Take Full Advantage of the Oracle PL/SQL Compiler
utPLSQL: Unit Testing for Oracle PL/SQL

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
Teaching material agriculture food technology
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Understanding_Digital_Forensics_Presentation.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Teaching material agriculture food technology
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Electronic commerce courselecture one. Pdf

High Performance PL/SQL