patterncsharpMinor
Bottleneck encountered returning records for my grid
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,
```
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
Naming
General
Consider to extract the "index" strings, like "sSearch_0", into class const variables
- for each property of
NameValueCollectionwhich is set, the condition can ommit the==0check.
- you are sorting the results 2 times
- using
.Count(), but this seems to be needed.
- calling unneccessary
.ToList()onparents
- posibly unneccessary call of
.ToList()onresult. Here you should check ifCustomerPaged.aaDatacan take anIEnumerable
Naming
resultis anIEnumerableso the plural should be used ->results
- the kind of hungarian notation you use is seen as bad practice
- local variables should be named using
camelCasecasing
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.