DEV Community

Discussion on: Why You Shouldn't Use OFFSET and LIMIT For Your Pagination

Collapse
 
elmuerte profile image
Michiel Hendriks

What if you do this?

SELECT * FROM table_name ORDER BY id LIMIT 10 OFFSET 8000001;

With PostgreSQL it will use the index to get your offset, no table scan needed.

It works for any b-tree based indexed column. So if you had an index on a column called first_name, you can do:

SELECT * FROM table_name ORDER BY first_name LIMIT 10 OFFSET 8000001;