In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to solve Hibernate performance problems". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve Hibernate performance problems.
If you receive the following warning during paging with Hibernate, here is a potential Hibernate performance problem:
WARNING: firstResult/maxResults specified with collection fetch; applying in memory!
The direct consequence of this warning is that no matter which page of the data you want to see, the SQL printed by Hibernate always looks up all the results that meet the criteria. Why is that? Let's take a look at the code where this warning is located, which is located in org.hibernate.hql.ast.QueryTranslatorImpl, with some excerpts as follows:
View plaincopy to clipboardprint?
QueryNode query = (QueryNode) sqlAst
Boolean hasLimit = queryParameters.getRowSelection ()! = null & &
QueryParameters.getRowSelection () .definesLimits ()
Boolean needsDistincting = (query.getSelectClause () .isDistinct () | | hasLimit) & &
ContainsCollectionFetches ()
QueryParameters queryParametersToUse
If (hasLimit & & containsCollectionFetches ()) {
Log.warn ("firstResult/maxResults specified with collection fetch; applying in memory!")
RowSelection selection = new RowSelection ()
Selection.setFetchSize (queryParameters.getRowSelection () .getFetchSize ())
Selection.setTimeout (queryParameters.getRowSelection () .getTimeout ())
QueryParametersqueryParametersToUse = queryParameters.createCopyUsing (selection)
}
Else {
QueryParametersqueryParametersToUse = queryParameters
}
List results = queryLoader.list (session, queryParametersToUse)
QueryNode query = (QueryNode) sqlAst
Boolean hasLimit = queryParameters.getRowSelection ()! = null & &
QueryParameters.getRowSelection () .definesLimits ()
Boolean needsDistincting = (query.getSelectClause () .isDistinct () | | hasLimit) & &
ContainsCollectionFetches ()
QueryParameters queryParametersToUse
If (hasLimit & & containsCollectionFetches ()) {
Log.warn ("firstResult/maxResults specified with collection fetch; applying in memory!")
RowSelection selection = new RowSelection ()
Selection.setFetchSize (queryParameters.getRowSelection () .getFetchSize ())
Selection.setTimeout (queryParameters.getRowSelection () .getTimeout ())
QueryParametersqueryParametersToUse = queryParameters.createCopyUsing (selection)
}
Else {
QueryParametersqueryParametersToUse = queryParameters
}
List results = queryLoader.list (session, queryParametersToUse)
The key lies in the judgment of if (hasLimit & & containsCollectionFetches ()). If this condition is met, the RowSelection will be regenerated, the firstRow and maxRows properties needed for paging will be lost, and the subsequent database paging will not be possible. The reason why Hibernate does this is also easy to understand from the code. If the query needs to limit the number of entries (limit/offset) and requires fetch to combine objects, then regenerate the RowSelection and further explain that when an entity (A) and another entity (B) are One-To-Many relations, a typical query statement that requires fetch is "select distinct a from An a left join fetch a.b", because one A may correspond to multiple B. At this time, the number of results of the database query and the number of An objects that need to be generated may not be consistent, so you cannot use database layer paging to achieve, because what you really want to paginate is A rather than A left join B. This warning reminds you that this query is actually querying all the data that meets the criteria, and Hibernate is pseudo-paging it in memory.
In this way, there is no doubt that it is a potential threat to the performance of Hibernate when there are many query results. In such a situation, it is also a solution to separate Many queries.
At this point, I believe you have a deeper understanding of "how to solve Hibernate performance problems". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.