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

后端 未结 2 706
抹茶落季
抹茶落季 2020-12-03 07:39
while( result_set.next() )
{
  ...
}

I have use System.nanoTime() and calculated the time, for each iteration the time taken is in mil

相关标签:
2条回答
  • 2020-12-03 08:06

    Try to change ResultSet fetch size. By default, Oracle JDBC driver receives the result set only 10 rows at a time from the database cursor. It may not significantly improve performance but there arent many other options to make driver work faster.

    0 讨论(0)
  • 2020-12-03 08:26

    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
    0 讨论(0)
提交回复
热议问题