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 '%unusual%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=921000.77..921001.27 rows=200 width=8) (actual time=73972.596..73972.599 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=255389 read=33920 -> HashAggregate (cost=920991.13..920993.13 rows=200 width=8) (actual time=73972.481..73972.494 rows=45 loops=1) Buffers: shared hit=255386 read=33920 -> HashAggregate (cost=883491.13..898491.13 rows=1500000 width=8) (actual time=72710.756..73607.654 rows=1500000 loops=1) Buffers: shared hit=255386 read=33920 -> Hash Right Join (cost=69028.00..809016.48 rows=14894929 width=8) (actual time=2043.878..53013.836 rows=15352910 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=255386 read=33920 -> Seq Scan on orders (cost=0.00..442089.90 rows=14894929 width=8) (actual time=0.040..12131.785 rows=14852891 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 162109 Buffers: shared hit=244290 read=9738 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2041.218..2041.218 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=11096 read=24182 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..905.590 rows=1500000 loops=1) Buffers: shared hit=11096 read=24182 Total runtime: 74043.208 ms (21 rows) COMMIT; COMMIT