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=853198.33..853198.83 rows=200 width=8) (actual time=31181.610..31181.612 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=219728 read=71354 written=670 -> HashAggregate (cost=853188.69..853190.69 rows=200 width=8) (actual time=31181.517..31181.526 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=219725 read=71354 written=670 -> HashAggregate (cost=815688.69..830688.69 rows=1500000 width=8) (actual time=30347.771..30866.436 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=219725 read=71354 written=670 -> Hash Right Join (cost=69028.00..740317.39 rows=15074260 width=8) (actual time=1009.085..19894.905 rows=15354672 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=219725 read=71354 written=670 -> Seq Scan on orders (cost=0.00..445175.49 rows=15074260 width=8) (actual time=0.031..9259.236 rows=14854654 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160346 Buffers: shared hit=188021 read=67780 written=664 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=995.946..995.946 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=31704 read=3574 written=6 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..563.070 rows=1500000 loops=1) Buffers: shared hit=31704 read=3574 written=6 Planning time: 2.972 ms Execution time: 31211.093 ms (24 rows) COMMIT; COMMIT