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%requests%' 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=928732.51..928733.01 rows=200 width=8) (actual time=69707.372..69707.378 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=253664 read=38177 -> HashAggregate (cost=928722.86..928724.86 rows=200 width=8) (actual time=69707.250..69707.260 rows=45 loops=1) Buffers: shared hit=253661 read=38177 -> HashAggregate (cost=891222.86..906222.86 rows=1500000 width=8) (actual time=68445.849..69345.190 rows=1500000 loops=1) Buffers: shared hit=253661 read=38177 -> Hash Right Join (cost=69028.00..816083.17 rows=15027939 width=8) (actual time=3133.498..50794.443 rows=15352527 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=253661 read=38177 -> Seq Scan on orders (cost=0.00..446496.39 rows=15027939 width=8) (actual time=0.046..13373.621 rows=14852508 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162492 Buffers: shared hit=253659 read=2901 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3131.525..3131.525 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.012..2282.993 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 69735.006 ms (21 rows) COMMIT; COMMIT