You can use setFetchSize(rows) to optimize the size of a sample that retrieves the specified number of rows at a time from the database.
conn = getDbConnection(); Statement createStatement = conn.createStatement(); createStatement.setFetchSize(1000); ResultSet rs = createStatement.executeQuery("Select * from myTable"); while (rs.next()) { //do nothing }
Note that fetchSize is just a hint of DB , and it can ignore this value. Only testing will show if it is optimal.
Also, in your case, it might be better to change the Scrollable Statement attribute, since you cannot process all records at once. Which scrollable option to choose depends on whether you want to see other changes during the iteration or not.
or
or
//TYPE_SCROLL_SENSITIVE // The constant indicating the type for a ResultSet object that is // scrollable and generally sensitive to changes made by others. conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
See the JDBC API Guide for more information.
source share