-
-
Notifications
You must be signed in to change notification settings - Fork 794
/
Get-DbaLatchStatistic.ps1
142 lines (115 loc) · 5.84 KB
/
Get-DbaLatchStatistic.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
function Get-DbaLatchStatistic {
<#
.SYNOPSIS
Displays latch statistics from sys.dm_os_latch_stats
.DESCRIPTION
This command is based off of Paul Randal's post "Advanced SQL Server performance tuning"
Returns:
LatchClass
WaitSeconds
WaitCount
Percentage
AverageWaitSeconds
URL
Reference: https://www.sqlskills.com/blogs/paul/advanced-performance-troubleshooting-waits-latches-spinlocks/
https://www.sqlskills.com/blogs/paul/most-common-latch-classes-and-what-they-mean/
.PARAMETER SqlInstance
The SQL Server instance. Server version must be SQL Server version 2005 or higher.
.PARAMETER 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.
.PARAMETER Threshold
Threshold, in percentage of all latch stats on the system. Default per Paul's post is 95%.
.PARAMETER 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.
.NOTES
Tags: LatchStatistics, Waits
Author: Patrick Flynn (@sqllensman)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Get-DbaLatchStatistic
.EXAMPLE
PS C:\> Get-DbaLatchStatistic -SqlInstance sql2008, sqlserver2012
Check latch statistics for servers sql2008 and sqlserver2012
.EXAMPLE
PS C:\> Get-DbaLatchStatistic -SqlInstance sql2008 -Threshold 98
Check latch statistics on server sql2008 for thresholds above 98%
.EXAMPLE
PS C:\> $output = Get-DbaLatchStatistic -SqlInstance sql2008 -Threshold 100 | Select-Object * | ConvertTo-DbaDataTable
Collects all latch statistics on server sql2008 into a Data Table.
.EXAMPLE
PS C:\> 'sql2008','sqlserver2012' | Get-DbaLatchStatistic
Get latch statistics for servers sql2008 and sqlserver2012 via pipline
.EXAMPLE
PS C:\> $cred = Get-Credential sqladmin
PS C:\> Get-DbaLatchStatistic -SqlInstance sql2008 -SqlCredential $cred
Connects using sqladmin credential and returns latch statistics from sql2008
.EXAMPLE
PS C:\> $output = Get-DbaLatchStatistic -SqlInstance sql2008
PS C:\> $output
PS C:\> foreach ($row in ($output | Sort-Object -Unique Url)) { Start-Process ($row).Url }
Displays the output then loads the associated sqlskills website for each result. Opens one tab per unique URL.
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[DbaInstance[]]$SqlInstance,
[PSCredential]$SqlCredential,
[int]$Threshold = 95,
[switch]$EnableException
)
BEGIN {
$sql = "WITH [Latches] AS
(
SELECT
[latch_class],
[wait_time_ms] / 1000.0 AS [WaitS],
[waiting_requests_count] AS [WaitCount],
CASE WHEN SUM([wait_time_ms]) OVER() > 0 THEN 100.0 * [wait_time_ms] / SUM([wait_time_ms]) OVER() END AS [Percentage],
ROW_NUMBER() OVER(ORDER BY [wait_time_ms] DESC) AS [RowNum]
FROM sys.dm_os_latch_stats
WHERE [latch_class] NOT IN (N'BUFFER')
)
SELECT
MAX ([W1].[latch_class]) AS [LatchClass],
CAST (MAX ([W1].[WaitS]) AS DECIMAL(14, 2)) AS [WaitSeconds],
MAX ([W1].[WaitCount]) AS [WaitCount],
CAST (MAX ([W1].[Percentage]) AS DECIMAL(14, 2)) AS [Percentage],
CAST (CASE WHEN MAX([W1].[WaitCount]) > 0 THEN MAX([W1].[WaitS]) / MAX([W1].[WaitCount]) END AS DECIMAL (14, 4)) AS [AvgWaitSeconds],
CAST ('https://www.sqlskills.com/help/latches/' + MAX ([W1].[latch_class]) as XML) AS [URL]
FROM [Latches] AS [W1]
INNER JOIN [Latches] AS [W2]
ON [W2].[RowNum] <= [W1].[RowNum]
GROUP BY [W1].[RowNum]
HAVING SUM ([W2].[Percentage]) - MAX ([W1].[Percentage]) < $Threshold;"
Write-Message -Level Debug -Message $sql
}
process {
if (Test-FunctionInterrupt) { return }
foreach ($instance in $SqlInstance) {
try {
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
foreach ($row in $server.Query($sql)) {
[PSCustomObject]@{
ComputerName = $server.ComputerName
InstanceName = $server.ServiceName
SqlInstance = $server.DomainInstanceName
WaitType = $row.LatchClass
WaitSeconds = $row.WaitSeconds
WaitCount = $row.WaitCount
Percentage = $row.Percentage
AverageWaitSeconds = $row.AvgWaitSeconds
URL = $row.URL
}
}
}
}
}