[size=small]oracle插入大量数据
1.生成大量的随机测试数据
create table mytest as
select rownum as id,
to_char(sysdate + rownum/24/3600, 'yyyy-mm-dd hh24:mi:ss') as ins_time,
trunc(dbms_random.value(0, 100)) as random_id,
dbms_random.string('x', 20) random_string
from dual
connect by level <= 10000;
SQL> col RANDOM_STRING for a30
SQL> select * from mytest where ROWNUM <5;
ID INS_TIME RANDOM_ID RANDOM_STRING
---------- ------------------- ---------- ------------------------------
1 2015-12-23 16:37:49 31 DFJXXVY82FH5FZBLRQGG
2 2015-12-23 16:37:50 54 PJXQG3QNYI3K7NXTCQS2
3 2015-12-23 16:37:51 86 B3RWA8H1VH3683WMNLUH
4 2015-12-23 16:37:52 34 MN438B2ETLSJBLT57IY7
1、利用Oracle特有的“connect by”树形连接语法生成测试记录,“level <= 10”表示要生成10记录;
2、利用rownum虚拟列生成递增的整数数据;
3、利用sysdate函数加一些简单运算来生成日期数据,本例中是每条记录的时间加1秒;
4、利用dbms_random.value函数生成随机的数值型数据,本例中是生成0到100之间的随机整数;
5、利用dbms_random.string函数生成随机的字符型数据,本例中是生成长度为20的随机字符串,字符串中可以包括字符或数字。
create table ta as
select rownum as id, dbms_random.string ('x', 20) name from dual connect by level <= 100000;
SQL> col name for a30
SQL>select * from ta where rownum<10
ID NAME
---------- ------------------------------
1 DLM4OQ0RDZ6HNHKTJTDG
2 BA1TCAPE5WT1O03TKKKE
3 4A4W1JBJHFCOUQIQWCIU
4 MTT1Z9B8247N3HCDTDDU
5 Q22ZNBSLCLN7EEG4JF44
6 NJ7KXP1N3VKNELT95LMC
7 VYFGIORNXHHHWKFOR4NQ
8 7C8R7HGKZ3GEEKK67Z7V
9 6I50G0C6PH0DYV32GW0I
9 rows selected.
2.如何快速插入:
SQL> set timing on
SQL> create table tb(id int,name varchar2(20));
SQL> insert into tb select * from ta;
100000 rows created.
Elapsed: 00:00:00.86
使用hint缩短插入时间
SQL> insert /*+Append*/ into tb select * from ta;
100000 rows created.
Elapsed: 00:00:00.13
3.如何批量提交:
1)模拟逐行提交效率(ta表有10w行数据,tb表为空)
DECLARE
BEGIN
FOR cur IN (SELECT * FROM ta) LOOP
INSERT INTO tb VALUES cur;
COMMIT;
END LOOP;
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:16.38 --16s的时间
2)模拟批量提交效率(ta表有10w行数据,tb表为空)
truncate table tb;
DECLARE
v_count NUMBER;
BEGIN
FOR cur IN (SELECT * FROM ta) LOOP
INSERT INTO tb VALUES cur;
v_count := v_count + 1;
IF v_count >= 1000 THEN
COMMIT;
END IF;
END LOOP;
COMMIT;
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:07.89 ---7s的时间
数据量小的时候,差别可能不大,但数据量大的时候,就能明显看到区别了。
3)另一种高速提交的方法(使用游标)
DECLARE
CURSOR cur IS
SELECT * FROM ta;
TYPE rec IS TABLE OF ta%ROWTYPE;
recs rec;
BEGIN
OPEN cur;
WHILE (TRUE) LOOP
FETCH cur BULK COLLECT
INTO recs LIMIT 100;
FORALL i IN 1 .. recs.COUNT
INSERT INTO tb VALUES recs (i);
COMMIT;
EXIT WHEN cur%NOTFOUND;
END LOOP;
CLOSE cur;
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.62 ---1s的时间都不要
说明:频繁的COMMIT会引起大量Redo Log的物理I/O,会极大的限制数据库的性能。因此,为提高数据库性能,尽可能的批量提交。[/size]
1.生成大量的随机测试数据
create table mytest as
select rownum as id,
to_char(sysdate + rownum/24/3600, 'yyyy-mm-dd hh24:mi:ss') as ins_time,
trunc(dbms_random.value(0, 100)) as random_id,
dbms_random.string('x', 20) random_string
from dual
connect by level <= 10000;
SQL> col RANDOM_STRING for a30
SQL> select * from mytest where ROWNUM <5;
ID INS_TIME RANDOM_ID RANDOM_STRING
---------- ------------------- ---------- ------------------------------
1 2015-12-23 16:37:49 31 DFJXXVY82FH5FZBLRQGG
2 2015-12-23 16:37:50 54 PJXQG3QNYI3K7NXTCQS2
3 2015-12-23 16:37:51 86 B3RWA8H1VH3683WMNLUH
4 2015-12-23 16:37:52 34 MN438B2ETLSJBLT57IY7
1、利用Oracle特有的“connect by”树形连接语法生成测试记录,“level <= 10”表示要生成10记录;
2、利用rownum虚拟列生成递增的整数数据;
3、利用sysdate函数加一些简单运算来生成日期数据,本例中是每条记录的时间加1秒;
4、利用dbms_random.value函数生成随机的数值型数据,本例中是生成0到100之间的随机整数;
5、利用dbms_random.string函数生成随机的字符型数据,本例中是生成长度为20的随机字符串,字符串中可以包括字符或数字。
create table ta as
select rownum as id, dbms_random.string ('x', 20) name from dual connect by level <= 100000;
SQL> col name for a30
SQL>select * from ta where rownum<10
ID NAME
---------- ------------------------------
1 DLM4OQ0RDZ6HNHKTJTDG
2 BA1TCAPE5WT1O03TKKKE
3 4A4W1JBJHFCOUQIQWCIU
4 MTT1Z9B8247N3HCDTDDU
5 Q22ZNBSLCLN7EEG4JF44
6 NJ7KXP1N3VKNELT95LMC
7 VYFGIORNXHHHWKFOR4NQ
8 7C8R7HGKZ3GEEKK67Z7V
9 6I50G0C6PH0DYV32GW0I
9 rows selected.
2.如何快速插入:
SQL> set timing on
SQL> create table tb(id int,name varchar2(20));
SQL> insert into tb select * from ta;
100000 rows created.
Elapsed: 00:00:00.86
使用hint缩短插入时间
SQL> insert /*+Append*/ into tb select * from ta;
100000 rows created.
Elapsed: 00:00:00.13
3.如何批量提交:
1)模拟逐行提交效率(ta表有10w行数据,tb表为空)
DECLARE
BEGIN
FOR cur IN (SELECT * FROM ta) LOOP
INSERT INTO tb VALUES cur;
COMMIT;
END LOOP;
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:16.38 --16s的时间
2)模拟批量提交效率(ta表有10w行数据,tb表为空)
truncate table tb;
DECLARE
v_count NUMBER;
BEGIN
FOR cur IN (SELECT * FROM ta) LOOP
INSERT INTO tb VALUES cur;
v_count := v_count + 1;
IF v_count >= 1000 THEN
COMMIT;
END IF;
END LOOP;
COMMIT;
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:07.89 ---7s的时间
数据量小的时候,差别可能不大,但数据量大的时候,就能明显看到区别了。
3)另一种高速提交的方法(使用游标)
DECLARE
CURSOR cur IS
SELECT * FROM ta;
TYPE rec IS TABLE OF ta%ROWTYPE;
recs rec;
BEGIN
OPEN cur;
WHILE (TRUE) LOOP
FETCH cur BULK COLLECT
INTO recs LIMIT 100;
FORALL i IN 1 .. recs.COUNT
INSERT INTO tb VALUES recs (i);
COMMIT;
EXIT WHEN cur%NOTFOUND;
END LOOP;
CLOSE cur;
END;
/
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.62 ---1s的时间都不要
说明:频繁的COMMIT会引起大量Redo Log的物理I/O,会极大的限制数据库的性能。因此,为提高数据库性能,尽可能的批量提交。[/size]