Академический Документы
Профессиональный Документы
Культура Документы
(http://twitter.com/intent/tweet?status=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle+https%3A%2F%2Foracle-
base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-9i)
(http://www.facebook.com/sharer/sharer.php?
u=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-9i&title=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle)
(https://plus.google.com/share?url=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-9i)
(http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-
9i&title=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle&source=oracle-base.com)
(http://www.reddit.com/submit?url=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-
9i&title=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle)
Home (/) » Articles (/articles) » 9i (/articles/9i) » Here
Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
This article is an update of one written for Oracle 8i (Bulk Binds (/articles/8i/BulkBinds8i)) which includes new features available in Oracle 9i
Release 2 and beyond.
Introduction
BULK COLLECT
FORALL
SQL%BULK_ROWCOUNT
SAVE EXCEPTIONS and SQL%BULK_EXCEPTION
Bulk Binds and Triggers
Updates
Related articles.
Introduction
Oracle uses two engines to process PL/SQL code. All procedural code is handled by the PL/SQL engine while all SQL is handled by the SQL
statement executor, or SQL engine.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 1/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
There is an overhead associated with each context switch between the two engines. If PL/SQL code loops through a collection performing the
same DML operation for each item in the collection it is possible to reduce context switches by bulk binding the whole collection to the DML
statement in one operation.
In Oracle8i a collection must be defined for every column bound to the DML which can make the code rather long winded. Oracle9i allows us to
use Record structures during bulk operations so long as we don't reference individual columns of the collection. This restriction means that
updates and deletes which have to reference inividual columns of the collection in the where clause are still restricted to the collection-per-
column approach used in Oracle8i.
BULK COLLECT
Bulk binds can improve the performance when loading collections from a queries. The BULK COLLECT INTO construct binds the output of the
query to the collection. To test this create the following table.
The following code compares the time taken to populate a collection manually and using a bulk bind.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 2/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;
SELECT *
BULK COLLECT INTO l_tab
FROM bulk_collect_test;
SQL>
We can see the improvement associated with bulk operations to reduce context switches.
The select list must match the collections record definition exactly for this to be successful.
Remember that collections are held in memory, so doing a bulk collect from a large query could cause a considerable performance problem. In
actual fact you would rarely do a straight bulk collect in this manner. Instead you would limit the rows returned using the LIMIT clause and move
through the data processing smaller chunks. This gives you the benefits of bulk binds, without hogging all the server memory. The following code
shows how to chunk through the data in a large table.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 3/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;
l_tab t_bulk_collect_test_tab;
CURSOR c_data IS
SELECT *
FROM bulk_collect_test;
BEGIN
OPEN c_data;
LOOP
FETCH c_data
BULK COLLECT INTO l_tab LIMIT 10000;
EXIT WHEN l_tab.count = 0;
SQL>
So we can see that with a LIMIT 10000 we were able to break the data into chunks of 10,000 rows, reducing the memory footprint of our
application, while still taking advantage of bulk binds. The array size you pick will depend on the width of the rows you are returning and the
amount of memory you are happy to use.
From Oracle 10g onward, the optimizing PL/SQL compiler converts cursor FOR LOOP s into BULK COLLECT s with an array size of 100. The
following example compares the speed of a regular cursor FOR LOOP with BULK COLLECT s using varying array sizes.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 4/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_bulk_collect_test_tab IS TABLE OF bulk_collect_test%ROWTYPE;
l_tab t_bulk_collect_test_tab;
CURSOR c_data IS
SELECT *
FROM bulk_collect_test;
l_start NUMBER;
BEGIN
-- Time a regular cursor for loop.
l_start := DBMS_UTILITY.get_time;
DBMS_OUTPUT.put_line('Regular : ' ||
(DBMS_UTILITY.get_time - l_start));
OPEN c_data;
LOOP
FETCH c_data
BULK COLLECT INTO l_tab LIMIT 10;
EXIT WHEN l_tab.count = 0;
END LOOP;
CLOSE c_data;
DBMS_OUTPUT.put_line('LIMIT 10 : ' ||
(DBMS_UTILITY.get_time - l_start));
OPEN c_data;
LOOP
FETCH c_data
BULK COLLECT INTO l_tab LIMIT 100;
EXIT WHEN l_tab.count = 0;
END LOOP;
CLOSE c_data;
OPEN c_data;
LOOP
FETCH c_data
BULK COLLECT INTO l_tab LIMIT 1000;
EXIT WHEN l_tab.count = 0;
END LOOP;
CLOSE c_data;
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 5/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
/
Regular : 18
LIMIT 10 : 80
LIMIT 100: 15
LIMIT 1000: 10
SQL>
You can see from this example the performance of a regular FOR LOOP is comparable to a BULK COLLECT using an array size of 100. Does this
mean you can forget about BULK COLLECT in 10g onward? In my opinion no. I think it makes sense to have control of the array size. If you have
very small rows, you might want to increase the array size substantially. If you have very wide rows, 100 may be too large an array size.
FORALL
The FORALL syntax allows us to bind the contents of a collection to a single DML statement, allowing the DML to be run for each row in the
collection without requiring a context switch each time. To test bulk binds using records we first create a test table.
The following test compares the time taken to insert 10,000 rows using regular FOR..LOOP and a bulk bind.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 6/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_forall_test_tab IS TABLE OF forall_test%ROWTYPE;
l_tab(l_tab.last).id := i;
l_tab(l_tab.last).code := TO_CHAR(i);
l_tab(l_tab.last).description := 'Description: ' || TO_CHAR(i);
END LOOP;
COMMIT;
END;
/
Normal Inserts: 305
Bulk Inserts : 14
SQL>
The output clearly demonstrates the performance improvements you can expect to see when using bulk binds to remove the context switches
between the SQL and PL/SQL engines.
Since no columns are specified in the insert statement the record structure of the collection must match the table exactly.
Oracle9i Release 2 also allows updates using record definitions by using the ROW keyword. The following example uses the ROW keyword, when
doing a comparison of normal and bulk updates.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 7/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_id_tab IS TABLE OF forall_test.id%TYPE;
TYPE t_forall_test_tab IS TABLE OF forall_test%ROWTYPE;
l_id_tab(l_id_tab.last) := i;
l_tab(l_tab.last).id := i;
l_tab(l_tab.last).code := TO_CHAR(i);
l_tab(l_tab.last).description := 'Description: ' || TO_CHAR(i);
END LOOP;
l_start := DBMS_UTILITY.get_time;
COMMIT;
END;
/
Normal Updates : 235
Bulk Updates : 20
SQL>
The reference to the ID column within the WHERE clause of the first update would cause the bulk operation to fail, so the second update uses a
separate collection for the ID column. This restriction has been lifted in Oracle 11g, as documented here (/articles/11g/plsql-new-features-and-
enhancements-11gr1#forall_restriction_removed).
Once again, the output shows the performance improvements you can expect to see when using bulk binds.
SQL%BULK_ROWCOUNT
The SQL%BULK_ROWCOUNT cursor attribute gives granular information about the rows affected by each iteration of the FORALL statement. Every row
in the driving collection has a corresponding row in the SQL%BULK_ROWCOUNT cursor attribute.
The following code creates a test table as a copy of the ALL_USERS view. It then attempts to delete 5 rows from the table based on the contents
of a collection. It then loops through the SQL%BULK_ROWCOUNT cursor attribute looking at the number of rows affected by each delete.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 8/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_array_tab IS TABLE OF VARCHAR2(30);
l_array t_array_tab := t_array_tab('SCOTT', 'SYS',
'SYSTEM', 'DBSNMP', 'BANANA');
BEGIN
-- Perform bulk delete operation.
FORALL i IN l_array.first .. l_array.last
DELETE FROM bulk_rowcount_test
WHERE username = l_array(i);
SQL>
So we can see that no rows were deleted when we performed a delete for the username "BANANA".
The following code creates a collection with 100 rows, but sets the value of rows 50 and 51 to NULL. Since the above table does not allow nulls,
these rows will result in an exception. The SAVE EXCEPTIONS clause allows the bulk operation to continue past any exceptions, but if any
exceptions were raised in the whole operation, it will jump to the exception handler once the operation is complete. In this case, the exception
handler just loops through the SQL%BULK_EXCEPTION cursor attribute to see what errors occured.
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 9/11
6/5/2018 ORACLE-BASE - Bulk Binds (BULK COLLECT & FORALL) and Record Processing in Oracle
SET SERVEROUTPUT ON
DECLARE
TYPE t_tab IS TABLE OF exception_test%ROWTYPE;
ex_dml_errors EXCEPTION;
PRAGMA EXCEPTION_INIT(ex_dml_errors, -24381);
BEGIN
-- Fill the collection.
FOR i IN 1 .. 100 LOOP
l_tab.extend;
l_tab(l_tab.last).id := i;
END LOOP;
-- Cause a failure.
l_tab(50).id := NULL;
l_tab(51).id := NULL;
Number of failures: 2
Error: 1 Array Index: 50 Message: ORA-01400: cannot insert NULL into ()
Error: 2 Array Index: 51 Message: ORA-01400: cannot insert NULL into ()
SQL>
As expected the errors were trapped. If we query the table we can see that 98 rows were inserted correctly.
SELECT COUNT(*)
FROM exception_test;
COUNT(*)
----------
98
1 row selected.
SQL>
Updates
In Oracle 10g and above, the optimizing PL/SQL compiler rewrites conventional cursor for loops to use a BULK COLLECT with a LIMIT 100 ,
so code that previously didn't take advantage of bulk binds may now run faster.
Oracle 10g introduced support for handling sparse collections in FORALL statements (here
(/articles/10g/PlsqlEnhancements10g#forall_support_for_non_consecutive_indexes)).
The restriction on accessing individual columns of the collection with a FORALL has been removed in Oracle 11g (here (/articles/11g/plsql-
new-features-and-enhancements-11gr1#forall_restriction_removed)).
(http://twitter.com/intent/tweet?status=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle+https%3A%2F%2Foracle-
base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-9i)
(http://www.facebook.com/sharer/sharer.php?
u=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-9i&title=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle)
(https://plus.google.com/share?url=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-9i)
(http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-
9i&title=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle&source=oracle-
base.com) (http://www.reddit.com/submit?url=https%3A%2F%2Foracle-base.com%2Farticles%2F9i%2Fbulk-binds-and-record-processing-
9i&title=ORACLE-BASE%20-
%20Bulk%20Binds%20(BULK%20COLLECT%20%26%20FORALL)%20and%20Record%20Processing%20in%20Oracle)
Home (/) | Articles (/articles/articles) | Scripts (/dba/scripts) | Blog (/blog/) | Certification (/misc/ocp-certification) | Misc (/misc/miscellaneous) | About
(/misc/site-info)
https://oracle-base.com/articles/9i/bulk-binds-and-record-processing-9i 11/11