【问题标题】:Jsf datatable optimization count queryjsf数据表优化计数查询
【发布时间】:2016-10-07 07:37:00
【问题描述】:

我有一个 JSF 数据表并显示一个包含延迟加载图像的实体。点击行查看详情后加载。

但我必须在数据表中显示图像计数。数据表使用分页,每页显示 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 秒才能加载,是否可以对此进行优化?也许并行加载每个警报的图像数量?这在 jsf 数据表中可以吗?

编辑:

休眠版本:4.2.17.Final

xhtml 文件中的行定义

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

【问题讨论】:

    标签: oracle hibernate jsf jpa


    【解决方案1】:

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

    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 对象。

    【讨论】:

    • 感谢您的回答。我很欣赏它。因为加载时间不是我的任务中明确定义的要求,所以我保留了它现在的样子。我会接受你的回答,因为异步请求将是要走的路
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2012-10-16
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多