简体   繁体   English

需要帮助,循环到数据库花费的时间太长

[英]Need Help, looping to database takes too long

i would like to ask for help as to what makes this code take too long to process : 我想寻求有关导致此代码处理时间过长的帮助:

using (OleDbDataReader dr = command.ExecuteReader()){
      while (dr.Read())
      {
          count += 1;
     if (Convert.ToDecimal(dr["AMOUNT"]) ==     Convert.ToDecimal(0.00) &&
     Convert.ToDecimal(dr["PENALTY"]) == Convert.ToDecimal(0.00) && Convert.ToDecimal(dr["DEPDIV"]) == Convert.ToDecimal(0.00))
      {
          //Cancel Upload and let user check is .DTLS contains Amount
          ViewBag.Message = "UPLOADING CANCELLED!! : .DTLS file does not contain any amount for the Following Fields :"
       + "AMOUNT UNPAID, "
       + "PENALTY "
       + "DEPDIV "
       + "PLEASE CHECK INGRES SETTING FOR MONEY FORMAT(REQUIRED MONEY FORMAT IS : II_MONEY_FORMAT=L:á"
       + "THEN REDOWNLOAD THE .MAST and DTLS FILE from the DELINQUENT EMPLOYERS EXTRACTION UTILITY BEFORE UPLOADING";

          return View();
      }
      else
      {
          string Acctno, EyerID, Percov;
          decimal Amount, Penalty, DepDiv;

          EyerID = dr["EYERID"].ToString().Trim();
          Percov = dr["PERCOV"].ToString().Trim();
          Amount = Convert.ToDecimal(dr["AMOUNT"]);
          Penalty = Convert.ToDecimal(dr["PENALTY"]);
          DepDiv = Convert.ToDecimal(dr["DEPDIV"]);
          Acctno = SaveUpdateTransMaster.spRetrieveAcctNo(EyerID, brid, "MCDEL", Convert.ToDateTime(currCutoff));

          ListLedger.Add(new TransLedger
          {
              EyerID = EyerID,
              AcctNo = Acctno,
              Percov = Percov,
              AmtDue = Amount,
              Penalty = Penalty,
              DepDiv = DepDiv,
          });
      }
  }
}

"MCDEL");
}

foreach (var row in ListLedger)
{
   SaveUpdateTransMaster.spUploadTransLedgerCA(row.AcctNo, row.EyerID, 
   currCutoff, row.Percov,Convert.ToDecimal(row.AmtDue), Convert.ToDecimal(row.Penalty), 
   Convert.ToDecimal(row.DepDiv), brid, "MCDEL");
}

Here is my scenario : 这是我的情况:

i usually fetch millions of rows to a FoxPRO DBF, store it to a List and loop it insert it to the DB through StoredProc. 我通常将数以百万计的行提取到FoxPRO DBF,将其存储到列表中,然后通过StoredProc将其循环插入到DB中。

It looks like you're duplicating some of your code. 看来您正在复制某些代码。 When you create each row, you are converting Amount, Penalty and and DepDiv to decimals, and then again when you are uploading them, you are converting those fields to decimal again. 创建每一行时,您将金额,罚款和DepDiv转换为小数,然后在上载时再次将这些字段转换为小数。 In addition, you are computing them in the conditional portion of the if statement. 另外,您正在if语句的条件部分中计算它们。 You should probably compute the values for each row before the if statement to reduce some of the code duplication and save time. 您可能应该在if语句之前计算每一行的值,以减少某些代码重复并节省时间。 Even if you save just a millisecond per row, that's almost 17 minutes per million rows. 即使每行仅节省一毫秒,也就相当于每百万行17分钟。

However, as Alan B suggested, perhaps you could post the code of the Foxpro query and we can look at that for improvements in speed and/or data format. 但是,正如艾伦·B(Alan B)所建议的那样,也许您可​​以发布Foxpro查询的代码,我们可以着眼于此以提高速度和/或数据格式。 It's possible the query could also be altered to output something you could directly import to SQL Server, such as a comma or tab delimited file. 查询也可能会更改为输出您可以直接导入到SQL Server的内容,例如逗号或制表符分隔的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM