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

原文:https://blog.csdn.net/xu1916659422/article/details/77971696/

 

上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题。

实现方式有两种,

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

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

下面进行实现。

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

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

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

<!-- 这次用resultmap接收输出结果  -->
    <select id="findByName" parameterType="string" resultMap="customerMap">
        select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,100
    </select>
 
 
    <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->
    <update id="batchUpdate" parameterType="java.util.Map">
        <!-- 接收list参数,循环着组装sql语句,注意for循环的写法
             separator=";" 代表着每次循环完,在sql后面放一个分号
             item="cus" 循环List的每条的结果集
             collection="list" list 即为 map传过来的参数key -->
        <foreach collection="list" separator=";" item="cus">
            update t_customer set
            c_name = #{cus.name},
            c_age = #{cus.age},
            c_sex = #{cus.sex},
            c_ceroNo = #{cus.ceroNo},
            c_ceroType = #{cus.ceroType}
            where id = #{cus.id}
        </foreach>
    </update>
 
    <!-- 批量更新第二种方法,通过 case when语句变相的进行批量更新 -->
    <update id="batchUpdateCaseWhen" parameterType="java.util.Map">
        update t_customer
        <trim prefix="set" suffixOverrides=",">
            <!-- 拼接case when 这是一种写法 -->
            <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->
            <!--when #{cus.id} then #{cus.age}-->
            <!--</foreach>-->
 
            <!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->
            <trim prefix="c_name =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.name!=null">
                        when id=#{cus.id} then #{cus.name}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_age =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.age!=null">
                        when id=#{cus.id} then #{cus.age}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_sex =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.sex!=null">
                        when id=#{cus.id} then #{cus.sex}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_ceroNo =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.ceroNo!=null">
                        when id=#{cus.id} then #{cus.ceroNo}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_ceroType =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.ceroType!=null">
                        when id=#{cus.id} then #{cus.ceroType}
                    </if>
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="cus">
                id = #{cus.id}
            </foreach>
        </where>
    </update>

 

接口

List<Customer> findByName(String name);

int batchUpdate(Map<String,Object> param);

int batchUpdateCaseWhen(Map<String,Object> param);

 

实现类

    /**
     * 用于更新时,获取更新数据
     * @param name
     * @return
     */
    public List<Customer> findByName(String name) {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlsessionUtil.getSqlSession();
            return sqlSession.selectList("customer.findByName", name);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlsessionUtil.closeSession(sqlSession);
        }
        return new ArrayList<Customer>();
    }
 
 
    /**
     * 批量更新第一种方式
     * @param param
     * @return
     */
    public int batchUpdate(Map<String,Object> param) {
        return bathUpdate("customer.batchUpdate",param);
    }
 
    /**
     * 批量更新第二种方式
     * @param param
     * @return
     */
    public int batchUpdateCaseWhen(Map<String,Object> param) {
        return bathUpdate("customer.batchUpdateCaseWhen",param);
    }
 
    /**
     * 公共部分提出
     * @param statementId
     * @param param
     * @return
     */
    private int bathUpdate(String statementId,Map param){
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlsessionUtil.getSqlSession();
            int key =  sqlSession.update(statementId, param);
            // commit
            sqlSession.commit();
            return key;
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
        } finally {
            SqlsessionUtil.closeSession(sqlSession);
        }
        return 0;
    }

 

 

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

    @Test
    public void batchInsert() throws Exception {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Customer> list = new ArrayList<Customer>();
        for(int i=0;i<10000;i++){
            Customer customer = new Customer();
            customer.setName("准备数据" + i);
            customer.setAge(15);
            customer.setCeroNo("111111111111"+i);
            customer.setCeroType(2);
            customer.setSex(1);
            list.add(customer);
        }
        param.put("list",list);
        Long start = System.currentTimeMillis();
        int result = customerDao.batchInsert(param);
        System.out.println("耗时 : "+(System.currentTimeMillis() - start));
    }

开始进行测试效率问题。

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

    <!-- 这次用resultmap接收输出结果  -->
    <select id="findByName" parameterType="string" resultMap="customerMap">
        select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,10
    </select>

测试类

    @Test
    public void batchudpate() throws Exception {
        Map<String,Object> param = new HashMap<String,Object>();
 
        param.put("list",getFindByName("准备数据","批量更新01"));
        Long start = System.currentTimeMillis();
        customerDao.batchUpdate(param);
        System.out.println("耗时 : "+(System.currentTimeMillis() - start));
    }
 
    @Test
    public void batchudpateCaseWhen() throws Exception {
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("list",getFindByName("批量更新01","准备数据"));
        Long start = System.currentTimeMillis();
        customerDao.batchUpdateCaseWhen(param);
        System.out.println("耗时 : "+(System.currentTimeMillis() - start));
    }
 
    private List<Customer> getFindByName(String name, String change){
        List<Customer> list = customerDao.findByName(name);
        System.out.println("查询出来的条数 : " + list.size());
        if(null != change && !"".equals(change)){
            for(Customer customer : list){
                customer.setName(change);
            }
        }
 
        return list;
    }

第一种拼完整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的时候会比较慢,所以效率问题严重。使用的时候建议分批插入。

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


 

posted @ 2019-01-31 11:30  这个名字想了很久~  阅读(17634)  评论(0编辑  收藏  举报