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=923982.93..923983.43 rows=200 width=8) (actual time=74726.220..74726.221 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=284614 read=6215 dirtied=77 -> HashAggregate (cost=923973.29..923975.29 rows=200 width=8) (actual time=74726.072..74726.084 rows=46 loops=1) Buffers: shared hit=284611 read=6215 dirtied=77 -> HashAggregate (cost=886473.29..901473.29 rows=1500000 width=8) (actual time=73377.441..74334.268 rows=1500000 loops=1) Buffers: shared hit=284611 read=6215 dirtied=77 -> Hash Right Join (cost=69028.00..811931.27 rows=14908404 width=8) (actual time=1725.855..53229.218 rows=15354647 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=284611 read=6215 dirtied=77 -> Seq Scan on orders (cost=0.00..444735.19 rows=14908404 width=8) (actual time=0.038..14911.348 rows=14854629 loops=1) Filter: ((o_comment)::text !~~ '%express%deposits%'::text) Rows Removed by Filter: 160371 Buffers: shared hit=249333 read=6215 dirtied=77 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1723.438..1723.438 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..953.949 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 74758.854 ms (21 rows) COMMIT; COMMIT