Here are the results I'm observing from my experiments.
There are three queries, I'll call them ntv, cln and sts. Here are the times I measured:
Query, Query.scroll(), JDBC
ntv, 9.63 min, 47 ms
cln, 9.52 min, 16 ms
sts, 9.74 min, 16 ms
I ran the JDBC calls first and then the 3 calls to Query.scroll(). In the case of the sts query I made a call to query.setFetchSize(1000) before the query.scroll() call. It doesnt seem to make any difference to the timing. Doesnt it just set the fetch size of the underlying resultset coming back from Oracle?
I did a bit of digging into the Hibernate 2.1.6 codebase and was able to trace the query.scroll call to the following code in SessionImpl.java:
Code:
public ScrollableResults scroll(String query, QueryParameters queryParameters) throws HibernateException {
if ( log.isTraceEnabled() ) {
log.trace( "scroll: " + query );
queryParameters.traceParameters(factory);
}
// This true/false bit is the "shallow" stuff that controls whether
// entity's are loaded, or just their ids.
// QueryTranslator[] q = factory.getQuery(query, true);
QueryTranslator[] q = factory.getQuery(query, false);
if (q.length!=1) throw new QueryException("implicit polymorphism not supported for scroll() queries");
autoFlushIfRequired( q[0].getQuerySpaces() );
dontFlushFromFind++; //stops flush being called multiple times if this method is recursively called
try {
return q[0].scroll(queryParameters, this);
}
catch (SQLException sqle) {
throw new JDBCException("Could not execute query", sqle);
}
finally {
dontFlushFromFind--;
}
}
Does the commented code have any significance to my problem? It seems like the "shallow" param has been defaulted to "false" which will cause the query.scroll() call to retrieve all the rows for the query and instantiate Hibernate beans for them. Still, I dont see how that can take 9+ minutes