2

This is my first time using MVC. I'm trying to make a search page for an MVC5 page where it'll crate a table with some basic information like name, gender, etc. This is a partial view I want to call using AJAX when someone hits the search button. So on my controler I am using linq to query a remote sql database where I can retrieve at most around 2k records if someone puts no filters. The linq to sql query is fairly fast (I tried returning just JSON instead of a view and it was practically instant), however there is a problem when I'm trying to render my view.

Right now I'm doing something like:

    @foreach(var item in Model){
        <tr>
               <td>@item.name<td>
               <td>@item.gender<td>
               ... etc.
        <tr>
    }

However, this its ridiculously slow. On chrome it could take anywhere from 30 seconds to over a minute to load. When testing IE10 it never loads, which is a problem because IE10 compatibility is required. I'm using similar syntax to dynamically load in a custom drop down lists with filters on it, which makes the home page take around two seconds to load. I believe it's an issue with the @foreach line because limiting it to 10 records makes it load in 4 seconds, which is still extremely slow, but better. I'm running this off of localhost.I have an old version of this website running off of asp webforms using the same sql query but bound to a gridview instead and it is extremely fast, it loads all records into the same table in under a second.

I really couldn't find anything online to help with speed. Is there something about razor syntax like this that I should be avoiding? Or should I stick with the webforms site since it seems to be faster?

user692942
  • 16,398
  • 7
  • 76
  • 175
ArielLuque
  • 25
  • 1
  • 4

1 Answers1

1

Your For Loop could be triggering other queries if you are implementing the IQueryable interface (lazy loading). Entity Framework / Linq To Sql can sometimes grab a lot more data than is readily obvious.

If you have not ruled this out already, I heartily recommend getting the Glimpse nuGet packages that would be relevant to your project. This will show you exactly what queries are being generated. You could also look at the execution plan in SQL management studio, but the Glimpse add-in is very nice.

The Glimpse packages stack on top of each other, but this is what I am currently using:

Glimpse Core Glimpse ADO Glimpse ASP.NET Glimpse EF6 Glimpse MVC

AnotherDeveloper
  • 1,242
  • 1
  • 15
  • 36
  • This was it. It was making a lot of extra calls to the DB. I switched over to the entity framework and it goes by a lot faster. Pulling in the entire data set takes just over 10 seconds now, which is still not as fast as I would like it. Going to keep searching for optimizations. – ArielLuque Aug 13 '15 at 14:14
  • I'm somewhat new to Entity Framework too. Here are some resources that have helped me: http://stackoverflow.com/questions/15637317/what-is-the-difference-between-sqlfunctions-and-entityfunctions, http://www.microsoftvirtualacademy.com/training-courses/implementing-entity-framework-with-mvc, https://lostechies.com/jimmybogard/2014/03/11/efficient-querying-with-linq-automapper-and-future-queries/ – AnotherDeveloper Aug 14 '15 at 18:31