@PrimaryKeyJoinColumn存在性能问题。通过Hibernate3 / Spring2.5从DB中获取列表(425行)大约需要12秒。太长了。我不明白为什么。
Umsuser Taetigkeit:
@Entity
@Table(name="UMSUSER_TAETIGKEIT")
public class UmsuserTaetigkeit implements Serializable {
private static final long serialVersionUID = 1L;
@GenericGenerator(name = "taetigkeit_generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "taetigkeit"))
@Id @GeneratedValue(generator="taetigkeit_generator")
@Column(name="TAETIGKEIT_ID")
private long taetigkeitId;
@Column(name="EDITOR_USER_ID")
private String editorUserId;
//TODO: check date/time format
@Temporal(TemporalType.TIMESTAMP)
@Column(name="TASK_TIMESTAMP")
private Date taskTimestamp;
@Column(name="UMS_CONFIGURATION")
private BigDecimal umsConfiguration;
@Column(name="UMS_ROLE")
private String umsRole;
@Column(name="UMS_WERK")
private String umsWerk;
@OneToOne(cascade={CascadeType.ALL})
@PrimaryKeyJoinColumn
private Taetigkeit taetigkeit;
//getter and setter
public static String fetchJoin() {
return " left join fetch o.taetigkeit ";
}
}
Taetigkeit:
@Entity
@Table(name="TAETIGKEIT")
public class Taetigkeit implements Serializable {
private static final long serialVersionUID = 1L;
@GenericGenerator(name = "taetigkeit_generator", strategy = "sequence-identity", parameters = @Parameter(name = "sequence", value = "TAETIGKEIT_SEQ"))
@Id @GeneratedValue(generator="taetigkeit_generator")
@Column(name="TAETIGKEIT_ID")
private long taetigkeitId;
@Column(name="PUBLISH_STATE")
private String publishState = "N";
@Lob
@Column(name="TAETIGKEIT_XML")
private String taetigkeitXml = "";
//setter and getter
}
方法findAll:
@SuppressWarnings("unchecked")
@Override
public List<T> findAll(final String fetchJoin,final String... orderby) {
final StringBuffer queryString = new StringBuffer(
"SELECT "+ INSTANCE +" from ");
queryString.append(type.getSimpleName()).append(INSTANCE_SPACE);
if(fetchJoin != null) {
queryString.append(fetchJoin);
}
if(orderby.length > 0) {
queryString.append(" order by ");
boolean addComma = false;
for(String arg : orderby) {
arg = INSTANCE + "." + arg;
if(addComma) {
queryString.append(",");
} else {
addComma = true;
}
queryString.append(arg);
}
}
final Query query = this.em.createQuery(queryString.toString());
return (List<T>) query.getResultList();
}
JUnit测试方法:
@Test
public void testFinAll_GOOD_CASE() {
List<UmsuserTaetigkeit> findAll = umsuserTaetigkeitDao.findAll(UmsuserTaetigkeit.fetchJoin());
assertNotNull(findAll);
System.out.println(findAll.size());
}
SQL语句:
Hibernate: select umsusertae0_.TAETIGKEIT_ID as TAETIGKEIT1_3_0_, taetigkeit1_.TAETIGKEIT_ID as TAETIGKEIT1_6_1_, umsusertae0_.EDITOR_USER_ID as EDITOR2_3_0_, umsusertae0_.TASK_TIMESTAMP as TASK3_3_0_, umsusertae0_.UMS_CONFIGURATION as UMS4_3_0_, umsusertae0_.UMS_ROLE as UMS5_3_0_, umsusertae0_.UMS_WERK as UMS6_3_0_, taetigkeit1_.PUBLISH_STATE as PUBLISH2_6_1_, taetigkeit1_.TAETIGKEIT_XML as TAETIGKEIT3_6_1_ from UMSUSER_TAETIGKEIT umsusertae0_ left outer join TAETIGKEIT taetigkeit1_ on umsusertae0_.TAETIGKEIT_ID=taetigkeit1_.TAETIGKEIT_ID
更新
执行计划
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3626287948
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 425 | 54400 | 15 (7)| 00:00:01 |
|* 1 | HASH JOIN OUTER | | 425 | 54400 | 15 (7)| 00:00:01 |
| 2 | TABLE ACCESS FULL| UMSUSER_TAETIGKEIT | 425 | 15725 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TAETIGKEIT | 644 | 58604 | 11 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("UMSUSERTAE0_"."TAETIGKEIT_ID"="TAETIGKEIT1_"."TAETIGKEIT_ID"(+))
15 rows selected