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 '%special%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=929931.91..929932.41 rows=200 width=8) (actual time=74037.204..74037.204 rows=44 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=183937 read=108157 written=26 -> HashAggregate (cost=929922.26..929924.26 rows=200 width=8) (actual time=74037.062..74037.071 rows=44 loops=1) Buffers: shared hit=183934 read=108157 written=26 -> HashAggregate (cost=892422.26..907422.26 rows=1500000 width=8) (actual time=72838.780..73685.860 rows=1500000 loops=1) Buffers: shared hit=183934 read=108157 written=26 -> Hash Right Join (cost=69028.00..817130.75 rows=15058303 width=8) (actual time=2104.266..51856.937 rows=15352859 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=183934 read=108157 written=26 -> Seq Scan on orders (cost=0.00..446936.69 rows=15058303 width=8) (actual time=0.047..13107.329 rows=14852841 loops=1) Filter: ((o_comment)::text !~~ '%special%deposits%'::text) Rows Removed by Filter: 162159 Buffers: shared hit=178657 read=78156 written=26 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2101.829..2101.829 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=5277 read=30001 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..1331.463 rows=1500000 loops=1) Buffers: shared hit=5277 read=30001 Total runtime: 74073.825 ms (21 rows) COMMIT; COMMIT