This post originated from an RSS feed registered with Java Buzz
by Andrej Koelewijn.
Original Post: Use rownum instead of scrollable resultsets
Feed Title: Andrej Koelewijn
Feed URL: http://feeds.feedburner.com/AndrejKoelewijn
Feed Description: On Oracle, Java and OpenSource
There are some interesting threads
on AskTom
discussing the best method to query a page of records (on Oracle), say
records 20 to 30 of a query. Tom Kyte's advice is to use rownum in the
where clause to retrieve only the required rows.
In java there's another way to query a page of records. You can use
a scrollable resultset. You position the cursor on the first record
required using absolute(), and then you loop through the number of
records you need. Here's an example.
stmt = conn.prepareStatement( "select * from big_table order by object_name", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.setFetchSize(pagesize); rset = stmt.executeQuery(); rset.absolute(start);
The first method is portable, the second is only useable on Oracle.
So, if both perform the same i would prefer to use the scrollable
resultset. This, however is not the case as can be demonstrated using
tkprof. I ran both statements 10 times, querying the first 10 records
with the scrollable resultset, the next with the rownum approach, then
again 10 with the scrollable resultset, etc. In total i queried 200
records, so both statements where created 10 times. Here's the output
from tkprof:
Misses in library cache during parse: 0 Optimizer goal: CHOOSE Parsing user id: 5
Rows Row Source Operation ------- --------------------------------------------------- 10 VIEW 20 COUNT STOPKEY 20 VIEW 20 SORT ORDER BY STOPKEY 221004 TABLE ACCESS FULL BIG_TABLE
As you can see from the output of tkprof, the rownum approach is better
for performance. Half the number of executes, and a lot less fetches.
So if you are using an Oracle database, and you're not going to use
anything else in the future, use the rownum method.