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 '%unusual%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=921438.22..921438.72 rows=200 width=8) (actual time=89293.818..89293.821 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=271574 read=17988 -> HashAggregate (cost=921428.57..921430.57 rows=200 width=8) (actual time=89293.704..89293.713 rows=46 loops=1) Buffers: shared hit=271571 read=17988 -> HashAggregate (cost=883928.57..898928.57 rows=1500000 width=8) (actual time=88038.161..88932.594 rows=1500000 loops=1) Buffers: shared hit=271571 read=17988 -> Hash Right Join (cost=69028.00..809454.50 rows=14894815 width=8) (actual time=2445.611..64014.130 rows=15353523 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=271571 read=17988 -> Seq Scan on orders (cost=0.00..442530.20 rows=14894815 width=8) (actual time=0.050..20538.144 rows=14853505 loops=1) Filter: ((o_comment)::text !~~ '%unusual%requests%'::text) Rows Removed by Filter: 161495 Buffers: shared hit=236293 read=17988 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2443.599..2443.599 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.015..1638.072 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 89328.540 ms (21 rows) COMMIT; COMMIT