问题
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:
- using
ExecutorType.BATCH
forsqlSession
- 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