Sql-server – SQL Server 2008 T-SQL select hanging, but not dead locked

lockingsql-server-2008stored-procedures

This is a pretty strange issue so bear with me. I have a stored proc that does some heavy duty processing. When it runs well it usually takes a few minutes depending on server load, but occasionally it appears to get stuck.

I have looked at the results of sp_who2 and what I see is that the process is not blocked, it's still listed as 'RUNNABLE', but the value in disk IO stays the same. Once the process gets in this state it never finishes. If I kill the process and re-execute the stored proc, I usually get the same result.

Sometimes re-executing the stored proc works, but increasingly I end up having to restart SQL Server. Not ideal… After restart the stored proc executes as expected. Has anyone even hit a problem like this? Any advice would be greatly appreciated.

Some more context. The stored proc I am executing looks something like this:

delete
from table1

insert into table1
select columns
from 
(
    select columns
    from giant_table_1 WITH (NOLOCK)
    where condition
    group by columns

    UNION

    select columns
    from giant_table_2 WITH (NOLOCK)
    where condition
    group by columns
)
where condition

more crud on table1
.
.
.

If I insert some logging statements into the stored proc I can see that when it hangs it hangs on the insert/select. Wondering if this could be a weird consequence of the NOLOCK statement. We (and by we I mean my boss :)) added the nolock because giant_table_1 and giant_table_2 are constantly being changed. Without the nolock our sproc was getting blocked a lot by other long running processes and vice versa.

Thanks in advance!

Best Answer

From the looks of your query I believe that the process is waiting on a spill to tempdb. You will need to look at the output of

SELECT * FROM sys.dm_os_waiting_tasks WHERE session_id = <SPID for the SP>

to get a better idea of whats occuring in the background, Adam Machanic's sp_whoisactive is a great tool for simplifing the collection of details about a queries execution.

Also make sure that autogrowth is set correctly for your database and tempdb with sensible auto growth amounts for the data and log and that the number of VLFs in the log files is under a 100. Look at his post for details 8 Steps to better Transaction Log throughput