Java 3种批量插入更新操作的效率横向比较

 

以前总是说批量插入和更新的效率比非批量的要高,但是到底高多少,没有评估过,今天我就具体的测试下

 

(1)三种插入操作的方法

1.1 利用for循环的批量插入

示例xml

 
  1. <insert id="insertUser">

  2. insert into test_user (u_name,create_date) value (#{userName},SYSDATE())

  3. </insert>

  • 1
  • 2
  • 3

示例代码:

 
  1. for (int i = 1; i <= num; i++) {

  2. User user = new User();

  3. user.setUserName("a" + i);

  4. user.setCreateDate(new Date());

  5. userDao.insertUser(user);

  6. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. 采用jdbc

示例代码:

 
  1. Connection conn;

  2. try {

  3. Class.forName("com.mysql.jdbc.Driver");

  4. conn = DriverManager.getConnection("jdbc:mysql://192.168.0.200:3306/xxx", "root", "root");

  5. conn.setAutoCommit(false);

  6. String sql = "insert into test_user (u_name,create_date) value (?,SYSDATE())";

  7. PreparedStatement prest = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,

  8. ResultSet.CONCUR_READ_ONLY);

  9. conn.setAutoCommit(false);

  10.  
  11. for (int i = 1; i <= 100; i++) {

  12. prest.setString(1, "a" + i);

  13. prest.addBatch();

  14. }

  15. prest.executeBatch();

  16. conn.commit();

  17.  
  18.  
  19. conn.close();

  20. } catch (Exception ex) {

  21. ex.printStackTrace();

  22. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

数据量分别是10,100,300,1000,5000条数据


数据量:10 
批量更新耗时:279 
非批量更新耗时:1522 
jdbc批量更新耗时:255

数据量:100 
批量更新耗时:720 
非批量更新耗时:3391 
jdbc批量更新耗时:1912

数据量:300 
批量更新耗时:987 
非批量更新耗时:9827 
jdbc批量更新耗时:7616

数据量:500 
批量更新耗时:1649 
非批量更新耗时:16253 
jdbc批量更新耗时:10475

数据量:1000 
批量更新耗时:2552 
非批量更新耗时:33048 
jdbc批量更新耗时:20793

数据量:5000 
批量更新耗时:19066 
非批量更新耗时:239127 
jdbc批量更新耗时:103273


这里写图片描述

综上分析,效率排比如下 
mybatis批量更新 > jdbc批量更新 > 循环调用update语句

 

 

(2)三种批量更新的方法

2.1 利用for循环批量更新

示例xml

 
  1. <update id="updateUser">

  2. update test_user set test_user.u_name = (#{updateUserName}) where test_user.u_name = (#{userName})

  3. </update>

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

示例代码

 
  1. for (int i = 1; i <= num; i++) {

  2. User user = new User();

  3. user.setUserName("a" + i);

  4. user.setUpdateUserName("b" + i);

  5. userDao.updateUser(user);

  6. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.2 jdbc 批量更新

示例代码

 
  1. Connection conn;

  2. try {

  3. Class.forName("com.mysql.jdbc.Driver");

  4. conn = DriverManager.getConnection("jdbc:mysql://192.168.0.200:3306/xxx", "root", "root");

  5. conn.setAutoCommit(false);

  6.  
  7. // 保存当前自动提交模式

  8. boolean autoCommit = conn.getAutoCommit();

  9. // 关闭自动提交

  10. conn.setAutoCommit(false);

  11. Statement stmt =conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

  12.  
  13. for (int i = 1; i <= num; i++) {

  14. stmt.addBatch("update test_user set test_user.u_name = ('d"+i+"') where test_user.u_name = ('c"+i+"')");

  15. }

  16.  
  17. stmt.executeBatch();

  18. conn.commit();

  19. conn.close();

  20. } catch (Exception ex) {

  21. ex.printStackTrace();

  22. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.3 mybatis 批量更新

其实是利用了mysql的批量更新的语法 
case when的语法 
详见 【case when 语法】 
示例xml

 
  1. <update id="batchUpdateList">

  2. update test_user

  3. <trim prefix="set" suffixOverrides=",">

  4.  
  5. <trim prefix="u_name =case" suffix="end,">

  6. <foreach item="item" collection="userList">

  7. when test_user.u_name = (#{item.userName})

  8. then #{item.updateUserName}

  9. </foreach>

  10. </trim>

  11.  
  12. </trim>

  13. where

  14. <foreach item="item" collection="userList" separator="or">

  15. (test_user.u_name = (#{item.userName}))

  16. </foreach>

  17.  
  18. </update>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

示例代码

 
  1. for (int i = 1; i <= num; i++) {

  2. User user = new User();

  3. user.setUserName("a" + i);

  4. user.setUpdateUserName("b" + i);

  5. userList.add(user);

  6. }

  7. userDao.batchUpdateList(userList);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

数据量分别是10,100,300,1000,5000条数据


数据量:10 
批量更新耗时:279 
非批量更新耗时:1522 
jdbc批量更新耗时:255

数据量:100 
批量更新耗时:720 
非批量更新耗时:3391 
jdbc批量更新耗时:1912

数据量:300 
批量更新耗时:987 
非批量更新耗时:9827 
jdbc批量更新耗时:7616

数据量:500 
批量更新耗时:1649 
非批量更新耗时:16253 
jdbc批量更新耗时:10475

数据量:1000 
批量更新耗时:2552 
非批量更新耗时:33048 
jdbc批量更新耗时:20793

数据量:5000 
批量更新耗时:19066 
非批量更新耗时:239127 
jdbc批量更新耗时:103273


这里写图片描述

综上分析,效率排比如下 
mybatis批量更新 > jdbc批量更新 > 循环调用update语句

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值