Suppose you have a phone book of names, addresses, etc. You are displaying 20 rows per page, you're on page 100, and you want to display page 99. How do you do this knowing only what page you are on?
Assuming...
LIMIT argument is ...
MAX(0,P-1) * Nwhich for the 99th 20-row page evaluates to 1960, and the second argument to LIMIT is just N, so to see page 99, write...
SELECT ... LIMIT (1960, N);The trouble with this is scaling. MySQL doesn't optimise LIMIT at all well. SELECT ... LIMIT 1000000,20 will unfortunately retrieve not just the twenty rows starting at the millionth row; it will retrieve a million and twenty rows before it shows you the 20 you asked for! The bigger the result, the longer LIMIT takes.
What's the alternative? Build pagination logic into the If your interface calls for showing only 20 rows per page on a given order, retrieve the twenty rows, plus the row just before the set if it exists, plus the next row after those twenty if it exists. When the user clicks the Previous Page button, adjust the For more detail see https://mysql.rjweb.org/doc.php/pagination. Last updated 11 Apr 2020 |
![]() |