gpt4 book ai didi

c# - 使用 LINQ .Select() 转换为新类型是否太慢?

转载 作者:太空宇宙 更新时间:2023-11-03 17:44:44 24 4
gpt4 key购买 nike

当前项目,为这个问题而崩溃:

客户端存储库:

public class ClientRepository
{
// Members
private masterDataContext _db;

// Constructor
public ClientRepository()
{
_db = new masterDataContext();
}

public IEnumerable<ClientName> GetCorporateClientNames()
{
return _db.corporate_client_tbs.Select(o => new ClientName { id = o.id, name = o.company_name }).AsEnumerable();
}

public IEnumerable<ClientName> GetRetailClientNames()
{
return _db.retail_client_tbs.Select(o => new ClientName { id = o.id, name = o.name }).AsEnumerable();
}

// Define return type
public class ClientName
{
public int id { get; set; }
public string name { get; set; }
}
}

现在在 Controller 中我有以下内容:

public ActionResult Index()
{
var _visits = _db.GetAllServiceVisits();
return View(_visits);
}

加载当前存在的 200 奇数行的 View 大约需要 4 秒。

我想在包含客户名称的访问模型中添加属性“client”。客户端的名称将来自两个不同的表之一,这两个表是从两个类型为“ClientName”的数组之一中获取的。

这是使用 LINQ 的第一种方法:

public ActionResult Index()
{
private ClientRepository _cr = new ClientRepository();
var _retailclients = _cr.GetRetailClientNames().ToArray();
var _corporateclients = _cr.GetCorporateClientNames().ToArray();
var _visits = _db.GetAllServiceVisits();

var _temp = _visits.Select(o => new ServiceVisitViewModel
{
service_visit = o,
client = (o.client_type ? _corporateclients.Where(p => p.id == o.client_id).First().name : _retailclients.Where(p => p.id == o.client_id).First().name)
}).ToArray();

return View(_temp);
}

这是第二种方法,使用普通的 'ol C#:

public ActionResult Index()
{
private ClientRepository _cr = new ClientRepository();
var _retailclients = _cr.GetRetailClientNames().ToArray();
var _corporateclients = _cr.GetCorporateClientNames().ToArray();
var _visits = _db.GetAllServiceVisits();

List<ServiceVisitViewModel> _temp = new List<ServiceVisitViewModel>();
foreach (service_visit_tb v in _visits)
{
_temp.Add(new ServiceVisitViewModel { service_visit = v, client = (v.client_type ? _corporateclients.Where(p => p.id == v.client_id).First().name : _retailclients.Where(p => p.id == v.client_id).First().name) });
}

return View(_temp);
}

根据我的测试,第二种方法大约快 8 到 10 倍。

我能看到的唯一区别是 .Select 语句。

有人可以告诉我,如果我在第一种方法或替代方法中做错了什么,为什么第一种方法这么慢!@#$ing 慢?!

编辑:_db.GetAllServiceVisits()定义如下:

public IEnumerable<service_visit_tb> GetAllServiceVisits()
{
var _visits = _db.service_visit_tbs;
return _visits.AsEnumerable();
}

结束编辑

第二次编辑:我已从日志中的每个条目中删除了这一行:

-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1

上下文日志如下:

// This query is to fetch all the clients of Type One (corresponding to _cr.GetRetailClientNames() )
SELECT [t0].[id], [t0].[name]
FROM [genii].[retail_client_tb] AS [t0]

// This query is to fetch all the clients of Type Two (corresponding to _cr.GetCorporateClientNames() )
SELECT [t0].[id], [t0].[company_name] AS [name]
FROM [genii].[corporate_client_tb] AS [t0]

// This is the main query (loading roughly 250 records) which fetchs all Visits
SELECT [t0].[id], [t0].[client_type], [t0].[client_id], [t0].[machine_type], [t0].[machineID], [t0].[visit_type], [t0].[scheduledon], [t0].[arrivedon], [t0].[completedon], [t0].[reported_problem], [t0].[diagnosed_problem], [t0].[action_taken], [t0].[visit_status], [t0].[engineer_id], [t0].[reference_id], [t0].[addedby], [t0].[addedon], [t0].[modifiedby], [t0].[modifiedon]
FROM [genii].[service_visit_tb] AS [t0]

// These next queries are not being manually called by me, I assume they are being
// called when the Razor view is compiled since I am calling the name value of a linked table as such:
// @item.service_visit.engineer_tb.name
SELECT [t0].[id], [t0].[type]
FROM [genii].[visit_type_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [8]

SELECT [t0].[id], [t0].[status]
FROM [genii].[visit_status_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]

SELECT [t0].[id], [t0].[name]
FROM [genii].[engineer_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [3]

SELECT [t0].[id], [t0].[type]
FROM [genii].[visit_type_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [11]

SELECT [t0].[id], [t0].[name]
FROM [genii].[engineer_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [2]

SELECT [t0].[id], [t0].[type]
FROM [genii].[visit_type_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [7]

SELECT [t0].[id], [t0].[type]
FROM [genii].[visit_type_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [2]

SELECT [t0].[id], [t0].[type]
FROM [genii].[visit_type_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [6]

SELECT [t0].[id], [t0].[type]
FROM [genii].[visit_type_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [3]

SELECT [t0].[id], [t0].[name]
FROM [genii].[engineer_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [5]

SELECT [t0].[id], [t0].[name]
FROM [genii].[engineer_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [4]

SELECT [t0].[id], [t0].[status]
FROM [genii].[visit_status_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [8]

SELECT [t0].[id], [t0].[status]
FROM [genii].[visit_status_tb] AS [t0]
WHERE [t0].[id] = @p0
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [2]

问题的附录:是否有更好的方法来提取这些数据?我一直认为(从未检查过)LINQ 上下文提供给我的基于外键的数据访问是最好的,但是看到这些额外的查询,我不再那么确定了!

将在今天晚些时候发布第二部分执行的速度(在孟买度过了一个漫长的周末,但我们正在努力完成)

结束编辑

第三次编辑(我正在考虑从网络服务器到客户端的响应,因为应该考虑所有计算/获取/绑定(bind)等。)

方法一:6.85 秒(从 3 个表调用,然后使用 C# 转换到 View 模型)

public IEnumerable<service_visit_tb> GetAllServiceVisits()
{
var _visits = _db.service_visit_tbs;
_db.Log = new DebuggerWriter();
return _visits.AsEnumerable();
}

public ActionResult Index()
{
var _retailclients = _cr.GetRetailClientNames().ToArray();
var _corporateclients = _cr.GetCorporateClientNames().ToArray();
var _visits = _db.GetAllServiceVisits();
List<ServiceVisitViewModel> _temp = new List<ServiceVisitViewModel>();
foreach (service_visit_tb v in _visits)
{
_temp.Add(new ServiceVisitViewModel { service_visit = v, client = (v.client_type ? _corporateclients.Where(p => p.id == v.client_id).First().name : _retailclients.Where(p => p.id == v.client_id).First().name) });
//}
return View(_temp);
}

方法二:8.59 秒(从 3 个表调用,然后使用 LINQ 转换到 View 模型)

public IEnumerable<service_visit_tb> GetAllServiceVisits()
{
var _visits = _db.service_visit_tbs;
_db.Log = new DebuggerWriter();
return _visits.AsEnumerable();
}

public ActionResult Index()
{
var _retailclients = _cr.GetRetailClientNames().ToArray();
var _corporateclients = _cr.GetCorporateClientNames().ToArray();
var _visits = _db.GetAllServiceVisits();
var _temp = _visits.Select(o => new ServiceVisitViewModel
{
service_visit = o,
client = (o.client_type ? _corporateclients.Where(p => p.id == o.client_id).First().name : _retailclients.Where(p => p.id == o.client_id).First().name)
});
return View(_temp);
}

方法三:5.76 秒(单个 LINQ 查询中的所有内容 - 在数据库上执行)

public IEnumerable<ServiceVisitViewModel> GetAllServiceVisitsNew()
{
var _visits = _db.service_visit_tbs.Select(o => new ServiceVisitViewModel
{
service_visit = o,
client = (o.client_type ? _db.corporate_client_tbs.Where(c=> c.id == o.client_id).First().company_name : _db.retail_client_tbs.Where(c=> c.id == o.client_id).First().name)
});
_db.Log = new DebuggerWriter();
return _visits;
}

public ActionResult Index()
{
var _visits = _db.GetAllServiceVisitsNew();
return View(_visits());
}

猜猜这决定了它。感谢大家的帮助。我将 Jon 标记为正确答案,因为他在数据库端做所有事情的方法是让培根回家的原因。非常感谢不厌其烦回复的所有人。

结束编辑

最佳答案

Select语句在这里有很大的不同——因为它改变了数据库中正在做的事情。 (我假设 _db.GetAllServiceVisits() 返回一个 IQueryable<T> 。)

因为您正在使用 ToArray,所以您已经将所有零售和企业客户存入内存 .你的Select调用将(我怀疑)然后将所有数据返回发送到数据库,以便 SelectWhere可以在那里进行。我怀疑如果您查看 SQL 分析器,它会是一个非常奇怪的查询。

如果您强制一切在客户端完成,它应该与您的后一种方法基本相同。您可以轻松做到这一点,方法是:

var _visits = _db.GetAllServiceVisits().ToList();

... 然而,这意味着您每次点击此页面时都会从数据库中提取所有 三个表。对我来说,这听起来不是个好计划。

但是,如果您可以先将所有零售和公司客户提取到内存中,就可以在数据库中执行所有操作会更好。

可能就像更改存储库方法以返回 IQueryable<T> 一样简单而不是 IEnumerable<T> ,并删除对 AsEnumerable 的调用.

关于c# - 使用 LINQ .Select() 转换为新类型是否太慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7051287/

24 4 0
文章推荐: html - 在 div 中垂直居中链接图像
文章推荐: c# - C#.NET 中的粗线绘制问题
文章推荐: html - 如何将