Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating a ResultSet using the JDBC for Oracle takes a lot of time about 16s?

Tags:

java

oracle

jdbc

while( result_set.next() )
{
  ...
}

I have use System.nanoTime() and calculated the time, for each iteration the time taken is in milliseconds but the overall loop takes about 16s. I am considering a possible reason that the condition test is taking a lot of time, the next() function.

FYI I am connecting to a remote database server and the select query that I make is completed in milliseconds again calculated using the above mentioned method. Any reasons about why it's happening and how I can bring the time to iterate the resultset down to at max a second?

EDIT:

I am dealing with about 4000 records and each record contians about 10 columns each having a size of about 10 chars

EDIT2 Thanks setFetchsize() did the magic, awesome, awesome

like image 349
hershey92 Avatar asked Jul 19 '13 10:07

hershey92


People also ask

What is the use of ResultSet in JDBC?

A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object. Navigational methods − Used to move the cursor around.

How does JDBC calculate ResultSet size?

Simply iterate on ResultSet object and increment rowCount to obtain size of ResultSet object in java. rowCount = rs. getRow();

How many types of ResultSet are there in JDBC?

There are two types of result sets namely, forward only and, bidirectional.

How is ResultSet value calculated?

executeQuery method to obtain the result table from the SELECT statement in a ResultSet object. In a loop, position the cursor using the next method, and retrieve data from each column of the current row of the ResultSet object using getXXX methods. XXX represents a data type. Invoke the ResultSet.


1 Answers

I have set up a table with 4000 rows and 10 columns with 10 characters each and made a simple performance test using the following approach (RealTimeCounter is a class which measures the real time between start() and stop()):

List<String> myResult = new ArrayList<>();
ResultSet rs = s.executeQuery("SELECT * FROM Performance");

RealTimeCounter rtc = new RealTimeCounter();
rtc.start();
while(rs.next()) {
    myResult.add(rs.getString(1));
}
rtc.stop();
System.out.println(rtc);

Results:

  • Default fetch size: execution time is approx. 20 sec
  • fetch size = 100: execution time is approx 2.2 sec
  • fetch size = 500: execution time is approx 450 msec
  • fetch size = 2000: execution time is approx 120 msec
  • fetch size = 4000: execution time is approx 50 msec
  • fetch size = 4001: execution time is approx 10 msec (!!)

So, the fetch size does have a significant impact on the execution speed.


Note that, on the other hand, the fetch size has some impact on memory consumption. Interestingly enough, a quick analysis using Runtime.getRuntime().freeMemory(); before and after the above code showed that the impact is much less than I would expect, though. The numbers I got are:

  • Default fetch size: 665k
  • fetch size = 100: 665k
  • fetch size = 500: 665k
  • fetch size = 2000: 743k
  • fetch size = 4000: 821k
  • fetch size = 4001: 861k
like image 88
Andreas Fester Avatar answered Sep 21 '22 20:09

Andreas Fester