Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResultSet - Cursor : rs.next() Taking lot of time

Tags:

java

oracle

jdbc

I have a cursor returned from Database executes in 31ms (milliseconds) .

But when I use this cursor having more than 1500 rows for fetching rows

  ResultSet rs = (ResultSet)cstm.getObject(6);

  while(rs.next()){
     system.out.println("...");
  }

Just simple transversing through each row of cursor it's taking more than 40 seconds (40000 ms)

What can be done?

like image 970
faraz Avatar asked Apr 10 '12 09:04

faraz


People also ask

What is the function of RS next () in a ResultSet called RS?

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 is while RS next ()) in Java?

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. while(rs.next()){ System.out.println(rs.getString(1)); System.out.println(rs.getString(2)); }

What is the return type of next () method in ResultSet?

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.

Why we use while RS next ())?

Moves the cursor forward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.


2 Answers

Indeed, by default JDBC use a fetch size of 10.
Thus, if you don't set a greater value, you'll call database for next records exactly 150 times ..., no need to explain drawbacks of round-trips.

All you have to do is to test performance by setting fetchSize to.. 100 for instance :

statement.setFetchSize(100);

You can play with this number to improve performance according to your environnement.

like image 172
Mik378 Avatar answered Sep 18 '22 14:09

Mik378


you have more than 1500 rows in your cursor and the rs what is returned by the database is just a reference to that cursor. So when you invoke rs.next(), every time it goes to the database cursr and get you the current record pointed by the cursor pointer.

So obviously it will take some time to go to database every time and fetch a single record for more than 1500 times with every while loop iteration.

like image 33
Chandra Sekhar Avatar answered Sep 18 '22 14:09

Chandra Sekhar