1
1
/*
2
- Max Server Memory Calculator
3
- https://bornsql.ca/memory/
4
- Copyright (c) BornSQL.ca
5
- Written by Randolph West, released under the MIT License
6
- Last updated: 17 March 2020
7
-
8
- Based on an original algorithm by Jonathan Kehayias:
9
- https://www.sqlskills.com/blogs/jonathan/how-much-memory-does-my-sql-server-actually-need/
10
-
11
- Max Worker Thread Stack calculation based on Tiger Toolbox Maintenance Solution.
12
- Copyright (c) Microsoft Corporation. All rights reserved.
13
- https://github.com/Microsoft/tigertoolbox/tree/master/MaintenanceSolution
14
-
15
- SQL Server, on a standalone instance, requires the following reserved RAM for a server:
16
- - 1 GB of RAM for the OS
17
- - plus 1 GB for each 4 GB of RAM installed from 4 - 16 GB
18
- - plus 1 GB for every 8 GB RAM installed above 16 GB RAM
19
-
20
- Memory for the Thread Stack can also be taken into account:
21
- - 32-bit, reserve 512KB per thread * Max Worker Threads
22
- - 64-bit, reserve 2MB per thread * Max Worker Threads
23
- - 128-bit, reserve 4MB per thread * Max Worker Threads
24
-
25
- Thanks to @sqlEmt and @sqlstudent144 for testing.
26
- Thanks to the Tiger Team for version number and thread stack calculations.
2
+ Max Server Memory Calculator
3
+ https://bornsql.ca/memory/
4
+ Copyright (c) BornSQL.ca
5
+ Written by Randolph West, released under the MIT License
6
+ Last updated: 19 June 2020
7
+
8
+ Based on an original algorithm by Jonathan Kehayias:
9
+ https://www.sqlskills.com/blogs/jonathan/how-much-memory-does-my-sql-server-actually-need/
10
+
11
+ Max Worker Thread Stack calculation based on Tiger Toolbox Maintenance Solution.
12
+ Copyright (c) Microsoft Corporation. All rights reserved.
13
+ https://github.com/Microsoft/tigertoolbox/tree/master/MaintenanceSolution
14
+
15
+ SQL Server, on a standalone instance, requires the following reserved RAM for a server:
16
+ - 1 GB of RAM for the OS
17
+ - plus 1 GB for each 4 GB of RAM installed from 4 - 16 GB
18
+ - plus 1 GB for every 8 GB RAM installed above 16 GB RAM
19
+
20
+ Memory for the Thread Stack can also be taken into account:
21
+ - 32-bit, reserve 512KB per thread * Max Worker Threads
22
+ - 64-bit, reserve 2MB per thread * Max Worker Threads
23
+ - 128-bit, reserve 4MB per thread * Max Worker Threads
24
+
25
+ Thanks to @sqlEmt and @sqlstudent144 for testing.
26
+ Thanks to the Tiger Team for version number and thread stack calculations.
27
27
28
28
v1.0 - 2016-08-19 - Initial release.
29
29
@@ -33,6 +33,8 @@ v1.2 - 2018-09-07 - Removed reference to errant DMV.
33
33
34
34
v1.3 - 2020-03-17 - Happy St. Patrick's Day.
35
35
36
+ v1.4 - 2020-06-19 - Fixes to comments and formatting.
37
+
36
38
*/
37
39
38
40
-- Set this to 1 if you want to configure NUMA Node Affinity
@@ -49,10 +51,25 @@ DECLARE @numaNodesAfinned TINYINT;
49
51
DECLARE @maxWorkerThreadCount INT ;
50
52
DECLARE @threadStack DECIMAL (20 , 4 );
51
53
52
- SELECT @cpuArchitecture = CASE WHEN @@VERSION LIKE ' %<X64>%' THEN 2 WHEN @@VERSION LIKE ' %<IA64>%' THEN 4 ELSE 0 .5 END ;
53
- SELECT @numaNodes = COUNT (DISTINCT parent_node_id) FROM sys .dm_os_schedulers WHERE scheduler_id < 255 AND parent_node_id < 64 ;
54
- SELECT @numaNodesAfinned = COUNT (DISTINCT parent_node_id) FROM sys .dm_os_schedulers WHERE scheduler_id < 255 AND parent_node_id < 64 AND is_online = 1 ;
55
- SELECT @maxWorkerThreadCount = max_workers_count FROM sys .dm_os_sys_info ;
54
+ SELECT @cpuArchitecture = CASE
55
+ WHEN @@VERSION LIKE ' %<X64>%' THEN
56
+ 2
57
+ WHEN @@VERSION LIKE ' %<IA64>%' THEN
58
+ 4
59
+ ELSE
60
+ 0 .5
61
+ END ;
62
+ SELECT @numaNodes = COUNT (DISTINCT parent_node_id)
63
+ FROM sys .dm_os_schedulers
64
+ WHERE scheduler_id < 255
65
+ AND parent_node_id < 64 ;
66
+ SELECT @numaNodesAfinned = COUNT (DISTINCT parent_node_id)
67
+ FROM sys .dm_os_schedulers
68
+ WHERE scheduler_id < 255
69
+ AND parent_node_id < 64
70
+ AND is_online = 1 ;
71
+ SELECT @maxWorkerThreadCount = max_workers_count
72
+ FROM sys .dm_os_sys_info ;
56
73
SELECT @threadStack = @maxWorkerThreadCount * @cpuArchitecture / 1024 .0 ;
57
74
58
75
-- Get physical RAM on server
@@ -67,23 +84,21 @@ BEGIN
67
84
SELECT @overheadMemory = 0 .5 ;
68
85
END ;
69
86
70
- IF (@physicalMemory > 2 .0
71
- AND @physicalMemory < 4 .0 )
87
+ IF (@physicalMemory > 2 .0 AND @physicalMemory < 4 .0 )
72
88
BEGIN
73
89
SELECT @overheadMemory = 2 .0 ;
74
90
END ;
75
91
76
- IF (@physicalMemory >= 4 .0
77
- AND @physicalMemory <= 16 .0 )
92
+ IF (@physicalMemory >= 4 .0 AND @physicalMemory <= 16 .0 )
78
93
BEGIN
79
94
SELECT @overheadMemory = 1 .0 /* Operating System minimum */
80
- + (@physicalMemory / 4 .0 );
95
+ + (@physicalMemory / 4 .0 );
81
96
END ;
82
97
83
98
IF (@physicalMemory > 16 .0 )
84
99
BEGIN
85
100
SELECT @overheadMemory = 1 .0 /* Operating System minimum */ + 4 .0 /* add in reserved for <= 16GB */
86
- + ((@physicalMemory - 16 .0 ) / 8 .0 );
101
+ + ((@physicalMemory - 16 .0 ) / 8 .0 );
87
102
END ;
88
103
89
104
-- Add in the Max Worker Threads Overhead
@@ -94,55 +109,50 @@ DECLARE @enterprise BIT = 0;
94
109
DECLARE @developer BIT = 0 ;
95
110
DECLARE @override BIT = 0 ;
96
111
97
- IF (
98
- @editionId IN (
99
- 1804890536 ,
100
- 1872460670 ,
101
- 610778273
102
- )
103
- )
112
+ IF (@editionId IN ( 1804890536 , 1872460670 , 610778273 ))
104
113
BEGIN
105
114
SELECT @enterprise = 1 ;
106
115
END ;
107
116
108
- IF (@editionId = - 2117995310 )
117
+ IF (@editionId = - 2117995310 )
118
+ BEGIN
109
119
SELECT @developer = 1 ;
120
+ END ;
110
121
111
122
-- Check for Standard Edition Limitations
112
- IF (
113
- @enterprise = 0
114
- AND @developer = 0
115
- )
123
+ IF (@enterprise = 0 AND @developer = 0 )
116
124
BEGIN
117
125
DECLARE @ProductVersion INT = CONVERT (INT , (@@MICROSOFTVERSION / 0x1000000) & 0xff);
118
126
119
127
IF (@ProductVersion >= 11 )
120
- AND (@physicalMemory > 128 )
128
+ AND (@physicalMemory > 128 )
121
129
BEGIN
122
130
SELECT @overheadMemory = 1 .0 + 4 .0 + ((128 - 16 .0 ) / 8 .0 );
123
131
124
132
-- Set the memory value to the max allowed, if there is enough headroom
125
133
IF (@physicalMemory - @overheadMemory >= 128 )
126
134
SELECT @recommendedMemory = 128 ,
127
- @overheadMemory = 0 ,
128
- @override = 1 ;
135
+ @overheadMemory = 0 ,
136
+ @override = 1 ;
129
137
END ;
130
138
131
139
IF (@ProductVersion < 11 )
132
- AND (@physicalMemory > 64 )
140
+ AND (@physicalMemory > 64 )
133
141
BEGIN
134
142
SELECT @overheadMemory = 1 .0 + 4 .0 + ((64 - 16 .0 ) / 8 .0 );
135
143
136
144
-- Set the memory value to the max allowed, if there is enough headroom
137
145
IF (@physicalMemory - @overheadMemory >= 64 )
138
146
SELECT @recommendedMemory = 64 ,
139
- @overheadMemory = 0 ,
140
- @override = 1 ;
147
+ @overheadMemory = 0 ,
148
+ @override = 1 ;
141
149
END ;
142
150
END ;
143
151
144
152
IF (@override = 0 )
153
+ BEGIN
145
154
SELECT @recommendedMemory = @physicalMemory - @overheadMemory;
155
+ END ;
146
156
147
157
-- Configure NUMA Affinity
148
158
IF (@configureNumaNodeAffinity = 1 )
@@ -151,20 +161,21 @@ BEGIN
151
161
END ;
152
162
153
163
SELECT @@VERSION AS [Version],
154
- CASE
155
- WHEN (@enterprise = 1 )
156
- THEN ' Enterprise Edition'
157
- WHEN (@developer = 1 )
158
- THEN ' Developer Edition'
159
- ELSE ' Non-Enterprise Edition'
160
- END AS [Edition],
161
- CAST (@physicalMemorySource AS INT ) AS [Physical RAM (MB)],
162
- c.[value] AS [Configured Value (MB)],
163
- c.[value_in_use] AS [Running Value (MB)],
164
- CAST (@recommendedMemory * 1024 AS INT ) AS [Recommended Value (MB)],
165
- N ' EXEC sp_configure '' show advanced options'' , 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure '' max server memory (MB)'' , '
166
- + CAST (CAST (@recommendedMemory * 1024 AS INT ) AS NVARCHAR (20 ))
167
- + ' ; EXEC sp_configure '' show advanced options'' , 0; RECONFIGURE WITH OVERRIDE;' AS [Script]
164
+ CASE
165
+ WHEN (@enterprise = 1 ) THEN
166
+ ' Enterprise Edition'
167
+ WHEN (@developer = 1 ) THEN
168
+ ' Developer Edition'
169
+ ELSE
170
+ ' Non-Enterprise Edition'
171
+ END AS [Edition],
172
+ CAST (@physicalMemorySource AS INT ) AS [Physical RAM (MB)],
173
+ c.[value] AS [Configured Value (MB)],
174
+ c.[value_in_use] AS [Running Value (MB)],
175
+ CAST (@recommendedMemory * 1024 AS INT ) AS [Recommended Value (MB)],
176
+ N ' EXEC sp_configure '' show advanced options'' , 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure '' max server memory (MB)'' , '
177
+ + CAST (CAST (@recommendedMemory * 1024 AS INT ) AS NVARCHAR (20 ))
178
+ + ' ; EXEC sp_configure '' show advanced options'' , 0; RECONFIGURE WITH OVERRIDE;' AS [Script]
168
179
FROM sys .configurations c
169
180
WHERE [c].[name] = N ' max server memory (MB)'
170
181
OPTION (RECOMPILE );
0 commit comments