Hi All,
The following queries give the same output. They have written differently.
But performance wise I do not know which one would be better or less expensive. How SQL server treats these two queries differently?
Can anyone tell me which one is more efficient?
Thanks,
techiPA
The following queries give the same output. They have written differently.
But performance wise I do not know which one would be better or less expensive. How SQL server treats these two queries differently?
Can anyone tell me which one is more efficient?
Code:
declare
@site int, @study int, @tg varchar(10),
@mattype varchar(50)
set @site = 0
set @study = 114
set @tg = 3
declare @seq int
--First
SELECT @seq=max(SeqNo)
FROM Transform t
INNER JOIN MatSite m
ON t.Site = m.Site
and t.Study = m.Study
and t.MatID = m.MatID
WHERE t.Site = @site
AND t.Study = @study
AND m.TG = @tg
AND t.TranType in (1, 15, 18, 19)
SELECT top 1 m.MatID, SeqNo
FROM MatSite m
LEFT JOIN Transform t
ON m.Site = t.Site
and m.Study = t.Study
and m.MatID = t.MatID
WHERE m.Site = @site
AND m.Study = @study
AND m.TG = @tg
AND t.MatID is null
AND m.SeqNo >= ISNULL(@seq,0)
ORDER BY m.SeqNo, m.MatID
--Or Second
SELECT top 1 m.MatID, SeqNo
FROM MatSite m
LEFT JOIN Transform t
ON m.Site = t.Site
and m.Study = t.Study
and m.MatID = t.MatID
WHERE m.Site = @site
AND m.Study = @study
AND m.TG = @tg
AND t.MatID is null
AND m.SeqNo >= ISNULL((SELECT max(SeqNo)
FROM Transform t
INNER JOIN MatSite m
ON t.Site = m.Site
and t.Study = m.Study
and t.MatID = m.MatID
WHERE t.Site = @site
AND t.Study = @study
AND m.TG = @tg
AND t.TranType in (1, 15, 18, 19)) , 0)
ORDER BY m.SeqNo, m.MatID
Thanks,
techiPA