这是我在我的几年前买的一个烂PC 上执行的:
SQL> create table t ( a number);
Table created
SQL>
SQL> create or replace procedure sp_ins_test
2 as
3 type t_target is table of t%rowtype;
4 l_target t_target;
5 l_limit number default 3000;
6 c_commit constant smallint :=100000;
7 l_ins_cnt number :=0;
8 l_start_time number default 0;
9 l_end_time number default 0;
10
11 cursor cur is select level rn from dual connect by level <= 1e6;
12 begin
13 execute immediate 'truncate table t';
14 l_start_time := dbms_utility.get_time;
15 open cur;
16 loop
17 fetch cur bulk collect into l_target limit l_limit;
18 forall i in l_target.first .. l_target.last
19 insert /*+append*/ into t values l_target(i);
20 l_ins_cnt := l_ins_cnt + l_target.count;
21 if l_ins_cnt >= c_commit then
22 commit;
23 l_ins_cnt :=0;
24 end if;
25 exit when cur%notfound;
26 end loop;
27 commit work;
28 close cur;
29 l_end_time := dbms_utility.get_time;
30 dbms_output.put_line('Elipse time :'||(l_end_time - l_start_time)/100);
31 end sp_ins_test;
32
33 /
Procedure created
SQL> set serveroutput on ;
SQL> exec sp_ins_test;
Elipse time :1.44
PL/SQL procedure successfully completed
SQL> select count(1),sum(a) from t;
COUNT(1) SUM(A)
---------- ----------
1000000 5000005000
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL>