Repository navigation
Expand file tree
/
Copy pathClear-DbaPlanCache.ps1
More file actions
128 lines (101 loc) 路 6.55 KB
/
Copy pathClear-DbaPlanCache.ps1
File metadata and controls
128 lines (101 loc) 路 6.55 KB
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
function Clear-DbaPlanCache {
<#
.SYNOPSIS
Clears SQL Server plan cache when single-use adhoc and prepared plans exceed memory threshold
.DESCRIPTION
Monitors your SQL Server's plan cache for single-use adhoc and prepared plans that consume excessive memory. When these plans exceed the specified threshold (default 100MB), the function clears the entire plan cache using DBCC FREESYSTEMCACHE('SQL Plans').
Single-use plans are a common cause of memory pressure in SQL Server environments with dynamic SQL or applications that don't use parameterized queries. Instead of manually checking sys.dm_exec_cached_plans and running DBCC commands, this function automates the detection and cleanup process.
Use this when you're experiencing memory pressure from plan cache bloat or as part of regular maintenance to prevent cache-related performance issues. The function only clears the cache when necessary, avoiding unnecessary disruption to your server's performance.
References: https://www.sqlskills.com/blogs/kimberly/plan-cache-adhoc-workloads-and-clearing-the-single-use-plan-cache-bloat/
.PARAMETER SqlInstance
The target SQL Server instance or instances.
.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
Specifies the memory threshold in megabytes for single-use adhoc and prepared plans before the plan cache is cleared. Default is 100 MB.
Use this to control when plan cache cleanup occurs based on your server's memory capacity and workload patterns.
.PARAMETER InputObject
Accepts plan cache objects from Get-DbaPlanCache via pipeline input. Each object contains plan cache statistics including memory usage and instance details.
Use this to process multiple instances or when you need to filter plan cache results before clearing.
.PARAMETER WhatIf
If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
.PARAMETER Confirm
If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
.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.
.OUTPUTS
PSCustomObject
Returns one object per plan cache result processed. The object contains the following properties:
- ComputerName: The name of the computer running the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The fully qualified SQL Server instance name (computer\instance)
- Size: The size of the plan cache (displayed with appropriate units)
- Status: The result of the operation - either "Plan cache cleared" if the cache exceeded the threshold and was cleared, or "Plan cache size below threshold (X)" if the size was under the specified threshold
.NOTES
Tags: Diagnostic, Memory
Author: Tracy Boggiano, databasesuperhero.com
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Clear-DbaPlanCache
.EXAMPLE
PS C:\> Clear-DbaPlanCache -SqlInstance sql2017 -Threshold 200
Logs into the SQL Server instance "sql2017" and removes plan caches if over 200 MB.
.EXAMPLE
PS C:\> Clear-DbaPlanCache -SqlInstance sql2017 -SqlCredential sqladmin
Logs into the SQL instance using the SQL Login 'sqladmin' and then Windows instance as 'ad\sqldba'
and removes if Threshold over 100 MB.
.EXAMPLE
PS C:\> Find-DbaInstance -ComputerName localhost | Get-DbaPlanCache | Clear-DbaPlanCache -Threshold 200
Scans localhost for instances using the browser service, traverses all instances and gets the plan cache for each, clears them out if they are above 200 MB.
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[int]$Threshold = 100,
[parameter(ValueFromPipeline)]
[object[]]$InputObject,
[switch]$EnableException
)
process {
foreach ($instance in $SqlInstance) {
$InputObject += Get-DbaPlanCache -SqlInstance $instance -SqlCredential $SqlCredential
}
foreach ($result in $InputObject) {
if ($result.MB -ge $Threshold) {
if ($Pscmdlet.ShouldProcess($($result.SqlInstance), "Cleared SQL Plans plan cache")) {
try {
$server = Connect-DbaInstance -SqlInstance $result.SqlInstance -SqlCredential $SqlCredential
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $result.SqlInstance -Continue
}
$null = $server.Query("DBCC FREESYSTEMCACHE('SQL Plans')")
[PSCustomObject]@{
ComputerName = $result.ComputerName
InstanceName = $result.InstanceName
SqlInstance = $result.SqlInstance
Size = $result.Size
Status = "Plan cache cleared"
}
}
} else {
if ($Pscmdlet.ShouldProcess($($result.SqlInstance), "Results $($result.Size) below threshold")) {
[PSCustomObject]@{
ComputerName = $result.ComputerName
InstanceName = $result.InstanceName
SqlInstance = $result.SqlInstance
Size = $result.Size
Status = "Plan cache size below threshold ($Threshold) "
}
Write-Message -Level Verbose -Message "Plan cache size below threshold ($Threshold) "
}
}
}
}
}