Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is while(rs.next()) faster than a series of rs.absolute()

Suppose to have a ResultSet rs with n object.

This code:

while(rs.next()) {
   // do something on rs
}

is algoritmically equal to this code (i.e. both gave the same result):

for(i=1; i<=n; i++) {
   rs.absolute(i)
   // do something on rs
}

But are this equivalant on terms of throughouts? Is the first faster? Or, for a given i, rs.next() is just a wrapper for rs.absolute(i+1)?

Best regards MC

like image 917
BAD_SEED Avatar asked Dec 19 '12 14:12

BAD_SEED


People also ask

What is the use of while RS next ()) in Java?

As to the concrete question about rs. next() , it shifts the cursor to the next row of the result set from the database and returns true if there is any row, otherwise false .

What does result next () do?

The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.

What RS return next?

This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true. Using this method in the while loop you can iterate the contents of the result set.


1 Answers

rs.next demands a simpler kind of database cursor (FORWARD_ONLY) than rs.absolute so in most cases you will degrade performance/resource efficiency with rs.absolute. In certain cases, where there is no optimization for a FORWARD_ONLY cursor anyway, you may get the same performance.

Some drivers may allow absolute calls even with FORWARD_ONLY, validating that the requested record is the next one, but again others may throw an exception regardless.

like image 124
Marko Topolnik Avatar answered Sep 18 '22 20:09

Marko Topolnik