This document discusses different techniques for pagination in databases. It begins by describing the issues with using offsets for pagination, as it can lead to unstable performance. It then introduces key-set pagination as an alternative, where the next set of rows is queried based on a unique identifier from the previous set rather than an offset. This allows for faster and more consistent performance even when browsing through many pages of data. The document also notes some limitations of key-set pagination and tools needed for it to work effectively.
12. Inside the Database: Worst Case
Speed depends on the number of matched rows
and the page fetched.
Fetching the last page
can take considerably
longer than fetching
the first page.
13. Improvement 1: Indexed order by
se!e"# *
$%om news
whe%e #op&" ' 1234
o%de% b( da#e des", &d des"
o$$se# 10
!&m&# 10;
%&ea"e $ndex on news ("op$%, da"e, $d)
A single index to support the whe%e and o%de% b(
clauses.
14. Improvement 1: Indexed order by
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'10)
Index *ond: (#op&" ' 0)
15. Improvement 1: Indexed order by
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'20)
Index *ond: (#op&" ' 0)
16. Improvement 1: Indexed order by
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'30)
Index *ond: (#op&" ' 0)
17. Improvement 1: Indexed order by
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'40)
Index *ond: (#op&" ' 0)
19. Improvement 1: Indexed order by
Fetching the first page is not affected by the
Base-Set size!
Fetching the next page
is also faster.
However, the database
might revert to the other
execution plan when
browsing to the end.
38. Key-Set Pagination w/o Index for order by
Always needs to retrieve the full base set, but the
top-n sort buffer needs
to hold only 10 rows.
The response time remains
the same even when
browsing to the last page.
And the memory footprint
is very low!
39. Key-Set Pagination: Best Case
se!e"# *
$%om news
whe%e #op&" ' 1234
and (da#e, &d) < (?, ?)
o%de% b( da#e des", &d des"
!&m&# 10;
%&ea"e $ndex on news ("op$%, da"e, $d)
40. Key-Set Pagination: Best Case
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'10)
Index *ond: (#op&" ' 1234)
41. Key-Set Pagination: Best Case
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 23456)))
42. Key-Set Pagination: Best Case
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 34567)))
43. Key-Set Pagination: Best Case
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 45678)))
44. Key-Set Pagination: Best Case
L&m&# (a%"ua# &ows'10)
-> Index S"an Ba")wa%d (a%"ua# &ows'10)
Index *ond: ((#op&" ' 1234)
AND (ROW(d#, &d) < ROW(‘...’, 56789)))
45. Key-Set Pagination: Best Case
Successively browsing back doesn’t slow down.
Neither the size of the
base set(*) nor the fetched
page number affects the
response time.
(*) the index tree depth still affects the response time.