Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResultSet behavior with MySQL database, does it store all rows in memory?

Tags:

java

mysql

jdbc

When returning results from a select query, does the ResultSet store all of the rows in memory? Or does it only fetch a limited amount of rows? Does it differ database to database? What's the behavior for MYSQL?

like image 501
user121196 Avatar asked Mar 01 '11 16:03

user121196


People also ask

How do you know if ResultSet contains data?

The JDBC ResultSet doesn't provide any isEmpty(), length() or size() method to check if its empty or not. Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() returns false it means ResultSet is empty.

How the data from a ResultSet can be retrieved?

Invoke the Statement. executeQuery method to obtain the result table from the SELECT statement in a ResultSet object. In a loop, position the cursor using the next method, and retrieve data from each column of the current row of the ResultSet object using getXXX methods.

How do I get all columns from ResultSet?

You can get the name of a particular column using the getColumnName() method of the ResultSetMetadata interface. This method accepts an integer value representing the index of a column and returns a String value representing the name of the specified column.


2 Answers

By default, the MySQL JDBC driver attempts to fetch everything in Java's memory. So if you're dealing with extremely a lot of rows and/or little memory, then you need to tell it to not do so. This is in detail described in its JDBC driver documentation. Here's an extract of relevance:

ResultSet

By default, ResultSets are completely retrieved and stored in memory. In most cases this is the most efficient way to operate, and due to the design of the MySQL network protocol is easier to implement. If you are working with ResultSets that have a large number of rows or large values, and can not allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.

To enable this functionality, you need to create a Statement instance in the following manner:

stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
             java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);

The combination of a forward-only, read-only result set, with a fetch size of Integer.MIN_VALUE serves as a signal to the driver to stream result sets row-by-row. After this any result sets created with the statement will be retrieved row-by-row.

There are some caveats with this approach. You will have to read all of the rows in the result set (or close it) before you can issue any other queries on the connection, or an exception will be thrown.

For other databases (read: other JDBC drivers) you have to consult its documentation.

like image 94
BalusC Avatar answered Oct 23 '22 21:10

BalusC


ResultSet does not store all the rows, it keeps a cursor open on the database.

Exact amount of data fetched at once depends on the driver (consequently it does differ from database to database).

like image 30
mcyalcin Avatar answered Oct 23 '22 20:10

mcyalcin