SQL Server SPIDS go into a sleeping state and never recover

706 Views Asked by At

I have a long running stored procedure that is executed from IIS. On average this stored procedure takes between two and five minutes to complete because it is searching through a large dataset. (although it has take around 20 minutes in some cases)

Most of the time the stored procedure works fine but every now and then the SPIDS go into a sleeping state and never recover. The only solution I have found is to restart the SQL Server and re-run the stored procedure

enter image description here

The are no table inserts in the proc (only table variable inserts), and the other statements are selects on a large table.

I'm stuck for where to start debugging this issue. Any hints one what it might be or suggestions on tools that would help me find the issue would be most helpful

EDIT: More info added:

  • The actual issue is the proc doesn't return the resultset. My first thought was to look at the spids, they were sleeping but the cputime was still increasing

  • It's a .Net app so .Net Core 3.1 with ASP.NET Core and a Blazor UI. The libary used for db connection is System.data.SqlClient I believe System.data.SqlClient uses it's own custom driver. Calling code below:

enter image description here

  • The stored procedure doesn't return multiple result sets, however obviously different instances of the proc run at the same time.

  • No limits to connection pooling in IIS

enter image description here

1

There are 1 best solutions below

3
BashBone On

@RichardWatts when you say " re-run the stored procedure" you mean that the same stored proc with the same parameter and data works once you restart SQL Server ?

If so look over your loc (sp_loc} inside your table probably another process loc some data and doesnt release it properly, specialy if you have transaction accessing the same tables. What is your your isolation level on your connexion ? If you can, try to change it to READ UNCOMMITTED to see if that solve your problem. as an alternate you can also add a WITH (NOLOCK) or (READUNCOMMITTED) to your sql command. Know that you will need to hold query with a read uncommited or nolock if you have some modification on the structure of your table or index re construction for example or they will in turn block its execution

Nevertheless be cautious this solution depend on your environment, specially if your tables gots lots of update, delete, insert,... this kind of isolation can lead to a Dirty read and doesnt adress the root cause of your problem wich I would bet is uncomited transaction (good article that explain it)

Make also a DBCC CHECKTABLE just to be sure on this side