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%requests%' 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=845348.04..845348.54 rows=200 width=8) (actual time=38965.303..38965.307 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=200103 read=88700 written=1227 -> HashAggregate (cost=845338.40..845340.40 rows=200 width=8) (actual time=38965.204..38965.209 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=200100 read=88700 written=1227 -> HashAggregate (cost=807838.40..822838.40 rows=1500000 width=8) (actual time=37857.511..38632.301 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=200100 read=88700 written=1227 -> Hash Right Join (cost=69028.00..733438.13 rows=14880055 width=8) (actual time=1297.751..23293.270 rows=15337628 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=200100 read=88700 written=1227 -> Seq Scan on orders (cost=0.00..441209.30 rows=14880055 width=8) (actual time=0.029..9392.651 rows=14837607 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162393 Buffers: shared hit=193742 read=59780 written=269 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1282.236..1282.236 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=6358 read=28920 written=958 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..742.205 rows=1500000 loops=1) Buffers: shared hit=6358 read=28920 written=958 Planning time: 3.115 ms Execution time: 38989.087 ms (24 rows) COMMIT; COMMIT