SQLIntermediate#queries#performance

How do you paginate results efficiently?

LIMIT/OFFSET is simple but slows down at large offsets because skipped rows are still read. Keyset (seek) pagination filters on the last seen sorted key and stays fast.

Example
-- offset pagination
SELECT * FROM orders ORDER BY id LIMIT 20 OFFSET 10000;
-- keyset pagination
SELECT * FROM orders WHERE id > 10020 ORDER BY id LIMIT 20;

Related Questions

1
SQLBeginner#fundamentals

What are DDL, DML, DQL, DCL and TCL?

Open
2
SQLIntermediate#joins

What is the difference between a natural join and an equi join?

Open
3
SQLBeginner#fundamentals

What is SQL?

Open