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=923982.93..923983.43 rows=200 width=8) (actual time=75642.860..75642.864 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=247068 read=43761 written=1 -> HashAggregate (cost=923973.29..923975.29 rows=200 width=8) (actual time=75642.733..75642.746 rows=46 loops=1) Buffers: shared hit=247065 read=43761 written=1 -> HashAggregate (cost=886473.29..901473.29 rows=1500000 width=8) (actual time=74383.094..75303.676 rows=1500000 loops=1) Buffers: shared hit=247065 read=43761 written=1 -> Hash Right Join (cost=69028.00..811931.27 rows=14908404 width=8) (actual time=3362.428..55061.613 rows=15354647 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=247065 read=43761 written=1 -> Seq Scan on orders (cost=0.00..444735.19 rows=14908404 width=8) (actual time=0.042..15398.695 rows=14854629 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160371 Buffers: shared hit=247062 read=8486 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3360.396..3360.396 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=3 read=35275 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=1.539..2492.381 rows=1500000 loops=1) Buffers: shared hit=3 read=35275 Total runtime: 75670.830 ms (21 rows) COMMIT; COMMIT