Reducing Loop Overhead for DML Statements and Queries (FORALL,BULK COLLECT)
Posted by: Edi Yanto on: May 31, 2007
PL/SQL sends SQL statements such as DML and queries to the SQL engine for execution, and SQL returns the result data to PL/SQL. You can minimize the performance overhead of this communication between PL/SQL and SQL by using the PL/SQL language features known collectively as bulk SQL. The FORALL statement sends INSERT, UPDATE, or DELETE statements in batches, rather than one at a time. The BULK COLLECT clause brings back batches of results from SQL. If the DML statement affects four or more database rows, the use of bulk SQL can improve performance considerably.
- Using the FORALL Statement, the keyword FORALL lets you run multiple DML statements very efficiently. It can only repeat a single DML statement, unlike a general-purpose FOR loop.
The following example loads some data into PL/SQL collections. Then it inserts the
collection elements into a database table twice: first using a FOR loop, then using a
FORALL statement. The FORALL version is much faster.

- Retrieving Query Results into Collections with the BULK COLLECT Clause, using the keywords BULK COLLECT with a query is a very efficient way to retrieve the result set. Instead of looping through each row, you store the results in one or more collections, in a single operation. You can use these keywords in the SELECT INTO and FETCH INTO statements, and the RETURNING INTO clause.With the BULK COLLECT clause, all the variables in the INTO list must be collections. The table columns can hold scalar or composite values, including object types. The following example loads two entire database columns into nested tables:

source: PL/SQL User’s Guide and Reference 10g Release 1 (10.1)
Recent Comments