Edit: I need help to rewrite these linq querys to SQL querys for highest possible performance.
I have a table with about 10 million rows. It consists of 7 columns including Id. First is Id, then three keys to "TradeObjectModel", finally three integers keeping the different TradeObjectModels rating values. Like this:
When a user, eg To1Id (TradeObjectModel1 owner) with key 71 handles her ratings of other Trade objects only one row is sufficent for the current view.
My attempt to solve this looks like this(explanation below code sample):
IEnumerable<RatingListTriangleModel> allTriangleModels1 =
this._ratingListTriangleRepository.All.Where(
ratingListRow =>
ratingListRow.To1Id == myTradeObject.TradeObjectId);
var filteredallTriangleModels1 = from row in allTriangleModels1
group row by row.To2Id into g
select g.First();
IEnumerable<RatingListTriangleModel> allTriangleModels2 =
this._ratingListTriangleRepository.All.Where(
ratingListRow =>
ratingListRow.To2Id == myTradeObject.TradeObjectId);
var filteredallTriangleModels2 = from row in allTriangleModels2
group row by row.To3Id into g
select g.First().
IEnumerable<RatingListTriangleModel> allTriangleModels3 =
this._ratingListTriangleRepository.All.Where(
ratingListRow =>
ratingListRow.To3Id == myTradeObject.TradeObjectId);
var filteredallTriangleModels3 = from row in allTriangleModels3
group row by row.To1Id into g
select g.First();
var fileredallTriangleModels =
filteredallTriangleModels1.Union(filteredallTriangleModels2).Union(filteredallTriangleModels3).ToList();
ViewBag.TriangleCount = fileredallTriangleModels.Count();
foreach (var ratingListRow in fileredallTriangleModels)
{
//Find which one is my ad and set me as setter and their object as receiver
if (ratingListRow.To1Id == customer.TradeObjectId)
{
var ri = new TriangleViewModel(
customer.TradeObjectId,
this._customerRepository.FindTradeObjectId(ratingListRow.To2Id),
ratingListRow,
this._tradeobjectRepository.Find(ratingListRow.To2Id));
model.Models3.Add(ri);
continue;
}
if (ratingListRow.To2Id == customer.TradeObjectId)
{
var ri = new TriangleViewModel(
customer.TradeObjectId,
this._customerRepository.FindTradeObjectId(ratingListRow.To3Id),
ratingListRow,
this._tradeobjectRepository.Find(ratingListRow.To3Id));
model.Models3.Add(ri);
continue;
}
if (ratingListRow.To3Id == customer.TradeObjectId)
{
var ri = new TriangleViewModel(
customer.TradeObjectId,
this._customerRepository.FindTradeObjectId(ratingListRow.To1Id),
ratingListRow,
this._tradeobjectRepository.Find(ratingListRow.To1Id));
model.Models3.Add(ri);
}
}
First I get all rows where my object is on the first column, groups them to select only one and then continues to do the same with me on the second and third column. The ToList() here is just temporary for med to be able to run stopwatch on them, each of these takes 0-12 seconds. Then I join them and run through them all to create the model used by the webgrid in front-end code.
This causes two problems: 1. It takes much to long. and 2. If my tradeobject id is on more than one column I will get more than one row presenting more than one o the Tradeobject I'm interested in.