繁体   English   中英

Spring 数据 jpa 批量执行所有插入操作

[英]Spring data jpa batch execute all inserts operations

我需要在 mysql 中插入大量数据(大约 100k)然后我尝试使用 Spring 数据 Jpa 批量插入,为此我使用了一个包含 30 条记录的简单示例。

首先是删除@GeneratedValue os,我的实体端实现了Persistable,在插入之前不需要select:

@Entity
public class User implements Persistable {

    @Id
    private Integer id;
    // properties here...

然后,在我的 application.yml 上:

spring:
  jpa:
    properties:
      hibernate.jdbc.batch_size: 30
      hibernate.generate_statistics: true
    show-sql: true
    hibernate:
      ddl-auto: validate
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db?cachePrepStmts=true&reWriteBatchedInserts=true
    // user and password

我有一个简单的存储库:

public interface UserRepository extends JpaRepository<User, Integer> { }

和插入方法:

public void process() {

        List<User> users = new ArrayList<>();

        for (int i = 1 ; i <= 30; i++) {
            User user = new User();
            user.setId(i);
            // set properties

            users.add(user);

            if(i % 30 == 0) {
                userRepository.saveAll(users);
                users.clear();
            }
        }
    }

然后我认为正确的只是 1 批操作,但我有 29 条语句:

    1745893 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    3524622 nanoseconds spent preparing 30 JDBC statements;
    68290171 nanoseconds spent executing 29 JDBC statements;
    215125391 nanoseconds spent executing 1 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    240389888 nanoseconds spent executing 1 flushes (flushing a total of 29 entities and 29 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

有什么想法吗?

谢谢!

测试下一个属性:

spring.jpa.properties.hibernate.order_inserts=true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM