News for hackers. Stuff that matters.
Re:Faster offset pagination for Rails apps by ajsharp on Tuesday August 16, 2022 @05:06PM (#32488922)
This is a smart solution to the problem, if you must use OFFSET. That said, if you can avoid OFFSET, do, as it's typically considerably slower than a simple indexed range query.

If you're OFFSET-ing, say, 1000 records, I believe the database needs to load those 1000 records in some way, to exclude them from the result set (it's possible mysql does this differently than postgres). With a ranged query and a cursor (e.g. select * from tweets where created_at > CURSOR LIMIT 20) is, generally, more efficient. But cursored range queries don't make sense in a lot of cases and often come with some additional complexity in the code.

Re:Faster offset pagination for Rails apps by ajsharp on Sunday August 21, 2022 @09:04PM (#32546141)
A much more complete post covering cursor-based pagination: https://www.moderntreasury.com/journal/shifting-our-apis-pag...
Re:Faster offset pagination for Rails apps by Rapzid on Tuesday August 16, 2022 @10:05PM (#32491463)
Sure but as soon as you start sorting and that column isn't the same as your range clause column and your indexes are perfect.. Sortsville.
Re:Faster offset pagination for Rails apps by ajsharp on Wednesday August 17, 2022 @02:04PM (#32499695)
Yep. It's brittle but if you need offset LOTS of records (e.g. a timeline) it's prob the only way to do it semi-scalable without storing the index somewhere else (redis etc)