2014-06-18 34 views
0

我目前正在研究一個項目,我必須從數據庫中提取數據並將其顯示在我的視圖中的表格上。我有這一切的工作,但它需要大約10-15秒來加載頁面。如果可能的話,我想減少這個時間。我如何加快我的asp.net mvc應用程序?

我認爲問題在於從數據庫中獲取信息。我從數據庫中提取了很多項目,我相信可能有更好的方法。

控制器:

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> 

Webconfig:多提前

<?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=&quot;data source=T520-R9K0H1K\SQLEXPRESS;initial catalog=RestoreDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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沒有經驗)

我添加了一個截圖,如果有人可以確認/提出一個很好的解決方案。

DotTrace Snapshot upon running the project

+0

您在此問題中沒有足夠的信息來給出有效答案。你需要確定的第一件事是你的慢點。它是你的數據訪問,是查詢速度慢,還是你返回1M記錄,隨後的渲染速度慢?實際上客戶端的緩慢點是一些非常慢的JavaScript ...? – Paddy

+0

對不起,缺乏信息。我不確定問題出在哪裏。 我確實知道我從數據庫中提取了很多信息,我相信這是性能可以提高的地方。 我不相信它在客戶端。 –

+1

嘗試運行諸如DotTrace或ANTS Profiler之類的探查器以查明某些緩慢部件的位置。如果是數據庫,我將運行SQL Server Profiler來查找哪些查詢正在運行以及它們需要多長時間。一旦您知道性能問題的確切原因,您就可以開始嘗試解決這些問題。 –

回答

0

有一兩件事你可以做的,是你的緩存輸出。這將節省後續請求的時間。無論您想要緩存哪種操作方法,都可以使用輸出緩存屬性。

[OutputCache(Duration = 3600, Location = System.Web.UI.OutputCacheLocation.ServerAndClient)] 

持續時間是多久你想緩存以秒爲單位輸出,位置OBV要進行緩存。

您還可以使用SqlCacheDependency緩存請求,直到數據更新完畢。使用sql依賴關係的輸出緩存屬性可能更適合您的問題。 這是關於如何在msdn上做的演練 - http://msdn.microsoft.com/en-us/library/e3w8402y(v=vs.140).aspx

你還可以做其他事情來加快你的mvc應用程序。我寫了一篇關於你可以做的一些事情的簡短文章。請在這裏查看 - http://www.paulsodimu.co.uk/Post/How-to-speed-up-your-MVC-web-application

相關問題