Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Looking for suggestions on monitoring blocking

Author  Topic 

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-08-26 : 18:16:54
Simple question, probably a more detailed answer :).

I'm looking for suggestions on how to monitor locking and having the ability to later analyze the details of the locking to determine the root cause.

My situation is this, we have some processes that run all the time. It seems lately that for some reason these processes run into all sorts of locks and end up timing out. Note, I have not witnesses any deadlocking - yet.

As far as monitoring, is it possible to setup an alert when blocking happens? If so I can easily pull together some queries to analyze the blocking when it happens. Or would I be better off using some third party solution (i.e. Idera?) to analyze this.

I would prefer an option where I can analyze there results later as I am not always available to look into these blocks when they happen (for example, at 3 AM).

Other suggestions are welcome to!

Thanks!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-08-26 : 18:47:19
You will have to get information from Master..Sys.Processes and schedule it as job with notification to you.
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-08-27 : 09:10:38
Any other suggestions?
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-08-27 : 10:09:09
I had a script to check blocking/locking and send mail if it finds one. Don't seem to find one right now.
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-08-27 : 14:20:38
Honestly, I can write a script to do this if necessary. I was really looking for more enterprise level options if any are available.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-27 : 14:25:23
Here is what Microsoft has asked that we run when blocking exists, so that they can help us with our current production issue:


-- Check for Blocking
-- lists the blocker and waiter sql statements

select getdate() as 'logtime',
es.session_id, es.last_request_start_time, es.last_request_end_time,
qr.status as 'Request Status',
es.status as 'Session Status', qr.blocking_session_id,
case qr.transaction_isolation_level
when 0 then 'Unspecified'
when 1 then 'ReadUncommitted'
when 2 then 'ReadCommitted'
when 3 then 'Repeatable'
when 4 then 'Serializable'
when 5 then 'Snapshot'
end as 'Request Isolation Level',
case es.transaction_isolation_level
when 0 then 'Unspecified'
when 1 then 'ReadUncommitted'
when 2 then 'ReadCommitted'
when 3 then 'Repeatable'
when 4 then 'Serializable'
when 5 then 'Snapshot'
end as 'Session Isolation Level',
qr.wait_type, qr.wait_time, qr.wait_resource,
qr.open_transaction_count, qr.transaction_id, qtran.transaction_begin_time,
case qtran.transaction_type
when 1 then 'Read/Write'
when 2 then 'Read-only'
when 3 then 'System'
when 4 then 'Distributed'
end as 'Transaction Type',
qr.cpu_time, qr.total_elapsed_time, qr.reads, qr.writes,
qr.logical_reads,
SUBSTRING(qt.text,qr.statement_start_offset/2,
(case when qr.statement_end_offset = -1
then len(convert(nvarchar(max), qt.text)) * 2
else qr.statement_end_offset end -qr.statement_start_offset)/2)
as request_query_text,
(select text from sys.dm_exec_connections as p --- get sql for blocker
cross apply sys.dm_exec_sql_text(p.most_recent_sql_handle) where p.session_id = qr.blocking_session_id) as blocker_query_text
from sys.dm_exec_sessions es
left outer join sys.dm_exec_requests qr on es.session_id = qr.session_id
outer apply sys.dm_exec_sql_text(qr.sql_handle) as qt
left outer join sys.dm_tran_active_transactions qtran on qr.transaction_id = qtran.transaction_id
where
es.is_user_process = 1
order by qr.total_elapsed_time desc



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-08-27 : 14:38:18
Here is the script: although Using DMV is great.

--Put this code in a job and run it every 5 minute.
--Have to use a global temp table (##) because of sendmail. # does not work.

declare @spid int, @counter int
create table ##temp_blocked_spids
(id int identity (1,1) primary key, spid int, blocked int,
hostname nchar(256), program_name nchar(256), loginame nchar(256))
create table ##ib_info
(id int identity (1,1) primary key, spid int, eventtype varchar(1000),
parameters varchar(1000), inputbuffer varchar(2000))
insert into ##temp_blocked_spids (spid, blocked, hostname, program_name, loginame)
select spid, blocked, rtrim(hostname), rtrim(program_name), rtrim(loginame)
from sysprocesses
where (blocked != 0 and spid !=blocked) or (spid in (select distinct blocked from sysprocesses where blocked !=0 and spid !=blocked))
group by spid, blocked, hostname, program_name, loginame
order by spid
set nocount on
declare ibuffer cursor fast_forward for
select spid from ##temp_blocked_spids
open ibuffer
fetch next from ibuffer into @spid
while (@@fetch_status != -1)
begin
insert into ##ib_info (eventtype, parameters, inputbuffer)
exec ('dbcc inputbuffer (' + @spid + ')')
fetch next from ibuffer into @spid
end
deallocate ibuffer
set nocount off
-- If there are entries in the table, send an email.
if (select count(*) from ##temp_blocked_spids) > 0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Profilename',
@recipients = 'email',
@query = 'select left(s.spid,4) as spid, left(s.blocked,4) as blocked, left(d.inputbuffer,52) as inputbuffer,
left(s.program_name,10) as program_name, left(s.loginame,10) as login_name,left(s.hostname,15) as host_name
from ##temp_blocked_spids s join ##ib_info d on (s.id = d.id)',
@body = 'The following spids are involved in blocking',
@subject = 'subject';
end
-- Drop the temporary tables.
drop table ##temp_blocked_spids
drop table ##ib_info

Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2008-08-27 : 15:19:49
Tara,

Excellent query, saves me a ton of time researching the specific DMVs :). I've added this to my toolset!
Go to Top of Page
   

- Advertisement -