MySQL & InnoDB (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Optimizing Deep Pagination: The Deferred Join Pattern.
senior LevelCode ExamplemysqlAdvanced Internals
In Simple Words:
SELECT * FROM orders ORDER BY id LIMIT 1000000, 20 takes seconds because MySQL scans 1,000,020 rows and reads full table columns from disk before discarding the first 1,000,000!
- Deferred Join (Late Row Lookup): Use the covering index to scan the 1,000,020 integer IDs only, then join back to fetch full columns for only the final 20 rows!
sql
-- Optimized: 100x faster than standard offset
SELECT o.*
FROM orders o
JOIN (
SELECT id FROM orders ORDER BY id LIMIT 1000000, 20
) AS subquery ON o.id = subquery.id;