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 '%special%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=922682.09..922682.59 rows=200 width=8) (actual time=64760.206..64760.208 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=285984 read=4084 written=161 -> HashAggregate (cost=922672.45..922674.45 rows=200 width=8) (actual time=64760.080..64760.090 rows=46 loops=1) Buffers: shared hit=285981 read=4084 written=161 -> HashAggregate (cost=885172.45..900172.45 rows=1500000 width=8) (actual time=63553.537..64417.304 rows=1500000 loops=1) Buffers: shared hit=285981 read=4084 written=161 -> Hash Right Join (cost=69028.00..810625.72 rows=14909346 width=8) (actual time=1550.848..45010.133 rows=15352895 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=285981 read=4084 written=161 -> Seq Scan on orders (cost=0.00..443410.80 rows=14909346 width=8) (actual time=0.055..10869.144 rows=14852877 loops=1) Filter: ((o_comment)::text !~~ '%special%deposits%'::text) Rows Removed by Filter: 162123 Buffers: shared hit=250703 read=4084 written=161 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1548.683..1548.683 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.021..837.082 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 64793.166 ms (21 rows) COMMIT; COMMIT