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 '%pending%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=922305.89..922306.39 rows=200 width=8) (actual time=58044.353..58044.358 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290049 read=19 -> HashAggregate (cost=922296.25..922298.25 rows=200 width=8) (actual time=58044.238..58044.252 rows=46 loops=1) Buffers: shared hit=290046 read=19 -> HashAggregate (cost=884796.25..899796.25 rows=1500000 width=8) (actual time=56800.629..57707.610 rows=1500000 loops=1) Buffers: shared hit=290046 read=19 -> Hash Right Join (cost=69028.00..810324.76 rows=14894298 width=8) (actual time=1021.042..40379.347 rows=15338258 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290046 read=19 -> Seq Scan on orders (cost=0.00..443410.80 rows=14894298 width=8) (actual time=0.043..9324.511 rows=14838242 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161758 Buffers: shared hit=254768 read=19 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1019.038..1019.038 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..592.946 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 58080.002 ms (21 rows) COMMIT; COMMIT