[英]Entity LINQ query to slow
I'm new to the C#, I have a database that someone else designed, query works great, but compared with SQL, it's 10 times slower. 我是C#的新手,我有一个别人设计的数据库,查询效果很好,但是与SQL相比,它慢了10倍。
I made mistakes here for sure, anybody have tips to speed this up a little bit. 我肯定在这里犯了错误,任何人都有一些技巧可以加快速度。 This model is for displaying in table, and I converting
int
to ENUM
and calculating discount for display. 该模型用于在表中显示,我将
int
转换为ENUM
并计算显示折扣。
Code is: 代码是:
var results = from w in db.Washes.AsEnumerable()
join t in db.Wash_Types.AsEnumerable() on w.WashTypeId equals t.Id
join a in db.Accounts.AsEnumerable() on w.AccountId equals a.Id
orderby w.Id descending
select new AllWashesTable
{
Id = w.Id,
WashTime = w.WashTime,
WashTimeEnd = w.WashTimeEnd,
Name = a.Name,
Client = (w.Client != null ? w.Client.Naziv : ""),
MobileNumber = a.MobileNumber,
Identification = w.Identification,
WashType = WashTypeShowEnum.WashTypeShowEnumToString((WashTypeShowEnum.WashType) w.WashTypeId),
Price = int.Parse(t.WashPrice) * (1 - w.Discount) + "",
Discount = w.Discount
};
return results.ToList();
Seems all my entity queries are at least 5+ times slower than SQL. 似乎我所有的实体查询都比SQL慢至少5倍以上。 Somewhere I am making some mistake.
我在某个地方犯了一些错误。
Your problem is the use of AsEnumerable
. 您的问题是使用
AsEnumerable
。 When the query gets executed (in your case the results.ToList()
), Everything that appears after it, will be evaluated using linq-to-objects. 执行查询后(在您的情况下为
results.ToList()
),查询之后出现的所有内容都将使用linq-to-objects进行评估。 It means that your joins will not be handled by DB. 这意味着您的联接将不会被数据库处理。 You will fetch all the records from the tables.
您将从表中获取所有记录。
However, your function WashTypeShowEnum.WashTypeShowEnumToString
will not be recognized by entity framework. 但是,实体框架将无法识别您的函数
WashTypeShowEnum.WashTypeShowEnumToString
。
You might want to move the AsEnumerable
to the end and then select
the results. 您可能需要将
AsEnumerable
移至末尾,然后select
结果。
var results = (from w in db.Washes
join t in db.Wash_Types on w.WashTypeId equals t.Id
join a in db.Accounts on w.AccountId equals a.Id
orderby w.Id descending
select new {w, a, t}).AsEnumerable().Select(arg=> new AllWashesTable
{
Id = arg.w.Id,
WashTime = arg.w.WashTime,
WashTimeEnd = arg.w.WashTimeEnd,
Name = arg.a.Name,
Client = (arg.w.Client != null ? arg.w.Client.Naziv : ""),
MobileNumber = arg.a.MobileNumber,
Identification = arg.w.Identification,
WashType = WashTypeShowEnum.WashTypeShowEnumToString((WashTypeShowEnum.WashType) arg.w.WashTypeId),
Price = int.Parse(arg.t.WashPrice) * (1 - arg.w.Discount) + "",
Discount = arg.w.Discount
};
return results.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.