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%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=852890.57..852891.07 rows=200 width=8) (actual time=43886.335..43886.338 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=182457 read=108625 written=2611 -> HashAggregate (cost=852880.93..852882.93 rows=200 width=8) (actual time=43886.234..43886.242 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=182454 read=108625 written=2611 -> HashAggregate (cost=815380.93..830380.93 rows=1500000 width=8) (actual time=42645.505..43560.903 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=182454 read=108625 written=2611 -> Hash Right Join (cost=69028.00..740086.57 rows=15058872 width=8) (actual time=1268.437..25222.178 rows=15338918 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=182454 read=108625 written=2611 -> Seq Scan on orders (cost=0.00..445175.49 rows=15058872 width=8) (actual time=0.029..8622.766 rows=14838896 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161104 Buffers: shared hit=182453 read=73348 written=643 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1256.681..1256.681 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=1 read=35277 written=1968 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..802.160 rows=1500000 loops=1) Buffers: shared hit=1 read=35277 written=1968 Planning time: 3.685 ms Execution time: 43931.495 ms (24 rows) COMMIT; COMMIT