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%accounts%' 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=919442.92..919443.42 rows=200 width=8) (actual time=64909.778..64909.781 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=271043 read=18013 written=45 -> HashAggregate (cost=919433.28..919435.28 rows=200 width=8) (actual time=64909.677..64909.685 rows=46 loops=1) Buffers: shared hit=271040 read=18013 written=45 -> HashAggregate (cost=881933.28..896933.28 rows=1500000 width=8) (actual time=63898.774..64589.285 rows=1500000 loops=1) Buffers: shared hit=271040 read=18013 written=45 -> Hash Right Join (cost=69028.00..807682.14 rows=14850227 width=8) (actual time=1471.491..44938.641 rows=15353957 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=271040 read=18013 written=45 -> Seq Scan on orders (cost=0.00..441649.60 rows=14850227 width=8) (actual time=0.037..9654.996 rows=14853941 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161059 Buffers: shared hit=241800 read=11975 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1469.644..1469.644 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=29240 read=6038 written=45 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..758.232 rows=1500000 loops=1) Buffers: shared hit=29240 read=6038 written=45 Total runtime: 64948.071 ms (21 rows) COMMIT; COMMIT