Skip to content

Commit a96255b

Browse files
committed
Update SQL Server Diagnostic Queries to 2020-12
1 parent 5066ff8 commit a96255b

11 files changed

+445
-467
lines changed

Scripts/SQL Managed Instance Diagnostic Information Queries.sql

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
-- SQL Managed Instance Diagnostic Information Queries
23
-- Glenn Berry
3-
-- Last Modified: September 1, 2020
4+
-- Last Modified: December 3, 2020
45
-- https://glennsqlperformance.com/
56
-- https://sqlserverperformance.wordpress.com/
67
-- YouTube: https://bit.ly/2PkoAM1
@@ -333,7 +334,6 @@ ORDER BY vs.volume_mount_point OPTION (RECOMPILE);
333334

334335

335336
-- Drive level latency information (Query 14) (Drive Level Latency)
336-
-- Based on code from Jimmy May
337337
SELECT tab.[Drive], tab.volume_mount_point AS [Volume Mount Point],
338338
CASE
339339
WHEN num_of_reads = 0 THEN 0
@@ -443,18 +443,25 @@ db.[compatibility_level] AS [DB Compatibility Level],
443443
db.is_mixed_page_allocation_on, db.page_verify_option_desc AS [Page Verify Option],
444444
db.is_auto_create_stats_on, db.is_auto_update_stats_on, db.is_auto_update_stats_async_on, db.is_parameterization_forced,
445445
db.snapshot_isolation_state_desc, db.is_read_committed_snapshot_on, db.is_auto_close_on, db.is_auto_shrink_on,
446-
db.target_recovery_time_in_seconds, db.is_cdc_enabled,
447-
db.delayed_durability_desc, db.is_auto_create_stats_incremental_on,
448-
db.is_query_store_on, db.is_sync_with_backup, db.is_temporal_history_retention_enabled,
449-
db.is_supplemental_logging_enabled,
450-
db.is_encrypted, de.encryption_state, de.percent_complete, de.key_algorithm, de.key_length, db.resource_pool_id,
451-
db.is_tempdb_spill_to_remote_store, db.is_result_set_caching_on, db.is_accelerated_database_recovery_on
446+
db.target_recovery_time_in_seconds, db.is_cdc_enabled, db.delayed_durability_desc,
447+
db.is_query_store_on, db.is_sync_with_backup, db.is_temporal_history_retention_enabled,
448+
db.is_encrypted, de.encryption_state, de.percent_complete, de.key_algorithm, de.key_length,
449+
db.is_accelerated_database_recovery_on
452450
FROM sys.databases AS db WITH (NOLOCK)
453451
LEFT OUTER JOIN sys.dm_database_encryption_keys AS de WITH (NOLOCK)
454452
ON db.database_id = de.database_id
455453
ORDER BY db.[name] OPTION (RECOMPILE);
456454
------
457455

456+
-- sys.databases (Transact-SQL)
457+
-- https://bit.ly/2G5wqaX
458+
459+
-- sys.dm_os_performance_counters (Transact-SQL)
460+
-- https://bit.ly/3kEO2JR
461+
462+
-- sys.dm_database_encryption_keys (Transact-SQL)
463+
-- https://bit.ly/3mE7kkx
464+
458465
-- Things to look at:
459466
-- How many databases are on the instance?
460467
-- What recovery models are they using?
@@ -826,7 +833,7 @@ AND counter_name = N'Page life expectancy' OPTION (RECOMPILE);
826833
-- Higher PLE is better. Watch the trend over time, not the absolute value
827834
-- This will only return one row for non-NUMA systems
828835

829-
-- Page Life Expectancy isnt what you think
836+
-- Page Life Expectancy isn’t what you think…
830837
-- https://bit.ly/2EgynLa
831838

832839

@@ -1450,7 +1457,8 @@ ORDER BY ps.avg_fragmentation_in_percent DESC OPTION (RECOMPILE);
14501457

14511458

14521459
--- Index Read/Write stats (all tables in current DB) ordered by Reads (Query 59) (Overall Index Usage - Reads)
1453-
SELECT OBJECT_NAME(i.[object_id]) AS [ObjectName], i.[name] AS [IndexName], i.index_id,
1460+
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName], OBJECT_NAME(i.[object_id]) AS [ObjectName],
1461+
i.[name] AS [IndexName], i.index_id,
14541462
s.user_seeks, s.user_scans, s.user_lookups,
14551463
s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
14561464
s.user_updates AS [Writes],
@@ -1461,6 +1469,8 @@ LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
14611469
ON i.[object_id] = s.[object_id]
14621470
AND i.index_id = s.index_id
14631471
AND s.database_id = DB_ID()
1472+
LEFT OUTER JOIN sys.tables AS t WITH (NOLOCK)
1473+
ON t.[object_id] = i.[object_id]
14641474
WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1
14651475
ORDER BY s.user_seeks + s.user_scans + s.user_lookups DESC OPTION (RECOMPILE); -- Order by reads
14661476
------
@@ -1469,7 +1479,8 @@ ORDER BY s.user_seeks + s.user_scans + s.user_lookups DESC OPTION (RECOMPILE); -
14691479

14701480

14711481
--- Index Read/Write stats (all tables in current DB) ordered by Writes (Query 60) (Overall Index Usage - Writes)
1472-
SELECT OBJECT_NAME(i.[object_id]) AS [ObjectName], i.[name] AS [IndexName], i.index_id,
1482+
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName],OBJECT_NAME(i.[object_id]) AS [ObjectName],
1483+
i.[name] AS [IndexName], i.index_id,
14731484
s.user_updates AS [Writes], s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
14741485
i.[type_desc] AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,
14751486
s.last_system_update, s.last_user_update
@@ -1478,9 +1489,10 @@ LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
14781489
ON i.[object_id] = s.[object_id]
14791490
AND i.index_id = s.index_id
14801491
AND s.database_id = DB_ID()
1492+
LEFT OUTER JOIN sys.tables AS t WITH (NOLOCK)
1493+
ON t.[object_id] = i.[object_id]
14811494
WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1
14821495
ORDER BY s.user_updates DESC OPTION (RECOMPILE); -- Order by writes
1483-
------
14841496

14851497
-- Show which indexes in the current database are most active for Writes
14861498

@@ -1654,30 +1666,10 @@ ORDER BY bs.backup_finish_date DESC OPTION (RECOMPILE);
16541666
-- https://bit.ly/28Rpb2x
16551667

16561668

1657-
-- These six Pluralsight Courses go into more detail about how to run these queries and interpret the results
1658-
1659-
-- Azure SQL Database: Diagnosing Performance Issues with DMVs
1660-
-- https://bit.ly/2meDRCN
1661-
1662-
-- SQL Server 2017: Diagnosing Performance Issues with DMVs
1663-
-- https://bit.ly/2FqCeti
1664-
1665-
-- SQL Server 2017: Diagnosing Configuration Issues with DMVs
1666-
-- https://bit.ly/2MSUDUL
1667-
1668-
-- SQL Server 2014 DMV Diagnostic Queries Part 1
1669-
-- https://bit.ly/2plxCer
1670-
1671-
-- SQL Server 2014 DMV Diagnostic Queries Part 2
1672-
-- https://bit.ly/2IuJpzI
1673-
1674-
-- SQL Server 2014 DMV Diagnostic Queries Part 3
1675-
-- https://bit.ly/2FIlCPb
1676-
1677-
1678-
16791669
-- Microsoft Visual Studio Dev Essentials
16801670
-- https://bit.ly/2qjNRxi
16811671

16821672
-- Microsoft Azure Learn
1683-
-- https://bit.ly/2O0Hacc
1673+
-- https://bit.ly/2O0Hacc
1674+
1675+

Scripts/SQL Server 2005 Diagnostic Information Queries.sql

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
-- SQL Server 2005 Diagnostic Information Queries
23
-- Glenn Berry
3-
-- Last Modified: September 1, 2020
4+
-- Last Modified: December 3, 2020
45
-- https://glennsqlperformance.com/
56
-- https://sqlserverperformance.wordpress.com/
67
-- YouTube: https://bit.ly/2PkoAM1
@@ -358,6 +359,15 @@ AND ls.counter_name LIKE N'Log File(s) Size (KB)%'
358359
AND ls.cntr_value > 0 OPTION (RECOMPILE);
359360
------
360361

362+
-- sys.databases (Transact-SQL)
363+
-- https://bit.ly/2G5wqaX
364+
365+
-- sys.dm_os_performance_counters (Transact-SQL)
366+
-- https://bit.ly/3kEO2JR
367+
368+
-- sys.dm_database_encryption_keys (Transact-SQL)
369+
-- https://bit.ly/3mE7kkx
370+
361371
-- Things to look at:
362372
-- How many databases are on the instance?
363373
-- What recovery models are they using?
@@ -668,7 +678,7 @@ AND counter_name = N'Page life expectancy' OPTION (RECOMPILE);
668678
-- Higher PLE is better. Watch the trend over time, not the absolute value.
669679
-- This will only return one row for non-NUMA systems.
670680

671-
-- Page Life Expectancy isnt what you think
681+
-- Page Life Expectancy isn’t what you think…
672682
-- https://www.sqlskills.com/blogs/paul/page-life-expectancy-isnt-what-you-think/
673683

674684

@@ -1072,32 +1082,41 @@ ORDER BY ps.avg_fragmentation_in_percent DESC OPTION (RECOMPILE);
10721082

10731083

10741084
--- Index Read/Write stats (all tables in current DB) ordered by Reads (Query 48) (Overall Index Usage - Reads)
1075-
SELECT OBJECT_NAME(s.[object_id]) AS [ObjectName], i.name AS [IndexName], i.index_id,
1076-
user_seeks + user_scans + user_lookups AS [Reads], s.user_updates AS [Writes],
1077-
i.type_desc AS [IndexType], i.fill_factor AS [FillFactor]
1078-
FROM sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1079-
INNER JOIN sys.indexes AS i WITH (NOLOCK)
1080-
ON s.[object_id] = i.[object_id]
1081-
WHERE OBJECTPROPERTY(s.[object_id],'IsUserTable') = 1
1085+
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName], OBJECT_NAME(i.[object_id]) AS [ObjectName],
1086+
i.[name] AS [IndexName], i.index_id,
1087+
s.user_seeks, s.user_scans, s.user_lookups,
1088+
s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
1089+
s.user_updates AS [Writes],
1090+
i.[type_desc] AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,
1091+
s.last_user_scan, s.last_user_lookup, s.last_user_seek
1092+
FROM sys.indexes AS i WITH (NOLOCK)
1093+
LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1094+
ON i.[object_id] = s.[object_id]
10821095
AND i.index_id = s.index_id
10831096
AND s.database_id = DB_ID()
1084-
ORDER BY user_seeks + user_scans + user_lookups DESC OPTION (RECOMPILE); -- Order by reads
1097+
LEFT OUTER JOIN sys.tables AS t WITH (NOLOCK)
1098+
ON t.[object_id] = i.[object_id]
1099+
WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1
1100+
ORDER BY s.user_seeks + s.user_scans + s.user_lookups DESC OPTION (RECOMPILE); -- Order by reads
10851101
------
10861102

10871103
-- Show which indexes in the current database are most active for Reads
10881104

10891105

10901106
--- Index Read/Write stats (all tables in current DB) ordered by Writes (Query 49) (Overall Index Usage - Writes)
1091-
SELECT OBJECT_NAME(s.[object_id]) AS [ObjectName], i.name AS [IndexName], i.index_id,
1092-
s.user_updates AS [Writes], user_seeks + user_scans + user_lookups AS [Reads],
1093-
i.type_desc AS [IndexType], i.fill_factor AS [FillFactor],
1107+
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName],OBJECT_NAME(i.[object_id]) AS [ObjectName],
1108+
i.[name] AS [IndexName], i.index_id,
1109+
s.user_updates AS [Writes], s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
1110+
i.[type_desc] AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,
10941111
s.last_system_update, s.last_user_update
1095-
FROM sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1096-
INNER JOIN sys.indexes AS i WITH (NOLOCK)
1097-
ON s.[object_id] = i.[object_id]
1098-
WHERE OBJECTPROPERTY(s.[object_id],'IsUserTable') = 1
1112+
FROM sys.indexes AS i WITH (NOLOCK)
1113+
LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1114+
ON i.[object_id] = s.[object_id]
10991115
AND i.index_id = s.index_id
11001116
AND s.database_id = DB_ID()
1117+
LEFT OUTER JOIN sys.tables AS t WITH (NOLOCK)
1118+
ON t.[object_id] = i.[object_id]
1119+
WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1
11011120
ORDER BY s.user_updates DESC OPTION (RECOMPILE); -- Order by writes
11021121
------
11031122

@@ -1155,5 +1174,6 @@ ORDER BY bs.backup_finish_date DESC OPTION (RECOMPILE);
11551174
-- Microsoft Azure Learn
11561175
-- https://bit.ly/2O0Hacc
11571176

1158-
-- August 2017 blog series about upgrading and migrating to SQL Server 2016/2017
1159-
-- https://bit.ly/2ftKVrX
1177+
1178+
1179+

Scripts/SQL Server 2008 Diagnostic Information Queries.sql

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
-- SQL Server 2008 Diagnostic Information Queries
23
-- Glenn Berry
3-
-- Last Modified: September 1, 2020
4+
-- Last Modified: December 3, 2020
45
-- https://glennsqlperformance.com/
56
-- https://sqlserverperformance.wordpress.com/
67
-- YouTube: https://bit.ly/2PkoAM1
@@ -411,6 +412,15 @@ AND ls.counter_name LIKE N'Log File(s) Size (KB)%'
411412
AND ls.cntr_value > 0 OPTION (RECOMPILE);
412413
------
413414

415+
-- sys.databases (Transact-SQL)
416+
-- https://bit.ly/2G5wqaX
417+
418+
-- sys.dm_os_performance_counters (Transact-SQL)
419+
-- https://bit.ly/3kEO2JR
420+
421+
-- sys.dm_database_encryption_keys (Transact-SQL)
422+
-- https://bit.ly/3mE7kkx
423+
414424
-- Things to look at:
415425
-- How many databases are on the instance?
416426
-- What recovery models are they using?
@@ -770,7 +780,7 @@ AND counter_name = N'Page life expectancy' OPTION (RECOMPILE);
770780
-- Higher PLE is better. Watch the trend over time, not the absolute value.
771781
-- This will only return one row for non-NUMA systems.
772782

773-
-- Page Life Expectancy isnt what you think
783+
-- Page Life Expectancy isn’t what you think…
774784
-- http://www.sqlskills.com/blogs/paul/page-life-expectancy-isnt-what-you-think/
775785

776786

@@ -1209,34 +1219,42 @@ ORDER BY ps.avg_fragmentation_in_percent DESC OPTION (RECOMPILE);
12091219

12101220

12111221
--- Index Read/Write stats (all tables in current DB) ordered by Reads (Query 56) (Overall Index Usage - Reads)
1212-
SELECT OBJECT_NAME(s.[object_id]) AS [ObjectName], i.name AS [IndexName], i.index_id,
1213-
user_seeks + user_scans + user_lookups AS [Reads], s.user_updates AS [Writes],
1214-
i.type_desc AS [IndexType], i.fill_factor AS [FillFactor], i.has_filter, i.filter_definition,
1222+
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName], OBJECT_NAME(i.[object_id]) AS [ObjectName],
1223+
i.[name] AS [IndexName], i.index_id,
1224+
s.user_seeks, s.user_scans, s.user_lookups,
1225+
s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
1226+
s.user_updates AS [Writes],
1227+
i.[type_desc] AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,
12151228
s.last_user_scan, s.last_user_lookup, s.last_user_seek
1216-
FROM sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1217-
INNER JOIN sys.indexes AS i WITH (NOLOCK)
1218-
ON s.[object_id] = i.[object_id]
1219-
WHERE OBJECTPROPERTY(s.[object_id],'IsUserTable') = 1
1229+
FROM sys.indexes AS i WITH (NOLOCK)
1230+
LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1231+
ON i.[object_id] = s.[object_id]
12201232
AND i.index_id = s.index_id
12211233
AND s.database_id = DB_ID()
1222-
ORDER BY user_seeks + user_scans + user_lookups DESC OPTION (RECOMPILE); -- Order by reads
1234+
LEFT OUTER JOIN sys.tables AS t WITH (NOLOCK)
1235+
ON t.[object_id] = i.[object_id]
1236+
WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1
1237+
ORDER BY s.user_seeks + s.user_scans + s.user_lookups DESC OPTION (RECOMPILE); -- Order by reads
12231238
------
12241239

12251240

12261241
-- Show which indexes in the current database are most active for Reads
12271242

12281243

12291244
--- Index Read/Write stats (all tables in current DB) ordered by Writes (Query 57) (Overall Index Usage - Writes)
1230-
SELECT OBJECT_NAME(s.[object_id]) AS [ObjectName], i.name AS [IndexName], i.index_id,
1231-
s.user_updates AS [Writes], user_seeks + user_scans + user_lookups AS [Reads],
1232-
i.type_desc AS [IndexType], i.fill_factor AS [FillFactor], i.has_filter, i.filter_definition,
1245+
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName],OBJECT_NAME(i.[object_id]) AS [ObjectName],
1246+
i.[name] AS [IndexName], i.index_id,
1247+
s.user_updates AS [Writes], s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
1248+
i.[type_desc] AS [Index Type], i.fill_factor AS [Fill Factor], i.has_filter, i.filter_definition,
12331249
s.last_system_update, s.last_user_update
1234-
FROM sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1235-
INNER JOIN sys.indexes AS i WITH (NOLOCK)
1236-
ON s.[object_id] = i.[object_id]
1237-
WHERE OBJECTPROPERTY(s.[object_id],'IsUserTable') = 1
1250+
FROM sys.indexes AS i WITH (NOLOCK)
1251+
LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s WITH (NOLOCK)
1252+
ON i.[object_id] = s.[object_id]
12381253
AND i.index_id = s.index_id
12391254
AND s.database_id = DB_ID()
1255+
LEFT OUTER JOIN sys.tables AS t WITH (NOLOCK)
1256+
ON t.[object_id] = i.[object_id]
1257+
WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1
12401258
ORDER BY s.user_updates DESC OPTION (RECOMPILE); -- Order by writes
12411259
------
12421260

@@ -1290,33 +1308,9 @@ ORDER BY bs.backup_finish_date DESC OPTION (RECOMPILE);
12901308
-- Have you done any backup tuning with striped backups, or changing the parameters of the backup command?
12911309

12921310

1293-
-- These six Pluralsight Courses go into more detail about how to run these queries and interpret the results
1294-
1295-
-- Azure SQL Database: Diagnosing Performance Issues with DMVs
1296-
-- https://bit.ly/2meDRCN
1297-
1298-
-- SQL Server 2017: Diagnosing Performance Issues with DMVs
1299-
-- https://bit.ly/2FqCeti
1300-
1301-
-- SQL Server 2017: Diagnosing Configuration Issues with DMVs
1302-
-- https://bit.ly/2MSUDUL
1303-
1304-
-- SQL Server 2014 DMV Diagnostic Queries Part 1
1305-
-- https://bit.ly/2plxCer
1306-
1307-
-- SQL Server 2014 DMV Diagnostic Queries Part 2
1308-
-- https://bit.ly/2IuJpzI
1309-
1310-
-- SQL Server 2014 DMV Diagnostic Queries Part 3
1311-
-- https://bit.ly/2FIlCPb
1312-
1313-
1314-
13151311
-- Microsoft Visual Studio Dev Essentials
13161312
-- https://bit.ly/2qjNRxi
13171313

13181314
-- Microsoft Azure Learn
13191315
-- https://bit.ly/2O0Hacc
13201316

1321-
-- August 2017 blog series about upgrading and migrating to SQL Server 2016/2017
1322-
-- https://bit.ly/2ftKVrX

0 commit comments

Comments
 (0)