How To Optimize Batch Updates Of Parent-Child Relationships In MySQL
Description: Batch updates of entities with associations in MySQL.
Key points:
- in
application.propertiessetspring.jpa.properties.hibernate.jdbc.batch_size - in
application.propertiesset JDBC URL withrewriteBatchedStatements=true(optimization for MySQL, statements get rewritten into a single string buffer and sent in a single request) - in
application.propertiesset JDBC URL withcachePrepStmts=true(enable caching and is useful if you decide to setprepStmtCacheSize,prepStmtCacheSqlLimit, etc as well; without this setting the cache is disabled) - in
application.propertiesset JDBC URL withuseServerPrepStmts=true(this way you switch to server-side prepared statements (may lead to signnificant performance boost)) - for parent-child relationship with cascade all/persist (e.g. one-to-many, many-to-many) then consider to set up
spring.jpa.properties.hibernate.order_updates=trueto optimize the batching by ordering updates - before Hibernate 5, we need to set in
application.propertiesa setting for enabling batching for versioned entities during update and delete operations (entities that contains@Versionfor implicit optimistic locking); this setting is:spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true; starting with Hibernate 5, this setting should betrueby default
Output example for parent-child relationship:


