Spring Boot MySQL 不批量插入

     2023-02-26     161

关键词:

【中文标题】Spring Boot MySQL 不批量插入【英文标题】:Spring Boot MySQL not batching inserts 【发布时间】:2019-02-09 05:12:24 【问题描述】:

关于这个问题,我已经应用了几乎所有在 SO 上发布的解决方案,但没有任何效果。

我的application.properties

spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?logger=com.mysql.jdbc.log.Slf4JLogger&rewriteBatchedStatements=true&profileSQL=true&autoReconnect=true&useSSL=false
spring.datasource.username=user
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.hibernate.jdbc.batch_size = 100
spring.jpa.hibernate.order_inserts   = true 
spring.jpa.hibernate.order_updates   = true

logging.level.root=info
logging.pattern.console=%dyyyy-MM-dd HH:mm:ss %highlight(%-5p) %gray(%c0::%M) - %m%n

我的EntityRepository

@Repository
public interface EntityRepository extends CrudRepository<Entity, Long>  

我的实体

@Data @Entity
public class Entity 

  @Id
  @GeneratedValue(generator = "generator")
  @GenericGenerator(name = "generator", strategy = "increment")
  private Long id;

  private Long attr;


以及调用存储库的简化代码:

int batchSize = 100;
List<Entity> batchEntities = new ArrayList<>();
for(Entity entity : entities) 
  batchEntities.add(entity);
  if(batchEntities.size() >= batchSize) 
    entityRepository.saveAll(batchEntities);
    batchEntities.clear();
  

我做了什么:

根据this SO question,我启用了profileSQL=true 选项,并且日志产生了几个单独的插入。另外,我在 SQL Server 上启用了全局日志记录,它也会生成单个插入序列。

根据this another SO question 和yet another SO question,我确保batch_size 设置在application.properties 文件中,虽然我没有父子关系,但我也尝试使用order_insertsorder_updates。另外,我启用了rewriteBatchedStatements=true 选项并使用saveAll(...) 方法。

我还尝试抛弃预制的CrudRepository 和我的自定义的,方法是在每个batchSize-th 坚持后冲洗。

以上没有任何帮助。

【问题讨论】:

spring.jpa.hibernate.order_updates 不会做任何事情(同样适用于其他属性)。而是使用spring.jpa.properties.hibernate.order_updates(同样适用于其他自定义休眠属性)。 @M.Deinum 谢谢!它现在完美运行。不敢相信我错过了。您想将您的评论改写成答案以便我接受吗? 【参考方案1】:

以下属性在 Spring Boot 中不存在。

spring.jpa.hibernate.jdbc.batch_size = 100
spring.jpa.hibernate.order_inserts   = true 
spring.jpa.hibernate.order_updates   = true

要添加自定义属性,请改用spring.jpa.properties 前缀。

spring.jpa.properties.hibernate.jdbc.batch_size = 100
spring.jpa.properties.hibernate.order_inserts   = true 
spring.jpa.properties.hibernate.order_updates   = true

应该做的伎俩。

另请参阅 how to configure JPA 上的 Spring Boot 文档。

【讨论】:

HIbernate 批量插入或更新在 Spring Boot 中不起作用

】HIbernate批量插入或更新在SpringBoot中不起作用【英文标题】:HIbernatebatchinsertorupdatenotworkinginspringboot【发布时间】:2017-12-3118:06:48【问题描述】:我需要在我的MySQL数据库中进行批量插入(近10000次)。我正在使用JPA/hibernate和spri... 查看详情

Spring Boot JPA 批量插入

】SpringBootJPA批量插入【英文标题】:SpringBootJPABulkinsert【发布时间】:2018-05-1720:10:37【问题描述】:我有3个实体父、子、子子。Parent是Child的parent,Child是SubChild的parent。我需要插入大约700个Parent对象。父级可以拥有50个子级对象... 查看详情

如何为批量插入配置spring boot和data jpa

】如何为批量插入配置springboot和datajpa【英文标题】:howtoconfigurespringbootanddatajpaforbatchinsert【发布时间】:2018-07-0803:38:55【问题描述】:我正在使用springboot和springdatajpawithhibernate,japrepository.save(List)花了20分钟将8000条记录保存到o... 查看详情

验证mybatis批量插入能否一次能插入1万条数据(代码片段)

...,后面就找时间做了下面这个实验。首先自己搭建了SpringBoot+Mybatis的项目测试的,搭建步骤如下1.搭建测试工程idea构建SpringBoot+MyBatis项目gitee上代码:https://gitee.com/AJiSun/SpringBoot-MyBatisFile->New->Project依赖:... 查看详情

为啥 Spring 在配置时并不总是使用批量插入/更新?

】为啥Spring在配置时并不总是使用批量插入/更新?【英文标题】:WhydoesSpringnotalwaysusebatchinsert/updatewhenconfigured?为什么Spring在配置时并不总是使用批量插入/更新?【发布时间】:2020-02-2522:08:28【问题描述】:在我基于SpringJpaReposi... 查看详情

Spring-data-jpa:批量插入不起作用

】Spring-data-jpa:批量插入不起作用【英文标题】:Spring-data-jpa:batchinsertsarenotworking【发布时间】:2021-03-0909:13:38【问题描述】:我使用这个article为我的一个实体使用spring-data-jpa实现批量插入。我正在使用MSSQLDB。我将主键生成策... 查看详情

mybatis批量插入的五种方式归纳总结(代码片段)

...><!--Mybatis依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-start 查看详情

Spring Data JPA saveAll 不进行批量插入

】SpringDataJPAsaveAll不进行批量插入【英文标题】:SpringDataJPAsaveAllnotdoingbatchinsert【发布时间】:2020-03-1823:45:54【问题描述】:所以我使用了一个简单的JpaRepository并调用了saveAll()方法。hibernate.jdbc.batch_size=500hibernate.order_inserts=truehib... 查看详情

Spring数据JPA存储库saveAll不生成批量插入查询

】Spring数据JPA存储库saveAll不生成批量插入查询【英文标题】:SpringdataJPArepositorysaveAllisnotgeneratingbulkinsertquery【发布时间】:2021-09-2206:45:45【问题描述】:我正在使用Springdata2.2.8和hibernate5.4.17.Final版本。数据库是oracle11g当我使用re... 查看详情

JPA/Hibernate 在 Spring Boot 应用程序中插入不存在的表

】JPA/Hibernate在SpringBoot应用程序中插入不存在的表【英文标题】:JPA/Hibernateinsertingintononexistingtableinspringbootapplication【发布时间】:2016-07-1413:31:14【问题描述】:我是springjpa/boot的新手。我在SpringBoot应用程序中使用JPA/Hibernate在Stud... 查看详情

Flyway Spring Boot应用程序在启动时不应用插入脚本

】FlywaySpringBoot应用程序在启动时不应用插入脚本【英文标题】:Flywayspringbootappdoesn\'tapplyinsertscriptuponstarting【发布时间】:2020-05-1607:32:21【问题描述】:我有一个spring-bootJPA应用程序,我正在尝试与flyway集成。我的应用程序启动... 查看详情

springboot使用resthighlevelclient批量插入(代码片段)

1、引入maven(注意版本要一致)<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency& 查看详情

连接 Spring Boot-MySql:不允许检索公钥

】连接SpringBoot-MySql:不允许检索公钥【英文标题】:ConnectionSpringBoot-MySql:PublicKeyRetrievalisnotallowed【发布时间】:2019-12-1609:37:48【问题描述】:我在今年6月初运行了我的一个应用程序,没有出现任何问题。今天再次尝试后,我在... 查看详情

MySQL 快速批量插入

】MySQL快速批量插入【英文标题】:MySQLQuickBulkInserts【发布时间】:2009-07-0802:01:31【问题描述】:我们正在为客户开发“搜索报告”功能。其中一项要求是他们可以查看特定结果并查看哪些搜索词会导致该结果。我们的search_result... 查看详情

Spring Boot 1.5.2.RELEASE:JDBC 模板仅在记录不存在时插入

】SpringBoot1.5.2.RELEASE:JDBC模板仅在记录不存在时插入【英文标题】:SpringBoot1.5.2.RELEASE:JDBCtemplateInsertonlyifrecorddoesntexist【发布时间】:2017-05-1022:27:35【问题描述】:我有以下groovy方法,它检查记录是否已经存在,并且仅在记录不... 查看详情

Spring Data JPA - 并发批量插入/更新

】SpringDataJPA-并发批量插入/更新【英文标题】:SpringDataJPA-concurrentBulkinserts/updates【发布时间】:2016-07-2106:27:00【问题描述】:目前我开发了一个SpringBoot应用程序,它主要从消息队列(约5个并发消费者)中提取产品评论数据并将... 查看详情

BoneCP 抛出“SQLException:连接已关闭!”批量插入 MySQL 时

...014-05-0420:01:12【问题描述】:我的任务是使用BoneCP与jOOQ和Spring建立一个项目,但我在这样做时遇到了一些困难。对我的MySQL数据库 查看详情

JPA 批量插入不会提高性能

...PA批量插入来提高我的postgresql插入的性能。我正在使用:spring-boot-starter-data-jpa2.1.3.RELEASEpostgresql42.2.5(jdbc驱动程序)。数据库为PostgreSQL9.6.2我已 查看详情