I have a db table with 2M entries
My XPositions table structure is
Id - int
FID - int
CoordinateQue - int
Latitude - float
Longitude - float
Each row represents a marker position and I need to calculate distance between each coordinates and save to another table.
My xWeights table structure is;
Id - int
x_Id - int
Tox - int
Distance - decimal(18,8)
So far my working code is
var query = _xRepository.TableNoTracking;
var xNodes = query.ToList()
var n = new xWeights();
foreach (var x in xNodes)
{
for (var i = 0; i < xNodes.Count; i++)
{
if(x.Id == xNodes[i].Id)
{
//Do nothing - Same Node
}
else
{
var R = 6378137;
var φ1 = (Math.PI / 180) * x.Latitude;
var φ2 = (Math.PI / 180) * xNodes[i].Latitude;
var Δφ = (xNodes[i].Latitude - x.Latitude) * (Math.PI / 180);
var Δλ = (xNodes[i].Longitude - x.Longitude) * (Math.PI / 180);
var Δψ = Math.Log(Math.Tan(Math.PI / 4 + φ2 / 2) / Math.Tan(Math.PI / 4 + φ1 / 2));
var q = Math.Abs(Δψ) > 10e-12 ? Δφ / Δψ : Math.Cos(φ1); // E-W course creates problem with 0/0
// if Longitude over 180° take shorter rhumb line across the anti-meridian:
if (Math.Abs(Δλ) > Math.PI) Δλ = Δλ > 0 ? -(2 * Math.PI - Δλ) : (2 * Math.PI + Δλ);
var dist = (Math.Sqrt(Δφ * Δφ + q * q * Δλ * Δλ)) * R;
n.x_Id = x.Id;
n.Tox = xNodes[i].Id;
n.Distance = dist;
_xWeightsRepository.Insert(n);
}
}
}
My problem is; I am getting approximately 35k records per minute so will be 2.1M record per hour. This will take forever to finish this. Any ideas how to improve the performance?