Académique Documents
Professionnel Documents
Culture Documents
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 PL/SQL Engine Procedural Statement Executor Data SQL Statement Executor SQL Engine
SQL
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
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_time-start_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_time-start_time)); END;
BULK COLLECT
SQL> / Conventional (100000): 40 Bulk Collect (100000): 27 PL/SQL procedure successfully completed.
BULK COLLECT Explicit Cursor- Fetch CursorDECLARE TYPE prod_tab IS TABLE OF products%ROWTYPE; products_tab prod_tab := prod_tab(); start_time number; end_time number; CURSOR products_data IS SELECT * FROM products; BEGIN start_time := DBMS_UTILITY.get_time; OPEN products_data; LOOP products_tab.extend; FETCH products_data INTO products_tab(products_tab.last); IF products_data%NOTFOUND THEN products_tab.delete(products_tab.last); EXIT; END IF; END LOOP; CLOSE products_data; end_time := DBMS_UTILITY.get_time; DBMS_OUTPUT.PUT_LINE(Conventional (||products_tab.count||): ||to_char(end_time-start_time)); Start_time := DBMS_UTILITY.get_time; OPEN products_data; FETCH products_data BULK COLLECT INTO products_tab; CLOSE products_data; end_time := DBMS_UTILITY.get_time; DBMS_OUTPUT.PUT_LINE(Bulk Collect (||products_tab.count||): ||to_char(end_time-start_time)); END;
BULK COLLECT
SQL> / Conventional (100000): 117 Bulk Collect (100000): 14 PL/SQL procedure successfully completed.
BULK COLLECT Explicit Cursor- LIMIT CursorDECLARE TYPE prod_tab IS TABLE OF products%ROWTYPE; products_tab prod_tab := prod_tab(); start_time number; end_time number; CURSOR products_data IS SELECT * FROM products; BEGIN Start_time := DBMS_UTILITY.get_time; OPEN products_data; LOOP FETCH products_data BULK COLLECT INTO products_tab LIMIT 10000; EXIT WHEN products_data%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Processed '||to_char(products_tab.count)||' rows'); END LOOP; CLOSE products_data; end_time := DBMS_UTILITY.get_time; DBMS_OUTPUT.PUT_LINE('Bulk Collect: ||to_char(end_time-start_time)); end;
Result Set is limited to only 10000 rows more memory efficient! Will the processing be slower because of the LIMIT?
BULK COLLECT Explicit Cursor- LIMIT CursorSQL> / Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Processed 10000 rows Bulk Collect: 15 PL/SQL procedure successfully completed.
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
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
SQL