30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4: Transactions<br />

)<br />

ELSE 'Unidentified'<br />

END AS [Parent Object] ,<br />

DTL.[request_mode] AS [Lock Type] ,<br />

DTL.[request_status] AS [Request Status] ,<br />

DOWT.[wait_duration_ms] AS [wait duration ms] ,<br />

DOWT.[wait_type] AS [wait type] ,<br />

DOWT.[session_id] AS [blocked session id] ,<br />

DES_blocked.[login_name] AS [blocked_user] ,<br />

SUBSTRING(dest_blocked.text, der.statement_start_offset / 2,<br />

( CASE WHEN der.statement_end_offset = -1<br />

THEN DATALENGTH(dest_blocked.text)<br />

ELSE der.statement_end_offset<br />

END - der.statement_start_offset ) / 2<br />

AS [blocked_command] ,<br />

DOWT.[blocking_session_id] AS [blocking session id] ,<br />

DES_blocking.[login_name] AS [blocking user] ,<br />

DEST_blocking.[text] AS [blocking command] ,<br />

DOWT.resource_description AS [blocking resource detail]<br />

FROM sys.dm_tran_locks DTL<br />

INNER JOIN sys.dm_os_waiting_tasks DOWT<br />

ON DTL.lock_owner_address = DOWT.resource_address<br />

INNER JOIN sys.[dm_exec_requests] DER<br />

ON DOWT.[session_id] = DER.[session_id]<br />

INNER JOIN sys.dm_exec_sessions DES_blocked<br />

ON DOWT.[session_id] = DES_Blocked.[session_id]<br />

INNER JOIN sys.dm_exec_sessions DES_blocking<br />

ON DOWT.[blocking_session_id] = DES_Blocking.[session_id]<br />

INNER JOIN sys.dm_exec_connections DEC<br />

ON DTL.[request_session_id] = DEC.[most_recent_session_id]<br />

CROSS APPLY sys.dm_exec_sql_text(DEC.[most_recent_sql_handle])<br />

AS DEST_Blocking<br />

CROSS APPLY sys.dm_exec_sql_text(DER.sql_handle) AS DEST_Blocked<br />

WHERE DTL.[resource_database_id] = DB_ID()<br />

Listing 4.6: Investigating locking and blocking based on waiting tasks.<br />

To see it in action, we'll set up some activity on the Production.Culture table of the<br />

AdventureWorks database. It's a narrow table <strong>with</strong> three columns and two indexes, one<br />

clustered index on the primary key and one unique non-clustered index on the Name<br />

134

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!