Created
April 23, 2025 17:06
-
-
Save fimars/da606766c7fb1b1342f03f037dd6a7f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| * | |
| FROM | |
| people | |
| WHERE | |
| ( | |
| (first_name > 'Aaron') -- Names after Aaron | |
| OR | |
| (first_name = 'Aaron' AND id > 25995) -- Aarons, but after the last id that the user saw | |
| ) | |
| ORDER BY | |
| first_name, id | |
| LIMIT | |
| 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT * FROM people | |
| INNER JOIN ( | |
| -- Paginate the narrow subquery instead of the entire table | |
| SELECT id FROM people ORDER BY first_name, id LIMIT 10 OFFSET 450000 | |
| ) AS tmp USING (id) | |
| ORDER BY | |
| first_name, id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT | |
| * | |
| FROM | |
| people | |
| ORDER BY | |
| first_name, id | |
| LIMIT | |
| 10 -- page_size | |
| OFFSET | |
| 10 -- (page_number - 1) * page_size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://planetscale.com/blog/mysql-pagination
https://www.pingcap.com/article/limit-offset-pagination-vs-cursor-pagination-in-mysql/