Oracle批处理insert性能比较

本文对比了两种批量插入数据到Oracle数据库的方法:直接使用INSERT语句与使用UNION ALL语句。通过本地单元测试评估了不同批量大小下的性能表现,结果显示使用UNION ALL方式显著提升了批量插入的速度。
摘要由CSDN通过智能技术生成

github地址

建表语句

create table TESTBATCH
(
  userid      NUMBER not null,
  username    VARCHAR2(4000),
  age         NUMBER,
  phone       NUMBER,
  currencyone VARCHAR2(4000),
  countone    NUMBER,
  currencytwo VARCHAR2(4000),
  counttwo    NUMBER,
  createtime  DATE,
  updatetime  DATE
);
alter table TESTBATCH
  add primary key (USERID);

插入测试数据

  • 准备了20万数据,只测试了1万数据的批量insert
--1、声明ylm_manager
create or replace procedure ylm_manager(startIndex in number,
                                        endIndex   in number) as
begin
  DBMS_OUTPUT.ENABLE(buffer_size => null);
  for p_userid in startIndex .. endIndex loop
    insert into testbatch
      (userid, username, age, phone,currencyone,countone,currencytwo,counttwo,createtime,updatetime)
    values
      (p_userid, '四年级学生森下下士'||p_userid, 18, 110120'科威特第纳尔',987654321,'津巴布韦',123456789,sysdate,sysdate);
    DBMS_OUTPUT.PUT_LINE('userid is ' || p_userid);
  end loop;
end ylm_manager;
--2、调用
call  ylm_manager(100000,200000);

实体类TestBatch.java

public class TestBatch {
    private Integer userId;
    private String userName;
    private Integer age;
    private Long phone;
    private String currencyOne;
    private Long countOne;
    private String currencyTwo;
    private Long countTwo;
    private String createTime;
    private String updateTime;
    
    ...get and set...

mapper

<insert id="batchInsertOne" parameterType="com.diy.sigmund.mybatisoracle.entity.TestBatch">
    <foreach collection="list" item="item" index="index" open="begin" close="end;">
      INSERT INTO testbatch
      (userid, username, age, phone,currencyone,countone,currencytwo,counttwo,createtime,updatetime)
      VALUES
      (#{item.userId,jdbcType=NUMERIC}, #{item.userName,jdbcType=VARCHAR},
      #{item.age,jdbcType=NUMERIC},
      #{item.phone,jdbcType=NUMERIC},
      #{item.currencyOne,jdbcType=VARCHAR},
      #{item.countOne,jdbcType=NUMERIC},
      #{item.currencyTwo,jdbcType=VARCHAR},
      #{item.countTwo,jdbcType=NUMERIC},
      to_date(#{item.createTime,jdbcType=VARCHAR},'YYYY-MM-DD HH24:MI:SS'),
      sysdate);
    </foreach>
  </insert>

  <insert id="batchInsertTwo" parameterType="com.diy.sigmund.mybatisoracle.entity.TestBatch">
    begin
    INSERT INTO testbatch
    (userid, username, age, phone,currencyone,countone,currencytwo,counttwo,createtime,updatetime)
    <foreach collection="list" item="item" separator="union all">
      SELECT #{item.userId,jdbcType=NUMERIC}, #{item.userName,jdbcType=VARCHAR},
      #{item.age,jdbcType=NUMERIC},
      #{item.phone,jdbcType=NUMERIC},
      #{item.currencyOne,jdbcType=VARCHAR},
      #{item.countOne,jdbcType=NUMERIC},
      #{item.currencyTwo,jdbcType=VARCHAR},
      #{item.countTwo,jdbcType=NUMERIC},
      to_date(#{item.createTime,jdbcType=VARCHAR},'YYYY-MM-DD HH24:MI:SS'),
      sysdate
      FROM dual
    </foreach>
    ;end;
  </insert>
  • 本地单元测试,重启了电脑,8g内存,启动了虚拟机,启动了oracle

耗时batchInsertOne升序

批量处理条数时间ms时间ms时间ms时间ms时间ms时间ms时间ms时间ms时间ms时间ms平均时间
40026341337127980878882478480013157721134.1
60024171288139981879285116327677688271155.9
7002474134421659728228007898457707551173.6
50025061312184279479278580413198567391174.9
3002277245314068078218018367959907341192
8536691744876944901898119788186910481302.7
8003308208611721098802787103577478711861303.5
20041541497146482197382084180413507871351.1
7031011732152391893899589590219188971381.9
10040282277169510268568959199577978801433
50456217559741073963963985118893910781448
900310719291098984804794799773110338541524.5
3046412666107710591198108610351034129111461623.3
1067665364458814981621147617022047144016962819.8
557986214814826182348210122072094197270974059.7

耗时batchInsertTwo升序

批量处理条数时间ms时间ms时间ms时间ms时间ms时间ms时间ms时间ms时间ms时间ms平均时间
900487459468454534489478471471476478.7
300526469607505486488495480480497503.3
200572507498874499508502506505513548.4
800481465608543468481475729465859557.4
6005247494614665041274471461631453599.4
100962556554550559725580563551574617.4
855865695765745765921022573574576621.8
7006164584701688824462470465485437637.5
50729653665721657653659659647640668.3
50048846747456849110081740457472594675.9
40050310008224754865544794721894461714.6
706716715958346005956142447595676829.8
308157618667317527567187511532742842.4
1018031196117611501186114211471147115011841228.1
521612215176217241928174817781732175417511855.3

结论

  1. 使用union all的方式要明显快于普通insert into的方式

  2. 900的批量处理条数最快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值