To quote and expand Quassnoi:
Most likely, these are not queries that work longer, but the overhead processes them in PL / SQL.
When processing query results in a PL / SQL script, context switching occurs. It requires the transfer of goods between Oracle processes and is fairly slow.
Like this code:
DECLARE cnt INTEGER := 0; CURSOR cr_main IS SELECT 1 AS id FROM dual CONNECT BY level <= 1000000; BEGIN FOR res IN cr_main LOOP cnt := cnt + res.id; END LOOP; DBMS_OUTPUT.put_line(cnt); END;
works on my machine for more than 3 seconds, and this one:
SELECT SUM(1) FROM dual CONNECT BY level <= 1000000
ends in just 0.5 seconds.
A context switch also occurs when you call PL / SQL from SQL, for example:
SELECT plsql_function(column) FROM mytable or when a trigger fires.
One way to solve the context switching problem is to use BULK COLLECT. If you are collecting many rows, using BULK COLLECT IN in a collection of some type can significantly speed up SQL in PL / SQL statements.
Brian
source share