实体 LINQ 查询变慢
Entity LINQ query to slow
我是 C# 新手,我有一个别人设计的数据库,查询效果很好,但与 SQL 相比,它慢了 10 倍。
我肯定在这里犯了错误,任何人都有提示可以加快速度。这个模型是为了展示在table,我把int
转换成ENUM
,计算折扣展示。
代码是:
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();
似乎我所有的实体查询都至少比 SQL 慢 5 倍以上。我在某个地方犯了一些错误。
你的问题是AsEnumerable
的使用。当查询被执行时(在您的例子中是 results.ToList()
),它后面出现的所有内容都将使用 linq-to-objects 进行评估。这意味着您的连接不会由数据库处理。您将从表中获取所有记录。
但是,您的函数 WashTypeShowEnum.WashTypeShowEnumToString
不会被 entity framework 识别。
您可能希望将 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();
我是 C# 新手,我有一个别人设计的数据库,查询效果很好,但与 SQL 相比,它慢了 10 倍。
我肯定在这里犯了错误,任何人都有提示可以加快速度。这个模型是为了展示在table,我把int
转换成ENUM
,计算折扣展示。
代码是:
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();
似乎我所有的实体查询都至少比 SQL 慢 5 倍以上。我在某个地方犯了一些错误。
你的问题是AsEnumerable
的使用。当查询被执行时(在您的例子中是 results.ToList()
),它后面出现的所有内容都将使用 linq-to-objects 进行评估。这意味着您的连接不会由数据库处理。您将从表中获取所有记录。
但是,您的函数 WashTypeShowEnum.WashTypeShowEnumToString
不会被 entity framework 识别。
您可能希望将 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();