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%deposits%' 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=926801.21..926801.71 rows=200 width=8) (actual time=63003.868..63003.871 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=249838 read=41750 written=84 -> HashAggregate (cost=926791.56..926793.56 rows=200 width=8) (actual time=63003.729..63003.738 rows=46 loops=1) Buffers: shared hit=249835 read=41750 written=84 -> HashAggregate (cost=889291.56..904291.56 rows=1500000 width=8) (actual time=61902.884..62673.587 rows=1500000 loops=1) Buffers: shared hit=249835 read=41750 written=84 -> Hash Right Join (cost=69028.00..814450.07 rows=14968299 width=8) (actual time=2162.554..45368.317 rows=15352954 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=249835 read=41750 written=84 -> Seq Scan on orders (cost=0.00..446056.09 rows=14968299 width=8) (actual time=0.040..10243.893 rows=14852933 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162067 Buffers: shared hit=249833 read=6474 written=57 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2159.906..2159.906 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 written=27 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.026..1369.862 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 written=27 Total runtime: 63031.378 ms (21 rows) COMMIT; COMMIT