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 '%pending%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=927986.48..927986.98 rows=200 width=8) (actual time=67231.015..67231.015 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=233719 read=58122 written=449 -> HashAggregate (cost=927976.84..927978.84 rows=200 width=8) (actual time=67230.912..67230.927 rows=46 loops=1) Buffers: shared hit=233716 read=58122 written=449 -> HashAggregate (cost=890476.84..905476.84 rows=1500000 width=8) (actual time=65999.128..66872.040 rows=1500000 loops=1) Buffers: shared hit=233716 read=58122 written=449 -> Hash Right Join (cost=69028.00..815486.35 rows=14998098 width=8) (actual time=1922.389..47431.754 rows=15353138 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=233716 read=58122 written=449 -> Seq Scan on orders (cost=0.00..446496.39 rows=14998098 width=8) (actual time=0.040..10713.588 rows=14853122 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161878 Buffers: shared hit=233714 read=22846 written=449 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1920.625..1920.625 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.041..1166.306 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 67761.531 ms (21 rows) COMMIT; COMMIT