The accepted way of getting the number of results from a JDBC result seems to be, to do resultSet.last()
, and then resultSet.getRow()
, according to this answer. But, in that answer, the author also says:
it may not be a good idea as it can mean reading the entire table over the network and throwing away the data. Do a SELECT COUNT(*) FROM ... query instead.
I'm looking for a definite answer on this. Performance wise, will it be better to do a separate COUNT(*)
query to get the number of results, or will it be better to do resultSet.last()
and resultSet.getRow()
, followed by resultSet.first()
again?
If the ResultSet has already fetched the results and is holding them in memory, then it'd undoubtedly be better just to do last()
and getRow()
as (I assume) it would just loop over the results in the memory. But the OP's answer above seems to imply that it lazy loads the results from the db as they're requested.