Sunteți pe pagina 1din 10

Oracle PL/SQL Programming

The PL/SQL Channel


Bulk Processing in Oracle PL/SQL
Introduction and Overview

Steven Feuerstein
steven@stevenfeuerstein.com
www.StevenFeuerstein.com
www.PLSQLChallenge.com

Oracle PL/SQL Programming

To get the most out of this webinar


Prerequisites: Working knowledge of PL/SQL,
and in particular with collections.
Oracle version: Oracle10g Release 2 or higher
Access to demonstration code
Unless otherwise mentioned, all code is available
from the demo.zip file on Quest Software's
PL/SQL Obsession site, which is located at:
PowerPoints for the trainings
www.ToadWorld.com/SF
are also available on the
"Trainings, Seminars and
Presentations" page.
Copyright 2010 Feuerstein and Associates

Page 2

Oracle PL/SQL Programming

Bulk Processing in PL/SQL - Agenda


The problem with executing SQL in PL/SQL
Relationship between the PL/SQL and SQL
statement execution engines
Introducing BULK COLLECT and FORALL

Copyright 2010 Feuerstein and Associates

Page 3

Oracle PL/SQL Programming

What's the problem?


Many PL/SQL blocks execute the same SQL statement
repeatedly.
Retrieve data one row at a time.
Performs same DML operation for each row retrieved.
CREATE OR REPLACE PROCEDURE upd_for_dept (
dept_in IN employee.department_id%TYPE
,newsal_in IN employee.salary%TYPE)
IS
CURSOR emp_cur IS
SELECT employee_id,salary,hire_date
FROM employee 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 2010 Feuerstein and Associates

The result? Simple and


elegant but
inefficient...why is this?

Page 4

Oracle PL/SQL Programming

Repetitive statement processing from PL/SQL


Oracle server
PL/SQL Runtime Engine
PL/SQL block
FOR rec IN emp_cur LOOP
UPDATE employee
SET salary = ...
WHERE employee_id =
rec.employee_id;
END LOOP;

Procedural
statement
executor

SQL Engine

SQL
statement
executor

Performance penalty
for many context
switches
Copyright 2010 Feuerstein and Associates

Page 5

Oracle PL/SQL Programming

Bulk Processing in PL/SQL


The goal is straightfoward: reduce the number of
context switches and you improver 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 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 2010 Feuerstein and Associates

Page 6

Oracle PL/SQL Programming

Bulk processing with FORALL


Oracle server
PL/SQL Runtime Engine
PL/SQL block
FORALL indx IN
list_of_emps.FIRST..
list_of_emps.LAST
UPDATE employee
SET salary = ...
WHERE employee_id =
list_of_emps(indx);
Update...
Update...
Update...
Update...
Update...
Update...

Copyright 2010 Feuerstein and Associates

Procedural
statement
executor

Fewer context switches,


same SQL behavior

SQL Engine

SQL
statement
executor
Update...
Update...
Update...
Update...
Update...
Update...

Page 7

Oracle PL/SQL Programming

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 statementlevel triggers only fire once per FORALL INSERT
statements.
Not for each INSERT statement passed to the SQL engine
from the FORALL statement.
Copyright 2010 Feuerstein and Associates

statement_trigger_and_forall.sql

Page 8

Oracle PL/SQL Programming

Conclusions
Look for loops that contain DML statements.
This means that the same statement is executing
repeatedly, with different bind variables.

Look for row-by-row query logic.


BULK COLLECT will allow you to retrieve multiple
rows with a single context switch.

But, of course, you need to get into the details


of both these features, so...

Copyright 2010 Feuerstein and Associates

Page 9

Oracle PL/SQL Programming

Next Steps
Download the demo.zip if you have not
already (www.ToadWorld.com/SF).
Run the sample code yourself to better
understand the features and techniques.

Watch the next lesson in this series:


Get Data Faster with BULK COLLECT

Copyright 2010 Feuerstein and Associates

Page 10

S-ar putea să vă placă și