SQL Swiss Army Knife #11.1 - Locking, blocking and active transactions
Published Mar 23 2019 12:54 PM 989 Views
Microsoft
First published on MSDN on Sep 18, 2012
Latest update: 12/2/2016 (Check change log )

Download on GitHub



Hello all,
Following the series "SQL Swiss Army Knife", here is another version of this script that I’ve been using and tweaking for years now. This way I can get a quick overview of query execution on a given SQL Server instance, from SQL Server 2005 up to SQL Server 2016.

The script leverages on some known DMVs ( sys.dm_exec_requests , sys.dm_exec_sessions , sys.dm_os_tasks , sys.dm_exec_query_plan , sys.dm_db_session_space_usage , sys.dm_resource_governor_workload_groups , sys.dm_db_task_space_usage and sys.dm_tran_locks ) and DMFs ( sys.dm_exec_sql_text , sys.dm_exec_query_plan and sys.dm_exec_query_statistics_xml ) to report on all running requests, waiter and blocking and SP execution statistics.

The first section will report on all requests. In here there is info on running batches, the statement in the batch that is currently running and its status. Also, there is information about the cached query plan, current I/O, CPU and time stats, the current wait type, resource (if any) and other useful data.

The output will look something like below, and you can get further detail on statements and plans by clicking on the XML links:





The second section is the Waiter and Blocking Report, where we find the queries that are blocked, such as the spid, its wait type, resource and time. Still on the blocked query, the associated batch, blocking elapsed time and database name. On the blockers, there is information on the spid, if it is the head blocker in a given blocking chain and what is the blocker batch. Additional information on the involved hosts and programs is also available.

As before, you can get further detail by clicking on the XML links:





And last, starting with SQL Server 2012, you can get some details on Stored Procedures execution statistics from the sys.dm_exec_procedure_stats DMV, like below:





That’s it for now, hope you find it useful.

Until next time!



Change log:

Version



Date



Comments



1



18-09-2012



Initial release.



1.1



12-04-2013



Added page type information (PFS; GAM or SGAM) when wait type is PAGELATCH_ or PAGEIOLATCH_.



1.2



23-05-2013



Fixed parse page issue.



1.3



17-10-2013



Added memory grant info, statements to blocking and blocked sections of blocking report, and fixed head blocker info.



1.4



09-12-2013



Fixed blocking section showing non-blocked sessions.



1.5



09-04-2014



Added information to blocking section, and fixed conversion issues.



1.6



09-04-2014



Handle illegal characters in XML conversion.



1.7



11/17/2016



support for SQL Server 2016 SP1 and live query plan snapshot.



1.8



12/2/2016



Fixed transport-level error issue with SQL Server 2016 SP1.



Disclaimer: I hope that the information on these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Ezequiel. Further, Ezequiel shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
1 Comment
Version history
Last update:
‎Mar 23 2019 12:54 PM
Updated by: