I am following this answer to paginate a table. As the title says, is it necessary to give both id and timestamp to get the next page? I am thinking of something like this:
select * from my_table where
(date_created, id) <
((select date_created from my_table where id = 1234), 1234)
order by date_created, id desc limit 10
If id
is the primary key, then remembering the timestamp is not necessary, logically. (The second column is functionally dependent on id
- corner cases with concurrent writes aside.) But besides being correct, pagination also needs to be fast. Obviously, remembering the timestamp is a lot faster than looking it up again, which would almost double the cost.
Also, using a unique ID as tiebreaker is just a special case. The general case of keyset pagination works with composite keys, where only the combination of two or more columns is unique. Then, remembering all of them is a bare logical necessity. See:
BTW, "cursor pagination" is probably not the best keyword here. No obvious involvement of any "cursors". The key feature of your query is the set of keys used to establish a deterministic sort order for paging. "Keyset pagination" or "row value comparison" will be more fitting terms / keywords.