做电力 公司网站,那间公司做网站好,开发公司企业简介,wordpress 后台 404总结最优的两种方法#xff1a;
方法1#xff1a; 使用了【MyBatis-plus】saveBatch 但是数据入库效率依旧很慢#xff0c;那可能是是因为JDBC没有配置#xff0c;saveBatch 批量写入并没有生效哦#xff01;#xff01;#xff01; 详细配置如下#xff1a;批量数据入…总结最优的两种方法
方法1 使用了【MyBatis-plus】saveBatch 但是数据入库效率依旧很慢那可能是是因为JDBC没有配置saveBatch 批量写入并没有生效哦 详细配置如下批量数据入库rewriteBatchedStatementstrue # 数据源master:driver-class-name: org.postgresql.Driverurl: jdbc:postgresql://127.0.0.1:5444/mxpt_business_databases?useUnicodetruecharacterEncodingutf8currentSchemapublicstringtypeunspecifiedrewriteBatchedStatementstrueusername: postgrespassword: postgresschema: public方法2 使用【MyBatis】进行数据的批量入库拼接sql语句每1000条数据入库一次。
Overridepublic String insertBoundValueListToDatabase(ListResourceCalcSceneBoundValue list){//1.先删除原有场次和工程的数据再进行导入ResourceCalcSceneBoundValue gongkuangValue list.get(0);Long scprodId gongkuangValue.getScprodId();Long gongkuangId gongkuangValue.getBoundId();ListResourceCalcSceneBoundValue listValue resourceCalcSceneBoundValueMapper.selectResourceCalcSceneBoundValueList(gongkuangValue);if(listValue ! null listValue.size() 0){resourceCalcSceneBoundValueMapper.deleteBoundValueByScprodIdAndBoundId(scprodId, gongkuangId);}//2.将结果插入到数据库中if (list.size() 0) {//条数为1if(list.size() 1){resourceCalcSceneBoundValueMapper.insertResourceCalcSceneBoundValueList(list.subList(0, 1));}//由于数据库对于插入字段的限制在这里对批量插入的数据进行分批处理int batchCount 120;//每批commit的个数int batchLastIndex batchCount - 1;// 每批最后一个的下标for (int index 0; index list.size() - 1; ) {if (batchLastIndex list.size() - 1) {batchLastIndex list.size() - 1;resourceCalcSceneBoundValueMapper.insertResourceCalcSceneBoundValueList(list.subList(index, batchLastIndex 1));break;// 数据插入完毕,退出循环} else {resourceCalcSceneBoundValueMapper.insertResourceCalcSceneBoundValueList(list.subList(index, batchLastIndex 1));index batchLastIndex 1;// 设置下一批下标batchLastIndex index (batchCount - 1);}}return 边界过程数据入库成功! 条数为list.size()条。 ;}return 数据条数为0。;}xml代码
insert idinsertResourceCalcSceneBoundValueList parameterTypejava.util.List useGeneratedKeysfalseINSERT INTO resource_calc_scene_bound_value(scprod_id, bound_id, tm, flow, water, kurong, inq, stcd, remark, jp, kaidu, kgnum)VALUESforeach collectionlist itemitem indexindex separator,(#{item.scprodId,jdbcTypeINTEGER},#{item.boundId,jdbcTypeINTEGER},#{item.tm,jdbcTypeTIMESTAMP},#{item.flow,jdbcTypeNUMERIC},#{item.water,jdbcTypeNUMERIC},#{item.kurong,jdbcTypeNUMERIC},#{item.inq,jdbcTypeNUMERIC},#{item.stcd,jdbcTypeVARCHAR},#{item.remark,jdbcTypeVARCHAR},#{item.jp,jdbcTypeNUMERIC},#{item.kaidu,jdbcTypeNUMERIC},#{item.kgnum,jdbcTypeINTEGER})/foreach/insert参考博客
https://www.cnblogs.com/natee/p/17428877.html 大神总结的超级详细 一起学习 发现接口处理速度慢的有点超出预期感觉很奇怪后面定位发现是数据库批量保存这块很慢。 这个项目用的是 mybatis-plus批量保存直接用的是 mybatis-plus 提供的 saveBatch。 我点进去看了下源码感觉有点不太对劲 继续追踪了下从这个代码来看确实是 for 循环一条一条执行了 sqlSession.insert下面的 consumer 执行的就是上面的 sqlSession.insert 然后累计一定数量后一批 flush。从这点来看这个 saveBach 的性能肯定比直接一条一条 insert 快。
1、1000条数据一条一条插入
Test
void MybatisPlusSaveOne() {SqlSession sqlSession sqlSessionFactory.openSession();try {StopWatch stopWatch new StopWatch();stopWatch.start(mybatis plus save one);for (int i 0; i 1000; i) {OpenTest openTest new OpenTest();openTest.setA(a i);openTest.setB(b i);openTest.setC(c i);openTest.setD(d i);openTest.setE(e i);openTest.setF(f i);openTest.setG(g i);openTest.setH(h i);openTest.setI(i i);openTest.setJ(j i);openTest.setK(k i);//一条一条插入openTestService.save(openTest);}sqlSession.commit();stopWatch.stop();log.info(mybatis plus save one stopWatch.getTotalTimeMillis());} finally {sqlSession.close();}
}可以看到执行一批 1000 条数的批量保存耗费的时间是 121011 毫秒。
2、1000条数据用 mybatis-plus 自带的 saveBatch 插入
Test
void MybatisPlusSaveBatch() {SqlSession sqlSession sqlSessionFactory.openSession();try {ListOpenTest openTestList new ArrayList();for (int i 0; i 1000; i) {OpenTest openTest new OpenTest();openTest.setA(a i);openTest.setB(b i);openTest.setC(c i);openTest.setD(d i);openTest.setE(e i);openTest.setF(f i);openTest.setG(g i);openTest.setH(h i);openTest.setI(i i);openTest.setJ(j i);openTest.setK(k i);openTestList.add(openTest);}StopWatch stopWatch new StopWatch();stopWatch.start(mybatis plus save batch);//批量插入openTestService.saveBatch(openTestList);sqlSession.commit();stopWatch.stop();log.info(mybatis plus save batch stopWatch.getTotalTimeMillis());} finally {sqlSession.close();}
}耗费的时间是 59927 毫秒比一条一条插入快了一倍从这点来看效率还是可以的。
然后常见的还有一种利用拼接 SQL 方式来实现批量插入我们也来对比试试看性能如何。
3、1000 条数据用手动拼接 SQL 方式插入 搞个手动拼接 来跑跑下性能如何
Test
void MapperSaveBatch() {SqlSession sqlSession sqlSessionFactory.openSession();try {ListOpenTest openTestList new ArrayList();for (int i 0; i 1000; i) {OpenTest openTest new OpenTest();openTest.setA(a i);openTest.setB(b i);openTest.setC(c i);openTest.setD(d i);openTest.setE(e i);openTest.setF(f i);openTest.setG(g i);openTest.setH(h i);openTest.setI(i i);openTest.setJ(j i);openTest.setK(k i);openTestList.add(openTest);}StopWatch stopWatch new StopWatch();stopWatch.start(mapper save batch);//手动拼接批量插入openTestMapper.saveBatch(openTestList);sqlSession.commit();stopWatch.stop();log.info(mapper save batch stopWatch.getTotalTimeMillis());} finally {sqlSession.close();}
}耗时只有 2275 毫秒性能比 mybatis-plus 自带的 saveBatch 好了 26 倍
这时我又突然回想起以前直接用 JDBC 批量保存的接口那都到这份上了顺带也跑跑看
4、1000 条数据用 JDBC executeBatch 插入
Test
void JDBCSaveBatch() throws SQLException {SqlSession sqlSession sqlSessionFactory.openSession();Connection connection sqlSession.getConnection();connection.setAutoCommit(false);String sql insert into open_test(a,b,c,d,e,f,g,h,i,j,k) values(?,?,?,?,?,?,?,?,?,?,?);PreparedStatement statement connection.prepareStatement(sql);try {for (int i 0; i 1000; i) {statement.setString(1,a i);statement.setString(2,b i);statement.setString(3, c i);statement.setString(4,d i);statement.setString(5,e i);statement.setString(6,f i);statement.setString(7,g i);statement.setString(8,h i);statement.setString(9,i i);statement.setString(10,j i);statement.setString(11,k i);statement.addBatch();}StopWatch stopWatch new StopWatch();stopWatch.start(JDBC save batch);statement.executeBatch();connection.commit();stopWatch.stop();log.info(JDBC save batch stopWatch.getTotalTimeMillis());} finally {statement.close();sqlSession.close();}
}耗时是 55663 毫秒所以 JDBC executeBatch 的性能跟 mybatis-plus 的 saveBatch 一样底层一样。
综上所述拼接 SQL 的方式实现批量保存效率最佳。
但是我又不太甘心总感觉应该有什么别的法子然后我就继续跟着 mybatis-plus 的源码 debug 了一下跟到了 MySQL 的驱动突然发现有个 if 里面的条件有点显眼 就是这个叫 rewriteBatchedStatements 的玩意从名字来看是要重写批操作的 Statement前面batchHasPlainStatements 已经是 false取反肯定是 true所以只要这参数是 true 就会进行一波操作。
我看了下默认是 false。 直接将 jdbcurl 加上了这个参数 然后继续跑了下 mybatis-plus 自带的 saveBatch果然性能大大提高跟拼接 SQL 差不多 然后我继续 debug 来探探 rewriteBatchedStatements 究竟是怎么 rewrite 的 如果这个参数是 true则会执行下面的方法且直接返回 看下 executeBatchedInserts 究竟干了什么 看到上面我圈出来的代码没好像已经有点感觉了继续往下 debug。
果然SQL 语句被 rewrite了 对插入而言所谓的 rewrite 其实就是将一批插入拼接成 insert into xxx values (a),(b),©…这样一条语句的形式然后执行这样一来跟拼接 SQL 的效果是一样的。
那为什么默认不给这个参数设置为 true 呢主要有以下两点
如果批量语句中的某些语句失败则默认重写会导致所有语句都失败。
批量语句的某些语句参数不一样则默认重写会使得查询缓存未命中。
看起来影响不大所以我给我的项目设置上了这个参数
最后
稍微总结下我粗略的对比虽然粗略但实验结果符合原理层面的理解如果你想更准确地做实验可以使用 JMH并且测试更多组数如 500010000等的情况。 所以如果有使用 JDBC 的 Batch 性能方面的需求要将 rewriteBatchedStatements 设置为 true这样能提高很多性能。
然后如果喜欢手动拼接 SQL 要注意一次拼接的数量分批处理。