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 '%unusual%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=847721.26..847721.76 rows=200 width=8) (actual time=56498.988..56498.992 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=255410 read=33899 written=21 -> HashAggregate (cost=847711.62..847713.62 rows=200 width=8) (actual time=56498.789..56498.798 rows=45 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=255407 read=33899 written=21 -> HashAggregate (cost=810211.62..825211.62 rows=1500000 width=8) (actual time=55202.070..56161.176 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=255407 read=33899 written=21 -> Hash Right Join (cost=69028.00..735438.19 rows=14954686 width=8) (actual time=1382.994..30483.911 rows=15338064 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=255407 read=33899 written=21 -> Seq Scan on orders (cost=0.00..442089.90 rows=14954686 width=8) (actual time=0.028..9539.955 rows=14838045 loops=1) Filter: ((o_comment)::text !~~ '%unusual%deposits%'::text) Rows Removed by Filter: 161955 Buffers: shared hit=220129 read=33899 written=21 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1365.517..1365.517 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=35278 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.027..740.103 rows=1500000 loops=1) Buffers: shared hit=35278 Planning time: 4.078 ms Execution time: 56531.983 ms (24 rows) COMMIT; COMMIT