在我们的系统出现性能问题时,往往避不开调查各种类型 Lock Wait,如Row Lock Wait、Page Lock Wait、Page IO Latch Wait等。从中找出可能的异常等待,为性能优化做一定的参考 。具体的查询语句分享如下,
/*******************************************************************************************
Row Lock Wait
*******************************************************************************************/
SELECT ‘[‘ + DB_NAME(ddios.[database_id]) + ‘].[‘ + su.[name] + ‘].[‘
+ o.[name] + ‘]‘ AS [statement] ,
i.[name] AS ‘index_name‘ ,
ddios.[partition_number] ,
ddios.[row_lock_count] ,
ddios.[row_lock_wait_count] ,
CAST (100.0 * ddios.[row_lock_wait_count]
/ ( ddios.[row_lock_count] ) AS DECIMAL(5, 2)) AS [%_times_blocked] ,
ddios.[row_lock_wait_in_ms] ,
CAST (1.0 * ddios.[row_lock_wait_in_ms]
/ ddios.[row_lock_wait_count] AS DECIMAL(15, 2))
AS [avg_row_lock_wait_in_ms]
FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) ddios
INNER JOIN sys.indexes i ON ddios.[object_id] = i.[object_id]
AND i.[index_id] = ddios.[index_id]
INNER JOIN sys.objects o ON ddios.[object_id] = o.[object_id]
INNER JOIN sys.sysusers su ON o.[schema_id] = su.[UID]
WHERE ddios.row_lock_wait_count > 0
AND OBJECTPROPERTY(ddios.[object_id], ‘IsUserTable‘) = 1 and OBJECT_NAME(ddios.[object_id]) like ‘POS_TRANSMST‘
AND i.[index_id] > 0
ORDER BY ddios.[row_lock_wait_count] DESC ,
su.[name] ,
o.[name] ,
i.[name]
/*******************************************************************************************
--Page Lock Wait
*******************************************************************************************/
SELECT OBJECT_NAME(ddios.object_id, ddios.database_id) AS object_name ,
i.name AS index_name ,
ddios.object_id,
ddios.partition_number ,
ddios.page_lock_wait_count ,
ddios.page_lock_wait_in_ms ,
CASE WHEN DDMID.database_id IS NULL THEN ‘N‘
ELSE ‘Y‘
END AS missing_index_identified
FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) ddios
INNER JOIN sys.indexes i ON ddios.object_id = i.object_id
AND ddios.index_id = i.index_id
LEFT OUTER JOIN ( SELECT DISTINCT
database_id ,
object_id
FROM sys.dm_db_missing_index_details
) AS DDMID ON DDMID.database_id = ddios.database_id
AND DDMID.object_id = ddios.object_id
WHERE ddios.page_lock_wait_in_ms > 0 and OBJECT_NAME(ddios.[object_id]) like ‘POS_TRANSMST‘
ORDER BY ddios.page_lock_wait_count DESC ;
/*******************************************************************************************
--Page IO Latch Wait
*******************************************************************************************/
SELECT ‘[‘ + DB_NAME() + ‘].[‘ + OBJECT_SCHEMA_NAME(ddios.[object_id])
+ ‘].[‘ + OBJECT_NAME(ddios.[object_id]) + ‘]‘ AS [object_name] ,
i.[name] AS index_name ,
ddios.page_io_latch_wait_count ,
ddios.page_io_latch_wait_in_ms ,
( ddios.page_io_latch_wait_in_ms / ddios.page_io_latch_wait_count )
AS avg_page_io_latch_wait_in_ms
FROM sys.dm_db_index_operational_stats(DB_ID(), NULL, NULL, NULL) ddios
INNER JOIN sys.indexes i ON ddios.[object_id] = i.[object_id]
AND i.index_id = ddios.index_id
WHERE ddios.page_io_latch_wait_count > 0 and OBJECT_NAME(ddios.[object_id]) like ‘POS_TRANSMST‘
AND OBJECTPROPERTY(i.object_id, ‘IsUserTable‘) = 1
ORDER BY ddios.page_io_latch_wait_count DESC ,
avg_page_io_latch_wait_in_ms DESC
/*******************************************************************************************
SameZhao