Sub topic 12
Batch Fetching with BULK COLLECT and LIMIT
This chapter assumes that you know basic SELECT statements, PL/SQL blocks, variables, loops, and exception handling. It also assumes that you understand explicit cursors and cursor FOR loops.
A cursor normally fetches one row at a time. That pattern is simple, but each fetch can require communication between the PL/SQL engine and the SQL engine. When a query returns many rows, fetching several rows at once can reduce this overhead.
BULK COLLECT is a PL/SQL feature that fetches multiple rows into a collection. A collection is an in-memory group of values. LIMIT controls the maximum number of rows fetched in one batch.
The basic pattern
Declare a collection whose element type matches the cursor rows. Open the cursor, fetch a batch, process the batch, and repeat until the collection is empty.
DECLARE
CURSOR c_emp IS
SELECT employee_id, last_name, salary
FROM employees
ORDER BY employee_id;
TYPE t_emp_tab IS TABLE OF c_emp%ROWTYPE;
l_emps t_emp_tab;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp BULK COLLECT INTO l_emps LIMIT 100;
EXIT WHEN l_emps.COUNT = 0;
FOR i IN 1 .. l_emps.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(
l_emps(i).employee_id || ': ' ||
l_emps(i).last_name || ', salary = ' ||
l_emps(i).salary
);
END LOOP;
END LOOP;
CLOSE c_emp;
END;
/The FETCH statement places up to 100 rows in l_emps. The collection indexes begin at 1, so the inner loop processes indexes from 1 through l_emps.COUNT.
On the final fetch, Oracle may return fewer than 100 rows. After all rows have been processed, the next fetch returns an empty collection. Therefore, test l_emps.COUNT = 0 immediately after the fetch.
| Technique | Rows handled at a time | Typical use |
|---|---|---|
| Ordinary `FETCH` | One | Simple row-by-row logic |
| `BULK COLLECT ... LIMIT` | A bounded batch | Large result sets |
| `SELECT ... BULK COLLECT INTO` | All matching rows | Small, known result sets |
Choosing a LIMIT value
A larger limit can reduce the number of fetch operations, but it also uses more session memory. A small limit uses less memory but may require more round trips. Values such as 100, 500, or 1,000 are common starting points. The best value depends on row size, processing time, available memory, and testing results.
LIMIT does not limit the total result set. It limits only one fetch. The loop still processes every row returned by the cursor.
When bulk fetching is appropriate
Use one SQL statement when the database can perform the entire operation, such as a set-based UPDATE. A cursor FOR loop is usually clearer when each row needs procedural logic. Use BULK COLLECT ... LIMIT when the result is large and you need procedural processing while keeping memory bounded.
Do not use an unlimited bulk collection for an unknown or very large result. Without LIMIT, Oracle attempts to place every matching row in memory at once.
Exercise
Why should a bulk-fetch loop test l_emps.COUNT = 0 after each FETCH?
Show answer
The final fetch can return an empty collection. Testing the count stops the loop before attempting to process nonexistent elements.