I have website and dedicated server for my website.
In my project there is 10^6 loop witch import record from xml file and finally save in database.
The for loop section is not fast as windows application version of this project.
the question is:
for loop
section of my project? Thank you
Code:
foreach (var row in records)// about 150,000 record
{
string cs = row.Field<string>("tsb");
try
{
int rowCount = row.Table.Columns.Count - 6;
string rowUserName = row.Field<string>("UserName").Trim();
if (rowUserName != userName)
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_eror_user_unique + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_eror_user_unique + ":" + cs });
}
long drh = Convert.ToInt32(row.Field<string>("drh"));
var gm = db.gms.AsNoTracking().FirstOrDefault(g => g.number == drh && g.id_gmState == 1);
if (gm == null || gm.rowCount != rowCount)
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_eror_gm_count + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_eror_gm_count + ":" + cs });
}
int ck = 0;
int cj = 0;
try
{
ck = Convert.ToInt32(row.Field<string>("_txtcodnakol"));
cj = Convert.ToInt32(row.Field<string>("_txtcodnajoz"));
}
catch
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_erorr_code + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_erorr_code + ":" + cs });
}
if (db.Cards.AsNoTracking().Any(c => c.id_user == user.Id && c.cs == cs && c.id_gm == gm.id && c.ck == ck && c.cj == cj))
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_eror_card_unique + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_eror_card_unique + ":" + cs });
}
string value = "";
string rowContent = gm.gmRow.gmRowTitles.FirstOrDefault()?.rowContent;
int countOption = Regex.Matches(rowContent, "ch=").Count;
var coefficientValue = gm.CoefficientValues.Any() ? gm.CoefficientValues.FirstOrDefault().value : 1;
int realCost = 0;
for (int i = 1; i <= gm.rowCount; i++) // at least 15 rows
{
string option = row.Field<string>("j" + i);
var distinctOption = option.Distinct().ToArray();
if (string.IsNullOrEmpty(option) || option.Length != distinctOption.Length)
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs });
}
for (int j = 0; j < distinctOption.Length; j++)// under 4 count loop
{
int item = Convert.ToInt32(distinctOption[j].ToString());
if (item < 1 || item > countOption)
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs });
}
}
for (int j = 1; j <= countOption; j++) // under 4 count loop
{
if (option?.Contains(j + "") ?? false)
value += "1,";
else
value += "0,";
}
if (realCost == 0 && i == 1)
realCost = 1;
realCost *= (option.Length * coefficientValue);
}
long cost = Convert.ToInt32(row.Field<string>("tmvs"));
if ((realCost != cost && (realCost > gm.minCardValue)) || (realCost > gm.maxCardValue))
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs });
}
if ((realCost != cost && (realCost <= gm.minCardValue)))
{
if (cost != gm.minCardValue)
{
task.taskStatus = (int)taskStatus.failed;
task.details += Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs });
}
}
Card card = new Card
{
id_gm = gm.id,
id_user = user.Id,
id_cardStates = 1,
value = value,
Cost = cost,
cj = cj,
ck = ck,
cs = cs
};
db.Cards.Add(card);
progress += perPercent;
if ((int)progress > task.percentCompleted)
{
task.percentCompleted = (int)progress;
TaskHelper.edit(task);
}
}
catch (Exception ex)
{
task.taskStatus = (int)taskStatus.failed;
task.details += ex.InnerException + ":" + cs;
TaskHelper.edit(task);
return Json(new { success = false, message = ex.InnerException + ":" + cs });
}
}
The problem resolved The main problem was
db.Cards.Add(card);
This method invoked and give many times to add new card
In my new code i used
List<Card> listCard = new List<Card>();
listCard.add(card); //db.Cards.Add(card);
this increased my performance from 35 minutes
to 4 seconds
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.