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=920258.97..920259.47 rows=200 width=8) (actual time=71271.953..71271.957 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=248998 read=40311 written=32 -> HashAggregate (cost=920249.32..920251.32 rows=200 width=8) (actual time=71271.859..71271.870 rows=46 loops=1) Buffers: shared hit=248995 read=40311 written=32 -> HashAggregate (cost=882749.32..897749.32 rows=1500000 width=8) (actual time=70175.402..70931.374 rows=1500000 loops=1) Buffers: shared hit=248995 read=40311 written=32 -> Hash Right Join (cost=69028.00..808423.04 rows=14865257 width=8) (actual time=2232.847..51478.667 rows=15353275 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=248995 read=40311 written=32 -> Seq Scan on orders (cost=0.00..442089.90 rows=14865257 width=8) (actual time=0.045..11741.096 rows=14853258 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161742 Buffers: shared hit=243927 read=10101 written=32 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2230.943..2230.943 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=5068 read=30210 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.006..1375.010 rows=1500000 loops=1) Buffers: shared hit=5068 read=30210 Total runtime: 71324.568 ms (21 rows) COMMIT; COMMIT