Hibernate Cache

5,298 views
5,145 views

Published on

Hibernate cache explained in Chinese and also some query optimization techniques.

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,298
On SlideShare
0
From Embeds
0
Number of Embeds
74
Actions
Shares
0
Downloads
0
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Hibernate Cache

  1. 1. Hibernate cache 2007/03/16
  2. 2. abstract <ul><li>cache level </li></ul><ul><li>cache strategies </li></ul><ul><li>entity cache, collection cache, query cache </li></ul><ul><li>clear 2nd-level cache </li></ul><ul><li>hibernate statistics </li></ul><ul><li>session & transaction </li></ul><ul><li>open session in view </li></ul>
  3. 3. 快取的層次 (1/2) <ul><li>transaction layer cache </li></ul><ul><ul><li>交易範圍內的資料快取 ( 資料庫 / 應用程式交易 ) </li></ul></ul><ul><li>application layer cache </li></ul><ul><ul><li>多個交易共用 </li></ul></ul><ul><li>cluster layer cache </li></ul><ul><ul><li>多個應用程式 /JVM 共用 </li></ul></ul>
  4. 4. cache level (2/2) cluster transaction application transaction application transaction transaction
  5. 5. Hibernate cache transaction application cluster concept session session factory third party hibernate
  6. 6. Cache providers Cache Cluster Safe Query Cache Hashtable yes EHcache yes OScache yes Swarmcache yes JBosscache yes yes
  7. 7. cache strategies <ul><li>read-only </li></ul><ul><ul><li>不會改變的資料 </li></ul></ul><ul><li>nonstrict-read-write </li></ul><ul><ul><li>不嚴格、更新頻率低 </li></ul></ul><ul><li>read-write </li></ul><ul><ul><li>implement ”read committed” </li></ul></ul><ul><li>transactional </li></ul><ul><ul><li>could be rollback </li></ul></ul><ul><ul><li>implement “repeatable read” </li></ul></ul>
  8. 8. cache concurrency strategy support cache read-only nonstrict-read-write read-write transactional hashtable yes yes yes EHCache yes yes yes OSCache yes yes yes SwarmCache yes yes JBoss yes yes
  9. 9. entity cache <ul><li>session.get </li></ul><ul><ul><li>1st level cache </li></ul></ul><ul><li>session.load </li></ul><ul><ul><li>1st/2nd level cache </li></ul></ul>
  10. 10. session.get step in session? DataBase No session.get Yes session
  11. 11. session.load step in session? session.get Yes session No in session factory? DataBase No session factory Yes
  12. 12. collection cache setting <hibernate mapping> <class name=“User”> ……… . <set name=“addresses” table=“t_addresses” …… > <cache usage=“read-only”> User.hbm.xml
  13. 13. collection cache <ul><li>如僅針對集合類別設定快取,只會快取其資料索引 </li></ul><ul><li>集合物件也要指定快取策略 </li></ul><hibernate-mapping> <class name=“com…..Adress” table=“t_addresses” …… > <cache usage=“read-write”>
  14. 14. query cache <ul><li>保存了之前的 SQL 與 result set </li></ul><ul><li>step </li></ul><ul><ul><li>set strategy </li></ul></ul><ul><ul><li>set hibernate.cfg.xml </li></ul></ul><ul><ul><li>set Query.Cacheable=true </li></ul></ul><ul><ul><li>set ehcache.xm. </li></ul></ul>
  15. 15. set ehcache.xml <defaultCache maxElementsInMemory=&quot;10000&quot; eternal=&quot;false&quot; timeToIdleSeconds=&quot;120&quot; timeToLiveSeconds=&quot;120&quot; overflowToDisk=&quot;false&quot; />
  16. 16. clear 2nd-level cache <ul><li>做什麼用 ? </li></ul>
  17. 17. hibernate statistics <ul><li>設定 </li></ul><ul><ul><li>hibernate.generate_statistics=enable </li></ul></ul><ul><ul><li>SessionFactory.getStatistics() </li></ul></ul><ul><li>例外 </li></ul><ul><ul><li>Criteria 與 native sql 不包含 query 紀錄在內 </li></ul></ul>
  18. 18. Query Optimization <ul><li>MySQL </li></ul><ul><ul><li>http://dev.mysql.com/doc/refman/5.0/en/query-speed.html </li></ul></ul><ul><ul><li>http:// www.mysqlperformanceblog.com / </li></ul></ul><ul><li>Explain </li></ul><ul><ul><li>檢查 query 效能 </li></ul></ul><ul><li>Index </li></ul><ul><ul><li>Multiple indexes 的順序有差別 </li></ul></ul><ul><li>Order by </li></ul><ul><ul><li>Order by 要在 index 上 </li></ul></ul><ul><li>Slow query log </li></ul><ul><ul><li>紀錄超過限定時間的 query </li></ul></ul>
  19. 19. Query Optimization <ul><li>Hibernate Collection </li></ul><ul><ul><li>如果 Collection 內的物件數有可能很多 , 請不要使用 Collection ( 直接使用 HQL 或 Criteria) </li></ul></ul>
  20. 20. Query Optimization <ul><li>One-to-One / Many-to-One </li></ul><ul><ul><li>HQL: from Article where category.name = :categoryName order by date desc </li></ul></ul><ul><ul><ul><li>Use filesort (temp disk table) </li></ul></ul></ul><ul><ul><li>HQL: from Article where category = (from Category where name = :categoryName) order by date desc </li></ul></ul><ul><ul><ul><li>Use index </li></ul></ul></ul>Article category Category
  21. 21. Query Optimization <ul><li>Many-to-Many </li></ul><ul><ul><li>HQL: from Article where : category in elements(categories) ; </li></ul></ul><ul><ul><ul><li>No index </li></ul></ul></ul><ul><ul><li>Native: from Article as a where exists (select 1 from Article_Category as b where b.category = :category and a.id = b.article) </li></ul></ul><ul><ul><ul><li>Limit rows in subquery </li></ul></ul></ul><ul><ul><ul><li>但好像沒有快很多 </li></ul></ul></ul>Article Article_Category Category category article
  22. 22. session & transaction <ul><li>All queries must be in transaction </li></ul><ul><li>When exception is thrown, the session need to be closed </li></ul><ul><li>Session can have more than one transactions </li></ul><ul><li>If there is no session opened before transaction started, the session will be opened/closed automatically </li></ul>

×