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%requests%' 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=851552.69..851553.19 rows=200 width=8) (actual time=25037.419..25037.422 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=232517 read=58312 -> HashAggregate (cost=851543.05..851545.05 rows=200 width=8) (actual time=25037.330..25037.338 rows=46 loops=1) Group Key: count(orders.o_orderkey) Buffers: shared hit=232514 read=58312 -> HashAggregate (cost=814043.05..829043.05 rows=1500000 width=8) (actual time=24325.905..24741.621 rows=1500000 loops=1) Group Key: customer.c_custkey Buffers: shared hit=232514 read=58312 -> Hash Right Join (cost=69028.00..738973.08 rows=15013993 width=8) (actual time=1029.803..16494.088 rows=15338732 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=232514 read=58312 -> Seq Scan on orders (cost=0.00..444735.19 rows=15013993 width=8) (actual time=0.036..7284.654 rows=14838714 loops=1) Filter: ((o_comment)::text !~~ '%unusual%requests%'::text) Rows Removed by Filter: 161286 Buffers: shared hit=232512 read=23036 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1020.407..1020.407 rows=1500000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 69119kB Buffers: shared hit=2 read=35276 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.006..631.507 rows=1500000 loops=1) Buffers: shared hit=2 read=35276 Planning time: 2.896 ms Execution time: 25053.756 ms (24 rows) COMMIT; COMMIT