WCF服务查询时间与直接查询到DB 6x的速度较慢

时间:2017-06-19 15:05:36

标签: c# sql-server winforms performance wcf

这里的第一个问题:)

我正在使用WCF服务,该服务从WinForms应用程序获取查询,将其转换为XML,将其发送到SQL Server数据库,将查询数据作为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!

1 个答案:

答案 0 :(得分:0)

如果我看一下,似乎我们需要看到转换呼叫的服务。 回答你的问题:

  1. 一般来说,直接调用更快是正常的,转换为XML可能需要一些时间。
  2. 在没有看到服务本身的情况下不能说。
  3. 我不认为改用JSON会产生很大的影响,你仍在处理24596行。
  4. 您可以在通话结束后处理结果。但总而言之,正常情况下你会有很大的性能影响,确保使用索引,如果你不使用它,如果选择你可能会遇到更大的查询时间问题只有部分项目。