Skip to content

Instantly share code, notes, and snippets.

@fimars
Created April 23, 2025 17:06
Show Gist options
  • Save fimars/da606766c7fb1b1342f03f037dd6a7f7 to your computer and use it in GitHub Desktop.
Save fimars/da606766c7fb1b1342f03f037dd6a7f7 to your computer and use it in GitHub Desktop.
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
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
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