Skip to content

Commit 3949dcc

Browse files
Merge pull request #6 from BrentOzarULTD/dev
MERGING HIGH QUALITY CODE
2 parents dd93c1b + 68d5fea commit 3949dcc

File tree

5 files changed

+91
-26
lines changed

5 files changed

+91
-26
lines changed

Documentation/sp_Blitz_Checks_by_Priority.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Before adding a new check, make sure to add a Github issue for it first, and hav
66

77
If you want to change anything about a check - the priority, finding, URL, or ID - open a Github issue first. The relevant scripts have to be updated too.
88

9-
CURRENT HIGH CHECKID: 231.
10-
If you want to add a new one, start at 232.
9+
CURRENT HIGH CHECKID: 232.
10+
If you want to add a new one, start at 233.
1111

1212
| Priority | FindingsGroup | Finding | URL | CheckID |
1313
|----------|-----------------------------|---------------------------------------------------------|------------------------------------------------------------------------|----------|
@@ -284,6 +284,7 @@ If you want to add a new one, start at 232.
284284
| 250 | Server Info | Agent is Currently Offline | | 167 |
285285
| 250 | Server Info | Azure Managed Instance | https://www.BrenOzar.com/go/azurevm | 222 |
286286
| 250 | Server Info | Container | https://www.BrentOzar.com/go/virtual | 214 |
287+
| 250 | Server Info | Data Size | | 232 |
287288
| 250 | Server Info | Default Trace Contents | https://www.BrentOzar.com/go/trace | 106 |
288289
| 250 | Server Info | Drive Space | | 92 |
289290
| 250 | Server Info | Full-text Filter Daemon is Currently Offline | | 168 |

sp_Blitz.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8515,6 +8515,46 @@ IF @ProductVersionMajor >= 10 AND NOT EXISTS ( SELECT 1
85158515
END;
85168516
END;
85178517

8518+
/* CheckID 232 - Server Info - Data Size */
8519+
IF NOT EXISTS ( SELECT 1
8520+
FROM #SkipChecks
8521+
WHERE DatabaseName IS NULL AND CheckID = 232 )
8522+
BEGIN
8523+
8524+
IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 232) WITH NOWAIT;
8525+
8526+
IF OBJECT_ID('tempdb..#MasterFiles') IS NOT NULL
8527+
DROP TABLE #MasterFiles;
8528+
CREATE TABLE #MasterFiles (database_id INT, file_id INT, type_desc NVARCHAR(50), name NVARCHAR(255), physical_name NVARCHAR(255), size BIGINT);
8529+
/* Azure SQL Database doesn't have sys.master_files, so we have to build our own. */
8530+
IF ((SERVERPROPERTY('Edition')) = 'SQL Azure'
8531+
AND (OBJECT_ID('sys.master_files') IS NULL))
8532+
SET @StringToExecute = 'INSERT INTO #MasterFiles (database_id, file_id, type_desc, name, physical_name, size) SELECT DB_ID(), file_id, type_desc, name, physical_name, size FROM sys.database_files;';
8533+
ELSE
8534+
SET @StringToExecute = 'INSERT INTO #MasterFiles (database_id, file_id, type_desc, name, physical_name, size) SELECT database_id, file_id, type_desc, name, physical_name, size FROM sys.master_files;';
8535+
EXEC(@StringToExecute);
8536+
8537+
8538+
INSERT INTO #BlitzResults
8539+
( CheckID ,
8540+
Priority ,
8541+
FindingsGroup ,
8542+
Finding ,
8543+
URL ,
8544+
Details
8545+
)
8546+
SELECT 232 AS CheckID
8547+
,250 AS Priority
8548+
,'Server Info' AS FindingsGroup
8549+
,'Data Size' AS Finding
8550+
,'' AS URL
8551+
,CAST(COUNT(DISTINCT database_id) AS NVARCHAR(100)) + N' databases, ' + CAST(CAST(SUM (CAST(size AS BIGINT)*8./1024./1024.) AS MONEY) AS VARCHAR(100)) + ' GB total file size' as Details
8552+
FROM #MasterFiles
8553+
WHERE database_id > 4;
8554+
8555+
END;
8556+
8557+
85188558
END; /* IF @CheckServerInfo = 1 */
85198559
END; /* IF ( ( SERVERPROPERTY('ServerName') NOT IN ( SELECT ServerName */
85208560

sp_BlitzCache.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4396,7 +4396,7 @@ SET frequent_execution = CASE WHEN ExecutionsPerMinute > @execution_threshold
43964396
AND MinReturnedRows < ((1.0 - (@parameter_sniffing_warning_pct / 100.0)) * AverageReturnedRows) THEN 1
43974397
WHEN ExecutionCount > 3 AND AverageReads > @parameter_sniffing_io_threshold
43984398
AND MaxReturnedRows > ((1.0 + (@parameter_sniffing_warning_pct / 100.0)) * AverageReturnedRows) THEN 1 END ,
4399-
near_parallel = CASE WHEN QueryPlanCost BETWEEN @ctp * (1 - (@ctp_threshold_pct / 100.0)) AND @ctp THEN 1 END,
4399+
near_parallel = CASE WHEN is_parallel <> 1 AND QueryPlanCost BETWEEN @ctp * (1 - (@ctp_threshold_pct / 100.0)) AND @ctp THEN 1 END,
44004400
long_running = CASE WHEN AverageDuration > @long_running_query_warning_seconds THEN 1
44014401
WHEN max_worker_time > @long_running_query_warning_seconds THEN 1
44024402
WHEN max_elapsed_time > @long_running_query_warning_seconds THEN 1 END,

sp_BlitzIndex.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,12 @@ BEGIN;
28352835
N'Unused NC index with High Writes' AS finding,
28362836
[database_name] AS [Database Name],
28372837
N'http://BrentOzar.com/go/IndexHoarder' AS URL,
2838-
N'0 reads: ' + i.db_schema_object_indexid AS details,
2838+
N'Reads: 0,'
2839+
+ N' Writes: '
2840+
+ REPLACE(CONVERT(NVARCHAR(30), CAST((i.user_updates) AS MONEY), 1), N'.00', N'')
2841+
+ N' on: '
2842+
+ i.db_schema_object_indexid
2843+
AS details,
28392844
i.index_definition,
28402845
i.secret_columns,
28412846
i.index_usage_summary,

sp_BlitzLock.sql

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ ALTER PROCEDURE dbo.sp_BlitzLock
1313
@AppName NVARCHAR(256) = NULL,
1414
@HostName NVARCHAR(256) = NULL,
1515
@LoginName NVARCHAR(256) = NULL,
16-
@EventSessionPath VARCHAR(256) = 'system_health*.xel',
16+
@EventSessionPath VARCHAR(256) = 'system_health*.xel',
17+
@VictimsOnly BIT = 0,
1718
@Debug BIT = 0,
1819
@Help BIT = 0,
1920
@Version VARCHAR(30) = NULL OUTPUT,
2021
@VersionDate DATETIME = NULL OUTPUT,
2122
@VersionCheckMode BIT = 0,
2223
@OutputDatabaseName NVARCHAR(256) = NULL ,
23-
@OutputSchemaName NVARCHAR(256) = 'dbo' , --dito as below
24+
@OutputSchemaName NVARCHAR(256) = 'dbo' , --ditto as below
2425
@OutputTableName NVARCHAR(256) = 'BlitzLock' --put a standard here no need to check later in the script
2526
)
2627
WITH RECOMPILE
@@ -184,6 +185,7 @@ You need to use an Azure storage account, and the path has to look like this: ht
184185
finding_group NVARCHAR(100),
185186
finding NVARCHAR(4000)
186187
);
188+
187189
DECLARE @d VARCHAR(40), @StringToExecute NVARCHAR(4000),@StringToExecuteParams NVARCHAR(500),@r NVARCHAR(200),@OutputTableFindings NVARCHAR(100);
188190
DECLARE @ServerName NVARCHAR(256)
189191
DECLARE @OutputDatabaseCheck BIT;
@@ -296,7 +298,9 @@ You need to use an Azure storage account, and the path has to look like this: ht
296298
AND LEFT(CAST(SERVERPROPERTY('MachineName') AS VARCHAR(8000)), 8) <> 'EC2AMAZ-'
297299
AND LEFT(CAST(SERVERPROPERTY('ServerName') AS VARCHAR(8000)), 8) <> 'EC2AMAZ-'
298300
AND db_id('rdsadmin') IS NULL
299-
UPDATE STATISTICS #t WITH ROWCOUNT = 100000000, PAGECOUNT = 100000000;
301+
BEGIN
302+
UPDATE STATISTICS #t WITH ROWCOUNT = 100000000, PAGECOUNT = 100000000;
303+
END
300304

301305
/*Grab the initial set of XML to parse*/
302306
SET @d = CONVERT(VARCHAR(40), GETDATE(), 109);
@@ -342,7 +346,7 @@ You need to use an Azure storage account, and the path has to look like this: ht
342346
ISNULL(ca2.ib.query('.'), '') AS input_buffer
343347
INTO #deadlock_process
344348
FROM ( SELECT dd.deadlock_xml,
345-
CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, dd.event_date ), DATENAME(TzOffset, SYSDATETIMEOFFSET()))) AS event_date,
349+
CONVERT(DATETIME2(7), SWITCHOFFSET(CONVERT(datetimeoffset, dd.event_date ), DATENAME(TzOffset, SYSDATETIMEOFFSET()))) AS event_date,
346350
dd.victim_id,
347351
dd.deadlock_graph,
348352
ca.dp.value('@id', 'NVARCHAR(256)') AS id,
@@ -373,7 +377,8 @@ You need to use an Azure storage account, and the path has to look like this: ht
373377
AND (ca.dp.value('@hostname', 'NVARCHAR(256)') = @HostName OR @HostName IS NULL)
374378
AND (ca.dp.value('@loginname', 'NVARCHAR(256)') = @LoginName OR @LoginName IS NULL)
375379
) AS q
376-
CROSS APPLY q.deadlock_xml.nodes('//deadlock/process-list/process/inputbuf') AS ca2(ib);
380+
CROSS APPLY q.deadlock_xml.nodes('//deadlock/process-list/process/inputbuf') AS ca2(ib)
381+
OPTION ( RECOMPILE );
377382

378383

379384
/*Parse execution stack XML*/
@@ -550,6 +555,12 @@ You need to use an Azure storage account, and the path has to look like this: ht
550555
CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l)
551556
OPTION ( RECOMPILE );
552557

558+
UPDATE d
559+
SET d.index_name = d.object_name
560+
+ '.HEAP'
561+
FROM #deadlock_owner_waiter AS d
562+
WHERE index_name IS NULL
563+
OPTION(RECOMPILE);
553564

554565
/*Parse parallel deadlocks*/
555566
SET @d = CONVERT(VARCHAR(40), GETDATE(), 109);
@@ -600,7 +611,8 @@ You need to use an Azure storage account, and the path has to look like this: ht
600611
FROM #deadlock_resource_parallel AS drp
601612
)
602613
DELETE FROM c
603-
WHERE c.rn > 1;
614+
WHERE c.rn > 1
615+
OPTION ( RECOMPILE );
604616

605617

606618
/*Get rid of nonsense*/
@@ -666,7 +678,8 @@ You need to use an Azure storage account, and the path has to look like this: ht
666678
) AS step_id
667679
FROM #deadlock_process AS dp
668680
WHERE dp.client_app LIKE 'SQLAgent - %'
669-
) AS x;
681+
) AS x
682+
OPTION ( RECOMPILE );
670683

671684

672685
ALTER TABLE #agent_job ADD job_name NVARCHAR(256),
@@ -682,7 +695,8 @@ You need to use an Azure storage account, and the path has to look like this: ht
682695
ON j.job_id = s.job_id
683696
JOIN #agent_job AS aj
684697
ON aj.job_id_guid = j.job_id
685-
AND aj.step_id = s.step_id;';
698+
AND aj.step_id = s.step_id
699+
OPTION ( RECOMPILE );';
686700
EXEC(@StringToExecute);
687701
END
688702

@@ -699,7 +713,8 @@ You need to use an Azure storage account, and the path has to look like this: ht
699713
JOIN #agent_job AS aj
700714
ON dp.event_date = aj.event_date
701715
AND dp.victim_id = aj.victim_id
702-
AND dp.id = aj.id;
716+
AND dp.id = aj.id
717+
OPTION ( RECOMPILE );
703718

704719
/*Begin checks based on parsed values*/
705720

@@ -1067,7 +1082,7 @@ You need to use an Azure storage account, and the path has to look like this: ht
10671082
GROUP BY PARSENAME(dow.object_name, 3), dow.object_name
10681083
)
10691084
INSERT #deadlock_findings WITH (TABLOCKX)
1070-
( check_id, database_name, object_name, finding_group, finding )
1085+
( check_id, database_name, object_name, finding_group, finding )
10711086
SELECT 10 AS check_id,
10721087
cs.database_name,
10731088
cs.object_name,
@@ -1124,7 +1139,8 @@ You need to use an Azure storage account, and the path has to look like this: ht
11241139
'Agent Job Deadlocks',
11251140
RTRIM(COUNT(*)) + ' deadlocks from this Agent Job and Step'
11261141
FROM #agent_job AS aj
1127-
GROUP BY DB_NAME(aj.database_id), aj.job_name, aj.step_name;
1142+
GROUP BY DB_NAME(aj.database_id), aj.job_name, aj.step_name
1143+
OPTION ( RECOMPILE );
11281144

11291145
/*Thank you goodnight*/
11301146
INSERT #deadlock_findings WITH (TABLOCKX)
@@ -1159,14 +1175,14 @@ You need to use an Azure storage account, and the path has to look like this: ht
11591175
dp.wait_resource COLLATE DATABASE_DEFAULT AS wait_resource,
11601176
CONVERT(
11611177
XML,
1162-
STUFF(( SELECT DISTINCT NCHAR(10)
1178+
STUFF(( SELECT DISTINCT NCHAR(10)
11631179
+ N' <object>'
11641180
+ ISNULL(c.object_name, N'')
11651181
+ N'</object> ' COLLATE DATABASE_DEFAULT AS object_name
11661182
FROM #deadlock_owner_waiter AS c
1167-
WHERE (dp.id = c.owner_id
1168-
OR dp.victim_id = c.waiter_id)
1169-
AND dp.event_date = c.event_date
1183+
WHERE ( dp.id = c.owner_id
1184+
OR dp.victim_id = c.waiter_id )
1185+
AND CONVERT(DATE, dp.event_date) = CONVERT(DATE, c.event_date)
11701186
FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(4000)'),
11711187
1, 1, N'')) AS object_names,
11721188
dp.wait_time,
@@ -1229,7 +1245,7 @@ You need to use an Azure storage account, and the path has to look like this: ht
12291245
ROW_NUMBER() OVER ( PARTITION BY dp.event_date, dp.id ORDER BY dp.event_date ) AS dn,
12301246
DENSE_RANK() OVER ( ORDER BY dp.event_date ) AS en,
12311247
ROW_NUMBER() OVER ( PARTITION BY dp.event_date ORDER BY dp.event_date ) -1 AS qn,
1232-
NULL AS is_victim,
1248+
1 AS is_victim,
12331249
cao.wait_type COLLATE DATABASE_DEFAULT AS owner_mode,
12341250
cao.waiter_type AS owner_waiter_type,
12351251
cao.owner_activity AS owner_activity,
@@ -1328,6 +1344,7 @@ You need to use an Azure storage account, and the path has to look like this: ht
13281344
d.deadlock_graph
13291345
FROM deadlocks AS d
13301346
WHERE d.dn = 1
1347+
AND (is_victim = @VictimsOnly OR @VictimsOnly = 0)
13311348
AND d.en < CASE WHEN d.deadlock_type = N'Parallel Deadlock' THEN 2 ELSE 2147483647 END
13321349
AND (DB_NAME(d.database_id) = @DatabaseName OR @DatabaseName IS NULL)
13331350
AND (d.event_date >= @StartDate OR @StartDate IS NULL)
@@ -1370,14 +1387,14 @@ ELSE --Output to database is not set output to client app
13701387
dp.wait_resource COLLATE DATABASE_DEFAULT AS wait_resource,
13711388
CONVERT(
13721389
XML,
1373-
STUFF(( SELECT DISTINCT NCHAR(10)
1390+
STUFF(( SELECT DISTINCT NCHAR(10)
13741391
+ N' <object>'
13751392
+ ISNULL(c.object_name, N'')
13761393
+ N'</object> ' COLLATE DATABASE_DEFAULT AS object_name
13771394
FROM #deadlock_owner_waiter AS c
1378-
WHERE (dp.id = c.owner_id
1379-
OR dp.victim_id = c.waiter_id)
1380-
AND dp.event_date = c.event_date
1395+
WHERE ( dp.id = c.owner_id
1396+
OR dp.victim_id = c.waiter_id )
1397+
AND CONVERT(DATE, dp.event_date) = CONVERT(DATE, c.event_date)
13811398
FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(4000)'),
13821399
1, 1, N'')) AS object_names,
13831400
dp.wait_time,
@@ -1440,7 +1457,7 @@ ELSE --Output to database is not set output to client app
14401457
ROW_NUMBER() OVER ( PARTITION BY dp.event_date, dp.id ORDER BY dp.event_date ) AS dn,
14411458
DENSE_RANK() OVER ( ORDER BY dp.event_date ) AS en,
14421459
ROW_NUMBER() OVER ( PARTITION BY dp.event_date ORDER BY dp.event_date ) -1 AS qn,
1443-
NULL AS is_victim,
1460+
1 AS is_victim,
14441461
cao.wait_type COLLATE DATABASE_DEFAULT AS owner_mode,
14451462
cao.waiter_type AS owner_waiter_type,
14461463
cao.owner_activity AS owner_activity,
@@ -1460,7 +1477,8 @@ ELSE --Output to database is not set output to client app
14601477
CROSS APPLY (SELECT TOP 1 * FROM #deadlock_resource_parallel AS drp WHERE drp.owner_id = dp.id AND drp.wait_type = 'e_waitPipeNewRow' ORDER BY drp.event_date) AS cao
14611478
CROSS APPLY (SELECT TOP 1 * FROM #deadlock_resource_parallel AS drp WHERE drp.owner_id = dp.id AND drp.wait_type = 'e_waitPipeGetRow' ORDER BY drp.event_date) AS caw
14621479
WHERE dp.victim_id IS NULL
1463-
AND dp.login_name IS NOT NULL)
1480+
AND dp.login_name IS NOT NULL
1481+
)
14641482
SELECT d.deadlock_type,
14651483
d.event_date,
14661484
DB_NAME(d.database_id) AS database_name,
@@ -1502,6 +1520,7 @@ ELSE --Output to database is not set output to client app
15021520
d.deadlock_graph
15031521
FROM deadlocks AS d
15041522
WHERE d.dn = 1
1523+
AND (is_victim = @VictimsOnly OR @VictimsOnly = 0)
15051524
AND d.en < CASE WHEN d.deadlock_type = N'Parallel Deadlock' THEN 2 ELSE 2147483647 END
15061525
AND (DB_NAME(d.database_id) = @DatabaseName OR @DatabaseName IS NULL)
15071526
AND (d.event_date >= @StartDate OR @StartDate IS NULL)

0 commit comments

Comments
 (0)