Skip to content

Commit 68e982d

Browse files
committed
Spring cleaning
1 parent b1f8d04 commit 68e982d

7 files changed

+660
-658
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# scripts
2+
23
Miscellaneous Free Stuff
34

45
## Max Server Memory

max_server_memory.sql

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/*
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.
2727
2828
v1.0 - 2016-08-19 - Initial release.
2929
@@ -33,6 +33,8 @@ v1.2 - 2018-09-07 - Removed reference to errant DMV.
3333
3434
v1.3 - 2020-03-17 - Happy St. Patrick's Day.
3535
36+
v1.4 - 2020-06-19 - Fixes to comments and formatting.
37+
3638
*/
3739

3840
-- Set this to 1 if you want to configure NUMA Node Affinity
@@ -49,10 +51,25 @@ DECLARE @numaNodesAfinned TINYINT;
4951
DECLARE @maxWorkerThreadCount INT;
5052
DECLARE @threadStack DECIMAL(20, 4);
5153

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;
5673
SELECT @threadStack = @maxWorkerThreadCount * @cpuArchitecture / 1024.0;
5774

5875
-- Get physical RAM on server
@@ -67,23 +84,21 @@ BEGIN
6784
SELECT @overheadMemory = 0.5;
6885
END;
6986

70-
IF (@physicalMemory > 2.0
71-
AND @physicalMemory < 4.0)
87+
IF (@physicalMemory > 2.0 AND @physicalMemory < 4.0)
7288
BEGIN
7389
SELECT @overheadMemory = 2.0;
7490
END;
7591

76-
IF (@physicalMemory >= 4.0
77-
AND @physicalMemory <= 16.0)
92+
IF (@physicalMemory >= 4.0 AND @physicalMemory <= 16.0)
7893
BEGIN
7994
SELECT @overheadMemory = 1.0 /* Operating System minimum */
80-
+ (@physicalMemory / 4.0);
95+
+ (@physicalMemory / 4.0);
8196
END;
8297

8398
IF (@physicalMemory > 16.0)
8499
BEGIN
85100
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);
87102
END;
88103

89104
-- Add in the Max Worker Threads Overhead
@@ -94,55 +109,50 @@ DECLARE @enterprise BIT = 0;
94109
DECLARE @developer BIT = 0;
95110
DECLARE @override BIT = 0;
96111

97-
IF (
98-
@editionId IN (
99-
1804890536,
100-
1872460670,
101-
610778273
102-
)
103-
)
112+
IF (@editionId IN ( 1804890536, 1872460670, 610778273 ))
104113
BEGIN
105114
SELECT @enterprise = 1;
106115
END;
107116

108-
IF (@editionId = - 2117995310)
117+
IF (@editionId = -2117995310)
118+
BEGIN
109119
SELECT @developer = 1;
120+
END;
110121

111122
-- Check for Standard Edition Limitations
112-
IF (
113-
@enterprise = 0
114-
AND @developer = 0
115-
)
123+
IF (@enterprise = 0 AND @developer = 0)
116124
BEGIN
117125
DECLARE @ProductVersion INT = CONVERT(INT, (@@MICROSOFTVERSION / 0x1000000) & 0xff);
118126

119127
IF (@ProductVersion >= 11)
120-
AND (@physicalMemory > 128)
128+
AND (@physicalMemory > 128)
121129
BEGIN
122130
SELECT @overheadMemory = 1.0 + 4.0 + ((128 - 16.0) / 8.0);
123131

124132
-- Set the memory value to the max allowed, if there is enough headroom
125133
IF (@physicalMemory - @overheadMemory >= 128)
126134
SELECT @recommendedMemory = 128,
127-
@overheadMemory = 0,
128-
@override = 1;
135+
@overheadMemory = 0,
136+
@override = 1;
129137
END;
130138

131139
IF (@ProductVersion < 11)
132-
AND (@physicalMemory > 64)
140+
AND (@physicalMemory > 64)
133141
BEGIN
134142
SELECT @overheadMemory = 1.0 + 4.0 + ((64 - 16.0) / 8.0);
135143

136144
-- Set the memory value to the max allowed, if there is enough headroom
137145
IF (@physicalMemory - @overheadMemory >= 64)
138146
SELECT @recommendedMemory = 64,
139-
@overheadMemory = 0,
140-
@override = 1;
147+
@overheadMemory = 0,
148+
@override = 1;
141149
END;
142150
END;
143151

144152
IF (@override = 0)
153+
BEGIN
145154
SELECT @recommendedMemory = @physicalMemory - @overheadMemory;
155+
END;
146156

147157
-- Configure NUMA Affinity
148158
IF (@configureNumaNodeAffinity = 1)
@@ -151,20 +161,21 @@ BEGIN
151161
END;
152162

153163
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]
168179
FROM sys.configurations c
169180
WHERE [c].[name] = N'max server memory (MB)'
170181
OPTION (RECOMPILE);

power_saving_check.sql

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
/*
2+
Power Saving Check
3+
https://bornsql.ca/
4+
Copyright (c) BornSQL.ca
5+
Written by Randolph West, released under the MIT License
6+
Last updated: 19 June 2020
7+
8+
A simple Windows Registry scan, using xp_cmdshell 'powercfg /list'.
9+
This script respects your 'show advanced options' and 'xp_cmdshell'
10+
settings under sp_configure, and will set them back the way it found
11+
them.
12+
13+
*/
14+
115
DECLARE @isCmdShellEnabled BIT;
216
DECLARE @isShowAdvanced BIT;
317

4-
SELECT
5-
@isCmdShellEnabled = CAST(value AS BIT)
6-
FROM
7-
sys.configurations
8-
WHERE
9-
name = 'xp_cmdshell';
10-
11-
SELECT
12-
@isShowAdvanced = CAST(value AS BIT)
13-
FROM
14-
sys.configurations
15-
WHERE
16-
name = 'show advanced options';
18+
SELECT @isCmdShellEnabled = CAST(value AS BIT)
19+
FROM sys.configurations
20+
WHERE name = 'xp_cmdshell';
21+
22+
SELECT @isShowAdvanced = CAST(value AS BIT)
23+
FROM sys.configurations
24+
WHERE name = 'show advanced options';
1725

1826
IF (@isShowAdvanced = 0)
1927
BEGIN

0 commit comments

Comments
 (0)