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 '%pending%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=853770.33..853770.83 rows=200 width=8) (actual time=37982.793..37982.796 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=195951 read=95637 written=1239 -> HashAggregate (cost=853760.69..853762.69 rows=200 width=8) (actual time=37982.679..37982.693 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=195948 read=95637 written=1239 -> HashAggregate (cost=816260.69..831260.69 rows=1500000 width=8) (actual time=36730.731..37644.400 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=195948 read=95637 written=1239 -> Hash Right Join (cost=69028.00..740966.54 rows=15058830 width=8) (actual time=1067.237..22872.823 rows=15352345 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=195948 read=95637 written=1239 -> Seq Scan on orders (cost=0.00..446056.09 rows=15058830 width=8) (actual time=0.029..9690.755 rows=14852327 loops=1) Filter: ((o_comment)::text !~~ '%pending%requests%'::text) Rows Removed by Filter: 162673 Buffers: shared hit=160672 read=95635 written=1239 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1058.515..1058.515 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35276 read=2 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..603.571 rows=1500000 loops=1) Buffers: shared hit=35276 read=2 Planning time: 3.784 ms Execution time: 38009.370 ms (24 rows) COMMIT; COMMIT