Databases 6 min read

Why OFFSET/LIMIT Pagination Fails and How to Use Fast Cursor Pagination

This article explains why traditional OFFSET/LIMIT pagination becomes inefficient with large tables, illustrates its performance drawbacks with real examples, and introduces a high‑performance cursor‑based pagination method that leverages indexed primary keys to dramatically speed up data retrieval.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why OFFSET/LIMIT Pagination Fails and How to Use Fast Cursor Pagination

Database pagination using OFFSET and LIMIT works for small datasets but becomes inefficient as data grows, requiring full table scans and high memory usage.

When OFFSET is large (e.g., 50 million out of 100 million rows), the database must read and discard millions of rows before returning the requested page, leading to slow queries. Rows 50,000 to 50,020 out of 100,000 rows Examples show that a naïve OFFSET/LIMIT query can be dozens of times slower than an optimized approach.

Problems with OFFSET and LIMIT

Full‑table scans, excessive I/O, and increased latency are the main issues.

Cursor‑based pagination alternative

Store the last seen primary key (or another unique, indexed column) and use it as the starting point for the next query instead of OFFSET.

This approach lets the database jump directly to the relevant rows using the index, dramatically reducing execution time.

Example of pointer‑based pagination query versus the original OFFSET/LIMIT query.

The optimized query runs in 0.01 seconds compared with 12.80 seconds for the OFFSET version.

Cursor pagination requires a unique sequential field (e.g., integer ID or timestamp). If a table lacks such a key, consider adding an auto‑increment primary key.

For large‑scale data queries, see Rick James’s article for deeper guidance: http://mysql.rjweb.org/doc.php/lists .

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

sqlcursor paginationOFFSET
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.