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=927931.83..927932.33 rows=200 width=8) (actual time=80979.551..80979.554 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=251609 read=39979 written=1 -> HashAggregate (cost=927922.19..927924.19 rows=200 width=8) (actual time=80979.422..80979.435 rows=46 loops=1) Buffers: shared hit=251606 read=39979 written=1 -> HashAggregate (cost=890422.19..905422.19 rows=1500000 width=8) (actual time=79680.577..80627.446 rows=1500000 loops=1) Buffers: shared hit=251606 read=39979 written=1 -> Hash Right Join (cost=69028.00..815354.57 rows=15013524 width=8) (actual time=3383.251..58444.736 rows=15353131 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=251606 read=39979 written=1 -> Seq Scan on orders (cost=0.00..446056.09 rows=15013524 width=8) (actual time=0.066..13980.738 rows=14853115 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161885 Buffers: shared hit=251604 read=4703 written=1 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=3381.601..3381.601 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.671..2135.649 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Total runtime: 81051.714 ms (21 rows) COMMIT; COMMIT