Jsf数据表优化计数查询

时间:2016-10-07 07:37:00

标签: oracle hibernate jsf jpa

我有一个JSF数据表并替换包含延迟加载的图像的实体。单击该行后加载以查看详细信息。

但我必须在数据表中显示imagecount。数据表使用分页并每页显示15行。

我使用以下注释进行映射

    // Fetch type must be lazy due to out of memory error in eager mode
@OneToMany(cascade = { CascadeType.REMOVE, CascadeType.MERGE }, mappedBy = "alarm", fetch = FetchType.LAZY, orphanRemoval = true)
@OrderBy("unixtime ASC")
@LazyCollection(LazyCollectionOption.EXTRA) // allows .size() and .contains() without initializing the whole collection
private volatile Set<AlarmImage> alarmImages = new HashSet<AlarmImage>();

使用此方法获取图像数量

 /**
 * @return the number of alarm images in this alarm
 */
public Integer getCountAlarmImages() {
    // wont initalize whole collection because LazyCollectionOption.EXTRA
    return this.alarmImages.size();
}

这是休眠日志

[2016-10-07 08:51:13,095] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,190] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,282] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,375] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,466] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,559] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,651] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,744] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,836] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:13,930] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,022] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,117] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,210] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,302] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?
[2016-10-07 08:51:14,395] [WebContainer : 1] DEBUG org.hibernate.SQL [] logStatement - select count(ID) from ALARM_IMAGE where ALARM_FK =?

我的问题是分页需要平均1.5秒加载才有可能优化这个?也许paralell加载每个警报的图像数量?这可能在jsf数据表中吗?

编辑:

Hibernate版本:4.2.17.Final

xhtml文件中的行定义

<p:column>
    <h:outputText value="#{alarm.countAlarmImages}" />
</p:column>

1 个答案:

答案 0 :(得分:0)

不要认为有可能“并行加载”这些,它可能已经在做了,但我确实有一些想法给你:

1。 保留一个@Formula的字段,当你获取所有警报时,你将运行该字段(@Formula将成为您已经在进行的查询的一部分,而不是新的查询)

@Formula("(select count(1) from ALARM_IMAGE AI where AI.ALARM_FK = _this.ALARM_PK)") 
private Int alarmImageCount;

不能完全记住当前实体在@Formula中引用的是什么,查看hibernate生成的sql你会想出来的,但我认为它通常是_this

2。 看起来你正在使用primefaces,你可以做一些像显示新页面,但保持图像计数丢失(或表示他们正在加载),直到他们完全加载,然后在他们初始化时ajax他们。绝对可以,但需要一些时间。如果您沿着这条路走下去,请查看p:remoteCommand组件和RequestContext对象。