(转) firstResult/maxResults specified with collection fetch; applying in memory
2014-09-23 09:27
525 查看
(转) firstResult/maxResults specified with collection fetch; applying in memory!
之前遇到了同样的问题。记录一下:
之前遇到了同样的问题。记录一下:
WARNING: firstResult/maxResults specified with collection fetch; applying in memory! ---------------------------------------- hxzon:使用了join fetch p.items导致这个警告。 ---------------------------------------- 学习Hibernate时,经常会遇到Hibernate性能问题,这里将介绍Hibernate性能问题的解决方法。 在使用Hibernate进行分页的过程中,如果你收到如下警告,那么这里就是一个潜在的Hibernate性能问题点: WARNING: firstResult/maxResults specified with collection fetch; applying in memory! 出现这个警告的直接后果是:无论你想要看第几页的数据,从Hibernate打印出的SQL来看它总是查询了所有满足条件的结果。这是为什么呢?来看看这 句警告所在的代码,它位于org.hibernate.hql.ast.QueryTranslatorImpl.list中。 public List list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException { // Delegate to the QueryLoader... errorIfDML(); 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() ); queryParametersToUse = queryParameters.createCopyUsing( selection ); } else { queryParametersToUse = queryParameters; } List results = queryLoader.list( session, queryParametersToUse ); if ( needsDistincting ) { int includedCount = -1; // NOTE : firstRow is zero-based int first = !hasLimit || queryParameters.getRowSelection().getFirstRow() == null ? 0 : queryParameters.getRowSelection().getFirstRow().intValue(); int max = !hasLimit || queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows().intValue(); int size = results.size(); List tmp = new ArrayList(); IdentitySet distinction = new IdentitySet(); for ( int i = 0; i < size; i++ ) { final Object result = results.get( i ); if ( !distinction.add( result ) ) { continue; } includedCount++; if ( includedCount < first ) { continue; } tmp.add( result ); // NOTE : ( max - 1 ) because first is zero-based while max is not... if ( max >= 0 && ( includedCount - first ) >= ( max - 1 ) ) { break; } } results = tmp; } return results; }关键在于if ( hasLimit && containsCollectionFetches() 这句判断,如果满足了这个条件,RowSelection将会被重新生成,原本分页需要的firstRow和maxRows属性将会丢失,后面的数据库分 页自然也无法进行。Hibernate这么做的原因从代码上也很容易理解,如果查询需要限制条数(limit/offset)并且需要fetch结合对 象,则重新生成RowSelection,进一步解释,就是当一个实体(A)和另一个实体(B)是One-To-Many关系的时候,一个需要fetch 的典型查询语句是“select distinct a from A a left join fetch a.b”,由于1个A可能对应多个B,这个时候数据库查询的结果条数和需要生成的A对象的条数可能不一致,所以无法利用数据库层的分页来实现,因为你真正 想分页的是A而不是A left join B。出现这个警告就是提醒你这个查询实际上是查询了所有满足条件的数据,Hibernate是在内存中对其进行了假分页的处理。 这样,对于查询结果比较多的情况无疑是一个Hibernate性能上的潜在威胁。碰到这样的情况,将Many的查询进行分开也是一种解决办法
相关文章推荐
- WARNING: firstResult/maxResults specified with collection fetch; applying in memory!
- hibernate的继承分页bug:firstResult/maxResults specified on polymorphic query; applying in memory!
- firstResult/maxResults specified on polymorphic query;
- Hibernate中的query.setFirstResult(),query.setMaxResults();
- Hibernate的query.setFirstResult(),query.setMaxResults()分页
- Hibernate中的query.setFirstResult(),query.setMaxResults();
- setFirstResult ,setMaxResults
- Hibernate中的query.setFirstResult(),query.setMaxResults();
- query.setFirstResult(),query.setMaxResults(); query.scroll();的用法
- Hibernate中的query.setFirstResult(),query.setMaxResults(); .
- query.scroll()和query.setMaxResults();query.setFirstResult()两种分页方式
- Hibernate中的query.setFirstResult(),query.setMaxResults(); .
- Hibernate的setFirstResult,setMaxResults实现数据库分页原理
- org.hibernate.Query 的setFirstResult(startRow) 和 setMaxResults(maxResult)
- hibernate的setFirstResult(a),setMaxResults(b)后,不按照一定顺序取值
- Hibernate中的query.setFirstResult(),query.setMaxResults();
- Hibernate中的query.setFirstResult(),query.setMaxResults();
- nhibernate中的setFirstResult setMaxResults 区别
- 使用Hibernate的setFirstResult,setMaxResults实现数据库分页
- query.scroll()和query.setFirstResult(),query.setMaxResults();