• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

moving cursor to last row in the resultset

 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
was wondering if i used the last() method call on a resultset to move the cursor to the last record so i could call getRow() method to get the size of the resultset will java load the entire records returned from the database in memory first ??

basically i dont understand how last() method call works under the hood so if someone could explain in terms of resources how expensive a last() method call can be for large dataset (in my case the number of rows being returned are over 3million)



thanks in advance
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikil shar wrote:was wondering if i used the last() method call on a resultset to move the cursor to the last record so i could call getRow() method to get the size of the resultset will java load the entire records returned from the database in memory first ??

basically i dont understand how last() method call works under the hood so if someone could explain in terms of resources how expensive a last() method call can be for large dataset (in my case the number of rows being returned are over 3million)


Quite likely, yes. It will at least have to read them from the database, which isn't free, and depending on how you declared the statement, it might have to store them in memory (or on disk) in case something else changes them in the database before your code gets around to actually looking at them. None of that is free.

Which is why it's a lot better to change your query to return a single row via "Select count(*) from ... ". You aren't going to process each of those three million rows individually anyway, are you? Or if you are, why do you need to know in advance how many there are going to be?
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
nikil shar wrote:was wondering if i used the last() method call on a resultset to move the cursor to the last record so i could call getRow() method to get the size of the resultset will java load the entire records returned from the database in memory first ??

basically i dont understand how last() method call works under the hood so if someone could explain in terms of resources how expensive a last() method call can be for large dataset (in my case the number of rows being returned are over 3million)


Quite likely, yes. It will at least have to read them from the database, which isn't free, and depending on how you declared the statement, it might have to store them in memory (or on disk) in case something else changes them in the database before your code gets around to actually looking at them. None of that is free.

Which is why it's a lot better to change your query to return a single row via "Select count(*) from ... ". You aren't going to process each of those three million rows individually anyway, are you? Or if you are, why do you need to know in advance how many there are going to be?


thanks for your reply. i was wondering if there is a way to speed things up when it comes to getting records/data from the database ?? given the huge amount of data i work with it takes a few hours for my programme to get all the records and process them. is there any other structure other than preparedStatement.executeQuery() which may be faster ??
 
Jan Cumps
Bartender
Posts: 2608
14
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to restrict your data. That is often the biggest performance booster.
And if you do need to work on huge data sets, move the logic to the database server.
Avoid pulling large amounts of data to the client side.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic