Hi, Let say I have a table containing 100 student records:
It is easy to query for "next page" with keyset pagination. For example, if I want to get 10 rows of student records after reading student records #10 to #20:
But how to query for "previous page"? Let say I'm reading student records #20 to #30, now I want to get 10 student records before the current set. Here is how I would do it:
In the sub-query, I sort the student records by the student ID in descending order, then I get 10 rows of student record, and finally sort those 10 student records by student ID in ascending order. Is it a good way to do it? Is there any way to avoid doing the sorting twice? Many thanks. asked Aug 17 '14 at 19:42 alextsg |
Hi! The way you do it is the way it is to be done. However, one clarifications:
Although you have two
You already have this index in that example to maintain the PK, I'm mentioning it nevertheless because real-world cases often require another index. answered Aug 21 '14 at 09:49 Markus Winand ♦♦ |