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%deposits%' 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=928361.43..928361.93 rows=200 width=8) (actual time=60622.633..60622.637 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=241701 read=50140 written=45 -> HashAggregate (cost=928351.79..928353.79 rows=200 width=8) (actual time=60622.514..60622.529 rows=45 loops=1) Buffers: shared hit=241698 read=50140 written=45 -> HashAggregate (cost=890851.79..905851.79 rows=1500000 width=8) (actual time=59478.730..60302.942 rows=1500000 loops=1) Buffers: shared hit=241698 read=50140 written=45 -> Hash Right Join (cost=69028.00..815786.31 rows=15013096 width=8) (actual time=1785.564..43865.114 rows=15352855 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=241698 read=50140 written=45 -> Seq Scan on orders (cost=0.00..446496.39 rows=15013096 width=8) (actual time=0.040..10631.780 rows=14852837 loops=1) Filter: ((o_comment)::text !~~ '%special%deposits%'::text) Rows Removed by Filter: 162163 Buffers: shared hit=241696 read=14864 written=45 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1783.534..1783.534 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.011..1082.717 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 60655.725 ms (21 rows) COMMIT; COMMIT