CREATE TABLE [dbo].[PageLockWait](
id int identity primary key,
[table_name] nvarchar NULL,
[index_name] [sysname] NULL,
[page_lock_count] [bigint] NOT NULL,
[page_lock_wait_count] [bigint] NOT NULL,
[page_block_pct] [decimal](6, 2) NULL,
[page_lock_wait_in_ms] [bigint] NOT NULL,
[page_avg_lock_wait_ms] [decimal](12, 2) NULL,
captureTime datetime2 default getdate()
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[RowLockWait](
id int identity primary key,
[table_name] nvarchar NULL,
[index_name] [sysname] NULL,
[row_lock_count] [bigint] NOT NULL,
[row_lock_wait_count] [bigint] NOT NULL,
[row_block_pct] [decimal](6, 2) NULL,
[row_lock_wait_in_ms] [bigint] NOT NULL,
[row_avg_lock_wait_ms] [decimal](12, 2) NULL,
captureTime datetime2 default getdate()
) ON [PRIMARY]
GO
while(1=1)
begin
insert hao.dbo.RowLockWait(table_name,index_name,row_lock_count,row_lock_wait_count,row_block_pct,row_lock_wait_in_ms,row_avg_lock_wait_ms)
SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name
,i.name as index_name
,row_lock_count
,row_lock_wait_count
,CAST(100. * row_lock_wait_count / NULLIF(row_lock_count,0) AS decimal(6,2)) AS row_block_pct
,row_lock_wait_in_ms
,CAST(1. * row_lock_wait_in_ms / NULLIF(row_lock_wait_count,0) AS decimal(12,2)) AS row_avg_lock_wait_ms
FROM sys.dm_db_index_operational_stats (DB_ID(), NULL, NULL, NULL) ios
INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id
WHERE OBJECTPROPERTY(ios.object_id,'IsUserTable') = 1 and ios.row_lock_wait_count>0
--ORDER BY row_lock_wait_count + page_lock_wait_count DESC, row_lock_count + page_lock_count DESC
insert hao.dbo.PageLockWait(table_name,index_name,page_lock_count,page_lock_wait_count,page_block_pct,page_lock_wait_in_ms,page_avg_lock_wait_ms)
SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name
,i.name as index_name
,page_lock_count
,page_lock_wait_count
,CAST(100. * page_lock_wait_count / NULLIF(page_lock_count,0) AS decimal(6,2)) AS page_block_pct
,page_lock_wait_in_ms
,CAST(1. * page_lock_wait_in_ms / NULLIF(page_lock_wait_count,0) AS decimal(12,2)) AS page_avg_lock_wait_ms
FROM sys.dm_db_index_operational_stats (DB_ID(), NULL, NULL, NULL) ios
INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id
WHERE OBJECTPROPERTY(ios.object_id,'IsUserTable') = 1 AND ios.page_lock_wait_count>0
--ORDER BY row_lock_wait_count + page_lock_wait_count DESC, row_lock_count + page_lock_count DESC
WAITFOR DELAY '00:00:30'
end