采用服务器PL/SQL 方式插入10万条记录的测试结果如下:
注:t_e1与t_employee同样的表结构
执行方式 |
说明 |
运行时间(单位:秒) |
pl/sql insert(脚本1) |
普通insert |
3.203 |
pl/sql forall insert(脚本2) |
从一个表BULK COLLECT INTO到目标表 |
0.578 |
insert into select *(脚本3) |
使用insert into select方式插入 |
0.156 |
insert /*+ append*/ into select *(脚本4) |
加append hint的插入 |
0.234 |
从测试结果分析,采用insert into select 的方式最快,只要0.156s,根据数据量统计,平均每行大小为134字节,总共插入数据量为134*100000=12.78MB,可得每秒约插入81MB的数据,基本上达到了硬盘的上限。
而采用append hint插入反而更慢,从同事讨论结果得到,采用append的insert会采用direct-path插入,因此数据会直接写入数据文件,所以消耗的时间更多。
--------------------------------------------脚本1--------------
declare i integer; begin for i in 1 .. 100000 loop insert into t_employee (id, name, birthday, address, email, mobilephone, telephone, identity_card, weight, height) values ( seq_t_employee_id.nextval, '张三' || i, sysdate - i, '上海市南京东路11号203室' || i, 'abcd' || i || '@gmail.com', '138' || trim(to_char(i, '00000000')), '021-' || trim(to_char(i, '00000000')), '3504561980' || trim(to_char(i, '00000000')), 64, 1.72); end loop; commit; end; |
--------------------------------------------脚本2--------------
DECLARE TYPE table_t_employee IS TABLE OF t_employee%ROWTYPE; v_table table_t_employee; BEGIN SELECT * BULK COLLECT INTO v_table FROM t_employee; FORALL idx IN 1 .. v_table.COUNT INSERT INTO t_e1 VALUES v_table (idx); COMMIT; END; |
--------------------------------------------脚本3--------------
insert into t_e1 select * from t_employee |
--------------------------------------------脚本4--------------
insert /*+ append*/ into t_e1 select * from t_employee |