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=919002.97..919003.47 rows=200 width=8) (actual time=45860.837..45860.839 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=279900 read=8903 -> HashAggregate (cost=918993.33..918995.33 rows=200 width=8) (actual time=45860.704..45860.710 rows=45 loops=1) Buffers: shared hit=279897 read=8903 -> HashAggregate (cost=881493.33..896493.33 rows=1500000 width=8) (actual time=44638.219..45561.809 rows=1500000 loops=1) Buffers: shared hit=279897 read=8903 -> Hash Right Join (cost=69028.00..807242.12 rows=14850241 width=8) (actual time=891.265..32602.831 rows=15352885 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=279897 read=8903 -> Seq Scan on orders (cost=0.00..441209.30 rows=14850241 width=8) (actual time=0.038..8571.040 rows=14852865 loops=1) Filter: ((o_comment)::text !~~ '%special%deposits%'::text) Rows Removed by Filter: 162135 Buffers: shared hit=252101 read=1421 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=888.040..888.040 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=27796 read=7482 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.023..479.750 rows=1500000 loops=1) Buffers: shared hit=27796 read=7482 Total runtime: 45885.977 ms (21 rows) COMMIT; COMMIT