There are two tables one with the actual fact data(table1) and the other table(table2) with more like a tracker information on date. I am trying to get all the fact data from table1 where the date field is less than max(date) from table 2. Following is the query I am using
SELECT * FROM TABLE1 WHERE date < SELECT MAX(date) from TABLE2
When I check the execution plan, I see SQL server is trying to optimize the query by using an inner join instead of straightforward filter condition
When I replace the same subquery with a value, the execution plan shows a straightforward filter
SELECT * FROM TABLE1 WHERE date < '2023-03-01'
My understanding is that an inner join is a costly operation when compared with a filter. How can I optimize the where condition with a subquery to avoid inner join and run it before the main query and provide it as a value in the final where condition
TABLE1
in opposite directions so are likely entirely different sets of rows.2023-03-01
) allows the query planner to work smarter, as opposed to using an unknown / runtime valueSELECT MAX(date) from TABLE2
an inner join is a costly operation when compared with a filter That sounds right but I'm not sure if it's true. It's actually a very strange choice given that it doesn't actually have a list of dates to join to, it only has one. Are you absolutely certain the queries are functionally the same?