I have written a class to upload a very large set of data into a database. The foreach loop seems to be slow, taking around 16ms per loop iteration. In what ways could I increase the speed of this operation? I have 41,000 rows to be added into a table that already has around 162,000 rows. The aditional 41k rows is taking 10min every time the method is called. I am retrieving this data from a large collection of thousands of files that are being parsed so you could imagine that this will be approximately 40 thousand rows of data per file times thousands of files. At 10 minutes each, I will be waiting an unreasonably long time to upload.
My theories are that perhaps the Convert.ToInt32() is costly but can that really be the cause of my slowness? The issue here is within my loop.
class Uploader
{
public void beginUpload(List<WaterData> data)
{
using (var db = new RTIDBContext())
{
var waterData = db.Set<water_data>();
foreach (var day in data)
{
var rowdata = new water_data { measurment_date = day.date.ToString(), cond = Convert.ToInt32(day.waterConductivity), temp = null };
waterData.Add(rowdata);
}
db.SaveChanges();
}
}
}
Also, here is my WaterData class:
class WaterData
{
public DateTime date { get; set; }
public double waterConductivity { get; set; }
}