(转) firstResult/maxResults specified with collection fetch; applying in memory
来源:互联网 发布:strpos php 编辑:程序博客网 时间:2024/06/08 12:50
(转) 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的查询进行分开也是一种解决办法
0 0
- (转) firstResult/maxResults specified with collection fetch; applying in memory
- hibernate的继承分页bug:firstResult/maxResults specified on polymorphic query; applying in memory!
- Applying Domain-Driven Design and Patterns: With Examples in C# and .NET
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework
- Memory Management in AIR / AS3 / Flash Garbage Collection.
- No. 04 - Paths with Specified Sum in Binary Tree
- Effects with the Pixel Bender Toolkit – Part 5: Applying a filter to an image in Flash
- In-memory Computing with SAP HANA读书笔记 - 第一章:Basic concepts of in-memory
- Tracing memory leaks in .NET applications with ANTS Profiler
- Dealing With Memory Leaks in Unreal Engine 4
- Applying Strategy Pattern in C++ Applications
- Applying on-the-fly transformation in SharpMap
- 遇到的问题-----No JREs in workspace compatible with specified execution environment: JavaSE-1.7,java
- No JREs in workspace compatible with specified execution environment: JavaSE-1.8,java
- Eclipse提示:No JREs in workspace compatible with specified execution environment
- Fetch hardware address with ioctl
- Promise with fetch and post
- 博客已经转移到Github Pages
- All work and no play makes jack a dull boy 中谓语为什么用makes
- 注册表 读写练习
- Sum Root to Leaf Numbers
- Android中 View not attached to window manager错误的解决办法
- (转) firstResult/maxResults specified with collection fetch; applying in memory
- UITableView 一直显示滚动条(ScrollBar Indicators)、滚动条Width(宽度)、滚动条Color(颜色)
- 【工作经验】程序员什么时候该考虑辞职
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
- win7 64位 安装oracle 10g “oraparam.ini中未找到先决条件检查”的解决办法汇总
- 行为设计模式---责任链模式(Chain of Responsibility)
- hdoj 5038 Grade【众数】
- android MSM8974 上DeviceTree简介
- 关于java语言和c语言中基本数据类型的长度