这篇文章会一步一步带你从一个新手的角度慢慢揭开批处理的神秘面纱,对于初次写Mybatis批处理的同学可能会有很大的帮助,建议收藏点赞~
处理批处理的方式有很多种,这里不分析各种方式的优劣,只是概述 ExecutorType.BATCH 这种的用法,另学艺不精,如果有错的地方,还请大佬们指出更正。
问题原因
在公司写项目的时候,有一个自动对账的需求,需要从文件中读取几万条数据插入到数据库中,后续可能跟着业务的增长,会上升到几十万,所以对于插入需要进行批处理操作,下面我们就来看看我是怎么一步一步踩坑的。
简单了解一下批处理背后的秘密,BatchExecutor
批处理是 JDBC 编程中的另一种优化手段。JDBC 在执行 SQL 语句时,会将 SQL 语句以及实参通过网络请求的方式发送到数据库,一次执行一条 SQL 语句,一方面会减小请求包的有效负载,另一个方面会增加耗费在网络通信上的时间。通过批处理的方式,我们就可以在 JDBC 客户端缓存多条 SQL 语句,然后在 flush 或缓存满的时候,将多条 SQL 语句打包发送到数据库执行,这样就可以有效地降低上述两方面的损耗,从而提高系统性能。
不过,有一点需要特别注意:每次向数据库发送的 SQL 语句的条数是有上限的,如果批量执行的时候超过这个上限值,数据库就会抛出异常,拒绝执行这一批 SQL 语句,所以我们需要控制批量发送 SQL 语句的条数和频率。
引用自《深入剖析 MyBatis 核心原理》- 杨四正 第18节
版本1-呱呱坠地
废话不多说,早先时候项目的代码里就已经存在了批处理的代码,伪代码的样子大概是这样子的:
@Resource
private 某Mapper类 mapper实例对象;
private int BATCH = 1000;
private void doUpdateBatch(Date accountDate, List<某实体类> data) {
SqlSession batchSqlSession = null;
try {
if (data == null || data.size() == 0) {
return;
}
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
for (int index = 0; index < data.size(); index++) {
mapper实例对象.更新/插入Method(accountDate, data.get(index).getOrderNo());
if (index != 0 && index % BATCH == 0) {
batchSqlSession.commit();
batchSqlSession.clearCache();
}
}
batchSqlSession.commit();
} catch (Exception e) {
batchSqlSession.rollback();
log.error(e.getMessage(), e);
} finally {
if (batchSqlSession != null) {
batchSqlSession.close();
}
}
}
我们先来看看上述这种写法的几种问题
你真的懂commit、clearCache、flushStatements嘛?
我们先看看官网给出的解释:
然后我们结合上述写法,它会在判断批处理条数达到1000条的时候会去手动commit
,然后又手动clearCache
,我们先来看看commit
到底都做了一些什么,以下为调用链
@Override
public void commit() {
commit(false);
}
@Override
public void commit(boolean force) {
try {
executor.commit(isCommitOrRollbackRequired(force));
dirty = false;
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error committing transaction. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
private boolean isCommitOrRollbackRequired(boolean force) {
// autoCommit默认为false,调用过插入、更新、删除之后的dirty值为true
return (!autoCommit && dirty) || force;
}
@Override
public void commit(boolean required) throws SQLException {
if (closed) {
throw new ExecutorException("Cannot commit, transaction is already closed");
}
clearLocalCache();
flushStatements();
if (required) {
transaction.commit();
}
}
我们会发现,其实你直接调用commit
的情况下,它就已经做了clearLocalCache
这件事情,所以大可不必在commit
后加上一句clearCache
,而且clearCache
是做了什么你又知道嘛?就搁这调用!!
另外flushStatements
的作用,官网里也有详细解释:
此方法的作用就是将前面所有执行过的INSERT、UPDATE、DELETE语句真正刷新到数据库中。底层调用了JDBC的statement.executeBatch
方法。这个方法的返回值通俗来说如果执行的是同一个方法并且执行的是同一条SQL,注意这里的SQL还没有设置参数,也就是说SQL里的占位符’?'还没有被处理成真正的参数,那么每次执行的结果共用一个BatchResult
,真正的结果可以通过BatchResult
中的getUpdateCounts
方法获取。
另外如果执行了SELECT操作,那么会将先前的UPDATE、INSERT、DELETE语句刷新到数据库中。 这一点去看BatchExecutor
中的doQuery
方法即可。
反例
看到这里,我们在来看点反例,你就会觉得这都是啥跟啥啊!!!误人子弟啊,直接在百度搜一段关键字:mybatis ExecutorType.BATCH 批处理,反例如下:
不具备通用性
由于项目中用到批处理的地方肯定不止一个,那每用一次就需要CV一下,0.0 那会不会显得太菜了?能不能一劳永逸?这个时候就得用上Java8中的接口函数了~
版本2-初具雏形
在解决完上述两个问题后,我们的代码版本来到了第2版,你以为这就对了?这就完事了?别急,我们继续往下看!
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.function.ToIntFunction;
@Slf4j
@Component
public class MybatisBatchUtils {
/**
* 每次处理1000条
*/
private static final int BATCH = 1000;
@Resource
private SqlSessionFactory sqlSessionFactory;
/**
* 批量处理修改或者插入
*
* @param data 需要被处理的数据
* @param function 自定义处理逻辑
* @return int 影响的总行数
*/
public <T> int batchUpdateOrInsert(List<T> data, ToIntFunction<T> function) {
int count = 0;
SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
for (int index = 0; index < data.size(); index++) {
count += function.applyAsInt(data.get(index));
if (index != 0 && index % BATCH == 0) {
batchSqlSession.flushStatements();
}
}
batchSqlSession.commit();
} catch (Exception e) {
batchSqlSession.rollback();
log.error(e.getMessage(), e);
} finally {
batchSqlSession.close();
}
return count;
}
}
伪代码使用案例
@Resource
private 某Mapper类 mapper实例对象;
batchUtils.batchUpdateOrInsert(数据集合, item -> mapper实例对象.insert方法(item));
这个时候我兴高采烈的收工了,直到过了一两天,导师问我,考虑过这个业务的性能嘛,后续量大了可能每天有十多万笔数据,问我现在每天要多久,我才发现 0.0 两三万条数据插入居然要7分钟(不完全是这个问题导致这么慢,还有Oracle插入语句的原因,下面会描述),,哈哈,笑不活了,简直就是Bug制造机,我就开始思考为什么会这么慢,肯定是批处理没生效,我就思考为什么会没生效?
版本3-标准写法(2023/10/26更新写法)
我们知道上面我们提到了BatchExecutor
执行器,我们知道每个SqlSession
都会拥有一个Executor
对象,这个对象才是执行 SQL 语句的幕后黑手,我们也知道Spring跟Mybatis整合的时候使用的SqlSession
是SqlSessionTemplate
,默认用的是ExecutorType.SIMPLE
,这个时候你通过自动注入获得的Mapper对象其实是没有开启批处理的
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
那么我们实际上是需要通过sqlSessionFactory.openSession(ExecutorType.BATCH)
得到的sqlSession
对象(此时里面的Executor是BatchExecutor
)去获得一个新的Mapper对象才能生效!!!
所以我们更改一下这个通用的方法,把MapperClass也一块传递进来
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import javax.annotation.Resource;
import java.util.List;
import java.util.function.BiFunction;
/**
* 该类用于提供批量处理 MyBatis 操作的工具方法
*/
@Slf4j
@Component
public class MybatisBatchUtils {
// 每次处理的最大条目数
private static final int BATCH_SIZE = 1000;
@Resource
private SqlSessionFactory sqlSessionFactory;
/**
* 对数据列表进行批量更新或插入操作。
* <p>
* 如果当前处于 Spring 管理的事务中,则该操作会在该事务中执行。
* 如果当前没有处于 Spring 管理的事务中,则该操作会在新的事务中执行。
*
* @param data 需要被处理的数据列表
* @param mapperClass MyBatis 的 Mapper 类
* @param function 用于定义如何处理每一个数据项的函数
* @return 影响的总行数
*/
@Transactional(rollbackFor = Exception.class)
public <T, U, R> int batchUpdateOrInsert(List<T> data, Class<U> mapperClass, BiFunction<T, U, R> function) {
if (data == null || data.isEmpty() || mapperClass == null) {
throw new IllegalArgumentException("Invalid input parameters. Cannot be null or empty");
}
SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
if (sqlSessionHolder != null) {
SqlSession sqlSession = sqlSessionHolder.getSqlSession();
//原生无法支持执行器切换,当存在批量操作时,会嵌套两个session的,优先commit上一个session
//按道理来说,这里的值应该一直为false。
sqlSession.commit(!transaction);
}
int totalAffectedRows = 0;
SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
if (!transaction) {
log.warn("SqlSession [" + batchSqlSession + "] Transaction not enabled");
}
try {
U mapper = batchSqlSession.getMapper(mapperClass);
for (int i = 0; i < data.size(); i++) {
function.apply(data.get(i), mapper);
// 每处理 BATCH_SIZE 条数据,或者已经处理到数据列表的最后一项,就执行一次刷新
if (i != 0 && (i % BATCH_SIZE == 0 || i == data.size() - 1)) {
batchSqlSession.flushStatements();
}
totalAffectedRows++;
}
batchSqlSession.flushStatements();
// 如果当前没有处于 Spring 管理的事务中,那么这个 commit 就会执行。否则,这个 commit 是无效的,将由 Spring 来执行实际的 commit。
batchSqlSession.commit(!transaction);
} catch (Exception e) {
batchSqlSession.rollback();
throw e;
} finally {
batchSqlSession.close();
}
return totalAffectedRows;
}
}
这里参考 mybatis-plus 的实现,会判断是否是事务环境,不是的话会强制提交,如果是事务环境的话,这个commit设置force值是无效的,这个在前面的官网截图中有提到。
感兴趣的可以看com.baomidou.mybatisplus.extension.toolkit.SqlHelper#executeBatch()
方法。
/**
* 执行批量操作
*
* @param entityClass 实体
* @param log 日志对象
* @param consumer consumer
* @return 操作结果
* @since 3.4.0
*/
@Deprecated
public static boolean executeBatch(Class<?> entityClass, Log log, Consumer<SqlSession> consumer) {
return executeBatch(sqlSessionFactory(entityClass), log, consumer);
}
@SneakyThrows
public static boolean executeBatch(SqlSessionFactory sqlSessionFactory, Log log, Consumer<SqlSession> consumer) {
SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
if (sqlSessionHolder != null) {
SqlSession sqlSession = sqlSessionHolder.getSqlSession();
//原生无法支持执行器切换,当存在批量操作时,会嵌套两个session的,优先commit上一个session
//按道理来说,这里的值应该一直为false。
sqlSession.commit(!transaction);
}
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
if (!transaction) {
log.warn("SqlSession [" + sqlSession + "] Transaction not enabled");
}
try {
consumer.accept(sqlSession);
//非事务情况下,强制commit。
sqlSession.commit(!transaction);
return true;
} catch (Throwable t) {
sqlSession.rollback();
Throwable unwrapped = ExceptionUtil.unwrapThrowable(t);
if (unwrapped instanceof PersistenceException) {
MyBatisExceptionTranslator myBatisExceptionTranslator
= new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true);
Throwable throwable = myBatisExceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
if (throwable != null) {
throw throwable;
}
}
throw ExceptionUtils.mpe(unwrapped);
} finally {
sqlSession.close();
}
}
如果你引入了MyBatisPlus那你就可以直接用这个类的方法都行,不需要自己在造轮子了。
使用案例:
batchUtils.batchUpdateOrInsert(数据集合, xxxxx.class, (item, mapper实例对象) -> mapper实例对象.insert方法(item));
没有开启事务导致批处理失效
经测试,工具类上如果没有标记事务注解,在 flushStatements 的时候会很慢,这是因为在Spring中,如果没有开启事务,每个数据库操作都将作为单独的事务来处理,这就意味着每个操作结束后都要进行提交。这导致了大量的数据库I/O操作,对每个操作都需要网络延迟和数据库的提交时间,这会影响性能。
另外我发现如果MySQL的JDBC驱动参数增加rewriteBatchedStatements=true
可以显著提高批处理操作的性能,结合事务会更快。
附:Oracle批量插入优化
我们都知道Oracle主键序列生成策略跟MySQL不一样,我们需要弄一个序列生成器,这里就不详细展开描述了,然后Mybatis Generator生成的模板代码中,insert的id
是这样获取的
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Long">
select XXX.nextval from dual
</selectKey>
如此,就相当于你插入1万条数据,其实就是insert和查询序列合计预计2万次交互,耗时竟然达到10s多。我们改为用原生的Batch插入,这样子的话,只要500多毫秒,也就是0.5秒的样子
<insert id="insert" parameterType="user">
insert into table_name(id, username, password)
values(SEQ_USER.NEXTVAL,#{username},#{password})
</insert>
最后这样一顿操作,批处理 + 语句优化一下,这个业务直接从7分多钟变成10多秒,完美解决,撒花庆祝~