I developed a ASP.NET MVC Web Application and using Linq to Entity
And I got a trouble with memory usage
Here is my code:
//Search for about 0.6 million transaction data
var presQuery=(from A in db.TransDetail
where A.TRAN_DATE.CompareTo("2015/02/01")>=0 && A.TRAN_DATE.CompareTo("2015/02/28")<=0
group A by A.MBR_ID into grp
select new {
MBR_ID=grp.Key,
Price=grp.Sum(s=>s.Price)}).ToList();
//Search for about 0.6 million transaction data
var preVQuery=(from A in db.TransDetail
where A.TRAN_DATE.CompareTo("2015/01/01")>=0 && A.TRAN_DATE.CompareTo("2015/01/31")<=0
group A by A.MBR_ID into grp
select new {
MBR_ID=grp.Key,
Price=grp.Sum(s=>s.Price)}).ToList();
int[] rankValue=new int(){2000,1000,500,250,100};
TransDetailViewModel model;
List<TransDetailViewModel> ls=new List<TransDetailViewModel>();
double CountRatio;
int TotalPrice;
int TotalCnt;
//start calculate
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if (i < 5 && j < 5)
{
var query1 = presQuery.Where(w => w.Price >= rankValue[i]);
var query2 = prevQuery.Where(w => w.Price >= rankValue[j]);
var query3 = from q1 in query1
join q2 in query2 on q1.MBR_ID equals q2.MBR_ID
select q1;
TotalCnt = query3.Count();
TotalPrice = Convert.ToInt32(query3.Sum(s => s.Price));
CountRatio = (Convert.ToDouble(query1.Count()) / Convert.ToDouble(query2.Count())) * 100;
model = new TransDetailViewModel()
{
RankLevel = "R" + i.ToString() + j.ToString(),
CountRatio = CountRatio,
TotalCount = TotalCnt,
TotalPrice = TotalPrice
};
ls.Add(model);
}
}
}
the above code will cause the issue about application memory usage increase to 1G when there are four people starting search at the same time.
Is there a more efficient way to solve?