ORDER BY & Sorting: Performance
Module: SQL Fundamentals
**Performance Tips:**
- Index columns used in ORDER BY for fast sorting
- Sorting large datasets without index is expensive
- LIMIT with ORDER BY: Database can stop early with index
- Avoid expressions in ORDER BY when possible
- Use covering indexes for optimal performance
**Benchmark:**
- 1M rows without index: 5 seconds (full sort)
- 1M rows with index: 0.1 seconds (read in order)
- 50x faster with index!
Index ORDER BY columns for 10-100x speedup
Covering indexes include ORDER BY columns
LIMIT with ORDER BY can use index to stop early
Sorting large datasets without index is expensive
Multiple ORDER BY columns: Index on all columns
Forgetting ORDER BY with LIMIT (unpredictable results)
Not indexing ORDER BY columns (slow on large tables)
Mixing ASC and DESC without understanding priority
Not handling NULL values explicitly
Using expressions in ORDER BY (prevents index usage)