Sub topic 15
When a Loop Should Be a Single SQL Statement
Assume you know basic SELECT and DML statements, anonymous PL/SQL blocks, variables, and cursor FOR loops. This chapter focuses on choosing between row-by-row PL/SQL and set-based SQL.
The row-by-row pattern
A cursor FOR loop is useful when each row needs procedural work. However, using a loop only to apply the same SQL change to every matching row adds unnecessary work.
This example gives every employee in department 50 a 10 percent raise:
BEGIN
FOR r IN (
SELECT employee_id
FROM employees
WHERE department_id = 50
) LOOP
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = r.employee_id;
END LOOP;
END;
/The loop fetches each employee, then sends a separate UPDATE for that employee. PL/SQL and SQL must repeatedly exchange control. This boundary crossing is often called a context switch. With many rows, the repeated switches can make the operation slower than necessary.
The set-based alternative
SQL is designed to operate on a set of rows at once. The equivalent statement is shorter and usually more efficient:
UPDATE employees
SET salary = salary * 1.10
WHERE department_id = 50;The WHERE clause identifies the set of rows. Oracle can then optimize the operation as one statement. It can choose an appropriate access path, process rows in groups, and reduce PL/SQL-to-SQL communication.
Put conditional logic in SQL when possible
A loop is still unnecessary when the change varies by row but the rule can be expressed with SQL expressions. A searched CASE expression can select a value for each row:
UPDATE employees
SET salary = salary *
CASE
WHEN performance_score >= 90 THEN 1.15
WHEN performance_score >= 75 THEN 1.10
ELSE 1.05
END
WHERE department_id = 50;Here, CASE returns a multiplier for each row. The statement remains set-based even though different rows receive different raises.
When a loop is appropriate
Use a loop when the operation genuinely requires procedural control, such as:
- Calling a PL/SQL procedure with different arguments for each row.
- Performing several dependent steps for each row.
- Applying rules that are difficult or inappropriate to express in SQL.
- Handling a row-specific error while allowing other rows to continue.
- Producing procedural output for each row.
Even then, consider whether a bulk technique such as FORALL or BULK COLLECT can reduce context switches. For straightforward filtering and modification, a single SQL statement is usually clearer.
A practical decision
First describe the operation as a sentence. If the sentence is “update all rows matching this condition,” write an UPDATE. If it is “for each row, call this procedure, inspect its result, and choose the next action,” a loop may be the correct construct. Measure large operations with representative data, because indexes, triggers, constraints, and transaction size also affect performance.
Exercise
Which is preferable for setting status = 'ARCHIVED' on every order older than one year: a cursor FOR loop or one UPDATE statement?
Show answer
Use one UPDATE statement with a WHERE clause. No row-specific procedural work is needed.