[英]Linq to Entities : Count is very slow
I need to have parent and parent.child.count()....in the query.. when i do this it is taking 20 seconds....its not a huge database...Any ideas for optimization... 我需要在查询中使用parent和parent.child.count()....当我这样做时需要20秒....它不是一个庞大的数据库...任何优化的想法......
var plist = context.persons
.Select(p => new
{
p.fullName,
c.personID,
p.Status,
p.Birthdate,
p.Accounts.Count
}).ToList();
Here is a great article on using count() when you really meant to use any() 这篇文章是关于在你真正想要使用any()时使用count()的好文章
http://blogs.teamb.com/craigstuntz/2010/04/21/38598/ http://blogs.teamb.com/craigstuntz/2010/04/21/38598/
Do you need to use .count or could you use .any? 你需要使用.count还是可以使用.any?
http://msdn.microsoft.com/en-us/library/bb534972.aspx http://msdn.microsoft.com/en-us/library/bb534972.aspx
Since this is entity framework, open up the sql profiler and take a look at what sql queries are being sent to the database. 由于这是实体框架,请打开sql profiler并查看正在向数据库发送的SQL查询。 It sounds like you may see that a single query is sent to fetch the group identifiers, and then another set of queries (one for each group) might be fetching the count.
听起来您可能会看到发送单个查询以获取组标识符,然后另一组查询(每个组一个)可能正在获取计数。 If that's happening, you'll have to post the linq query for someone to resolve the issue.
如果发生这种情况,您必须发布linq查询给某人以解决问题。
Based on the code you sent, it doesn't look like things should be taking that long. 根据您发送的代码,看起来事情应该花费那么长时间。 I have a few suggestions:
我有一些建议:
Count()
is. Count()
花费的时间多得多。
.Take(int)
method before calling .ToList()
to get only as many entries as you need. .ToList()
之前使用.ToList()
.Take(int)
方法,以获得所需数量的条目。 If you can describe how this data is being used, or provide a screenshot of the SQL's execution, we can provide more feedback. 如果您可以描述如何使用此数据,或提供SQL执行的屏幕截图,我们可以提供更多反馈。
I had a somehow similar problem, I tried these and worked out better : 我有一个类似的问题,我尝试了这些并且更好地解决了:
child.count(x=> x.paretnID == inputParentID) child.where(x=> x.parentID == inputParentID) child.count(x => x.paretnID == inputParentID)child.where(x => x.parentID == inputParentID)
my original code which took around 15-20 seconds on each iteration was: return (isEdit) ? 我的原始代码在每次迭代时花了大约15-20秒: return(isEdit)? db.ChasisBuys.Single(x => x.ChasisBuyID == long.Parse(Request.QueryString["chbid"])).Chasises.Count(y => y.Bikes.Count > 0 && y.ColorID == buyItems[(int)index].ColorID && y.ChasisTypeID == buyItems[(int)index].ChasisTypeID).ToString() : "-";
db.ChasisBuys.Single(x => x.ChasisBuyID == long.Parse(Request.QueryString [“chbid”]))。Chasises.Count(y => y.Bikes.Count> 0 && y.ColorID == buyItems [(int)index] .ColorID && y.ChasisTypeID == buyItems [(int)index] .ChasisTypeID).ToString():“ - ”;
new code which runs good is : 运行良好的新代码是:
**return (isEdit) ? db.Chasises.Where(x => x.ChasisBuyID == long.Parse(Request.QueryString["chbid"])).Count(y => y.Bikes.Count > 0 && y.ColorID == buyItems[(int)index].ColorID && y.ChasisTypeID == buyItems[(int)index].ChasisTypeID).ToString() : "-";**
Database has around 1000 records in chasises , about 5 in chasisBuys and about 20 in Bikes. 数据库在chasises中有大约1000条记录,在chasisBuys中大约有5条记录,在自行车中大约有20条。 my opinion is that Linq to SQL queries does not do preevaluations such in logical statements which for instance if you write "return a && b && c;"
我的观点是Linq to SQL查询不会在逻辑语句中进行预先评估,例如,如果你写“return a && b && c;” if statement a is false other statements are not evaluated and I was expecting such thing in linq to sql but it's not the case.
如果语句a是false,则其他语句不会被评估,我在linq中期待这样的事情,但事实并非如此。
I solved a similar problem using the GroupBy method. 我使用GroupBy方法解决了类似的问题。
IEnumerable> accounts = Accounts.GroupBy(x => x.personID); IEnumerable> accounts = Accounts.GroupBy(x => x.personID); accounts.Count() will return the number of accounts that belong to the person.
accounts.Count()将返回属于该人的帐户数。 accounts.Key will return the personID of the group.
accounts.Key将返回组的personID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.