Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select record in batches Spring JDBCTemplate

I have a table which has around 5 million records. I want to read all the records from this table and do some processing on them. Now I want to query these records in batches say 1000 in one go, process them and fetch next 1000 records and so on.

However JDBCTemplate.query method only returns List containing all the records in the table. Obviously I can not have 5 million records in memory.

Is there a way address my problem using Spring JDBC? Underlying database is going to be DB2 if that helps.

like image 455
coolbootgeek Avatar asked Oct 05 '22 07:10

coolbootgeek


1 Answers

Read the javadoc of JdbcTemplate. There are plenty of other methods, also named query(), that don't return a list, and take a RowCallbackHandler or a ResultSetExtractor as argument. Use these ones.

To set the number of rows fetched at once by the resultset, override applyStatementSettings() and call Statement.setFetchSize()

like image 122
JB Nizet Avatar answered Oct 13 '22 09:10

JB Nizet