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=849448.69..849449.19 rows=200 width=8) (actual time=30549.945..30549.947 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=248845 read=41984 written=35 -> HashAggregate (cost=849439.05..849441.05 rows=200 width=8) (actual time=30549.825..30549.837 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=248842 read=41984 written=35 -> HashAggregate (cost=811939.05..826939.05 rows=1500000 width=8) (actual time=29657.953..30219.415 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=248842 read=41984 written=35 -> Hash Right Join (cost=69028.00..737395.08 rows=14908793 width=8) (actual time=996.690..19702.096 rows=15354647 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=248842 read=41984 written=35 -> Seq Scan on orders (cost=0.00..444735.19 rows=14908793 width=8) (actual time=0.031..9063.317 rows=14854629 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160371 Buffers: shared hit=213564 read=41984 written=35 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=985.103..985.103 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.017..563.952 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 3.099 ms Execution time: 31152.011 ms (24 rows) COMMIT; COMMIT