Test Your PL/SQL Knowledge
This puzzler has come from Steven Feuerstein for the month of February 2008. So I thought to reproduce the puzzler with its answer:
The employees table in the production Oracle Database 10g Release 2 instance of the MassiveGlobalCorp company contains 2.5 million rows.
Below are three different blocks of code, each of which fetch all rows from this table and then "do stuff" with each fetched record. Which will run much slower than the other two, and why?
a.
DECLARE
CURSOR employees_cur IS SELECT * FROM employees;
BEGIN
FOR employee_rec IN employees_cur LOOP
do_stuff (employee_rec);
END LOOP ;
END;
b.
DECLARE
CURSOR employees_cur IS SELECT * FROM employees;
l_employee employees%ROWTYPE;
BEGIN
OPEN employees_cur;
LOOP
FETCH employees_cur INTO l_employee;
EXIT WHEN employees_cur%NOTFOUND;
do_stuff (l_employee);
END LOOP ;
CLOSE employees_cur;
END;
c.
DECLARE
CURSOR employees_cur IS SELECT * FROM employees;
TYPE employees_aat IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER;
l_employees employees_aat;
BEGIN
OPEN employees_cur;
LOOP
FETCH employees_cur
BULK COLLECT INTO l_employees LIMIT 100;
EXIT WHEN l_employees.COUNT () = 0;
FOR indx IN 1 .. l_employees.COUNT
LOOP
do_stuff (l_employees (indx));
END LOOP ;
END LOOP ;
CLOSE employees_cur;
END;
Scroll below to see the answer:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
(b) Is the slowest. That's because on Oracle 10g and higher, the PL/SQL optimizer will automatically rewrite cursor FOR loops so that they are executed in the same way as the BULK COLLECT query.
Sounds Interesting???
Steven Feuerstein is considered one of the world's leading experts on the Oracle PL/SQL language, having written ten books on PL/SQL all published by O'Reilly Media, including Oracle PL/SQL Programming and Oracle PL/SQL Best Practices (all from O'Reilly Media).
Steven has been developing software since 1980, spent five years with Oracle (1987-1992), and serves as a PL/SQL Evangelist to Quest Software.
He publishes puzzlers every month and here is where you can see all of them.