据我所知,我已经完成了我的家庭作业。我知道自动生成的密钥不能插入。但是对于作为UUID的主键,我该如何解决这个问题呢?
我有这个主键配置
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private UUID id;我希望这不被认为是一种“自动”策略,但它似乎是,因为我的实体仍然是单独插入的,用10k插入破坏了我的性能。为什么这不能被认为是序列策略?UUID无论如何都保证是唯一的,所以不应该有任何技术上的东西,仅仅是生成一些UUID并批量插入它们。
我的application.properties中确实有适当的设置:
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而且我确实使用了PagingAndSortingRepository的方法saveAll来保存实体。
一些日志:
03:12:01.122 | INFO | o.h.e.i.StatisticalLoggingSessionEventListener | Session Metrics {
35000 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
1533100 nanoseconds spent preparing 8 JDBC statements;
49660800 nanoseconds spent executing 5 JDBC statements;
5137013700 nanoseconds spent executing 401 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
6933591600 nanoseconds spent executing 2 flushes (flushing a total of 60152 entities and 40294 collections);
244354300 nanoseconds spent executing 5 partial-flushes (flushing a total of 60009 entities and 60009 collections)
}这很有趣,因为它确实提到了批处理。但是,在启用spring.jpa.show-sql=true的情况下,我看到了数千个日志行,如下所示:
Hibernate: insert into ingestion_id_mapping (derive_ingestion_id, ingestion_id, project_id, metric_id, subject_id) values (?, ?, ?, ?, ?)
这似乎与批处理统计数据中的数字无关。
发布于 2021-06-02 23:35:06
复制您的样例as follows,假设设置了reWriteBatchedInserts jdbc连接属性,它实际上看起来像预期的那样工作:
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres?reWriteBatchedInserts=true例如,使用spring.jpa.properties.hibernate.jdbc.batch_size=8,当通过repository.saveAll插入8个新创建的实体时,相应的postgres日志将如下所示
postgres_db_1 | 2021-06-02 15:22:54.327 UTC [386] LOG:
execute <unnamed>: /* insert org.demo.batchinsert.UuidEntity */
insert into uuid_entity (id) values ($1),($2),($3),($4),($5),($6),($7),($8)
...发布于 2021-06-03 15:08:20
实际上,您不能仅仅通过查看hibernate日志来判断批处理是否有效。
您必须使用像P6Spy这样的工具来代理JDBC Driver或Datasource。然后,您可能会看到批处理实际上是有效的。
您可以使用这个装饰器:https://github.com/gavlyukovskiy/spring-boot-data-source-decorator。
现在,如果您希望加快数据库端的批处理速度,也可以使用属性reWriteBatchedInserts。
https://stackoverflow.com/questions/67798674
复制相似问题