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%requests%' 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=923050.62..923051.12 rows=200 width=8) (actual time=67007.837..67007.838 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=275886 read=14182 written=484 -> HashAggregate (cost=923040.98..923042.98 rows=200 width=8) (actual time=67007.722..67007.731 rows=44 loops=1) Buffers: shared hit=275883 read=14182 written=484 -> HashAggregate (cost=885540.98..900540.98 rows=1500000 width=8) (actual time=65973.566..66687.707 rows=1500000 loops=1) Buffers: shared hit=275883 read=14182 written=484 -> Hash Right Join (cost=69028.00..810920.54 rows=14924087 width=8) (actual time=1049.322..46662.607 rows=15352288 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=275883 read=14182 written=484 -> Seq Scan on orders (cost=0.00..443410.80 rows=14924087 width=8) (actual time=0.032..10300.143 rows=14852270 loops=1) Filter: ((o_comment)::text !~~ '%pending%requests%'::text) Rows Removed by Filter: 162730 Buffers: shared hit=240605 read=14182 written=484 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1047.385..1047.385 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.017..557.616 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 67047.203 ms (21 rows) COMMIT; COMMIT