Re: Wasteful nested loop join when there is `limit` in the query

2025-02-17 Thread WU Yan
Thank you for your help, Tom. You are right. I added an index on employee.name (by making it unique), and then postgres can visit employee table in a pre-sorted manner, and can exit early without joining more rows. Just sharing the tweak I did to the example, if anyone else is interested in a qu

Re: Wasteful nested loop join when there is `limit` in the query

2025-02-16 Thread Tom Lane
WU Yan <4wu...@gmail.com> writes: > Hello everyone, I am still learning postgres planner and performance > optimization, so please kindly point out if I missed something obvious. An index on employee.name would likely help here. Even if we had an optimization for pushing LIMIT down through a join

Wasteful nested loop join when there is `limit` in the query

2025-02-16 Thread WU Yan
Hello everyone, I am still learning postgres planner and performance optimization, so please kindly point out if I missed something obvious. I've noticed that postgres joins all rows in two tables, even though there's a `limit` in the query. It means lots of joined rows are discarded eventually, a