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=927500.93..927501.43 rows=200 width=8) (actual time=79631.346..79631.349 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=273183 read=18152 written=292 -> HashAggregate (cost=927491.29..927493.29 rows=200 width=8) (actual time=79631.216..79631.228 rows=46 loops=1) Buffers: shared hit=273180 read=18152 written=292 -> HashAggregate (cost=889991.29..904991.29 rows=1500000 width=8) (actual time=78286.867..79263.882 rows=1500000 loops=1) Buffers: shared hit=273180 read=18152 written=292 -> Hash Right Join (cost=69028.00..814921.79 rows=15013900 width=8) (actual time=2107.271..57776.739 rows=15353262 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=273180 read=18152 written=292 -> Seq Scan on orders (cost=0.00..445615.79 rows=15013900 width=8) (actual time=0.049..16814.561 rows=14853245 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161755 Buffers: shared hit=238214 read=17840 written=292 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2105.414..2105.414 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=34966 read=312 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.013..1266.507 rows=1500000 loops=1) Buffers: shared hit=34966 read=312 Total runtime: 79665.737 ms (21 rows) COMMIT; COMMIT