Reputation: 5709
Is it possible to know the size of a dataset returned by a SQL statement in bytes
It would be nice if from the query itself I can get the size of the data. I found something like using SQL Plus, but I could not use that in my Java codes.
I know there maybe ways in doing it Java, can anyone guide me on how can I do that.
Thanks in advance.
Upvotes: 1
Views: 2655
Reputation: 718798
I know there maybe ways in doing it Java, can anyone guide me on how can I do that.
I'm pretty sure that the answer is that there is no easy way to do this.
The JDBC abstraction hides database driver implementation details from you, and the number of bytes used to encode the results set is very driver specific.
Once the resultset is materialized into the Java heap, it is theoretically possible to find out its size. But in practice, you have to traverse every heap node in the resultset ... which is not a simple problem.
COUNT() method give us the number of rows from the query, is there a way to convert it into bytes?
In a word, no. Not even in theory.
Upvotes: 1
Reputation: 18631
Docs:
A
ResultSet
object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
ResultSet
is not a collection, it is merely an abstraction of the cursor that is being used to get the data in a row-wise manner.
So, what exactly do need? The amount of memory needed to store the result? The size of the data in the database? ...? Why would it be nice?
You can always do SELECT COUNT(*) FROM
and using a certain average size of row estimate the result size... Instead of using the SELECT COUNT(*)
you can use a more convoluted way: go to the last element ResultSet.last()
and get the row number: ResultSet.getRow()
.
Upvotes: 2