With a subquery within the IN clause, am I better off returning a large number of results (10,000+ rows) or using table joins to restrict the results?
For example:
select * from Units u
inner join pm_Properties p on p.PropertyId = u.PropertyId
where p.State = 'CA'
and u.UnitId in (select UnitId from Leases l where l.IsActive = 1)
vs
select * from Units u
inner join pm_Properties p on p.PropertyId = u.PropertyId
where p.State = 'CA'
and u.UnitId in (select UnitId from Leases l
inner join Units u on u.UnitId = l.UnitId
inner join Properties p on p.PropertyId = u.PropertyId
where l.IsActive = 1 and p.State = 'CA')
In both cases, I do want to return unit results joined with properties. I’m just curious about the performance difference regarding the IN statement. Additionally, if it matters, the server is MS SQL 2008 R2.
Use the first one (unless you have performance problems for some reason).
The second one is just supplying redundant information that SQL server will be able to work out for itself – all you have done is increase the complexity of the query increasing the cost of working out the execution plan and increasing the chances that it won’t be able to find the optimal plan.