LIMIT & OFFSET: Performance
Module: SQL Fundamentals
**Performance Tips:**
- Always use ORDER BY with LIMIT for consistent results
- Index ORDER BY columns for fast sorting
- Large OFFSET is slow - use keyset pagination instead
- LIMIT with index: Database can stop early
- Without index: Must scan all rows before limiting
**Keyset Pagination (for deep pages):**
Instead of: LIMIT 10 OFFSET 10000
Use: WHERE id > last_seen_id ORDER BY id LIMIT 10
Result: Constant time, no matter how deep
Large OFFSET is slow (scans skipped rows)
Keyset pagination: WHERE id > last_id (constant time)
Index ORDER BY columns for fast sorting
LIMIT with index: Database stops early
Avoid OFFSET > 1000 (use keyset instead)
LIMIT without ORDER BY (unpredictable results)
Large OFFSET values (slow performance)
Not indexing ORDER BY columns
Allowing unlimited page_size (DoS risk)
Not handling empty pages gracefully