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 '%pending%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=855417.65..855418.15 rows=200 width=8) (actual time=29070.406..29070.409 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=153665 read=138176 written=219 -> HashAggregate (cost=855408.01..855410.01 rows=200 width=8) (actual time=29070.338..29070.347 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=153662 read=138176 written=219 -> HashAggregate (cost=817908.01..832908.01 rows=1500000 width=8) (actual time=28222.933..28739.167 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=153662 read=138176 written=219 -> Hash Right Join (cost=69028.00..742312.10 rows=15119181 width=8) (actual time=1167.600..18161.076 rows=15338309 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=153662 read=138176 written=219 -> Seq Scan on orders (cost=0.00..446496.39 rows=15119181 width=8) (actual time=0.018..7559.451 rows=14838293 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161707 Buffers: shared hit=153660 read=102900 written=141 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1158.670..1158.670 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=78 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.008..696.801 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=78 Planning time: 2.694 ms Execution time: 29083.822 ms (24 rows) COMMIT; COMMIT