
PostgreSQL EXPLAIN ANALYZE: Reading Query Plans Like a Pro
Why Your Queries Are Slow You added an index. The query is still slow. You're not sure why. EXPLAIN ANALYZE tells you exactly what PostgreSQL is doing—and why. Basic Usage EXPLAIN ANALYZE SELECT u . email , COUNT ( o . id ) as order_count FROM users u LEFT JOIN orders o ON o . user_id = u . id WHERE u . created_at > '2024-01-01' GROUP BY u . email ORDER BY order_count DESC LIMIT 10 ; Output: Limit ( cost = 1847 . 23 .. 1847 . 26 rows = 10 width = 44 ) ( actual time = 23 . 415 .. 23 . 418 rows = 10 loops = 1 ) -> Sort ( cost = 1847 . 23 .. 1872 . 23 rows = 10000 width = 44 ) ( actual time = 23 . 414 .. 23 . 415 rows = 10 loops = 1 ) Sort Key : ( count ( o . id )) DESC Sort Method : top - N heapsort Memory : 26 kB -> HashAggregate ( cost = 1597 . 23 .. 1697 . 23 rows = 10000 width = 44 ) ( actual time = 22 . 891 .. 23 . 123 rows = 9847 loops = 1 ) Group Key : u . email -> Hash Left Join ( cost = 450 . 00 .. 1472 . 23 rows = 25000 width = 36 ) ( actual time = 5 . 234 .. 19 . 876 rows = 25
Continue reading on Dev.to
Opens in a new tab


