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%requests%' 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=846202.78..846203.28 rows=200 width=8) (actual time=37597.636..37597.639 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=240240 read=49575 -> HashAggregate (cost=846193.14..846195.14 rows=200 width=8) (actual time=37597.519..37597.527 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=240237 read=49575 -> HashAggregate (cost=808693.14..823693.14 rows=1500000 width=8) (actual time=36683.738..37300.904 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=240237 read=49575 -> Hash Right Join (cost=69028.00..734519.48 rows=14834732 width=8) (actual time=1057.352..24091.752 rows=15338734 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=240237 read=49575 -> Seq Scan on orders (cost=0.00..442970.50 rows=14834732 width=8) (actual time=0.035..7709.092 rows=14838716 loops=1) Filter: ((o_comment)::text !~~ '%unusual%requests%'::text) Rows Removed by Filter: 161284 Buffers: shared hit=240235 read=14299 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1040.727..1040.727 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.007..622.417 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Planning time: 3.111 ms Execution time: 37631.430 ms (24 rows) COMMIT; COMMIT