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%deposits%' 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=851690.39..851690.89 rows=200 width=8) (actual time=29274.266..29274.270 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=196003 read=95079 written=1317 -> HashAggregate (cost=851680.75..851682.75 rows=200 width=8) (actual time=29274.155..29274.165 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=196000 read=95079 written=1317 -> HashAggregate (cost=814180.75..829180.75 rows=1500000 width=8) (actual time=28449.638..28956.714 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=196000 read=95079 written=1317 -> Hash Right Join (cost=69028.00..739186.43 rows=14998863 width=8) (actual time=1199.432..18597.799 rows=15338061 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=196000 read=95079 written=1317 -> Seq Scan on orders (cost=0.00..445175.49 rows=14998863 width=8) (actual time=0.033..8224.439 rows=14838042 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 161958 Buffers: shared hit=195998 read=59803 written=1127 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1188.476..1188.476 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=190 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.009..758.601 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=190 Planning time: 3.400 ms Execution time: 29635.010 ms (24 rows) COMMIT; COMMIT