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 '%pending%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=852120.87..852121.37 rows=200 width=8) (actual time=30524.514..30524.516 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=178339 read=112996 dirtied=119 written=744 -> HashAggregate (cost=852111.23..852113.23 rows=200 width=8) (actual time=30524.384..30524.393 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=178336 read=112996 dirtied=119 written=744 -> HashAggregate (cost=814611.23..829611.23 rows=1500000 width=8) (actual time=29760.301..30206.369 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=178336 read=112996 dirtied=119 written=744 -> Hash Right Join (cost=69028.00..739619.37 rows=14998372 width=8) (actual time=1520.672..20304.865 rows=15353127 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=178336 read=112996 dirtied=119 written=744 -> Seq Scan on orders (cost=0.00..445615.79 rows=14998372 width=8) (actual time=0.030..8961.285 rows=14853111 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161889 Buffers: shared hit=178334 read=77720 dirtied=119 written=663 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1507.034..1507.034 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=81 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.025..1006.856 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=81 Planning time: 5.478 ms Execution time: 30552.330 ms (24 rows) COMMIT; COMMIT