Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jtds ignores setFetchSize. How can I limit the fetch size?

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?

like image 415
Alex Avatar asked Feb 08 '16 21:02

Alex


People also ask

How do I set 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.

What is the default fetch size in JDBC?

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.

What is jTDS jar?

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.


1 Answers

The solution is to insert ;useCursors=true into the url.

 url="jdbc:jtds:sqlserver://myserver:1433;DatabaseName=myDb;useCursors=true";
like image 139
Alex Avatar answered Sep 25 '22 09:09

Alex