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=928369.63..928370.13 rows=200 width=8) (actual time=68353.250..68353.251 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=273177 read=18664 written=152 -> HashAggregate (cost=928359.99..928361.99 rows=200 width=8) (actual time=68353.125..68353.137 rows=46 loops=1) Buffers: shared hit=273174 read=18664 written=152 -> HashAggregate (cost=890859.99..905859.99 rows=1500000 width=8) (actual time=67063.162..67996.053 rows=1500000 loops=1) Buffers: shared hit=273174 read=18664 written=152 -> Hash Right Join (cost=69028.00..815792.87 rows=15013424 width=8) (actual time=1850.173..47909.133 rows=15353267 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=273174 read=18664 written=152 -> Seq Scan on orders (cost=0.00..446496.39 rows=15013424 width=8) (actual time=0.060..11279.396 rows=14853250 loops=1) Filter: ((o_comment)::text !~~ '%special%packages%'::text) Rows Removed by Filter: 161750 Buffers: shared hit=243301 read=13259 written=119 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1847.416..1847.416 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=29873 read=5405 written=33 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..1055.478 rows=1500000 loops=1) Buffers: shared hit=29873 read=5405 written=33 Total runtime: 68402.560 ms (21 rows) COMMIT; COMMIT