Optimize joins with similar subqueries