c# - 使用实体框架获取视图数据非常慢
问题描述
我正在开发一个 ASP.NET MVC 4 应用程序,在该应用程序中我试图创建一个从 2 个数据库视图读取的仪表板;一种视图列出了具有计数和目标的建筑物,另一种视图列出了具有计数和目标的建筑物的楼层。我正在尝试将它们组合在仪表板上,因此我创建了一个名为的数据模型,该模型DashboardView
将建筑物与物业的楼层保持在一起Children
。
它在本地运行良好,但部署在生产 IIS 服务器上运行速度非常慢,我不确定这是否是由于我访问数据的方式效率低下:
using (var db = new MyDBContext())
{
var views = (from building in db.BuildingViews.OrderBy(x => x.BuildingName) select new { building })
.AsEnumerable()
.Select(bldgView => new DashboardView
{
ViewType = "Building",
ViewLabel = bldgView.building.BuildingName,
CurrentCount = bldgView.building.Count,
GoalCount = bldgView.building.Goal,
Children = (from floors in db.FloorViews.Where(v => v.BuildingId == bldgView.Building.BuildingId) select new { floor })
.AsEnumerable()
.Select(floorView => new DashboardView
{
ViewType = "Floor",
ViewLabel = floorView.floor.FloorName
CurrentCount = floorView.floor.Count,
GoalCount = floorView.floor.Goal
}).ToList()
}).ToList();
double totalBldgCount = db.BuildingViews.Select(x => x.Count).Sum();
double totalGoalCount = db.BuildingViews.Select(x => x.Goal).Sum();
}
有没有更好的方法来创建我想要在这里实现的数据包,或者这个问题可能与多人同时访问数据库有关?
::编辑::
我现在明白 using.AsEnumerable()
可能是性能不佳的罪魁祸首;但是我很难理解如何在没有它的情况下正确构建我DashboardView
的子DashboardView
对象,因为这不起作用:
var query = (from buildings in db.BuildingViews
join floors in db.FloorViews on buildings.BuildingId equals floors.BuildingId
select new DashboardSprinklerView
{
ViewType = "Building",
ViewLabel = building.BuildingName,
CurrentCount = building.Count,
GoalCount = building.Goal,
Children = (floor from floors select new DashboardSprinklerView
{
....
}).ToList()
}).ToList();
解决方案
推荐阅读
- python - discord.py 加入消息嵌入未发送
- c++ - 纹理不填充图形 - OpenGL
- php - 查找并删除数组中的下一个数组
- python - 将 pathos 与 XML 一起使用时出现酸洗错误
- ruby-on-rails - Docker compose 的 Rails 控制台问题
- css - 如何在另一个元素中设置元素相对于选定文本的 xy 位置?
- php - 指向特定页面的搜索结果页面 [向上]
- python - Discordpy 成员 history() 函数不返回任何内容
- mysql - 如何将 MySQL DATETIME 更新为 NOW()?(错误 1064)
- java - Apache Beam 似乎没有运行测试