functions/Get-DbaExecutionPlan.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
function Get-DbaExecutionPlan {
    <#
.SYNOPSIS
Gets execution plans and metadata
  
.DESCRIPTION
Gets execution plans and metadata. Can pipe to Export-DbaExecutionPlan :D
  
Thanks to
 https://www.simple-talk.com/sql/t-sql-programming/dmvs-for-query-plan-metadata/
 and
 http://www.scarydba.com/2017/02/13/export-plans-cache-sqlplan-file/
for the idea and query.
  
.PARAMETER SqlInstance
The SQL Server that you're connecting to.
 
.PARAMETER SqlCredential
Credential object used to connect to the SQL Server as a different user
 
.PARAMETER Database
Return restore information for only specific databases. These are only the databases that currently exist on the server.
  
.PARAMETER ExcludeDatabase
Return restore information for all but these specific databases
 
.PARAMETER SinceCreation
Datetime object used to narrow the results to a date
 
.PARAMETER SinceLastExecution
Datetime object used to narrow the results to a date
 
.PARAMETER ExcludeEmptyQueryPlan
Exclude results with empty query plan
 
.PARAMETER Force
Returns a ton of raw information about the execution plans
  
.NOTES
Tags: Performance
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Get-DbaExecutionPlan
 
.EXAMPLE
Get-DbaExecutionPlan -SqlInstance sqlserver2014a
 
Gets all execution plans on sqlserver2014a
 
.EXAMPLE
Get-DbaExecutionPlan -SqlInstance sqlserver2014a -Database db1, db2 -SinceLastExecution '7/1/2016 10:47:00'
 
Gets all execution plans for databases db1 and db2 on sqlserve2014a since July 1, 2016 at 10:47 AM.
  
.EXAMPLE
Get-DbaExecutionPlan -SqlInstance sqlserver2014a, sql2016 -Exclude db1 | Format-Table
 
Gets execution plan info for all databases except db1 on sqlserver2014a and sql2016 and makes the output pretty
 
.EXAMPLE
Get-DbaExecutionPlan -SqlInstance sql2014 -Database AdventureWorks2014, pubs -Force
 
Gets super detailed information for execution plans on only for AdventureWorks2014 and pubs
  
#>

    [CmdletBinding()]
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [Alias("Credential")]
        [PSCredential]$SqlCredential,
        [object[]]$Database,
        [object[]]$ExcludeDatabase,
        [datetime]$SinceCreation,
        [datetime]$SinceLastExecution,
        [switch]$ExcludeEmptyQueryPlan,
        [switch]$Force
    )

    begin {
                
        if ($SinceCreation -ne $null) {
            $SinceCreation = $SinceCreation.ToString("yyyy-MM-dd HH:mm:ss")
        }
        
        if ($SinceLastExecution -ne $null) {
            $SinceLastExecution = $SinceLastExecution.ToString("yyyy-MM-dd HH:mm:ss")
        }
    }
    process {

        foreach ($instance in $sqlinstance) {
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $Credential
                
                if ($server.VersionMajor -lt 9) {
                    Write-Warning "SQL Server 2000 not supported"
                    continue
                }
                
                if ($force -eq $true) {
                    $select = "SELECT * "
                }
                else {
                    $select = "SELECT DB_NAME(deqp.dbid) as DatabaseName, OBJECT_NAME(deqp.objectid) as ObjectName,
     detqp.query_plan AS SingleStatementPlan,
     deqp.query_plan AS BatchQueryPlan,
     ROW_NUMBER() OVER ( ORDER BY Statement_Start_offset ) AS QueryPosition,
     sql_handle as SqlHandle,
     plan_handle as PlanHandle,
     creation_time as CreationTime,
     last_execution_time as LastExecutionTime"

                }
                
                $from = " FROM sys.dm_exec_query_stats deqs
      CROSS APPLY sys.dm_exec_text_query_plan(deqs.plan_handle,
       deqs.statement_start_offset,
       deqs.statement_end_offset) AS detqp
      CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp
      CROSS APPLY sys.dm_exec_sql_text(deqs.plan_handle) AS execText"

                
                if ($ExcludeDatabase -or $Database -or $SinceCreation.length -gt 0 -or $SinceLastExecution.length -gt 0 -or $ExcludeEmptyQueryPlan -eq $true) {
                    $where = " WHERE "
                }
                
                $wherearray = @()
                
                if ($Database) {
                    $dblist = $Database -join "','"
                    $wherearray += " DB_NAME(deqp.dbid) in ('$dblist') "
                }
                
                if ($SinceCreation -ne $null) {
                    Write-Verbose "Adding creation time"
                    $wherearray += " creation_time >= '$SinceCreation' "
                }
                
                if ($SinceLastExecution -ne $null) {
                    Write-Verbose "Adding last exectuion time"
                    $wherearray += " last_execution_time >= '$SinceLastExecution' "
                }
                
                if ($ExcludeDatabase) {
                    $dblist = $ExcludeDatabase -join "','"
                    $wherearray += " DB_NAME(deqp.dbid) not in ('$dblist') "
                }
                
                if ($ExcludeEmptyQueryPlan) {
                    $wherearray += " detqp.query_plan is not null"
                }
                
                if ($where.length -gt 0) {
                    $wherearray = $wherearray -join " and "
                    $where = "$where $wherearray"
                }
                
                $sql = "$select $from $where"
                Write-Debug $sql
                
                if ($Force -eq $true) {
                    $server.ConnectionContext.ExecuteWithResults($sql).Tables.Rows
                }
                
                $datatable = $server.ConnectionContext.ExecuteWithResults($sql).Tables
                
                foreach ($row in ($datatable.Rows)) {
                    $simple = ([xml]$row.SingleStatementPlan).ShowPlanXML.BatchSequence.Batch.Statements.StmtSimple
                    $sqlhandle = "0x"; $row.sqlhandle | ForEach-Object { $sqlhandle += ("{0:X}" -f $_).PadLeft(2, "0") }
                    $planhandle = "0x"; $row.planhandle | ForEach-Object { $planhandle += ("{0:X}" -f $_).PadLeft(2, "0") }
                    
                    [pscustomobject]@{
                        ComputerName                      = $server.NetName
                        InstanceName                      = $server.ServiceName
                        SqlInstance                       = $server.DomainInstanceName
                        DatabaseName                      = $row.DatabaseName
                        ObjectName                        = $row.ObjectName
                        QueryPosition                     = $row.QueryPosition
                        SqlHandle                         = $SqlHandle
                        PlanHandle                        = $PlanHandle
                        CreationTime                      = $row.CreationTime
                        LastExecutionTime                 = $row.LastExecutionTime
                        StatementCondition                = ([xml]$row.SingleStatementPlan).ShowPlanXML.BatchSequence.Batch.Statements.StmtCond
                        StatementSimple                   = $simple
                        StatementId                       = $simple.StatementId
                        StatementCompId                   = $simple.StatementCompId
                        StatementType                     = $simple.StatementType
                        RetrievedFromCache                = $simple.RetrievedFromCache
                        StatementSubTreeCost              = $simple.StatementSubTreeCost
                        StatementEstRows                  = $simple.StatementEstRows
                        SecurityPolicyApplied             = $simple.SecurityPolicyApplied
                        StatementOptmLevel                = $simple.StatementOptmLevel
                        QueryHash                         = $simple.QueryHash
                        QueryPlanHash                     = $simple.QueryPlanHash
                        StatementOptmEarlyAbortReason     = $simple.StatementOptmEarlyAbortReason
                        CardinalityEstimationModelVersion = $simple.CardinalityEstimationModelVersion
                        ParameterizedText                 = $simple.ParameterizedText
                        StatementSetOptions               = $simple.StatementSetOptions
                        QueryPlan                         = $simple.QueryPlan
                        BatchConditionXml                 = ([xml]$row.BatchQueryPlan).ShowPlanXML.BatchSequence.Batch.Statements.StmtCond
                        BatchSimpleXml                    = ([xml]$row.BatchQueryPlan).ShowPlanXML.BatchSequence.Batch.Statements.StmtSimple
                        BatchQueryPlanRaw                 = [xml]$row.BatchQueryPlan
                        SingleStatementPlanRaw            = [xml]$row.SingleStatementPlan
                    } | Select-DefaultView -ExcludeProperty BatchQueryPlan, SingleStatementPlan, BatchConditionXmlRaw, BatchQueryPlanRaw, SingleStatementPlanRaw
                }
            }
            catch {
                # Will fix this tomorrow, Fred ;)
                Write-Warning $_.Exception
            }
        }
    }
}