SQL Server Query: Fast with Literal but Slow with Variable

前端 未结 8 1827
清歌不尽
清歌不尽 2020-11-28 07:18

I have a view that returns 2 ints from a table using a CTE. If I query the view like this it runs in less than a second

SELECT * FROM view1 WHERE ID = 1
         


        
8条回答
  •  爱一瞬间的悲伤
    2020-11-28 07:51

    You could add an OPTIMIZE FOR hint to your query, e.g.

    DECLARE @id INT = 1
    SELECT * FROM View1 WHERE ID = @id OPTION (OPTIMIZE FOR (@ID = 1))
    

提交回复
热议问题