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.28..929445.78 rows=200 width=8) (actual time=68169.182..68169.184 rows=45 loops=1) Sort Key: (count(*)), (count(orders.o_orderkey)) Sort Method: quicksort Memory: 27kB Buffers: shared hit=289851 read=1737 written=5 -> HashAggregate (cost=929435.64..929437.64 rows=200 width=8) (actual time=68169.073..68169.087 rows=45 loops=1) Buffers: shared hit=289848 read=1737 written=5 -> HashAggregate (cost=891935.64..906935.64 rows=1500000 width=8) (actual time=66946.961..67835.478 rows=1500000 loops=1) Buffers: shared hit=289848 read=1737 written=5 -> Hash Right Join (cost=69028.00..816565.33 rows=15074062 width=8) (actual time=1304.637..47926.441 rows=15338672 loops=1) Hash Cond: (orders.o_custkey = customer.c_custkey) Buffers: shared hit=289848 read=1737 written=5 -> Seq Scan on orders (cost=0.00..446056.09 rows=15074062 width=8) (actual time=0.034..11350.665 rows=14838651 loops=1) Filter: ((o_comment)::text !~~ '%express%requests%'::text) Rows Removed by Filter: 161349 Buffers: shared hit=255122 read=1185 written=5 -> Hash (cost=50278.00..50278.00 rows=1500000 width=4) (actual time=1302.479..1302.479 rows=1500000 loops=1) Buckets: 262144 Batches: 1 Memory Usage: 52735kB Buffers: shared hit=34726 read=552 -> Seq Scan on customer (cost=0.00..50278.00 rows=1500000 width=4) (actual time=0.014..731.009 rows=1500000 loops=1) Buffers: shared hit=34726 read=552 Total runtime: 68226.536 ms (21 rows) COMMIT; COMMIT