简体   繁体   English

使用count()优化linq查询

[英]Optimize linq query with count()

I have the following query that works fine 我有以下查询工作正常

var myList = (from p in db.full


                      group p by p.object into g
                      orderby g.Count() descending
                      select new StringIntType
                      {
                          str = g.Key,

                          nbr = g.Count()
                      }).Take(50).ToList();

The problem is that it's a bit slow due to the fact that i'm using count(), which is translated to count(*). 问题是它有点慢,因为我使用的是count(),它被转换为count(*)。
I need to know if is there a way to use count(object), 我需要知道是否有办法使用count(对象),

Here is what i got in sql server profiler 这是我在sql server profiler中得到的

  exec sp_executesql N'SELECT TOP (50) 
  [Project1].[C2] AS [C1], 
  [Project1].[object] AS [object], 
  [Project1].[C1] AS [C2]
  FROM ( SELECT 
      [GroupBy1].[A1] AS [C1], 
    [GroupBy1].[K1] AS [object], 
    1 AS [C2]
    FROM ( SELECT 
        [Extent1].[object], AS [K1], 
        COUNT(1) AS [A1]
        FROM (SELECT 
[full].[mc_host_class] AS [mc_host_class], 
[full].[event_handle] AS [event_handle], 
[full].[mc_host_address] AS [mc_host_address], 
[full].[mc_object_class] AS [mc_object_class], 
[full].[mc_object] AS [mc_object], 
[full].[mc_incident_time] AS [mc_incident_time], 
[full].[date_reception] AS [date_reception], 
[full].[status] AS [status], 
[full].[mc_owner] AS [mc_owner], 
[full].[msg] AS [msg], 
[full].[duration] AS [duration], 
[full].[repeat_count] AS [repeat_count], 
[full].[mc_date_modification] AS [mc_date_modification], 
[full].[event_class] AS [event_class], 
[full].[bycn_ticket_remedy] AS [bycn_ticket_remedy], 
[full].[mc_host] AS [mc_host], 
[full].[acknowledge_by] AS [acknowledge_by], 
[full].[acknowledge_by_time] AS [acknowledge_by_time], 
[full].[assigned_by] AS [assigned_by], 
[full].[assigned_to] AS [assigned_to], 
[full].[assigned_by_time] AS [assigned_by_time], 
[full].[closed_b            y] AS [closed_by], 
[full].[closed_by_time] AS [closed_by_time], 
[full].[blacked_out] AS [blacked_out], 
[full].[bycn_liaison_type] AS [bycn_liaison_type], 
[full].[bycn_liaison_debit] AS [bycn_liaison_debit], 
[full].[cause] AS [cause], 
[full].[mc_location] AS [mc_location], 
[full].[mc_parameter] AS [mc_parameter]
FROM [dbo].[full] AS [full]) AS [Extent1]
        GROUP BY [Extent1].[object], 
        )  AS [GroupBy1]
     )  AS [Project1]
ORDER BY [Project1].[C1] DESC',N'@p__linq__0 datetime2(7),@p__linq__1 datetime2(7)',@p__linq__0='2015-03-14 00:00:00',@p__linq__1='2015-04-15 00:00:00'

Perhaps couple optimisations can do the trick: 也许情侣优化可以解决问题:

  • Do the take first, before selecting 在选择之前先做好
  • Count groups only once using a let keyword 使用let关键字只计算一次组

So metacode (this code written in notepad and won't compile!) 所以metacode(这个代码写在记事本中,不会编译!)

var topFifty = (
    from p in db.full   
    group p by p.object into g
    let groupedCount = g.Count()
    orderby groupedCount descending 
    select p.key, groupedCount
    )
    .Take(50).ToList();

var topFifty.Select(x => new StringIntType
    {
        str = x.Key,
        nbr = x.Count
    }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM