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%accounts%' 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=920628.97..920629.47 rows=200 width=8) (actual time=77873.046..77873.051 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=258838 read=30471 written=167 -> HashAggregate (cost=920619.32..920621.32 rows=200 width=8) (actual time=77872.921..77872.931 rows=46 loops=1) Buffers: shared hit=258835 read=30471 written=167 -> HashAggregate (cost=883119.32..898119.32 rows=1500000 width=8) (actual time=76747.258..77528.355 rows=1500000 loops=1) Buffers: shared hit=258835 read=30471 written=167 -> Hash Right Join (cost=69028.00..808719.04 rows=14880057 width=8) (actual time=2339.364..54162.866 rows=15353984 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=258835 read=30471 written=167 -> Seq Scan on orders (cost=0.00..442089.90 rows=14880057 width=8) (actual time=0.048..12719.289 rows=14853968 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161032 Buffers: shared hit=238490 read=15538 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2337.264..2337.264 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=20345 read=14933 written=167 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.019..1484.781 rows=1500000 loops=1) Buffers: shared hit=20345 read=14933 written=167 Total runtime: 78864.580 ms (21 rows) COMMIT; COMMIT