首页 文章

不寻常的SQL /数据问题

提问于
浏览
0

我们有一份报告给我们一些严重的问题,因此我决定将其放入控制台应用程序以解决问题 .

该报告只是一个简单的SQL选择,返回大约25列,我们的日期范围可以是3-6个月,返回大约10k行,所以我们不是在谈论大量数据 .

这里发生了什么,当报告运行时,它从我们的网站,控制台,超过13-18分钟到完成,等待似乎发生在

da.Fill(ds);

现在这是奇怪的事情,它在SQL Server Management Studio中运行大约1-3秒,当我们的Delphi开发人员创建类似的应用程序时,它也是几秒钟运行,这只发生在使用.NET

我们尝试使用此代码从数据集更改为加载到datareader .

using (var dr = _command.ExecuteReader())
{
  if (dr.HasRows)
  {
    int i = 0;
    while (dr.Read())
    {
      var startRead = DateTime.Now;
      Console.Write("{2}\t{0}\t{1}\t", dr.GetInt32(0), dr.GetString(1), i);
      var tookRead = DateTime.Now.Subtract(startRead);
      Console.WriteLine("Took: " + tookRead);
      i++;
    }
}

然而它根本没有帮助,它只是显示在chucks中,但经常有延迟 . 我'm thinking its SQL, but can'解释为什么它在Delphi和SQL Management Studio中正常工作 .

我尝试过在所有框架上使用.NET 2.0,3.5和4 .

这是我的代码

public static DataSet GetData()
{
  var now = DateTime.Now;
  var _command = new SqlCommand();
  var _connection = new SqlConnection();

  try
  {
    _connection.ConnectionString = connectionString;

    _command.Connection = _connection;
    _command.CommandText = storedProcedure;
    _command.CommandType = CommandType.StoredProcedure;
    _command.CommandTimeout = 60;

    if (string.IsNullOrEmpty(_connection.ConnectionString)) { throw new Exception("Connection String was not supplied"); }

    _command.Parameters.Add(new SqlParameter("DateFrom", dateFrom));
    _command.Parameters.Add(new SqlParameter("DateTo", dateTo));

    SqlDataAdapter da;
    var ds = new DataSet();

    _connection.Open();

    var done = DateTime.Now;

    da = new SqlDataAdapter(_command);
    da.Fill(ds);

    if (ds == null) { throw new Exception("DataSet is null."); }
    if (ds.Tables.Count == 0) { throw new Exception("Table count is 0"); }

    var took = done.Subtract(now);

    return ds;

  }
  catch (Exception ex)
  {
    File.WriteAllText(Path.Combine(Application.StartupPath, String.Format("Exception{0:MMddyyyy_HHmmss}.log", DateTime.Now)), ex.ToString());
  }
  finally
  {
    if (_connection.State != ConnectionState.Closed) { _connection.Close(); }
  }

  return null;
}

有任何想法吗?我们的DBA指责框架,我实际上在SQL中责备某些东西..(可能是统计数据,或者是损坏的数据库)

2 回答

  • 2

    .NET与其他客户端(SQL Management Studio)之间SQL性能的差异通常取决于配置不同的连接 - 频繁的罪魁祸首是ANSI_NULLS; ANSI_PADDING .

    尝试查看SQL Management Studio中如何配置连接,然后在.NET应用程序中复制相同的内容 .

  • 0

    您提供的信息不包含足够的细节以帮助...

    如果SSMS真的快得多,那么原因可能是某些会话/连接设置 - 与.NET相比,SSMS使用了微妙的不同设置 .

    有关可能有什么不同/错误等的一些解释和提示,请参阅http://www.sommarskog.se/query-plan-mysteries.html

相关问题