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%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=851989.47..851989.97 rows=200 width=8) (actual time=46878.460..46878.463 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=218384 read=72698 written=342 -> HashAggregate (cost=851979.83..851981.83 rows=200 width=8) (actual time=46878.316..46878.329 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=218381 read=72698 written=342 -> HashAggregate (cost=814479.83..829479.83 rows=1500000 width=8) (actual time=45641.461..46537.686 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=218381 read=72698 written=342 -> Hash Right Join (cost=69028.00..739410.74 rows=15013817 width=8) (actual time=1578.455..27989.749 rows=15352522 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=218381 read=72698 written=342 -> Seq Scan on orders (cost=0.00..445175.49 rows=15013817 width=8) (actual time=0.032..9911.757 rows=14852503 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162497 Buffers: shared hit=183178 read=72623 written=342 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1559.109..1559.109 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35203 read=75 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.029..848.514 rows=1500000 loops=1) Buffers: shared hit=35203 read=75 Planning time: 4.653 ms Execution time: 46906.433 ms (24 rows) COMMIT; COMMIT