<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblogcasts.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Martin Croft Musings on SQL Server : Traces, Blocking</title><link>http://sqlblogcasts.com/blogs/martincroft/archive/tags/Traces/Blocking/default.aspx</link><description>Tags: Traces, Blocking</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>SQL Server Top 10 Queries</title><link>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/04/sql-server-top-10-queries.aspx</link><pubDate>Wed, 04 May 2011 19:29:00 GMT</pubDate><guid isPermaLink="false">fa8c4e8e-46a3-4193-8264-2c1a9cb3475d:15591</guid><dc:creator>MartinCroft</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://sqlblogcasts.com/blogs/martincroft/rsscomments.aspx?PostID=15591</wfw:commentRss><comments>http://sqlblogcasts.com/blogs/martincroft/archive/2011/05/04/sql-server-top-10-queries.aspx#comments</comments><description>&lt;h4&gt;&lt;font style="font-weight:normal;" size="2"&gt;Day 4 and still rehashing some old post i needed to migrate from another site. In the garden burning wood and posting the latest blog when the bench collapsed around me .&lt;/font&gt;&lt;/h4&gt;  &lt;p&gt;&lt;img src="http://sqlblogcasts.com/blogs/martincroft/bench_thumb_05E9F03C.jpg" style="border:0px none;background-image:none;padding-left:0px;padding-right:0px;display:inline;padding-top:0px;" title="bench" alt="bench" border="0" height="239" width="357" /&gt;&lt;/p&gt;  &lt;p&gt;This is a list of the top 10 queries I use on a frequent basis that are relatively simple, either one liners or next to one liners. These are in reverse order from 10 through my favourite 1.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;10 Job Information      &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;You want to quickly access job information, you don’t want to have to trawl through joining MSDB.dbo.sysjobs , MSDB.sysjobsteps, just want a quick overview of what jobs are enabled/disabled or what job was updated yesterday? Or when it that job last run or next run &lt;/p&gt;  &lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;--Name,Enabled, description, owner, modeified date,etc&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;EXEC&lt;/span&gt; MSDB.dbo.sp_help_job&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;--Or specify Job_id and get step details, schedule times&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;EXEC&lt;/span&gt; MSDB.dbo.sp_help_job ’34DD4F82-423C-46E9-9E9A-BF7786′&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;9 Quick Search 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Trying to work out where a particular column or table is called in a procedure, you can check the dependency’s or another option is to just quicker check syscomments for the text you are after, there are several procedure out on the web that add a high degree of search ability but this is fast and easy&lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;USE&lt;/span&gt; NorthWind;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      OBJECT_NAME(iD),&lt;span&gt;text&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      syscomments&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHERE&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;      &lt;span&gt;Text&lt;/span&gt; &lt;span&gt;LIKE&lt;/span&gt; ‘%sales%’&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;8 Traces running 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;I have seen it before your running a trace, the trace hangs is it still running, how can it be you have closed the crashed profiler, always safer to check. or is big brother watching! one way to check! See BOL for the output to this function &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; * &lt;span&gt;FROM&lt;/span&gt; ::&lt;span&gt;fn_trace_getinfo&lt;/span&gt;(&lt;span&gt;default&lt;/span&gt;) &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;7 Am I sysadmin 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Maybe not useful for everyone, but I have had uses for this multiple times, especially with a variety of access accounts and SQL2005 /2008 ability to switch environments quickly (right click and change connection). I find it useful to know if the account I am logged in has Sysadmin as a usual theme with SQL there are a various ways of finding this out, here is one I use. &lt;/p&gt;

&lt;p&gt;–System Admin 1 yes god like powers 0 no I can’t drop that database &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; is_srvrolemember(‘sysadmin’) [Sysadmin] &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;6 System Uptime 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Is the system running like a dog? People start asking when was SQL rebooted, I.T’s magic wand lets reboot the server, when was SQL last started. There are several ways of finding out this information ( can use Top tip 5 as well!) but this is an easy approach. Basically see when SPID 1 logged in, also if you useDATEDIFF you can get SQL to tell you how many days, how many DBA’s can count I even use SELECT 10+20 to work out calculations, far too slow opening up calculator. &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;–Logintime &lt;span&gt;for&lt;/span&gt; SPID 1 &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; DATEDIFF(dd,login_time,Getdate())Uptime,Login_time &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;FROM&lt;/span&gt; master..sysprocesses &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;WHERE&lt;/span&gt; spid =1 &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;5 Errorlogs 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Sometimes useful when evaluating an issue, the SQL error logs can be access from the object explorer, but can be quicker and especially if it’s a long log it open far quicker this way. &lt;/p&gt;

&lt;p&gt;–Read Error log takes Integer value for the error log number &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;exec master..xp_readerrorlog &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Useful to find out a variety of information quickly, the log gets recycled when the server reboots, header shows were these logs are actually kept, version of SQL and a variety of message. If database are in recovery good place to look to get idea how long its going to take &lt;/p&gt;

&lt;p&gt;&lt;b&gt;4 Statistics 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Lifted directly from the pages of the SQL bible, or BOL as it known. Things are running pants trying to work out what has changed, how up to date are the statistics? This will tell you. &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;–STATS_DATE code from BOL &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;‘Index Name’= i.name,‘Statistics &lt;span&gt;Date&lt;/span&gt;’=STATS_DATE(i.id,i.indid) &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sysobjects o &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;JOIN &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sysindexes i &lt;span&gt;ON&lt;/span&gt; o.id = i.id &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;I tend to use order by 2 DESC added onto the end to order by the tables that were last updated.&lt;b&gt; 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;3 Disk Space 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Another one of those problem solving procedures. Used quite frequently on development system, as you really should have no excuses for production systems running out of space, unless it is the log drive and something untoward has occurred. One of the first procedures run when a developer says my database restore won’t work, 90Gb doesn’t fit on 45Gb free space funnily enough! &lt;/p&gt;

&lt;p&gt;–List disk information, useful for those users filling logs! &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;EXEC Master.dbo.xp_fixeddrives &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;2 Short Cut Keys 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Life savers when reviewing production incidents, how often you get “it’s not working” with little or no information, multiple systems that you’re unsure of the exact schema, so it is useful to know short cut keys. These are some of the ones I use daily. By assigning to short cuts you can specify, by highlighting SQL I can quickly pull up lots of useful info &lt;/p&gt;

&lt;p&gt;–Get the stored procedure text of system proc sp_who , just highlight sp_who &amp;amp; press Ctrl-F1 &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;USE &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;MASTER; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;GO &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sp_helptext &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;sp_who &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;– &lt;span&gt;Get&lt;/span&gt; infomation &lt;span&gt;on&lt;/span&gt; tables , Highlight Region &amp;amp; Press ALT +F1 &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;USE NorthWind &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;GO &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;Region &lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;1 Quick Blocking 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The piece of code that I probably use more than most, so simple but so helpful in times of crisis, which was basically many years ago ripped off from the system stoted procedure EXECsp_blockcnt, which basically just tells you the number of blocked processes on ther server. This querys can be written from memory with no need for fancy solutions on production boxes that your can’t role out code to. &lt;/p&gt;

&lt;p&gt;–Blocking processes &lt;/p&gt;

&lt;pre style="padding:5px;overflow:auto;min-height:40px;width:450px;"&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;span&gt;SELECT&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;* &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;FROM &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;MASTER.dbo.sysprocesses &lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;WHERE blocked &amp;lt;&amp;gt; 0&lt;br /&gt;&lt;/pre&gt;&lt;pre style="margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:12px;"&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;img src="http://sqlblogcasts.com/aggbug.aspx?PostID=15591" width="1" height="1"&gt;</description><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Sysadmin/default.aspx">Sysadmin</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Blocking/default.aspx">Blocking</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Stats/default.aspx">Stats</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Traces/default.aspx">Traces</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Tips/default.aspx">Tips</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Jobs/default.aspx">Jobs</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Uptime/default.aspx">Uptime</category><category domain="http://sqlblogcasts.com/blogs/martincroft/archive/tags/Shortcuy+Keys/default.aspx">Shortcuy Keys</category></item></channel></rss>