2010-06-12 62 views
0

我正在使用Hibernate中的二級緩存(啓用EhCache)進行基準測試,但似乎並未提高性能。實際上,執行查詢的時間略有增加。使用EhCache for session.createCriteria(...)。list()

查詢是:

session.createCriteria(MyEntity.class).list(); 

的實體是:

@Entity 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class MyEntity { 
    @Id 
    @GeneratedValue 
    private long id; 

    @Column(length=5000) 
    private String data; 

    //---SNIP getters and setters--- 
} 

我的hibernate.cfg.xml是:

<!-- all the normal stuff to get it to connect & map the entities plus:--> 
<property name="hibernate.cache.region.factory_class"> 
    net.sf.ehcache.hibernate.EhCacheRegionFactory 
</property> 

的myEntity所表包含約2000行。

問題是,在添加緩存之前,上面列出所有實體的查詢平均需要65毫秒。緩存之後,它們平均需要74毫秒。有什麼我失蹤?是否還有其他需要完成的工作可以提高性能?

回答

0

默認情況下,查詢結果是而不是緩存,只有實體被緩存。因此,每次運行查詢時,SQL仍會發出,它將返回與查詢匹配的實體的ID。然後從緩存中檢索那些實體(如果存在)。

如果你想查詢結果被緩存,你需要turn that on explicitly

+0

謝謝,我啓用了查詢緩存,它絕對有所作爲。查詢從未緩存的65毫秒變爲22毫秒,然後恢復。 – 2010-06-12 17:05:39

+0

@James:對於更復雜的查詢,差異應該更明顯。 – skaffman 2010-06-12 17:44:00