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%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=928681.01..928681.51 rows=200 width=8) (actual time=66149.445..66149.448 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=285151 read=6437 written=1 -> HashAggregate (cost=928671.36..928673.36 rows=200 width=8) (actual time=66149.351..66149.362 rows=46 loops=1) Buffers: shared hit=285148 read=6437 written=1 -> HashAggregate (cost=891171.36..906171.36 rows=1500000 width=8) (actual time=64920.916..65819.138 rows=1500000 loops=1) Buffers: shared hit=285148 read=6437 written=1 -> Hash Right Join (cost=69028.00..815953.91 rows=15043491 width=8) (actual time=1774.255..46735.097 rows=15338896 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=285148 read=6437 written=1 -> Seq Scan on orders (cost=0.00..446056.09 rows=15043491 width=8) (actual time=0.067..11258.508 rows=14838874 loops=1) Filter: ((o_comment)::text !~~ '%unusual%accounts%'::text) Rows Removed by Filter: 161126 Buffers: shared hit=249870 read=6437 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1772.505..1772.505 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.015..977.052 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 66178.028 ms (21 rows) COMMIT; COMMIT