MyBatis 批量插入不要再再乱用 foreach 了,这里5000 条数据花了 14 分钟

最近我们的项目遇到了一个问题,即一个耗时较长的Job导致了CPU占用过高的问题。我们经过排查发现,问题的主要原因在于往MyBatis中批量插入数据时所耗费的时间。具体来说,mapper configuration是用foreach循环来实现的,其大致代码如下所示(由于项目保密,以下代码均为自己手写的demo代码)

这个方法提升批量插入速度的原理是,将传统的: 

转化为: 

在MySql Docs中也提到过这个trick,如果要优化插入速度时,可以将许多小型操作组合到一个大型操作中。理想情况下,这样可以在单个连接中一次性发送许多新行的数据,并将所有索引更新和一致性检查延迟到最后才进行。

虽然这个foreach看起来没有问题,但是经过项目实践发现,当表的列数较多(20+),以及一次性插入的行数较多(5000+)时,整个插入的耗时十分漫长,达到了14分钟,这是不能忍的。在资料中也提到了一句话:

Of course don't combine ALL of them, if the amount is HUGE. Say you have 1000 rows you need to insert, then don't do it one at a time. You shouldn't equally try to have all 1000 rows in a single query. Instead break it into smaller sizes.

它强调,当插入数量很多时,不能一次性全放在一条语句里。可是为什么不能放在同一条语句里呢?这条语句为什么会耗时这么久呢?我查阅了资料发现:

Insert inside Mybatis foreach is not batch, this is a single (could become giant) SQL statement and that brings drawbacks:

  • some database such as Oracle here does not support.

  • in relevant cases: there will be a large number of records to insert and the database configured limit (by default around 2000 parameters per statement) will be hit, and eventually possibly DB stack error if the statement itself become too large.

Iteration over the collection must not be done in the mybatis XML. Just execute a simple Insertstatement in a Java Foreach loop. The most important thing is the session Executor type.

Unlike default ExecutorType.SIMPLE, the statement will be prepared once and executed for each record to insert.

从资料中可知,默认执行器类型为Simple,会为每个语句创建一个新的预处理语句,也就是创建一个PreparedStatement对象。在我们的项目中,会不停地使用批量插入这个方法,而因为MyBatis对于含有<foreach>的语句,无法采用缓存,那么在每次调用方法时,都会重新解析sql语句。

Internally, it still generates the same single insert statement with many placeholders as the JDBC code above.

MyBatis has an ability to cache PreparedStatement, but this statement cannot be cached because it contains <foreach /> element and the statement varies depending on the parameters. As a result, MyBatis has to 1) evaluate the foreach part and 2) parse the statement string to build parameter mapping [1] on every execution of this statement.

And these steps are relatively costly process when the statement string is big and contains many placeholders.

[1] simply put, it is a mapping between placeholders and the parameters.

根据上述资料可以看出,耗时主要花费在循环操作上。由于我使用foreach循环后有5000+个values,因此这个PreparedStatement特别长,包含了很多占位符,而占位符和参数的映射耗费了大量时间。此外,根据相关资料的研究表明,values的增加与所需解析时间呈指数型增长,因此这个循环操作需要更多的时间来完成。

 

所以,如果非要使用 foreach 的方式来进行批量插入的话,可以考虑减少一条 insert 语句中 values 的个数,最好能达到上面曲线的最底部的值,使速度最快。一般按经验来说,一次性插20~50行数量是比较合适的,时间消耗也能接受。

重点来了。上面讲的是,如果非要用<foreach>的方式来插入,可以提升性能的方式。而实际上,MyBatis文档中写批量插入的时候,是推荐使用另外一种方法。(可以看 http://www.mybatis.org/mybatis-dynamic-sql/docs/insert.html 中 Batch Insert Support 标题里的内容)

 

即基本思想是将 MyBatis session 的 executor type 设为 Batch ,然后多次执行插入语句。就类似于JDBC的下面语句一样。 

经过我们的试验,我们可以得出以下结论:使用 ExecutorType.BATCH 的插入方式比 <foreach> 的插入方式效率更高。我们做了一个测试,在使用 ExecutorType.BATCH 的情况下,不到 2 秒就能全部插入完成。相比之下,使用 <foreach> 的插入方式需要将每次插入的记录控制在 20~50 左右,才能保证插入的效率。

虽然 ExecutorType.BATCH 的插入方式效率更高,但是需要注意的是,它也有一些限制。在进行批量操作的时候,需要保证每个操作都是同一个数据库的连接,否则可能会报错。此外,ExecutorType.BATCH 的插入方式也只适用于一些数据量比较大的场景,如果数据量较小,使用 <foreach> 的插入方式就足够了。

因此,我们建议在进行批量插入时,优先考虑使用 ExecutorType.BATCH 的插入方式。这不仅可以有效提高插入效率,还可以保证数据的完整性。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永钊源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值