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%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=919757.29..919757.79 rows=200 width=8) (actual time=47473.396..47473.398 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=279509 read=9294 -> HashAggregate (cost=919747.65..919749.65 rows=200 width=8) (actual time=47473.295..47473.303 rows=46 loops=1) Buffers: shared hit=279506 read=9294 -> HashAggregate (cost=882247.65..897247.65 rows=1500000 width=8) (actual time=46261.494..47172.731 rows=1500000 loops=1) Buffers: shared hit=279506 read=9294 -> Hash Right Join (cost=69028.00..807845.58 rows=14880414 width=8) (actual time=802.750..33842.998 rows=15353288 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=279506 read=9294 -> Seq Scan on orders (cost=0.00..441209.30 rows=14880414 width=8) (actual time=0.040..8416.235 rows=14853269 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161731 Buffers: shared hit=251710 read=1812 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=801.792..801.792 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=27796 read=7482 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.026..465.642 rows=1500000 loops=1) Buffers: shared hit=27796 read=7482 Total runtime: 47498.635 ms (21 rows) COMMIT; COMMIT