I have a table with about 6 million records in it. I need to retrieve the ID
of each row to process in Java. The ID
is an alphanumeric nvarchar(8). I would like to do this a memory safe and efficient manner.
Below is my approach, in it I: select the top 1000 records. Process those. Select the the next 1000 records where the ID is greater than the greatest ID in my last set of records. Continue until I retrieve a set with less than 1000 records.
I believe my strategy will retrieve all the records in a memory safe fashion, but have no idea if there is a faster/better strategy. (Also not positive that the strategy will retrieve all records, I need to test in an environment with more resources, mine is very unstable).
List<String> ids = myDao.getTopThousandIds();
processIds(ids);
while(skus.size() == 1000) {
ids = myDao.getTopThousandIdsGreaterThan(ids.get(ids.size() -1));
processIds(skus);
}
public interface MyDao {
@SqlQuery("SELECT TOP 1000 [ID] FROM [tbl] WITH(NOLOCK) ORDER BY [ID] ASC")
List<String> getTopThousandIds();
@SqlQuery("SELECT TOP 1000 [ID] FROM [tbl] WITH(NOLOCK) WHERE [ID] > :id ORDER BY [ID] ASC")
List<String> getTopThousandIdsGreaterThan(@Bind("id") String id);
}