Skip to content

Commit

Permalink
update version
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Sep 7, 2022
1 parent 27a62ab commit 5dc3bf2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 82 deletions.
2 changes: 1 addition & 1 deletion bin/dbatools-index.json.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cf92feb4fd030401766b91d875799c5493a3ebc1
70a62b01171b822ba6443722bf66f9df41ccc5ef
84 changes: 49 additions & 35 deletions bin/diagnosticquery/SQLServerDiagnosticQueries_2016SP2.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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


-- Look for long duration buffer pool scans (Query 50) (Long Buffer Pool Scans)
EXEC sys.xp_readerrorlog 0, 1, N'Buffer pool scan took';
------

-- Finds buffer pool scans that took more than 10 seconds in the current SQL Server Error log
-- Only in SQL Server 2016 SP3 and later

-- Operations that trigger buffer pool scan may run slowly on large-memory computers - SQL Server | Microsoft Docs
-- https://bit.ly/3QrFC81



-- Database specific queries *****************************************************************

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

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


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


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



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


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



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


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

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


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



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


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


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



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



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



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



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


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


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


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


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



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



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



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



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


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


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



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



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


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


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



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

0 comments on commit 5dc3bf2

Please sign in to comment.