aspose file tools
The moose likes Java in General and the fly likes Writing the results of this ResultSet to File is VERY slow (SOLVED) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Writing the results of this ResultSet to File is VERY slow (SOLVED)" Watch "Writing the results of this ResultSet to File is VERY slow (SOLVED)" New topic
Author

Writing the results of this ResultSet to File is VERY slow (SOLVED)

Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Hi,

I hope I'm posting in the right place. Since I can't tell whether my problem is in my use of ResultSet or with the OutputStream, I'm posting in general.

This output of this code is running very slowly. There are about 10,000 records for the period in question that my ResultSet object is returning. It is then formatting and writing the results to a BufferedWriter "out".



When this code is first executed, the time it takes the cursor to move to the next row in the while loop is less than a second. It then slows down considerably as it continues to run. I just timed it at record 122 (of 10,000) and it took more than 15 seconds for the cursor to move to the next row. Thanks for helping me figure out what I'm doing wrong.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If you want to know if it is the writing that is causing the slowness or not, simply take out the actual writing (and flushing) code and try again. If it still is slow then it's not caused by the I/O.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'd be a little surprised if it's the IO, but you're flushing between each chunk, which may somewhat defeat the purpose of using a buffered writer (I'm not sure of the internals off the top of my head).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24051
    
  13

It's almost certainly the ResultSet.TYPE_SCROLL_SENSITIVE flag, which instructs the driver to re-check the database every time a row is fetched. This flag can drastically reduce your performance.


[Jess in Action][AskingGoodQuestions]
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

Thanks! Earnest, you are correct. It was the SENSITIVE flag. I changed it to INSENSITIVE and the report was done in about 15 seconds. 15 seconds versus a day to get through 1,000 records is pretty good.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Actually you would be still better off using a FORWARD_ONLY result set, since that's what your program actually does. It never does any scrolling (reading back old records again) so there's no point in saying it does. If you say you want a scrolling result set, the JDBC driver has to make every single record available in case you might go back and ask for it again. Whereas if you ask for a forward-only result set, the JDBC driver can happily discard a record the instant you ask for the next one. Much easier on the memory if you do that, then.

In fact that's the case for about 99% of JDBC applications in real life, I would guess, so I don't know why the tutorials persist in starting with anything but FORWARD_ONLY examples.
Al Johnston
Ranch Hand

Joined: May 02, 2009
Posts: 99

That makes a lot of sense. I changed the ResultSet to Type.FORWARD_ONLY. I'll go back through my code and change all appropriate ResultSets to that flag.

Thanks for your help.

Best,
Al
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: Writing the results of this ResultSet to File is VERY slow (SOLVED)
 
Similar Threads
How can we call a stored procedure from java file
connection to mssql server database
sql server callable statement result set issue..
Invalid cursor state?
Java application Microsoft SQL server