Java JPA批处理插入不会提高性能

Java JPA批处理插入不会提高性能,java,postgresql,spring-boot,jpa,Java,Postgresql,Spring Boot,Jpa,我希望通过JPA批处理插入来提高postgresql插入的性能 我正在使用: spring boot starter数据jpa 2.1.3.1版本 postgresql 42.2.5(jdbc驱动程序) 数据库是PostgreSQL 9.6.2 我已经成功地激活了JPA的批处理插入,但性能并没有任何改善 我在我的实体中使用@GeneratedValue(strategy=GenerationType.SEQUENCE) 我在jdbc连接字符串中使用了reWriteBatchedInsert

我希望通过JPA批处理插入来提高postgresql插入的性能

我正在使用:

  • spring boot starter数据jpa 2.1.3.1版本
  • postgresql 42.2.5(jdbc驱动程序)
  • 数据库是PostgreSQL 9.6.2
我已经成功地激活了JPA的批处理插入,但性能并没有任何改善

  • 我在我的实体中使用
    @GeneratedValue(strategy=GenerationType.SEQUENCE)

  • 我在jdbc连接字符串中使用了
    reWriteBatchedInserts=true

  • 我设置了以下属性:
  • 我使用saveAll(collection)方法
  • 我试着在每一批之后冲洗和清洁我的整个管理器
  • 我尝试了批量大小为100和1000的产品,每批产品都会刷新,没有明显的变化
我可以在日志中看到hibernate确实使用批插入,但不确定我的数据库是否使用(我正在尝试获取日志,文件夹权限待定)


@服务
@配置
@交易的
公共类SecteUrGeographyQueServiceImpl实现SecteUrGeographyQueService{
私有静态最终记录器Logger=LoggerFactory.getLogger(SecteUrGeographyQueServiceImpl.class);
@值(${spring.jpa.properties.hibernate.jdbc.batch_size})
私有整数批量大小;
@持久上下文
私人实体管理者实体管理者;
@自动连线
私人部门地理位置查询部门地理位置查询;
@凌驾
公共列表SaveAllSecteUrGeographiquesOS(列表SecteUrGeographiques){
logger.warn(“批大小:“+this.batchSize”);
最终列表模板列表=新的ArrayList();
最终列表savedList=newarraylist();
对于(int i=0;i
我的存储库实现是:

org.springframework.data.jpa.repository.JpaRepository<SecteurGeographique, Long>
在插入my 16073实体后的日志中(此测试不包括刷新):

请注意,这只是一个表,没有约束/外键。表中只有简单的基本数据,没有什么特别之处

从日志来看,ot确实存在问题:

240144821872 nanoseconds spent executing <b>16073 JDBC statements</b>;
3778202166 nanoseconds spent executing 161 JDBC batches;
每次我都有4分钟30秒的执行时间。批量插入感觉非常棒。
我遗漏了什么/误解了什么?

在本地主机(v10.1.1)上使用postgresql server尝试了1000的批处理大小后,执行时间不到3秒。因此,这里似乎不应归咎于代码或配置

不幸的是,我无法调查为什么远程postgresql(托管在AWS上)花费了这么多时间,但我只能断定这是一个网络或数据库问题

到今天为止,我无法访问postgresql远程日志,但如果您对如何查找postgresql实例有任何建议,我洗耳恭听

分批(1000)和冲洗+清洁的原木:

未进行批处理、冲洗或清理的日志:


此比较显示JDBC语句的总体执行时间增加了46%。

在本地主机(v10.1.1)上使用postgresql server尝试了1000的批处理大小后,执行时间不到3秒。因此,这里似乎不应归咎于代码或配置

不幸的是,我无法调查为什么远程postgresql(托管在AWS上)花费了这么多时间,但我只能断定这是一个网络或数据库问题

到今天为止,我无法访问postgresql远程日志,但如果您对如何查找postgresql实例有任何建议,我洗耳恭听

分批(1000)和冲洗+清洁的原木:

未进行批处理、冲洗或清理的日志:


这个比较显示JDBC语句的总体执行时间增加了46%。

您的存储库的实现是什么?@Zorglube我使用org.springframework.data.jpa.repository.JpaRepository。我相应地更新了这个问题。如果您想让insert更快,特别是16073 insert更快,最好使用本机参数化SQLry:
插入到SecteurGeographique(id,…)值(:id,…)
。如果你想走得快一点,在插入过程中获得
secteurogeographique\u PK
的位置来伪造你的
id
,并在插入后推新位置
secteurogeraphique\u PK
。@Zorglube是的,我了解这两个位置,但我的问题是真正了解发生了什么。大多数使用我使用的解决方案见证了非常显著的性能提升,而我没有。您的存储库的实现是什么?@Zorglube我使用org.springframework.data.jpa.repository.JpaRepository。我相应地更新了问题。如果您希望插入更快,特别是16073插入,最好使用本机参数化SQL Q查询:
插入到SecteurGeographique(id,…)值(:id,…)
。如果你想走得快一点,在插入过程中获得
secteurogeographique\u PK
的位置来伪造你的
id
,并在插入后推新位置
secteurogeraphique\u PK
。@Zorglube是的,我了解这两个位置,但我的问题是真正了解发生了什么。大多数使用我使用的解决方案见证了非常显著的性能提升,而我没有任何改进。
spring.datasource.url=jdbc:postgresql://xx.xx.xx.xx:5432/bddname?reWriteBatchedInserts=true
spring.jpa.properties.hibernate.default_schema=schema
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.generate_statistics=true
13:31:40.882 [restartedMain] INFO  o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    15721506 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    121091067 nanoseconds spent preparing 16074 JDBC statements;
    240144821872 nanoseconds spent executing 16073 JDBC statements;
    3778202166 nanoseconds spent executing 161 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    4012929596 nanoseconds spent executing 1 flushes (flushing a total of 16073 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
240144821872 nanoseconds spent executing <b>16073 JDBC statements</b>;
3778202166 nanoseconds spent executing 161 JDBC batches;
15:32:17.612 [restartedMain] WARN  f.g.j.a.r.s.i.SecteurGeographiqueServiceImpl - BATCH SIZE : 100
15:36:46.206 [restartedMain] INFO  o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    15416324 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    105369002 nanoseconds spent preparing 16234 JDBC statements;
    262388696401 nanoseconds spent executing 16073 JDBC statements;
    3669253410 nanoseconds spent executing 161 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    3956493726 nanoseconds spent executing 161 flushes (flushing a total of 16073 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}

15:43:54.155 [restartedMain] WARN  f.g.j.a.r.s.i.SecteurGeographiqueServiceImpl - BATCH SIZE : 1000
15:48:22.335 [restartedMain] INFO  o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    15676227 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    111370586 nanoseconds spent preparing 16090 JDBC statements;
    265089247563 nanoseconds spent executing 16073 JDBC statements;
    599946208 nanoseconds spent executing 17 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    866452023 nanoseconds spent executing 17 flushes (flushing a total of 16073 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
16:20:52.360 [restartedMain] WARN  f.g.j.a.r.s.i.SecteurGeographiqueServiceImpl - BATCH SIZE : 1000
16:20:54.844 [restartedMain] INFO  o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    523125 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    44649191 nanoseconds spent preparing 16090 JDBC statements;
    1311557995 nanoseconds spent executing 16073 JDBC statements;
    204225325 nanoseconds spent executing 17 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    381230968 nanoseconds spent executing 17 flushes (flushing a total of 16073 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
16:57:34.426 [restartedMain] INFO  o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics {
    725069 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    55763008 nanoseconds spent preparing 32146 JDBC statements;
    2816525053 nanoseconds spent executing 32146 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    1796451447 nanoseconds spent executing 1 flushes (flushing a total of 16073 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}