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 '%express%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=928790.43..928790.93 rows=200 width=8) (actual time=89002.664..89002.667 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=250162 read=41932 written=247 -> HashAggregate (cost=928780.79..928782.79 rows=200 width=8) (actual time=89002.543..89002.552 rows=46 loops=1) Buffers: shared hit=250159 read=41932 written=247 -> HashAggregate (cost=891280.79..906280.79 rows=1500000 width=8) (actual time=87727.923..88616.003 rows=1500000 loops=1) Buffers: shared hit=250159 read=41932 written=247 -> Hash Right Join (cost=69028.00..816217.57 rows=15012644 width=8) (actual time=4015.340..63902.767 rows=15354132 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=250159 read=41932 written=247 -> Seq Scan on orders (cost=0.00..446936.69 rows=15012644 width=8) (actual time=0.045..19433.564 rows=14854112 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160888 Buffers: shared hit=247764 read=9049 written=227 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=4013.023..4013.023 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2395 read=32883 written=20 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.011..3150.174 rows=1500000 loops=1) Buffers: shared hit=2395 read=32883 written=20 Total runtime: 89046.066 ms (21 rows) COMMIT; COMMIT