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%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=849334.08..849334.58 rows=200 width=8) (actual time=38190.104..38190.105 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=201177 read=88891 written=1624 -> HashAggregate (cost=849324.44..849326.44 rows=200 width=8) (actual time=38189.989..38190.003 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=201174 read=88891 written=1624 -> HashAggregate (cost=811824.44..826824.44 rows=1500000 width=8) (actual time=37119.821..37885.766 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=201174 read=88891 written=1624 -> Hash Right Join (cost=69028.00..736978.03 rows=14969282 width=8) (actual time=1265.642..24120.857 rows=15353558 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=201174 read=88891 written=1624 -> Seq Scan on orders (cost=0.00..443410.80 rows=14969282 width=8) (actual time=0.033..7675.377 rows=14853540 loops=1) Filter: ((o_comment)::text !~~ '%unusual%requests%'::text) Rows Removed by Filter: 161460 Buffers: shared hit=201172 read=53615 written=1615 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1246.510..1246.510 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=9 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..715.646 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=9 Planning time: 2.754 ms Execution time: 38225.299 ms (24 rows) COMMIT; COMMIT