Tuesday, August 6, 2013

Oracle BULK COLLECT and FORALL statements



Oracle BULK COLLECT and FORALL :

BULK COLLECT:
SELECT statements that retrieve multiple rows with a single fetch, improving the speed of data retrieval.

1. Oracle BULK COLLECT INTO with queries:

In software development when dealing with database we may use to write functions, procedure or executable blocks in oracle pl/sql in certain circumstance where we need an alternate to cursors we can use bulk collect for selecting values from the table.

Below is a example for BULK COLLECT INTO FROM QUERY

Create a table temp_test using the below query.

create table temp_test (emp_name varchar2(50), emp_id number(10) primary key, emp_dept varchar2(25), emp_gender varchar2(1));

Assume below are the data in the table.
EMP_NAME
EMP_ID
EMP_DEPT
EMP_GENDER
test1
1
dept1
M
test2
2
dept2
M
test3
3
dept1
F
test4
4
dept2
F
test5
5
dept2
M
test5
6
dept2
F
test7
7
dept2
M
test8
8
dept2
M
test9
9
dept9
M





Code Example#1 
For BULK COLLECT INTO to select the data from the table 

DECLARE
   --Below is the variable that can hold the filed values of the table.
   TYPE insrt_temp IS TABLE OF temp_test%ROWTYPE;
   v_temp   insrt_temp;
BEGIN
   --query to select the values from temp_test table and store it in the type variable
   SELECT *
     BULK COLLECT INTO v_temp
     FROM temp_test;
   --below code will loop thru the selected values and prints the information
   FOR i IN 1 .. v_temp.COUNT
   LOOP
      DBMS_OUTPUT.put_line ('inside loop : ' || v_temp (i).emp_name);
   END LOOP;
END;
/ 
Output: (enable dbms_output if you are using tools like toad)

inside loop : test1
inside loop : test2
inside loop : test3
inside loop : test4
inside loop : test5
inside loop : test5
inside loop : test7
inside loop : test8
inside loop : test9

2. Oracle BULK COLLECT INTO with Cursors:

Below is the code sample with the same table information.
we try to fetch the data from the table via cursor and bulk collect


Code Example #2:


DECLARE
   --Below is the variable that can hold the filed values of the table.
   TYPE insrt_temp IS TABLE OF temp_test%ROWTYPE;

   v_temp   insrt_temp;
   --Cursor declaration
   CURSOR cur_temptest
   IS
      SELECT *
        FROM temp_test
       WHERE emp_id BETWEEN 1 AND 9;
BEGIN
   --query to select the values from temp_test table and store it in the type variable
   OPEN cur_temptest;

   FETCH cur_temptest BULK COLLECT INTO v_temp;

   CLOSE cur_temptest;

   --below code will loop thru the selected values and prints the information
   FOR i IN 1 .. v_temp.COUNT
   LOOP
      DBMS_OUTPUT.put_line ('employ loop : ' || v_temp (i).emp_name);
   END LOOP;
END;
/

Output of code example #2: 

employ loop : test1
employ loop : test2
employ loop : test3
employ loop : test4
employ loop : test5
employ loop : test5
employ loop : test7
employ loop : test8
employ loop : test9


3. Oracle FORALL :
FORALL: INSERTs, UPDATEs, and DELETEs that use collections to change multiple rows of data very quickly

Code Example #3: 
The below code sample will insert the values into temp_test table. after executing this pl/sql block. use the above BULK COLLECT INTO code to view the value.


DECLARE
   TYPE num_list_type IS TABLE OF NUMBER
      INDEX BY BINARY_INTEGER;
   v_num   num_list_type;
BEGIN
   v_num (1) := 13;
   v_num (2) := 14;
   v_num (3) := 15;
   v_num (4) := 16;

   FORALL i IN v_num.FIRST .. v_num.LAST
      INSERT INTO temp_test
           VALUES ('test' || v_num (i),
                   v_num (i),
                   'dept' || v_num (i),
                   'M');

END;
/
Output:
The code will get executed successfully.

On executing the Code Example #1 you get the below output after executing the code example #3:

Output after executing the example #3 and using the code example #1 or #2: 

nside loop : test1
inside loop : test2
inside loop : test3
inside loop : test4
inside loop : test5
inside loop : test5
inside loop : test7
inside loop : test8
inside loop : test9
inside loop : test13
inside loop : test14
inside loop : test15
inside loop : test16

4. Oracle FORALL with SQL%BULK_ROWCOUNT:

The %BULK_ROWCOUNT cursor attribute is a composite structure designed to use with FORALL statement.
The %BULK_ROWCOUNT acts like an associative array (index-by-table). Its ith element stores the number of rows processed by the ith execution of the INSERT statement. If the ith execution affects no rows, then %BULK_ROWCOUNT(i) returns zero.


Code Example #4:


DECLARE
   TYPE num_list_type IS TABLE OF NUMBER
      INDEX BY BINARY_INTEGER;

   v_num   num_list_type;
BEGIN
   v_num (1) := 13;
   v_num (2) := 14;
   v_num (3) := 15;
   v_num (4) := 16;

   FORALL i IN v_num.FIRST .. v_num.LAST
      INSERT INTO temp_test
           VALUES ('test' || v_num (i),
                   v_num (i),
                   'dept' || v_num (i),
                   'M');

   FOR i IN v_num.FIRST .. v_num.LAST
   LOOP
      DBMS_OUTPUT.put_line (
            'Value inserted '
         || SQL%BULK_ROWCOUNT (i)
         || ' row (s)'
         || ' within for loop '
         || i);
   END LOOP;
END;
/
Output:
Value inserted 1 row (s) within for loop 1
Value inserted 1 row (s) within for loop 2
Value inserted 1 row (s) within for loop 3
Value inserted 1 row (s) within for loop 4


The performance as always is a factor of data volume. For bulk collect and forall statements there is a detailed information related to performance in the below link
http://www.oracle.com/technetwork/issue-archive/2012/12-sep/o52plsql-1709862.html





No comments:

Post a Comment