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=927553.56..927554.06 rows=200 width=8) (actual time=64344.770..64344.775 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=290595 read=993 -> HashAggregate (cost=927543.91..927545.91 rows=200 width=8) (actual time=64344.614..64344.621 rows=45 loops=1) Buffers: shared hit=290592 read=993 -> HashAggregate (cost=890043.91..905043.91 rows=1500000 width=8) (actual time=63132.365..64009.753 rows=1500000 loops=1) Buffers: shared hit=290592 read=993 -> Hash Right Join (cost=69028.00..815051.95 rows=14998393 width=8) (actual time=1388.727..45626.082 rows=15354056 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=290592 read=993 -> Seq Scan on orders (cost=0.00..446056.09 rows=14998393 width=8) (actual time=0.034..10933.444 rows=14854040 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 160960 Buffers: shared hit=255319 read=988 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1386.505..1386.505 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35273 read=5 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.018..769.023 rows=1500000 loops=1) Buffers: shared hit=35273 read=5 Total runtime: 64378.693 ms (21 rows) COMMIT; COMMIT