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=853164.37..853164.87 rows=200 width=8) (actual time=46720.706..46720.710 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=230399 read=61189 written=1459 -> HashAggregate (cost=853154.73..853156.73 rows=200 width=8) (actual time=46720.602..46720.608 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=230396 read=61189 written=1459 -> HashAggregate (cost=815654.73..830654.73 rows=1500000 width=8) (actual time=45669.687..46402.852 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=230396 read=61189 written=1459 -> Hash Right Join (cost=69028.00..740512.07 rows=15028532 width=8) (actual time=971.821..27416.149 rows=15352345 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=230396 read=61189 written=1459 -> Seq Scan on orders (cost=0.00..446056.09 rows=15028532 width=8) (actual time=0.028..8413.564 rows=14852327 loops=1) Filter: ((o_comment)::text !~~ '%pending%requests%'::text) Rows Removed by Filter: 162673 Buffers: shared hit=195118 read=61189 written=1459 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=955.321..955.321 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..512.923 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 3.015 ms Execution time: 46760.497 ms (24 rows) COMMIT; COMMIT