gpt4 book ai didi

sql-server - Linq to SQL 性能与分组

转载 作者:行者123 更新时间:2023-12-03 21:16:54 26 4
gpt4 key购买 nike

我的问题是关于 Linq to SQL 性能,我有一个 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 查询相比,人工编写的 SQL 查询为 7%,Linq 生成查询为 97%。

我的 Linq to SQL 查询有什么问题?还是 Linq 的性能和限制?

最佳答案

我认为问题在于您在 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
});

关于sql-server - Linq to SQL 性能与分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10501742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com