Performance comparison between mybaits Batch ExecutorType and for_each Xml

人盡茶涼 提交于 2020-06-01 07:22:46

问题


I have a list of records to be inserted into DB using my baits. Previously, my code is something like:

for(Item item :  items){
    sqlSession.insert("insert", item);
}

Using this method works but I find there is dynamical incremental DiskIO at Mysql server, due to number of items.

As I have little access to MySql configuration, and hope to resolve this high disk io issue, I find some possible solutions:

  1. using ExecutorType.BATCH for sqlSession
  2. insert multiple values within a single insert statement, such as: insert into Item values <foreach collection="list" item="item" index="index" separator=","> (#{item.a}, #{item.b}...) </foreach>

Does anyone has idea which one is suitable to solve my high diskIO problem and what the main difference between these two solution? thanks.


回答1:


Although it is difficult to predict the effect on the disk I/O of your server, method 1 (using ExecutorType.BATCH) is the recommended way.
It basically inserts a certain number of items at a time.
As it internally uses JDBC's batch operation API which is designed to handle many rows, you could benefit from optimizations by the driver.

Notes:

  • Finding a proper 'batch size' is important. Please see the example code in another answer.
  • In case of MySQL, adding rewriteBatchedStatements=true to the connection URL usually improves the performance significantly.

Method 2 (multi-row insert) executes a single statement binding all items at once.
It could cause memory issues.



来源:https://stackoverflow.com/questions/56034380/performance-comparison-between-mybaits-batch-executortype-and-for-each-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!