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 '%special%accounts%' 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=927121.76..927122.26 rows=200 width=8) (actual time=70146.513..70146.517 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=277695 read=13640 written=81 -> HashAggregate (cost=927112.11..927114.11 rows=200 width=8) (actual time=70146.407..70146.416 rows=45 loops=1) Buffers: shared hit=277692 read=13640 written=81 -> HashAggregate (cost=889612.11..904612.11 rows=1500000 width=8) (actual time=68997.155..69794.939 rows=1500000 loops=1) Buffers: shared hit=277692 read=13640 written=81 -> Hash Right Join (cost=69028.00..814618.45 rows=14998733 width=8) (actual time=1761.436..50694.217 rows=15354058 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=277692 read=13640 written=81 -> Seq Scan on orders (cost=0.00..445615.79 rows=14998733 width=8) (actual time=0.051..13659.789 rows=14854042 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 160958 Buffers: shared hit=242449 read=13605 written=81 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1758.910..1758.910 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35243 read=35 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.015..1039.400 rows=1500000 loops=1) Buffers: shared hit=35243 read=35 Total runtime: 70177.716 ms (21 rows) COMMIT; COMMIT