日期:2014-05-18  浏览次数:20941 次

C#代码读取MSserver数据库为什么逻辑读取这么大
Audit Logout cpu:0 reads:29116676 writes 32730 duration 13
……
Audit Logout cpu:0 reads:29116671 writes 32730 duration 16
追踪了一下,结果逻辑读取这么大。谁帮我看一下。是我重复打开的原因吗?

 
public string strcon = "Integrated Security=SSPI;Initial Catalog='db123';Data Source='Ghost-1383119';User ID='sa';Password='sa';Connect Timeout=0";
try
{
 SqlConnection conn = new SqlConnection(strcon);
  conn.Open();
  strSQL = "select id,title,keyword from web where id<120 and id>98 order by id asc";
  SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
  da.SelectCommand.CommandTimeout = 60000;  

  da.Fill(ds, "web");
  conn.Close();
  n = ds.Tables["web"].Rows.Count;
   
  foreach (DataRow dr in ds.Tables["web"].Rows)
  {
  titleid = dr["id"].ToString();
  if (titleid != null)
  {
  titleid = titleid.ToString();
  }
   


long id = long.Parse(inputid);
  string strNextSQL = "select top 1 id,title,time from web where keyword='" + keyword + "' and id>" + id + " order by id asc";
   
  SqlConnection con = new SqlConnection(strcon);
  con.Open();
  SqlDataAdapter datar = new SqlDataAdapter(strPrevSQL, con);
   
  DataSet ds = new DataSet();
  datar.Fill(ds);
  previd = ds.Tables[0].Rows[1]["id"].ToString();
  prevtitle = ds.Tables[0].Rows[1]["title"].ToString();
  ds = null;
con.Close();

SqlConnection mycon = new SqlConnection(strcon);  
  mycon.Open();

  long id = long.Parse(inputid);
  string strSQL = "select * from web where id=" + id;
  SqlDataReader dr = null;
  SqlCommand mycommand = new SqlCommand(strSQL, mycon);
  dr = mycommand.ExecuteReader();
  while (dr.Read())
  {
  titleid = dr["id"].ToString();
  title = dr["title"].ToString();
  }
  dr.Close();
  mycommand.CommandText = "update web set show_path=1 where id=" + long.Parse(inputid);  
  mycommand.ExecuteNonQuery();
  mycon.Close();

  }


------解决方案--------------------
不是这么“看看”就能看出问题所在的,你需要看sql server的执行计划,分析哪里有索引扫描或者全表扫描,看看是否缺索引等等。就sql语句来说,order by也会影响性能