实体框架sp_executesql性能问题

时间:2015-09-17 12:07:49

标签: c# sql-server entity-framework tsql

我有以下C#代码

 selectedJobTypesIds.Each(x => matchingJobTypeIds.AddRange(context.GetDescendantJobTypes(x).Select(y => y.Id)));

当profiled提供以下SQL时

exec sp_executesql N'SELECT 
    [Extent2].[Id] AS [Id]
    FROM  [dbo].[JobTypeDescendantJobTypes] AS [Extent1]
    INNER JOIN [dbo].[JobTypes] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
    WHERE [Extent1].[ParentJobTypeId] = @p__linq__0',N'@p__linq__0 bigint',@p__linq__0=1 

当我在SQL Server Management Studio中运行此SQL代码时,需要8-9秒。如果我提取SQL并运行它,它会立即返回(见下文)

SELECT 
[Extent2].[Id] AS [Id]
FROM  [dbo].[JobTypeDescendantJobTypes] AS [Extent1]
INNER JOIN [dbo].[JobTypes] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
WHERE [Extent1].[ParentJobTypeId] = 2

为什么sp_executesql会遇到性能问题?如何在实体框架中解决这个问题?

0 个答案:

没有答案