ORACLE insert performance comparison

ORACLE insert performance comparison
In Oracle database, the more frequent the submission, the better. On the contrary, batch submission can get better performance. This article will briefly show you the performance difference between row-by-row submission and batch submission in Oracle database. Finally, a method that can greatly change performance is given.

1. Create table t_ref and initialize 880992 pieces of data for later use. Experiment
sec@ora10g> create table t_ref as select * from all_objects;

sec@ora10g>insertinto t_ref select * from t;

220248 rows created.

sec@ora10g> /
sec@ora10g> /
sec@ora10g> /
sec@ora10g> /
sec@ora10g> /

440496 rows created.

2. Write a simple PL/SQL block to simulate row-by-row submission, and pay attention to the execution time.
Our goal is to insert all the data in the t_ref table into t.
sec@ora10g> set timing on

sec@ora10g> DECLARE
  2  BEGIN
  3    FOR cur IN (SELECT * FROM t_ref) LOOP
  4      INSERT INTO t VALUES cur;
  5      COMMIT;
  6    END LOOP;
  7  END;
  8  /

PL/SQL procedure successfully completed.

Elapsed: 00:03:12.77

In the case of line-by-line submission, the total time is 3 minutes and 12 seconds.

3. Let’s simulate batch submission.
sec@ora10g> truncate table t;

Table truncated.

sec@ora10g> DECLARE
  2    v_count NUMBER;
  3  BEGIN
  4    FOR cur IN (SELECT * FROM t_ref) LOOP
  5      INSERT INTO t VALUES cur;
  6      v_count := v_count + 1;
  7      IF v_count >= 100 THEN
  8        COMMIT;
  9      END IF;
 10    END LOOP;
 11    COMMIT;
 12  END;
 13  /

PL/SQL procedure successfully completed.

Elapsed: 00:01:27.69

At this time, the total time is 1 minute and 27 seconds, which is about half the time of the line-by-line submission method. It can be seen that Oracle should try to complete the work in batch submission.

4. Finally, we use Oracle's more advanced methods to complete the above tasks and experience the ultimate speed.
sec@ora10g> DECLARE
  2 CURSOR cur IS
  3 SELECT * FROM t_ref;
  4 TYPE rec IS TABLE OF t_ref%ROWTYPE;
  5 recs rec;
  6 BEGIN
  7 OPEN cur;
  8 WHILE (TRUE) LOOP
  9 FETCH cur BULK COLLECT
 10 INTO recs LIMIT 100;
 11 FORALL i IN 1 .. recs.COUNT
 12 INSERT INTO t VALUES recs (i);
 13 COMMIT;
 14 EXIT WHEN cur%NOTFOUND;
 15 END LOOP;
 16 CLOSE cur;
 17 END;
 18 /

Elapsed: 00:00:09.75

At this point, it took us less than 10 seconds to complete a task that once took several minutes.

5. Summary
In Oracle database, frequent COMMIT will cause a large amount of Redo Log physical I/O, which will greatly limit the performance. Therefore, in order to improve database performance, submit the database in batches as much as possible
 

Guess you like

Origin blog.csdn.net/caryxp/article/details/133542298