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=923436.14..923436.64 rows=200 width=8) (actual time=67124.059..67124.061 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=283688 read=6380 written=336 -> HashAggregate (cost=923426.50..923428.50 rows=200 width=8) (actual time=67123.947..67123.953 rows=46 loops=1) Buffers: shared hit=283685 read=6380 written=336 -> HashAggregate (cost=885926.50..900926.50 rows=1500000 width=8) (actual time=65963.281..66789.788 rows=1500000 loops=1) Buffers: shared hit=283685 read=6380 written=336 -> Hash Right Join (cost=69028.00..811228.96 rows=14939508 width=8) (actual time=1514.548..47396.503 rows=15352895 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=283685 read=6380 written=336 -> Seq Scan on orders (cost=0.00..443410.80 rows=14939508 width=8) (actual time=0.042..11706.345 rows=14852877 loops=1) Filter: ((o_comment)::text !~~ '%special%deposits%'::text) Rows Removed by Filter: 162123 Buffers: shared hit=248407 read=6380 written=336 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1512.449..1512.449 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.022..844.259 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 67165.065 ms (21 rows) COMMIT; COMMIT