查询JOIN FETCH性能问题 [英] Query with JOIN FETCH performance problem
问题描述
我有hibernate查询性能的问题,我无法弄清楚。在下面的代码片段中,我需要选择具有至少一个映射和过滤映射的实体。我正在使用FETCH JOIN来加载仅过滤的映射。
但在这种情况下,我的查询存在性能问题。 Hibernate说警告日志:
I have problem with hibernate query performance which I can't figure out. In code snippet below I need select entities with at least one mapping and filtered mapping. I'm using FETCH JOIN for this to load only filtered mappings. But in that case I have performance problems with query. Hibernate says warning log :
org.hibernate.hql.ast.QueryTranslatorImpl
- 使用collection fetch指定的firstResult / maxResults;申请
内存!
org.hibernate.hql.ast.QueryTranslatorImpl - firstResult/maxResults specified with collection fetch; applying in memory!
当我省略FETCH JOIN并且只留下JOIN查询时很快。但结果我将所有映射加载到实体,这对我来说是不可接受的状态。有没有办法提高查询性能?映射表中有很多行。
When I omit FETCH JOIN and left only JOIN query is nice fast. But in result I have all mappings loaded to entity which is not acceptable state for me. Is there a way to boost query performance? There are a lot rows in mapping table.
HQL查询:
select distinct e from Entity
join fetch e.mappings as mapping
where e.deleted = 0 and e.mappings is not empty
and e = mapping.e and mapping.approval in (:approvals)
实体:
@Entity
@Table(name="entity")
class Entity {
...
@OneToMany(mappedBy="entity", cascade=CascadeType.REMOVE, fetch=FetchType.LAZY)
@OrderBy("created")
private List<Mapping> mappings = new ArrayList<Mapping>();
...
}
@Entity
@Table(name="mapping")
class Mapping {
public static enum MappingApproval {
WAITING, // mapping is waiting for approval
APPROVED, // mapping was approved
DECLINED; // mapping was declined
}
...
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="entity_id", nullable=false)
private Entity entity;
@Enumerated(EnumType.STRING)
@Column(name="approval", length=20)
private MappingApproval approval;
...
}
谢谢
推荐答案
增加JVM的内存后,情况要好得多。毕竟我在查询中没有使用FETCH结束。
after increasing memory for JVM things goes much better. After all I end with not using FETCH in queries.
这篇关于查询JOIN FETCH性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!