Let\'s say I have a table with millions of rows. Using JPA, what\'s the proper way to iterate over a query against that table, such that I don\'t have all an in-memo
Here's a simple, straight JPA example (in Kotlin) that shows how you can paginate over an arbitrarily large result set, reading chunks of 100 items at a time, without using a cursor (each cursor consumes resources on the database). It uses keyset pagination.
See https://use-the-index-luke.com/no-offset for the concept of keyset pagination, and https://www.citusdata.com/blog/2016/03/30/five-ways-to-paginate/ for a comparison of different ways to paginate along with their drawbacks.
/*
create table my_table(
id int primary key, -- index will be created
my_column varchar
)
*/
fun keysetPaginationExample() {
var lastId = Integer.MIN_VALUE
do {
val someItems =
myRepository.findTop100ByMyTableIdAfterOrderByMyTableId(lastId)
if (someItems.isEmpty()) break
lastId = someItems.last().myTableId
for (item in someItems) {
process(item)
}
} while (true)
}