mybatis学习之路----批量更新数据两种方法效率对比

批量更新数据两种写法的效率问题。

实现方式有两种,

一种用for循环通过循环传过来的参数集合,循环出N条sql,

另一种 用mysql的case when 条件判断变相的进行批量更新  

下面进行实现。

注意第一种方法要想成功,需要在db链接url后面带一个参数  &allowMultiQueries=true

即:  jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true

其实这种东西写过来写过去就是差不多一样的代码,不做重复的赘述,直接上代码。

[html]  view plain  copy
  1. <!-- 这次用resultmap接收输出结果  -->  
  2. <select id="findByName" parameterType="string" resultMap="customerMap">  
  3.     select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,100  
  4. </select>  
  5.   
  6.   
  7. <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->  
  8. <update id="batchUpdate" parameterType="java.util.Map">  
  9.     <!-- 接收list参数,循环着组装sql语句,注意for循环的写法  
  10.          separator=";" 代表着每次循环完,在sql后面放一个分号  
  11.          item="cus" 循环List的每条的结果集  
  12.          collection="list" list 即为 map传过来的参数key -->  
  13.     <foreach collection="list" separator=";" item="cus">  
  14.         update t_customer set  
  15.         c_name = #{cus.name},  
  16.         c_age = #{cus.age},  
  17.         c_sex = #{cus.sex},  
  18.         c_ceroNo = #{cus.ceroNo},  
  19.         c_ceroType = #{cus.ceroType}  
  20.         where id = #{cus.id}  
  21.     </foreach>  
  22. </update>  
  23.   
  24. <!-- 批量更新第二种方法,通过 case when语句变相的进行批量更新 -->  
  25. <update id="batchUpdateCaseWhen" parameterType="java.util.Map">  
  26.     update t_customer  
  27.     <trim prefix="set" suffixOverrides=",">  
  28.         <!-- 拼接case when 这是一种写法 -->  
  29.         <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->  
  30.         <!--when #{cus.id} then #{cus.age}-->  
  31.         <!--</foreach>-->  
  32.   
  33.         <!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->  
  34.         <trim prefix="c_name =case" suffix="end,">  
  35.             <foreach collection="list" item="cus">  
  36.                 <if test="cus.name!=null">  
  37.                     when id=#{cus.id} then #{cus.name}  
  38.                 </if>  
  39.             </foreach>  
  40.         </trim>  
  41.         <trim prefix="c_age =case" suffix="end,">  
  42.             <foreach collection="list" item="cus">  
  43.                 <if test="cus.age!=null">  
  44.                     when id=#{cus.id} then #{cus.age}  
  45.                 </if>  
  46.             </foreach>  
  47.         </trim>  
  48.         <trim prefix="c_sex =case" suffix="end,">  
  49.             <foreach collection="list" item="cus">  
  50.                 <if test="cus.sex!=null">  
  51.                     when id=#{cus.id} then #{cus.sex}  
  52.                 </if>  
  53.             </foreach>  
  54.         </trim>  
  55.         <trim prefix="c_ceroNo =case" suffix="end,">  
  56.             <foreach collection="list" item="cus">  
  57.                 <if test="cus.ceroNo!=null">  
  58.                     when id=#{cus.id} then #{cus.ceroNo}  
  59.                 </if>  
  60.             </foreach>  
  61.         </trim>  
  62.         <trim prefix="c_ceroType =case" suffix="end,">  
  63.             <foreach collection="list" item="cus">  
  64.                 <if test="cus.ceroType!=null">  
  65.                     when id=#{cus.id} then #{cus.ceroType}  
  66.                 </if>  
  67.             </foreach>  
  68.         </trim>  
  69.     </trim>  
  70.     <where>  
  71.         <foreach collection="list" separator="or" item="cus">  
  72.             id = #{cus.id}  
  73.         </foreach>  
  74.     </where>  
  75. </update>  



接口

[java]  view plain  copy
  1. List<Customer> findByName(String name);  
  2.   
  3. int batchUpdate(Map<String,Object> param);  
  4.   
  5. int batchUpdateCaseWhen(Map<String,Object> param);  

实现类

[java]  view plain  copy
  1. /** 
  2.  * 用于更新时,获取更新数据 
  3.  * @param name 
  4.  * @return 
  5.  */  
  6. public List<Customer> findByName(String name) {  
  7.     SqlSession sqlSession = null;  
  8.     try {  
  9.         sqlSession = SqlsessionUtil.getSqlSession();  
  10.         return sqlSession.selectList("customer.findByName", name);  
  11.     } catch (Exception e) {  
  12.         e.printStackTrace();  
  13.     } finally {  
  14.         SqlsessionUtil.closeSession(sqlSession);  
  15.     }  
  16.     return new ArrayList<Customer>();  
  17. }  
  18.   
  19.   
  20. /** 
  21.  * 批量更新第一种方式 
  22.  * @param param 
  23.  * @return 
  24.  */  
  25. public int batchUpdate(Map<String,Object> param) {  
  26.     return bathUpdate("customer.batchUpdate",param);  
  27. }  
  28.   
  29. /** 
  30.  * 批量更新第二种方式 
  31.  * @param param 
  32.  * @return 
  33.  */  
  34. public int batchUpdateCaseWhen(Map<String,Object> param) {  
  35.     return bathUpdate("customer.batchUpdateCaseWhen",param);  
  36. }  
  37.   
  38. /** 
  39.  * 公共部分提出 
  40.  * @param statementId 
  41.  * @param param 
  42.  * @return 
  43.  */  
  44. private int bathUpdate(String statementId,Map param){  
  45.     SqlSession sqlSession = null;  
  46.     try {  
  47.         sqlSession = SqlsessionUtil.getSqlSession();  
  48.         int key =  sqlSession.update(statementId, param);  
  49.         // commit  
  50.         sqlSession.commit();  
  51.         return key;  
  52.     } catch (Exception e) {  
  53.         sqlSession.rollback();  
  54.         e.printStackTrace();  
  55.     } finally {  
  56.         SqlsessionUtil.closeSession(sqlSession);  
  57.     }  
  58.     return 0;  
  59. }  

测试前准备   首先用上节的 mybatis学习之路----批量更新数据 批量插入,插入10000条数据以备下面的批量更新用。

[java]  view plain  copy
  1. @Test  
  2. public void batchInsert() throws Exception {  
  3.     Map<String,Object> param = new HashMap<String,Object>();  
  4.     List<Customer> list = new ArrayList<Customer>();  
  5.     for(int i=0;i<10000;i++){  
  6.         Customer customer = new Customer();  
  7.         customer.setName("准备数据" + i);  
  8.         customer.setAge(15);  
  9.         customer.setCeroNo("111111111111"+i);  
  10.         customer.setCeroType(2);  
  11.         customer.setSex(1);  
  12.         list.add(customer);  
  13.     }  
  14.     param.put("list",list);  
  15.     Long start = System.currentTimeMillis();  
  16.     int result = customerDao.batchInsert(param);  
  17.     System.out.println("耗时 : "+(System.currentTimeMillis() - start));  
  18. }  

开始进行测试效率问题。

首先进行的是测试十条数据。调整查询数据为查询十条

[html]  view plain  copy
  1. <!-- 这次用resultmap接收输出结果  -->  
  2. <select id="findByName" parameterType="string" resultMap="customerMap">  
  3.     select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,10  
  4. </select>  
测试类

[java]  view plain  copy
  1. @Test  
  2. public void batchudpate() throws Exception {  
  3.     Map<String,Object> param = new HashMap<String,Object>();  
  4.   
  5.     param.put("list",getFindByName("准备数据","批量更新01"));  
  6.     Long start = System.currentTimeMillis();  
  7.     customerDao.batchUpdate(param);  
  8.     System.out.println("耗时 : "+(System.currentTimeMillis() - start));  
  9. }  
  10.   
  11. @Test  
  12. public void batchudpateCaseWhen() throws Exception {  
  13.     Map<String,Object> param = new HashMap<String,Object>();  
  14.     param.put("list",getFindByName("批量更新01","准备数据"));  
  15.     Long start = System.currentTimeMillis();  
  16.     customerDao.batchUpdateCaseWhen(param);  
  17.     System.out.println("耗时 : "+(System.currentTimeMillis() - start));  
  18. }  
  19.   
  20. private List<Customer> getFindByName(String name, String change){  
  21.     List<Customer> list = customerDao.findByName(name);  
  22.     System.out.println("查询出来的条数 : " + list.size());  
  23.     if(null != change && !"".equals(change)){  
  24.         for(Customer customer : list){  
  25.             customer.setName(change);  
  26.         }  
  27.     }  
  28.   
  29.     return list;  
  30. }  
第一种拼完整sql的方式耗时:

第二种case when 耗时情况:



结果可以看出,其实case when 耗时比较多。


下面来加大数据量到100条;

第一种拼完整sql的方式耗时:

第二种case when 耗时情况:


结果可以看出,其实case when 耗时仍然比第一种多。


继续加大数据量到1000条

第一种拼完整sql的方式耗时:

第二种case when 耗时情况:

结果可以看出,其实case when 耗时仍然比第一种多。


继续加大数据量到10000条

第一种拼完整sql的方式耗时:

第二种case when 耗时情况:

结果可以看出,两种方式进行批量更新,效率已经不在一个数量级了。case when明显的慢的多。


看网上有人说第一种的效率跟用代码循环着一条一条的循环着插入的效率差不多,通过测试我就有疑问了,他是怎么做到的。难道我的代码有问题?明明第一种的效率很高嘛。


                      第一种效率其实相当高的,因为它仅仅有一个循环体,只不过最后update语句比较多,量大了就有可能造成sql阻塞。

      第二种虽然最后只会有一条更新语句,但是xml中的循环体有点多,每一个case when 都要循环一遍list集合,所以大批量拼sql的时候会比较慢,所以效率问题严重。使用的时候建议分批插入。

根据效率,安全方面综合考虑,选择适合的很重要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值