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=851090.37..851090.87 rows=200 width=8) (actual time=29444.609..29444.613 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=215566 read=75516 written=137 -> HashAggregate (cost=851080.73..851082.73 rows=200 width=8) (actual time=29444.518..29444.524 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=215563 read=75516 written=137 -> HashAggregate (cost=813580.73..828580.73 rows=1500000 width=8) (actual time=28629.080..29124.115 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=215563 read=75516 written=137 -> Hash Right Join (cost=69028.00..738736.42 rows=14968862 width=8) (actual time=979.896..19117.542 rows=15353467 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=215563 read=75516 written=137 -> Seq Scan on orders (cost=0.00..445175.49 rows=14968862 width=8) (actual time=0.028..9151.092 rows=14853446 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161554 Buffers: shared hit=180294 read=75507 written=137 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=972.711..972.711 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35269 read=9 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.017..567.397 rows=1500000 loops=1) Buffers: shared hit=35269 read=9 Planning time: 3.073 ms Execution time: 29470.499 ms (24 rows) COMMIT; COMMIT