jpa的批量修改_jpa批量处理

jpa批量处理

我们知道jpa提供saveAll处理批量插入,代码如下

public List saveAll(Iterable entities){

Assert.notNull(entities, "Entities must not be null!");

List result = new ArrayList();

for (S entity : entities) {

result.add(save(entity));

}

return result;

}

复制代码

乍一看似乎是一个一个save

其实不然,查看源码(这里不细说),jpa每次save都会先添加到action queue,在flush的时候,再通过insert action构造statement的batch操作,然后到达一个批量的时候才perform,达到一个batch的时候会调用executeBatch()

也就是说,最终还是使用了jdbc statement的executeBatch的调用模式

同理 jdbcTemplate.batchUpdate同样最终也是使用了jdbc的batch

如何使用jpa批量处理

经查阅资料,了解到要使用jpa的批量处理需要在jpa配置:

spring.jpa.properties.hibernate.jdbc.batch_size=500

spring.jpa.properties.hibernate.order_inserts=true

-- 重点是配置rewriteBatchedStatements=true,开启jdbc的批量处理

spring.datasource.url = jdbc:mysql://192.168.240.4:8066/xxx?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true

复制代码

本着严谨的态度测试:

@Test(description = "批量插入测试saveAll测试 自增主键")// 9968ms 加入jpa配置 9929ms

public void testSaveAll(){

List entities = new ArrayList();

for (int i =0 ; i<1000; i++){

ShortUrlQueryRecordEntity entity = new ShortUrlQueryRecordEntity();

entity.setCreateTime(new Timestamp(System.currentTimeMillis()));

entity.setIp("119.75.217.109");

entity.setLongUrl("https://baijiahao.baidu.com/");

entities.add(entity);

//shortUrlQueryRecordRepository.save(shortUrlQueryRecordEntity);

}

shortUrlQueryRecordRepository.saveAll(entities);

}

复制代码

发现使用jpa批量配置和不使用的结果分别为9929ms、9968ms,基本无差距

那么是为什么呢?

经过大量的google发现

当主键生成策略为GenerationType.IDENTITY时,hibernate在jdbc级别禁用批量插入

复制代码

相关stackoverflow看这里:

使用uuid

这里我们使用uuid作为id,首先将配置修改为

c5af6c58ddb767ff4a7d2bf997bf0989.png

测试用例:

@Test(description = "批量插入测试saveAll测试 uuid")//

public void testCopySaveAll(){

List entities = new ArrayList<>();

for (int i =0 ; i<10000; i++){

ShortUrlQueryRecordCopyEntity entity = new ShortUrlQueryRecordCopyEntity();

entity.setCreateTime(new Timestamp(System.currentTimeMillis()));

entity.setIp("119.75.217.109");

entity.setLongUrl("https://baijiahao.baidu.com/");

entities.add(entity);

}

shortUrlQueryRecordCopyRepository.saveAll(entities);

}

复制代码

测试结果对比

测试结果加了jpa批量配置没加1000条1138ms13071ms

10000条2687ms88523ms

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值