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=923063.09..923063.59 rows=200 width=8) (actual time=63431.392..63431.396 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=280211 read=9857 written=7 -> HashAggregate (cost=923053.45..923055.45 rows=200 width=8) (actual time=63431.275..63431.281 rows=44 loops=1) Buffers: shared hit=280208 read=9857 written=7 -> HashAggregate (cost=885553.45..900553.45 rows=1500000 width=8) (actual time=62301.709..63114.008 rows=1500000 loops=1) Buffers: shared hit=280208 read=9857 written=7 -> Hash Right Join (cost=69028.00..810930.52 rows=14924586 width=8) (actual time=1488.977..45494.234 rows=15338582 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=280208 read=9857 written=7 -> Seq Scan on orders (cost=0.00..443410.80 rows=14924586 width=8) (actual time=0.045..10382.166 rows=14838561 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161439 Buffers: shared hit=244930 read=9857 written=7 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1487.230..1487.230 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..769.617 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 63475.528 ms (21 rows) COMMIT; COMMIT