spring 数据保存很慢

spring data saveAll very slow

我正在使用 spring data saveAll 在 Oracle 数据库中保存 3500 条记录,但执行速度非常慢,有没有办法进行批量插入或任何其他快速方法

 noteRepository.saveAll(noteEntityList);//<- this one is slow for 3000 records

提前致谢

saveAll默认不创建批处理,需要开启批处理。 您需要设置以下属性以启用批处理

spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true (if inserts)
OR
spring.jpa.properties.hibernate.order_updates=true (if updates)

第一个 属性 批量收集交易,第二个 属性 收集按实体分组的报表。

查看此线程了解更多详情

另外,如果你想做批量插入,确保如果你的 table 有一个 auto-incremented 列(比如 PK),它设置为序列(不是标识) 并且 allocationSize (Java) 和 increment_by 值(数据库序列)设置为您尝试保留的批量大小。不要将这些值设置为 1,否则插入仍然会很慢,因为 JPA 需要继续返回数据库以从序列中获取下一个值。