Use explicit casts to avoid datatype issues (#3980)

This commit is contained in:
Mark Wilkinson - m82labs 2018-04-06 17:58:33 -04:00 committed by Daniel Nelson
parent 72e9f1d7d0
commit cbaf8481c9
1 changed files with 106 additions and 110 deletions

View File

@ -397,9 +397,7 @@ CROSS APPLY (
OPTION( RECOMPILE ); OPTION( RECOMPILE );
` `
const sqlPerformanceCountersV2 string = `DECLARE @DynamicPerf NVARCHAR(MAX) = '' const sqlPerformanceCountersV2 string = `
SET @DynamicPerf += REPLACE('
DECLARE @PCounters TABLE DECLARE @PCounters TABLE
( (
object_name nvarchar(128), object_name nvarchar(128),
@ -409,140 +407,120 @@ 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 INSERT INTO @PCounters
SELECT DISTINCT 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,
RTrim(spi.instance_name) instance_name, RTrim(spi.instance_name) instance_name,
spi.cntr_value, 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
WHERE ( 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',
"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)'
) )
) OR ( ) OR (
instance_name IN ("_Total","Column store object pool") instance_name IN ('_Total','Column store object pool')
AND counter_name IN ( AND counter_name IN (
"Log Flushes/sec", 'Log Flushes/sec',
"Log Flush Wait Time", 'Log Flush Wait Time',
"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',
"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)'
) OR ( ) OR (
counter_name IN ( counter_name IN (
"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'
) )
AND instance_name = "_Total" AND instance_name = '_Total'
) )
) OR ( ) OR (
counter_name IN ("Transaction Delay") counter_name IN ('Transaction Delay')
) OR ( ) OR (
counter_name IN ( counter_name IN (
"CPU usage %", 'CPU usage %',
"CPU usage % base", 'CPU usage % base',
"Queued requests", 'Queued requests',
"Requests completed/sec", 'Requests completed/sec',
"Blocked tasks" 'Blocked tasks'
) )
) OR ( ) OR (
counter_name IN ( counter_name IN (
"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)'
) )
) OR ( ) OR (
object_name LIKE "%User Settable%" object_name LIKE '%User Settable%'
OR object_name LIKE "%SQL Errors%" OR object_name LIKE '%SQL Errors%'
) )
'
,'"','''')
SET @DynamicPerf += REPLACE(' DECLARE @SQL NVARCHAR(MAX)
SELECT "sqlserver_performance" AS [measurement], SET @SQL = REPLACE('
REPLACE(@@SERVERNAME,"\",":") AS [sql_instance],
pc.object_name AS [object],
pc.counter_name AS [counter],
CASE pc.instance_name WHEN "_Total" THEN "Total" ELSE ISNULL(pc.instance_name,"") END AS [instance],
CASE WHEN pc.cntr_type = 537003264 AND pc1.cntr_value > 0 THEN (pc.cntr_value * 1.0) / (pc1.cntr_value * 1.0) * 100 ELSE pc.cntr_value END AS [value]
FROM @PCounters AS pc
LEFT OUTER JOIN @PCounters AS pc1
ON (
pc.counter_name = REPLACE(pc1.counter_name," base","")
OR pc.counter_name = REPLACE(pc1.counter_name," base"," (ms)")
)
AND pc.object_name = pc1.object_name
AND pc.instance_name = pc1.instance_name
AND pc1.counter_name LIKE "%base"
WHERE pc.counter_name NOT LIKE "% base"
UNION ALL
SELECT SELECT
"sqlserver_performance" As [measurement],
REPLACE(@@SERVERNAME,"\",":") AS [sql_instance],
"SQLServer:Workload Group Stats" AS object, "SQLServer:Workload Group Stats" AS object,
counter, counter,
instance, instance,
vs.value CAST(vs.value AS BIGINT) AS value,
1
FROM FROM
( (
SELECT SELECT
@ -561,11 +539,29 @@ FROM
) AS rg ) AS rg
UNPIVOT ( UNPIVOT (
value FOR counter IN ( [Request Count], [Queued Request Count], [CPU Limit Violation Count], [CPU Usage (time)], ' + CASE WHEN SERVERPROPERTY('ProductMajorVersion') > 10 THEN '[Premptive CPU Usage (time)], ' ELSE '' END + '[Lock Wait Count], [Lock Wait Time], [Reduced Memory Grant Count] ) value FOR counter IN ( [Request Count], [Queued Request Count], [CPU Limit Violation Count], [CPU Usage (time)], ' + CASE WHEN SERVERPROPERTY('ProductMajorVersion') > 10 THEN '[Premptive CPU Usage (time)], ' ELSE '' END + '[Lock Wait Count], [Lock Wait Time], [Reduced Memory Grant Count] )
) AS vs ) AS vs'
OPTION(RECOMPILE);'
,'"','''') ,'"','''')
EXEC(@DynamicPerf) INSERT INTO @PCounters
EXEC( @SQL )
SELECT 'sqlserver_performance' AS [measurement],
REPLACE(@@SERVERNAME,'\',':') AS [sql_instance],
pc.object_name AS [object],
pc.counter_name AS [counter],
CASE pc.instance_name WHEN '_Total' THEN 'Total' ELSE ISNULL(pc.instance_name,'') END AS [instance],
CAST(CASE WHEN pc.cntr_type = 537003264 AND pc1.cntr_value > 0 THEN (pc.cntr_value * 1.0) / (pc1.cntr_value * 1.0) * 100 ELSE pc.cntr_value END AS float(10)) AS [value]
FROM @PCounters AS pc
LEFT OUTER JOIN @PCounters AS pc1
ON (
pc.counter_name = REPLACE(pc1.counter_name,' base','')
OR pc.counter_name = REPLACE(pc1.counter_name,' base',' (ms)')
)
AND pc.object_name = pc1.object_name
AND pc.instance_name = pc1.instance_name
AND pc1.counter_name LIKE '%base'
WHERE pc.counter_name NOT LIKE '% base'
OPTION(RECOMPILE);
` `
const sqlWaitStatsCategorizedV2 string = `SELECT const sqlWaitStatsCategorizedV2 string = `SELECT