各种jdbc批处理效率比较

花了一天的时间试验了一下各种jdbc批处理,一共测试了4种10万条数据本地mysql插入:
      1.原装jdbc PreparedStatement addBatch();  耗时(7.7秒)
      2.原装jdbc Statement addBatch(sql);  耗时(9.2秒)
      3.原装jdbc Statement execute(sql);  耗时(8秒)
      4.spring jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {...}) ;耗时(30分钟以上)
      5.hibernate session.flush()  (很早以前测试的) (10分钟)
    

    程序片段如下:
public void test() {
  StopWatch sw = new StopWatch();
  System.out.println("-----------------start---------------");
  final List<String> list1 = new ArrayList<String>();
  
  sw.start();
  for (int i = 0; i < 2000; i++) {
//   list1.add("douzi");
//   jdbcTemplate.execute();
  }
  Connection con = null;
//  PreparedStatement ps = null;
  Statement st = null;
  try {
   con = jdbcTemplate.getDataSource().getConnection();
   con.setAutoCommit(false);
//   ps = con.prepareStatement("insert into Aa(name) values('douzi')");
   st = con.createStatement();
   for (int i = 0; i < 100000; i++) {
//    ps.setString(1, "douzi" + i);
//    st.addBatch("insert into Aa(name) values('douzi')");
    st.execute("insert into Aa(name) values('douzi')");
//    ps.addBatch();
//    if (i % 10000 == 0) {
     ps.executeBatch();
     ps.clearBatch();
//     st.executeBatch();
//     st.clearBatch();
//     con.commit();
//    }
   }
//   ps.executeBatch();
   st.executeBatch();
   st.clearBatch();
   con.commit();
   con.setAutoCommit(true);

  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
//    ps.close();
    st.close();
    con.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  
  sw.stop();
  System.out.println("-----------------end---------------" + sw.getTime());
 }

    得出结论为:以后的批处理,就用原始的jdbc批处理即可,其他的框架批处理可以直接忽略掉了,其次前三种jdbc中,我认为使用前两种代码是到哪里都ok的;第三种虽然速度快,但不适用与远程数据库;
    第四种速度慢的根本原因我不清楚,但是知道它是每次都commit一句sql引起的;如何关闭autoCommit,没有实验出来;欢迎知道的告知一下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
回答: 引用中提到了使用jdbcTemplate的batchUpdate方法进行批量数据插入,但是插入50000条数据时的耗时较长。为了优化效率,可以考虑使用批处理功能。引用中提到,批处理可以帮助提高性能。通过将多个插入语句打包在一起,减少了与数据库的通信次数,从而提高了效率。在批处理中,可以一次性执行多个插入语句,而不是逐个执行。这样可以减少了与数据库的交互次数,提高了效率。具体实现是将多个插入语句的参数打包成一个列表,然后一次性执行这个列表。可以使用JdbcTemplate的batchUpdate方法来实现批处理。 在引用中的代码中,通过循环将每条数据的参数加入到批处理列表中,然后使用batchUpdate方法一次性执行这个列表。这样可以大大减少与数据库的交互次数,提高效率。需要注意的是,在批处理过程中,数据库的事务处理也是非常重要的。为了保证数据的一致性和完整性,可以在批处理执行之前开启一个事务,然后在批处理执行完毕后提交事务。 另外,引用提到了在数据库连接的url中添加rewriteBatchedStatements=true的设置。这个设置对于MySQL驱动可以有效提高批处理效率。但是,根据引用的描述,该设置对于SQL Server驱动无效。所以,在SQL Server中使用rewriteBatchedStatements=true可能无法提高批处理效率。 综上所述,通过使用批处理功能,并确保正确的事务处理,可以有效提高jdbc批处理效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [关于使用jdbcTemplate的PreparedStatement进行批量插入速度缓慢问题记录](https://blog.csdn.net/weixin_40580131/article/details/124610468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Statement 和PreparedStatement 批量处理JDBC语句提高处理速度](https://blog.csdn.net/axu20/article/details/4005190)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

窦再兴

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值