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%packages%' 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=924317.51..924318.01 rows=200 width=8) (actual time=68180.518..68180.521 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=227011 read=63565 dirtied=154 written=3038 -> HashAggregate (cost=924307.86..924309.86 rows=200 width=8) (actual time=68180.392..68180.404 rows=46 loops=1) Buffers: shared hit=227008 read=63565 dirtied=154 written=3038 -> HashAggregate (cost=886807.86..901807.86 rows=1500000 width=8) (actual time=67004.111..67844.002 rows=1500000 loops=1) Buffers: shared hit=227008 read=63565 dirtied=154 written=3038 -> Hash Right Join (cost=69028.00..812110.87 rows=14939399 width=8) (actual time=2353.572..47722.718 rows=15353299 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=227008 read=63565 dirtied=154 written=3038 -> Seq Scan on orders (cost=0.00..444294.89 rows=14939399 width=8) (actual time=0.077..11709.677 rows=14853282 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161718 Buffers: shared hit=227006 read=28289 dirtied=154 written=263 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2351.372..2351.372 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=2775 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.021..1546.280 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=2775 Total runtime: 68218.686 ms (21 rows) COMMIT; COMMIT