Search code examples
c#sql-serverconnection

Why is connecting to SQL Server very slow?


We see that creating new connections takes about 250 milliseconds, which is much slower than expected.

From our application server to the SQL Server, we have about 1 millisecond ping-time, and we are operating inside pretty fast LAN.

Our connection string is:

Data Source=SQLServer;Initial Catalog=Database;Integrated Security=True

I have measured elapsed time around this simple statement

var conn = new SqlConnection(_connectionString);

if (conn.State == ConnectionState.Closed)
{
    var sw = Stopwatch.StartNew();
    await conn.OpenAsync().ConfigureAwait(false);
    sw.StopAndLogIfDelay(20, _log, "Open SQL Server connection");
}

I would not expect 250 milliseconds to connect in a fast LAN. Does anyone have ideas or experience in where the problem could be?


Solution

  • Setting minimum connections to 50 in th sql-server connection pool solved the immidate problem.

    What remains is to investigate why open db-connections takes such a long time in our organisation. I will ally with our network-team to see if we can locate the problem.