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=31085.717..31085.718 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=201931 read=89151 written=384 -> HashAggregate (cost=851979.83..851981.83 rows=200 width=8) (actual time=31085.608..31085.617 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=201928 read=89151 written=384 -> HashAggregate (cost=814479.83..829479.83 rows=1500000 width=8) (actual time=30258.271..30751.540 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=201928 read=89151 written=384 -> Hash Right Join (cost=69028.00..739410.74 rows=15013817 width=8) (actual time=1369.972..20449.523 rows=15352522 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=201928 read=89151 written=384 -> Seq Scan on orders (cost=0.00..445175.49 rows=15013817 width=8) (actual time=0.032..9428.826 rows=14852503 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162497 Buffers: shared hit=198021 read=57780 written=123 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1361.068..1361.068 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=3907 read=31371 written=261 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..885.385 rows=1500000 loops=1) Buffers: shared hit=3907 read=31371 written=261 Planning time: 4.840 ms Execution time: 31114.528 ms (24 rows) COMMIT; COMMIT