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=920570.49..920570.99 rows=200 width=8) (actual time=74564.160..74564.164 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=148201 read=140855 written=6 -> HashAggregate (cost=920560.85..920562.85 rows=200 width=8) (actual time=74564.071..74564.079 rows=46 loops=1) Buffers: shared hit=148198 read=140855 written=6 -> HashAggregate (cost=883060.85..898060.85 rows=1500000 width=8) (actual time=73401.014..74217.704 rows=1500000 loops=1) Buffers: shared hit=148198 read=140855 written=6 -> Hash Right Join (cost=69028.00..808584.20 rows=14895330 width=8) (actual time=2240.940..52923.558 rows=15353957 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=148198 read=140855 written=6 -> Seq Scan on orders (cost=0.00..441649.60 rows=14895330 width=8) (actual time=0.039..12330.528 rows=14853941 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161059 Buffers: shared hit=118874 read=134901 written=6 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2238.984..2238.984 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=29324 read=5954 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.011..808.681 rows=1500000 loops=1) Buffers: shared hit=29324 read=5954 Total runtime: 74625.077 ms (21 rows) COMMIT; COMMIT