5 Steps to Optimize MySQL Deep Pagination

MySQL deep pagination is a common performance bottleneck in real-world development, particularly evident when paginating through large datasets. Many developers habitually use the LIMIT offset, size
syntax to implement pagination. However, when querying large page numbers or accessing data located far back in the result set, this straightforward approach can lead to severe performance issues. Excessively large offsets force the database to scan and discard vast amounts of data, resulting in drastically increased response times and excessive system resource consumption. This article delves into the root causes of deep pagination problems, validates an efficient solution—Deferred Join—through comprehensive experiments, and compares performance differences before and after optimization, offering developers a practical approach to enhancement.
Component | Specification |
---|---|
CPU | 4-core |
Memory | 8GB |
Storage | 1TB SSD |
MySQL Version | 5.7.24 |
|
|
Generated 2 million test records, sufficient for deep pagination testing.
|
|
|
|
We attempted to rewrite using SQLFlash.
Rewritten SQL Query:
|
|
View Detailed Optimization Report
Metric | Original SQL | Optimized SQL | Improvement |
---|---|---|---|
Execution Time | 2.34s | 0.46s | 80.3% faster |
Rows Scanned | 1,800,010 | 20 | 99.998% reduction |
Back-to-Table Operations | 1,800,010 | 10 | 99.999% reduction |
Compared to the original SQL, the rewritten SQL achieves optimization through covering indexes and split queries:
|
|
Analysis: The original SQL performs full table scan with in-memory filesort, processing nearly 2 million rows.
|
|
Analysis: The optimized execution plan uses index scanning for primary key retrieval followed by efficient primary key matching.
Through comparative analysis, we confirm that Deferred Join is an efficient solution for optimizing MySQL deep pagination queries. This approach splits the query into two phases: first uses covering indexes to quickly locate required primary key IDs, then obtains complete row data through primary key association.
Core Advantages:
The Deferred Join method offers low modification cost and wide applicability, making it the preferred solution for MySQL deep pagination performance issues. For practical applications, we recommend implementing this optimization for pagination queries exceeding 100 pages.
Additionally, consider combining with other optimization strategies such as business-level maximum pagination depth limits and cursor-based pagination to build more comprehensive pagination solutions.
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.
Join us and experience the power of SQLFlash today!.