首页 > 解决方案 > Spring Boot - 批量更新单独插入记录而不是批量插入

问题描述

我正在尝试进行批量插入(spring boot,jpa,hibernate),当我查看生成的统计信息时,意识到它仍在将它们一个接一个地插入,而不是作为一个批处理。下面是配置和使用 JpaRepository 的 saveAll(collection) 方法。我唯一的预感是,我使用的是分配的主键而不是生成的主键,不确定这是否是原因。

我该如何解决这个问题?

特性

spring.jpa.properties.hibernate.jdbc.batch_size=10
spring.jpa.properties.hibernate.order_inserts = true
spring.jpa.properties.hibernate.order_updates = true
spring.jpa.properties.hibernate.batch_versioned_data=true
spring.jpa.properties.hibernate.generate_statistics=true
   @Id
   @Column(name = "SKU")
   private String sku;

统计数据:

43972157 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
128270518 nanoseconds spent preparing 10 JDBC statements;
1320687164 nanoseconds spent executing 8 JDBC statements;
126666002 nanoseconds spent executing 2 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
152061315 nanoseconds spent executing 2 flushes (flushing a total of 16 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

标签: javaspringhibernatespring-bootspring-data-jpa

解决方案


chunk调整步长和hibernate.jdbc.batch_size休眠状态会很好。


推荐阅读