top of page
Search
Writer's pictureLeonard Anghel

How To Optimize Batch Inserts of Parent-Child Relationships

Updated: May 10, 2020

Motivation:

Parent-child batch inserts can be optimized by ordering inserts. The figure below represents a time trend performance graphic of ordered (green) via non-ordered (blue) inserts. NxM represents N parents having M children (for a detailed explanation consider my book, Spring Boot Persistence Best Practices):


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 afterward books, we need to order inserts as follows.


Key points:

Besides all the settings specific to batching inserts in MySQL, we set up in application.properties the property: spring.jpa.properties.hibernate.order_inserts:

Testing time:

  • no ordering (80 JDBC batches):

  • ordering enabled (17 JDBC batches)

Tam Ta Da Dam! :) The complete application is available on GitHub.


Note: This application uses a single transaction for the entire batch process. This can lead to a long-running transaction and, a potential rollback, will rollback all batches. For avoiding these issues consider a batch per transaction as in this application.


If you need a deep dive into the performance recipes exposed in this repository then I am sure that you will love my book "Spring Boot Persistence Best Practices".



2,341 views0 comments

Recent Posts

See All

How To Bulk Updates

Motivation: This article is useful if you need a fast way to update a significant amount of data in the database. Bulk operations...

Comments


bottom of page