Skip to content

Commit f56b648

Browse files
2 parents 9f846b1 + 30d58cf commit f56b648

1 file changed

Lines changed: 71 additions & 4 deletions

File tree

sp_PerfCheck/sp_PerfCheck.sql

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
310310
@io_sql nvarchar(max) = N'',
311311
@file_io_sql nvarchar(max) = N'',
312312
@db_size_sql nvarchar(max) = N'',
313-
@tempdb_files_sql nvarchar(max) = N'';
313+
@tempdb_files_sql nvarchar(max) = N'',
314+
/* TempDB pagelatch contention variables */
315+
@pagelatch_wait_hours decimal(20,2),
316+
@server_uptime_hours decimal(20,2),
317+
@pagelatch_ratio_to_uptime decimal(10,4);
314318

315319

316320
/* Check for VIEW SERVER STATE permission */
@@ -926,7 +930,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
926930
url
927931
)
928932
SELECT
929-
check_id = 4103,
933+
check_id = 5103,
930934
priority =
931935
CASE
932936
WHEN
@@ -1984,6 +1988,28 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19841988
ws.wait_time_percent_of_uptime DESC;
19851989
END;
19861990

1991+
/* Calculate pagelatch wait time for TempDB contention check */
1992+
IF @has_view_server_state = 1
1993+
BEGIN
1994+
SELECT
1995+
@pagelatch_wait_hours =
1996+
SUM
1997+
(
1998+
CASE
1999+
WHEN osw.wait_type IN (N'PAGELATCH_UP', N'PAGELATCH_SH', N'PAGELATCH_EX')
2000+
THEN osw.wait_time_ms / 1000.0 / 3600.0
2001+
ELSE 0
2002+
END
2003+
),
2004+
@server_uptime_hours =
2005+
DATEDIFF(SECOND, osi.sqlserver_start_time, GETDATE()) / 3600.0
2006+
FROM sys.dm_os_wait_stats AS osw
2007+
CROSS JOIN sys.dm_os_sys_info AS osi;
2008+
2009+
SET @pagelatch_ratio_to_uptime =
2010+
@pagelatch_wait_hours / NULLIF(@server_uptime_hours, 0) * 100;
2011+
END;
2012+
19872013
/* Check for CPU scheduling pressure (signal wait ratio) */
19882014
IF @has_view_server_state = 1
19892015
BEGIN
@@ -3283,8 +3309,12 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32833309
50, /* High priority */
32843310
N'TempDB Configuration',
32853311
N'Single TempDB Data File',
3286-
N'TempDB has only one data file. Multiple files can reduce allocation page contention. ' +
3287-
N'Recommendation: Use multiple files (equal to number of logical processors up to 8).',
3312+
N'TempDB has only one data file on a ' + CONVERT(nvarchar(10), @processors) +
3313+
N'-core system. This creates allocation contention. Recommendation: Add ' +
3314+
CASE
3315+
WHEN @processors > 8 THEN N'8'
3316+
ELSE CONVERT(nvarchar(10), @processors)
3317+
END + N' data files total.',
32883318
N'https://erikdarling.com/sp_PerfCheck#tempdb'
32893319
);
32903320
END;
@@ -3426,6 +3456,43 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34263456
);
34273457
END;
34283458

3459+
/* Check for TempDB allocation contention based on pagelatch waits */
3460+
IF @tempdb_data_file_count <= @processors
3461+
AND @tempdb_data_file_count < 8
3462+
AND @has_view_server_state = 1
3463+
AND @pagelatch_ratio_to_uptime >= 1.0
3464+
BEGIN
3465+
INSERT INTO
3466+
#results
3467+
(
3468+
check_id,
3469+
priority,
3470+
category,
3471+
finding,
3472+
details,
3473+
url
3474+
)
3475+
VALUES
3476+
(
3477+
2010,
3478+
40, /* High priority */
3479+
N'TempDB Performance',
3480+
N'TempDB Allocation Contention Detected',
3481+
N'Server has spent ' +
3482+
CONVERT(nvarchar(20), CONVERT(decimal(10,2), @pagelatch_wait_hours)) +
3483+
N' hours (' +
3484+
CONVERT(nvarchar(10), CONVERT(decimal(5,2), @pagelatch_ratio_to_uptime)) +
3485+
N'% of uptime) waiting on page latches. TempDB has ' +
3486+
CONVERT(nvarchar(10), @tempdb_data_file_count) +
3487+
N' data files. Consider adding files up to ' +
3488+
CASE
3489+
WHEN @processors > 8 THEN N'8'
3490+
ELSE CONVERT(nvarchar(10), @processors)
3491+
END + N' total to reduce allocation contention.',
3492+
N'https://erikdarling.com/sp_PerfCheck#tempdb-contention'
3493+
);
3494+
END;
3495+
34293496
/* Memory configuration checks */
34303497
IF @min_server_memory >= (@max_server_memory * 0.9) /* Within 10% */
34313498
BEGIN

0 commit comments

Comments
 (0)