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 '%unusual%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=853164.37..853164.87 rows=200 width=8) (actual time=28656.158..28656.160 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=211629 read=79959 written=1521 -> HashAggregate (cost=853154.73..853156.73 rows=200 width=8) (actual time=28656.054..28656.059 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=211626 read=79959 written=1521 -> HashAggregate (cost=815654.73..830654.73 rows=1500000 width=8) (actual time=27834.344..28341.026 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=211626 read=79959 written=1521 -> Hash Right Join (cost=69028.00..740512.07 rows=15028532 width=8) (actual time=1208.873..18417.245 rows=15352887 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=211626 read=79959 written=1521 -> Seq Scan on orders (cost=0.00..446056.09 rows=15028532 width=8) (actual time=0.031..8056.499 rows=14852868 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162132 Buffers: shared hit=211624 read=44683 written=1466 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1197.441..1197.441 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 written=55 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..750.356 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=55 Planning time: 3.366 ms Execution time: 28683.301 ms (24 rows) COMMIT; COMMIT