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%packages%' 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=847670.85..847671.35 rows=200 width=8) (actual time=38783.372..38783.375 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=242791 read=47531 written=1506 -> HashAggregate (cost=847661.21..847663.21 rows=200 width=8) (actual time=38783.259..38783.267 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=242788 read=47531 written=1506 -> HashAggregate (cost=810161.21..825161.21 rows=1500000 width=8) (actual time=37843.497..38474.906 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=242788 read=47531 written=1506 -> Hash Right Join (cost=69028.00..735841.12 rows=14864018 width=8) (actual time=986.941..23947.439 rows=15352721 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=242788 read=47531 written=1506 -> Seq Scan on orders (cost=0.00..443852.85 rows=14864018 width=8) (actual time=0.029..7754.378 rows=14852700 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162300 Buffers: shared hit=207510 read=47531 written=1506 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=972.522..972.522 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.013..494.924 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 2.605 ms Execution time: 38807.848 ms (24 rows) COMMIT; COMMIT