Medium java backend

OFFSET pagination is slow on page 5,000. Why, and how do you fix it?

The 30 second answer

With OFFSET, the database still reads and throws away all the skipped rows, so a deep page means scanning a huge number of rows just to return a few. The fix is keyset pagination: remember the last sort key and ask for rows after it, with an index on the sort columns. Every page then costs about the same.

With OFFSET the database still reads and throws away all skipped rows, so deep pages get slower. I switch to keyset pagination: the client sends the last id and timestamp, and I query 'where key is less than that, order by key, limit 20' with an index on those columns.


Sources (2)
  1. PostgreSQL 17 docs: LIMIT and OFFSET
  2. Use The Index, Luke: Paging through results