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=919065.72..919066.22 rows=200 width=8) (actual time=66753.099..66753.103 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=257235 read=31821 written=64 -> HashAggregate (cost=919056.08..919058.08 rows=200 width=8) (actual time=66752.989..66752.992 rows=46 loops=1) Buffers: shared hit=257232 read=31821 written=64 -> HashAggregate (cost=881556.08..896556.08 rows=1500000 width=8) (actual time=65567.627..66428.056 rows=1500000 loops=1) Buffers: shared hit=257232 read=31821 written=64 -> Hash Right Join (cost=69028.00..807380.38 rows=14835139 width=8) (actual time=2069.998..47120.947 rows=15353060 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=257232 read=31821 written=64 -> Seq Scan on orders (cost=0.00..441649.60 rows=14835139 width=8) (actual time=0.038..9207.726 rows=14853044 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161956 Buffers: shared hit=232274 read=21501 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2067.793..2067.793 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=24958 read=10320 written=64 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.008..759.337 rows=1500000 loops=1) Buffers: shared hit=24958 read=10320 written=64 Total runtime: 66817.460 ms (21 rows) COMMIT; COMMIT