Pagination via OFFSET
suggest changeWhen adding a LIMIT to a UNION, this is the pattern to use:
( SELECT ... ORDER BY x LIMIT 10 )
UNION
( SELECT ... ORDER BY x LIMIT 10 )
ORDER BY x LIMIT 10
You cannot predict which SELECT supplies the 10 rows, so fetch 10 from each, then trim the combined list. Repeat both the ORDER BY and the LIMIT.
For the 4th page of 10 items, this pattern is needed:
( SELECT ... ORDER BY x LIMIT 40 )
UNION
( SELECT ... ORDER BY x LIMIT 40 )
ORDER BY x LIMIT 30, 10
Collect 4 pages from each SELECT, then apply the offset to the UNION. Large offsets still read and discard the skipped rows, so this gets slower as the page number grows.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents