Linq to SQL生成的查询速度要慢得多

时间:2018-04-19 22:29:34

标签: linq-to-sql

我有一个SQL查询,当我在SQL Server Mgmt Studio中运行它时需要15秒才能完成。当我将它翻译为linq并在LINQPad中运行时,需要1:50才能完成。我是否完全搞砸了#34;翻译"到linq?

这是SQL:

WITH details AS (
    SELECT DISTINCT SerialNumber, OrgLevel3, OrgLevel4, [Status Label]
    FROM vw_PSM_AssetIntransit
    WHERE yyyyww = DATENAME(yy, GETDATE()) + RIGHT('00' + DATENAME(wk, GETDATE()), 2) AND pro_status <> 'retired'
)
SELECT OrgLevel3, OrgLevel4, COUNT(DISTINCT SerialNumber) Total,
    SUM(CASE WHEN [Status Label] NOT IN ('15-21 Days InTransit', '8-14 Days InTransit', 'over 21 Days InTransit') THEN 1 ELSE 0 END) Compliant
FROM details
GROUP BY OrgLevel3, OrgLevel4
ORDER BY 1, 2

这是LINQ我把它翻译成:

var now = DateTime.Now;
var dtf = System.Globalization.DateTimeFormatInfo.CurrentInfo;
var calendar = dtf.Calendar;

var yearWW = now.Year * 100 + calendar.GetWeekOfYear(now, dtf.CalendarWeekRule, dtf.FirstDayOfWeek);

var badStatusLabels = new string[] { "15-21 Days InTransit", "8-14 Days InTransit", "over 21 Days InTransit" };

var details = (
                from r in Vw_PSM_AssetIntransit
                where r.yyyyww == yearWW && r.Pro_status != "retired"
                select new { r.SerialNumber, r.OrgLevel3, r.OrgLevel4, r.StatusLabel }
              ).Distinct();

var data = from r in details
           group r by new { r.OrgLevel3, r.OrgLevel4 } into grp
           orderby grp.Key.OrgLevel3, grp.Key.OrgLevel4
           select new
           {
               OrgLevel3 = grp.Key.OrgLevel3,
               OrgLevel4 = grp.Key.OrgLevel4,
               Total = grp.Select(x => x.SerialNumber).Distinct().Count(),
               Compliant = grp.Sum(x => !badStatusLabels.Contains(x.StatusLabel) ? 1 : 0)
           };

Console.WriteLine(data);

这就是LINQPad显示SQL查询的结果:

-- Region Parameters
DECLARE @p0 Int = 201816
DECLARE @p1 NVarChar(1000) = 'retired'
DECLARE @p2 NVarChar(1000) = '15-21 Days InTransit'
DECLARE @p3 NVarChar(1000) = '8-14 Days InTransit'
DECLARE @p4 NVarChar(1000) = 'over 21 Days InTransit'
DECLARE @p5 Int = 1
DECLARE @p6 Int = 0
-- EndRegion
SELECT [t3].[OrgLevel3], [t3].[OrgLevel4], (
    SELECT COUNT(*)
    FROM (
        SELECT DISTINCT [t5].[SerialNumber]
        FROM (
            SELECT DISTINCT [t4].[SerialNumber], [t4].[OrgLevel3], [t4].[OrgLevel4], [t4].[Status Label]
            FROM [vw_PSM_AssetIntransit] AS [t4]
            WHERE ([t4].[yyyyww] = @p0) AND ([t4].[pro_status] <> @p1)
            ) AS [t5]
        WHERE ((([t3].[OrgLevel3] IS NULL) AND ([t5].[OrgLevel3] IS NULL)) OR (([t3].[OrgLevel3] IS NOT NULL) AND ([t5].[OrgLevel3] IS NOT NULL) AND ((([t3].[OrgLevel3] IS NULL) AND ([t5].[OrgLevel3] IS NULL)) OR (([t3].[OrgLevel3] IS NOT NULL) AND ([t5].[OrgLevel3] IS NOT NULL) AND ([t3].[OrgLevel3] = [t5].[OrgLevel3]))))) AND ((([t3].[OrgLevel4] IS NULL) AND ([t5].[OrgLevel4] IS NULL)) OR (([t3].[OrgLevel4] IS NOT NULL) AND ([t5].[OrgLevel4] IS NOT NULL) AND ((([t3].[OrgLevel4] IS NULL) AND ([t5].[OrgLevel4] IS NULL)) OR (([t3].[OrgLevel4] IS NOT NULL) AND ([t5].[OrgLevel4] IS NOT NULL) AND ([t3].[OrgLevel4] = [t5].[OrgLevel4])))))
        ) AS [t6]
    ) AS [Total], [t3].[value] AS [Compliant]
FROM (
    SELECT SUM([t2].[value]) AS [value], [t2].[OrgLevel3], [t2].[OrgLevel4]
    FROM (
        SELECT
            (CASE
                WHEN NOT ([t1].[Status Label] IN (@p2, @p3, @p4)) THEN @p5
                ELSE @p6
             END) AS [value], [t1].[OrgLevel3], [t1].[OrgLevel4]
        FROM (
            SELECT DISTINCT [t0].[SerialNumber], [t0].[OrgLevel3], [t0].[OrgLevel4], [t0].[Status Label]
            FROM [vw_PSM_AssetIntransit] AS [t0]
            WHERE ([t0].[yyyyww] = @p0) AND ([t0].[pro_status] <> @p1)
            ) AS [t1]
        ) AS [t2]
    GROUP BY [t2].[OrgLevel3], [t2].[OrgLevel4]
    ) AS [t3]
ORDER BY [t3].[OrgLevel3], [t3].[OrgLevel4]

0 个答案:

没有答案