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=924248.67..924249.17 rows=200 width=8) (actual time=75378.079..75378.081 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=225584 read=64738 written=4418 -> HashAggregate (cost=924239.03..924241.03 rows=200 width=8) (actual time=75377.952..75377.963 rows=45 loops=1) Buffers: shared hit=225581 read=64738 written=4418 -> HashAggregate (cost=886739.03..901739.03 rows=1500000 width=8) (actual time=74143.228..75021.705 rows=1500000 loops=1) Buffers: shared hit=225581 read=64738 written=4418 -> Hash Right Join (cost=69028.00..811967.39 rows=14954327 width=8) (actual time=3574.706..55031.428 rows=15338054 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=225581 read=64738 written=4418 -> Seq Scan on orders (cost=0.00..443852.85 rows=14954327 width=8) (actual time=0.036..12912.710 rows=14838035 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 161965 Buffers: shared hit=219710 read=35331 written=961 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3573.092..3573.092 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=5871 read=29407 written=3457 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..1690.310 rows=1500000 loops=1) Buffers: shared hit=5871 read=29407 written=3457 Total runtime: 75435.271 ms (21 rows) COMMIT; COMMIT