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%deposits%' 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=929928.61..929929.11 rows=200 width=8) (actual time=95578.856..95578.860 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=236721 read=55373 written=6 -> HashAggregate (cost=929918.96..929920.96 rows=200 width=8) (actual time=95578.741..95578.752 rows=45 loops=1) Buffers: shared hit=236718 read=55373 written=6 -> HashAggregate (cost=892418.96..907418.96 rows=1500000 width=8) (actual time=94199.159..95217.379 rows=1500000 loops=1) Buffers: shared hit=236718 read=55373 written=6 -> Hash Right Join (cost=69028.00..817128.11 rows=15058171 width=8) (actual time=4350.242..67984.493 rows=15352974 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=236718 read=55373 written=6 -> Seq Scan on orders (cost=0.00..446936.69 rows=15058171 width=8) (actual time=0.050..19092.548 rows=14852953 loops=1) Filter: ((o_comment)::text !~~ '%pending%deposits%'::text) Rows Removed by Filter: 162047 Buffers: shared hit=236715 read=20098 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=4348.434..4348.434 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=3 read=35275 written=6 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.012..3484.101 rows=1500000 loops=1) Buffers: shared hit=3 read=35275 written=6 Total runtime: 95630.525 ms (21 rows) COMMIT; COMMIT