How To Optimize Batch Inserts of Parent-Child Relationships In MySQL
Description: Let's suppose that we have a one-to-many relationship between Author
and Book
entities. When we save an author, we save his books as well thanks to cascading all/persist. We want to create a bunch of authors with books and save them in the database (e.g., a MySQL database) using the batch technique. By default, this will result in batching each author and the books per author (one batch for the author and one batch for the books, another batch for the author and another batch for the books, and so on). In order to batch authors and books, we need to order inserts as in this application.
Key points:
Beside all setting specific to batching inserts in MySQL, we need to set up in application.properties
the following property: spring.jpa.properties.hibernate.order_inserts=true
Example without ordered inserts: