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=850073.35..850073.85 rows=200 width=8) (actual time=45183.007..45183.010 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=235313 read=55009 written=368 -> HashAggregate (cost=850063.71..850065.71 rows=200 width=8) (actual time=45182.906..45182.921 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=235310 read=55009 written=368 -> HashAggregate (cost=812563.71..827563.71 rows=1500000 width=8) (actual time=43959.033..44863.842 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=235310 read=55009 written=368 -> Hash Right Join (cost=69028.00..737642.99 rows=14984143 width=8) (actual time=1079.106..25998.768 rows=15353812 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=235310 read=55009 written=368 -> Seq Scan on orders (cost=0.00..443852.85 rows=14984143 width=8) (actual time=0.025..8343.072 rows=14853790 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161210 Buffers: shared hit=200035 read=55006 written=368 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1066.842..1066.842 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.018..565.218 rows=1500000 loops=1) Buffers: shared hit=35275 read=3 Planning time: 2.802 ms Execution time: 45216.732 ms (24 rows) COMMIT; COMMIT