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=926256.08..926256.58 rows=200 width=8) (actual time=64666.900..64666.902 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290829 -> HashAggregate (cost=926246.44..926248.44 rows=200 width=8) (actual time=64666.790..64666.800 rows=46 loops=1) Buffers: shared hit=290826 -> HashAggregate (cost=888746.44..903746.44 rows=1500000 width=8) (actual time=63437.679..64333.526 rows=1500000 loops=1) Buffers: shared hit=290826 -> Hash Right Join (cost=69028.00..813749.79 rows=14999330 width=8) (actual time=1582.772..46406.642 rows=15353284 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290826 -> Seq Scan on orders (cost=0.00..444735.19 rows=14999330 width=8) (actual time=0.037..10132.001 rows=14853267 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161733 Buffers: shared hit=255548 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1580.355..1580.355 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..561.011 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 64721.946 ms (21 rows) COMMIT; COMMIT