Skip to content

Commit 5dc3bf2

Browse files
update version
1 parent 27a62ab commit 5dc3bf2

6 files changed

+119
-82
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cf92feb4fd030401766b91d875799c5493a3ebc1
1+
70a62b01171b822ba6443722bf66f9df41ccc5ef

bin/diagnosticquery/SQLServerDiagnosticQueries_2016SP2.sql

+49-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
-- SQL Server 2016 SP2 Diagnostic Information Queries
33
-- Glenn Berry
4-
-- Last Modified: September 1, 2022
4+
-- Last Modified: September 6, 2022
55
-- https://glennsqlperformance.com/
66
-- https://sqlserverperformance.wordpress.com/
77
-- YouTube: https://bit.ly/2PkoAM1
@@ -1339,13 +1339,26 @@ ORDER BY total_worker_time DESC OPTION (RECOMPILE);
13391339
-- Showplan Enhancements for UDFs
13401340
-- https://bit.ly/2LVqiQ1
13411341

1342+
1343+
-- Look for long duration buffer pool scans (Query 50) (Long Buffer Pool Scans)
1344+
EXEC sys.xp_readerrorlog 0, 1, N'Buffer pool scan took';
1345+
------
1346+
1347+
-- Finds buffer pool scans that took more than 10 seconds in the current SQL Server Error log
1348+
-- Only in SQL Server 2016 SP3 and later
1349+
1350+
-- Operations that trigger buffer pool scan may run slowly on large-memory computers - SQL Server | Microsoft Docs
1351+
-- https://bit.ly/3QrFC81
1352+
1353+
1354+
13421355
-- Database specific queries *****************************************************************
13431356

13441357
-- **** Please switch to a user database that you are interested in! *****
13451358
--USE YourDatabaseName; -- make sure to change to an actual database on your instance, not the master system database
13461359
--GO
13471360

1348-
-- Individual File Sizes and space available for current database (Query 50) (File Sizes and Space)
1361+
-- Individual File Sizes and space available for current database (Query 51) (File Sizes and Space)
13491362
SELECT f.name AS [File Name] , f.physical_name AS [Physical Name],
13501363
CAST((f.size/128.0) AS DECIMAL(15,2)) AS [Total Size in MB],
13511364
CAST(f.size/128.0 - CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS int)/128.0 AS DECIMAL(15,2))
@@ -1370,7 +1383,7 @@ ORDER BY f.[file_id] OPTION (RECOMPILE);
13701383
-- https://bit.ly/2evRZSR
13711384

13721385

1373-
-- Log space usage for current database (Query 51) (Log Space Usage)
1386+
-- Log space usage for current database (Query 52) (Log Space Usage)
13741387
SELECT DB_NAME(lsu.database_id) AS [Database Name], db.recovery_model_desc AS [Recovery Model],
13751388
CAST(lsu.total_log_size_in_bytes/1048576.0 AS DECIMAL(10, 2)) AS [Total Log Space (MB)],
13761389
CAST(lsu.used_log_space_in_bytes/1048576.0 AS DECIMAL(10, 2)) AS [Used Log Space (MB)],
@@ -1389,7 +1402,7 @@ OPTION (RECOMPILE);
13891402
-- https://bit.ly/2H4MQw9
13901403

13911404

1392-
-- Status of last VLF for current database (Query 52) (Last VLF Status)
1405+
-- Status of last VLF for current database (Query 53) (Last VLF Status)
13931406
SELECT TOP(1) DB_NAME(li.database_id) AS [Database Name], li.[file_id],
13941407
li.vlf_size_mb, li.vlf_sequence_number, li.vlf_active, li.vlf_status
13951408
FROM sys.dm_db_log_info(DB_ID()) AS li
@@ -1408,7 +1421,7 @@ ORDER BY vlf_sequence_number DESC OPTION (RECOMPILE);
14081421

14091422

14101423

1411-
-- Get database scoped configuration values for current database (Query 53) (Database-scoped Configurations)
1424+
-- Get database scoped configuration values for current database (Query 54) (Database-scoped Configurations)
14121425
SELECT configuration_id, name, [value] AS [value_for_primary], value_for_secondary
14131426
FROM sys.database_scoped_configurations WITH (NOLOCK) OPTION (RECOMPILE);
14141427
------
@@ -1422,7 +1435,7 @@ FROM sys.database_scoped_configurations WITH (NOLOCK) OPTION (RECOMPILE);
14221435
-- https://bit.ly/2sOH7nb
14231436

14241437

1425-
-- I/O Statistics by file for the current database (Query 54) (IO Stats By File)
1438+
-- I/O Statistics by file for the current database (Query 55) (IO Stats By File)
14261439
SELECT DB_NAME(DB_ID()) AS [Database Name], df.name AS [Logical Name], vfs.[file_id], df.type_desc,
14271440
df.physical_name AS [Physical Name], CAST(vfs.size_on_disk_bytes/1048576.0 AS DECIMAL(15, 2)) AS [Size on Disk (MB)],
14281441
vfs.num_of_reads, vfs.num_of_writes, vfs.io_stall_read_ms, vfs.io_stall_write_ms,
@@ -1445,7 +1458,7 @@ ON vfs.[file_id]= df.[file_id] OPTION (RECOMPILE);
14451458

14461459

14471460

1448-
-- Get most frequently executed queries for this database (Query 55) (Query Execution Counts)
1461+
-- Get most frequently executed queries for this database (Query 56) (Query Execution Counts)
14491462
SELECT TOP(50) LEFT(t.[text], 50) AS [Short Query Text], qs.execution_count AS [Execution Count],
14501463
qs.total_logical_reads AS [Total Logical Reads],
14511464
qs.total_logical_reads/qs.execution_count AS [Avg Logical Reads],
@@ -1465,9 +1478,9 @@ ORDER BY qs.execution_count DESC OPTION (RECOMPILE);
14651478
------
14661479

14671480

1468-
-- Queries 56 through 62 are the "Bad Man List" for stored procedures
1481+
-- Queries 57 through 63 are the "Bad Man List" for stored procedures
14691482

1470-
-- Top Cached SPs By Execution Count (Query 56) (SP Execution Counts)
1483+
-- Top Cached SPs By Execution Count (Query 57) (SP Execution Counts)
14711484
SELECT TOP(100) p.name AS [SP Name], qs.execution_count AS [Execution Count],
14721485
ISNULL(qs.execution_count/DATEDIFF(Minute, qs.cached_time, GETDATE()), 0) AS [Calls/Minute],
14731486
qs.total_elapsed_time/qs.execution_count AS [Avg Elapsed Time],
@@ -1491,7 +1504,7 @@ ORDER BY qs.execution_count DESC OPTION (RECOMPILE);
14911504
-- It also helps you find possible caching opportunities
14921505

14931506

1494-
-- Top Cached SPs By Avg Elapsed Time (Query 57) (SP Avg Elapsed Time)
1507+
-- Top Cached SPs By Avg Elapsed Time (Query 58) (SP Avg Elapsed Time)
14951508
SELECT TOP(25) p.name AS [SP Name], qs.min_elapsed_time, qs.total_elapsed_time/qs.execution_count AS [avg_elapsed_time],
14961509
qs.max_elapsed_time, qs.last_elapsed_time, qs.total_elapsed_time, qs.execution_count,
14971510
ISNULL(qs.execution_count/DATEDIFF(Minute, qs.cached_time, GETDATE()), 0) AS [Calls/Minute],
@@ -1515,7 +1528,7 @@ ORDER BY avg_elapsed_time DESC OPTION (RECOMPILE);
15151528

15161529

15171530

1518-
-- Top Cached SPs By Total Worker time. Worker time relates to CPU cost (Query 58) (SP Worker Time)
1531+
-- Top Cached SPs By Total Worker time. Worker time relates to CPU cost (Query 59) (SP Worker Time)
15191532
SELECT TOP(25) p.name AS [SP Name], qs.total_worker_time AS [TotalWorkerTime],
15201533
qs.total_worker_time/qs.execution_count AS [AvgWorkerTime], qs.execution_count,
15211534
ISNULL(qs.execution_count/DATEDIFF(Minute, qs.cached_time, GETDATE()), 0) AS [Calls/Minute],
@@ -1537,7 +1550,7 @@ ORDER BY qs.total_worker_time DESC OPTION (RECOMPILE);
15371550
-- You should look at this if you see signs of CPU pressure
15381551

15391552

1540-
-- Top Cached SPs By Total Logical Reads. Logical reads relate to memory pressure (Query 59) (SP Logical Reads)
1553+
-- Top Cached SPs By Total Logical Reads. Logical reads relate to memory pressure (Query 60) (SP Logical Reads)
15411554
SELECT TOP(25) p.name AS [SP Name], qs.total_logical_reads AS [TotalLogicalReads],
15421555
qs.total_logical_reads/qs.execution_count AS [AvgLogicalReads],qs.execution_count,
15431556
ISNULL(qs.execution_count/DATEDIFF(Minute, qs.cached_time, GETDATE()), 0) AS [Calls/Minute],
@@ -1559,7 +1572,7 @@ ORDER BY qs.total_logical_reads DESC OPTION (RECOMPILE);
15591572
-- You should look at this if you see signs of memory pressure
15601573

15611574

1562-
-- Top Cached SPs By Total Physical Reads. Physical reads relate to disk read I/O pressure (Query 60) (SP Physical Reads)
1575+
-- Top Cached SPs By Total Physical Reads. Physical reads relate to disk read I/O pressure (Query 61) (SP Physical Reads)
15631576
SELECT TOP(25) p.name AS [SP Name],qs.total_physical_reads AS [TotalPhysicalReads],
15641577
qs.total_physical_reads/qs.execution_count AS [AvgPhysicalReads], qs.execution_count,
15651578
qs.total_logical_reads,qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count AS [avg_elapsed_time],
@@ -1581,7 +1594,7 @@ ORDER BY qs.total_physical_reads DESC, qs.total_logical_reads DESC OPTION (RECOM
15811594

15821595

15831596

1584-
-- Top Cached SPs By Total Logical Writes (Query 61) (SP Logical Writes)
1597+
-- Top Cached SPs By Total Logical Writes (Query 62) (SP Logical Writes)
15851598
-- Logical writes relate to both memory and disk I/O pressure
15861599
SELECT TOP(25) p.name AS [SP Name], qs.total_logical_writes AS [TotalLogicalWrites],
15871600
qs.total_logical_writes/qs.execution_count AS [AvgLogicalWrites], qs.execution_count,
@@ -1606,7 +1619,7 @@ ORDER BY qs.total_logical_writes DESC OPTION (RECOMPILE);
16061619

16071620

16081621

1609-
-- Cached SPs Missing Indexes by Execution Count (Query 62) (SP Missing Index)
1622+
-- Cached SPs Missing Indexes by Execution Count (Query 63) (SP Missing Index)
16101623
SELECT TOP(25) p.name AS [SP Name], qs.execution_count AS [Execution Count],
16111624
ISNULL(qs.execution_count/DATEDIFF(Minute, qs.cached_time, GETDATE()), 0) AS [Calls/Minute],
16121625
qs.total_elapsed_time/qs.execution_count AS [Avg Elapsed Time],
@@ -1630,7 +1643,7 @@ ORDER BY qs.execution_count DESC OPTION (RECOMPILE);
16301643

16311644

16321645

1633-
-- Lists the top statements by average input/output usage for the current database (Query 63) (Top IO Statements)
1646+
-- Lists the top statements by average input/output usage for the current database (Query 64) (Top IO Statements)
16341647
SELECT TOP(50) OBJECT_NAME(qt.objectid, dbid) AS [SP Name],
16351648
(qs.total_logical_reads + qs.total_logical_writes) /qs.execution_count AS [Avg IO], qs.execution_count AS [Execution Count],
16361649
SUBSTRING(qt.[text],qs.statement_start_offset/2,
@@ -1649,7 +1662,7 @@ ORDER BY [Avg IO] DESC OPTION (RECOMPILE);
16491662

16501663

16511664

1652-
-- Possible Bad NC Indexes (writes > reads) (Query 64) (Bad NC Indexes)
1665+
-- Possible Bad NC Indexes (writes > reads) (Query 65) (Bad NC Indexes)
16531666
SELECT SCHEMA_NAME(o.[schema_id]) AS [Schema Name],
16541667
OBJECT_NAME(s.[object_id]) AS [Table Name],
16551668
i.name AS [Index Name], i.index_id,
@@ -1675,11 +1688,12 @@ ORDER BY [Difference] DESC, [Total Writes] DESC, [Total Reads] ASC OPTION (RECOM
16751688
-- Investigate further before dropping an index!
16761689

16771690

1678-
-- Missing Indexes for current database by Index Advantage (Query 65) (Missing Indexes)
1691+
-- Missing Indexes for current database by Index Advantage (Query 66) (Missing Indexes)
16791692
SELECT DISTINCT CONVERT(decimal(18,2), migs.user_seeks * migs.avg_total_user_cost * (migs.avg_user_impact * 0.01)) AS [index_advantage],
1680-
migs.last_user_seek, mid.[statement] AS [Database.Schema.Table],
1681-
mid.equality_columns, mid.inequality_columns, mid.included_columns,
1682-
migs.user_seeks, migs.avg_total_user_cost, migs.avg_user_impact,
1693+
CONVERT(nvarchar(25), migs.last_user_seek, 20) AS [last_user_seek],
1694+
mid.[statement] AS [Database.Schema.Table],
1695+
mid.equality_columns, mid.inequality_columns, mid.included_columns, migs.user_seeks,
1696+
CONVERT(decimal(18,2), migs.avg_total_user_cost) AS [avg_total_user_,cost], migs.avg_user_impact,
16831697
OBJECT_NAME(mid.[object_id]) AS [Table Name], p.rows AS [Table Rows]
16841698
FROM sys.dm_db_missing_index_group_stats AS migs WITH (NOLOCK)
16851699
INNER JOIN sys.dm_db_missing_index_groups AS mig WITH (NOLOCK)
@@ -1699,7 +1713,7 @@ ORDER BY index_advantage DESC OPTION (RECOMPILE);
16991713
-- Håkan Winther has given me some great suggestions for this query
17001714

17011715

1702-
-- Find missing index warnings for cached plans in the current database (Query 66) (Missing Index Warnings)
1716+
-- Find missing index warnings for cached plans in the current database (Query 67) (Missing Index Warnings)
17031717
-- Note: This query could take some time on a busy instance
17041718
SELECT TOP(25) OBJECT_NAME(objectid) AS [ObjectName],
17051719
cp.objtype, cp.usecounts, cp.size_in_bytes, qp.query_plan
@@ -1714,7 +1728,7 @@ ORDER BY cp.usecounts DESC OPTION (RECOMPILE);
17141728
-- This can help you decide whether to add them or not
17151729

17161730

1717-
-- Breaks down buffers used by current database by object (table, index) in the buffer cache (Query 67) (Buffer Usage)
1731+
-- Breaks down buffers used by current database by object (table, index) in the buffer cache (Query 68) (Buffer Usage)
17181732
-- Note: This query could take some time on a busy instance
17191733
SELECT fg.name AS [Filegroup Name], SCHEMA_NAME(o.Schema_ID) AS [Schema Name],
17201734
OBJECT_NAME(p.[object_id]) AS [Object Name], p.index_id,
@@ -1746,7 +1760,7 @@ ORDER BY [BufferCount] DESC OPTION (RECOMPILE);
17461760
-- It can help identify possible candidates for data compression
17471761

17481762

1749-
-- Get Schema names, Table names, object size, row counts, and compression status for clustered index or heap (Query 68) (Table Sizes)
1763+
-- Get Schema names, Table names, object size, row counts, and compression status for clustered index or heap (Query 69) (Table Sizes)
17501764
SELECT DB_NAME(DB_ID()) AS [Database Name], SCHEMA_NAME(o.Schema_ID) AS [Schema Name],
17511765
OBJECT_NAME(p.object_id) AS [Table Name],
17521766
CAST(SUM(ps.reserved_page_count) * 8.0 / 1024 AS DECIMAL(19,2)) AS [Object Size (MB)],
@@ -1768,7 +1782,7 @@ ORDER BY SUM(ps.reserved_page_count) DESC, SUM(p.Rows) DESC OPTION (RECOMPILE);
17681782

17691783

17701784

1771-
-- Get some key table properties (Query 69) (Table Properties)
1785+
-- Get some key table properties (Query 70) (Table Properties)
17721786
SELECT OBJECT_NAME(t.[object_id]) AS [ObjectName], p.[rows] AS [Table Rows], p.index_id,
17731787
p.data_compression_desc AS [Index Data Compression],
17741788
t.create_date, t.lock_on_bulk_load, t.is_replicated, t.has_replication_filter,
@@ -1791,7 +1805,7 @@ ORDER BY OBJECT_NAME(t.[object_id]), p.index_id OPTION (RECOMPILE);
17911805

17921806

17931807

1794-
-- When were Statistics last updated on all indexes? (Query 70) (Statistics Update)
1808+
-- When were Statistics last updated on all indexes? (Query 71) (Statistics Update)
17951809
SELECT SCHEMA_NAME(o.Schema_ID) + N'.' + o.[NAME] AS [Object Name], o.[type_desc] AS [Object Type],
17961810
i.[name] AS [Index Name], STATS_DATE(i.[object_id], i.index_id) AS [Statistics Date],
17971811
s.auto_created, s.no_recompute, s.user_created, s.is_incremental, s.is_temporary,
@@ -1825,7 +1839,7 @@ ORDER BY STATS_DATE(i.[object_id], i.index_id) DESC OPTION (RECOMPILE);
18251839

18261840

18271841

1828-
-- Look at most frequently modified indexes and statistics (Query 71) (Volatile Indexes)
1842+
-- Look at most frequently modified indexes and statistics (Query 72) (Volatile Indexes)
18291843
SELECT o.[name] AS [Object Name], o.[object_id], o.[type_desc], s.[name] AS [Statistics Name],
18301844
s.stats_id, s.no_recompute, s.auto_created, s.is_incremental, s.is_temporary,
18311845
sp.modification_counter, sp.[rows], sp.rows_sampled, sp.last_updated
@@ -1843,7 +1857,7 @@ ORDER BY sp.modification_counter DESC, o.name OPTION (RECOMPILE);
18431857

18441858

18451859

1846-
-- Get fragmentation info for all indexes above a certain size in the current database (Query 72) (Index Fragmentation)
1860+
-- Get fragmentation info for all indexes above a certain size in the current database (Query 73) (Index Fragmentation)
18471861
-- Note: This query could take some time on a very large database
18481862
SELECT DB_NAME(ps.database_id) AS [Database Name], SCHEMA_NAME(o.[schema_id]) AS [Schema Name],
18491863
OBJECT_NAME(ps.OBJECT_ID) AS [Object Name], i.[name] AS [Index Name], ps.index_id, ps.index_type_desc,
@@ -1864,7 +1878,7 @@ ORDER BY ps.avg_fragmentation_in_percent DESC OPTION (RECOMPILE);
18641878
-- and how effective your index maintenance strategy is
18651879

18661880

1867-
--- Index Read/Write stats (all tables in current DB) ordered by Reads (Query 73) (Overall Index Usage - Reads)
1881+
--- Index Read/Write stats (all tables in current DB) ordered by Reads (Query 74) (Overall Index Usage - Reads)
18681882
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName], OBJECT_NAME(i.[object_id]) AS [ObjectName],
18691883
i.[name] AS [IndexName], i.index_id, i.[type_desc] AS [Index Type],
18701884
s.user_seeks, s.user_scans, s.user_lookups,
@@ -1886,7 +1900,7 @@ ORDER BY s.user_seeks + s.user_scans + s.user_lookups DESC OPTION (RECOMPILE); -
18861900
-- Show which indexes in the current database are most active for Reads
18871901

18881902

1889-
--- Index Read/Write stats (all tables in current DB) ordered by Writes (Query 74) (Overall Index Usage - Writes)
1903+
--- Index Read/Write stats (all tables in current DB) ordered by Writes (Query 75) (Overall Index Usage - Writes)
18901904
SELECT SCHEMA_NAME(t.[schema_id]) AS [SchemaName],OBJECT_NAME(i.[object_id]) AS [ObjectName],
18911905
i.[name] AS [IndexName], i.index_id, i.[type_desc] AS [Index Type],
18921906
s.user_updates AS [Writes], s.user_seeks + s.user_scans + s.user_lookups AS [Total Reads],
@@ -1907,7 +1921,7 @@ ORDER BY s.user_updates DESC OPTION (RECOMPILE); -- Order by writes
19071921

19081922

19091923

1910-
-- Get lock waits for current database (Query 75) (Lock Waits)
1924+
-- Get lock waits for current database (Query 76) (Lock Waits)
19111925
SELECT o.name AS [table_name], i.name AS [index_name], ios.index_id, ios.partition_number,
19121926
SUM(ios.row_lock_wait_count) AS [total_row_lock_waits],
19131927
SUM(ios.row_lock_wait_in_ms) AS [total_row_lock_wait_in_ms],
@@ -1932,7 +1946,7 @@ ORDER BY total_lock_wait_in_ms DESC OPTION (RECOMPILE);
19321946

19331947

19341948

1935-
-- Look at UDF execution statistics (Query 76) (UDF Statistics)
1949+
-- Look at UDF execution statistics (Query 77) (UDF Statistics)
19361950
SELECT OBJECT_NAME(object_id) AS [Function Name], execution_count,
19371951
total_worker_time, total_worker_time/execution_count AS [avg_worker_time],
19381952
total_logical_reads, total_physical_reads, total_elapsed_time,
@@ -1952,7 +1966,7 @@ ORDER BY total_worker_time DESC OPTION (RECOMPILE);
19521966
-- https://bit.ly/2q1Q6BM
19531967

19541968

1955-
-- Get QueryStore Options for this database (Query 77) (QueryStore Options)
1969+
-- Get QueryStore Options for this database (Query 78) (QueryStore Options)
19561970
SELECT actual_state_desc, desired_state_desc, [interval_length_minutes],
19571971
current_storage_size_mb, [max_storage_size_mb],
19581972
query_capture_mode_desc, size_based_cleanup_mode_desc
@@ -1972,7 +1986,7 @@ FROM sys.database_query_store_options WITH (NOLOCK) OPTION (RECOMPILE);
19721986
-- ALTER DATABASE [DatabaseName] SET QUERY_STORE = OFF(FORCED);
19731987

19741988

1975-
-- Get input buffer information for the current database (Query 78) (Input Buffer)
1989+
-- Get input buffer information for the current database (Query 79) (Input Buffer)
19761990
SELECT es.session_id, DB_NAME(es.database_id) AS [Database Name],
19771991
es.[program_name], es.[host_name], es.login_name,
19781992
es.login_time, es.cpu_time, es.logical_reads, es.memory_usage,
@@ -1995,7 +2009,7 @@ AND es.session_id <> @@SPID OPTION (RECOMPILE);
19952009

19962010

19972011

1998-
-- Look at recent Full backups for the current database (Query 81) (Recent Full Backups)
2012+
-- Look at recent Full backups for the current database (Query 80) (Recent Full Backups)
19992013
SELECT TOP (30) bs.machine_name, bs.server_name, bs.database_name AS [Database Name], bs.recovery_model,
20002014
CONVERT (BIGINT, bs.backup_size / 1048576 ) AS [Uncompressed Backup Size (MB)],
20012015
CONVERT (BIGINT, bs.compressed_backup_size / 1048576 ) AS [Compressed Backup Size (MB)],

0 commit comments

Comments
 (0)