Spring Boot 2 升级 - spring boot 数据 jpa saveAll() 很慢

DaT*_*aTa 7 java mysql hibernate spring-boot

我将 mysql 与 hibernate 和 spring 启动数据 jpa(spring-boot-starter-data-jpa 和 mysql-connector-java)一起使用。最近我将我的 spring boot 项目从 1.5 升级到 2.0。Spring 数据 CrudRepository 用于保存可迭代对象的 API 已从 save() 更改为 saveAll()。我对代码进行了更改,它可以工作,但速度很慢:

  • 插入 10 个项目 -> 慢 2 倍(49ms -> 95ms)
  • 插入 100 个项目 -> 慢 6 倍(132ms -> 840ms)
  • 插入 1000 项 -> 慢 10 倍(792 毫秒 -> 8028 毫秒)
  • 插入 10000 个项目 -> 慢 15 倍(4912ms -> 73542ms)
  • 插入 100000 个项目 -> 慢 22 倍(32042ms -> 712702ms)

我已经用一张空桌子测试了两个弹簧版本的插件。mysql 服务器版本没有变化:5.7.21 - MySQL Community Server (GPL)

我需要每天插入约 200 万个项目,因此这种放缓非常严重。这是我的配置:

spring.datasource.url = jdbc:mysql://localhost:3306/service?useSSL=false&rewriteBatchedStatements=true
spring.datasource.username = service
spring.datasource.password = service
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.maximum-pool-size=5

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
Run Code Online (Sandbox Code Playgroud)

有人知道更新发生了什么变化以及如何再次加快速度吗?

小智 0

在 application.properties 中设置 spring.jpa.properties.hibernate.generate_statistics = true

hibernate:
  generate_statistics: true
Run Code Online (Sandbox Code Playgroud)

https://www.baeldung.com/spring-data-jpa-batch-inserts