sqlskills-logo-2015-white.png

SQL Server Diagnostic Information Queries for February 2019

This month, I have just made some minor improvements to most of the query sets, mainly in the comments and documentation.

I have a T-SQL script that you can use to check whether your instance of SQL Server has been patched to mitigate against the Spectre/Meltdown CPU vulnerability. This works for SQL Server 2008 through SQL Server 2017, for on-premises and cloud-based VM (IaaS) usage. You can get the query for this here.

I often make additional minor updates to the queries periodically during the month, so if you are in doubt, downloading the latest version is always a good idea.

Rather than having a separate blog post for each version, I have just put the links for all ten major versions in this single post. There are two separate links for each version. The first one on the top left is the actual diagnostic query script, and the one below on the right is the matching blank results spreadsheet, with labeled tabs that correspond to each query in the set.

Here are links to the latest versions of these queries for Azure SQL Database, SQL Server 2019, SQL Server 2017, SQL Server 2016 SP2, SQL Server 2016, and SQL Server 2014:

Azure SQL Database Diagnostic Information Queries

Azure SQL Database Blank Results Spreadsheet

SQL Server 2019 Diagnostic Information Queries

SQL Server 2019 Blank Results Spreadsheet

SQL Server 2017 Diagnostic Information Queries

SQL Server 2017 Blank Results Spreadsheet

SQL Server 2016 SP2 Diagnostic Information Queries

SQL Server 2016 SP2 Blank Results Spreadsheet

SQL Server 2016 Diagnostic Information Queries

SQL Server 2016 Blank Results Spreadsheet

SQL Server 2014 Diagnostic Information Queries

SQL Server 2014 Blank Results Spreadsheet

Here are links to the most recent versions of these scripts for SQL Server 2012 and older:

Since SQL Server 2012 and older are out of Mainstream support from Microsoft (and because fewer of my customers are using these old versions of SQL Server), I am not going to be updating the scripts for these older versions of SQL Server every single month going forward.  I started this policy a while ago, and so far, I have not heard any complaints.

SQL Server 2012 Diagnostic Information Queries

SQL Server 2012 Blank Results Spreadsheet

SQL Server 2008 R2 Diagnostic Information Queries

SQL Server 2008 R2 Blank Results Spreadsheet

SQL Server 2008 Diagnostic Information Queries

SQL Server 2008 Blank Results Spreadsheet

SQL Server 2005 Diagnostic Information Queries

SQL Server 2005 Blank Results Spreadsheet

The basic instructions for using these queries is that you should run each query in the set, one at a time (after reading the directions for that query). It is not really a good idea to simply run the entire batch in one shot, especially the first time you run these queries on a particular server, since some of these queries can take some time to run, depending on your workload and hardware. I also think it is very helpful to run each query, look at the results (and my comments on how to interpret the results) and think about the emerging picture of what is happening on your server as you go through the complete set. I have quite a few comments and links in the script on how to interpret the results after each query.

After running each query, you need to click on the top left square of the results grid in SQL Server Management Studio (SSMS) to select all of the results, and then right-click and select “Copy with Headers” to copy all of the results, including the column headers to the Windows clipboard. Then you paste the results into the matching tab in the blank results spreadsheet.

About half of the queries are instance specific and about half are database specific, so you will want to make sure you are connected to a database that you are concerned about instead of the master system database. Running the database-specific queries while being connected to the master database is a very common mistake that I see people making when they run these queries.

Note: These queries are stored on Dropbox. I occasionally get reports that the links to the queries and blank results spreadsheets do not work, which is most likely because Dropbox is blocked wherever people are trying to connect. I am not planning on moving these to Github any time soon.

I also occasionally get reports that some of the queries simply don’t work. This usually turns out to be an issue where people have some of their user databases in 80 compatibility mode, which breaks many DMV queries, or that someone is running an incorrect version of the script for their version of SQL Server.

It is very important that you are running the correct version of the script that matches the major version of SQL Server that you are running. There is an initial query in each script that tries to confirm that you are using the correct version of the script for your version of SQL Server. If you are not using the correct version of these queries for your version of SQL Server, some of the queries are not going to work correctly.

If you want to understand how to better run and interpret these queries, you should consider listening to my five related Pluralsight courses, which are SQL Server 2017: Diagnosing Performance Issues with DMVs, SQL Server 2017: Diagnosing Configuration Issues with DMVs, SQL Server 2014 DMV Diagnostic Queries – Part 1SQL Server 2014 DMV Diagnostic Queries – Part 2, and SQL Server 2014 DMV Diagnostic Queries – Part 3. All five of these courses are pretty short and to the point, at 164, 106, 67, 77, and 68 minutes respectively. Listening to these five courses is really the best way to thank me for maintaining and improving these scripts…

Please let me know what you think of these queries, and whether you have any suggestions for improvements. Thanks!

5 thoughts on “SQL Server Diagnostic Information Queries for February 2019

  1. Hello,

    I’m wondering how CPU usage generated by query can be connected with sql server process CPU usage in system (CPU usage visible in Task Manager). For example I have database which CPU usage generated by query shows almost 80% but CPU usage of SQL Server process is about 17-20%. Any suggestions?

    Thanks

    1. WITH DB_CPU_Stats
      AS
      (SELECT pa.DatabaseID, DB_Name(pa.DatabaseID) AS [Database Name], SUM(qs.total_worker_time/1000) AS [CPU_Time_Ms]
      FROM sys.dm_exec_query_stats AS qs WITH (NOLOCK)
      CROSS APPLY (SELECT CONVERT(int, value) AS [DatabaseID]
      FROM sys.dm_exec_plan_attributes(qs.plan_handle)
      WHERE attribute = N’dbid’) AS pa
      GROUP BY DatabaseID)
      SELECT ROW_NUMBER() OVER(ORDER BY [CPU_Time_Ms] DESC) AS [CPU Rank],
      [Database Name], [CPU_Time_Ms] AS [CPU Time (ms)],
      CAST([CPU_Time_Ms] * 1.0 / SUM([CPU_Time_Ms]) OVER() * 100.0 AS DECIMAL(5, 2)) AS [CPU Percent]
      FROM DB_CPU_Stats
      WHERE DatabaseID 32767 — ResourceDB
      ORDER BY [CPU Rank] OPTION (RECOMPILE);

      on SQL Server 2012.

      Example query return:

      1 4122760 48.12
      2 698825 8.16
      3 366716 4.28
      4 225181 2.63
      5 189481 2.21
      6 168764 1.97
      7 165586 1.93
      8 162245 1.89
      9 160038 1.87
      10 125754 1.47
      11 117338 1.37
      12 112374 1.31
      13 101302 1.18
      14 77422 0.90
      15 75807 0.88
      16 69983 0.82
      17 68913 0.80
      18 62744 0.73
      19 60981 0.71
      20 58789 0.69
      21 52464 0.61
      22 43531 0.51
      23 42549 0.50
      24 41085 0.48
      25 39204 0.46
      26 37351 0.44
      27 36043 0.42
      28 31864 0.37
      29 31555 0.37
      30 31202 0.36
      31 29249 0.34
      32 28182 0.33
      33 27638 0.32
      34 26152 0.31
      35 25743 0.30
      36 25332 0.30
      37 25205 0.29
      38 25008 0.29
      39 23854 0.28
      40 23353 0.27
      41 22938 0.27
      42 22406 0.26
      43 22113 0.26
      44 21788 0.25
      45 20785 0.24
      46 20611 0.24
      47 20595 0.24
      48 19367 0.23
      49 18894 0.22
      50 18767 0.22
      51 18468 0.22
      52 16354 0.19
      53 15043 0.18
      54 14877 0.17
      55 14780 0.17
      56 13837 0.16
      57 13736 0.16
      58 12843 0.15
      59 12745 0.15
      60 12020 0.14
      61 11893 0.14
      62 11709 0.14
      63 11511 0.13
      64 11177 0.13
      65 11090 0.13
      66 10956 0.13
      67 10296 0.12
      68 10096 0.12
      69 9909 0.12
      70 9773 0.11
      71 9579 0.11
      72 9355 0.11
      73 8571 0.10
      74 8558 0.10
      75 8403 0.10
      76 8111 0.09
      77 7935 0.09
      78 7682 0.09
      79 7543 0.09
      80 7360 0.09
      81 7322 0.09
      82 7005 0.08
      83 6587 0.08
      84 6238 0.07
      85 6196 0.07
      86 5331 0.06
      87 5273 0.06
      88 5011 0.06
      89 4966 0.06
      90 4382 0.05
      91 4113 0.05
      92 3787 0.04
      93 3768 0.04
      94 3761 0.04
      95 3754 0.04
      96 3520 0.04
      97 3478 0.04
      98 3434 0.04
      99 3335 0.04
      100 3135 0.04
      101 3113 0.04
      102 2996 0.03
      103 2643 0.03
      104 2569 0.03
      105 2393 0.03
      106 2277 0.03
      107 2265 0.03
      108 2243 0.03
      109 2046 0.02
      110 1961 0.02
      111 1906 0.02
      112 1870 0.02
      113 1710 0.02
      114 1697 0.02
      115 1624 0.02
      116 1598 0.02
      117 1478 0.02
      118 1459 0.02
      119 1441 0.02
      120 1395 0.02
      121 1388 0.02
      122 1362 0.02
      123 1339 0.02
      124 1302 0.02
      125 1268 0.01
      126 1218 0.01
      127 1211 0.01
      128 1198 0.01
      129 1123 0.01
      130 1119 0.01
      131 1012 0.01
      132 997 0.01
      133 969 0.01
      134 960 0.01
      135 941 0.01
      136 887 0.01
      137 879 0.01
      138 839 0.01
      139 836 0.01
      140 759 0.01
      141 756 0.01
      142 730 0.01
      143 729 0.01
      144 695 0.01
      145 642 0.01
      146 621 0.01
      147 601 0.01
      148 600 0.01
      149 595 0.01
      150 578 0.01
      151 560 0.01
      152 528 0.01
      153 513 0.01
      154 477 0.01
      155 459 0.01
      156 458 0.01
      157 449 0.01
      158 446 0.01
      159 437 0.01
      160 436 0.01

      (without DB name) where sum of CPU usage column is about 100% and Windows task manager is showing about 30-50%. I’ve started wondering if somehow this 100% of query return is this 30% of current usage in system somehow (so 1% in CPU query is somehow 1/3 of % in task manager – 100% in query 30% in task manager)

Leave a Reply

Your email address will not be published. Required fields are marked *

Other articles

Imagine feeling confident enough to handle whatever your database throws at you.

With training and consulting from SQLskills, you’ll be able to solve big problems, elevate your team’s capacity, and take control of your data career.