实体间的java优化关系

6tr1vspr  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(385)

我有这样的实体:

@Entity
@Table(name = "test")
public class TestEntity {

@ElementCollection
@Column(name = "record_type_id", nullable = false)
@CollectionTable(name = "test_entity_record_type", joinColumns = @JoinColumn(name = "test_entity_id"))
private Set<RecordType> recordTypes = new HashSet<>();

@ElementCollection
@Column(name = "record_type_id", nullable = false)
@CollectionTable(name = "test_entity_record_type1", joinColumns = @JoinColumn(name = "test_entity_id"))
private Set<RecordType> recordTypes1 = new HashSet<>();

@ElementCollection
@Column(name = "record_type_id", nullable = false)
@CollectionTable(name = "test_entity_record_type2", joinColumns = @JoinColumn(name = "test_entity_id"))
private Set<RecordType> recordTypes2 = new HashSet<>();
}

我在db里有1000个实体的记录。当我试图在日志中看到1000个实体时:
1000选择实体表
1000按测试实体id选择测试实体记录类型表
1000选择按测试实体id测试实体记录类型1表
1000按测试实体id选择测试实体记录类型2表
结果答题时间很长。
如何缩短响应时间?

roejwanj

roejwanj1#

您可以使用@batchsize注解:

@BatchSize(size = 1000)
private Set<RecordType> recordTypes = new HashSet<>();
pxyaymoc

pxyaymoc2#

我重新定义了相同的测试用例,以下配置是否比第一个配置(属性文件)的时间更短:

spring.jpa.open-in-view=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=false
spring.jpa.properties.hibernate.cache.use_query_cache=false
spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.jdbc.batch_size=1000
spring.jpa.properties.hibernate.jdbc.fetch_size=1000
spring.jpa.properties.hibernate.order_inserts=true
spring.datasource.maxActive=5
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true
spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true

spring.datasource.hikari.poolName=Hikari
spring.datasource.hikari.auto-commit=false
spring.datasource.hikari.data-source-properties.cachePrepStmts=true
spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250
spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048
spring.datasource.hikari.data-source-properties.useServerPrepStmts=true

希望这会有帮助。

相关问题