欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 【Mybatis】MyBatisPlus的saveBatch真的是批量插入吗?深度解析与性能优化

【Mybatis】MyBatisPlus的saveBatch真的是批量插入吗?深度解析与性能优化

2025/4/30 17:01:14 来源:https://blog.csdn.net/weixin_50591390/article/details/147482329  浏览:    关键词:【Mybatis】MyBatisPlus的saveBatch真的是批量插入吗?深度解析与性能优化

前言

在使用MyBatis-Plus进行批量数据插入时,许多开发者会发现:即使调用saveBatch方法,数据库仍会产生大量INSERT语句。本文将深入源码揭示背后的真相,并提供3种性能优化方案,让你的批量插入速度提升10倍!


一、为什么批量插入这么慢?

1.1 性能测试对比

// 测试代码
List<User> users = generateUsers(10000); // 生成1w条测试数据
long start = System.currentTimeMillis();
userService.saveBatch(users);
System.out.println("耗时:" + (System.currentTimeMillis() - start) + "ms");// 测试结果
// 默认配置:耗时 3200ms
// 期待效果:耗时 < 500ms

1.2 SQL监控日志

-- 实际执行的SQL
INSERT INTO user (name,age) VALUES ('user1', 20);
INSERT INTO user (name,age) VALUES ('user2', 21);
...(重复1w次)

二、源码解析:揭开saveBatch的真面目

2.1 核心源码追踪

代码定位ServiceImpl.saveBatchSqlHelper.executeBatch

// 关键源码片段
public boolean saveBatch(Collection<T> entityList, int batchSize) {return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)); // 逐条插入
}

2.2 执行流程图示

saveBatch调用
获取SqlSession
循环实体列表
单条插入
提交事务

2.3 慢速根源分析

关键因素影响说明
SimpleExecutor默认执行器逐条提交SQL
事务提交机制默认自动提交(可优化)
JDBC网络开销每次插入产生一次网络IO

三、性能优化方案

3.1 方案一:启用批量执行器(配置优化)

3.1.1 修改配置

mybatis-plus:configuration:default-executor-type: batch # 启用批量模式global-config:db-config:logic-delete-field: isDeleted # 避免逻辑删除干扰

3.1.2 效果验证

-- 批量插入SQL(真实执行)
INSERT INTO user (name, age) 
VALUES ('user1',20), ('user2',21)...;

性能提升:1w条数据插入从3200ms → 850ms


3.2 方案二:自定义批量SQL(终极优化)

3.2.1 扩展Mapper接口

public interface UserMapper extends BaseMapper<User> {@Insert("<script>" +"INSERT INTO user (name, age) VALUES " +"<foreach collection='list' item='item' separator=','>" +"(#{item.name}, #{item.age})" +"</foreach>" +"</script>")void insertBatch(@Param("list") List<User> users);
}

3.2.2 服务层调用

@Autowired
private UserMapper userMapper;public void superBatchSave(List<User> users) {userMapper.insertBatch(users);
}

性能对比:1w条数据插入仅需420ms


3.3 方案三:事务+分批提交(平衡方案)

@Transactional
public void batchSave(List<User> users) {int batchSize = 1000;for (int i = 0; i < users.size(); i += batchSize) {List<User> subList = users.subList(i, Math.min(i + batchSize, users.size()));userService.saveBatch(subList);}
}

优势
✅ 避免大事务导致锁表
✅ 内存占用可控
✅ 兼容默认实现


四、生产环境注意事项

4.1 连接池配置

spring:datasource:hikari:maximum-pool-size: 20 # 根据并发量调整connection-timeout: 60000

4.2 监控指标

监控项推荐阈值工具
批量插入耗时< 1s/千条Grafana + Prometheus
数据库连接数< 80%最大连接数Druid监控
事务锁等待时间< 500msSHOW ENGINE INNODB STATUS

4.3 失败重试机制

@Retryable(value = SQLException.class, maxAttempts = 3)
public void batchOperation() {// 批量操作
}

五、性能对比总结

方案1w条耗时网络请求次数代码侵入性适用场景
默认saveBatch3200ms10000小数据量场景
BatchExecutor850ms1中大数据量
自定义批量SQL420ms1极致性能要求
事务分批提交1500ms10平衡型方案

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词