【发布时间】:2014-06-18 14:50:42
【问题描述】:
我目前正在从事一个项目,我必须从数据库中提取数据并将它们显示到我视图中的表中。我已经完成了所有这些工作,但加载页面大约需要 10-15 秒。如果可能的话,我想减少这个时间。
我认为问题在于从数据库中获取信息。我从数据库中提取了许多项目,我相信可能有更好的方法。
控制器:
public class HomeController : Controller
{
private RestoreDBEntities db = new RestoreDBEntities();
public ActionResult Index()
{
W6ViewModel viewModel = new W6ViewModel();
viewModel.engineers = db.W6ENGINEERS.OrderBy(w => w.Name).Include(w => w.W6CALENDARS).ToList();
viewModel.tasks = db.W6TASKS.ToList();
return View(viewModel);
}
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
W6ENGINEERS w6ENGINEERS = db.W6ENGINEERS.Find(id);
if (w6ENGINEERS == null)
{
return HttpNotFound();
}
return View(w6ENGINEERS);
}
[HttpPost]
public ActionResult Filter(FormCollection collection)
{
string city = collection["city"];
W6ViewModel viewModel = new W6ViewModel();
viewModel.engineers = db.W6ENGINEERS.Where(w => w.City == city).Include(w => w.W6CALENDARS).ToList();
viewModel.tasks = db.W6TASKS.Where(w => w.City == city).Include(w => w.W6CALENDARS).ToList();
return View("Index", viewModel);
}
查看:
@model WebApplication1.ViewModel.W6ViewModel
@{
ViewBag.Title = "Dispatcher";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<section id="fields">
<h3 id="field_filter"><strong>Filters</strong></h3>
<h3 id="field_engineer"><strong>Engineers</strong></h3>
<h3 id="field_task"><strong>Tasks</strong></h3>
</section>
<section id="filters">
@using (Html.BeginForm("Filter", "Home", FormMethod.Post))
{
<input name="city" id="city" type="text" maxlength="15" title="City" value ="City" style="color:#888;"
onfocus ="inputFocus(this)" onblur="inputBlur(this)" />
<input id="submit" type="submit" value="Submit" />`enter code here`
}
</section>
<section id="engineers">
<table class="table-condensed table-striped">
<tr>
<th>
Name
</th>
<th>
Phone number
</th>
<th>
City
</th>
<th>
Region
</th>
<th>
Availability Factor
</th>
</tr>
@foreach (var item in Model.engineers)
{
<tr>
<td>
@Html.ActionLink(item.Name, "Details", new { id = item.W6Key })
</td>
<td>
@Html.DisplayFor(modelItem => item.MobilePhone)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Region)
</td>
<td>
@Html.DisplayFor(modelItem => item.AvailabilityFactor)
</td>
</tr>
}
</table>
</section>
<section id="work">
<table class="table-condensed table-striped">
<tr>
<th>
Job ID
</th>
<th>
Skills
</th>
<th>
Address
</th>
</tr>
@foreach(var item in Model.tasks)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsScheduled)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsPartsNotUsed)
</td>
</tr>
}
</table>
网络配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20140611092404.mdf;Initial Catalog=aspnet-WebApplication1-20140611092404;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="RestoreDBEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=T520-R9K0H1K\SQLEXPRESS;initial catalog=RestoreDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.webServer>
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
</system.webServer>
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
非常感谢!
编辑:
所以在安装 DotTrace 并使用它来诊断我的项目之后,似乎是我对数据库的调用导致加载时间过长(我认为,虽然我没有使用 DotTrace 的经验)
如果有人可以确认/提出一个很棒的解决方案,我已经添加了一个屏幕截图。
【问题讨论】:
-
您在这个问题中没有足够的信息来给您一个有效的答案。您需要确定的第一件事是您的慢点在哪里。是您的数据访问,查询速度慢,还是您返回 1M 记录并且随后的渲染很慢......?慢点实际上是在客户端使用一些非常慢的 javascript...?
-
抱歉信息不足。我不确定问题出在哪里。我确实知道我从数据库中提取了大量信息,我相信这是可以提高性能的地方。我不相信它在客户端。
-
尝试运行诸如 DotTrace 或 ANTS Profiler 之类的分析器以确定慢速部分在哪里。如果是数据库,我会运行 SQL Server Profiler 来找出正在运行的查询以及它们需要多长时间。一旦您知道性能问题的确切原因,您就可以开始尝试解决它们。
-
你可能还想看看这个 (code.google.com/p/mvc-mini-profiler) 是制作这个网站的人建立的。
标签: asp.net-mvc performance pageload