RetroCoder
RetroCoder

Reputation: 2685

Is sp_who2 for the entire server and why is it not detecting all high priority processes?

I'm looking at sp_who2 and it appears as though all processes are in a sleep mode and our website is extremely slow. I was wandering if sp_who2 looks at only the current database, or is it looking at another database can be causing the problem and sp_who2 only looks at the current database.

Also is there a way to detect if a sql process is running at a higher priority and blocking other processes. When I ran sp_who2 we had several processes that are normally in a runnable state, but they were all in a sleeping state and I was trying to figure out what was blocking them. It would nice to detect if a process was blocking other sql processes out.

There is also a BlkBy column where we set to the same process ID of 77 which I think means block all other processes but am not sure. Does anyone know what BlkBy means?

I placed asterics ** ** around what I think is the culprit. Is there a way to change priority or blocking so it no longer blocks. thx

SPID    Status                          Login       HostName        BlkBy   DBName  Command CPUTime DiskIO  LastBatch   ProgramName SPID
76      sleeping                        db\uname    UNAME             .     db  AWAITING COMMAND    0   0   02/24 09:15:54  srv / db                                        76   
77      sleeping                        db\uname    UNAME             .     db  SELECT              705422  0   02/24 09:25:49  srv / db                                        77   
77      sleeping                        db\uname    UNAME             .     db  SELECT              16  0   02/24 09:25:49  srv / db                                        77   
77      sleeping                        db\uname    UNAME           **77**      db  SELECT              32  0   02/24 09:25:49  srv / db                                        77   
77      sleeping                        db\uname    UNAME           **77**      db  SELECT              31  0   02/24 09:25:49  srv / db                                        77   
77      sleeping                        db\uname    UNAME             .     db  SELECT              16  0   02/24 09:25:49  srv / db                                        77   

Thanks.

Upvotes: 2

Views: 2972

Answers (1)

Oleg Dok
Oleg Dok

Reputation: 21766

sp_who2 showl the entire server's connections if you not specified spid explicitly

For locks see

exec sp_locks

To see in details who blocks who:

set nocount on

create table #t_sql (EventType text, Parameters int,EventInfo text, spid smallint default(0))
declare @qs_blocking_list table (
    spid int,
    blocked int,
    loginame sysname,
    nt_username sysname,
    lastwaittype sysname,
    waitresource sysname,
    status sysname,
    waittime bigint,
    program_name sysname,
    cmd sysname,
    cpu bigint,
    physical_io bigint,
    hostname sysname,
    dbid int
)   
    insert into @qs_blocking_list (
        spid ,
        blocked ,
        loginame, 
        nt_username ,
        lastwaittype ,
        waitresource ,
        status ,
        waittime ,
        program_name ,
        cmd ,
        cpu ,
        physical_io ,
        hostname ,
        dbid 
    )
    select  
        spid ,
        blocked ,
        loginame, 
        nt_username ,
        lastwaittype ,
        waitresource ,
        status ,
        waittime ,
        program_name ,
        cmd ,
        cpu ,
        physical_io ,
        hostname ,
        dbid 
    from master.dbo.sysprocesses
delete from @qs_blocking_list
where not ( blocked <> 0 or (spid in (select blocked from @qs_blocking_list)) )
declare @spid   smallint
declare @sql    nvarchar(255)
set @spid = 0
while 1=1
begin
    select @spid = min(spid) from @qs_blocking_list where @spid<spid
    if @spid is null
        break 
    set @sql = 'dbcc inputbuffer ('+convert(nvarchar(20),@spid)+')'
    insert into #t_sql (EventType, Parameters, EventInfo)
    exec (@sql)
    update #t_sql set spid = @spid where spid = 0
end
set nocount off
select  b.spid              as 'spid'
    ,   blocked             as 'BlockedBySPID'
    ,   rtrim(loginame)     as 'SQLUser'
    ,   rtrim(nt_username)  as 'NTUser'
    ,   rtrim(lastwaittype) as 'Type'
    ,   rtrim(waitresource) as 'Resource'
    ,   rtrim(status) + 
            case when blocked > 0 then ' and blocked' else '' end +
            case when b.spid in (select blocked from @qs_blocking_list) then ' and blocking' else '' end as 'Status'
    ,   waittime            as 'WaitTimeMS'
    ,   rtrim(program_name) as 'Program'
    ,   rtrim(cmd)          as 'Command'
    ,   cpu                 as 'CPU'
    ,   physical_io         as 'PhysicalIO'
    ,   rtrim(hostname)     as 'HostName'
    ,   case
            when dbid = 0 then ''
        else
            db_name(dbid)       
        end                 as 'DBName'
    ,   isnull(sql.EventInfo,'/* SQL Text not available */')    as 'sql_text'
from @qs_blocking_list b
left outer join #t_sql sql on b.spid = sql.spid

drop table #t_sql

Upvotes: 1

Related Questions