Test-DbaDbQueryStore
View SourceSynopsis
Compares Query Store settings against best practices.
Description
Evaluates Query Store against a set of rules to match best practices. The rules are:
- ActualState = ReadWrite (This means Query Store is enabled and collecting data.)
- DataFlushIntervalInSeconds = 900 (Recommended to leave this at the default of 900 seconds (15 mins).)
- MaxPlansPerQuery = 200 (Number of distinct plans per query. 200 is a good starting point for most environments.)
- MaxStorageSizeInMB = 2048 (How much disk space Query Store will use. 2GB is a good starting point.)
- QueryCaptureMode = Auto (With auto, queries that are insignificant from a resource utilization perspective, or executed infrequently, are not captured.)
- SizeBasedCleanupMode = Auto (With auto, as Query Store gets close to out of space it will automatically purge older data.)
- StaleQueryThresholdInDays = 30 (Determines how much historic data to keep. 30 days is a good value here.)
- StatisticsCollectionIntervalInMinutes = 30 (Time window that runtime stats will be aggregated. Use 30 unless you have space concerns, then leave at the default (60).)
- WaitStatsCaptureMode = ON (Adds valuable data when troubleshooting.)
- Trace Flag 7745 enabled
- Trace Flag 7752 enabled
Syntax
Test-DbaDbQueryStore
[[-SqlInstance] <DbaInstanceParameter[]>]
[[-SqlCredential] <PSCredential>]
[[-Database] <Object[]>]
[[-ExcludeDatabase] <Object[]>]
[[-InputObject] <Object[]>]
[-EnableException]
[<CommonParameters>]
Examples
Example: 1
PS C:\> Test-DbaDbQueryStore -SqlInstance localhost
Checks that Query Store is enabled and meets best practices for all user databases on the localhost machine.
Example: 2
PS C:\> Test-DbaDbQueryStore -SqlInstance localhost -Database AdventureWorks2017
Checks that Query Store is enabled and meets best practices for the AdventureWorks2017 database on the localhost machine.
Example: 3
PS C:\> Test-DbaDbQueryStore -SqlInstance localhost -ExcludeDatabase AdventureWorks2017
Checks that Query Store is enabled and meets best practices for all user databases except AdventureWorks2017 on the localhost machine.
Example: 4
PS C:\> $databases = Get-DbaDatabase -SqlInstance localhost
PS C:\> $databases | Test-DbaDbQueryStore
Checks that Query Store is enabled and meets best practices for all databases that are piped on the localhost machine.
Optional Parameters
-SqlInstance
The target SQL Server instance or instances.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-Database
Specifies which databases to test for Query Store best practices. Accepts wildcards for pattern matching.
Use this when you need to evaluate Query Store settings for specific databases instead of all user databases on the instance.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-ExcludeDatabase
Excludes specific databases from Query Store evaluation. System databases (master, model, tempdb) are automatically excluded.
Use this when you want to test most databases but skip certain ones like development or temporary databases.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value |
-InputObject
Accepts database objects piped from Get-DbaDatabase, server objects, or instance parameters for testing.
Use this when you want to test Query Store settings on a pre-filtered set of databases or work within a pipeline workflow.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | true (ByValue) |
| Default Value |
-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.
| Property | Value |
|---|---|
| Alias | |
| Required | False |
| Pipeline | false |
| Default Value | False |
Outputs
PSCustomObject
Returns one object per Query Store configuration property per database, plus one object per enabled trace flag per instance (on non-Azure servers only).
Query Store Configuration Properties (returned for each evaluated database):
- ComputerName: The name of the computer hosting the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance format)
- Database: The name of the database being evaluated
- Name: The configuration property name (e.g., ActualState, MaxStorageSizeInMB, StaleQueryThresholdInDays)
- Value: The current value of the configuration property
- RecommendedValue: The recommended value for this property based on best practices
- IsBestPractice: Boolean indicating if the current value matches the recommended value (true = best practice compliant)
- Justification: Explanation of why the recommended value is best practice
Trace Flag Status (returned only on SQL Server instances that are not Azure SQL Database):
- ComputerName: The name of the computer hosting the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name
- Name: The trace flag name (e.g., “Trace Flag 7745 Enabled”, “Trace Flag 7752 Enabled”)
- Value: The status of the trace flag (Enabled or Disabled)
- RecommendedValue: The trace flag number (7745 or 7752)
- IsBestPractice: Boolean indicating if the trace flag is enabled as recommended
- Justification: Explanation of why the trace flag should be enabled Output volume: One object per configuration property per database, typically resulting in 9-10 objects per database evaluated (Query Store configuration + trace flags on non-Azure instances).
dbatools