{"id":167,"date":"2011-12-29T23:53:46","date_gmt":"2011-12-30T05:53:46","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=167"},"modified":"2015-12-28T16:33:32","modified_gmt":"2015-12-28T22:33:32","slug":"query-plans-and-other-connections-information-on-active-queries","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=167","title":{"rendered":"Query Plans and active query information"},"content":{"rendered":"<p>Quick script to see active processes and to grab the query plan. It leverages some DMVs to get the query text and plan. Only can be used on SQL 2005 and above. It is a great and light wight way to see what is going on with the server and performance tuning.<\/p>\n<p><!--more--><br \/>\n<code><br \/>\n--jackdonnell.com <\/code><\/p>\n<p>USE MASTER<br \/>\nGO<br \/>\nSELECT @@SERVERNAME [Server]<br \/>\n, r.session_id [SPID]<br \/>\n,d.name [DB NAME]<br \/>\n,s.host_name [HOST NAME]<br \/>\n,s.login_name [LOGIN]<br \/>\n,r.status [STATUS]<br \/>\n,r.command [COMMAND]<br \/>\n,(select text from sys.dm_exec_sql_text(r.sql_handle)) as [SQL TEXT]<br \/>\n,(SELECT CAST (a.query_plan as XML) from sys.dm_exec_query_plan (r.plan_handle) as a) as [QUERY PLAN]<br \/>\n,r.cpu_time [CPU TIME]<br \/>\n,r.logical_reads [LOGICAL READS]<br \/>\n,s.reads<br \/>\n,s.writes<br \/>\n,sp.[ThreadCount]<br \/>\n,s.login_time [LOGIN TIME]<br \/>\n,s.last_request_start_time [LAST REQUEST START]<br \/>\n,last_request_end_time [LAST REQUEST END]<br \/>\n,r.wait_type [WAIT TYPE]<br \/>\n,r.last_wait_type[LASTWAIT TYPE]<br \/>\n,s.program_name[APPLICATION]<br \/>\n,s.client_interface_name<br \/>\n,r.plan_handle as [PLAN HANDLE]<br \/>\n,r.sql_handle as [SQL HANDLE]<br \/>\n,GetDate() [SAMPLE TIME]<br \/>\nFROM sys.dm_exec_sessions as s<br \/>\ninner join sys.dm_exec_requests as r<br \/>\non s.session_id =r.session_id<br \/>\ninner join sys.databases as d<br \/>\non d.database_id=r.database_id<br \/>\ninner join (Select spid , count(spid)[ThreadCount]from sysprocesses group by spid)as sp<br \/>\non sp.spid=r.session_id<br \/>\nwhere is_user_process = 1<br \/>\nGO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick script to see active processes and to grab the query plan. It leverages some DMVs to get the query text and plan. Only can be used on SQL 2005 and above. It is a great and light wight way &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=167\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[273,266,22,5],"tags":[267,263,315,286,249,35,36,295,310,314,291],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-administration","category-dba","category-sql-server","category-t-sql","tag-admin","tag-dmv","tag-query","tag-query-plans","tag-script","tag-select","tag-sql","tag-sql-server","tag-sys-dm_exec_query_plan","tag-sys-dm_exec_sql_text","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":4,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}