CodexBloom - Programming Q&A Platform

PostgreSQL: guide with Pagination and OFFSET with Large Result Sets

👀 Views: 80 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-07
postgresql pagination performance SQL

I'm working on a project and hit a roadblock... I'm working with an scenario with pagination using `OFFSET` in PostgreSQL 14. When I try to paginate through a large dataset (about 1 million rows), the performance degrades significantly as I increase the offset value. For example, when I run a query like this: ```sql SELECT * FROM orders ORDER BY order_date DESC OFFSET 100000 LIMIT 10; ``` The response time is unacceptably high, sometimes taking several seconds. I've read that using `OFFSET` can lead to inefficiencies, especially with large result sets, but I don't know how to implement a better solution. I've also tried using a cursor approach, but that didn't seem to improve performance either. My current query setup looks like this: ```sql BEGIN; DECLARE my_cursor CURSOR FOR SELECT * FROM orders ORDER BY order_date DESC; FETCH FORWARD 100000 FROM my_cursor; FETCH FORWARD 10 FROM my_cursor; COMMIT; ``` Unfortunately, the cursor solution is still slow and doesn't yield the expected performance boost. Is there a more efficient way to handle pagination with large datasets in PostgreSQL? Should I consider alternative strategies like keyset pagination? Any insights or examples would be greatly appreciated. For context: I'm using Sql on macOS. Any help would be greatly appreciated! For reference, this is a production REST API. Has anyone else encountered this?