functions/Export-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
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
283
284
285
Function Export-DbaExecutionPlan
{
<#
.SYNOPSIS
Exports execution plans to disk.
  
.DESCRIPTION
Exports execution plans to disk. Can pipe from 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 Databases
Return restore information for only specific databases. These are only the databases that currently exist on the server.
  
.PARAMETER Exclude
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 Path
The directory where all of the sqlxml files will be exported
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
 
.PARAMETER PipedObject
Internal parameter
 
.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/Export-DbaExecutionPlan
 
.EXAMPLE
Export-DbaExecutionPlan -SqlInstance sqlserver2014a
 
Exports all execution plans for sqlserver2014a.
 
.EXAMPLE
Export-DbaExecutionPlan -SqlInstance sqlserver2014a -Databases db1, db2 -SinceLastExecution '7/1/2016 10:47:00'
 
Exports all execution plans for databases db1 and db2 on sqlserve2014a since July 1, 2016 at 10:47 AM.
  
#>

    [cmdletbinding(SupportsShouldProcess = $true, DefaultParameterSetName = "Default")]
    Param (
        [parameter(ParameterSetName = 'NotPiped', Mandatory)]
        [Alias("ServerInstance", "SqlServer")]
        [string[]]$SqlInstance,
        [parameter(ParameterSetName = 'NotPiped')]
        [Alias("Credential")]
        [PsCredential]$SqlCredential,
        [parameter(ParameterSetName = 'Piped', Mandatory)]
        [parameter(ParameterSetName = 'NotPiped', Mandatory)]
        [string]$Path,
        [parameter(ParameterSetName = 'NotPiped')]
        [datetime]$SinceCreation,
        [parameter(ParameterSetName = 'NotPiped')]
        [datetime]$SinceLastExecution,
        [Parameter(ParameterSetName = 'Piped', Mandatory, ValueFromPipeline)]
        [object[]]$PipedObject
    )
    
    DynamicParam { if ($SqlInstance) { return Get-ParamSqlDatabases -SqlServer $SqlInstance[0] -SqlCredential $SqlCredential } }
    
    BEGIN
    {
        # Convert from RuntimeDefinedParameter object to regular array
        $databases = $psboundparameters.Databases
        $exclude = $psboundparameters.Exclude
        
        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")
        }
        
        function Process-Object ($object)
        {
            $instancename = $object.SqlInstance
            $dbname = $object.DatabaseName
            $queryposition = $object.QueryPosition
            $sqlhandle = "0x"; $object.sqlhandle | ForEach-Object { $sqlhandle += ("{0:X}" -f $_).PadLeft(2, "0") }
            $sqlhandle = $sqlhandle.TrimStart('0x02000000').TrimEnd('0000000000000000000000000000000000000000')
            $shortname = "$instancename-$dbname-$queryposition-$sqlhandle"
            
            foreach ($queryplan in $object.BatchQueryPlanRaw)
            {
                $filename = "$path\$shortname-batch.sqlplan"
                
                try
                {
                    If ($Pscmdlet.ShouldProcess("localhost", "Writing XML file to $filename"))
                    {
                        $queryplan.Save($filename)
                    }
                }
                catch
                {
                    Write-Verbose "Skipped query plan for $filename because it is null"
                }
            }
            
            foreach ($statementplan in $object.SingleStatementPlanRaw)
            {
                $filename = "$path\$shortname.sqlplan"
                
                try
                {
                    If ($Pscmdlet.ShouldProcess("localhost", "Writing XML file to $filename"))
                    {
                        $statementplan.Save($filename)
                    }
                }
                catch
                {
                    Write-Verbose "Skipped statement plan for $filename because it is null"
                }
            }
            
            If ($Pscmdlet.ShouldProcess("console", "Showing output object"))
            {
                Add-Member -InputObject $object -MemberType NoteProperty -Name OutputFile -Value $filename
                Select-DefaultView -InputObject $object -Property ComputerName, InstanceName, SqlInstance, DatabaseName, SqlHandle, CreationTime, LastExecutionTime, OutputFile
            }
        }
    }
    
    PROCESS
    {
        if (!(Test-Path $Path))
        {
            $null = New-Item -ItemType Directory -Path $Path
        }
        
        if ($PipedObject)
        {
            
            foreach ($object in $pipedobject)
            {
                Process-Object $object
                return
            }
        }
        
        foreach ($instance in $sqlinstance)
        {
            try
            {
                $server = Connect-SqlServer -SqlServer $instance -SqlCredential $Credential
                
                if ($server.VersionMajor -lt 9)
                {
                    Write-Warning "SQL Server 2000 not supported"
                    continue
                }
                
                $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 ($exclude.length -gt 0 -or $databases.length -gt 0 -or $SinceCreation.length -gt 0 -or $SinceLastExecution.length -gt 0 -or $ExcludeEmptyQueryPlan -eq $true)
                {
                    $where = " WHERE "
                }
                
                $wherearray = @()
                
                if ($databases.length -gt 0)
                {
                    $dblist = $databases -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 ($exclude.length -gt 0)
                {
                    $dblist = $exclude -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
                                
                $datatable = $server.ConnectionContext.ExecuteWithResults($sql).Tables
                
                foreach ($row in ($datatable.Rows))
                {
                    
                    $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") }
                    
                    $object = [pscustomobject]@{
                        ComputerName = $server.NetName
                        InstanceName = $server.ServiceName
                        SqlInstance = $server.DomainInstanceName
                        DatabaseName = $row.DatabaseName
                        SqlHandle = $sqlhandle
                        PlanHandle = $planhandle
                        SingleStatementPlan = $row.SingleStatementPlan
                        BatchQueryPlan = $row.BatchQueryPlan
                        QueryPosition = $row.QueryPosition
                        CreationTime = $row.CreationTime
                        LastExecutionTime = $row.LastExecutionTime
                        BatchQueryPlanRaw = [xml]$row.BatchQueryPlan
                        SingleStatementPlanRaw = [xml]$row.SingleStatementPlan
                    }
                    
                    Process-Object $object
                }
            }
            catch
            {
                Write-Warning $_
                continue
                # Stop-Function -Message $_.Exception -Silent $Silent -InnerErrorRecord $_ -Target $filename
            }
        }
    }
}