Executing stored proc from DotNet takes very long but in SSMS it is immediate

。_饼干妹妹 提交于 2019-12-01 04:14:27

Since my comment seemed to provide the correct answer, I decided to move it into a full answer for posterity in the spirit of stackoverflow.

Your problem seems to be caused by SQL Server's Parameter Sniffing. To prevent it, just assign your incoming parameter values to other variables declared right at the top of your SP.

See this nice Article about it

Example:

CREATE PROCEDURE dbo.MyProcedure
(
    @Param1 INT
)
AS

declare @MyParam1 INT
set @MyParam1 = @Param1

SELECT * FROM dbo.MyTable WHERE ColumnName = @MyParam1 

GO

I copied this information from eggheadcafe.com.

Edit: As per Johann Strydom's comment, here is another option: Optimize Parameter Driven Queries with SQL Server OPTIMIZE FOR Hint.

Just recreated the stored proc and that fixed it. Very strange indeed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!