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=851384.17..851384.67 rows=200 width=8) (actual time=47938.504..47938.507 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=156973 read=134109 written=437 -> HashAggregate (cost=851374.53..851376.53 rows=200 width=8) (actual time=47938.377..47938.389 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=156970 read=134109 written=437 -> HashAggregate (cost=813874.53..828874.53 rows=1500000 width=8) (actual time=46996.401..47598.214 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=156970 read=134109 written=437 -> Hash Right Join (cost=69028.00..738956.77 rows=14983552 width=8) (actual time=2737.487..30018.763 rows=15338278 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=156970 read=134109 written=437 -> Seq Scan on orders (cost=0.00..445175.49 rows=14983552 width=8) (actual time=0.034..9249.684 rows=14838262 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161738 Buffers: shared hit=150014 read=105787 written=386 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1625.483..1625.483 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=6956 read=28322 written=51 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.029..1035.132 rows=1500000 loops=1) Buffers: shared hit=6956 read=28322 written=51 Planning time: 4.461 ms Execution time: 47973.112 ms (24 rows) COMMIT; COMMIT