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 '%special%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=919807.44..919807.94 rows=200 width=8) (actual time=84552.931..84552.934 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=152283 read=136773 written=68 -> HashAggregate (cost=919797.80..919799.80 rows=200 width=8) (actual time=84552.839..84552.849 rows=46 loops=1) Buffers: shared hit=152280 read=136773 written=68 -> HashAggregate (cost=882297.80..897297.80 rows=1500000 width=8) (actual time=83321.570..84216.671 rows=1500000 loops=1) Buffers: shared hit=152280 read=136773 written=68 -> Hash Right Join (cost=69028.00..807973.76 rows=14864808 width=8) (actual time=4597.635..60638.410 rows=15352473 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=152280 read=136773 written=68 -> Seq Scan on orders (cost=0.00..441649.60 rows=14864808 width=8) (actual time=0.047..15563.605 rows=14852454 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162546 Buffers: shared hit=117053 read=136722 written=68 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=4595.326..4595.326 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35227 read=51 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..818.476 rows=1500000 loops=1) Buffers: shared hit=35227 read=51 Total runtime: 84592.812 ms (21 rows) COMMIT; COMMIT