A 5sec SP hitting a 30sec timeout through LINQ-to-SQL

喜夏-厌秋 提交于 2019-12-24 03:09:38

问题


I have a SP that executes in 5 seconds through SSMS

When that same SP is executed through a LINQ-to-SQL excel add-in it times out after 30 seconds (simpler queries for that same SP take a long time but return results)

I then changed the SP so that it reassigns all the input parameters to new local parameters inside the SP. That made the SP run in 36seconds in SSMS (so there is the reason why SSMS was so fast to begin with)

So I'm guessing that SQL server isn't making use of parameter sniffing for my LINQ-to-SQL queries?

So, my question is, is there any way to make the SP as fast in LINQ-to-SQL as it is in SSMS (with it's parameter sniffing)


回答1:


SQL Server optimizes stored procedures the same way whether you call them from SSMS or from LINQ. But it does use plan caching. A plan is stored for later reuse with the same login + ansi settings. The first values passed in can determine how the plan looks. If a different login/settings starts with different values, that can result in a different cached plan. That's one explanation for performance differences between LINQ and SSMS.

To reset all cached plans, use:

DBCC FREEPROCCACHE

In order to have the SP optimized for exactly the values you're calling with, you could use with recompile:

create procedure dbo.MySP with recompile as ...

This causes the procedure to be compiled for every invocation. This would negate parameterization.

(Your situation is rather unusual. SQL Server has an option to force parameterization, but there's no option to prevent it.)



来源:https://stackoverflow.com/questions/6986607/a-5sec-sp-hitting-a-30sec-timeout-through-linq-to-sql

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