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=65898.275..65898.277 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=277758 read=14083 written=286 -> HashAggregate (cost=927976.84..927978.84 rows=200 width=8) (actual time=65898.167..65898.177 rows=46 loops=1) Buffers: shared hit=277755 read=14083 written=286 -> HashAggregate (cost=890476.84..905476.84 rows=1500000 width=8) (actual time=64710.859..65525.554 rows=1500000 loops=1) Buffers: shared hit=277755 read=14083 written=286 -> Hash Right Join (cost=69028.00..815486.35 rows=14998098 width=8) (actual time=1402.970..46811.704 rows=15353138 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=277755 read=14083 written=286 -> Seq Scan on orders (cost=0.00..446496.39 rows=14998098 width=8) (actual time=0.058..10815.082 rows=14853122 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161878 Buffers: shared hit=242477 read=14083 written=286 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1401.243..1401.243 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.012..748.029 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 65923.769 ms (21 rows) COMMIT; COMMIT