HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Bottleneck encountered returning records for my grid

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bottleneckgridrecordsencounteredreturningfor

Problem

I am using MVC 4.0/C#/EF4.0 and I have a grid page that grabs results that are filtered, sorted and paged. It's filtering results from a table that has 45,000 records. How do I improve performance? This page takes over 30 seconds to load.

```
var filteredresults = _unitOfWork.ModifiedDataRepository.Get();

NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
int ID = (String.IsNullOrEmpty(nvc["sSearch_0"]) ? 0 : Convert.ToInt32(nvc["sSearch_0"]));
int TaxYear = (String.IsNullOrEmpty(nvc["sSearch_1"]) ? 0 : Convert.ToInt32(nvc["sSearch_1"]));
int AdditionalTax = (String.IsNullOrEmpty(nvc["sSearch_2"]) ? 0 : Convert.ToInt32(nvc["sSearch_2"]));

filteredresults = filteredresults.Where(i => (ID == 0 || i.ID == ID)
&& (TaxYear == 0 || i.TaxYear == TaxYear)
&& (AdditionalTax == 0 || i.AdditionalChildTaxCreditAmount == AdditionalTax)
);

int Count = filteredresults.Count();

string sortCol = param.sColumns.Split(',')[param.iSortCol_0];
string sortExpression = sortCol + " " + param.sSortDir_0;

filteredresults = filteredresults.OrderBy(sortExpression);
filteredresults = from p in filteredresults
orderby (sortExpression)
select p;

var parents = filteredresults.Skip(param.iDisplayStart).Take(param.iDisplayLength);

var result = from c in parents.ToList()
select new MODIFIED_DATA()
{
ID = c.ID,
TaxYear = c.TaxYear,
AdditionalChildTaxCreditAmount = c.AdditionalChildTaxCreditAmount,

Solution

Obvious slowdowns

  • for each property of NameValueCollection which is set, the condition can ommit the ==0 check.



  • you are sorting the results 2 times



  • using .Count(), but this seems to be needed.



  • calling unneccessary .ToList() on parents



  • posibly unneccessary call of .ToList() on result. Here you should check if CustomerPaged.aaData can take an IEnumerable



Naming

  • result is an IEnumerable so the plural should be used -> results



  • the kind of hungarian notation you use is seen as bad practice



  • local variables should be named using camelCase casing



General

Consider to extract the "index" strings, like "sSearch_0", into class const variables

Context

StackExchange Code Review Q#71807, answer score: 2

Revisions (0)

No revisions yet.