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%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=927610.88..927611.38 rows=200 width=8) (actual time=69429.693..69429.696 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=233730 read=58111 written=652 -> HashAggregate (cost=927601.24..927603.24 rows=200 width=8) (actual time=69429.558..69429.575 rows=46 loops=1) Buffers: shared hit=233727 read=58111 written=652 -> HashAggregate (cost=890101.24..905101.24 rows=1500000 width=8) (actual time=68047.341..69085.873 rows=1500000 loops=1) Buffers: shared hit=233727 read=58111 written=652 -> Hash Right Join (cost=69028.00..815185.87 rows=14983074 width=8) (actual time=2472.602..50096.293 rows=15352973 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=233727 read=58111 written=652 -> Seq Scan on orders (cost=0.00..446496.39 rows=14983074 width=8) (actual time=0.033..10651.028 rows=14852952 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162048 Buffers: shared hit=233724 read=22836 written=652 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2470.513..2470.513 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=3 read=35275 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..1071.482 rows=1500000 loops=1) Buffers: shared hit=3 read=35275 Total runtime: 69493.897 ms (21 rows) COMMIT; COMMIT