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%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=929877.46..929877.96 rows=200 width=8) (actual time=72113.141..72113.144 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=189155 read=102686 dirtied=164 written=3883 -> HashAggregate (cost=929867.81..929869.81 rows=200 width=8) (actual time=72112.974..72112.986 rows=45 loops=1) Buffers: shared hit=189152 read=102686 dirtied=164 written=3883 -> HashAggregate (cost=892367.81..907367.81 rows=1500000 width=8) (actual time=70956.619..71764.203 rows=1500000 loops=1) Buffers: shared hit=189152 read=102686 dirtied=164 written=3883 -> Hash Right Join (cost=69028.00..816999.13 rows=15073737 width=8) (actual time=2516.267..52415.577 rows=15339289 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=189152 read=102686 dirtied=164 written=3883 -> Seq Scan on orders (cost=0.00..446496.39 rows=15073737 width=8) (actual time=0.050..14698.479 rows=14839269 loops=1) Filter: ((o_comment)::text !~~ '%express%packages%'::text) Rows Removed by Filter: 160731 Buffers: shared hit=189150 read=67410 dirtied=164 written=1423 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2514.039..2514.039 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=2460 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.022..1748.531 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=2460 Total runtime: 72152.554 ms (21 rows) COMMIT; COMMIT