SQL Server Query: Fast with Literal but Slow with Variable

前端 未结 8 1840
清歌不尽
清歌不尽 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:52

    Probably it is because in the parameter case, the optimizer cannot know that the value is not null, so it needs to create a plan that returns correct results even when it is. If you have SQL Server 2008 SP1 you can try adding OPTION(RECOMPILE) to the query.

提交回复
热议问题