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 '%express%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=928133.01..928133.51 rows=200 width=8) (actual time=66110.391..66110.393 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=263959 read=26870 written=4552 -> HashAggregate (cost=928123.36..928125.36 rows=200 width=8) (actual time=66110.285..66110.297 rows=44 loops=1) Buffers: shared hit=263956 read=26870 written=4552 -> HashAggregate (cost=890623.36..905623.36 rows=1500000 width=8) (actual time=64986.364..65783.756 rows=1500000 loops=1) Buffers: shared hit=263956 read=26870 written=4552 -> Hash Right Join (cost=69028.00..815251.33 rows=15074407 width=8) (actual time=1759.650..48825.826 rows=15353467 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=263956 read=26870 written=4552 -> Seq Scan on orders (cost=0.00..444735.19 rows=15074407 width=8) (actual time=0.034..11159.814 rows=14853446 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161554 Buffers: shared hit=252595 read=2953 written=211 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1757.105..1757.105 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=11361 read=23917 written=4341 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..1034.526 rows=1500000 loops=1) Buffers: shared hit=11361 read=23917 written=4341 Total runtime: 66157.182 ms (21 rows) COMMIT; COMMIT