Within SQL Server it is extremely difficult to find which spid is using the most CPU. There are two methods for doing this:
-Using PERFMON, looking at the Thread Performance Object: % Processor Time, ID Process, ID Thread. Select ALL SQL threads (sqlservr/*). Then flipping it to “tabular” report format (ie. NOT the graph) and scrolling over 100 entries to find the thread that is using the most CPU time. Since a query can move from thread to thread, it’s a real tough time. Once you have the thread with the CPU, then get the ID Thread. The ID Thread is the KPID in sysprocesses. Join on this to get the SPID. Then DBCC INPUTBUFFER(spid) to see what the query is running. You can also KILL the spid, too. The ID Process is the NT PID.
select spid
from master..sysprocesses
where kpid =
go
DBCC INPUTBUFFER(
go
-As you can see, this is a difficult process. A quick and dirty method that I use that isn’t 100% accurate, but is about 75% correct is to run the following query. The long running jobs that this query reports tend to be the ones using the most CPU time.
CREATE proc boe_proc as
select datediff(mi, last_batch,getdate())'minutes',spid, waittype, cpu, physical_io, convert(char(15),hostname), convert(char(15),program_name), convert(char(20),getdate()),spid, last_batch, cmd
from master..sysprocesses
where spid > 50 and
cmd not like '%WAIT%' and
datediff(mi, last_batch,getdate()) > 1
order by last_batch
Here’s sample output, it shows the backup running and a batch job that has been going for 5 minutes app server MSSAP32 Batch Process (BTC) 20.
.
exec boe_proc
minutesspidwaittype cpuphysical_iospidlast_batchcmd
----------- ------ -------- ----------- -------------------- --------------- --------------- -------------------- ------ ------------------------------------------------------ ----------------
3891500x042224247296929618MSSAP01R3D03(3) unc rd
6516870x000028755756332MSSAP99SQLAgent - TSQL
6516870x0000638285756332MSSAP99SQLAgent - TSQL
6516870x0000623915756332MSSAP99SQLAgent - TSQL
6516870x0000592815756332MSSAP99SQLAgent - TSQL
6516870x0000297035756333MSSAP99SQLAgent - TSQL
65 16870x0000279845756333MSSAP99SQLAgent - TSQL
6516870x0000296715756333MSSAP99SQLAgent - TSQL
6516870x0000336415756333MSSAP99SQLAgent - TSQL
6516870x0000309225756333MSSAP99SQLAgent - TSQL
6516870x0000308595756333MSSAP99SQLAgent - TSQL
6516870x0000284385756333MSSAP99SQLAgent - TSQL
6516870x0000307035756333MSSAP99SQLAgent - TSQL
6516870x0000287815756333MSSAP99SQLAgent - TSQL
6516870x0000287195756333MSSAP99SQLAgent - TSQL
6516870x0000293595756334MSSAP99SQLAgent - TSQL
6516870x0000308445756334MSSAP99SQLAgent - TSQL
6514670x0800746750212792823MSSAP32R3B14(3) unc rd
3513330x0800600630213123578MSSAP31R3B17(3) unc rd
515030x02086433006526323MSSAP32R3B20(3) unc rd
515030x040480786526323MSSAP32R3B20(3) unc rd
515030x042280316526323MSSAP32R3B20(3) unc rd
515030x040480006526323MSSAP32R3B20(3) unc rd
515030x040474536526323MSSAP32R3B20(3) unc rd
515030x040479066526323MSSAP32R3B20(3) unc rd
515030x040477506526323MSSAP32R3B20(3) unc rd
(26 row(s) affected)