按语句加速linq组

时间:2015-01-29 19:51:55

标签: c# linq

我有一张这样的表

UserID   Year   EffectiveDate   Type    SpecialExpiryDate
     1   2015   7/1/2014        A   
     1   2016   7/1/2015        B       10/1/2015

表格中没有ExpriyDate因为它只有一年有效,所以可以通过添加一年来从生效日期计算到期日。

我想得到的结果是这样的(当年的生效日期和下一年的到期日期)

UserID   EffectiveDate   ExpiryDate
     1    7/1/2014        7/1/2016

如果用户的类型是B,那么会有一个特殊的到期日,所以对于这个人来说,结果将是

UserID   EffectiveDate   ExpiryDate
     1    7/1/2014        10/1/2015

这是我写的代码

var result = db.Table1
            .Where(x => x.Year>= 2015 && (x.Type == "A" || x.Type == "B"))
            .GroupBy(y => y.UserID)
            .OrderByDescending(x => x.FirstOrDefault().Year)
            .Select(t => new
                         {
                             ID = t.Key,
                             Type = t.FirstOrDefault().Type,
                             EffectiveDate = t.FirstOrDefault().EffectiveDate,
                             ExpiryDate = t.FirstOrDefault().SpecialExpiryDate != null ? t.FirstOrDefault().SpecialExpiryDate : (t.Count() >= 2 ? NextExpiryDate : CurrentExpiryDate)
                          }
                    );

代码可以得到我需要的结果,但问题是在结果集中有大约10000条记录需要大约5到6秒。该项目是针对网络搜索API的,所以我想加快速度,有更好的方法来进行查询吗?

修改

抱歉,我犯了一个错误,在select子句中它应该是

EffectiveDate = t.LastOrDefault().EffectiveDate

但在C#的Linq中,它并不支持将此LastOrDefault函数转移到sql,它会导致新问题,获取该组第二项的最简单方法是什么?

2 个答案:

答案 0 :(得分:2)

试试这个:

var result =
    db
        .Table1
        .Where(x => x.Year>= 2015 && (x.Type == "A" || x.Type == "B"))
        .GroupBy(y => y.UserID)
        .SelectMany(y => y.Take(1), (y, z) => new
        {
            ID = y.Key,
            z.Type,
            z.EffectiveDate,
            ExpiryDate = z.SpecialExpiryDate != null
                ? z.SpecialExpiryDate 
                : (t.Count() >= 2 ? NextExpiryDate : CurrentExpiryDate),
            z.Year,
        })
        .OrderByDescending(x => x.Year);

.SelectMany(y => y.Take(1)有效地执行了代码的.FirstOrDefault()部分。通过这样做一次而不是许多属性,你可以极大地提高速度。

在我使用类似结构化查询执行的测试中,我在使用您的方法时运行了这些子查询:

SELECT t0.increment_id
FROM sales_flat_order AS t0
GROUP BY t0.increment_id

SELECT t0.hidden_tax_amount
FROM sales_flat_order AS t0
WHERE ((t0.increment_id IS NULL AND @n0 IS NULL) OR (t0.increment_id = @n0))
LIMIT 0, 1
-- n0 = [100000001]

SELECT t0.customer_email
FROM sales_flat_order AS t0
WHERE ((t0.increment_id IS NULL AND @n0 IS NULL) OR (t0.increment_id = @n0))
LIMIT 0, 1
-- n0 = [100000001]

SELECT t0.hidden_tax_amount
FROM sales_flat_order AS t0
WHERE ((t0.increment_id IS NULL AND @n0 IS NULL) OR (t0.increment_id = @n0))
LIMIT 0, 1
-- n0 = [100000002]

SELECT t0.customer_email
FROM sales_flat_order AS t0
WHERE ((t0.increment_id IS NULL AND @n0 IS NULL) OR (t0.increment_id = @n0))
LIMIT 0, 1
-- n0 = [100000002]

(每个记录号继续进行两次子查询。)

如果我运行我的方法,我得到了这个单一的查询:

SELECT t0.increment_id, t1.hidden_tax_amount, t1.customer_email
FROM (
  SELECT t2.increment_id
  FROM sales_flat_order AS t2
  GROUP BY t2.increment_id
  ) AS t0
CROSS APPLY (
  SELECT t3.customer_email, t3.hidden_tax_amount
  FROM sales_flat_order AS t3
  WHERE ((t3.increment_id IS NULL AND t0.increment_id IS NULL) OR (t3.increment_id = t0.increment_id))
  LIMIT 0, 1
  ) AS t1

我的方法应该快得多。

答案 1 :(得分:1)

您可以使用数据库中的View动态生成计算数据。

像这样(伪代码):

Create View vwUsers AS 
    Select 
        UserID, 
        Year, 
        EffectiveDate, 
        EffectiveData + 1 as ExpiryDate,   // <-- 
        Type, 
        SpecialExpiryDate
    From 
        tblUsers

只需将您的LINQ查询与此相关联。