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=68215.080..68215.083 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=291295 read=546 written=33 -> HashAggregate (cost=928722.86..928724.86 rows=200 width=8) (actual time=68214.945..68214.957 rows=45 loops=1) Buffers: shared hit=291292 read=546 written=33 -> HashAggregate (cost=891222.86..906222.86 rows=1500000 width=8) (actual time=66951.475..67853.028 rows=1500000 loops=1) Buffers: shared hit=291292 read=546 written=33 -> Hash Right Join (cost=69028.00..816083.17 rows=15027939 width=8) (actual time=2072.765..49217.311 rows=15352527 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=291292 read=546 written=33 -> Seq Scan on orders (cost=0.00..446496.39 rows=15027939 width=8) (actual time=0.056..12910.161 rows=14852508 loops=1) Filter: ((o_comment)::text !~~ '%special%requests%'::text) Rows Removed by Filter: 162492 Buffers: shared hit=256014 read=546 written=33 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2070.630..2070.630 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.024..1280.087 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 68242.276 ms (21 rows) COMMIT; COMMIT