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=843562.04..843562.54 rows=200 width=8) (actual time=37342.360..37342.361 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=262084 read=26719 -> HashAggregate (cost=843552.40..843554.40 rows=200 width=8) (actual time=37342.208..37342.216 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=262081 read=26719 -> HashAggregate (cost=806052.40..821052.40 rows=1500000 width=8) (actual time=36214.649..37055.225 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=262081 read=26719 -> Hash Right Join (cost=69028.00..732098.63 rows=14790755 width=8) (actual time=1097.691..22210.867 rows=15352915 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=262081 read=26719 -> Seq Scan on orders (cost=0.00..441209.30 rows=14790755 width=8) (actual time=0.024..6862.870 rows=14852894 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162106 Buffers: shared hit=249111 read=4411 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1080.450..1080.450 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=12970 read=22308 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..529.860 rows=1500000 loops=1) Buffers: shared hit=12970 read=22308 Planning time: 4.684 ms Execution time: 37377.661 ms (24 rows) COMMIT; COMMIT