Professional Documents
Culture Documents
Crotty PLSQL Bulk Collect Forall
Crotty PLSQL Bulk Collect Forall
AGENDA
Performance gains with Bulk Processing Array processing with BULK COLLECT and FORALL Oracle 10g FORALL improvements. Error handling.
Dr. Tim Hall Oracle ACE And Oracles ACE of the Year
Bulk Processing
Supercharge your PL/SQL code with BULK COLLECT and FORALL Working at a table-level instead of the row-level Simple and easy to use
PL/SQL Code
Consists of two types of statements Procedural (declare, begin, if, while, for ) SQL (select, insert, update, delete) Oracle has two engines to process that information PL/SQL Engine SQL Engine A Content Switch occurs each time the PL/SQL engine needs to execute a SQL statement Switches are fast but large loops can cause performance delays
Context Switches
Oracle Server Session PL/SQL Block PL/SQL Block PL/SQL Block SQL Data SQL Statement Executor SQL Engine PL/SQL Engine Procedural Statement Executor
PL/SQL Code
Consider this procedure code
CREATE OR REPLACE PROCEDURE update_price ( product_type_in IN product.product_type%TYPE, multiplier_in IN number(2,2) ) IS CURSOR products_cur IS SELECT product_id, product_price FROM products WHERE product_type = product_type_in; BEGIN FOR prod_rec IN products_cur LOOP UPDATE products SET product_price = product_price * multiplier_in WHERE product_id = prod_rec.product_id; END LOOP; END update_price;
PL/SQL Code
FOR prod_rec IN products_cur LOOP UPDATE products SET product_price = product_price * multiplier_in WHERE product_id = prod_rec.product_id; END LOOP;
For each iteration of this loop, there is going to be a conventional bind and a context switch! Overhead for these statements can be large But there is a solution Bulk Collections
SELECT or FETCH statements BULK COLLECT INTO Out-Bind binding RETURNING clause In-Bind binding FORALL INSERT, UPDATE, DELETE
Products Table
SQL> create table products ( 2 product_id number, 3 product_name varchar2(15), 4 effective_date date ); Table created. SQL> begin -- inserting 100000 records into the products table 1 for i in 1 .. 100000 loop 2 insert into products values (i, 'PROD'||to_char(i),sysdate-1); 3 end loop; 4 end; 5 / PL/SQL procedure successfully completed. SQL> commit; Commit complete.
Used in a SELECT statement Binds the result set of the query to a collection Much less communication between the PL/SQL and SQL engines All variables in the INTO clause must be a collection
BULK COLLECT
SET SERVEROUTPUT ON DECLARE TYPE prod_tab IS TABLE OF products%ROWTYPE; products_tab prod_tab := prod_tab(); start_time number; end_time number; BEGIN start_time := DBMS_UTILITY.get_time; FOR prod_rec in (SELECT * FROM products WHERE effective_date BETWEEN sysdate - 2 AND TRUNC(sysdate)) LOOP products_tab.extend; products_tab(products_tab.last) := prod_rec; END LOOP; end_time := DBMS_UTILITY.get_time; DBMS_OUTPUT.PUT_LINE(Conventional (||products_tab.count||): ||to_char(end_timestart_time)); Start_time := DBMS_UTILITY.get_time; SELECT * BULK COLLECT INTO products_tab FROM products WHERE effective_date BETWEEN sysdate - 2 AND TRUNC(sysdate); end_time := DBMS_UTILITY.get_time; DBMS_OUTPUT.PUT_LINE(Bulk Collect (||products_tab.count||): ||to_char(end_timestart_time)); END;
BULK COLLECT
SQL> / Conventional (100000): 40 Bulk Collect (100000): 27 PL/SQL procedure successfully completed.
BULK COLLECT
SQL> / Conventional (100000): 117 Bulk Collect (100000): 14 PL/SQL procedure successfully completed.
Result Set is limited to only 10000 rows more memory efficient! Will the processing be slower because of the LIMIT?
Yes, but only slightly. Still over 8 times better than conventional!
Use the LIMIT clause to manage memory requirements NO_DATA_FOUND will not be raised if no records are returned check contents to make sure records are retrieved
FORALL Use
Only a single DML statement is allowed per FORALL In 9i, the binding array must be sequentially filled Use SAVE EXCEPTIONS to continue past errors SQL%BULK_ROWCOUNT returns the number of affected rows
Use bulk bind techniques for recurring SQL statements in a PL/SQL loop. Bulk bind rules:
Can be used with any type of collection Collection subscripts cannot be expressions Collections should be densely filled If error, statement is rolled back. Prior successful DML statements are not rolled back.
Bulk Collects
Can be used with implicit or explicit cursors Collection is always filled sequentially starting with 1
FORALL driving array no longer needs to be processed in sequential order The INDICES OF clause is used to reference the row numbers defined in another array The VALUES OF clause is used to reference the values defined in another array