MySQL skip first 10 results
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Skipping the first ten rows in MySQL is a standard pagination task, but it is easy to implement it in a way that becomes slow or inconsistent as data grows. The SQL itself is short, yet correct behavior depends on deterministic ordering, safe query construction, and index strategy. A strong pattern here prevents duplicate rows across pages and avoids latency spikes under production load.
Using LIMIT and OFFSET Correctly
MySQL supports two equivalent forms for paging:
Both queries skip ten rows and return the next ten. The critical part is not the syntax, it is ORDER BY. Without a stable order, page boundaries drift because MySQL can return rows in different physical order between executions.
A robust sort uses a tie-breaker column:
If several rows share the same timestamp, id provides a deterministic secondary key.
Building Pagination in Application Code
Most bugs come from dynamic SQL assembly in the application layer. Keep pagination values parameterized and computed from a clear page formula:
This prevents injection bugs and keeps request behavior predictable. Keep one pagination utility function in your codebase so all endpoints share the same formula and constraints.
Why Deep Offsets Become Slow
OFFSET is simple but not free. MySQL still reads and discards skipped rows before returning the requested window. A request for page 5000 often touches many rows that never reach the client.
For deep scrolling, keyset pagination is usually better:
Here the client sends the last seen key from the previous page, then fetches the next slice. This reduces skipped-row work and gives stable navigation even while new rows are inserted.
Index Design for Fast Page Queries
Pagination performance depends on index alignment with both filtering and sorting. If you query published posts ordered by creation time, a composite index is usually needed:
This allows MySQL to satisfy filtering and order with one index traversal. If your query profile differs by endpoint, benchmark each query shape with EXPLAIN instead of guessing.
Check for filesort or large row estimates. Those are early warnings that paging cost will grow badly under traffic.
Total Count and Consistency Concerns
Many UIs want both a page slice and a total record count. Usually that means two queries:
If data changes between count and page query, totals can appear inconsistent. For dashboard use cases this is acceptable. For financial or audit views, consider transactional reads or cursor-based APIs that avoid page-number semantics.
For background jobs, do not use offset loops for full-table processing. Prefer a monotonic cursor approach:
Store the last processed key, then continue. This pattern is easier to resume after failure and scales better.
Common Pitfalls
- Paginating without explicit
ORDER BY, which causes unstable page contents. - Using a non-unique sort key without tie-breaker and seeing duplicates between pages.
- Building SQL with string concatenation instead of parameterized values.
- Using very large offsets in user-facing APIs and accepting avoidable latency.
- Reusing offset pagination for ETL backfills, then struggling with missed or repeated rows.
Summary
- '
LIMITplusOFFSETis valid for simple paging, but only with deterministic ordering.' - Include a tie-breaker column such as
idin ordered queries. - Parameterize page inputs in application code for safety and consistency.
- Move to keyset pagination for deep page traversal and better performance.
- Align indexes with both
WHEREandORDER BYclauses to keep queries fast.
Related reading
- Mysql Slave not updating
- mysql slave parallel workers from lower version master
- MySQL Sort GROUP_CONCAT values
- MySQL string replace
- MySQL syntax for Join Update
- MYSQL syntax not evaluating not equal to in presence of NULL
- MySQL Table doesn't exist. But it does or it should
- MySQL table is marked as crashed and last automatic? repair failed

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.