Stored procedure is executing with different indexes when called via Entity Framework compared to SSMS

余生长醉 提交于 2019-12-13 17:14:10

问题


We have a stored procedure that executes dynamic sql via sp_executesql.

We have observed via the SQL Server profiler and looking at the execution plans the profiler shows that when this procedure is called via SSMS (SQL Server Management Studio) it uses a good combination of indexes, therefore returning in 2 secs.

On the other hand, when we call this procedure via our .NET application (called via Entity Framework) - and exactly the same parameters are used compared to the call in SSMS - then SQL Server uses a much worse choice for the indexes.

And this behaviour is deterministic. Called via our app - bad index. Called via SSMS - good index.

Does anybody have an idea why this could be the case?

Many thanks.

Update:

The stored procedure call looks like this: EXEC [schema].[sp] @a=123,@b=NULL

The dynamic SQL inside the procedure is executed like this:

   `EXEC sp_executesql
    @sql,
    N'@a int, @b smallint',
    @a = @a,
    @b = @b`

回答1:


I found the answer: I focused two much on entity framework, but when I widened my search I found answers about ADO.NET using different settings on the connection, therefore using a different execution plan: Why is some sql query much slower when used with SqlCommand?



来源:https://stackoverflow.com/questions/28222474/stored-procedure-is-executing-with-different-indexes-when-called-via-entity-fram

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