There are 3 tables that I need to Join with different filters.
EG:
Select 'First' as Header, *
from A join B on A.ID=B.ID
where A.Type=1 and B.Startdate>Getdate()
Union
Select 'Second' as Header, *
from A join B on A.ID=B.ID
where A.Type=2 and B.Startdate=Getdate()
Union
Select 'Third' as Header, *
from A join B on A.ID=B.ID
where A.Type=3 and B.Startdate<Getdate()
Is any impact will be in performance change if above rewritten as
With CTE
as
(
Select *
from A join B on A.ID=B.ID
Where A.Type in (1,2,3)
)
Select 'First' as Header, *
from CTE
where Type=1 and B.Startdate>Getdate()
Union
Select 'Second' as Header, *
from CTE
where Type=2 and B.Startdate=Getdate()
Union
Select 'Third' as Header, *
from CTE
where Type=3 and B.Startdate<Getdate()
Both Table A and B have over 100k records
I have noticed, Execution Query plan seems to be same for both queries but little variation in Physical, Logical and read-ahead reads with execution timing differing in milliseconds.
Does the above CTE better in performance wise than select mentioned at the start of the segment or not. Is any other methods to get above results with better performance.