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%packages%' 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=852423.81..852424.31 rows=200 width=8) (actual time=37351.576..37351.580 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=209642 read=81693 written=1097 -> HashAggregate (cost=852414.17..852416.17 rows=200 width=8) (actual time=37351.391..37351.401 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=209639 read=81693 written=1097 -> HashAggregate (cost=814914.17..829914.17 rows=1500000 width=8) (actual time=36115.584..37011.474 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=209639 read=81693 written=1097 -> Hash Right Join (cost=69028.00..739846.57 rows=15013519 width=8) (actual time=1466.869..22934.379 rows=15337869 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=209639 read=81693 written=1097 -> Seq Scan on orders (cost=0.00..445615.79 rows=15013519 width=8) (actual time=0.037..9189.585 rows=14837848 loops=1) Filter: ((o_comment)::text !~~ '%unusual%packages%'::text) Rows Removed by Filter: 162152 Buffers: shared hit=174361 read=81693 written=1097 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1457.521..1457.521 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.025..866.155 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 4.426 ms Execution time: 37380.990 ms (24 rows) COMMIT; COMMIT