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=928742.63..928743.13 rows=200 width=8) (actual time=78679.668..78679.672 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=241817 read=50024 dirtied=253 written=193 -> HashAggregate (cost=928732.99..928734.99 rows=200 width=8) (actual time=78679.508..78679.519 rows=46 loops=1) Buffers: shared hit=241814 read=50024 dirtied=253 written=193 -> HashAggregate (cost=891232.99..906232.99 rows=1500000 width=8) (actual time=77495.996..78325.692 rows=1500000 loops=1) Buffers: shared hit=241814 read=50024 dirtied=253 written=193 -> Hash Right Join (cost=69028.00..816091.27 rows=15028344 width=8) (actual time=3404.362..56141.146 rows=15353138 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=241814 read=50024 dirtied=253 written=193 -> Seq Scan on orders (cost=0.00..446496.39 rows=15028344 width=8) (actual time=0.045..13136.609 rows=14853122 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161878 Buffers: shared hit=241812 read=14748 dirtied=253 written=172 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3402.400..3402.400 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=21 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.016..2259.080 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=21 Total runtime: 78735.340 ms (21 rows) COMMIT; COMMIT