Query Execution & OptimizationdebuggingBeginner
The orders page went from 20 ms to 4 seconds
Symptoms
- A customer’s order list loaded instantly at launch and now takes ~4 s.
- No code has changed in months.
- The orders table has grown from ~5,000 rows to ~3,000,000.
EXPLAIN ANALYZE SELECT id, total, created_at FROM orders
WHERE user_id = 17 ORDER BY created_at DESC LIMIT 20;
Limit (actual time=3980..3980 rows=20 loops=1)
-> Sort (actual rows=142 loops=1)
Sort Method: quicksort
-> Seq Scan on orders (actual rows=142 loops=1)
Filter: (user_id = 17)
Rows Removed by Filter: 2999858Investigate
Inspect areas in any order (0/4 inspected). When you think you know the root cause, commit to it.
The query plan
Server CPU and memory
The ORM-generated SQL
Table and index list