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=849751.39..849751.89 rows=200 width=8) (actual time=36734.419..36734.422 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=177369 read=113460 written=619 -> HashAggregate (cost=849741.75..849743.75 rows=200 width=8) (actual time=36734.318..36734.326 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=177366 read=113460 written=619 -> HashAggregate (cost=812241.75..827241.75 rows=1500000 width=8) (actual time=35813.194..36408.890 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=177366 read=113460 written=619 -> Hash Right Join (cost=69028.00..737622.11 rows=14923928 width=8) (actual time=1315.552..22404.614 rows=15339813 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=177366 read=113460 written=619 -> Seq Scan on orders (cost=0.00..444735.19 rows=14923928 width=8) (actual time=0.029..8963.055 rows=14839795 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160205 Buffers: shared hit=172277 read=83271 written=521 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1302.578..1302.578 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=5089 read=30189 written=98 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.011..776.846 rows=1500000 loops=1) Buffers: shared hit=5089 read=30189 written=98 Planning time: 3.131 ms Execution time: 36757.701 ms (24 rows) COMMIT; COMMIT