Including value from temp table slows down query
I have a stored procedure that uses a temporary table to make some joins in a select clause. The select clause contains the value from the开发者_Python百科 Id column of the temporary table like this:
CREATE TABLE #TempTable
(
Id INT PRIMARY KEY,
RootVal INT
)
The Select looks like this:
Select value1, value2, #TempTable.Id AS ValKey
From MainTable INNER JOIN #TempTable ON MainTable.RootVal = #TempTable.RootVal
The query takes over a minute to run in real life but if I remove the "#TempTable.Id" from the select list it runs in a second.
Does anyone know why there is such a huge cost to including a value from a #temp table compared to just using it in a join?
Most likely:
data type mismatch
eg nvarchar vs intlack of index on MainTable.RootVal
Why haveId
as PK and then JOIN on another column?
精彩评论