Differences
This shows you the differences between two versions of the page.
— |
control_sql_-_log_database_record_locks_every_few_minutes [2019/01/27 11:29] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== | ||
+ | |||
+ | |||
+ | |||
+ | ===== Explanation of SQL ===== | ||
+ | |||
+ | |||
+ | |||
+ | These queries create a temporary table and then every X minutes (set below) it inserts into that table information about any locked records in the SQL Database. | ||
+ | |||
+ | |||
+ | |||
+ | ===== Risk of Data Corruption if Run Improperly ===== | ||
+ | |||
+ | **Small**. | ||
+ | |||
+ | |||
+ | |||
+ | ===== Step 1 - Create Temp Table ===== | ||
+ | |||
+ | |||
+ | |||
+ | Run this SQL one time to create the temp table. | ||
+ | |||
+ | |||
+ | |||
+ | <code sql> | ||
+ | CREATE TABLE [dbo].[Temp_BlockingItems]( | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | [ID] [bigint] IDENTITY(1, | ||
+ | CONSTRAINT [PK_Temp_BlockingItems] PRIMARY KEY CLUSTERED | ||
+ | ( | ||
+ | [ID] ASC | ||
+ | ) | ||
+ | ) | ||
+ | ; | ||
+ | CREATE NONCLUSTERED INDEX [IX_Temp_BlockingItems_DT] ON [dbo].[Temp_BlockingItems] | ||
+ | ( | ||
+ | | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Step 2 - Create Temp Table ===== | ||
+ | |||
+ | |||
+ | |||
+ | Run this SQL to create the log entries. | ||
+ | |||
+ | |||
+ | |||
+ | Notes: | ||
+ | * This will run every 3 minutes for 6 hours. | ||
+ | * You must set the time-out in SQL server to longer than the run time. (This is under Tools | Options | Designers | Transaction Time-Out.) | ||
+ | * SQL must be set to compatibility mode of 2005 or greater or your will get a syntax error in the query. | ||
+ | |||
+ | |||
+ | |||
+ | <code sql> | ||
+ | INSERT INTO Temp_BlockingItems | ||
+ | SELECT | ||
+ | | ||
+ | , b.[session_id] AS [WaitingSessionID] | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | ,b.[text] AS [WaitingCommandText] | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | FROM | ||
+ | ( | ||
+ | SELECT | ||
+ | w.[session_id] | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | , | ||
+ | ,q.[text] | ||
+ | ,q.[dbid] | ||
+ | , | ||
+ | , | ||
+ | FROM [sys].[dm_os_waiting_tasks] w | ||
+ | INNER JOIN [sys].[dm_exec_sessions] s ON w.[session_id] = s.[session_id] | ||
+ | INNER JOIN [sys].[dm_exec_requests] r ON s.[session_id] = r.[session_id] | ||
+ | CROSS APPLY [sys].[dm_exec_sql_text](r.[plan_handle]) q | ||
+ | CROSS APPLY [sys].[dm_exec_query_plan](r.[plan_handle]) p | ||
+ | WHERE w.[session_id] > 50 | ||
+ | --AND w.[wait_type] NOT IN (' | ||
+ | -- ,' | ||
+ | ) b | ||
+ | INNER JOIN [sys].[dm_exec_sessions] s1 | ||
+ | ON b.[blocking_session_id] = s1.[session_id] | ||
+ | INNER JOIN [sys].[dm_tran_locks] t | ||
+ | ON t.[request_session_id] = b.[session_id] | ||
+ | WHERE t.[request_status] = ' | ||
+ | ; | ||
+ | PRINT ' | ||
+ | ; | ||
+ | WAITFOR DELAY ' | ||
+ | ; | ||
+ | Go 120 -- Run repeatedly 120 times (= for 6 hours) | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Step 3 - View the Results ===== | ||
+ | |||
+ | |||
+ | |||
+ | You can run this repeatedly even as #2 is running to see the results. | ||
+ | |||
+ | |||
+ | |||
+ | <code sql> | ||
+ | SELECT * FROM [dbo].[Temp_BlockingItems] | ||
+ | ORDER BY DTRun Desc | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Version Information ===== | ||
+ | * Entered : __/__/2009 | ||
+ | * Version : | ||
+ | |||
+ | |||
+ | |||