When SORT ORDER BY Is Healthy
Fast in-memory sort for small result sets (< 10,000 rows).
When SORT ORDER BY Becomes a Production Bottleneck
Blocking operation (cannot stream first rows until all input rows are sorted) and causes Used-Tmp disk spills on large sets.
DBMS_XPLAN Tuning Checklist
- Check Used-Tmp and 0/1/M columns for disk spills.
- Align index trailing columns with ORDER BY to eliminate the sort completely (especially for pagination).
Before (Slow Pattern)
SELECT id, amount FROM ledger WHERE account_id = :a ORDER BY created_at DESC FETCH FIRST 20 ROWS ONLY;
After (Tuned Pattern)
CREATE INDEX idx_ledger_acct_created ON ledger (account_id, created_at DESC) ONLINE;