2012-01-16 44 views
0

我們有一個報告給我們一些嚴重的問題,所以我決定把它放到控制檯應用程序中以排除問題。不尋常的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)

+0

如果查詢在SSMS中正常運行,您認爲它是SQL如何? – JNK

+0

在執行SQL語句之後向下滾動到SQL Management studion中的最後一行? – Yahia

+1

看看這個主題: http://stackoverflow.com/questions/250713/sqldataadapter-fill-method-slow – granaker

回答

0

你給不包含足夠的細節,真正幫助的信息......

IF SSMS實際上要快得多,原因可能是某些會話/連接設置 - 與.NET相比,SSMS使用微妙不同的設置。

有關,這可能是不同的/錯誤等一些解釋和提示見(SQL Management Studio中).NET和其他客戶之間的SQL性能http://www.sommarskog.se/query-plan-mysteries.html

1

差異通常是下降到連接被不同的配置 - 頻繁罪魁禍首是ANSI_NULLS; ANSI_PADDING。

嘗試查看SQL Management Studio中連接的配置方式,然後在.NET應用程序中複製相同的內容。