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 '%special%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=929171.86..929172.36 rows=200 width=8) (actual time=67014.829..67014.832 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=182824 read=109270 written=73 -> HashAggregate (cost=929162.21..929164.21 rows=200 width=8) (actual time=67014.665..67014.673 rows=44 loops=1) Buffers: shared hit=182821 read=109270 written=73 -> HashAggregate (cost=891662.21..906662.21 rows=1500000 width=8) (actual time=65690.146..66681.649 rows=1500000 loops=1) Buffers: shared hit=182821 read=109270 written=73 -> Hash Right Join (cost=69028.00..816522.71 rows=15027901 width=8) (actual time=1912.259..47917.503 rows=15352859 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=182821 read=109270 written=73 -> Seq Scan on orders (cost=0.00..446936.69 rows=15027901 width=8) (actual time=0.032..11475.030 rows=14852841 loops=1) Filter: ((o_comment)::text !~~ '%special%deposits%'::text) Rows Removed by Filter: 162159 Buffers: shared hit=182819 read=73994 written=29 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1909.877..1909.877 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=44 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.025..1147.505 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=44 Total runtime: 67047.413 ms (21 rows) COMMIT; COMMIT