• 对比 MyBatis 和 MyBatis-Plus 批量插入、批量更新的性能和区别


    1 环境准备

    demo 地址:learn-mybatis · Sean/spring-cloud-alibaba - 码云(gitee.com)

    1.1 搭建 MyBatis-Plus 环境

    1. 创建 maven springboot 工程
    2. 导入依赖:web 启动器、jdbc、、java 连接 mysql、Lombok、druid 连接池启动器、mybatis-plus 启动器
    3. 编写 yaml 配置文件,配置 druid 数据源、mybatis-plus

    注意要点:

    1. mapper 接口继承 BaseMapper<实体类>
    2. service 接口继承 IService<实体类>
    3. service 接口实现类继承 ServiceImpl 实现 service 接口

    1.2 批量插入测试接口

    1. MyBatis 批量插入接口
       @GetMapping("/mybatis-batch-insert")
       public String mybatisBatchInsert(){
         // 开始时间
         long stime = System.currentTimeMillis();    
         // 批量插入
         orderService.mySaveBatch(list);
         // 结束时间
         long etime = System.currentTimeMillis();
         return "mybatis 批量插入 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
       }
      
      Mybatis 的批量插入是调用 mapper 的批量插入接口,使用 标签拼接 sql 进行插入
    2. Mybatis-Plus 批量插入接口
      @GetMapping("/mybatis-batch-insert")
      public String mybatisBatchInsert(){
          // 开始时间
          long stime = System.currentTimeMillis();
          // 批量插入
          orderService.mySaveBatch(list);
          // 结束时间
          long etime = System.currentTimeMillis();
          return "mybatis 批量插入 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
      }
      
      MyBatis-Plus 的批量插入是调用 mybatis-plus 的 IService 接口的 saveBatch 进行批量插入

    1.3 批量更新测试接口

    1. MyBatis 批量更新接口

      @GetMapping("/mybatis-batch-update")
      public String mybatisBatchUpdate(){
          long stime = System.currentTimeMillis();
          orderService.myUpdateBatchById(updateList);
          long etime = System.currentTimeMillis();
          return "mybatis 批量更新 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
      }
      

      MyBatis 的批量插入是调用 mapper 的批量更新接口,使用 标签拼接 sql 进行更新,是将多个更新语句拼接在同一个 mapper 接口中,需要在数据库连接 url 添加 allowMultiQueries=true 开启多查询

    2. MyBatis-Plus 批量更新接口

      @GetMapping("/mybatis-plus-batch-update")
      public String mybatisPlusBatchUpdate(){
          long stime = System.currentTimeMillis();
          orderService.updateBatchById(updateList);
          long etime = System.currentTimeMillis();
          return "mybatis plus 批量更新 1w 条数据的时间: " + (etime - stime) / 1000.0 + "秒";
      }
      

      MyBatis -Plus 的批量更新是调用 mybatis-plus 的 IService 接口的 updateBatchById 进行批量更新

    2. 性能对比

    经过预热,尽量避免了缓存的影响。

    2.1 批量插入性能对比

    数据量:1w 条数据,每条数据 4 个字段

    测试结果:

    • MyBatis:5 次运行平均耗时:0.3212 秒
    • MyBatis-Plus:5次运行平均耗时:1.35 秒
    • MyBatis-Plus(开启批处理):5次运行平均耗时:0.2424 秒

    2.2 批量更新性能对比

    数据量:1w 条数据,每条数据更新 4 个字段

    测试结果:

    • MyBatis:5 次运行平均耗时:1.0378 秒
    • MyBatis-Plus:5次运行平均耗时:1.6448 秒
    • MyBatis-Plus(开启批处理):5次运行平均耗时:0.743 秒

    3. 原理探究

    3.1 批量插入

    3.1.1 MyBatis

    MyBatis 的批量插入 xml

    <insert id="mySaveBatch">
        insert into order_table(id, product_id, total_amount, status) values
        <foreach collection="list" separator="," item="item">
            (#{item.id}, #{item.productId}, #{item.totalAmount}, #{item.status})
        foreach>
    insert>
    

    通过 标签,将插入的数据拼接成一条 SQL 语句,一次性进行插入操作,拼接完的 SQL 语句如下例子:

    insert into order_table(id, product_id, total_amount, status) values(1, 2. 2.0, 1),(2, 2, 2.0, 1);
    

    3.1.2 MyBatis-Plus

    MyBatis-Plus 的批量插入本质采用 for 遍历每条数据依次插入,但使用了批处理优化,默认是每 1000 条数据,刷新一次 statement 提交到数据库,执行插入操作。

    注意:批处理需要在数据库连接中添加 rewriteBatchedStatements=true 否则 jdbc 驱动在默认情况下会无视executeBatch() 语句

    源码如下:

    @Transactional(rollbackFor = Exception.class)   // 开启事务
    @Override
    public boolean saveBatch(Collection entityList, int batchSize) {
        String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);    // 获得插入 statement
        // 执行批处理操作
        // 参数是:待插入集合,批次大小(默认1000),函数式接口 accept
        return executeBatch(entityList, batchSize, (sqlSession, entity) ->  sqlSession.insert(sqlStatement, entity)); 
    }
    
    public static  boolean executeBatch(Class entityClass, Log log, Collection list, int batchSize, BiConsumer consumer) {
        Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
        return !CollectionUtils.isEmpty(list) && executeBatch(entityClass, log, sqlSession -> {
            int size = list.size();
            int idxLimit = Math.min(batchSize, size);
            int i = 1;
            // 遍历插入
            for (E element : list) {
                // 调用 sqlSession.insert(sqlStatement, entity)); 
                // 对元素执行插入操作,但此时数据库还没真正执行插入语句
                consumer.accept(sqlSession, element);   
                // 计数达到 1000 或者 集合结束
                if (i == idxLimit) {        
                     // 刷新 statement 提交批处理语句,数据库真正执行 SQL
                    sqlSession.flushStatements();
                    idxLimit = Math.min(idxLimit + batchSize, size);
                }
                i++;
            }
        });
    }
    

    3.2 批量更新

    3.2.1 MyBatis

    MyBatis 的批量更新 xml

    <update id="myUpdateBatchById">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update order_table
            <set>
                product_id = #{item.productId},
                total_amount = #{item.totalAmount},
                status = #{item.status}
            set>
            where id = #{item.id}
        foreach>
    update>
    

    通过 标签,拼接成多条更新的 SQL 语句,一次性提交数据库执行。语句例子如下:

    update order_table set product_id = 1, total_amount = 2, status = 1 where id = 1;
    update order_table set product_id = 1, total_amount = 2, status = 1 where id = 2;
    

    3.1.2 MyBatis-Plus

    MyBatis-Plus 批量更新的原理基本和其批量插入的原理一致,都是调用 executeBatch 执行批处理操作。

    4. 优缺点分析

    4.1 批量插入

    对于批量插入,MyBatis 拼接 SQL 的写法比 MyBatis-Plus 的批量插入方法有明显更高的性能。
    但在开启批处理优化之后,MyBatis-Plus 的方法性能更高了。

    MyBatis:

    • 优点:性能较高
    • 缺点:在处理大数据量(SQL 语句大于 64MB 时)会报错,MySQL 8.0.33 默认最大 SQL 大小为 64MB
      也要考虑内存异常等问题。

    MyBatisPlus:

    • 优点:分组提交,适用于大数据量的处理
    • 缺点:单条插入语句执行,性能较低

    总结:

    • 没开启批处理时:10w 数据量以下建议使用 MyBatis、10w 数据量以上建议使用 MyBatis-Plus
    • 开启批处理时:建议使用 MyBatis-Plus

    4.2 批量更新

    对于批量更新,MyBatis 拼接 SQL 的写法与 MyBatis-Plus 的批量更新方法无明显的性能差别.
    大于大数据量,拼接写法甚至容易出现内存耗尽等问题,相比之下 MyBatis-Plus 的方法更优。

    总结:建议使用 MyBatis-Plus

  • 相关阅读:
    奥菲斯办公
    CentOS 8最小安装,VM使用这个内存占用小很多
    git的安装 及 命令
    clickhouse在执行alter table update delete等命令后数据没有更新
    huggingface transformers库中LlamaForCausalLM
    【单片机】17-温度传感器DS18B20
    【计算机网络】TCP协议与UDP协议
    编译原理笔记01
    最新版两款不同版SEO超级外链工具PHP源码
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java成绩管理与学情分析系统11yzf
  • 原文地址:https://www.cnblogs.com/SihanLin/p/17681811.html