JPA: what is the proper pattern for iterating over large result sets?

后端 未结 15 2588
攒了一身酷
攒了一身酷 2020-11-27 09:50

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

15条回答
  •  不知归路
    2020-11-27 10:29

    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)
    }
    

提交回复
热议问题