Friday 26 August 2011

What is the difference between INSERT-SELECT and FORALL-INSERT

Question:
What is the difference between INSERT-SELECT and FORALL-INSERT, aside from having to load the latter to the collection before issuing INSERT?
Answer:

Tere, INSERT-SELECT and FORALL-INSERT both offer very high-speed methods for inserting multiple rows into a table.
INSERT-SELECT is a SQL implementation that transfers data from one table to another. You can, however, call a PL/SQL function in the column list of the SELECT statement.
FORALL-INSERT is a PL/SQL extension to the standard SQL INSERT. It requires, as you note correctly in your question, the use of a collection to cache the data and then push it very efficiently into the table of your choice.
Which you choose to use depends on your application requirements, but I suggest you follow these guidelines:
1. If you can perform the data transfer within an INSERT-SELECT format, it is likely to be the fastest approach. For example, in some of my own tests, I was able to execute an INSERT-SELECT of approximately 100,000 rows in 0.38 seconds, while it took FORALL-INSERT 1.02 seconds to insert the same number of rows.
2. If you need to rely on PL/SQL processing to apply complex transformations to your data before inserting it into the new table, you will probably want to shift to the FORALL approach. Manipulation of the contents of a collection is very efficient, as is the FORALL mechanism. (One second for 100,000 rows is, after all, very fast!)

No comments:

Post a Comment