增加linq查询效率

时间:2015-01-12 10:13:33

标签: linq linq-to-entities

我正在优化我的Web应用程序,并且遇到了瓶颈,我的linq表达式生成的SQL非常慢。

以下SQL在一秒钟内执行:

SELECT
    ISNULL(COUNT(distinct JOBIDNumber),0),
    ISNULL(SUM(JIMQuantityActual * JIMNetMarginFactor),0),
    ISNULL(sum((isnull(MATRecoverablePercent,0) / 100) * JIMQuantityActual * JIMNetMarginFactor),0),
    ISNULL(sum(CarbonSaving),0)
FROM
    dbo.fn_GetJobsForUser(183486) jb
    inner join cd_JobMaterial on JIMJobId = jb.JOBIDNumber
WHERE
    JOBCollectionDate >= '2014-11-01'

而以下查询的sql输出在相同数据上需要4到16秒之间:

DateTime d = new DateTime(2014, 11, 1)

from job in sp.sp_GetJobsForUser(183486)
where job.JOBCollectionDate >= d
join material in UnitOfWork.Repository<cd_JobMaterial>().Queryable()
on job.JOBIDNumber equals material.JIMJobId 
group material by 1 into f
select new
{
     Jobs = f.Distinct().Count(),
     Weight = f.Sum(x=> x.JIMQuantityActual * x.JIMNetMarginFactor),
     Carbon = f.Sum(x=> x.CarbonSaving),
     Recovery = f.Sum(x => ((x.MATRecoverablePercent / 100) * x.JIMQuantityActual * x.JIMNetMarginFactor))
}

其中输出以下内容:

-- Region Parameters
DECLARE @contactId Int = 183486
DECLARE @p__linq__0 DateTime2 = '2014-11-01 00:00:00.0000000'
-- EndRegion

SELECT
    [Project4].[C1] AS [C1],
    [Project4].[C5] AS [C2],
    [Project4].[C2] AS [C3],
    [Project4].[C3] AS [C4],
    [Project4].[C4] AS [C5]
FROM (SELECT
    [Project2].[C1] AS [C1],
    [Project2].[C2] AS [C2],
    [Project2].[C3] AS [C3],
    [Project2].[C4] AS [C4],
    (SELECT
        COUNT(1) AS [A1]
    FROM (SELECT DISTINCT
        /*Fields omitted for brevity */
    FROM [dbo].[fn_GetJobsForUser](@contactId) AS [Extent3]
    INNER JOIN (SELECT
        /*Fields omitted for brevity */
    FROM [dbo].[cd_JobMaterial] AS [cd_JobMaterial]) AS [Extent4]
        ON [Extent3].[JOBIDNumber] = [Extent4].[JIMJobId]
    WHERE ([Extent3].[JOBCollectionDate] >= @p__linq__0)
    AND ([Project2].[C1] = 1)) AS [Distinct1])
    AS [C5]
FROM (SELECT
    @contactId AS [contactId],
    @p__linq__0 AS [p__linq__0],
    [GroupBy1].[K1] AS [C1],
    [GroupBy1].[A1] AS [C2],
    [GroupBy1].[A2] AS [C3],
    [GroupBy1].[A3] AS [C4]
FROM (SELECT
    [Project1].[K1] AS [K1],
    SUM([Project1].[A1]) AS [A1],
    SUM([Project1].[A2]) AS [A2],
    SUM([Project1].[A3]) AS [A3]
FROM (SELECT
    1 AS [K1],
    [Project1].[JIMQuantityActual] * [Project1].[JIMNetMarginFactor] AS [A1],
    [Project1].[CarbonSaving] AS [A2],
    ([Project1].[MATRecoverablePercent] / CAST(100 AS DECIMAL(18))) * [Project1].[JIMQuantityActual] * [Project1].[JIMNetMarginFactor] AS [A3]
FROM (SELECT
    [Extent2].[MATRecoverablePercent] AS [MATRecoverablePercent],
    [Extent2].[JIMQuantityActual] AS [JIMQuantityActual],
    [Extent2].[JIMNetMarginFactor] AS [JIMNetMarginFactor],
    [Extent2].[CarbonSaving] AS [CarbonSaving]
FROM [dbo].[fn_GetJobsForUser](@contactId) AS [Extent1]
INNER JOIN (SELECT
    /*Fields omitted for brevity */
FROM [dbo].[cd_JobMaterial] AS [cd_JobMaterial]) AS [Extent2]
    ON [Extent1].[JOBIDNumber] = [Extent2].[JIMJobId]
WHERE [Extent1].[JOBCollectionDate] >= @p__linq__0) AS [Project1]) AS [Project1]
GROUP BY [K1]) AS [GroupBy1]) AS [Project2]) AS [Project4]

如何重新编写linq表达式以生成更高效的sql,或者仅仅是编写存储过程并使用它的情况?

1 个答案:

答案 0 :(得分:1)

不幸的是,使用像Entity Framework一样灵活的东西的缺点之一是必须支持各种复杂的翻译。这显然在其他方面带来了很大的好处,所以你必须在性能方面平衡这些。

即使您可以找到一种方法来重写可以生成更好的SQL now 的查询,但在未来的版本中,这些更改会受到基础提供程序的更改。在 应用程序不再接受性能之前,尽可能享受干净,简洁的代码。如果EF没有在那时削减它,那么使用提供的一些钩子来执行原始的SQL,存储过程等,这些都不会很漂亮。