commands

^

Get-DbaExecutionPlan

Author Chrissy LeMaire (@cl), netnerds.net
Availability Windows, Linux, macOS

 

Want to see the source code for this command? Check out Get-DbaExecutionPlan on GitHub.
Want to see the Bill Of Health for this command? Check out Get-DbaExecutionPlan.

Synopsis

Gets execution plans and metadata

Description

Gets execution plans and metadata. Can pipe to Export-DbaExecutionPlan

Thanks to following for the queries:
https://www.simple-talk.com/sql/t-sql-programming/dmvs-for-query-plan-metadata/
http://www.scarydba.com/2017/02/13/export-plans-cache-sqlplan-file/

Syntax

Get-DbaExecutionPlan
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [[-SinceCreation] <DateTime>]
    [[-SinceLastExecution] <DateTime>]
    [-ExcludeEmptyQueryPlan]
    [-Force]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Get-DbaExecutionPlan -SqlInstance sqlserver2014a

Gets all execution plans on sqlserver2014a

Example: 2
PS C:\> Get-DbaExecutionPlan -SqlInstance sqlserver2014a -Database db1, db2 -SinceLastExecution '2016-07-01 10:47:00'

Gets all execution plans for databases db1 and db2 on sqlserver2014a since July 1, 2016 at 10:47 AM.

Example: 3
PS C:\> 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: 4
PS C:\> Get-DbaExecutionPlan -SqlInstance sql2014 -Database AdventureWorks2014, pubs -Force

Gets super detailed information for execution plans on only for AdventureWorks2014 and pubs

Example: 5
PS C:\> $servers = "sqlserver2014a","sql2016t"
PS C:\> $servers | Get-DbaExecutionPlan -Force

Gets super detailed information for execution plans on sqlserver2014a and sql2016

Required Parameters

-SqlInstance

The target SQL Server instance or instances.

Alias
Required True
Pipeline true (ByValue)
Default Value

Optional Parameters

-SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

Alias
Required False
Pipeline false
Default Value
-Database

Return execution plans and metadata for only specific databases.

Alias
Required False
Pipeline false
Default Value
-ExcludeDatabase

Return execution plans and metadata for all but these specific databases

Alias
Required False
Pipeline false
Default Value
-SinceCreation

Datetime object used to narrow the results to a date

Alias
Required False
Pipeline false
Default Value
-SinceLastExecution

Datetime object used to narrow the results to a date

Alias
Required False
Pipeline false
Default Value
-ExcludeEmptyQueryPlan

Exclude results with empty query plan

Alias
Required False
Pipeline false
Default Value False
-Force

Returns a ton of raw information about the execution plans

Alias
Required False
Pipeline false
Default Value False
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

Alias
Required False
Pipeline false
Default Value False