In the following query, the join to table title_deed is only required for the title_deed_no search criterion, and no fields are selected from it. If the @titleDeedNumber parameter is null, will a join still be performed? How much difference would this make to performance anyway? I am trying to replace dynamic generation of a big ugly T-SQL string, and the code that does that only adds a join if a title_deed_no value is supplied.
DECLARE @registrarId nchar(1)
DECLARE @titleDeedNumber nvarchar(50)
SELECT TOP 100
vw.*
FROM ACC.dbo.vw_Property_Nad vw
INNER JOIN title_deed td ON ( @titleDeedNumber IS NULL )
OR ( td.Prop_ID = vw.prop_id )
WHERE vw.Prop_ID IS NOT NULL
AND Registrar = isnull(@registrarId, vw.Registrar)
AND td.title_deed_no = isnull(@titleDeedNumber, td.title_deed_no)
I have tried examning the execution plan, but it is just too busy with joins from the vw_Property_Nad view, and I’d rather get an expert opinion anyway.
In this case, probably not.
You’re better off with:
(vw.Registrar = @registrarId OR @registrarId IS NULL)Also
I’d also consider an IF or UNION ALL