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.

129<br />

Chapter 4: Transactions<br />

If, from a second tab in SSMS, we now run a second query, shown in Listing 4.4 (having<br />

neither committed nor rolled back the query in Listing 4.2), we'll introduce some blocking<br />

in the AdventureWorks database.<br />

SELECT *<br />

FROM [Production].[ProductCategory] ;<br />

Listing 4.4: A simple query against the ProductCategory table, which will be blocked.<br />

We'll present a more detailed script for detecting blocking in the next section, but the one<br />

in Listing 4.5 demonstrates the basic pattern for joining to the execution-related DMOs<br />

to find out which sessions are involved in blocking, which login "owns" these sessions,<br />

and what <strong>SQL</strong> statements they are executing.<br />

SELECT DTL.[request_session_id] AS [session_id] ,<br />

DB_NAME(DTL.[resource_database_id]) AS [Database] ,<br />

DTL.resource_type ,<br />

CASE WHEN DTL.resource_type IN ( 'DATABASE', 'FILE', 'METADATA' )<br />

THEN DTL.resource_type<br />

WHEN DTL.resource_type = 'OBJECT'<br />

THEN OBJECT_NAME(DTL.resource_associated_entity_id,<br />

DTL.[resource_database_id])<br />

WHEN DTL.resource_type IN ( 'KEY', 'PAGE', 'RID' )<br />

THEN ( SELECT OBJECT_NAME([object_id])<br />

FROM sys.partitions<br />

WHERE sys.partitions.hobt_id =<br />

DTL.resource_associated_entity_id<br />

)<br />

ELSE 'Unidentified'<br />

END AS [Parent Object] ,<br />

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

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

DER.[blocking_session_id] ,<br />

DES.[login_name] ,<br />

CASE DTL.request_lifetime<br />

WHEN 0 THEN DEST_R.TEXT<br />

ELSE DEST_C.TEXT

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

Saved successfully!

Ooh no, something went wrong!