我發現,在某些情況下,像SQL服務器加入VS子查詢的性能問題
select
usertable.userid,
(select top 1 name from nametable where userid = usertable.userid) as name
from usertable
where active = 1
查詢所需的數量級不再SS2008R2完成比同等聯接查詢
select
usertable.userid,
nametable.name
from usertable
left join nametable on nametable.userid = usertable.userid
where usertable.active = 1
其中兩個表索引並有超過10萬行。有趣的是,插入頂部條款列入原始查詢使得它看齊聯接查詢執行:
select
top (select count(*) from usertable where active = 1) usertable.userid,
(select top 1 name from nametable where userid = usertable.userid) as name
from usertable
where active = 1
沒有人有任何想法,爲什麼原來的查詢執行如此糟糕?
FYI診斷性能問題時,你應該總是讓查詢執行計劃。 – Justin
如果您有權訪問Safari Books Online,請參閱「InsideMicrosoft®SQL Server 2005:查詢優化和優化 - 第3章」或另一個好鏈接http://blogs.msdn.com/b/craigfr/archive/2006 /09/27/774107.aspx –