BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select c_count, count(*) as custdist from ( select c_custkey, count(o_orderkey) from customer left outer join orders on c_custkey = o_custkey and o_comment not like '%unusual%accounts%' group by c_custkey ) as c_orders (c_custkey, c_count) group by c_count order by custdist desc, c_count desc; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=847369.07..847369.57 rows=200 width=8) (actual time=26172.446..26172.448 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=247885 read=42437 written=180 -> HashAggregate (cost=847359.43..847361.43 rows=200 width=8) (actual time=26172.368..26172.378 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=247882 read=42437 written=180 -> HashAggregate (cost=809859.43..824859.43 rows=1500000 width=8) (actual time=25401.566..25839.177 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=247882 read=42437 written=180 -> Hash Right Join (cost=69028.00..735614.79 rows=14848929 width=8) (actual time=806.608..17000.529 rows=15338965 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=247882 read=42437 written=180 -> Seq Scan on orders (cost=0.00..443852.85 rows=14848929 width=8) (actual time=0.018..7408.308 rows=14838943 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161057 Buffers: shared hit=212604 read=42437 written=180 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=800.388..800.388 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..415.969 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.455 ms Execution time: 26183.121 ms (24 rows) COMMIT; COMMIT