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=918320.37..918320.87 rows=200 width=8) (actual time=97387.396..97387.398 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=181334 read=107722 -> HashAggregate (cost=918310.72..918312.72 rows=200 width=8) (actual time=97387.299..97387.309 rows=46 loops=1) Buffers: shared hit=181331 read=107722 -> HashAggregate (cost=880810.72..895810.72 rows=1500000 width=8) (actual time=96252.514..97030.165 rows=1500000 loops=1) Buffers: shared hit=181331 read=107722 -> Hash Right Join (cost=69028.00..806784.10 rows=14805325 width=8) (actual time=2611.021..72202.180 rows=15353957 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=181331 read=107722 -> Seq Scan on orders (cost=0.00..441649.60 rows=14805325 width=8) (actual time=0.050..24217.470 rows=14853941 loops=1) Filter: ((o_comment)::text !~~ '%special%accounts%'::text) Rows Removed by Filter: 161059 Buffers: shared hit=151020 read=102755 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2609.181..2609.181 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=30311 read=4967 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..1567.943 rows=1500000 loops=1) Buffers: shared hit=30311 read=4967 Total runtime: 97430.995 ms (21 rows) COMMIT; COMMIT