我们有一个报告给我们一些严重的问题,所以我决定把它放到控制台应用程序中以排除问题。不寻常的SQL /数据问题
该报告只是一个简单的SQL选择,返回大约25列, ,我们的日期范围可以是3-6个月,返回大约10k行,所以我们不是在讨论大量数据。
这里是发生了什么,运行报告,它是从我们的网站超时,在控制台时,它从13-18分钟才能完成的任何地方,等待似乎在
da.Fill(ds);
发生现在这是奇怪的事情,它在SQL Server Management Studio中运行大约1-3秒,当我们的Delphi开发人员创建一个类似的应用程序时,它也运行几秒钟,这只发生在.NET
我们试过从数据集变成加载到数据读取器, 使用此代码..
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++; } }然而它没有任何帮助,它只是显示在卡盘中,但频繁延迟。我在想它的SQL,但不能解释为什么它在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 ..(也许统计数据,或损坏DB)
如果查询在SSMS中正常运行,您认为它是SQL如何? – JNK
在执行SQL语句之后向下滚动到SQL Management studion中的最后一行? – Yahia
看看这个主题: http://stackoverflow.com/questions/250713/sqldataadapter-fill-method-slow – granaker