使用Oracle SQL测试
实验环境
机器:win10虚机
配置:4c8G + 500G硬盘
持续写入
每次insert操作后,进行一次commit操作,性能低。实验用时:00:02:39.66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| drop table t purge; create table t(x int);
alter system flush shared_pool;
create or replace procedure proc as begin for i in 1 .. 1000000 loop insert into t values(i); commit; end loop; end; /
set timing on;
exec proc;
|
批量写入
对insert操作进行批量提交,性能相较于上者有所提升。实验用时:00:01:25.37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| drop table t purge; create table t(x int);
alter system flush shared_pool;
create or replace procedure proc as begin for i in 1 .. 1000000 loop insert into t values(i); end loop; commit; end; /
set timing on;
exec proc;
|
connect by方式写入
Oracle提供的层次查询(Hierarchical Queries)功能中的connect by子句可以快速插入连续数字。该方法会将数据写入数据缓冲区(data buffer)中,然后刷入磁盘中。实验用时:00:00:01.56
1 2 3 4 5 6 7 8 9 10 11
| drop table t purge; create table t(x int);
alter system flush shared_pool;
set timing on;
insert into t select rownum from dual connect by level<=1000000;
|
直接路径写入
该方式跳过了数据缓冲区(data buffer), 直接写进磁盘中,这种方式称之为直接路径读写方式。实验用时:00:00:00.78
1 2 3 4 5 6 7 8 9 10
| drop table t purge;
alter system flush shared_pool;
set timing on;
create table t as select rownum x from dual connect by level<=1000000;
|
并行模式直接写入
并行加直接路径,而且是不写日志。实验用时:00:00:00.67
1 2 3 4 5 6 7 8 9 10
| drop table t purge;
alter system flush shared_pool;
set timing on;
create table t nologging parallel 4 as select rownum x from dual connect by level<=1000000;
|
大对象存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| create table t ( tid int primary key, tname varchar2(20), tcontent blob );
create sequence sequ_t;
create or replace directory TEST_DIR as 'C:\Users\lenovo\Desktop';
declare tempimg blob; tempdir bfile := bfilename('TEST_DIR','test.png'); begin insert into t values (sequ_t.nextval,'test.png',empty_blob()) returning tcontent into tempimg;
dbms_lob.fileopen(tempdir); dbms_lob.loadfromfile(tempimg,tempdir,dbms_lob.getlength(tempdir)); dbms_lob.fileclose(tempdir); commit; end ; /
declare l_file utl_file.file_type; l_buffer raw(32767); l_amount binary_integer := 3276; l_pos int :=1; l_blob blob; l_blob_len int; begin select tcontent into l_blob from t where tname='test.png';
l_blob_len := dbms_lob.getlength(l_blob);
l_file := utl_file.fopen('TEST_DIR','t.png','wb');
while l_pos<l_blob_len loop dbms_lob.read(l_blob,l_amount,l_pos,l_buffer); utl_file.put_raw(l_file,l_buffer,true); l_pos := l_pos + l_amount; end loop; utl_file.fclose(l_file); end; /
|
参考文档