我有一个每天都有分区的表。
当我通过 HQL/Criteria 使用查询时,在 Hibernate 中获得结果需要 240 秒。当我直接使用或通过 JDBC 连接使用时,相同的查询(由 Hibernate 创建的本机查询)需要 2 秒。
注意:本机查询从 postgres db(直接触发)花费了不到 2 秒的时间。
请回复可能是哪里的问题
这是具有命名查询的实体:
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name = "sms_info")
@NamedQueries(value = { @NamedQuery(name = "smsInfoforMsisdn", query = "SELECT si FROM SmsInfo si WHERE msisdn = :msisdn and sendStatus IN (0,5) order by lastSmsTrialTime desc") })
public class SmsInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "sms_id")
private Integer smsId;
@Column(name = "msisdn")
private String msisdn;
@Column(name = "vertical", length = 30)
private String vertical;
@Column(name = "sms_text", length = 1024)
private String smsText;
@Column(name = "sms_type", length = 255)
private String smsType;
@Column(name = "last_sms_trial_time")
private Timestamp lastSmsTrialTime;
@Column(name = "send_status")
private Integer sendStatus;
@Column(name = "unique_id")
private String uniqueId;
@Column(name = "source_of_generation")
private String sourceOfGeneration;
public Integer getSmsId() {
return smsId;
}
public void setSmsId(Integer smsId) {
this.smsId = smsId;
}
public String getMsisdn() {
return msisdn;
}
public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}
public String getVertical() {
return vertical;
}
public void setVertical(String vertical) {
this.vertical = vertical;
}
public String getSmsText() {
return smsText;
}
public void setSmsText(String smsText) {
this.smsText = smsText;
}
public String getSmsType() {
return smsType;
}
public void setSmsType(String smsType) {
this.smsType = smsType;
}
public Timestamp getLastSmsTrialTime() {
return lastSmsTrialTime;
}
public void setLastSmsTrialTime(Timestamp lastSmsTrialTime) {
this.lastSmsTrialTime = lastSmsTrialTime;
}
public Integer getSendStatus() {
return sendStatus;
}
public void setSendStatus(Integer sendStatus) {
this.sendStatus = sendStatus;
}
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
public void setSourceOfGeneration(String sourceOfGeneration) {
this.sourceOfGeneration = sourceOfGeneration;
}
public String getSourceOfGeneration() {
return sourceOfGeneration;
}
}
首先我使用命名查询,然后使用本机查询,如下所示:
Query smsinfo = session
.createSQLQuery("select smsinfo.sms_id as sms_id, smsinfo.last_sms_trial_time as last_sms_trial_time, smsinfo.msisdn as msisdn, smsinfo.send_status as send_status, smsinfo.sms_text as sms_text, smsinfo.sms_type as sms_type, smsinfo.source_of_generation as source_of_generation, smsinfo.unique_id as unique_id, smsinfo.vertical as vertical from sms_info smsinfo where smsinfo.msisdn=:msisdn and (smsinfo.send_status in (0 , 5)) order by smsinfo.last_sms_trial_time desc").addEntity(SmsInfo.class);
如果您需要更多信息,请告诉我。