Fix collation issue

This commit is contained in:
zensqlmonitor 2016-09-11 09:04:43 +02:00 committed by Cameron Sparr
parent e477620dc5
commit 573628dbdd
1 changed files with 6 additions and 28 deletions

View File

@ -626,26 +626,22 @@ const sqlDatabaseIO string = `SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
DECLARE @secondsBetween tinyint = 5; DECLARE @secondsBetween tinyint = 5;
DECLARE @delayInterval char(8) = CONVERT(Char(8), DATEADD(SECOND, @secondsBetween, '00:00:00'), 108); DECLARE @delayInterval char(8) = CONVERT(Char(8), DATEADD(SECOND, @secondsBetween, '00:00:00'), 108);
IF OBJECT_ID('tempdb..#baseline') IS NOT NULL IF OBJECT_ID('tempdb..#baseline') IS NOT NULL
DROP TABLE #baseline; DROP TABLE #baseline;
IF OBJECT_ID('tempdb..#baselinewritten') IS NOT NULL IF OBJECT_ID('tempdb..#baselinewritten') IS NOT NULL
DROP TABLE #baselinewritten; DROP TABLE #baselinewritten;
SELECT DB_NAME(mf.database_id) AS databaseName , SELECT DB_NAME(mf.database_id) AS databaseName ,
mf.physical_name, mf.physical_name,
divfs.num_of_bytes_read, divfs.num_of_bytes_read,
divfs.num_of_bytes_written, divfs.num_of_bytes_written,
divfs.num_of_reads, divfs.num_of_reads,
divfs.num_of_writes, divfs.num_of_writes,
GETDATE() AS baselineDate GETDATE() AS baselinedate
INTO #baseline INTO #baseline
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs
INNER JOIN sys.master_files AS mf ON mf.database_id = divfs.database_id INNER JOIN sys.master_files AS mf ON mf.database_id = divfs.database_id
AND mf.file_id = divfs.file_id AND mf.file_id = divfs.file_id
WAITFOR DELAY @delayInterval; WAITFOR DELAY @delayInterval;
;WITH currentLine AS ;WITH currentLine AS
( (
SELECT DB_NAME(mf.database_id) AS databaseName , SELECT DB_NAME(mf.database_id) AS databaseName ,
@ -655,12 +651,11 @@ WAITFOR DELAY @delayInterval;
divfs.num_of_bytes_written, divfs.num_of_bytes_written,
divfs.num_of_reads, divfs.num_of_reads,
divfs.num_of_writes, divfs.num_of_writes,
GETDATE() AS currentlineDate GETDATE() AS currentlinedate
FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs FROM sys.dm_io_virtual_file_stats(NULL, NULL) AS divfs
INNER JOIN sys.master_files AS mf ON mf.database_id = divfs.database_id INNER JOIN sys.master_files AS mf ON mf.database_id = divfs.database_id
AND mf.file_id = divfs.file_id AND mf.file_id = divfs.file_id
) )
SELECT database_name SELECT database_name
, datafile_type , datafile_type
, num_of_bytes_read_persec = SUM(num_of_bytes_read_persec) , num_of_bytes_read_persec = SUM(num_of_bytes_read_persec)
@ -673,23 +668,21 @@ FROM
SELECT SELECT
database_name = currentLine.databaseName database_name = currentLine.databaseName
, datafile_type = type_desc , datafile_type = type_desc
, num_of_bytes_read_persec = (currentLine.num_of_bytes_read - T1.num_of_bytes_read) / (DATEDIFF(SECOND,baseLineDate,currentLineDate)) , num_of_bytes_read_persec = (currentLine.num_of_bytes_read - T1.num_of_bytes_read) / (DATEDIFF(SECOND,baselinedate,currentlinedate))
, num_of_bytes_written_persec = (currentLine.num_of_bytes_written - T1.num_of_bytes_written) / (DATEDIFF(SECOND,baseLineDate,currentLineDate)) , num_of_bytes_written_persec = (currentLine.num_of_bytes_written - T1.num_of_bytes_written) / (DATEDIFF(SECOND,baselinedate,currentlinedate))
, num_of_reads_persec = (currentLine.num_of_reads - T1.num_of_reads) / (DATEDIFF(SECOND,baseLineDate,currentLineDate)) , num_of_reads_persec = (currentLine.num_of_reads - T1.num_of_reads) / (DATEDIFF(SECOND,baselinedate,currentlinedate))
, num_of_writes_persec = (currentLine.num_of_writes - T1.num_of_writes) / (DATEDIFF(SECOND,baseLineDate,currentLineDate)) , num_of_writes_persec = (currentLine.num_of_writes - T1.num_of_writes) / (DATEDIFF(SECOND,baselinedate,currentlinedate))
FROM currentLine FROM currentLine
INNER JOIN #baseline T1 ON T1.databaseName = currentLine.databaseName INNER JOIN #baseline T1 ON T1.databaseName = currentLine.databaseName
AND T1.physical_name = currentLine.physical_name AND T1.physical_name = currentLine.physical_name
) as T ) as T
GROUP BY database_name, datafile_type GROUP BY database_name, datafile_type
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX), @ColumnName2 AS NVARCHAR(MAX) DECLARE @ColumnName AS NVARCHAR(MAX), @ColumnName2 AS NVARCHAR(MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',','') + QUOTENAME(database_name) SELECT @ColumnName = ISNULL(@ColumnName + ',','') + QUOTENAME(database_name)
FROM (SELECT DISTINCT database_name FROM #baselinewritten) AS bl FROM (SELECT DISTINCT database_name FROM #baselinewritten) AS bl
SELECT @ColumnName2 = ISNULL(@ColumnName2 + '+','') + QUOTENAME(database_name) SELECT @ColumnName2 = ISNULL(@ColumnName2 + '+','') + QUOTENAME(database_name)
FROM (SELECT DISTINCT database_name FROM #baselinewritten) AS bl FROM (SELECT DISTINCT database_name FROM #baselinewritten) AS bl
SET @DynamicPivotQuery = N' SET @DynamicPivotQuery = N'
SELECT measurement = ''Log writes (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Log writes (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
@ -699,9 +692,7 @@ FROM #baselinewritten
WHERE datafile_type = ''LOG'' WHERE datafile_type = ''LOG''
) as V ) as V
PIVOT(SUM(num_of_bytes_written_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_bytes_written_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
UNION ALL UNION ALL
SELECT measurement = ''Rows writes (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Rows writes (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -710,9 +701,7 @@ FROM #baselinewritten
WHERE datafile_type = ''ROWS'' WHERE datafile_type = ''ROWS''
) as V ) as V
PIVOT(SUM(num_of_bytes_written_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_bytes_written_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
UNION ALL UNION ALL
SELECT measurement = ''Log reads (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Log reads (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -721,9 +710,7 @@ FROM #baselinewritten
WHERE datafile_type = ''LOG'' WHERE datafile_type = ''LOG''
) as V ) as V
PIVOT(SUM(num_of_bytes_read_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_bytes_read_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
UNION ALL UNION ALL
SELECT measurement = ''Rows reads (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Rows reads (bytes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -732,9 +719,7 @@ FROM #baselinewritten
WHERE datafile_type = ''ROWS'' WHERE datafile_type = ''ROWS''
) as V ) as V
PIVOT(SUM(num_of_bytes_read_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_bytes_read_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
UNION ALL UNION ALL
SELECT measurement = ''Log (writes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Log (writes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -743,9 +728,7 @@ FROM #baselinewritten
WHERE datafile_type = ''LOG'' WHERE datafile_type = ''LOG''
) as V ) as V
PIVOT(SUM(num_of_writes_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_writes_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
UNION ALL UNION ALL
SELECT measurement = ''Rows (writes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Rows (writes/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -754,9 +737,7 @@ FROM #baselinewritten
WHERE datafile_type = ''ROWS'' WHERE datafile_type = ''ROWS''
) as V ) as V
PIVOT(SUM(num_of_writes_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTabl PIVOT(SUM(num_of_writes_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTabl
UNION ALL UNION ALL
SELECT measurement = ''Log (reads/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Log (reads/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -765,9 +746,7 @@ FROM #baselinewritten
WHERE datafile_type = ''LOG'' WHERE datafile_type = ''LOG''
) as V ) as V
PIVOT(SUM(num_of_reads_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_reads_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
UNION ALL UNION ALL
SELECT measurement = ''Rows (reads/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO'' SELECT measurement = ''Rows (reads/sec)'', servername = REPLACE(@@SERVERNAME, ''\'', '':''), type = ''Database IO''
, ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM , ' + @ColumnName + ', Total = ' + @ColumnName2 + ' FROM
( (
@ -777,7 +756,6 @@ WHERE datafile_type = ''ROWS''
) as V ) as V
PIVOT(SUM(num_of_reads_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable PIVOT(SUM(num_of_reads_persec) FOR database_name IN (' + @ColumnName + ')) AS PVTTable
' '
EXEC sp_executesql @DynamicPivotQuery; EXEC sp_executesql @DynamicPivotQuery;
` `