Spring Boot: Boost JPA Bulk Insert Performance by 100x.

  1. Change the number of records while inserting.
spring.jpa.properties.hibernate.jdbc.batch_size=30
cachePrepStmts=true
useServerPrepStmts=true
rewriteBatchedStatements=true
e.g
jdbc:mysql://localhost:3306/BOOKS_DB?serverTimezone=UTC&cachePrepStmts=true&useServerPrepStmts=true&rewriteBatchedStatements=true
for (int i = 0; i < totalObjects; i = i + batchSize) {
if( i+ batchSize > totalObjects){
List<Book> books1 = books.subList(i, totalObjects - 1);
repository.saveAll(books1);
break;
}
List<Book> books1 = books.subList(i, i + batchSize);
repository.saveAll(books1);
}
public class Book {
@Id
@GeneratedValue(strategy = SEQUENCE, generator = "seqGen")
@SequenceGenerator(name = "seqGen", sequenceName = "seq", initialValue = 1)
private Long id;
CREATE TABLE `seq` (
`next_val` bigint(20) DEFAULT NULL
);
INSERT INTO `seq` (`next_val`)
VALUES
(1);

--

--

--

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Designing for enterprise scale with Node.js

How To Make REST API`s .

React Fiber

What is JWT (Json web token)

Javascript Tutorial: Record Audio and Encode it to mp3

“Good to Follow” JavaScript Coding Practices

Top 5 Basic React JS Interview Questions and Answers

Circuit Breakers in Javascript & Opossum

This was legit just to grab your attention

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amrut Prabhu

Amrut Prabhu

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles

More from Medium

Spring WebFlux — Rest API with Annotated Controllers

Spring boot Initializr

Spring Boot Exception Handling — ResponseStatusException

Java, ElasticSearch and Spring Boot Framework tutorial