从外部应用程序运行时,经过的时间和CPU时间不正确

时间:2016-03-11 07:02:37

标签: sql-server console-application sqlperformance

从.net控制台应用程序执行SqlCommand时是否有任何改进或神奇的方法? 从基于.net的应用程序运行命令时,我看到了巨大的改进和一致的时间,但在Sql server management studio中执行时,它完全不同。

.NET代码

static void Main(string[] args)
        {

            try
            {

                string strConn = "Data Source=.;Initial Catalog=school;Integrated Security=True";
                using (SqlConnection conn = new SqlConnection(strConn)) 
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand();
                    command.CommandType = System.Data.CommandType.Text;
                    command.CommandText = "SET NOCOUNT OFF;SET STATISTICS IO,TIME ON; Exec spStudents @type='SELECTALL';SET STATISTICS IO,TIME OFF;";
                    conn.InfoMessage += conn_InfoMessage;
                    command.Connection = conn;
                    using (var reader = command.ExecuteReader())
                    {
                        do
                        {
                            while (reader.Read())
                            {
                                //grab the first column to force the row down the pipe
                                var x = reader[0];
                            }
                        } while (reader.NextResult());

                    }
                    Console.Read();
                }                
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        static void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
        {
            foreach (SqlError item in e.Errors)
            {
                Console.WriteLine(item.Message);
            }
        }

我看到sql报告的CPU time = 130 ~ 150 mselapsed time = 250ms ~ 300 ms时间一致。但是当在SSMS中运行相同时,它报告我CPU time = 900ms, elapsed time = 1s。那么哪一个是可靠的结果呢? 我昨天问过的另一个相关问题SQL query performance statistics messages returned multiple times

0 个答案:

没有答案