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%accounts%' 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=921557.57..921558.07 rows=200 width=8) (actual time=86355.823..86355.827 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=278494 read=11574 written=1 -> HashAggregate (cost=921547.92..921549.92 rows=200 width=8) (actual time=86355.713..86355.724 rows=46 loops=1) Buffers: shared hit=278491 read=11574 written=1 -> HashAggregate (cost=884047.92..899047.92 rows=1500000 width=8) (actual time=85039.812..85996.272 rows=1500000 loops=1) Buffers: shared hit=278491 read=11574 written=1 -> Hash Right Join (cost=69028.00..809726.10 rows=14864365 width=8) (actual time=1880.871..62436.508 rows=15353984 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=278491 read=11574 written=1 -> Seq Scan on orders (cost=0.00..443410.80 rows=14864365 width=8) (actual time=0.039..20491.920 rows=14853968 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161032 Buffers: shared hit=243352 read=11435 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1879.179..1879.179 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35139 read=139 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..802.499 rows=1500000 loops=1) Buffers: shared hit=35139 read=139 Total runtime: 86398.547 ms (21 rows) COMMIT; COMMIT