Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query performance difference pl/sql forall insert and plain SQL insert

We have been using temporary table to store intermediate results in pl/sql Stored procedure. Could anyone tell if there is a performance difference between doing bulk collect insert through pl/sql and a plain SQL insert.

Insert into [Table name] [Select query Returning huge amount of data]

or

Cursor for [Select query returning huge amount of data]

open cursor

fetch cursor bulk collect into collection

Use FORALL to perform insert

Which of the above 2 options is better to insert huge amount of temporary data?.

like image 762
Prakash Avatar asked Apr 20 '10 05:04

Prakash


2 Answers

Some experimental data for your problem (Oracle 9.2)

bulk collect

DECLARE 
  TYPE t_number_table IS TABLE OF NUMBER;
  v_tab t_number_table;
BEGIN
  SELECT ROWNUM
  BULK COLLECT INTO v_tab
  FROM dual
  CONNECT BY LEVEL < 100000;

  FORALL i IN 1..v_tab.COUNT
    INSERT INTO test VALUES (v_tab(i));
END;
/
-- 2.6 sec

insert

-- test table 
CREATE global TEMPORARY TABLE test (id number)
ON COMMIT preserve ROWS;

BEGIN
  INSERT INTO test
  SELECT ROWNUM FROM dual
  CONNECT BY LEVEL < 100000;
END;
/
-- 1.4 sec

direct path insert http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c21dlins.htm

BEGIN
  INSERT /*+ append */ INTO test
  SELECT ROWNUM FROM dual
  CONNECT BY LEVEL < 100000;
END;
/
-- 1.2 sec
like image 165
jva Avatar answered Oct 04 '22 21:10

jva


Insert into select must certainly be faster. Skips the overhead of storing the data in a collection first.

like image 41
Rene Avatar answered Oct 04 '22 22:10

Rene