Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么ExecuteScalar第一次调用需要时间?_C#_Sql Server 2008 - Fatal编程技术网

C# 为什么ExecuteScalar第一次调用需要时间?

C# 为什么ExecuteScalar第一次调用需要时间?,c#,sql-server-2008,C#,Sql Server 2008,下面是我的程序,用于测量ExecuteScalar多次迭代所花费的时间 static void Main(string[] args) { string sqlConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"; SqlConnection connection = new SqlConnection(sqlConnectionString);

下面是我的程序,用于测量ExecuteScalar多次迭代所花费的时间

static void Main(string[] args)
{
    string sqlConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
    SqlConnection connection = new SqlConnection(sqlConnectionString);
    for(int i=0; i <4; i++){
        Stopwatch stopWatch = new Stopwatch();

        string sqlCommand = "Insert into TestTable (SNO, Name) values (" + i + ",' " + i + "')";
        SqlCommand command = new SqlCommand(sqlCommand, connection);
        connection.Open();
        stopWatch.Start();
        var result = command.ExecuteScalar();
        stopWatch.Stop();
        connection.Close();
        Console.WriteLine("Time elapsed to insert row " + i + " : " + stopWatch.ElapsedMilliseconds);
    }
    Console.ReadKey();
}
我的问题是,为什么第一次迭代要花3毫秒,而剩下的时间要比这少


提前感谢。

最有可能的是连接的建立,它并没有在关闭时真正结束,而是返回到连接池并在下一次迭代中重新使用

static void Main(string[] args)
{
    string sqlConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
    SqlConnection connection = new SqlConnection(sqlConnectionString);
    for(int i=0; i <4; i++){
        Stopwatch stopWatch = new Stopwatch();

        string sqlCommand = "Insert into TestTable (SNO, Name) values (" + i + ",' " + i + "')";
        SqlCommand command = new SqlCommand(sqlCommand, connection);
        connection.Open();
        stopWatch.Start();
        var result = command.ExecuteScalar();
        stopWatch.Stop();
        connection.Close();
        Console.WriteLine("Time elapsed to insert row " + i + " : " + stopWatch.ElapsedMilliseconds);
    }
    Console.ReadKey();
}

通常,您还可以考虑查询计划缓存和来自DBMS的实际数据缓存,但在这种情况下,这并不真正适用于INSERT操作(尽管如此,在第一次迭代期间,它所需的元数据可能是冷的).

最有可能的是连接的建立,它并没有在关闭时真正结束,而是返回到连接池,并在下一次迭代中重新使用

static void Main(string[] args)
{
    string sqlConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";
    SqlConnection connection = new SqlConnection(sqlConnectionString);
    for(int i=0; i <4; i++){
        Stopwatch stopWatch = new Stopwatch();

        string sqlCommand = "Insert into TestTable (SNO, Name) values (" + i + ",' " + i + "')";
        SqlCommand command = new SqlCommand(sqlCommand, connection);
        connection.Open();
        stopWatch.Start();
        var result = command.ExecuteScalar();
        stopWatch.Stop();
        connection.Close();
        Console.WriteLine("Time elapsed to insert row " + i + " : " + stopWatch.ElapsedMilliseconds);
    }
    Console.ReadKey();
}

通常,您还可以考虑查询计划缓存和来自DBMS的实际数据缓存,但在这种情况下,这并不真正适用于插入操作(尽管如此,在第一次迭代期间,它所需的元数据可能是冷的)。

首先,您应该使用
ExecuteNonQuery()
方法。现在谈谈时间;第一次,它必须建立连接,然后执行查询,但对于以后的迭代,情况不再如此。

首先,您应该使用
ExecuteNonQuery()
方法。现在谈谈时间;第一次它必须建立连接,然后执行查询,但对于以后的迭代,情况不再如此。

这是由于连接池。一旦建立连接(无论是否关闭),只要连接字符串保持不变,连接就会被池化,从而导致更快的连续执行。

这是由于连接池化。一旦建立了连接(无论是否关闭),只要连接字符串保持不变,连接就会被合并,从而导致更快的连续执行。

-第一次连接时,打开连接时,它确实需要与服务器建立真正的连接。在随后的运行中,它可能再次连接到相同的连接。2毫秒的差异真的重要吗?可能是因为连接池?除了不太准确的测量方法之外,发生这种情况可能有多种原因。例如,sql连接仅在第一次创建,其他时间则从连接池中获取。谢谢您的回答。在我的程序中,我只测量ExecuteScalar部分,而不是连接建立。-第一次连接时。打开,它确实需要与服务器建立真正的连接。在随后的运行中,它可能再次连接到相同的连接。2毫秒的差异真的重要吗?可能是因为连接池?除了不太准确的测量方法之外,发生这种情况可能有多种原因。例如,sql连接仅在第一次创建,其他时间则从连接池中获取。谢谢您的回答。在我的程序中,我只测量ExecuteScalar部分,而不是连接建立。