views:

78

answers:

3

I have previously asked a question about a stored proc that was executing too slowly on a sql server box, however, if I ran the sproc in Query Analyzer, it would return under one second. The client is a .NET 1.1 winforms app.

I was able to VNC into the user's box and, of course, they did not have SQL tools installed, so I cranked up Excel, went into VBA and wrote a quick function to call the sproc with exact same params.

It turns out that the sproc does return subsecond and I can loop through all the rows in no time at all. However, closing the connection is what takes a really long time, ranging from 5 seconds to 30.

Why would closing a connection take that long?

+1  A: 

The symptoms you describe are almost always due to an 'incorrect' cached query plan. While this is an large topic (see parameter sniffing here on SO), you can often (but not always) alleviate the problem by rebuilding a database's indexes andensuring that all statistics are up to date.

Mitch Wheat
I eliminated parameter sniffing as a possible culprit. It's not it. I've also rebuild indexes and did a full update statistics with fullscan. Note that the data comes back quickly. The connection takes forever to close.
AngryHacker
@AngryHacker: when you say 'close the connection', are you talking about it being returned to the connection pool? Can you post some example code that gives this behaviour?
Mitch Wheat
The issue is solved (see my comment on the OQ). However, something I learned is that when you close the connection (conn.Close) , but have not looped through the rows, the method will actually bring all the rows down to the client, before actually closing the connection.
AngryHacker
+1  A: 

If you're using a SqlDataReader, one thing you can try is once you have all the data you need, call Cancel on the SqlCommand before calling Close on the SqkDataReader. This will prevent the out parameters and return values from being filled in which might be the cause of the slowness to close the connection. Do it in a try catch block because it can throw a cancelled by user exception.

JP Alioto
+1  A: 

Connection pooling?

That, or I'd check for any service packs or KB articles for the client library.

Michael K Campbell