Add Database IO Tempdb per Azure DB and fix PerfmonV2 collection for on-prem (#7163)

This commit is contained in:
denzilribeiro 2020-03-16 15:32:30 -05:00 committed by GitHub
parent 9508bbdf29
commit 0a75dea70d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 153 additions and 122 deletions

View File

@ -359,24 +359,40 @@ BEGIN
SELECT SELECT
''sqlserver_database_io'' As [measurement] ''sqlserver_database_io'' As [measurement]
,REPLACE(@@SERVERNAME,''\'','':'') AS [sql_instance] ,REPLACE(@@SERVERNAME,''\'','':'') AS [sql_instance]
,DB_NAME([vfs].[database_id]) AS [database_name] ,DB_NAME() as database_name
,vfs.database_id -- /*needed as tempdb is different for each Azure SQL DB as grouping has to be by logical server + db_name + database_id*/
,vfs.file_id
,vfs.io_stall_read_ms AS read_latency_ms ,vfs.io_stall_read_ms AS read_latency_ms
,vfs.num_of_reads AS reads ,vfs.num_of_reads AS reads
,vfs.num_of_bytes_read AS read_bytes ,vfs.num_of_bytes_read AS read_bytes
,vfs.io_stall_write_ms AS write_latency_ms ,vfs.io_stall_write_ms AS write_latency_ms
,vfs.num_of_writes AS writes ,vfs.num_of_writes AS writes
,vfs.num_of_bytes_written AS write_bytes ,vfs.num_of_bytes_written AS write_bytes
,vfs.io_stall_queued_read_ms as rg_read_stall_ms ,vfs.io_stall_queued_read_ms AS [rg_read_stall_ms]
,ISNULL(b.name ,''RBPEX'') as logical_filename ,vfs.io_stall_queued_write_ms AS [rg_write_stall_ms]
,ISNULL(b.physical_name, ''RBPEX'') as physical_filename ,CASE
WHEN (vfs.database_id = 0) THEN ''RBPEX''
ELSE b.logical_filename
END as logical_filename
,CASE
WHEN (vfs.database_id = 0) THEN ''RBPEX''
ELSE b.physical_filename
END as physical_filename
,CASE WHEN vfs.file_id = 2 THEN ''LOG'' ELSE ''DATA'' END AS file_type ,CASE WHEN vfs.file_id = 2 THEN ''LOG'' ELSE ''DATA'' END AS file_type
,ISNULL(size,0)/128 AS current_size_mb ,ISNULL(size,0)/128 AS current_size_mb
,ISNULL(FILEPROPERTY(b.name,''SpaceUsed'')/128,0) as space_used_mb ,ISNULL(FILEPROPERTY(b.logical_filename,''SpaceUsed'')/128,0) as space_used_mb
,vfs.io_stall_queued_read_ms AS [rg_read_stall_ms]
,vfs.io_stall_queued_write_ms AS [rg_write_stall_ms]
FROM [sys].[dm_io_virtual_file_stats](NULL,NULL) AS vfs FROM [sys].[dm_io_virtual_file_stats](NULL,NULL) AS vfs
LEFT OUTER join sys.database_files b -- needed to get Tempdb file names on Azure SQL DB so you can join appropriately. Without this had a bug where join was only on file_id
ON b.file_id = vfs.file_id LEFT OUTER join
(
SELECT DB_ID() as database_id, file_id, logical_filename=name COLLATE SQL_Latin1_General_CP1_CI_AS
, physical_filename = physical_name COLLATE SQL_Latin1_General_CP1_CI_AS, size from sys.database_files
where type <> 2
UNION ALL
SELECT 2 as database_id, file_id, logical_filename = name , physical_filename = physical_name, size
from tempdb.sys.database_files
) b ON b.database_id = vfs.database_id and b.file_id = vfs.file_id
where vfs.database_id IN (DB_ID(),0,2)
' '
EXEC sp_executesql @SqlStatement EXEC sp_executesql @SqlStatement
@ -390,7 +406,7 @@ BEGIN
,REPLACE(@@SERVERNAME,''\'','':'') AS [sql_instance] ,REPLACE(@@SERVERNAME,''\'','':'') AS [sql_instance]
,DB_NAME(vfs.[database_id]) AS [database_name] ,DB_NAME(vfs.[database_id]) AS [database_name]
,COALESCE(mf.[physical_name],''RBPEX'') AS [physical_filename] --RPBEX = Resilient Buffer Pool Extension ,COALESCE(mf.[physical_name],''RBPEX'') AS [physical_filename] --RPBEX = Resilient Buffer Pool Extension
,COALESCE(mf.[name],''RBPEX'') AS [logical_filename] --RPBEX = Resilient Buffer Pool Extension ,COALESCE(mf.[name],''RBPEX'') AS [logical_filename] --RPBEX = Resilient Buffer Pool Extension
,mf.[type_desc] AS [file_type] ,mf.[type_desc] AS [file_type]
,IIF( RIGHT(vs.[volume_mount_point],1) = ''\'' /*Tag value cannot end with \ */ ,IIF( RIGHT(vs.[volume_mount_point],1) = ''\'' /*Tag value cannot end with \ */
,LEFT(vs.[volume_mount_point],LEN(vs.[volume_mount_point])-1) ,LEFT(vs.[volume_mount_point],LEN(vs.[volume_mount_point])-1)
@ -536,135 +552,150 @@ DECLARE @PCounters TABLE
cntr_type INT, cntr_type INT,
Primary Key(object_name, counter_name, instance_name) Primary Key(object_name, counter_name, instance_name)
); );
INSERT INTO @PCounters
SELECT DISTINCT DECLARE @SQL NVARCHAR(MAX)
SET @SQL = N'SELECT DISTINCT
RTrim(spi.object_name) object_name, RTrim(spi.object_name) object_name,
RTrim(spi.counter_name) counter_name, RTrim(spi.counter_name) counter_name,'
CASE WHEN ( +
RTRIM(spi.object_name) LIKE '%:Databases' CASE
OR RTRIM(spi.object_name) LIKE '%:Database Replica' WHEN CAST(SERVERPROPERTY('EngineEdition') AS int) IN (5,8) --- needed to get actual DB Name for SQL DB/ Managed instance
OR RTRIM(spi.object_name) LIKE '%:Catalog Metadata' THEN N'CASE WHEN (
OR RTRIM(spi.object_name) LIKE '%:Query Store' RTRIM(spi.object_name) LIKE ''%:Databases''
OR RTRIM(spi.object_name) LIKE '%:Columnstore' OR RTRIM(spi.object_name) LIKE ''%:Database Replica''
OR RTRIM(spi.object_name) LIKE '%:Advanced Analytics') OR RTRIM(spi.object_name) LIKE ''%:Catalog Metadata''
AND SERVERPROPERTY ('EngineEdition') IN (5,8) OR RTRIM(spi.object_name) LIKE ''%:Query Store''
AND TRY_CONVERT(uniqueidentifier, spi.instance_name) IS NOT NULL -- for cloud only OR RTRIM(spi.object_name) LIKE ''%:Columnstore''
OR RTRIM(spi.object_name) LIKE ''%:Advanced Analytics'')
AND TRY_CONVERT(uniqueidentifier, spi.instance_name)
IS NOT NULL -- for cloud only
THEN d.name THEN d.name
WHEN RTRIM(object_name) LIKE '%:Availability Replica' WHEN RTRIM(object_name) LIKE ''%:Availability Replica''
AND SERVERPROPERTY ('EngineEdition') IN (5,8)
AND TRY_CONVERT(uniqueidentifier, spi.instance_name) IS NOT NULL -- for cloud only AND TRY_CONVERT(uniqueidentifier, spi.instance_name) IS NOT NULL -- for cloud only
THEN d.name + RTRIM(SUBSTRING(spi.instance_name, 37, LEN(spi.instance_name))) THEN d.name + RTRIM(SUBSTRING(spi.instance_name, 37, LEN(spi.instance_name)))
ELSE spi.instance_name ELSE spi.instance_name
END AS instance_name, END AS instance_name,'
CAST(spi.cntr_value AS BIGINT) AS cntr_value, ELSE 'spi.instance_name as instance_name, '
END
+
'CAST(spi.cntr_value AS BIGINT) AS cntr_value,
spi.cntr_type spi.cntr_type
FROM sys.dm_os_performance_counters AS spi FROM sys.dm_os_performance_counters AS spi '
LEFT JOIN sys.databases AS d +
ON LEFT(spi.instance_name, 36) -- some instance_name values have an additional identifier appended after the GUID CASE
= CASE WHEN -- in SQL DB standalone, physical_database_name for master is the GUID of the user database WHEN CAST(SERVERPROPERTY('EngineEdition') AS int) IN (5,8) --- Join is ONLY for managed instance and SQL DB, not for on-prem
d.name = 'master' AND TRY_CONVERT(uniqueidentifier, d.physical_database_name) IS NOT NULL THEN CAST(N'LEFT JOIN sys.databases AS d
ON LEFT(spi.instance_name, 36) -- some instance_name values have an additional identifier appended after the GUID
= CASE WHEN -- in SQL DB standalone, physical_database_name for master is the GUID of the user database
d.name = ''master'' AND TRY_CONVERT(uniqueidentifier, d.physical_database_name) IS NOT NULL
THEN d.name THEN d.name
ELSE d.physical_database_name ELSE d.physical_database_name
END END ' as NVARCHAR(MAX))
WHERE ( ELSE N' '
END
SET @SQL = @SQL + CAST(N' WHERE (
counter_name IN ( counter_name IN (
'SQL Compilations/sec', ''SQL Compilations/sec'',
'SQL Re-Compilations/sec', ''SQL Re-Compilations/sec'',
'User Connections', ''User Connections'',
'Batch Requests/sec', ''Batch Requests/sec'',
'Logouts/sec', ''Logouts/sec'',
'Logins/sec', ''Logins/sec'',
'Processes blocked', ''Processes blocked'',
'Latch Waits/sec', ''Latch Waits/sec'',
'Full Scans/sec', ''Full Scans/sec'',
'Index Searches/sec', ''Index Searches/sec'',
'Page Splits/sec', ''Page Splits/sec'',
'Page lookups/sec', ''Page lookups/sec'',
'Page reads/sec', ''Page reads/sec'',
'Page writes/sec', ''Page writes/sec'',
'Readahead pages/sec', ''Readahead pages/sec'',
'Lazy writes/sec', ''Lazy writes/sec'',
'Checkpoint pages/sec', ''Checkpoint pages/sec'',
'Page life expectancy', ''Page life expectancy'',
'Log File(s) Size (KB)', ''Log File(s) Size (KB)'',
'Log File(s) Used Size (KB)', ''Log File(s) Used Size (KB)'',
'Data File(s) Size (KB)', ''Data File(s) Size (KB)'',
'Transactions/sec', ''Transactions/sec'',
'Write Transactions/sec', ''Write Transactions/sec'',
'Active Temp Tables', ''Active Temp Tables'',
'Temp Tables Creation Rate', ''Temp Tables Creation Rate'',
'Temp Tables For Destruction', ''Temp Tables For Destruction'',
'Free Space in tempdb (KB)', ''Free Space in tempdb (KB)'',
'Version Store Size (KB)', ''Version Store Size (KB)'',
'Memory Grants Pending', ''Memory Grants Pending'',
'Memory Grants Outstanding', ''Memory Grants Outstanding'',
'Free list stalls/sec', ''Free list stalls/sec'',
'Buffer cache hit ratio', ''Buffer cache hit ratio'',
'Buffer cache hit ratio base', ''Buffer cache hit ratio base'',
'Backup/Restore Throughput/sec', ''Backup/Restore Throughput/sec'',
'Total Server Memory (KB)', ''Total Server Memory (KB)'',
'Target Server Memory (KB)', ''Target Server Memory (KB)'',
'Log Flushes/sec', ''Log Flushes/sec'',
'Log Flush Wait Time', ''Log Flush Wait Time'',
'Memory broker clerk size', ''Memory broker clerk size'',
'Log Bytes Flushed/sec', ''Log Bytes Flushed/sec'',
'Bytes Sent to Replica/sec', ''Bytes Sent to Replica/sec'',
'Log Send Queue', ''Log Send Queue'',
'Bytes Sent to Transport/sec', ''Bytes Sent to Transport/sec'',
'Sends to Replica/sec', ''Sends to Replica/sec'',
'Bytes Sent to Transport/sec', ''Bytes Sent to Transport/sec'',
'Sends to Transport/sec', ''Sends to Transport/sec'',
'Bytes Received from Replica/sec', ''Bytes Received from Replica/sec'',
'Receives from Replica/sec', ''Receives from Replica/sec'',
'Flow Control Time (ms/sec)', ''Flow Control Time (ms/sec)'',
'Flow Control/sec', ''Flow Control/sec'',
'Resent Messages/sec', ''Resent Messages/sec'',
'Redone Bytes/sec', ''Redone Bytes/sec'',
'XTP Memory Used (KB)', ''XTP Memory Used (KB)'',
'Transaction Delay', ''Transaction Delay'',
'Log Bytes Received/sec', ''Log Bytes Received/sec'',
'Log Apply Pending Queue', ''Log Apply Pending Queue'',
'Redone Bytes/sec', ''Redone Bytes/sec'',
'Recovery Queue', ''Recovery Queue'',
'Log Apply Ready Queue', ''Log Apply Ready Queue'',
'CPU usage %', ''CPU usage %'',
'CPU usage % base', ''CPU usage % base'',
'Queued requests', ''Queued requests'',
'Requests completed/sec', ''Requests completed/sec'',
'Blocked tasks', ''Blocked tasks'',
'Active memory grant amount (KB)', ''Active memory grant amount (KB)'',
'Disk Read Bytes/sec', ''Disk Read Bytes/sec'',
'Disk Read IO Throttled/sec', ''Disk Read IO Throttled/sec'',
'Disk Read IO/sec', ''Disk Read IO/sec'',
'Disk Write Bytes/sec', ''Disk Write Bytes/sec'',
'Disk Write IO Throttled/sec', ''Disk Write IO Throttled/sec'',
'Disk Write IO/sec', ''Disk Write IO/sec'',
'Used memory (KB)', ''Used memory (KB)'',
'Forwarded Records/sec', ''Forwarded Records/sec'',
'Background Writer pages/sec', ''Background Writer pages/sec'',
'Percent Log Used', ''Percent Log Used'',
'Log Send Queue KB', ''Log Send Queue KB'',
'Redo Queue KB', ''Redo Queue KB'',
'Mirrored Write Transactions/sec', ''Mirrored Write Transactions/sec'',
'Group Commit Time', ''Group Commit Time'',
'Group Commits/Sec' ''Group Commits/Sec''
) )
) OR ( ) OR (
object_name LIKE '%User Settable%' object_name LIKE ''%User Settable%''
OR object_name LIKE '%SQL Errors%' OR object_name LIKE ''%SQL Errors%''
) OR ( ) OR (
object_name LIKE '%Batch Resp Statistics%' object_name LIKE ''%Batch Resp Statistics%''
) OR ( ) OR (
instance_name IN ('_Total') instance_name IN (''_Total'')
AND counter_name IN ( AND counter_name IN (
'Lock Timeouts/sec', ''Lock Timeouts/sec'',
'Number of Deadlocks/sec', ''Number of Deadlocks/sec'',
'Lock Waits/sec', ''Lock Waits/sec'',
'Latch Waits/sec' ''Latch Waits/sec''
) )
) )
' as NVARCHAR(MAX))
INSERT INTO @PCounters
EXEC (@SQL)
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = REPLACE('SELECT SET @SQL = REPLACE('SELECT
"SQLServer:Workload Group Stats" AS object, "SQLServer:Workload Group Stats" AS object,
counter, counter,