开发者

Including value from temp table slows down query

开发者 https://www.devze.com 2023-03-10 21:25 出处:网络
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:

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 int

  • lack of index on MainTable.RootVal
    Why have Id as PK and then JOIN on another column?

0

精彩评论

暂无评论...
验证码 换一张
取 消