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 '%express%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=929445.73..929446.23 rows=200 width=8) (actual time=68216.328..68216.332 rows=46 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=285783 read=5805 written=248 -> HashAggregate (cost=929436.09..929438.09 rows=200 width=8) (actual time=68216.210..68216.220 rows=46 loops=1) Buffers: shared hit=285780 read=5805 written=248 -> HashAggregate (cost=891936.09..906936.09 rows=1500000 width=8) (actual time=66987.433..67804.046 rows=1500000 loops=1) Buffers: shared hit=285780 read=5805 written=248 -> Hash Right Join (cost=69028.00..816565.69 rows=15074080 width=8) (actual time=1235.362..49219.530 rows=15353498 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=285780 read=5805 written=248 -> Seq Scan on orders (cost=0.00..446056.09 rows=15074080 width=8) (actual time=0.036..12838.882 rows=14853477 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161523 Buffers: shared hit=250503 read=5804 written=248 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1233.291..1233.291 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=35277 read=1 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.022..682.155 rows=1500000 loops=1) Buffers: shared hit=35277 read=1 Total runtime: 68242.936 ms (21 rows) COMMIT; COMMIT