Backend Development 15 min read

Optimizing MyBatis-Plus Batch Insert Performance with rewriteBatchedStatements and Multithreading

This article explains how to dramatically speed up MyBatis-Plus batch inserts by enabling the rewriteBatchedStatements JDBC property, creating custom batch insert/update methods, and applying multithreading, providing code examples, performance test results, and practical recommendations for handling large data synchronizations.

Top Architect
Top Architect
Top Architect
Optimizing MyBatis-Plus Batch Insert Performance with rewriteBatchedStatements and Multithreading

When synchronizing tens of thousands of base records between systems, using MyBatis-Plus's default saveBatch() can take many seconds because the JDBC URL lacks the rewriteBatchedStatements=true property.

Adding the property to the connection string, e.g. jdbc:mysql://host/db?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&rewriteBatchedStatements=true , reduces the insertion time of 10,000 rows to a few hundred milliseconds.

The article then demonstrates the default saveBatch() implementation, showing the source code of the method and a unit test that inserts 200,000 rows, which takes about 10 seconds.

To achieve better performance, a custom RootMapper interface extending BaseMapper is defined with insertBatch and updateBatch methods, followed by custom injector classes InsertBatchColumn and UpdateBatchColumn that generate the appropriate SQL for bulk operations.

The custom injector MysqlInjector registers these methods, and a configuration class exposes it as a Spring bean.

Using the custom batch insert with a batch size of 5,000 rows reduces the insertion time to 4–5 seconds, while the same data inserted with the default saveBatch() (with the JDBC property enabled) also achieves similar speed.

Further performance gains are obtained by combining saveBatch() with multithreading: a fixed thread pool of five threads processes the data in parallel, bringing the total time down to about 3 seconds.

Finally, the article shows a multithreaded approach using the custom insertBatch method, which completes the 200,000‑row insertion in roughly 2 seconds, and compares all results, concluding that enabling rewriteBatchedStatements and applying multithreading provide the most efficient solution.

Overall, the guide provides practical code snippets, performance measurements, and recommendations for developers needing fast bulk data operations in Java backend projects.

Javaperformance optimizationdatabasemultithreadingMyBatis-Plusbatch insert
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.