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=847668.35..847668.85 rows=200 width=8) (actual time=29929.428..29929.432 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=265751 read=24571 written=90 -> HashAggregate (cost=847658.71..847660.71 rows=200 width=8) (actual time=29929.316..29929.326 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=265748 read=24571 written=90 -> HashAggregate (cost=810158.71..825158.71 rows=1500000 width=8) (actual time=29069.837..29606.488 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=265748 read=24571 written=90 -> Hash Right Join (cost=69028.00..735839.24 rows=14863893 width=8) (actual time=1068.907..18725.002 rows=15353812 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=265748 read=24571 written=90 -> Seq Scan on orders (cost=0.00..443852.85 rows=14863893 width=8) (actual time=0.032..8242.204 rows=14853790 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161210 Buffers: shared hit=230473 read=24568 written=90 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1036.100..1036.100 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35275 read=3 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.022..590.324 rows=1500000 loops=1) Buffers: shared hit=35275 read=3 Planning time: 2.975 ms Execution time: 29956.943 ms (24 rows) COMMIT; COMMIT