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=65755.004..65755.008 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290757 read=72 -> HashAggregate (cost=926998.79..927000.79 rows=200 width=8) (actual time=65754.888..65754.896 rows=44 loops=1) Buffers: shared hit=290754 read=72 -> HashAggregate (cost=889498.79..904498.79 rows=1500000 width=8) (actual time=64645.231..65421.760 rows=1500000 loops=1) Buffers: shared hit=290754 read=72 -> Hash Right Join (cost=69028.00..814351.67 rows=15029424 width=8) (actual time=1351.360..47207.442 rows=15353467 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290754 read=72 -> Seq Scan on orders (cost=0.00..444735.19 rows=15029424 width=8) (actual time=0.046..11977.543 rows=14853446 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161554 Buffers: shared hit=255476 read=72 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1349.708..1349.708 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.014..767.936 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 65779.704 ms (21 rows) COMMIT; COMMIT