spring boot 2升级-spring boot data jpa saveall()非常慢

6yt4nkrj  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(735)

我将mysql与hibernate和spring boot data jpa(spring boot starter data jpa和mysql connector java)结合使用。最近我把我的spring boot项目从1.5升级到了2.0。用于保存iterable的spring data crudepository的api已从形式save()更改为saveall()。我对代码进行了更改,它可以正常工作,但速度非常慢:
插入10项->慢2倍(49ms->95ms)
插入100项->慢6倍(132ms->840ms)
插入1000个项目->慢10倍(792ms->8028ms)
插入10000个项目->慢15倍(4912毫秒->73542毫秒)
插入100000个项目->慢22倍(32042ms->712702ms)
我用一个空表测试了两个spring版本的插入。mysql服务器版本没有改变:5.7.21-mysql社区服务器(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

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

bpsygsoo

bpsygsoo1#

在application.properties中,设置spring.jpa.properties.hibernate.generate\u statistics=true

hibernate:
  generate_statistics: true

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

相关问题