1
第一個問題在這裏:)WCF服務查詢時間與直接查詢到DB 6X慢
我與得到的查詢從一個WinForms應用程序,將其轉換爲XML,將其發送到SQL Server數據庫WCF服務工作,將查詢數據作爲XML返回,將其轉換爲DataTable並將其發送回WinForms應用程序。
我測試了這個和直接的數據庫查詢之間的速度差異,看起來使用我的服務減慢了6倍。
我需要做的是減少服務執行查詢的時間,但我不知道它是如何工作的。 關於問題出在哪裏,你有什麼大概的想法嗎?有什麼方法可以提高服務的性能嗎?使用JSON(而不是XML)可以幫助解決這個問題嗎?
非常感謝您閱讀本文,期待着您的解答!
這裏是我用來測試兩個連接代碼:
namespace TestServiceVSDatabaseSpeed
{
class Program
{
static void Main(string[] args)
{
// Initialization
DataTable dtService = new DataTable(), dtDirect = new DataTable();
Console.Write("~Direct Connection VS Service Connection~\n\nSELECT * FROM COILS\n");
Stopwatch sw1 = new Stopwatch(), sw2 = new Stopwatch();
// Direct connection test
sw1.Start();
dtDirect = GetSqlDataDirectConnection();
Single directTime = sw1.ElapsedMilliseconds;
Console.WriteLine("Direct query time: {0} ms ({1} rows)", directTime, dtDirect.Rows.Count);
sw1.Stop();
// Service connection test
sw2.Start();
dtService = GetSqlDataServiceConnection();
Single serviceTime = sw2.ElapsedMilliseconds;
Console.WriteLine("Query via service time: {0} ms ({1} rows)", serviceTime, dtService.Rows.Count);
sw2.Stop();
// Conclusion
Console.WriteLine("\nDirect Connection faster than Service Connection by {0} ms!", serviceTime - directTime);
Console.WriteLine("That's {0} times faster!", serviceTime/directTime);
Console.ReadLine();
}
/// <summary>
/// Does a direct query to the database
/// </summary>
/// <returns>A DataTable.</returns>
private static DataTable GetSqlDataDirectConnection()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Persist Security Info=False;User ID=MyId;Password=MyPassword;Initial Catalog=The_Catalogue;Server=ThisServer";
try
{
conn.Open();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
using (SqlCommand command = new SqlCommand("SELECT * FROM Coils", conn))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
DataTable coilTable = new DataTable("Coils");
adapter.Fill(coilTable);
return coilTable;
}
}
}
}
/// <summary>
/// Does a query to the database via the IOM Service.
/// </summary>
/// <returns>A DataTable.</returns>
private static DataTable GetSqlDataServiceConnection()
{
DataContractClient contract = new DataContractClient();
contract.Endpoint.Address = new EndpointAddress("net.tcp://localhost:8888/MyService/DataContract");
NetTcpContextBinding netTcpContextBinding = new NetTcpContextBinding(SecurityMode.None) { MaxReceivedMessageSize = 2147483647 };
contract.Endpoint.Binding = netTcpContextBinding;
contract.Endpoint.Contract = ContractDescription.GetContract(typeof(IDataContract));
return contract.ExecuteQueryNoParam("SELECT * FROM Coils", Catalog.SqlCatalog).Tables[0];
}
}
}
下面是結果:
~Direct Connection VS Service Connection~
SELECT * FROM COILS
Direct query time: 436 ms (24596 rows)
Query via service time: 2748 ms (24596 rows)
Direct Connection faster than Service Connection by 2312 ms!
That's 6.302752 times faster!
除了轉換數據的開銷之外,這裏有相當數量的[DataTables中的固有開銷](https://stackoverflow.com/questions/424598/what-is-the-memory-overhead-of-storing- data-in-a-net-datatable),這將全部必須回落。 – stuartd
很多原因......對於 - 可能會考慮net.tcp的二進制編碼。檢查第一次呼叫與後續呼叫性能......因爲服務中可能存在一些初始化成本。服務實例可能會起作用 - 特別是如果您的服務啓動緩慢。在服務案例中,數據表被填充,然後序列化,傳輸,然後反序列化......所有代價都很高。隨着尺寸的增加,賭注開銷下降(百分比)。 – Clay
使我的皮膚爬行看到通過服務連接發送的SQL語句,順便說一句。可怕 - 並且緊密耦合...但是再次,使用數據表耦合太緊密。考慮一個數據協定裝飾的POCO--它可能會更快地序列化/反序列化。 – Clay