溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Hibernate性能問題怎么解決

發(fā)布時(shí)間:2021-12-04 16:19:47 來源:億速云 閱讀:150 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“Hibernate性能問題怎么解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Hibernate性能問題怎么解決”吧!

在使用Hibernate進(jìn)行分頁的過程中,如果你收到如下警告,那么這里就是一個(gè)潛在的Hibernate性能問題點(diǎn):

WARNING: firstResult/maxResults specified with collection fetch; applying in memory!

出現(xiàn)這個(gè)警告的直接后果是:無論你想要看第幾頁的數(shù)據(jù),從Hibernate打印出的SQL來看它總是查詢了所有滿足條件的結(jié)果。這是為什么呢?來看看這句警告所在的代碼,它位于org.hibernate.hql.ast.QueryTranslatorImpl中,部分摘錄如下:

  1. view plaincopy to clipboardprint?  

  2. QueryNode query = ( QueryNode ) sqlAst;  

  3. boolean hasLimit = queryParameters.getRowSelection() != null && 
    queryParameters.getRowSelection().definesLimits();  

  4. boolean needsDistincting = ( query.getSelectClause().isDistinct() || hasLimit ) && 
    containsCollectionFetches();  

  5. QueryParameters queryParametersToUse;  

  6. if ( hasLimit && containsCollectionFetches() ) {  

  7. log.warn( "firstResult/maxResults specified with collection fetch; applying in memory!" )  

  8. RowSelection selection = new RowSelection();  

  9. selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );  

  10. selection.setTimeout( queryParameters.getRowSelection().getTimeout() );  

  11. queryParametersqueryParametersToUse = queryParameters.createCopyUsing( selection );  

  12. }  

  13. else {  

  14. queryParametersqueryParametersToUse = queryParameters;  

  15. }  

  16. List results = queryLoader.list( session, queryParametersToUse );  

  17. QueryNode query = ( QueryNode ) sqlAst;  

  18. boolean hasLimit = queryParameters.getRowSelection() != null && 
    queryParameters.getRowSelection().definesLimits();  

  19. boolean needsDistincting = ( query.getSelectClause().isDistinct() || hasLimit ) && 
    containsCollectionFetches();  

  20. QueryParameters queryParametersToUse;  

  21. if ( hasLimit && containsCollectionFetches() ) {  

  22. log.warn( "firstResult/maxResults specified with collection fetch; applying in memory!" );  

  23. RowSelection selection = new RowSelection();  

  24. selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );  

  25. selection.setTimeout( queryParameters.getRowSelection().getTimeout() );  

  26. queryParametersqueryParametersToUse = queryParameters.createCopyUsing( selection );  

  27. }  

  28. else {  

  29. queryParametersqueryParametersToUse = queryParameters;  

  30. }  

  31. List results = queryLoader.list( session, queryParametersToUse );  

關(guān)鍵在于if ( hasLimit && containsCollectionFetches() 這句判斷,如果滿足了這個(gè)條件,RowSelection將會(huì)被重新生成,原本分頁需要的firstRow和maxRows屬性將會(huì)丟失,后面的數(shù)據(jù)庫分頁自然也無法進(jìn)行。Hibernate這么做的原因從代碼上也很容易理解,如果查詢需要限制條數(shù)(limit/offset)并且需要fetch結(jié)合對(duì)象,則重新生成RowSelection,進(jìn)一步解釋,就是當(dāng)一個(gè)實(shí)體(A)和另一個(gè)實(shí)體(B)是One-To-Many關(guān)系的時(shí)候,一個(gè)需要fetch的典型查詢語句是“select distinct a from A a left join fetch a.b”,由于1個(gè)A可能對(duì)應(yīng)多個(gè)B,這個(gè)時(shí)候數(shù)據(jù)庫查詢的結(jié)果條數(shù)和需要生成的A對(duì)象的條數(shù)可能不一致,所以無法利用數(shù)據(jù)庫層的分頁來實(shí)現(xiàn),因?yàn)槟阏嬲敕猪摰氖茿而不是A left join B。出現(xiàn)這個(gè)警告就是提醒你這個(gè)查詢實(shí)際上是查詢了所有滿足條件的數(shù)據(jù),Hibernate是在內(nèi)存中對(duì)其進(jìn)行了假分頁的處理。

這樣,對(duì)于查詢結(jié)果比較多的情況無疑是一個(gè)Hibernate性能上的潛在威脅。碰到這樣的情況,將Many的查詢進(jìn)行分開也是一種解決辦法。

到此,相信大家對(duì)“Hibernate性能問題怎么解決”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI