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=844294.34..844294.84 rows=200 width=8) (actual time=44906.616..44906.620 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=228759 read=60297 written=122 -> HashAggregate (cost=844284.70..844286.70 rows=200 width=8) (actual time=44906.507..44906.523 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=228756 read=60297 written=122 -> HashAggregate (cost=806784.70..821784.70 rows=1500000 width=8) (actual time=43618.007..44574.081 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=228756 read=60297 written=122 -> Hash Right Join (cost=69028.00..732757.93 rows=14805355 width=8) (actual time=903.336..26210.184 rows=15354710 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=228756 read=60297 written=122 -> Seq Scan on orders (cost=0.00..441649.60 rows=14805355 width=8) (actual time=0.028..9269.106 rows=14854691 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160309 Buffers: shared hit=193480 read=60295 written=122 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=894.348..894.348 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35276 read=2 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..484.073 rows=1500000 loops=1) Buffers: shared hit=35276 read=2 Planning time: 2.981 ms Execution time: 44929.754 ms (24 rows) COMMIT; COMMIT