< Back

Get-DbaQueryExecutionTime

Mon Jan 13, 2020 11:29 am

NAME Get-DbaQueryExecutionTime



SYNOPSIS

Displays Stored Procedures and Ad hoc queries with the highest execution times. Works on SQL Server 2008 and

above.





SYNTAX

Get-DbaQueryExecutionTime -SqlInstance <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>

[-SqlCredential <Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>]

[-MaxResultsPerDb <Int>] [-MinExecs <Int>] [[-MinExecMs] <Int>] [[-ExcludeSystem] <Switch>] [-EnableException

<Switch>] [<CommonParameters>]





DESCRIPTION

Quickly find slow query executions within a database. Results will include stored procedures and individual SQL

statements.





PARAMETERS

-Database [<System.Object[]>]

The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will

be processed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDatabase [<System.Object[]>]

The database(s) to exclude - this list is auto-populated from the server



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeSystem [<Switch>]

Allows you to suppress output on system databases



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-MaxResultsPerDb [<Int>]

Allows you to limit the number of results returned, as many systems can have very large amounts of query

plans. Default value is 100 results.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-MinExecMs [<Int>]

Allows you to limit the scope to queries with a specified average execution time. Default value is 500 (ms).



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-MinExecs [<Int>]

Allows you to limit the scope to queries that have been executed a minimum number of time. Default value is

100 executions.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

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.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Query, Performance

Author: Brandon Abshire, netnerds.net



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Get-DbaQueryExecutionTime -SqlInstance sql2008, sqlserver2012



Return the top 100 slowest stored procedures or statements for servers sql2008 and sqlserver2012.

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Get-DbaQueryExecutionTime -SqlInstance sql2008 -Database TestDB



Return the top 100 slowest stored procedures or statements on server sql2008 for only the TestDB database.

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Get-DbaQueryExecutionTime -SqlInstance sql2008 -Database TestDB -MaxResultsPerDb 100 -MinExecs 200

-MinExecMs 1000



Return the top 100 slowest stored procedures or statements on server sql2008 for only the TestDB database,

limiting results to queries with more than 200 total executions and an execution time over 1000ms or higher.



RELATED LINKS

https://dbatools.io/Get-DbaQueryExecutionTime