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%requests%' 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=844456.96..844457.46 rows=200 width=8) (actual time=32780.188..32780.194 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=228814 read=59989 written=1907 -> HashAggregate (cost=844447.32..844449.32 rows=200 width=8) (actual time=32780.060..32780.066 rows=44 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=228811 read=59989 written=1907 -> HashAggregate (cost=806947.32..821947.32 rows=1500000 width=8) (actual time=31764.380..32471.367 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=228811 read=59989 written=1907 -> Hash Right Join (cost=69028.00..732769.82 rows=14835501 width=8) (actual time=1039.690..18794.166 rows=15338525 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=228811 read=59989 written=1907 -> Seq Scan on orders (cost=0.00..441209.30 rows=14835501 width=8) (actual time=0.031..8515.822 rows=14838502 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161498 Buffers: shared hit=207463 read=46059 written=792 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1024.209..1024.209 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=21348 read=13930 written=1115 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.009..585.009 rows=1500000 loops=1) Buffers: shared hit=21348 read=13930 written=1115 Planning time: 2.786 ms Execution time: 32873.655 ms (24 rows) COMMIT; COMMIT