@@ -866,7 +866,9 @@ function Get-DbaWaitStatistic {
866866
867867 # Build the effective ignorable list
868868 $ignorable = New-Object System.Collections.ArrayList
869- $ignorable.AddRange ($defaultIgnorable )
869+ if (-not $IncludeIgnorable ) {
870+ $ignorable.AddRange ($defaultIgnorable )
871+ }
870872
871873 # Add user-specified exclusions
872874 if ($ExcludeWaitType ) {
@@ -886,66 +888,42 @@ function Get-DbaWaitStatistic {
886888 }
887889 }
888890
889- if ($IncludeIgnorable ) {
890- $sql = " WITH [Waits] AS
891- (SELECT
892- [wait_type],
893- [wait_time_ms] / 1000.0 AS [WaitS],
894- ([wait_time_ms] - [signal_wait_time_ms]) / 1000.0 AS [ResourceS],
895- [signal_wait_time_ms] / 1000.0 AS [SignalS],
896- [waiting_tasks_count] AS [WaitCount],
897- CASE WHEN SUM ([wait_time_ms]) OVER() = 0 THEN NULL ELSE 100.0 * [wait_time_ms] / SUM ([wait_time_ms]) OVER() END AS [Percentage],
898- ROW_NUMBER() OVER(ORDER BY [wait_time_ms] DESC) AS [RowNum]
899- FROM sys.dm_os_wait_stats
900- WHERE [waiting_tasks_count] > 0
901- )
902- SELECT
903- MAX ([W1].[wait_type]) AS [WaitType],
904- CAST (MAX ([W1].[WaitS]) AS DECIMAL (16,2)) AS [WaitSeconds],
905- CAST (MAX ([W1].[ResourceS]) AS DECIMAL (16,2)) AS [ResourceSeconds],
906- CAST (MAX ([W1].[SignalS]) AS DECIMAL (16,2)) AS [SignalSeconds],
907- MAX ([W1].[WaitCount]) AS [WaitCount],
908- CAST (MAX ([W1].[Percentage]) AS DECIMAL (5,2)) AS [Percentage],
909- CAST ((MAX ([W1].[WaitS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgWaitSeconds],
910- CAST ((MAX ([W1].[ResourceS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgResSeconds],
911- CAST ((MAX ([W1].[SignalS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgSigSeconds],
912- CAST ('https://www.sqlskills.com/help/waits/' + MAX ([W1].[wait_type]) AS XML) AS [URL]
913- FROM [Waits] AS [W1]
914- INNER JOIN [Waits] AS [W2]
915- ON [W2].[RowNum] <= [W1].[RowNum]
916- GROUP BY [W1].[RowNum] HAVING SUM ([W2].[Percentage]) - MAX([W1].[Percentage]) < $Threshold "
891+ if ($ignorable.Count -gt 0 ) {
892+ $ignorableSql = " AND CAST([wait_type] AS VARCHAR(60)) NOT IN ('$ ( $ignorable -join " ','" ) ')"
917893 } else {
918- $IgnorableList = " '$ ( $ignorable -join " ','" ) '"
919- $sql = " WITH [Waits] AS
920- (SELECT
921- [wait_type],
922- [wait_time_ms] / 1000.0 AS [WaitS],
923- ([wait_time_ms] - [signal_wait_time_ms]) / 1000.0 AS [ResourceS],
924- [signal_wait_time_ms] / 1000.0 AS [SignalS],
925- [waiting_tasks_count] AS [WaitCount],
926- CASE WHEN SUM ([wait_time_ms]) OVER() = 0 THEN NULL ELSE 100.0 * [wait_time_ms] / SUM ([wait_time_ms]) OVER() END AS [Percentage],
927- ROW_NUMBER() OVER(ORDER BY [wait_time_ms] DESC) AS [RowNum]
928- FROM sys.dm_os_wait_stats
929- WHERE [waiting_tasks_count] > 0
930- AND CAST([wait_type] AS VARCHAR(60)) NOT IN ($IgnorableList )
931- )
932- SELECT
933- MAX ([W1].[wait_type]) AS [WaitType],
934- CAST (MAX ([W1].[WaitS]) AS DECIMAL (16,2)) AS [WaitSeconds],
935- CAST (MAX ([W1].[ResourceS]) AS DECIMAL (16,2)) AS [ResourceSeconds],
936- CAST (MAX ([W1].[SignalS]) AS DECIMAL (16,2)) AS [SignalSeconds],
937- MAX ([W1].[WaitCount]) AS [WaitCount],
938- CAST (MAX ([W1].[Percentage]) AS DECIMAL (5,2)) AS [Percentage],
939- CAST ((MAX ([W1].[WaitS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgWaitSeconds],
940- CAST ((MAX ([W1].[ResourceS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgResSeconds],
941- CAST ((MAX ([W1].[SignalS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgSigSeconds],
942- CAST ('https://www.sqlskills.com/help/waits/' + MAX ([W1].[wait_type]) AS XML) AS [URL]
943- FROM [Waits] AS [W1]
944- INNER JOIN [Waits] AS [W2]
945- ON [W2].[RowNum] <= [W1].[RowNum]
946- GROUP BY [W1].[RowNum] HAVING SUM ([W2].[Percentage]) - MAX([W1].[Percentage]) < $Threshold "
947-
894+ $ignorableSql = " "
948895 }
896+ Write-Message - Level Verbose - Message " Using ignorableSql '$ignorableSql '"
897+
898+ $sql = " WITH [Waits] AS
899+ (SELECT
900+ [wait_type],
901+ [wait_time_ms] / 1000.0 AS [WaitS],
902+ ([wait_time_ms] - [signal_wait_time_ms]) / 1000.0 AS [ResourceS],
903+ [signal_wait_time_ms] / 1000.0 AS [SignalS],
904+ [waiting_tasks_count] AS [WaitCount],
905+ CASE WHEN SUM ([wait_time_ms]) OVER() = 0 THEN NULL ELSE 100.0 * [wait_time_ms] / SUM ([wait_time_ms]) OVER() END AS [Percentage],
906+ ROW_NUMBER() OVER(ORDER BY [wait_time_ms] DESC) AS [RowNum]
907+ FROM sys.dm_os_wait_stats
908+ WHERE [waiting_tasks_count] > 0
909+ $ignorableSql
910+ )
911+ SELECT
912+ MAX ([W1].[wait_type]) AS [WaitType],
913+ CAST (MAX ([W1].[WaitS]) AS DECIMAL (16,2)) AS [WaitSeconds],
914+ CAST (MAX ([W1].[ResourceS]) AS DECIMAL (16,2)) AS [ResourceSeconds],
915+ CAST (MAX ([W1].[SignalS]) AS DECIMAL (16,2)) AS [SignalSeconds],
916+ MAX ([W1].[WaitCount]) AS [WaitCount],
917+ CAST (MAX ([W1].[Percentage]) AS DECIMAL (5,2)) AS [Percentage],
918+ CAST ((MAX ([W1].[WaitS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgWaitSeconds],
919+ CAST ((MAX ([W1].[ResourceS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgResSeconds],
920+ CAST ((MAX ([W1].[SignalS]) / MAX ([W1].[WaitCount])) AS DECIMAL (16,4)) AS [AvgSigSeconds],
921+ CAST ('https://www.sqlskills.com/help/waits/' + MAX ([W1].[wait_type]) AS XML) AS [URL]
922+ FROM [Waits] AS [W1]
923+ INNER JOIN [Waits] AS [W2]
924+ ON [W2].[RowNum] <= [W1].[RowNum]
925+ GROUP BY [W1].[RowNum] HAVING SUM ([W2].[Percentage]) - MAX([W1].[Percentage]) < $Threshold "
926+
949927 Write-Message - Level Debug - Message $sql
950928 }
951929 process {
@@ -964,9 +942,6 @@ function Get-DbaWaitStatistic {
964942
965943 foreach ($row in $server.Query ($sql )) {
966944 $waitType = $row.WaitType
967- if (-not $IncludeIgnorable ) {
968- if ($ignorable -contains $waitType ) { continue }
969- }
970945
971946 [PSCustomObject ]@ {
972947 ComputerName = $server.ComputerName
@@ -982,7 +957,7 @@ function Get-DbaWaitStatistic {
982957 AverageWaitSeconds = $row.AvgWaitSeconds
983958 AverageResourceSeconds = $row.AvgResSeconds
984959 AverageSignalSeconds = $row.AvgSigSeconds
985- Ignorable = ($ignorable -contains $waitType )
960+ Ignorable = ($defaultIgnorable -contains $waitType )
986961 URL = $row.URL
987962 Notes = ($details ).$waitType
988963 } | Select-DefaultView - ExcludeProperty $excludeColumns
0 commit comments