我有这样的实体:
@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<>();
}
我在数据库中有 1000 条实体记录。当我尝试获取 1000 个实体时,我在日志中看到:
- 1000 select到实体表
- 1000 根据test_entity_id 选择到test_entity_record_type 表
- 1000 根据test_entity_id 选择到test_entity_record_type1 表
- 1000 根据test_entity_id 选择到test_entity_record_type2 表
结果回答时间很长。
如何缩短响应时间?
最佳答案
您可以使用@BatchSize 注解:
@BatchSize(size = 1000)
private Set<RecordType> recordTypes = new HashSet<>();
关于java - 实体间的优化关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65303175/