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=844752.14..844752.64 rows=200 width=8) (actual time=39474.306..39474.309 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=217689 read=71114 written=236 -> HashAggregate (cost=844742.50..844744.50 rows=200 width=8) (actual time=39474.213..39474.222 rows=44 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=217686 read=71114 written=236 -> HashAggregate (cost=807242.50..822242.50 rows=1500000 width=8) (actual time=38612.980..39136.833 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=217686 read=71114 written=236 -> Hash Right Join (cost=69028.00..732991.20 rows=14850260 width=8) (actual time=1407.055..25720.837 rows=15338525 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=217686 read=71114 written=236 -> Seq Scan on orders (cost=0.00..441209.30 rows=14850260 width=8) (actual time=0.036..9668.853 rows=14838502 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161498 Buffers: shared hit=182536 read=70986 written=236 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1398.111..1398.111 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35150 read=128 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..784.888 rows=1500000 loops=1) Buffers: shared hit=35150 read=128 Planning time: 2.478 ms Execution time: 39496.696 ms (24 rows) COMMIT; COMMIT