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%deposits%' 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=847977.99..847978.49 rows=200 width=8) (actual time=33233.706..33233.710 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=198189 read=92133 dirtied=104 written=1267 -> HashAggregate (cost=847968.35..847970.35 rows=200 width=8) (actual time=33233.615..33233.623 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=198186 read=92133 dirtied=104 written=1267 -> HashAggregate (cost=810468.35..825468.35 rows=1500000 width=8) (actual time=32421.399..32917.120 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=198186 read=92133 dirtied=104 written=1267 -> Hash Right Join (cost=69028.00..736071.47 rows=14879375 width=8) (actual time=1738.196..19615.806 rows=15338054 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=198186 read=92133 dirtied=104 written=1267 -> Seq Scan on orders (cost=0.00..443852.85 rows=14879375 width=8) (actual time=0.034..8162.793 rows=14838035 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 161965 Buffers: shared hit=198184 read=56857 dirtied=104 written=1267 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1234.458..1234.458 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..759.769 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Planning time: 3.218 ms Execution time: 33263.091 ms (24 rows) COMMIT; COMMIT