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 '%express%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=927008.43..927008.93 rows=200 width=8) (actual time=66040.890..66040.893 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=289643 read=1186 dirtied=253 written=49 -> HashAggregate (cost=926998.79..927000.79 rows=200 width=8) (actual time=66040.781..66040.790 rows=44 loops=1) Buffers: shared hit=289640 read=1186 dirtied=253 written=49 -> HashAggregate (cost=889498.79..904498.79 rows=1500000 width=8) (actual time=64914.255..65705.507 rows=1500000 loops=1) Buffers: shared hit=289640 read=1186 dirtied=253 written=49 -> Hash Right Join (cost=69028.00..814351.67 rows=15029424 width=8) (actual time=1658.609..47409.853 rows=15353467 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=289640 read=1186 dirtied=253 written=49 -> Seq Scan on orders (cost=0.00..444735.19 rows=15029424 width=8) (actual time=0.032..12074.720 rows=14853446 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161554 Buffers: shared hit=255079 read=469 dirtied=253 written=2 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1656.470..1656.470 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=34561 read=717 written=47 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..913.799 rows=1500000 loops=1) Buffers: shared hit=34561 read=717 written=47 Total runtime: 66068.678 ms (21 rows) COMMIT; COMMIT