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=71572.516..71572.518 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=284008 read=7580 written=116 -> HashAggregate (cost=927922.19..927924.19 rows=200 width=8) (actual time=71572.389..71572.401 rows=46 loops=1) Buffers: shared hit=284005 read=7580 written=116 -> HashAggregate (cost=890422.19..905422.19 rows=1500000 width=8) (actual time=70361.716..71214.877 rows=1500000 loops=1) Buffers: shared hit=284005 read=7580 written=116 -> Hash Right Join (cost=69028.00..815354.57 rows=15013524 width=8) (actual time=2012.158..51287.439 rows=15338298 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=284005 read=7580 written=116 -> Seq Scan on orders (cost=0.00..446056.09 rows=15013524 width=8) (actual time=0.058..14544.542 rows=14838282 loops=1) Filter: ((o_comment)::text !~~ '%pending%accounts%'::text) Rows Removed by Filter: 161718 Buffers: shared hit=249761 read=6546 written=32 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=2010.525..2010.525 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=34244 read=1034 written=84 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..1205.542 rows=1500000 loops=1) Buffers: shared hit=34244 read=1034 written=84 Total runtime: 71598.439 ms (21 rows) COMMIT; COMMIT