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=845644.90..845645.40 rows=200 width=8) (actual time=24002.525..24002.527 rows=47 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=261553 read=27250 -> HashAggregate (cost=845635.26..845637.26 rows=200 width=8) (actual time=24002.409..24002.415 rows=47 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=261550 read=27250 -> HashAggregate (cost=808135.26..823135.26 rows=1500000 width=8) (actual time=23303.809..23710.928 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=261550 read=27250 -> Hash Right Join (cost=69028.00..733660.77 rows=14894898 width=8) (actual time=947.310..15704.529 rows=15353766 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=261550 read=27250 -> Seq Scan on orders (cost=0.00..441209.30 rows=14894898 width=8) (actual time=0.034..6867.828 rows=14853742 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161258 Buffers: shared hit=248439 read=5083 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=935.856..935.856 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=13111 read=22167 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.024..547.613 rows=1500000 loops=1) Buffers: shared hit=13111 read=22167 Planning time: 4.803 ms Execution time: 24022.934 ms (24 rows) COMMIT; COMMIT