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=928746.23..928746.73 rows=200 width=8) (actual time=59569.456..59569.459 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=194593 read=97248 written=4 -> HashAggregate (cost=928736.59..928738.59 rows=200 width=8) (actual time=59569.345..59569.357 rows=46 loops=1) Buffers: shared hit=194590 read=97248 written=4 -> HashAggregate (cost=891236.59..906236.59 rows=1500000 width=8) (actual time=58394.115..59243.838 rows=1500000 loops=1) Buffers: shared hit=194590 read=97248 written=4 -> Hash Right Join (cost=69028.00..816094.15 rows=15028488 width=8) (actual time=1333.176..42624.507 rows=15338126 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=194590 read=97248 written=4 -> Seq Scan on orders (cost=0.00..446496.39 rows=15028488 width=8) (actual time=0.036..9415.217 rows=14838105 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 161895 Buffers: shared hit=194588 read=61972 written=4 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1331.209..1331.209 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.010..791.752 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 59596.875 ms (21 rows) COMMIT; COMMIT