我正在使用EntityFramework 6并遇到一些主要的速度问题-该查询需要两秒钟的时间才能运行。我花了大部分时间使用LinqPad来加快查询速度,但我只能将其从4秒钟缩短到两秒钟。我尝试了分组,联接等,但是生成的SQL对我来说似乎过于复杂。我猜想我只是在使用错误的方法来编写LINQ。
这是我正在尝试做的
A
地方Valid
是空,AccountId
是不是当前用户Collection
of的当前用户B
不在B
哪里AccountId
A
的编号B
降序排列A
不包含的内容B
应位于返回结果的末尾。我必须建立如下模型:
public class A
{
public int Id { get; set; }
public bool? Valid { get; set; }
public string AccountId { get; set; }
public virtual ICollection<B> Collection { get; set; }
}
public class B
{
public int Id { get; set; }
public bool Valid { get; set; }
public string AccountId { get; set; }
public DateTime CreatedDate { get; set; }
public virtual A Property { get; set; }
}
该表A
有大约一百万行,B
最终将有大约一千万行。现在B
坐着50,000。
这是查询当前的样子。它给了我预期的结果,但是我必须运行orderby
多次并执行其他不必要的步骤:
var filterA = this.context.A.Where(gt => gt.Valid == null && !gt.AccountId.Contains(account.Id));
var joinedQuery = from b in this.context.B.Where(gv => !gv.AccountId.Contains(account.Id))
join a in filterA on gv.A equals a
where !a.Collection.Any(v => v.AccountId.Contains(account.Id))
let count = gt.Collection.Count()
orderby count descending
select new { A = gt, Count = count };
IQueryable<GifTag> output = joinedQuery
.Where(t => t.A != null)
.Select(t => t.A)
.Distinct()
.Take(20)
.OrderBy(t => t.Collection.Count);
谢谢
好吧,您总是可以尝试从joinQuery中删除这两行
其中!a.Collection.Any(v => v.AccountId.Contains(account.Id))
和
orderby计数降序
第一行查询和订单行中的第一行已被过滤,在最后一个查询中做得很好,因此没有必要重复两次
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句