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%deposits%' 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=849772.69..849773.19 rows=200 width=8) (actual time=39384.078..39384.081 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=185409 read=104913 written=709 -> HashAggregate (cost=849763.05..849765.05 rows=200 width=8) (actual time=39383.978..39383.991 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=185406 read=104913 written=709 -> HashAggregate (cost=812263.05..827263.05 rows=1500000 width=8) (actual time=38248.239..39047.627 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=185406 read=104913 written=709 -> Hash Right Join (cost=69028.00..737417.50 rows=14969110 width=8) (actual time=1318.197..23535.814 rows=15354690 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=185406 read=104913 written=709 -> Seq Scan on orders (cost=0.00..443852.85 rows=14969110 width=8) (actual time=0.042..9942.843 rows=14854671 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160329 Buffers: shared hit=185404 read=69637 written=689 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1310.134..1310.134 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=20 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..859.133 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=20 Planning time: 3.749 ms Execution time: 39408.090 ms (24 rows) COMMIT; COMMIT