I am trying to limit the fetch size of my select query. Unfortunately the JTDS MS SQL driver still reads all rows.
I do not want a limit or offset select. I just want to save my memory such that I need less RAM. I do not need setMaxRows.
My sample code:
url="jdbc:jtds:sqlserver://myserver:1433;DatabaseName=myDb";
mySql="select * from myVeryLargeTable";
con.setAutoCommit(false);
batchSize=1000;
s =con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
s.setFetchSize(batchSize);
rs = s.executeQuery(mySql);
rs.setFetchSize(batchSize);
rs.next(); // here all rows are coming in over network but i only want 'batchSize'
How can I limit the fetch size?
The setFetchSize(int) method defines the number of rows that will be read from the database when the ResultSet needs more rows. setFetchSize(int) affects how the database returns the ResultSet data. Whereas, setMaxRows(int) method of the ResultSet specifies how many rows a ResultSet can contain at a time.
By default, most JDBC drivers use a fetch size of 10. , so if you are reading 1000 objects, increasing the fetch size to 256 can significantly reduce the time required to fetch the query's results.
Overview. jTDS is an open source 100% pure Java (type 4) JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000, 2005, 2008 and 2012) and Sybase Adaptive Server Enterprise (10, 11, 12 and 15). jTDS is based on FreeTDS and is currently the fastest production-ready JDBC driver for SQL Server and Sybase ASE.
The solution is to insert ;useCursors=true
into the url.
url="jdbc:jtds:sqlserver://myserver:1433;DatabaseName=myDb;useCursors=true";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With