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%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=849179.63..849180.13 rows=200 width=8) (actual time=48133.818..48133.822 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=221867 read=68455 written=452 -> HashAggregate (cost=849169.99..849171.99 rows=200 width=8) (actual time=48133.707..48133.719 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=221864 read=68455 written=452 -> HashAggregate (cost=811669.99..826669.99 rows=1500000 width=8) (actual time=47128.036..47802.771 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=221864 read=68455 written=452 -> Hash Right Join (cost=69028.00..736972.71 rows=14939457 width=8) (actual time=1210.209..26121.822 rows=15352979 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=221864 read=68455 written=452 -> Seq Scan on orders (cost=0.00..443852.85 rows=14939457 width=8) (actual time=0.030..8454.829 rows=14852958 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162042 Buffers: shared hit=195262 read=59779 written=447 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1193.515..1193.515 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=26602 read=8676 written=5 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..634.907 rows=1500000 loops=1) Buffers: shared hit=26602 read=8676 written=5 Planning time: 2.943 ms Execution time: 48159.274 ms (24 rows) COMMIT; COMMIT