Linq to SQL性能与分组

时间:2012-05-08 15:31:55

标签: sql-server performance linq tsql linq-to-sql

我的问题是关于Linq to SQL Performance,我有一个SQL字符串并将其转换为Linq to sql:

SQL查询:

SELECT CONVERT(VARCHAR(10), ClockIn, 103) AS ClockDate, MIN(ClockIn) AS ClockIn, MAX(ClockOut) AS ClockOut, SUM(DATEDIFF(MINUTE, ClockIn, ClockOut)) AS [TotalTime]
FROM TimeLog
WHERE (EmployeeId = 10)
GROUP BY CONVERT(VARCHAR(10), ClockIn, 103)
ORDER BY ClockIn DESC

LINQ查询:

From u In objDC.TimeLogs
Where u.EmployeeId = 10
Group By Key = New With {u.ClockIn.Year, u.ClockIn.Month, u.ClockIn.Day} Into G = Group
Order By G.First.ClockIn Descending
Select New With {.ClockDate = Key.Day & "/" & Key.Month & "/" & Key.Year,
 .ClockIn = G.Min(Function(p) p.ClockIn),
 .ClockOut = G.Max(Function(p) p.ClockOut),
 .TotalTime = G.Sum(Function(p) SqlMethods.DateDiffMinute(p.ClockIn, p.ClockOut))}

SQL分析器中LINQ生成的查询字符串为:

SELECT [t4].[value] AS [ClockDate], [t4].[value2] AS [ClockIn2], [t4].[value22] AS [ClockOut], [t4].[value3] AS [TotalTime]
 FROM (
 SELECT ((((CONVERT(NVarChar,[t3].[value32])) + '/') + (CONVERT(NVarChar,[t3].[value222]))) + '/') + (CONVERT(NVarChar,[t3].[value22])) AS [value], [t3].[value] AS [value2], [t3].[value2] AS [value22], [t3].[value3], [t3].[value22] AS [value222], [t3].[value222] AS [value2222], [t3].[value32]
 FROM (
 SELECT MIN([t2].[ClockIn]) AS [value], MAX([t2].[ClockOut]) AS [value2], SUM([t2].[value]) AS [value3], [t2].[value2] AS [value22], [t2].[value22] AS [value222], [t2].[value3] AS [value32]
 FROM (
 SELECT DATEDIFF(Minute, [t1].[ClockIn], [t1].[ClockOut]) AS [value], [t1].[EmployeeId], [t1].[value] AS [value2], [t1].[value2] AS [value22], [t1].[value3], [t1].[ClockIn], [t1].[ClockOut]
 FROM (
 SELECT DATEPART(Year, [t0].[ClockIn]) AS [value], DATEPART(Month, [t0].[ClockIn]) AS [value2], DATEPART(Day, [t0].[ClockIn]) AS [value3], [t0].[ClockIn], [t0].[ClockOut], [t0].[EmployeeId]
 FROM [dbo].[TimeLog] AS [t0]
 ) AS [t1]
 ) AS [t2]
 WHERE [t2].[EmployeeId] = 10
 GROUP BY [t2].[value2], [t2].[value22], [t2].[value3]
 ) AS [t3]
 ) AS [t4]
 ORDER BY (
 SELECT [t6].[ClockIn]
 FROM (
 SELECT TOP (1) [t5].[ClockIn]
 FROM [dbo].[TimeLog] AS [t5]
 WHERE ((([t4].[value222] IS NULL) AND (DATEPART(Year, [t5].[ClockIn]) IS NULL)) OR (([t4].[value222] IS NOT NULL) AND (DATEPART(Year, [t5].[ClockIn]) IS NOT NULL) AND ((([t4].[value222] IS NULL) AND (DATEPART(Year, [t5].[ClockIn]) IS NULL)) OR (([t4].[value222] IS NOT NULL) AND (DATEPART(Year, [t5].[ClockIn]) IS NOT NULL) AND ([t4].[value222] = DATEPART(Year, [t5].[ClockIn])))))) AND ((([t4].[value2222] IS NULL) AND (DATEPART(Month, [t5].[ClockIn]) IS NULL)) OR (([t4].[value2222] IS NOT NULL) AND (DATEPART(Month, [t5].[ClockIn]) IS NOT NULL) AND ((([t4].[value2222] IS NULL) AND (DATEPART(Month, [t5].[ClockIn]) IS NULL)) OR (([t4].[value2222] IS NOT NULL) AND (DATEPART(Month, [t5].[ClockIn]) IS NOT NULL) AND ([t4].[value2222] = DATEPART(Month, [t5].[ClockIn])))))) AND ((([t4].[value32] IS NULL) AND (DATEPART(Day, [t5].[ClockIn]) IS NULL)) OR (([t4].[value32] IS NOT NULL) AND (DATEPART(Day, [t5].[ClockIn]) IS NOT NULL) AND ((([t4].[value32] IS NULL) AND (DATEPART(Day, [t5].[ClockIn]) IS NULL)) OR (([t4].
 [value32] IS NOT NULL) AND (DATEPART(Day, [t5].[ClockIn]) IS NOT NULL) AND ([t4].[value32] = DATEPART(Day, [t5].[ClockIn])))))) AND ([t5].[EmployeeId] = 10)
 ) AS [t6]
 ) DESC

LINQ to SQL太慢了,生成的查询与SQL Query的执行计划与人类编写的SQL查询相比为7%,而Linq生成的查询则为97%。

我的Linq to SQL查询出了什么问题?或者它是Linq的表现和限制?

2 个答案:

答案 0 :(得分:4)

我认为问题是你在OrderBy G.First语句中访问每个组的行并在Linq-to-SQL中触发N + 1行为,你能尝试类似的东西吗?

var query = objDC.TimeLogs
            .Where(c => c.EmployeeId == 10)
            .GroupBy(c => c.ClockIn.Date)
            .OrderBy(g => g.Key)
            .Select(g => new
            {
                Date = g.Key,
                ClockIn = g.Min(c => c.ClockIn),
                ClockOut = g.Max(c => c.ClockOut),
            })
            .Select(g => new 
            {
                g.Date,
                g.ClockIn,
                g.ClockOut,
                TotalTime = g.ClockOut - g.ClockIn
            });

答案 1 :(得分:0)

同样,这是基于Guillaume建议的linq查询。

非常感谢Guillaume解决了这个问题,我同意你的看法,这个问题与G.First有关。

我根据你的回答改变了我的Linq查询:

From u In objDC.TimeLogs
Where u.EmployeeId = 10
Group By key = New With {u.ClockIn.Date} Into G = Group
Order By key.Date Descending
Select New With {
    .ClockDate = key.Date,
    .ClockIn = G.Min(Function(p) p.ClockIn),
    .ClockOut = G.Max(Function(p) p.ClockOut),
    .TotalTime = G.Sum(Function(p) SqlMethods.DateDiffMinute(p.ClockIn, p.ClockOut)) / 60}

我得到了相同的结果但查询速度要快得多,并且分析器给了我55%的书面查询和45%的新生成的查询,它比原始的字符串查询更快。

非常感谢你的帮助。