functions/Get-DbaTopResourceUsage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
function Get-DbaTopResourceUsage {
    <#
 .SYNOPSIS
  Returns the top 20 resource consumers for cached queries based on four different metrics: duration, frequency, IO, and CPU.
 
 .DESCRIPTION
  Returns the top 20 resource consumers for cached queries based on four different metrics: duration, frequency, IO, and CPU.
  
  This command is based off of queries provided by Michael J. Swart at http://michaeljswart.com/go/Top20
  
  Per Michael: "I've posted queries like this before, and others have written many other versions of this query. All these queries are based on sys.dm_exec_query_stats."
  
 .PARAMETER SqlInstance
  Allows you to specify a comma separated list of servers to query.
 
 .PARAMETER SqlCredential
  Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
  $cred = Get-Credential, this pass this $cred to the param.
 
  Windows Authentication will be used if DestinationSqlCredential is not specified. To connect as a different Windows user, run PowerShell as that user.    
 
 .PARAMETER Database
  The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed.
  
 .PARAMETER ExcludeDatabase
  The database(s) to exclude - this list is auto-populated from the server
  
 .PARAMETER Type
  By default, all Types run but you can specify one or more of the following: Duration, Frequency, IO, or CPU
 
 .PARAMETER Limit
  By default, these query the Top 20 worst offenders (though more than 20 results can be returend if each of the top 20 have more than 1 subsequent result)
  
 .PARAMETER Silent
  Use this switch to disable any kind of verbose messages (this is required)
 
 .NOTES
  Tags: Query, Performance
  Website: https://dbatools.io
  Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
  License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
 .LINK
  https://dbatools.io/Get-DbaTopResourceUsage
 
 .EXAMPLE
  Get-DbaTopResourceUsage -SqlInstance sql2008, sql2012
  Return the 80 (20 x 4 types) top usage results by duration, frequency, IO, and CPU servers for servers sql2008 and sql2012
 
 .EXAMPLE
  Get-DbaTopResourceUsage -SqlInstance sql2008 -Type Duration, Frequency -Database TestDB
  Return the highest usage by duration (top 20) and frequency (top 20) for the TestDB on sql2008
 
 .EXAMPLE
  Get-DbaTopResourceUsage -SqlInstance sql2016 -Limit 30
  Return the highest usage by duration (top 30) and frequency (top 30) for the TestDB on sql2016
 
 .EXAMPLE
  Get-DbaTopResourceUsage -SqlInstance sql2016| Select *
  Return all the columns plus the QueryPlan column
 #>

    [CmdletBinding()]
    param (
        [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $True)]
        [Alias("ServerInstance", "SqlServer", "SqlServers")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [Alias("Databases")]
        [object[]]$Database,
        [object[]]$ExcludeDatabase,
        [ValidateSet("All", "Duration", "Frequency", "IO", "CPU")]
        [string[]]$Type = "All",
        [int]$Limit = 20,
        [switch]$Silent
    )
    
    begin {
        
        $instancecolumns = " SERVERPROPERTY('MachineName') AS ComputerName,
        ISNULL(SERVERPROPERTY('InstanceName'), 'MSSQLSERVER') AS InstanceName,
        SERVERPROPERTY('ServerName') AS SqlInstance, "

        
        if ($database) {
            $wheredb = " and coalesce(db_name(st.dbid), db_name(cast(pa.value AS INT)), 'Resource') in ('$($database -join '', '')')"
        }
        
        if ($ExcludeDatabase) {
            $wherenotdb = " and coalesce(db_name(st.dbid), db_name(cast(pa.value AS INT)), 'Resource') notin '$($excludedatabase -join '', '')'"
        }
        
        $duration = ";with long_queries as
      (
          select top $Limit
              query_hash,
              sum(total_elapsed_time) elapsed_time
          from sys.dm_exec_query_stats
          where query_hash <> 0x0
          group by query_hash
          order by sum(total_elapsed_time) desc
      )
      select $instancecolumns
          coalesce(db_name(st.dbid), db_name(cast(pa.value AS INT)), 'Resource') AS [Database],
          coalesce(object_name(ST.objectid, ST.dbid), '<none>') as ObjectName,
          qs.query_hash as QueryHash,
          qs.total_elapsed_time / 1000 as TotalElapsedTimeMs,
          qs.execution_count as ExecutionCount,
          cast((total_elapsed_time / 1000) / (execution_count + 0.0) as money) as AverageDurationMs,
          lq.elapsed_time / 1000 as QueryTotalElapsedTimeMs,
          SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
              (CASE
                  WHEN QS.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                  ELSE QS.statement_end_offset
                  END - QS.statement_start_offset) / 2) as QueryText,
          qp.query_plan as QueryPlan
      from sys.dm_exec_query_stats qs
      join long_queries lq
          on lq.query_hash = qs.query_hash
      cross apply sys.dm_exec_sql_text(qs.sql_handle) st
      cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
      outer apply sys.dm_exec_plan_attributes(qs.plan_handle) pa
      where pa.attribute = 'dbid' $wheredb $wherenotdb
      order by lq.elapsed_time desc,
          lq.query_hash,
          qs.total_elapsed_time desc
      option (recompile)"

        
        $frequency = ";with frequent_queries as
      (
          select top $Limit
              query_hash,
              sum(execution_count) executions
          from sys.dm_exec_query_stats
          where query_hash <> 0x0
          group by query_hash
          order by sum(execution_count) desc
      )
      select $instancecolumns
          coalesce(db_name(st.dbid), db_name(cast(pa.value AS INT)), 'Resource') AS [Database],
          coalesce(object_name(ST.objectid, ST.dbid), '<none>') as ObjectName,
          qs.query_hash as QueryHash,
          qs.execution_count as ExecutionCount,
          executions as QueryTotalExecutions,
          SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
              (CASE
                  WHEN QS.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                  ELSE QS.statement_end_offset
                  END - QS.statement_start_offset) / 2) as QueryText,
          qp.query_plan as QueryPlan
      from sys.dm_exec_query_stats qs
      join frequent_queries fq
          on fq.query_hash = qs.query_hash
      cross apply sys.dm_exec_sql_text(qs.sql_handle) st
      cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
      outer apply sys.dm_exec_plan_attributes(qs.plan_handle) pa
      where pa.attribute = 'dbid' $wheredb $wherenotdb
      order by fq.executions desc,
          fq.query_hash,
          qs.execution_count desc
      option (recompile)"

        
        $io = ";with high_io_queries as
    (
        select top $Limit
            query_hash,
            sum(total_logical_reads + total_logical_writes) io
        from sys.dm_exec_query_stats
        where query_hash <> 0x0
        group by query_hash
        order by sum(total_logical_reads + total_logical_writes) desc
    )
    select $instancecolumns
        coalesce(db_name(st.dbid), db_name(cast(pa.value AS INT)), 'Resource') AS [Database],
        coalesce(object_name(ST.objectid, ST.dbid), '<none>') as ObjectName,
        qs.query_hash as QueryHash,
        qs.total_logical_reads + total_logical_writes as TotalIO,
        qs.execution_count as ExecutionCount,
        cast((total_logical_reads + total_logical_writes) / (execution_count + 0.0) as money) as AverageIO,
        io as QueryTotalIO,
        SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
            (CASE
                WHEN QS.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                ELSE QS.statement_end_offset
                END - QS.statement_start_offset) / 2) as QueryText,
        qp.query_plan as QueryPlan
    from sys.dm_exec_query_stats qs
    join high_io_queries fq
        on fq.query_hash = qs.query_hash
    cross apply sys.dm_exec_sql_text(qs.sql_handle) st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
    outer apply sys.dm_exec_plan_attributes(qs.plan_handle) pa
    where pa.attribute = 'dbid' $wheredb $wherenotdb
    order by fq.io desc,
        fq.query_hash,
        qs.total_logical_reads + total_logical_writes desc
    option (recompile)"

        
        $cpu = ";with high_cpu_queries as
    (
        select top $Limit
            query_hash,
            sum(total_worker_time) cpuTime
        from sys.dm_exec_query_stats
        where query_hash <> 0x0
        group by query_hash
        order by sum(total_worker_time) desc
    )
    select $instancecolumns
        coalesce(db_name(st.dbid), db_name(cast(pa.value AS INT)), 'Resource') AS [Database],
        coalesce(object_name(ST.objectid, ST.dbid), '<none>') as ObjectName,
        qs.query_hash as QueryHash,
        qs.total_worker_time as CpuTime,
        qs.execution_count as ExecutionCount,
        cast(total_worker_time / (execution_count + 0.0) as money) as AverageCpuMs,
        cpuTime as QueryTotalCpu,
        SUBSTRING(ST.TEXT,(QS.statement_start_offset + 2) / 2,
            (CASE
                WHEN QS.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX),ST.text)) * 2
                ELSE QS.statement_end_offset
                END - QS.statement_start_offset) / 2) as QueryText,
        qp.query_plan as QueryPlan
    from sys.dm_exec_query_stats qs
    join high_cpu_queries hcq
        on hcq.query_hash = qs.query_hash
    cross apply sys.dm_exec_sql_text(qs.sql_handle) st
    cross apply sys.dm_exec_query_plan (qs.plan_handle) qp
    outer apply sys.dm_exec_plan_attributes(qs.plan_handle) pa
    where pa.attribute = 'dbid' $wheredb $wherenotdb
    order by hcq.cpuTime desc,
        hcq.query_hash,
        qs.total_worker_time desc
    option (recompile)"

    }
    
    process {
        foreach ($instance in $SqlInstance) {
            Write-Message -Level Verbose -Message "Attempting to connect to $instance"
            
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }
            
            if ($Type -in "All", "Duration") {
                try {
                    $server.Query($duration) | Select-DefaultView -ExcludeProperty QueryPlan
                }
                catch {
                    Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
                }
            }
            
            if ($Type -in "All", "Frequency") {
                try {
                    $server.Query($frequency) | Select-DefaultView -ExcludeProperty QueryPlan
                }
                catch {
                    Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
                }
            }
            
            if ($Type -in "All", "IO") {
                try {
                    $server.Query($io) | Select-DefaultView -ExcludeProperty QueryPlan
                }
                catch {
                    Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
                }
            }
            
            if ($Type -in "All", "CPU") {
                try {
                    $server.Query($cpu) | Select-DefaultView -ExcludeProperty QueryPlan
                }
                catch {
                    Stop-Function -Message "Failure" -ErrorRecord $_ -Target $server -Continue
                }
            }
        }
    }
}