How can i improve the performance of this LINQ?

时间:2015-06-25 10:03:05

标签: c# sql-server linq

UPDATE thanks to @usr I have got this down to ~3 seconds simply by changing

.Select(
     log => log.OrderByDescending(
     d => d.DateTimeUTC
     ).FirstOrDefault()
)

to

    .Select(
     log => log.OrderByDescending(
     d => d.Id
     ).FirstOrDefault()
)

I have a database with two tables - Logs and Collectors - which I am using Entity Framework to read. There are 86 collector records and each one has 50000+ corresponding Log records.

I want to get the most recent log record for each collector which is easily done with this SQL

SELECT CollectorLogModels_1.Status, CollectorLogModels_1.NumericValue,
    CollectorLogModels_1.StringValue, CollectorLogModels_1.DateTimeUTC,
    CollectorSettingsModels.Target, CollectorSettingsModels.TypeName 
FROM
    (SELECT CollectorId, MAX(Id) AS Id 
     FROM CollectorLogModels GROUP BY CollectorId) AS RecentLogs 
INNER JOIN CollectorLogModels AS CollectorLogModels_1 
   ON RecentLogs.Id = CollectorLogModels_1.Id 
      INNER JOIN CollectorSettingsModels 
          ON CollectorLogModels_1.CollectorId = CollectorSettingsModels.Id

This takes ~2 seconds to execute.

the closest I have been able to get with LINQ is the following

var logs = context.Logs.Include(co => co.Collector)
                .GroupBy(
                    log => log.CollectorId, log => log
                )
                .Select(
                    log => log.OrderByDescending(
                        d => d.DateTimeUtc
                        ).FirstOrDefault()
                )
                .Join(
                    context.Collectors,
                    (l => l.CollectorId),
                    (c => c.Id),
                    (l, c) => new
                    {
                        c.Target,
                        DateTimeUTC = l.DateTimeUtc,
                        l.Status,
                        l.StringValue,
                        CollectorName = c.TypeName
                    }
                ).OrderBy(
                    o => o.Target
                ).ThenBy(
                    o => o.CollectorName
                )
                ;

This produces the results I want but takes ~35 seconds to execute.

This becomes the following SQL

SELECT 
[Distinct1].[CollectorId] AS [CollectorId], 
[Extent3].[Target] AS [Target], 
[Limit1].[DateTimeUtc] AS [DateTimeUtc], 
[Limit1].[Status] AS [Status], 
[Limit1].[StringValue] AS [StringValue], 
[Extent3].[TypeName] AS [TypeName]
FROM    (SELECT DISTINCT 
    [Extent1].[CollectorId] AS [CollectorId]
    FROM [dbo].[CollectorLogModels] AS [Extent1] ) AS [Distinct1]
OUTER APPLY  (SELECT TOP (1) [Project2].[Status] AS [Status], [Project2].[StringValue] AS [StringValue], [Project2].[DateTimeUtc] AS [DateTimeUtc], [Project2].[CollectorId] AS [CollectorId]
    FROM ( SELECT 
        [Extent2].[Status] AS [Status], 
        [Extent2].[StringValue] AS [StringValue], 
        [Extent2].[DateTimeUtc] AS [DateTimeUtc], 
        [Extent2].[CollectorId] AS [CollectorId]
        FROM [dbo].[CollectorLogModels] AS [Extent2]
        WHERE [Distinct1].[CollectorId] = [Extent2].[CollectorId]
    )  AS [Project2]
    ORDER BY [Project2].[DateTimeUtc] DESC ) AS [Limit1]
INNER JOIN [dbo].[CollectorSettingsModels] AS [Extent3] ON [Limit1].[CollectorId] = [Extent3].[Id]
ORDER BY [Extent3].[Target] ASC, [Extent3].[TypeName] ASC

How can I get performance closer to what is achievable with SQL alone?

2 个答案:

答案 0 :(得分:0)

在原始SQL中,您可以从与MAX(ID)不同的行中选择一个集合DateTimeUTC。这可能是个错误。 EF没有那个问题。它在语义上不相同,这是一个更难的查询。

如果您重写EF查询在结构上与SQL查询相同,那么您将获得相同的性能。我在这里看不到EF不支持。

同时使用EF计算max(id)并加入。

答案 1 :(得分:0)

我有完全相同的问题,我通过添加索引解决了这个问题。

我的查询需要45秒才能完成,我设法在不到一秒的时间内完成。