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 '%pending%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=848736.58..848737.08 rows=200 width=8) (actual time=46030.691..46030.694 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=215495 read=74573 written=1192 -> HashAggregate (cost=848726.94..848728.94 rows=200 width=8) (actual time=46030.573..46030.587 rows=44 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=215492 read=74573 written=1192 -> HashAggregate (cost=811226.94..826226.94 rows=1500000 width=8) (actual time=44963.537..45701.478 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=215492 read=74573 written=1192 -> Hash Right Join (cost=69028.00..736529.91 rows=14939407 width=8) (actual time=1008.449..23612.886 rows=15352288 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=215492 read=74573 written=1192 -> Seq Scan on orders (cost=0.00..443410.80 rows=14939407 width=8) (actual time=0.028..8229.728 rows=14852270 loops=1) Filter: ((o_comment)::text !~~ '%pending%requests%'::text) Rows Removed by Filter: 162730 Buffers: shared hit=182567 read=72220 written=1192 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=994.239..994.239 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=32925 read=2353 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..546.275 rows=1500000 loops=1) Buffers: shared hit=32925 read=2353 Planning time: 3.368 ms Execution time: 46064.321 ms (24 rows) COMMIT; COMMIT