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 '%pending%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=919817.47..919817.97 rows=200 width=8) (actual time=79851.283..79851.289 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=268171 read=20885 -> HashAggregate (cost=919807.82..919809.82 rows=200 width=8) (actual time=79851.147..79851.155 rows=46 loops=1) Buffers: shared hit=268168 read=20885 -> HashAggregate (cost=882307.82..897307.82 rows=1500000 width=8) (actual time=78543.899..79493.094 rows=1500000 loops=1) Buffers: shared hit=268168 read=20885 -> Hash Right Join (cost=69028.00..807981.78 rows=14865209 width=8) (actual time=2016.579..56303.479 rows=15338207 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=268168 read=20885 -> Seq Scan on orders (cost=0.00..441649.60 rows=14865209 width=8) (actual time=0.051..14193.132 rows=14838191 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161809 Buffers: shared hit=232890 read=20885 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2014.235..2014.235 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.014..1238.487 rows=1500000 loops=1) Buffers: shared hit=35278 Total runtime: 79897.103 ms (21 rows) COMMIT; COMMIT