c#花费数小时的时间来执行

use*_*640 1 c#

下面的代码需要花费数小时的时间来执行.我正在比较数据库中的每个字符串来计算N00,N01,N10,N11参数.Temp1是类型字符串的列表,它包含5000多个单词

foreach (string ri in temp1)
{
  for (int a3 = 0; a3 < ssl.Count; a3++)
  {
    //for (int tn = 0; tn < tempNam.Count ; tn++)
    //{
        try
        {
          SqlCommand cmd5 = new SqlCommand("select count(*) from sample s inner join   sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + "=0 and s.KeyWord='y' and s. " +  ssl[a3].ToString()+ "=0", con);
          int im = (int)cmd5.ExecuteScalar();
          if (im == 1)
          {
            gh += 1;
          }
          SqlCommand cmd6 = new SqlCommand("select count(*) from sample s inner join   sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + "=0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + ">0", con);
          int im1 = (int)cmd6.ExecuteScalar();
          if (im1 == 1)
          {
            gh2 += 1;
          }
          SqlCommand cmd7 = new SqlCommand("select count(*) from sample s inner join   sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + ">0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + "=0", con);
          int im2 = (int)cmd7.ExecuteScalar();
          if (im2 == 1)
          {
            gh3 += 1;
          }
          SqlCommand cmd8 = new SqlCommand("select count(*) from sample s inner join   sample ss on ss.KeyWord='" + ri + "' and ss. " + ssl[a3].ToString() + ">0 and s.KeyWord='y' and s. " + ssl[a3].ToString() + ">0", con);
          int im3 = (int)cmd8.ExecuteScalar();
          if (im3 == 1)
          {
            gh4 += 1;
          }
          if (a3 == (ssl.Count-1))
          {
            SqlCommand ins = new SqlCommand("update sample set N00=" + gh + " where KeyWord='" + ri + "'", con);
            ins.ExecuteNonQuery();
            gh = 0;
            SqlCommand ins1 = new SqlCommand("update sample set N01=" + gh2 + " where KeyWord='" + ri + "'", con);
            ins1.ExecuteNonQuery();
            gh2 = 0;
            SqlCommand ins2 = new SqlCommand("update sample set N10=" + gh3 + " where KeyWord='" + ri + "'", con);
            ins2.ExecuteNonQuery();
            gh3 = 0;
            SqlCommand ins4 = new SqlCommand("update sample set N11=" + gh4 + " where KeyWord='" + ri + "'", con);
            ins4.ExecuteNonQuery();
            gh4 = 0;
          }
        }
        catch (Exception ex)
        {
        }
      // }
    }
  }

  SqlCommand cmd1s = new SqlCommand("select KeyWord from sample", con);
  SqlDataAdapter da = new SqlDataAdapter(cmd1s);
  DataSet ds = new DataSet();
  da.Fill(ds, "sample");
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
    string dd = dr["KeyWord"].ToString();
    if (dd != "y")
    {
      if (!li.Contains(dd))
      {
        li.Add(dd);
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

Kyl*_*ndo 11

放一个索引Sample.KeyWord.这会让这更加快速.

和Anthony的评论一样,这段代码有很多错误.该索引是我猜的花费最多的时间,但你也应该阅读这些主题:


在这里查看创建索引:http://msdn.microsoft.com/en-us/library/ms188783.aspx