Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to investigate why SqlConnection takes too long to open?

In our testing environment, opening a SqlConnection takes around 5 seconds. I would like to investigate why it is taking so long, but I am not sure where to look at. Until the connection is open there are no entries in the SqlProfiler. The sql server (ver 2012) is running on a separate machine and we are connecting to it from another computer in the same network. We see the 5 second freeze in console applications as well as from the IIS (ASP.NET). The communication itself is fast.

Here's a screenshot from dotTrace:

dotTrace

What can I do to investigate what exactly is taking those 5 seconds? Thanks.

Update: Thanks everyone for great comments. It was the authentication - once we switched from integrated to sql authentication, the slowdowns were gone.

like image 359
Martin Haluza Avatar asked Oct 29 '25 09:10

Martin Haluza


1 Answers

Here go the usual best bets:

  • Change the connection string from SSPI to SQL Authentication. If SQL authentication is faster > focus on authentication providers
  • Change the connectivity from tcp/ip to named pipes. If named pipes are faster > focus on networking stack
  • Change connection string from using host name to fully qualified host name. If faster > fix DNS/WINS properties. See also Initial connection to SQL Server Connection Is Slow. Why?.
  • Change connection string from using host name to IP address. If faster, fix DNS lookup. See also https://social.msdn.microsoft.com/Forums/en-US/807748eb-51c8-4702-b7e9-fa32b7028747/sqlconnectionopen-takes-up-to-5-seconds-first-time-its-called-why-is-it-so-slow-using-net-35?forum=sqldataaccess
like image 57
Cee McSharpface Avatar answered Oct 31 '25 01:10

Cee McSharpface



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!