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 '%special%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=921382.04..921382.54 rows=200 width=8) (actual time=68077.364..68077.369 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=249056 read=40253 written=40 -> HashAggregate (cost=921372.40..921374.40 rows=200 width=8) (actual time=68077.231..68077.247 rows=46 loops=1) Buffers: shared hit=249053 read=40253 written=40 -> HashAggregate (cost=883872.40..898872.40 rows=1500000 width=8) (actual time=66922.180..67721.059 rows=1500000 loops=1) Buffers: shared hit=249053 read=40253 written=40 -> Hash Right Join (cost=69028.00..809321.50 rows=14910180 width=8) (actual time=1975.671..48640.140 rows=15353984 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=249053 read=40253 written=40 -> Seq Scan on orders (cost=0.00..442089.90 rows=14910180 width=8) (actual time=0.050..11422.851 rows=14853968 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161032 Buffers: shared hit=249051 read=4977 written=40 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1973.215..1973.215 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..1273.000 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 68134.310 ms (21 rows) COMMIT; COMMIT