diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..42c950d --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,59 @@ +name: Publish docs (GitHub Pages) + +on: + push: + branches: [dev, main] + paths: + - "docs/**" + - "mkdocs.yml" + - ".github/workflows/docs.yml" + workflow_dispatch: + +permissions: + contents: read + pages: write + id-token: write + +concurrency: + group: pages + cancel-in-progress: false + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + cache: pip + + - name: Install MkDocs + Material + run: | + python -m pip install --upgrade pip + pip install \ + "mkdocs==1.6.1" \ + "mkdocs-material==9.5.44" \ + "mkdocs-minify-plugin==0.8.0" \ + "mkdocs-glightbox==0.4.0" \ + "pymdown-extensions==10.11.2" + + - name: Build site (strict) + run: mkdocs build --strict + + - uses: actions/upload-pages-artifact@v3 + with: + path: site + + deploy: + needs: build + runs-on: ubuntu-latest + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - id: deployment + uses: actions/deploy-pages@v4 diff --git a/.gitignore b/.gitignore index 583bc71..d2a3bb5 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,11 @@ __pycache__/ .vs tempCodeRunnerFile.ps1 /FlaskRestAPI/ +sql_exporter.exe +sql_exporter/LICENSE +mssql-metrics-collector.collector.yml +*tessell* +SQL-Exporter-Metrics-Dashboard-POC.json # Ignore Virtual Env Folder /SQLMonitorVenv/* diff --git a/AI-Agent/AI-Agent.ssmssqlproj b/AI-Agent/AI-Agent.ssmssqlproj new file mode 100644 index 0000000..25cfc75 --- /dev/null +++ b/AI-Agent/AI-Agent.ssmssqlproj @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Alerting/Alerting.ssmssqlproj b/Alerting/Alerting.ssmssqlproj index d39fd69..0bf48d9 100644 --- a/Alerting/Alerting.ssmssqlproj +++ b/Alerting/Alerting.ssmssqlproj @@ -1,5 +1,5 @@ - + diff --git a/Credential-Manager/SCH-[dbo].[credential_manager].sql b/Credential-Manager/SCH-[dbo].[credential_manager].sql index 7ea882d..3f6db84 100644 --- a/Credential-Manager/SCH-[dbo].[credential_manager].sql +++ b/Credential-Manager/SCH-[dbo].[credential_manager].sql @@ -29,8 +29,9 @@ create table dbo.credential_manager ,valid_to datetime2 generated always as row end hidden NOT NULL ,period for system_time (valid_from,valid_to) ) -with (system_versioning = on (HISTORY_TABLE = dbo.credential_manager_history)) +with (system_versioning = on (HISTORY_TABLE = dbo.credential_manager_history, HISTORY_RETENTION_PERIOD = 2 YEARS)); go +--alter table dbo.credential_manager set (system_versioning = on (HISTORY_RETENTION_PERIOD = 2 YEARS)); create nonclustered index uq__server_name__user_name on dbo.credential_manager (server_name, [user_name]) go diff --git a/DDLs/grafana-login.sql b/DDLs/DCL-[grafana-login].sql similarity index 100% rename from DDLs/grafana-login.sql rename to DDLs/DCL-[grafana-login].sql diff --git a/DDLs/DatabaseMail_Using_GMail.sql b/DDLs/DDL-[DatabaseMail_Using_GMail].sql similarity index 100% rename from DDLs/DatabaseMail_Using_GMail.sql rename to DDLs/DDL-[DatabaseMail_Using_GMail].sql diff --git a/DDLs/SCH-sp_WhoIsActive_v12_00(Modified).sql b/DDLs/DDL-[sp_WhoIsActive_v12_00(Modified)].sql similarity index 100% rename from DDLs/SCH-sp_WhoIsActive_v12_00(Modified).sql rename to DDLs/DDL-[sp_WhoIsActive_v12_00(Modified)].sql diff --git a/DDLs/DDLs.ssmssqlproj b/DDLs/DDLs.ssmssqlproj index 5ce9b78..73cc1d0 100644 --- a/DDLs/DDLs.ssmssqlproj +++ b/DDLs/DDLs.ssmssqlproj @@ -1,5 +1,5 @@ - + @@ -133,27 +133,45 @@ - - - + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) + + DCL-[grafana-login].sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) - DatabaseMail_Using_GMail.sql + DDL-[DatabaseMail_Using_GMail].sql - + - FirstResponderKit-Install-All-Scripts.sql + DDL-[sp_WhoIsActive_v12_00(Modified)].sql - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True (local) - grafana-login.sql + QRY-DDL-[FirstResponderKit-Install-All-Scripts].sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) + + QRY-SELECT-[Find-Missing-Tables-For-Purging].sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:SQLEXPRESS:True + SQLEXPRESS + + QRY-UPDATE-[SQLAgentJobsThreshold].sql - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) SCH-BlitzIndex_Mode0-Partitioning.sql @@ -325,9 +343,15 @@ SCH-Job-[(dba) Get-AllServerAgHealthState].sql + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True + localhost + + SCH-Job-[(dba) Get-AllServerAlertHistory].sql + - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True + localhost SCH-Job-[(dba) Get-AllServerBackups].sql @@ -344,8 +368,8 @@ SCH-Job-[(dba) Get-AllServerCollectionLatencyInfo].sql - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True + localhost SCH-Job-[(dba) Get-AllServerDashboardMail].sql @@ -488,8 +512,8 @@ SCH-Job-[Update-SQLMonitorIPAddress].sql - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) SCH-Linked-Servers-Sample.sql @@ -499,12 +523,6 @@ SCH-sp_WhatIsRunning.sql - - - - - SCH-sp_WhoIsActive_v12_00(Modified).sql - @@ -572,9 +590,9 @@ SCH-usp_collect_memory_clerks.sql - 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:Experiment:False:sa - Experiment - sa + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True + localhost + SCH-usp_collect_performance_metrics.sql @@ -620,8 +638,8 @@ SCH-usp_compute_LAMA_targets.sql - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) SCH-usp_create_agent_alerts.sql @@ -632,8 +650,8 @@ SCH-usp_enable_page_compression.sql - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:localhost:True + localhost SCH-usp_GetAllServerCollectedData.sql @@ -710,8 +728,8 @@ SCH-usp_wrapper_CollectPrivilegedInfo.sql - - + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True + (local) SCH-usp_wrapper_GetAllServerCollectedData.sql @@ -733,12 +751,6 @@ SCH-WhoIsActive-Partitioning.sql - - 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:SQLEXPRESS:True - SQLEXPRESS - - Update-SQLAgentJobsThreshold.sql - diff --git a/DDLs/QRY-DDL-[FirstResponderKit-Install-All-Scripts].sql b/DDLs/QRY-DDL-[FirstResponderKit-Install-All-Scripts].sql new file mode 100644 index 0000000..2a41a2b --- /dev/null +++ b/DDLs/QRY-DDL-[FirstResponderKit-Install-All-Scripts].sql @@ -0,0 +1,47509 @@ +USE master +go + +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; +SET ANSI_WARNINGS ON; +SET ARITHABORT ON; +SET CONCAT_NULL_YIELDS_NULL ON; +SET QUOTED_IDENTIFIER ON; +SET STATISTICS IO OFF; +SET STATISTICS TIME OFF; +GO + +IF OBJECT_ID('dbo.sp_AllNightLog_Setup') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_AllNightLog_Setup AS RETURN 0;'); +GO + + +ALTER PROCEDURE dbo.sp_AllNightLog_Setup + @RPOSeconds BIGINT = 30, + @RTOSeconds BIGINT = 30, + @BackupPath NVARCHAR(MAX) = NULL, + @RestorePath NVARCHAR(MAX) = NULL, + @Jobs TINYINT = 10, + @RunSetup BIT = 0, + @UpdateSetup BIT = 0, + @EnableBackupJobs INT = NULL, + @EnableRestoreJobs INT = NULL, + @Debug BIT = 0, + @FirstFullBackup BIT = 0, + @FirstDiffBackup BIT = 0, + @MoveFiles BIT = 1, + @Help BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS +SET NOCOUNT ON; +SET STATISTICS XML OFF; + +BEGIN; + +SELECT @Version = '8.16', @VersionDate = '20230820'; + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + +IF @Help = 1 + +BEGIN + + PRINT ' + /* + + + sp_AllNightLog_Setup from http://FirstResponderKit.org + + This script sets up a database, tables, rows, and jobs for sp_AllNightLog, including: + + * Creates a database + * Right now it''s hard-coded to use msdbCentral, that might change later + + * Creates tables in that database! + * dbo.backup_configuration + * Hold variables used by stored proc to make runtime decisions + * RPO: Seconds, how often we look for databases that need log backups + * Backup Path: The path we feed to Ola H''s backup proc + * dbo.backup_worker + * Holds list of databases and some information that helps our Agent jobs figure out if they need to take another log backup + + * Creates tables in msdb + * dbo.restore_configuration + * Holds variables used by stored proc to make runtime decisions + * RTO: Seconds, how often to look for log backups to restore + * Restore Path: The path we feed to sp_DatabaseRestore + * Move Files: Whether to move files to default data/log directories. + * dbo.restore_worker + * Holds list of databases and some information that helps our Agent jobs figure out if they need to look for files to restore + + * Creates agent jobs + * 1 job that polls sys.databases for new entries + * 10 jobs that run to take log backups + * Based on a queue table + * Requires Ola Hallengren''s Database Backup stored proc + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000! And really, maybe not even anything less than 2016. Heh. + - The repository database name is hard-coded to msdbCentral. + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + + Parameter explanations: + + @RunSetup BIT, defaults to 0. When this is set to 1, it will run the setup portion to create database, tables, and worker jobs. + @UpdateSetup BIT, defaults to 0. When set to 1, will update existing configs for RPO/RTO and database backup/restore paths. + @RPOSeconds BIGINT, defaults to 30. Value in seconds you want to use to determine if a new log backup needs to be taken. + @BackupPath NVARCHAR(MAX), This is REQUIRED if @Runsetup=1. This tells Ola''s job where to put backups. + @MoveFiles BIT, defaults to 1. When this is set to 1, it will move files to default data/log directories + @Debug BIT, defaults to 0. Whent this is set to 1, it prints out dynamic SQL commands + + Sample call: + EXEC dbo.sp_AllNightLog_Setup + @RunSetup = 1, + @RPOSeconds = 30, + @BackupPath = N''M:\MSSQL\Backup'', + @Debug = 1 + + + For more documentation: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + + */'; + +RETURN; +END; /* IF @Help = 1 */ + +DECLARE @database NVARCHAR(128) = NULL; --Holds the database that's currently being processed +DECLARE @error_number INT = NULL; --Used for TRY/CATCH +DECLARE @error_severity INT; --Used for TRY/CATCH +DECLARE @error_state INT; --Used for TRY/CATCH +DECLARE @msg NVARCHAR(4000) = N''; --Used for RAISERROR +DECLARE @rpo INT; --Used to hold the RPO value in our configuration table +DECLARE @backup_path NVARCHAR(MAX); --Used to hold the backup path in our configuration table +DECLARE @db_sql NVARCHAR(MAX) = N''; --Used to hold the dynamic SQL to create msdbCentral +DECLARE @tbl_sql NVARCHAR(MAX) = N''; --Used to hold the dynamic SQL that creates tables in msdbCentral +DECLARE @database_name NVARCHAR(256) = N'msdbCentral'; --Used to hold the name of the database we create to centralize data + --Right now it's hardcoded to msdbCentral, but I made it dynamic in case that changes down the line + + +/*These variables control the loop to create/modify jobs*/ +DECLARE @job_sql NVARCHAR(MAX) = N''; --Used to hold the dynamic SQL that creates Agent jobs +DECLARE @counter INT = 0; --For looping to create 10 Agent jobs +DECLARE @job_category NVARCHAR(MAX) = N'''Database Maintenance'''; --Job category +DECLARE @job_owner NVARCHAR(128) = QUOTENAME(SUSER_SNAME(0x01), ''''); -- Admin user/owner +DECLARE @jobs_to_change TABLE(name SYSNAME); -- list of jobs we need to enable or disable +DECLARE @current_job_name SYSNAME; -- While looping through Agent jobs to enable or disable +DECLARE @active_start_date INT = (CONVERT(INT, CONVERT(VARCHAR(10), GETDATE(), 112))); +DECLARE @started_waiting_for_jobs DATETIME; --We need to wait for a while when disabling jobs + +/*Specifically for Backups*/ +DECLARE @job_name_backups NVARCHAR(MAX) = N'''sp_AllNightLog_Backup_Job_'''; --Name of log backup job +DECLARE @job_description_backups NVARCHAR(MAX) = N'''This is a worker for the purposes of taking log backups from msdbCentral.dbo.backup_worker queue table.'''; --Job description +DECLARE @job_command_backups NVARCHAR(MAX) = N'''EXEC sp_AllNightLog @Backup = 1'''; --Command the Agent job will run + +/*Specifically for Restores*/ +DECLARE @job_name_restores NVARCHAR(MAX) = N'''sp_AllNightLog_Restore_Job_'''; --Name of log backup job +DECLARE @job_description_restores NVARCHAR(MAX) = N'''This is a worker for the purposes of restoring log backups from msdb.dbo.restore_worker queue table.'''; --Job description +DECLARE @job_command_restores NVARCHAR(MAX) = N'''EXEC sp_AllNightLog @Restore = 1'''; --Command the Agent job will run + + +/* + +Sanity check some variables + +*/ + + + +IF ((@RunSetup = 0 OR @RunSetup IS NULL) AND (@UpdateSetup = 0 OR @UpdateSetup IS NULL)) + + BEGIN + + RAISERROR('You have to either run setup or update setup. You can''t not do neither nor, if you follow. Or not.', 0, 1) WITH NOWAIT; + + RETURN; + + END; + + +/* + +Should be a positive number + +*/ + +IF (@RPOSeconds < 0) + + BEGIN + RAISERROR('Please choose a positive number for @RPOSeconds', 0, 1) WITH NOWAIT; + + RETURN; + END; + + +/* + +Probably shouldn't be more than 20 + +*/ + +IF (@Jobs > 20) OR (@Jobs < 1) + + BEGIN + RAISERROR('We advise sticking with 1-20 jobs.', 0, 1) WITH NOWAIT; + + RETURN; + END; + +/* + +Probably shouldn't be more than 4 hours + +*/ + +IF (@RPOSeconds >= 14400) + BEGIN + + RAISERROR('If your RPO is really 4 hours, perhaps you''d be interested in a more modest recovery model, like SIMPLE?', 0, 1) WITH NOWAIT; + + RETURN; + END; + + +/* + +Can't enable both the backup and restore jobs at the same time + +*/ + +IF @EnableBackupJobs = 1 AND @EnableRestoreJobs = 1 + BEGIN + + RAISERROR('You are not allowed to enable both the backup and restore jobs at the same time. Pick one, bucko.', 0, 1) WITH NOWAIT; + + RETURN; + END; + +/* +Make sure xp_cmdshell is enabled +*/ +IF NOT EXISTS (SELECT * FROM sys.configurations WHERE name = 'xp_cmdshell' AND value_in_use = 1) + BEGIN + RAISERROR('xp_cmdshell must be enabled so we can get directory contents to check for new databases to restore.', 0, 1) WITH NOWAIT + + RETURN; + END + +/* +Make sure Ola Hallengren's scripts are installed in same database +*/ +DECLARE @CurrentDatabaseContext nvarchar(128) = DB_NAME(); +IF NOT EXISTS (SELECT * FROM sys.procedures WHERE name = 'CommandExecute') + BEGIN + RAISERROR('Ola Hallengren''s CommandExecute must be installed in the same database (%s) as SQL Server First Responder Kit. More info: http://ola.hallengren.com', 0, 1, @CurrentDatabaseContext) WITH NOWAIT; + + RETURN; + END +IF NOT EXISTS (SELECT * FROM sys.procedures WHERE name = 'DatabaseBackup') + BEGIN + RAISERROR('Ola Hallengren''s DatabaseBackup must be installed in the same database (%s) as SQL Server First Responder Kit. More info: http://ola.hallengren.com', 0, 1, @CurrentDatabaseContext) WITH NOWAIT; + + RETURN; + END + +/* +Make sure sp_DatabaseRestore is installed in same database +*/ +IF NOT EXISTS (SELECT * FROM sys.procedures WHERE name = 'sp_DatabaseRestore') + BEGIN + RAISERROR('sp_DatabaseRestore must be installed in the same database (%s) as SQL Server First Responder Kit. To get it: http://FirstResponderKit.org', 0, 1, @CurrentDatabaseContext) WITH NOWAIT; + + RETURN; + END + +/* + +Basic path sanity checks + +*/ + +IF @RunSetup = 1 and @BackupPath is NULL + BEGIN + + RAISERROR('@BackupPath is required during setup', 0, 1) WITH NOWAIT; + + RETURN; + END + +IF (@BackupPath NOT LIKE '[c-zC-Z]:\%') --Local path, don't think anyone has A or B drives +AND (@BackupPath NOT LIKE '\\[a-zA-Z0-9]%\%') --UNC path + + BEGIN + RAISERROR('Are you sure that''s a real path?', 0, 1) WITH NOWAIT; + + RETURN; + END; + +/* + +If you want to update the table, one of these has to not be NULL + +*/ + +IF @UpdateSetup = 1 + AND ( @RPOSeconds IS NULL + AND @BackupPath IS NULL + AND @RPOSeconds IS NULL + AND @RestorePath IS NULL + AND @EnableBackupJobs IS NULL + AND @EnableRestoreJobs IS NULL + ) + + BEGIN + + RAISERROR('If you want to update configuration settings, they can''t be NULL. Please Make sure @RPOSeconds / @RTOSeconds or @BackupPath / @RestorePath has a value', 0, 1) WITH NOWAIT; + + RETURN; + + END; + + +IF @UpdateSetup = 1 + GOTO UpdateConfigs; + +IF @RunSetup = 1 +BEGIN + BEGIN TRY + + BEGIN + + + /* + + First check to see if Agent is running -- we'll get errors if it's not + + */ + + + IF ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_services' ) IS NOT NULL + + BEGIN + + IF EXISTS ( + SELECT 1 + FROM sys.dm_server_services + WHERE servicename LIKE 'SQL Server Agent%' + AND status_desc = 'Stopped' + ) + + BEGIN + + RAISERROR('SQL Server Agent is not currently running -- it needs to be enabled to add backup worker jobs and the new database polling job', 0, 1) WITH NOWAIT; + + RETURN; + + END; + + END + + + BEGIN + + + /* + + Check to see if the database exists + + */ + + RAISERROR('Checking for msdbCentral', 0, 1) WITH NOWAIT; + + SET @db_sql += N' + + IF DATABASEPROPERTYEX(' + QUOTENAME(@database_name, '''') + ', ''Status'') IS NULL + + BEGIN + + RAISERROR(''Creating msdbCentral'', 0, 1) WITH NOWAIT; + + CREATE DATABASE ' + QUOTENAME(@database_name) + '; + + ALTER DATABASE ' + QUOTENAME(@database_name) + ' SET RECOVERY FULL; + + END + + '; + + + IF @Debug = 1 + BEGIN + RAISERROR(@db_sql, 0, 1) WITH NOWAIT; + END; + + + IF @db_sql IS NULL + BEGIN + RAISERROR('@db_sql is NULL for some reason', 0, 1) WITH NOWAIT; + END; + + + EXEC sp_executesql @db_sql; + + + /* + + Check for tables and stuff + + */ + + + RAISERROR('Checking for tables in msdbCentral', 0, 1) WITH NOWAIT; + + SET @tbl_sql += N' + + USE ' + QUOTENAME(@database_name) + ' + + + IF OBJECT_ID(''' + QUOTENAME(@database_name) + '.dbo.backup_configuration'') IS NULL + + BEGIN + + RAISERROR(''Creating table dbo.backup_configuration'', 0, 1) WITH NOWAIT; + + CREATE TABLE dbo.backup_configuration ( + database_name NVARCHAR(256), + configuration_name NVARCHAR(512), + configuration_description NVARCHAR(512), + configuration_setting NVARCHAR(MAX) + ); + + END + + ELSE + + BEGIN + + + RAISERROR(''Backup configuration table exists, truncating'', 0, 1) WITH NOWAIT; + + + TRUNCATE TABLE dbo.backup_configuration + + + END + + + RAISERROR(''Inserting configuration values'', 0, 1) WITH NOWAIT; + + + INSERT dbo.backup_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES (''all'', ''log backup frequency'', ''The length of time in second between Log Backups.'', ''' + CONVERT(NVARCHAR(10), @RPOSeconds) + '''); + + INSERT dbo.backup_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES (''all'', ''log backup path'', ''The path to which Log Backups should go.'', ''' + @BackupPath + '''); + + INSERT dbo.backup_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES (''all'', ''change backup type'', ''For Ola Hallengren DatabaseBackup @ChangeBackupType param: Y = escalate to fulls, MSDB = escalate by checking msdb backup history.'', ''MSDB''); + + INSERT dbo.backup_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES (''all'', ''encrypt'', ''For Ola Hallengren DatabaseBackup: Y = encrypt the backup. N (default) = do not encrypt.'', NULL); + + INSERT dbo.backup_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES (''all'', ''encryptionalgorithm'', ''For Ola Hallengren DatabaseBackup: native 2014 choices include TRIPLE_DES_3KEY, AES_128, AES_192, AES_256.'', NULL); + + INSERT dbo.backup_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES (''all'', ''servercertificate'', ''For Ola Hallengren DatabaseBackup: server certificate that is used to encrypt the backup.'', NULL); + + + IF OBJECT_ID(''' + QUOTENAME(@database_name) + '.dbo.backup_worker'') IS NULL + + BEGIN + + + RAISERROR(''Creating table dbo.backup_worker'', 0, 1) WITH NOWAIT; + + CREATE TABLE dbo.backup_worker ( + id INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + database_name NVARCHAR(256), + last_log_backup_start_time DATETIME DEFAULT ''19000101'', + last_log_backup_finish_time DATETIME DEFAULT ''99991231'', + is_started BIT DEFAULT 0, + is_completed BIT DEFAULT 0, + error_number INT DEFAULT NULL, + last_error_date DATETIME DEFAULT NULL, + ignore_database BIT DEFAULT 0, + full_backup_required BIT DEFAULT ' + CASE WHEN @FirstFullBackup = 0 THEN N'0,' ELSE N'1,' END + CHAR(10) + + N'diff_backup_required BIT DEFAULT ' + CASE WHEN @FirstDiffBackup = 0 THEN N'0' ELSE N'1' END + CHAR(10) + + N'); + + END; + + ELSE + + BEGIN + + + RAISERROR(''Backup worker table exists, truncating'', 0, 1) WITH NOWAIT; + + + TRUNCATE TABLE dbo.backup_worker + + + END + + + RAISERROR(''Inserting databases for backups'', 0, 1) WITH NOWAIT; + + INSERT ' + QUOTENAME(@database_name) + '.dbo.backup_worker (database_name) + SELECT d.name + FROM sys.databases d + WHERE NOT EXISTS ( + SELECT * + FROM msdbCentral.dbo.backup_worker bw + WHERE bw.database_name = d.name + ) + AND d.database_id > 4; + + '; + + + IF @Debug = 1 + BEGIN + SET @msg = SUBSTRING(@tbl_sql, 0, 2044) + RAISERROR(@msg, 0, 1) WITH NOWAIT; + SET @msg = SUBSTRING(@tbl_sql, 2044, 4088) + RAISERROR(@msg, 0, 1) WITH NOWAIT; + SET @msg = SUBSTRING(@tbl_sql, 4088, 6132) + RAISERROR(@msg, 0, 1) WITH NOWAIT; + SET @msg = SUBSTRING(@tbl_sql, 6132, 8176) + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + + IF @tbl_sql IS NULL + BEGIN + RAISERROR('@tbl_sql is NULL for some reason', 0, 1) WITH NOWAIT; + END; + + + EXEC sp_executesql @tbl_sql; + + + /* + + This section creates tables for restore workers to work off of + + */ + + + /* + + In search of msdb + + */ + + RAISERROR('Checking for msdb. Yeah, I know...', 0, 1) WITH NOWAIT; + + IF DATABASEPROPERTYEX('msdb', 'Status') IS NULL + + BEGIN + + RAISERROR('YOU HAVE NO MSDB WHY?!', 0, 1) WITH NOWAIT; + + RETURN; + + END; + + + /* In search of restore_configuration */ + + RAISERROR('Checking for Restore Worker tables in msdb', 0, 1) WITH NOWAIT; + + IF OBJECT_ID('msdb.dbo.restore_configuration') IS NULL + + BEGIN + + RAISERROR('Creating restore_configuration table in msdb', 0, 1) WITH NOWAIT; + + CREATE TABLE msdb.dbo.restore_configuration ( + database_name NVARCHAR(256), + configuration_name NVARCHAR(512), + configuration_description NVARCHAR(512), + configuration_setting NVARCHAR(MAX) + ); + + END; + + + ELSE + + + BEGIN + + RAISERROR('Restore configuration table exists, truncating', 0, 1) WITH NOWAIT; + + TRUNCATE TABLE msdb.dbo.restore_configuration; + + END; + + + RAISERROR('Inserting configuration values to msdb.dbo.restore_configuration', 0, 1) WITH NOWAIT; + + INSERT msdb.dbo.restore_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES ('all', 'log restore frequency', 'The length of time in second between Log Restores.', @RTOSeconds); + + INSERT msdb.dbo.restore_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES ('all', 'log restore path', 'The path to which Log Restores come from.', @RestorePath); + + INSERT msdb.dbo.restore_configuration (database_name, configuration_name, configuration_description, configuration_setting) + VALUES ('all', 'move files', 'Determines if we move database files to default data/log directories.', @MoveFiles); + + IF OBJECT_ID('msdb.dbo.restore_worker') IS NULL + + BEGIN + + + RAISERROR('Creating table msdb.dbo.restore_worker', 0, 1) WITH NOWAIT; + + CREATE TABLE msdb.dbo.restore_worker ( + id INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + database_name NVARCHAR(256), + last_log_restore_start_time DATETIME DEFAULT '19000101', + last_log_restore_finish_time DATETIME DEFAULT '99991231', + is_started BIT DEFAULT 0, + is_completed BIT DEFAULT 0, + error_number INT DEFAULT NULL, + last_error_date DATETIME DEFAULT NULL, + ignore_database BIT DEFAULT 0, + full_backup_required BIT DEFAULT 0, + diff_backup_required BIT DEFAULT 0 + ); + + + RAISERROR('Inserting databases for restores', 0, 1) WITH NOWAIT; + + INSERT msdb.dbo.restore_worker (database_name) + SELECT d.name + FROM sys.databases d + WHERE NOT EXISTS ( + SELECT * + FROM msdb.dbo.restore_worker bw + WHERE bw.database_name = d.name + ) + AND d.database_id > 4; + + + END; + + + + /* + + Add Jobs + + */ + + + + /* + + Look for our ten second schedule -- all jobs use this to restart themselves if they fail + + Fun fact: you can add the same schedule name multiple times, so we don't want to just stick it in there + + */ + + + RAISERROR('Checking for ten second schedule', 0, 1) WITH NOWAIT; + + IF NOT EXISTS ( + SELECT 1 + FROM msdb.dbo.sysschedules + WHERE name = 'ten_seconds' + ) + + BEGIN + + + RAISERROR('Creating ten second schedule', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_add_schedule @schedule_name= ten_seconds, + @enabled = 1, + @freq_type = 4, + @freq_interval = 1, + @freq_subday_type = 2, + @freq_subday_interval = 10, + @freq_relative_interval = 0, + @freq_recurrence_factor = 0, + @active_start_date = @active_start_date, + @active_end_date = 99991231, + @active_start_time = 0, + @active_end_time = 235959; + + END; + + + /* + + Look for Backup Pollster job -- this job sets up our watcher for new databases to back up + + */ + + + RAISERROR('Checking for pollster job', 0, 1) WITH NOWAIT; + + + IF NOT EXISTS ( + SELECT 1 + FROM msdb.dbo.sysjobs + WHERE name = 'sp_AllNightLog_PollForNewDatabases' + ) + + + BEGIN + + + RAISERROR('Creating pollster job', 0, 1) WITH NOWAIT; + + IF @EnableBackupJobs = 1 + BEGIN + EXEC msdb.dbo.sp_add_job @job_name = sp_AllNightLog_PollForNewDatabases, + @description = 'This is a worker for the purposes of polling sys.databases for new entries to insert to the worker queue table.', + @category_name = 'Database Maintenance', + @owner_login_name = 'sa', + @enabled = 1; + END + ELSE + BEGIN + EXEC msdb.dbo.sp_add_job @job_name = sp_AllNightLog_PollForNewDatabases, + @description = 'This is a worker for the purposes of polling sys.databases for new entries to insert to the worker queue table.', + @category_name = 'Database Maintenance', + @owner_login_name = 'sa', + @enabled = 0; + END + + + RAISERROR('Adding job step', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_add_jobstep @job_name = sp_AllNightLog_PollForNewDatabases, + @step_name = sp_AllNightLog_PollForNewDatabases, + @subsystem = 'TSQL', + @command = 'EXEC sp_AllNightLog @PollForNewDatabases = 1'; + + + + RAISERROR('Adding job server', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_add_jobserver @job_name = sp_AllNightLog_PollForNewDatabases; + + + + RAISERROR('Attaching schedule', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_attach_schedule @job_name = sp_AllNightLog_PollForNewDatabases, + @schedule_name = ten_seconds; + + + END; + + + + /* + + Look for Restore Pollster job -- this job sets up our watcher for new databases to back up + + */ + + + RAISERROR('Checking for restore pollster job', 0, 1) WITH NOWAIT; + + + IF NOT EXISTS ( + SELECT 1 + FROM msdb.dbo.sysjobs + WHERE name = 'sp_AllNightLog_PollDiskForNewDatabases' + ) + + + BEGIN + + + RAISERROR('Creating restore pollster job', 0, 1) WITH NOWAIT; + + + IF @EnableRestoreJobs = 1 + BEGIN + EXEC msdb.dbo.sp_add_job @job_name = sp_AllNightLog_PollDiskForNewDatabases, + @description = 'This is a worker for the purposes of polling your restore path for new entries to insert to the worker queue table.', + @category_name = 'Database Maintenance', + @owner_login_name = 'sa', + @enabled = 1; + END + ELSE + BEGIN + EXEC msdb.dbo.sp_add_job @job_name = sp_AllNightLog_PollDiskForNewDatabases, + @description = 'This is a worker for the purposes of polling your restore path for new entries to insert to the worker queue table.', + @category_name = 'Database Maintenance', + @owner_login_name = 'sa', + @enabled = 0; + END + + + + RAISERROR('Adding restore job step', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_add_jobstep @job_name = sp_AllNightLog_PollDiskForNewDatabases, + @step_name = sp_AllNightLog_PollDiskForNewDatabases, + @subsystem = 'TSQL', + @command = 'EXEC sp_AllNightLog @PollDiskForNewDatabases = 1'; + + + + RAISERROR('Adding restore job server', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_add_jobserver @job_name = sp_AllNightLog_PollDiskForNewDatabases; + + + + RAISERROR('Attaching schedule', 0, 1) WITH NOWAIT; + + + EXEC msdb.dbo.sp_attach_schedule @job_name = sp_AllNightLog_PollDiskForNewDatabases, + @schedule_name = ten_seconds; + + + END; + + + + /* + + This section creates @Jobs (quantity) of worker jobs to take log backups with + + They work in a queue + + It's queuete + + */ + + + RAISERROR('Checking for sp_AllNightLog backup jobs', 0, 1) WITH NOWAIT; + + + SELECT @counter = COUNT(*) + 1 + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp[_]AllNightLog[_]Backup[_]%'; + + SET @msg = 'Found ' + CONVERT(NVARCHAR(10), (@counter - 1)) + ' backup jobs -- ' + CASE WHEN @counter < @Jobs THEN + 'starting loop!' + WHEN @counter >= @Jobs THEN 'skipping loop!' + ELSE 'Oh woah something weird happened!' + END; + + RAISERROR(@msg, 0, 1) WITH NOWAIT; + + + WHILE @counter <= @Jobs + + + BEGIN + + + RAISERROR('Setting job name', 0, 1) WITH NOWAIT; + + SET @job_name_backups = N'sp_AllNightLog_Backup_' + CASE WHEN @counter < 10 THEN N'0' + CONVERT(NVARCHAR(10), @counter) + WHEN @counter >= 10 THEN CONVERT(NVARCHAR(10), @counter) + END; + + + RAISERROR('Setting @job_sql', 0, 1) WITH NOWAIT; + + + SET @job_sql = N' + + EXEC msdb.dbo.sp_add_job @job_name = ' + @job_name_backups + ', + @description = ' + @job_description_backups + ', + @category_name = ' + @job_category + ', + @owner_login_name = ' + @job_owner + ','; + IF @EnableBackupJobs = 1 + BEGIN + SET @job_sql = @job_sql + ' @enabled = 1; '; + END + ELSE + BEGIN + SET @job_sql = @job_sql + ' @enabled = 0; '; + END + + + SET @job_sql = @job_sql + ' + EXEC msdb.dbo.sp_add_jobstep @job_name = ' + @job_name_backups + ', + @step_name = ' + @job_name_backups + ', + @subsystem = ''TSQL'', + @command = ' + @job_command_backups + '; + + + EXEC msdb.dbo.sp_add_jobserver @job_name = ' + @job_name_backups + '; + + + EXEC msdb.dbo.sp_attach_schedule @job_name = ' + @job_name_backups + ', + @schedule_name = ten_seconds; + + '; + + + SET @counter += 1; + + + IF @Debug = 1 + BEGIN + RAISERROR(@job_sql, 0, 1) WITH NOWAIT; + END; + + + IF @job_sql IS NULL + BEGIN + RAISERROR('@job_sql is NULL for some reason', 0, 1) WITH NOWAIT; + END; + + + EXEC sp_executesql @job_sql; + + + END; + + + + /* + + This section creates @Jobs (quantity) of worker jobs to restore logs with + + They too work in a queue + + Like a queue-t 3.14 + + */ + + + RAISERROR('Checking for sp_AllNightLog Restore jobs', 0, 1) WITH NOWAIT; + + + SELECT @counter = COUNT(*) + 1 + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp[_]AllNightLog[_]Restore[_]%'; + + SET @msg = 'Found ' + CONVERT(NVARCHAR(10), (@counter - 1)) + ' restore jobs -- ' + CASE WHEN @counter < @Jobs THEN + 'starting loop!' + WHEN @counter >= @Jobs THEN 'skipping loop!' + ELSE 'Oh woah something weird happened!' + END; + + RAISERROR(@msg, 0, 1) WITH NOWAIT; + + + WHILE @counter <= @Jobs + + + BEGIN + + + RAISERROR('Setting job name', 0, 1) WITH NOWAIT; + + SET @job_name_restores = N'sp_AllNightLog_Restore_' + CASE WHEN @counter < 10 THEN N'0' + CONVERT(NVARCHAR(10), @counter) + WHEN @counter >= 10 THEN CONVERT(NVARCHAR(10), @counter) + END; + + + RAISERROR('Setting @job_sql', 0, 1) WITH NOWAIT; + + + SET @job_sql = N' + + EXEC msdb.dbo.sp_add_job @job_name = ' + @job_name_restores + ', + @description = ' + @job_description_restores + ', + @category_name = ' + @job_category + ', + @owner_login_name = ' + @job_owner + ','; + IF @EnableRestoreJobs = 1 + BEGIN + SET @job_sql = @job_sql + ' @enabled = 1; '; + END + ELSE + BEGIN + SET @job_sql = @job_sql + ' @enabled = 0; '; + END + + + SET @job_sql = @job_sql + ' + + EXEC msdb.dbo.sp_add_jobstep @job_name = ' + @job_name_restores + ', + @step_name = ' + @job_name_restores + ', + @subsystem = ''TSQL'', + @command = ' + @job_command_restores + '; + + + EXEC msdb.dbo.sp_add_jobserver @job_name = ' + @job_name_restores + '; + + + EXEC msdb.dbo.sp_attach_schedule @job_name = ' + @job_name_restores + ', + @schedule_name = ten_seconds; + + '; + + + SET @counter += 1; + + + IF @Debug = 1 + BEGIN + RAISERROR(@job_sql, 0, 1) WITH NOWAIT; + END; + + + IF @job_sql IS NULL + BEGIN + RAISERROR('@job_sql is NULL for some reason', 0, 1) WITH NOWAIT; + END; + + + EXEC sp_executesql @job_sql; + + + END; + + + RAISERROR('Setup complete!', 0, 1) WITH NOWAIT; + + END; --End for the Agent job creation + + END;--End for Database and Table creation + + END TRY + + BEGIN CATCH + + + SELECT @msg = N'Error occurred during setup: ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + END CATCH; + +END; /* IF @RunSetup = 1 */ + +RETURN; + + +UpdateConfigs: + +IF @UpdateSetup = 1 + + BEGIN + + /* If we're enabling backup jobs, we may need to run restore with recovery on msdbCentral to bring it online: */ + IF @EnableBackupJobs = 1 AND EXISTS (SELECT * FROM sys.databases WHERE name = 'msdbCentral' AND state = 1) + BEGIN + RAISERROR('msdbCentral exists, but is in restoring state. Running restore with recovery...', 0, 1) WITH NOWAIT; + + BEGIN TRY + RESTORE DATABASE [msdbCentral] WITH RECOVERY; + END TRY + + BEGIN CATCH + + SELECT @error_number = ERROR_NUMBER(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + SELECT @msg = N'Error running restore with recovery on msdbCentral, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + END CATCH; + + END + + /* Only check for this after trying to restore msdbCentral: */ + IF @EnableBackupJobs = 1 AND NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'msdbCentral' AND state = 0) + BEGIN + RAISERROR('msdbCentral is not online. Repair that first, then try to enable backup jobs.', 0, 1) WITH NOWAIT; + RETURN + END + + + IF OBJECT_ID('msdbCentral.dbo.backup_configuration') IS NOT NULL + + RAISERROR('Found backup config, checking variables...', 0, 1) WITH NOWAIT; + + BEGIN + + BEGIN TRY + + + IF @RPOSeconds IS NOT NULL + + + BEGIN + + RAISERROR('Attempting to update RPO setting', 0, 1) WITH NOWAIT; + + UPDATE c + SET c.configuration_setting = CONVERT(NVARCHAR(10), @RPOSeconds) + FROM msdbCentral.dbo.backup_configuration AS c + WHERE c.configuration_name = N'log backup frequency'; + + END; + + + IF @BackupPath IS NOT NULL + + BEGIN + + RAISERROR('Attempting to update Backup Path setting', 0, 1) WITH NOWAIT; + + UPDATE c + SET c.configuration_setting = @BackupPath + FROM msdbCentral.dbo.backup_configuration AS c + WHERE c.configuration_name = N'log backup path'; + + + END; + + END TRY + + + BEGIN CATCH + + + SELECT @error_number = ERROR_NUMBER(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + SELECT @msg = N'Error updating backup configuration setting, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + + END CATCH; + + END; + + + IF OBJECT_ID('msdb.dbo.restore_configuration') IS NOT NULL + + RAISERROR('Found restore config, checking variables...', 0, 1) WITH NOWAIT; + + BEGIN + + BEGIN TRY + + EXEC msdb.dbo.sp_update_schedule @name = ten_seconds, @active_start_date = @active_start_date, @active_start_time = 000000; + + IF @EnableRestoreJobs IS NOT NULL + BEGIN + RAISERROR('Changing restore job status based on @EnableBackupJobs parameter...', 0, 1) WITH NOWAIT; + INSERT INTO @jobs_to_change(name) + SELECT name + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp_AllNightLog_Restore%' OR name = 'sp_AllNightLog_PollDiskForNewDatabases'; + DECLARE jobs_cursor CURSOR FOR + SELECT name + FROM @jobs_to_change + + OPEN jobs_cursor + FETCH NEXT FROM jobs_cursor INTO @current_job_name + + WHILE @@FETCH_STATUS = 0 + BEGIN + RAISERROR(@current_job_name, 0, 1) WITH NOWAIT; + EXEC msdb.dbo.sp_update_job @job_name=@current_job_name,@enabled = @EnableRestoreJobs; + FETCH NEXT FROM jobs_cursor INTO @current_job_name + END + + CLOSE jobs_cursor + DEALLOCATE jobs_cursor + DELETE @jobs_to_change; + END; + + /* If they wanted to turn off restore jobs, wait to make sure that finishes before we start enabling the backup jobs */ + IF @EnableRestoreJobs = 0 + BEGIN + SET @started_waiting_for_jobs = GETDATE(); + SELECT @counter = COUNT(*) + FROM [msdb].[dbo].[sysjobactivity] [ja] + INNER JOIN [msdb].[dbo].[sysjobs] [j] + ON [ja].[job_id] = [j].[job_id] + WHERE [ja].[session_id] = ( + SELECT TOP 1 [session_id] + FROM [msdb].[dbo].[syssessions] + ORDER BY [agent_start_date] DESC + ) + AND [start_execution_date] IS NOT NULL + AND [stop_execution_date] IS NULL + AND [j].[name] LIKE 'sp_AllNightLog_Restore%'; + + WHILE @counter > 0 + BEGIN + IF DATEADD(SS, 120, @started_waiting_for_jobs) < GETDATE() + BEGIN + RAISERROR('OH NOES! We waited 2 minutes and restore jobs are still running. We are stopping here - get a meatbag involved to figure out if restore jobs need to be killed, and the backup jobs will need to be enabled manually.', 16, 1) WITH NOWAIT; + RETURN + END + SET @msg = N'Waiting for ' + CAST(@counter AS NVARCHAR(100)) + N' sp_AllNightLog_Restore job(s) to finish.' + RAISERROR(@msg, 0, 1) WITH NOWAIT; + WAITFOR DELAY '0:00:01'; -- Wait until the restore jobs are fully stopped + + SELECT @counter = COUNT(*) + FROM [msdb].[dbo].[sysjobactivity] [ja] + INNER JOIN [msdb].[dbo].[sysjobs] [j] + ON [ja].[job_id] = [j].[job_id] + WHERE [ja].[session_id] = ( + SELECT TOP 1 [session_id] + FROM [msdb].[dbo].[syssessions] + ORDER BY [agent_start_date] DESC + ) + AND [start_execution_date] IS NOT NULL + AND [stop_execution_date] IS NULL + AND [j].[name] LIKE 'sp_AllNightLog_Restore%'; + END + END /* IF @EnableRestoreJobs = 0 */ + + + IF @EnableBackupJobs IS NOT NULL + BEGIN + RAISERROR('Changing backup job status based on @EnableBackupJobs parameter...', 0, 1) WITH NOWAIT; + INSERT INTO @jobs_to_change(name) + SELECT name + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp_AllNightLog_Backup%' OR name = 'sp_AllNightLog_PollForNewDatabases'; + DECLARE jobs_cursor CURSOR FOR + SELECT name + FROM @jobs_to_change + + OPEN jobs_cursor + FETCH NEXT FROM jobs_cursor INTO @current_job_name + + WHILE @@FETCH_STATUS = 0 + BEGIN + RAISERROR(@current_job_name, 0, 1) WITH NOWAIT; + EXEC msdb.dbo.sp_update_job @job_name=@current_job_name,@enabled = @EnableBackupJobs; + FETCH NEXT FROM jobs_cursor INTO @current_job_name + END + + CLOSE jobs_cursor + DEALLOCATE jobs_cursor + DELETE @jobs_to_change; + END; + + + + IF @RTOSeconds IS NOT NULL + + BEGIN + + RAISERROR('Attempting to update RTO setting', 0, 1) WITH NOWAIT; + + UPDATE c + SET c.configuration_setting = CONVERT(NVARCHAR(10), @RTOSeconds) + FROM msdb.dbo.restore_configuration AS c + WHERE c.configuration_name = N'log restore frequency'; + + END; + + + IF @RestorePath IS NOT NULL + + BEGIN + + RAISERROR('Attempting to update Restore Path setting', 0, 1) WITH NOWAIT; + + UPDATE c + SET c.configuration_setting = @RestorePath + FROM msdb.dbo.restore_configuration AS c + WHERE c.configuration_name = N'log restore path'; + + + END; + + END TRY + + + BEGIN CATCH + + + SELECT @error_number = ERROR_NUMBER(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + SELECT @msg = N'Error updating restore configuration setting, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + + END CATCH; + + END; + + RAISERROR('Update complete!', 0, 1) WITH NOWAIT; + + RETURN; + + END; --End updates to configuration table + + +END; -- Final END for stored proc +GO + +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; +SET ANSI_WARNINGS ON; +SET ARITHABORT ON; +SET CONCAT_NULL_YIELDS_NULL ON; +SET QUOTED_IDENTIFIER ON; +SET STATISTICS IO OFF; +SET STATISTICS TIME OFF; +GO + +IF OBJECT_ID('dbo.sp_AllNightLog') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_AllNightLog AS RETURN 0;') +GO + + +ALTER PROCEDURE dbo.sp_AllNightLog + @PollForNewDatabases BIT = 0, /* Formerly Pollster */ + @Backup BIT = 0, /* Formerly LogShaming */ + @PollDiskForNewDatabases BIT = 0, + @Restore BIT = 0, + @Debug BIT = 0, + @Help BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS +SET NOCOUNT ON; +SET STATISTICS XML OFF; + +BEGIN; + + +SELECT @Version = '8.16', @VersionDate = '20230820'; + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + +IF @Help = 1 + +BEGIN + + PRINT ' + /* + + + sp_AllNightLog from http://FirstResponderKit.org + + * @PollForNewDatabases = 1 polls sys.databases for new entries + * Unfortunately no other way currently to automate new database additions when restored from backups + * No triggers or extended events that easily do this + + * @Backup = 1 polls msdbCentral.dbo.backup_worker for databases not backed up in [RPO], takes LOG backups + * Will switch to a full backup if none exists + + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000! And really, maybe not even anything less than 2016. Heh. + - When restoring encrypted backups, the encryption certificate must already be installed. + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + + Parameter explanations: + + @PollForNewDatabases BIT, defaults to 0. When this is set to 1, runs in a perma-loop to find new entries in sys.databases + @Backup BIT, defaults to 0. When this is set to 1, runs in a perma-loop checking the backup_worker table for databases that need to be backed up + @Debug BIT, defaults to 0. Whent this is set to 1, it prints out dynamic SQL commands + @RPOSeconds BIGINT, defaults to 30. Value in seconds you want to use to determine if a new log backup needs to be taken. + @BackupPath NVARCHAR(MAX), defaults to = ''D:\Backup''. You 99.99999% will need to change this path to something else. This tells Ola''s job where to put backups. + + For more documentation: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + + */'; + +RETURN +END + +DECLARE @database NVARCHAR(128) = NULL; --Holds the database that's currently being processed +DECLARE @error_number INT = NULL; --Used for TRY/CATCH +DECLARE @error_severity INT; --Used for TRY/CATCH +DECLARE @error_state INT; --Used for TRY/CATCH +DECLARE @msg NVARCHAR(4000) = N''; --Used for RAISERROR +DECLARE @rpo INT; --Used to hold the RPO value in our configuration table +DECLARE @rto INT; --Used to hold the RPO value in our configuration table +DECLARE @backup_path NVARCHAR(MAX); --Used to hold the backup path in our configuration table +DECLARE @changebackuptype NVARCHAR(MAX); --Config table: Y = escalate to full backup, MSDB = escalate if MSDB history doesn't show a recent full. +DECLARE @encrypt NVARCHAR(MAX); --Config table: Y = encrypt the backup. N (default) = do not encrypt. +DECLARE @encryptionalgorithm NVARCHAR(MAX); --Config table: native 2014 choices include TRIPLE_DES_3KEY, AES_128, AES_192, AES_256 +DECLARE @servercertificate NVARCHAR(MAX); --Config table: server certificate that is used to encrypt the backup +DECLARE @restore_path_base NVARCHAR(MAX); --Used to hold the base backup path in our configuration table +DECLARE @restore_path_full NVARCHAR(MAX); --Used to hold the full backup path in our configuration table +DECLARE @restore_path_log NVARCHAR(MAX); --Used to hold the log backup path in our configuration table +DECLARE @restore_move_files INT; -- used to hold the move files bit in our configuration table +DECLARE @db_sql NVARCHAR(MAX) = N''; --Used to hold the dynamic SQL to create msdbCentral +DECLARE @tbl_sql NVARCHAR(MAX) = N''; --Used to hold the dynamic SQL that creates tables in msdbCentral +DECLARE @database_name NVARCHAR(256) = N'msdbCentral'; --Used to hold the name of the database we create to centralize data + --Right now it's hardcoded to msdbCentral, but I made it dynamic in case that changes down the line +DECLARE @cmd NVARCHAR(4000) = N'' --Holds dir cmd +DECLARE @FileList TABLE ( BackupFile NVARCHAR(255) ); --Where we dump @cmd +DECLARE @restore_full BIT = 0 --We use this one +DECLARE @only_logs_after NVARCHAR(30) = N'' + + + +/* + +Make sure we're doing something + +*/ + +IF ( + @PollForNewDatabases = 0 + AND @PollDiskForNewDatabases = 0 + AND @Backup = 0 + AND @Restore = 0 + AND @Help = 0 +) + BEGIN + RAISERROR('You don''t seem to have picked an action for this stored procedure to take.', 0, 1) WITH NOWAIT + + RETURN; + END + +/* +Make sure xp_cmdshell is enabled +*/ +IF NOT EXISTS (SELECT * FROM sys.configurations WHERE name = 'xp_cmdshell' AND value_in_use = 1) + BEGIN + RAISERROR('xp_cmdshell must be enabled so we can get directory contents to check for new databases to restore.', 0, 1) WITH NOWAIT + + RETURN; + END + +/* +Make sure Ola Hallengren's scripts are installed in same database +*/ +DECLARE @CurrentDatabaseContext nvarchar(128) = DB_NAME(); +IF NOT EXISTS (SELECT * FROM sys.procedures WHERE name = 'CommandExecute') + BEGIN + RAISERROR('Ola Hallengren''s CommandExecute must be installed in the same database (%s) as SQL Server First Responder Kit. More info: http://ola.hallengren.com', 0, 1, @CurrentDatabaseContext) WITH NOWAIT; + + RETURN; + END +IF NOT EXISTS (SELECT * FROM sys.procedures WHERE name = 'DatabaseBackup') + BEGIN + RAISERROR('Ola Hallengren''s DatabaseBackup must be installed in the same database (%s) as SQL Server First Responder Kit. More info: http://ola.hallengren.com', 0, 1, @CurrentDatabaseContext) WITH NOWAIT; + + RETURN; + END + +/* +Make sure sp_DatabaseRestore is installed in same database +*/ +IF NOT EXISTS (SELECT * FROM sys.procedures WHERE name = 'sp_DatabaseRestore') + BEGIN + RAISERROR('sp_DatabaseRestore must be installed in the same database (%s) as SQL Server First Responder Kit. To get it: http://FirstResponderKit.org', 0, 1, @CurrentDatabaseContext) WITH NOWAIT; + + RETURN; + END + + +IF (@PollDiskForNewDatabases = 1 OR @Restore = 1) AND OBJECT_ID('msdb.dbo.restore_configuration') IS NOT NULL + BEGIN + + IF @Debug = 1 RAISERROR('Checking restore path', 0, 1) WITH NOWAIT; + + SELECT @restore_path_base = CONVERT(NVARCHAR(512), configuration_setting) + FROM msdb.dbo.restore_configuration c + WHERE configuration_name = N'log restore path'; + + + IF @restore_path_base IS NULL + BEGIN + RAISERROR('@restore_path cannot be NULL. Please check the msdb.dbo.restore_configuration table', 0, 1) WITH NOWAIT; + RETURN; + END; + + IF CHARINDEX('**', @restore_path_base) <> 0 + BEGIN + + /* If they passed in a dynamic **DATABASENAME**, stop at that folder looking for databases. More info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/993 */ + IF CHARINDEX('**DATABASENAME**', @restore_path_base) <> 0 + BEGIN + SET @restore_path_base = SUBSTRING(@restore_path_base, 1, CHARINDEX('**DATABASENAME**',@restore_path_base) - 2); + END; + + SET @restore_path_base = REPLACE(@restore_path_base, '**AVAILABILITYGROUP**', ''); + SET @restore_path_base = REPLACE(@restore_path_base, '**BACKUPTYPE**', 'FULL'); + SET @restore_path_base = REPLACE(@restore_path_base, '**SERVERNAME**', REPLACE(CAST(SERVERPROPERTY('servername') AS nvarchar(max)),'\','$')); + + IF CHARINDEX('\',CAST(SERVERPROPERTY('servername') AS nvarchar(max))) > 0 + BEGIN + SET @restore_path_base = REPLACE(@restore_path_base, '**SERVERNAMEWITHOUTINSTANCE**', SUBSTRING(CAST(SERVERPROPERTY('servername') AS nvarchar(max)), 1, (CHARINDEX('\',CAST(SERVERPROPERTY('servername') AS nvarchar(max))) - 1))); + SET @restore_path_base = REPLACE(@restore_path_base, '**INSTANCENAME**', SUBSTRING(CAST(SERVERPROPERTY('servername') AS nvarchar(max)), CHARINDEX('\',CAST(SERVERPROPERTY('servername') AS nvarchar(max))), (LEN(CAST(SERVERPROPERTY('servername') AS nvarchar(max))) - CHARINDEX('\',CAST(SERVERPROPERTY('servername') AS nvarchar(max)))) + 1)); + END + ELSE /* No instance installed */ + BEGIN + SET @restore_path_base = REPLACE(@restore_path_base, '**SERVERNAMEWITHOUTINSTANCE**', CAST(SERVERPROPERTY('servername') AS nvarchar(max))); + SET @restore_path_base = REPLACE(@restore_path_base, '**INSTANCENAME**', 'DEFAULT'); + END + + IF CHARINDEX('**CLUSTER**', @restore_path_base) <> 0 + BEGIN + DECLARE @ClusterName NVARCHAR(128); + IF EXISTS(SELECT * FROM sys.all_objects WHERE name = 'dm_hadr_cluster') + BEGIN + SELECT @ClusterName = cluster_name FROM sys.dm_hadr_cluster; + END + SET @restore_path_base = REPLACE(@restore_path_base, '**CLUSTER**', COALESCE(@ClusterName,'')); + END; + + END /* IF CHARINDEX('**', @restore_path_base) <> 0 */ + + SELECT @restore_move_files = CONVERT(BIT, configuration_setting) + FROM msdb.dbo.restore_configuration c + WHERE configuration_name = N'move files'; + + IF @restore_move_files is NULL + BEGIN + -- Set to default value of 1 + SET @restore_move_files = 1 + END + + END /* IF @PollDiskForNewDatabases = 1 OR @Restore = 1 */ + + +/* + +Certain variables necessarily skip to parts of this script that are irrelevant +in both directions to each other. They are used for other stuff. + +*/ + + +/* + +Pollster use happens strictly to check for new databases in sys.databases to place them in a worker queue + +*/ + +IF @PollForNewDatabases = 1 + GOTO Pollster; + +/* + +LogShamer happens when we need to find and assign work to a worker job for backups + +*/ + +IF @Backup = 1 + GOTO LogShamer; + +/* + +Pollster use happens strictly to check for new databases in sys.databases to place them in a worker queue + +*/ + +IF @PollDiskForNewDatabases = 1 + GOTO DiskPollster; + + +/* + +Restoregasm Addict happens when we need to find and assign work to a worker job for restores + +*/ + +IF @Restore = 1 + GOTO Restoregasm_Addict; + + + +/* + +Begin Polling section + +*/ + + + +/* + +This section runs in a loop checking for new databases added to the server, or broken backups + +*/ + + +Pollster: + + IF @Debug = 1 RAISERROR('Beginning Pollster', 0, 1) WITH NOWAIT; + + IF OBJECT_ID('msdbCentral.dbo.backup_worker') IS NOT NULL + + BEGIN + + WHILE @PollForNewDatabases = 1 + + BEGIN + + BEGIN TRY + + IF @Debug = 1 RAISERROR('Checking for new databases...', 0, 1) WITH NOWAIT; + + /* + + Look for new non-system databases -- there should probably be additional filters here for accessibility, etc. + + */ + + INSERT msdbCentral.dbo.backup_worker (database_name) + SELECT d.name + FROM sys.databases d + WHERE NOT EXISTS ( + SELECT 1 + FROM msdbCentral.dbo.backup_worker bw + WHERE bw.database_name = d.name + ) + AND d.database_id > 4; + + IF @Debug = 1 RAISERROR('Checking for wayward databases', 0, 1) WITH NOWAIT; + + /* + + This section aims to find databases that have + * Had a log backup ever (the default for finish time is 9999-12-31, so anything with a more recent finish time has had a log backup) + * Not had a log backup start in the last 5 minutes (this could be trouble! or a really big log backup) + * Also checks msdb.dbo.backupset to make sure the database has a full backup associated with it (otherwise it's the first full, and we don't need to start taking log backups yet) + + */ + + IF EXISTS ( + + SELECT 1 + FROM msdbCentral.dbo.backup_worker bw WITH (READPAST) + WHERE bw.last_log_backup_finish_time < '99991231' + AND bw.last_log_backup_start_time < DATEADD(SECOND, (@rpo * -1), GETDATE()) + AND EXISTS ( + SELECT 1 + FROM msdb.dbo.backupset b + WHERE b.database_name = bw.database_name + AND b.type = 'D' + ) + ) + + BEGIN + + IF @Debug = 1 RAISERROR('Resetting databases with a log backup and no log backup in the last 5 minutes', 0, 1) WITH NOWAIT; + + + UPDATE bw + SET bw.is_started = 0, + bw.is_completed = 1, + bw.last_log_backup_start_time = '19000101' + FROM msdbCentral.dbo.backup_worker bw + WHERE bw.last_log_backup_finish_time < '99991231' + AND bw.last_log_backup_start_time < DATEADD(SECOND, (@rpo * -1), GETDATE()) + AND EXISTS ( + SELECT 1 + FROM msdb.dbo.backupset b + WHERE b.database_name = bw.database_name + AND b.type = 'D' + ); + + + END; --End check for wayward databases + + /* + + Wait 1 minute between runs, we don't need to be checking this constantly + + */ + + + IF @Debug = 1 RAISERROR('Waiting for 1 minute', 0, 1) WITH NOWAIT; + + WAITFOR DELAY '00:01:00.000'; + + END TRY + + BEGIN CATCH + + + SELECT @msg = N'Error inserting databases to msdbCentral.dbo.backup_worker, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + + END CATCH; + + + END; + + /* Check to make sure job is still enabled */ + IF NOT EXISTS ( + SELECT * + FROM msdb.dbo.sysjobs + WHERE name = 'sp_AllNightLog_PollForNewDatabases' + AND enabled = 1 + ) + BEGIN + RAISERROR('sp_AllNightLog_PollForNewDatabases job is disabled, so gracefully exiting. It feels graceful to me, anyway.', 0, 1) WITH NOWAIT; + RETURN; + END + + END;-- End Pollster loop + + ELSE + + BEGIN + + RAISERROR('msdbCentral.dbo.backup_worker does not exist, please create it.', 0, 1) WITH NOWAIT; + RETURN; + + END; + RETURN; + + +/* + +End of Pollster + +*/ + + +/* + +Begin DiskPollster + +*/ + + +/* + +This section runs in a loop checking restore path for new databases added to the server, or broken restores + +*/ + +DiskPollster: + + IF @Debug = 1 RAISERROR('Beginning DiskPollster', 0, 1) WITH NOWAIT; + + IF OBJECT_ID('msdb.dbo.restore_configuration') IS NOT NULL + + BEGIN + + WHILE @PollDiskForNewDatabases = 1 + + BEGIN + + BEGIN TRY + + IF @Debug = 1 RAISERROR('Checking for new databases in: ', 0, 1) WITH NOWAIT; + IF @Debug = 1 RAISERROR(@restore_path_base, 0, 1) WITH NOWAIT; + + /* + + Look for new non-system databases -- there should probably be additional filters here for accessibility, etc. + + */ + + /* + + This setups up the @cmd variable to check the restore path for new folders + + In our case, a new folder means a new database, because we assume a pristine path + + */ + + SET @cmd = N'DIR /b "' + @restore_path_base + N'"'; + + IF @Debug = 1 + BEGIN + PRINT @cmd; + END + + + DELETE @FileList; + INSERT INTO @FileList (BackupFile) + EXEC master.sys.xp_cmdshell @cmd; + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The system cannot find the path specified.' + OR fl.BackupFile = 'File Not Found' + ) = 1 + + BEGIN + + RAISERROR('No rows were returned for that database\path', 0, 1) WITH NOWAIT; + + END; + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'Access is denied.' + ) = 1 + + BEGIN + + RAISERROR('Access is denied to %s', 16, 1, @restore_path_base) WITH NOWAIT; + + END; + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + ) = 1 + AND ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile IS NULL + ) = 1 + + BEGIN + + RAISERROR('That directory appears to be empty', 0, 1) WITH NOWAIT; + + RETURN; + + END + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The user name or password is incorrect.' + ) = 1 + + BEGIN + + RAISERROR('Incorrect user name or password for %s', 16, 1, @restore_path_base) WITH NOWAIT; + + END; + + INSERT msdb.dbo.restore_worker (database_name) + SELECT fl.BackupFile + FROM @FileList AS fl + WHERE fl.BackupFile IS NOT NULL + AND fl.BackupFile COLLATE DATABASE_DEFAULT NOT IN (SELECT name from sys.databases where database_id < 5) + AND NOT EXISTS + ( + SELECT 1 + FROM msdb.dbo.restore_worker rw + WHERE rw.database_name = fl.BackupFile + ) + + IF @Debug = 1 RAISERROR('Checking for wayward databases', 0, 1) WITH NOWAIT; + + /* + + This section aims to find databases that have + * Had a log restore ever (the default for finish time is 9999-12-31, so anything with a more recent finish time has had a log restore) + * Not had a log restore start in the last 5 minutes (this could be trouble! or a really big log restore) + * Also checks msdb.dbo.backupset to make sure the database has a full backup associated with it (otherwise it's the first full, and we don't need to start adding log restores yet) + + */ + + IF EXISTS ( + + SELECT 1 + FROM msdb.dbo.restore_worker rw WITH (READPAST) + WHERE rw.last_log_restore_finish_time < '99991231' + AND rw.last_log_restore_start_time < DATEADD(SECOND, (@rto * -1), GETDATE()) + AND EXISTS ( + SELECT 1 + FROM msdb.dbo.restorehistory r + WHERE r.destination_database_name = rw.database_name + AND r.restore_type = 'D' + ) + ) + + BEGIN + + IF @Debug = 1 RAISERROR('Resetting databases with a log restore and no log restore in the last 5 minutes', 0, 1) WITH NOWAIT; + + + UPDATE rw + SET rw.is_started = 0, + rw.is_completed = 1, + rw.last_log_restore_start_time = '19000101' + FROM msdb.dbo.restore_worker rw + WHERE rw.last_log_restore_finish_time < '99991231' + AND rw.last_log_restore_start_time < DATEADD(SECOND, (@rto * -1), GETDATE()) + AND EXISTS ( + SELECT 1 + FROM msdb.dbo.restorehistory r + WHERE r.destination_database_name = rw.database_name + AND r.restore_type = 'D' + ); + + + END; --End check for wayward databases + + /* + + Wait 1 minute between runs, we don't need to be checking this constantly + + */ + + /* Check to make sure job is still enabled */ + IF NOT EXISTS ( + SELECT * + FROM msdb.dbo.sysjobs + WHERE name = 'sp_AllNightLog_PollDiskForNewDatabases' + AND enabled = 1 + ) + BEGIN + RAISERROR('sp_AllNightLog_PollDiskForNewDatabases job is disabled, so gracefully exiting. It feels graceful to me, anyway.', 0, 1) WITH NOWAIT; + RETURN; + END + + IF @Debug = 1 RAISERROR('Waiting for 1 minute', 0, 1) WITH NOWAIT; + + WAITFOR DELAY '00:01:00.000'; + + END TRY + + BEGIN CATCH + + + SELECT @msg = N'Error inserting databases to msdb.dbo.restore_worker, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + + END CATCH; + + + END; + + END;-- End Pollster loop + + ELSE + + BEGIN + + RAISERROR('msdb.dbo.restore_worker does not exist, please create it.', 0, 1) WITH NOWAIT; + RETURN; + + END; + RETURN; + + + +/* + +Begin LogShamer + +*/ + +LogShamer: + + IF @Debug = 1 RAISERROR('Beginning Backups', 0, 1) WITH NOWAIT; + + IF OBJECT_ID('msdbCentral.dbo.backup_worker') IS NOT NULL + + BEGIN + + /* + + Make sure configuration table exists... + + */ + + IF OBJECT_ID('msdbCentral.dbo.backup_configuration') IS NOT NULL + + BEGIN + + IF @Debug = 1 RAISERROR('Checking variables', 0, 1) WITH NOWAIT; + + /* + + These settings are configurable + + I haven't found a good way to find the default backup path that doesn't involve xp_regread + + */ + + SELECT @rpo = CONVERT(INT, configuration_setting) + FROM msdbCentral.dbo.backup_configuration c + WHERE configuration_name = N'log backup frequency' + AND database_name = N'all'; + + + IF @rpo IS NULL + BEGIN + RAISERROR('@rpo cannot be NULL. Please check the msdbCentral.dbo.backup_configuration table', 0, 1) WITH NOWAIT; + RETURN; + END; + + + SELECT @backup_path = CONVERT(NVARCHAR(512), configuration_setting) + FROM msdbCentral.dbo.backup_configuration c + WHERE configuration_name = N'log backup path' + AND database_name = N'all'; + + + IF @backup_path IS NULL + BEGIN + RAISERROR('@backup_path cannot be NULL. Please check the msdbCentral.dbo.backup_configuration table', 0, 1) WITH NOWAIT; + RETURN; + END; + + SELECT @changebackuptype = configuration_setting + FROM msdbCentral.dbo.backup_configuration c + WHERE configuration_name = N'change backup type' + AND database_name = N'all'; + + SELECT @encrypt = configuration_setting + FROM msdbCentral.dbo.backup_configuration c + WHERE configuration_name = N'encrypt' + AND database_name = N'all'; + + SELECT @encryptionalgorithm = configuration_setting + FROM msdbCentral.dbo.backup_configuration c + WHERE configuration_name = N'encryptionalgorithm' + AND database_name = N'all'; + + SELECT @servercertificate = configuration_setting + FROM msdbCentral.dbo.backup_configuration c + WHERE configuration_name = N'servercertificate' + AND database_name = N'all'; + + IF @encrypt = N'Y' AND (@encryptionalgorithm IS NULL OR @servercertificate IS NULL) + BEGIN + RAISERROR('If encryption is Y, then both the encryptionalgorithm and servercertificate must be set. Please check the msdbCentral.dbo.backup_configuration table', 0, 1) WITH NOWAIT; + RETURN; + END; + + END; + + ELSE + + BEGIN + + RAISERROR('msdbCentral.dbo.backup_configuration does not exist, please run setup script', 0, 1) WITH NOWAIT; + RETURN; + + END; + + + WHILE @Backup = 1 + + /* + + Start loop to take log backups + + */ + + + BEGIN + + BEGIN TRY + + BEGIN TRAN; + + IF @Debug = 1 RAISERROR('Begin tran to grab a database to back up', 0, 1) WITH NOWAIT; + + + /* + + This grabs a database for a worker to work on + + The locking hints hope to provide some isolation when 10+ workers are in action + + */ + + + SELECT TOP (1) + @database = bw.database_name + FROM msdbCentral.dbo.backup_worker bw WITH (UPDLOCK, HOLDLOCK, ROWLOCK) + WHERE + ( /*This section works on databases already part of the backup cycle*/ + bw.is_started = 0 + AND bw.is_completed = 1 + AND bw.last_log_backup_start_time < DATEADD(SECOND, (@rpo * -1), GETDATE()) + AND (bw.error_number IS NULL OR bw.error_number > 0) /* negative numbers indicate human attention required */ + AND bw.ignore_database = 0 + ) + OR + ( /*This section picks up newly added databases by Pollster*/ + bw.is_started = 0 + AND bw.is_completed = 0 + AND bw.last_log_backup_start_time = '1900-01-01 00:00:00.000' + AND bw.last_log_backup_finish_time = '9999-12-31 00:00:00.000' + AND (bw.error_number IS NULL OR bw.error_number > 0) /* negative numbers indicate human attention required */ + AND bw.ignore_database = 0 + ) + ORDER BY bw.last_log_backup_start_time ASC, bw.last_log_backup_finish_time ASC, bw.database_name ASC; + + + IF @database IS NOT NULL + BEGIN + SET @msg = N'Updating backup_worker for database ' + ISNULL(@database, 'UH OH NULL @database'); + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + /* + + Update the worker table so other workers know a database is being backed up + + */ + + + UPDATE bw + SET bw.is_started = 1, + bw.is_completed = 0, + bw.last_log_backup_start_time = GETDATE() + FROM msdbCentral.dbo.backup_worker bw + WHERE bw.database_name = @database; + END + + COMMIT; + + END TRY + + BEGIN CATCH + + /* + + Do I need to build retry logic in here? Try to catch deadlocks? I don't know yet! + + */ + + SELECT @msg = N'Error securing a database to backup, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + SET @database = NULL; + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + END CATCH; + + + /* If we don't find a database to work on, wait for a few seconds */ + IF @database IS NULL + + BEGIN + IF @Debug = 1 RAISERROR('No databases to back up right now, starting 3 second throttle', 0, 1) WITH NOWAIT; + WAITFOR DELAY '00:00:03.000'; + + /* Check to make sure job is still enabled */ + IF NOT EXISTS ( + SELECT * + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp_AllNightLog_Backup%' + AND enabled = 1 + ) + BEGIN + RAISERROR('sp_AllNightLog_Backup jobs are disabled, so gracefully exiting. It feels graceful to me, anyway.', 0, 1) WITH NOWAIT; + RETURN; + END + + + END + + + BEGIN TRY + + BEGIN + + IF @database IS NOT NULL + + /* + + Make sure we have a database to work on -- I should make this more robust so we do something if it is NULL, maybe + + */ + + + BEGIN + + SET @msg = N'Taking backup of ' + ISNULL(@database, 'UH OH NULL @database'); + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + /* + + Call Ola's proc to backup the database + + */ + + IF @encrypt = 'Y' + EXEC dbo.DatabaseBackup @Databases = @database, --Database we're working on + @BackupType = 'LOG', --Going for the LOGs + @Directory = @backup_path, --The path we need to back up to + @Verify = 'N', --We don't want to verify these, it eats into job time + @ChangeBackupType = @changebackuptype, --If we need to switch to a FULL because one hasn't been taken + @CheckSum = 'Y', --These are a good idea + @Compress = 'Y', --This is usually a good idea + @LogToTable = 'Y', --We should do this for posterity + @Encrypt = @encrypt, + @EncryptionAlgorithm = @encryptionalgorithm, + @ServerCertificate = @servercertificate; + + ELSE + EXEC dbo.DatabaseBackup @Databases = @database, --Database we're working on + @BackupType = 'LOG', --Going for the LOGs + @Directory = @backup_path, --The path we need to back up to + @Verify = 'N', --We don't want to verify these, it eats into job time + @ChangeBackupType = @changebackuptype, --If we need to switch to a FULL because one hasn't been taken + @CheckSum = 'Y', --These are a good idea + @Compress = 'Y', --This is usually a good idea + @LogToTable = 'Y'; --We should do this for posterity + + + /* + + Catch any erroneous zones + + */ + + SELECT @error_number = ERROR_NUMBER(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + END; --End call to dbo.DatabaseBackup + + END; --End successful check of @database (not NULL) + + END TRY + + BEGIN CATCH + + IF @error_number IS NOT NULL + + /* + + If the ERROR() function returns a number, update the table with it and the last error date. + + Also update the last start time to 1900-01-01 so it gets picked back up immediately -- the query to find a log backup to take sorts by start time + + */ + + BEGIN + + SET @msg = N'Error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()); + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + SET @msg = N'Updating backup_worker for database ' + ISNULL(@database, 'UH OH NULL @database') + ' for unsuccessful backup'; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + + + UPDATE bw + SET bw.is_started = 0, + bw.is_completed = 1, + bw.last_log_backup_start_time = '19000101', + bw.error_number = @error_number, + bw.last_error_date = GETDATE() + FROM msdbCentral.dbo.backup_worker bw + WHERE bw.database_name = @database; + + + /* + + Set @database back to NULL to avoid variable assignment weirdness + + */ + + SET @database = NULL; + + + /* + + Wait around for a second so we're not just spinning wheels -- this only runs if the BEGIN CATCH is triggered by an error + + */ + + IF @Debug = 1 RAISERROR('Starting 1 second throttle', 0, 1) WITH NOWAIT; + + WAITFOR DELAY '00:00:01.000'; + + END; -- End update of unsuccessful backup + + END CATCH; + + IF @database IS NOT NULL AND @error_number IS NULL + + /* + + If no error, update everything normally + + */ + + + BEGIN + + IF @Debug = 1 RAISERROR('Error number IS NULL', 0, 1) WITH NOWAIT; + + SET @msg = N'Updating backup_worker for database ' + ISNULL(@database, 'UH OH NULL @database') + ' for successful backup'; + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + + UPDATE bw + SET bw.is_started = 0, + bw.is_completed = 1, + bw.last_log_backup_finish_time = GETDATE() + FROM msdbCentral.dbo.backup_worker bw + WHERE bw.database_name = @database; + + + /* + + Set @database back to NULL to avoid variable assignment weirdness + + */ + + SET @database = NULL; + + + END; -- End update for successful backup + + + END; -- End @Backup WHILE loop + + + END; -- End successful check for backup_worker and subsequent code + + + ELSE + + BEGIN + + RAISERROR('msdbCentral.dbo.backup_worker does not exist, please run setup script', 0, 1) WITH NOWAIT; + + RETURN; + + END; +RETURN; + + +/* + +Begin Restoregasm_Addict section + +*/ + +Restoregasm_Addict: + +IF @Restore = 1 + IF @Debug = 1 RAISERROR('Beginning Restores', 0, 1) WITH NOWAIT; + + /* Check to make sure backup jobs aren't enabled */ + IF EXISTS ( + SELECT * + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp_AllNightLog_Backup%' + AND enabled = 1 + ) + BEGIN + RAISERROR('sp_AllNightLog_Backup jobs are enabled, so gracefully exiting. You do not want to accidentally do restores over top of the databases you are backing up.', 0, 1) WITH NOWAIT; + RETURN; + END + + IF OBJECT_ID('msdb.dbo.restore_worker') IS NOT NULL + + BEGIN + + /* + + Make sure configuration table exists... + + */ + + IF OBJECT_ID('msdb.dbo.restore_configuration') IS NOT NULL + + BEGIN + + IF @Debug = 1 RAISERROR('Checking variables', 0, 1) WITH NOWAIT; + + /* + + These settings are configurable + + */ + + SELECT @rto = CONVERT(INT, configuration_setting) + FROM msdb.dbo.restore_configuration c + WHERE configuration_name = N'log restore frequency'; + + + IF @rto IS NULL + BEGIN + RAISERROR('@rto cannot be NULL. Please check the msdb.dbo.restore_configuration table', 0, 1) WITH NOWAIT; + RETURN; + END; + + + END; + + ELSE + + BEGIN + + RAISERROR('msdb.dbo.restore_configuration does not exist, please run setup script', 0, 1) WITH NOWAIT; + + RETURN; + + END; + + + WHILE @Restore = 1 + + /* + + Start loop to restore log backups + + */ + + + BEGIN + + BEGIN TRY + + BEGIN TRAN; + + IF @Debug = 1 RAISERROR('Begin tran to grab a database to restore', 0, 1) WITH NOWAIT; + + + /* + + This grabs a database for a worker to work on + + The locking hints hope to provide some isolation when 10+ workers are in action + + */ + + + SELECT TOP (1) + @database = rw.database_name, + @only_logs_after = REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(30), rw.last_log_restore_start_time, 120), ' ', ''), '-', ''), ':', ''), + @restore_full = CASE WHEN rw.is_started = 0 + AND rw.is_completed = 0 + AND rw.last_log_restore_start_time = '1900-01-01 00:00:00.000' + AND rw.last_log_restore_finish_time = '9999-12-31 00:00:00.000' + THEN 1 + ELSE 0 + END + FROM msdb.dbo.restore_worker rw WITH (UPDLOCK, HOLDLOCK, ROWLOCK) + WHERE ( + ( /*This section works on databases already part of the backup cycle*/ + rw.is_started = 0 + AND rw.is_completed = 1 + AND rw.last_log_restore_start_time < DATEADD(SECOND, (@rto * -1), GETDATE()) + AND (rw.error_number IS NULL OR rw.error_number > 0) /* negative numbers indicate human attention required */ + ) + OR + ( /*This section picks up newly added databases by DiskPollster*/ + rw.is_started = 0 + AND rw.is_completed = 0 + AND rw.last_log_restore_start_time = '1900-01-01 00:00:00.000' + AND rw.last_log_restore_finish_time = '9999-12-31 00:00:00.000' + AND (rw.error_number IS NULL OR rw.error_number > 0) /* negative numbers indicate human attention required */ + ) + ) + AND rw.ignore_database = 0 + AND NOT EXISTS ( + /* Validation check to ensure the database either doesn't exist or is in a restoring/standby state */ + SELECT 1 + FROM sys.databases d + WHERE d.name = rw.database_name + AND state <> 1 /* Restoring */ + AND NOT (state=0 AND d.is_in_standby=1) /* standby mode */ + ) + ORDER BY rw.last_log_restore_start_time ASC, rw.last_log_restore_finish_time ASC, rw.database_name ASC; + + + IF @database IS NOT NULL + BEGIN + SET @msg = N'Updating restore_worker for database ' + ISNULL(@database, 'UH OH NULL @database'); + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + /* + + Update the worker table so other workers know a database is being restored + + */ + + + UPDATE rw + SET rw.is_started = 1, + rw.is_completed = 0, + rw.last_log_restore_start_time = GETDATE() + FROM msdb.dbo.restore_worker rw + WHERE rw.database_name = @database; + END + + COMMIT; + + END TRY + + BEGIN CATCH + + /* + + Do I need to build retry logic in here? Try to catch deadlocks? I don't know yet! + + */ + + SELECT @msg = N'Error securing a database to restore, error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()) + ', error message is ' + ERROR_MESSAGE(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + SET @database = NULL; + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + END CATCH; + + + /* If we don't find a database to work on, wait for a few seconds */ + IF @database IS NULL + + BEGIN + IF @Debug = 1 RAISERROR('No databases to restore up right now, starting 3 second throttle', 0, 1) WITH NOWAIT; + WAITFOR DELAY '00:00:03.000'; + + /* Check to make sure backup jobs aren't enabled */ + IF EXISTS ( + SELECT * + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp_AllNightLog_Backup%' + AND enabled = 1 + ) + BEGIN + RAISERROR('sp_AllNightLog_Backup jobs are enabled, so gracefully exiting. You do not want to accidentally do restores over top of the databases you are backing up.', 0, 1) WITH NOWAIT; + RETURN; + END + + /* Check to make sure job is still enabled */ + IF NOT EXISTS ( + SELECT * + FROM msdb.dbo.sysjobs + WHERE name LIKE 'sp_AllNightLog_Restore%' + AND enabled = 1 + ) + BEGIN + RAISERROR('sp_AllNightLog_Restore jobs are disabled, so gracefully exiting. It feels graceful to me, anyway.', 0, 1) WITH NOWAIT; + RETURN; + END + + END + + + BEGIN TRY + + BEGIN + + IF @database IS NOT NULL + + /* + + Make sure we have a database to work on -- I should make this more robust so we do something if it is NULL, maybe + + */ + + + BEGIN + + SET @msg = CASE WHEN @restore_full = 0 + THEN N'Restoring logs for ' + ELSE N'Restoring full backup for ' + END + + ISNULL(@database, 'UH OH NULL @database'); + + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + /* + + Call sp_DatabaseRestore to backup the database + + */ + + SET @restore_path_full = @restore_path_base + N'\' + @database + N'\' + N'FULL\' + + SET @msg = N'Path for FULL backups for ' + @database + N' is ' + @restore_path_full + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + SET @restore_path_log = @restore_path_base + N'\' + @database + N'\' + N'LOG\' + + SET @msg = N'Path for LOG backups for ' + @database + N' is ' + @restore_path_log + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + IF @restore_full = 0 + + BEGIN + + IF @Debug = 1 RAISERROR('Starting Log only restores', 0, 1) WITH NOWAIT; + + EXEC dbo.sp_DatabaseRestore @Database = @database, + @BackupPathFull = @restore_path_full, + @BackupPathLog = @restore_path_log, + @ContinueLogs = 1, + @RunRecovery = 0, + @OnlyLogsAfter = @only_logs_after, + @MoveFiles = @restore_move_files, + @Debug = @Debug + + END + + IF @restore_full = 1 + + BEGIN + + IF @Debug = 1 RAISERROR('Starting first Full restore from: ', 0, 1) WITH NOWAIT; + IF @Debug = 1 RAISERROR(@restore_path_full, 0, 1) WITH NOWAIT; + + EXEC dbo.sp_DatabaseRestore @Database = @database, + @BackupPathFull = @restore_path_full, + @BackupPathLog = @restore_path_log, + @ContinueLogs = 0, + @RunRecovery = 0, + @MoveFiles = @restore_move_files, + @Debug = @Debug + + END + + + + /* + + Catch any erroneous zones + + */ + + SELECT @error_number = ERROR_NUMBER(), + @error_severity = ERROR_SEVERITY(), + @error_state = ERROR_STATE(); + + END; --End call to dbo.sp_DatabaseRestore + + END; --End successful check of @database (not NULL) + + END TRY + + BEGIN CATCH + + IF @error_number IS NOT NULL + + /* + + If the ERROR() function returns a number, update the table with it and the last error date. + + Also update the last start time to 1900-01-01 so it gets picked back up immediately -- the query to find a log restore to take sorts by start time + + */ + + BEGIN + + SET @msg = N'Error number is ' + CONVERT(NVARCHAR(10), ERROR_NUMBER()); + RAISERROR(@msg, @error_severity, @error_state) WITH NOWAIT; + + SET @msg = N'Updating restore_worker for database ' + ISNULL(@database, 'UH OH NULL @database') + ' for unsuccessful backup'; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + + + UPDATE rw + SET rw.is_started = 0, + rw.is_completed = 1, + rw.last_log_restore_start_time = '19000101', + rw.error_number = @error_number, + rw.last_error_date = GETDATE() + FROM msdb.dbo.restore_worker rw + WHERE rw.database_name = @database; + + + /* + + Set @database back to NULL to avoid variable assignment weirdness + + */ + + SET @database = NULL; + + + /* + + Wait around for a second so we're not just spinning wheels -- this only runs if the BEGIN CATCH is triggered by an error + + */ + + IF @Debug = 1 RAISERROR('Starting 1 second throttle', 0, 1) WITH NOWAIT; + + WAITFOR DELAY '00:00:01.000'; + + END; -- End update of unsuccessful restore + + END CATCH; + + + IF @database IS NOT NULL AND @error_number IS NULL + + /* + + If no error, update everything normally + + */ + + + BEGIN + + IF @Debug = 1 RAISERROR('Error number IS NULL', 0, 1) WITH NOWAIT; + + /* Make sure database actually exists and is in the restoring state */ + IF EXISTS (SELECT * FROM sys.databases WHERE name = @database AND state = 1) /* Restoring */ + BEGIN + SET @msg = N'Updating backup_worker for database ' + ISNULL(@database, 'UH OH NULL @database') + ' for successful backup'; + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + UPDATE rw + SET rw.is_started = 0, + rw.is_completed = 1, + rw.last_log_restore_finish_time = GETDATE() + FROM msdb.dbo.restore_worker rw + WHERE rw.database_name = @database; + + END + ELSE /* The database doesn't exist, or it's not in the restoring state */ + BEGIN + SET @msg = N'Updating backup_worker for database ' + ISNULL(@database, 'UH OH NULL @database') + ' for UNsuccessful backup'; + IF @Debug = 1 RAISERROR(@msg, 0, 1) WITH NOWAIT; + + UPDATE rw + SET rw.is_started = 0, + rw.is_completed = 1, + rw.error_number = -1, /* unknown, human attention required */ + rw.last_error_date = GETDATE() + /* rw.last_log_restore_finish_time = GETDATE() don't change this - the last log may still be successful */ + FROM msdb.dbo.restore_worker rw + WHERE rw.database_name = @database; + END + + + + /* + + Set @database back to NULL to avoid variable assignment weirdness + + */ + + SET @database = NULL; + + + END; -- End update for successful backup + + END; -- End @Restore WHILE loop + + + END; -- End successful check for restore_worker and subsequent code + + + ELSE + + BEGIN + + RAISERROR('msdb.dbo.restore_worker does not exist, please run setup script', 0, 1) WITH NOWAIT; + + RETURN; + + END; +RETURN; + + + +END; -- Final END for stored proc + +GO +IF OBJECT_ID('dbo.sp_Blitz') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_Blitz AS RETURN 0;'); +GO + +ALTER PROCEDURE [dbo].[sp_Blitz] + @Help TINYINT = 0 , + @CheckUserDatabaseObjects TINYINT = 1 , + @CheckProcedureCache TINYINT = 0 , + @OutputType VARCHAR(20) = 'TABLE' , + @OutputProcedureCache TINYINT = 0 , + @CheckProcedureCacheFilter VARCHAR(10) = NULL , + @CheckServerInfo TINYINT = 0 , + @SkipChecksServer NVARCHAR(256) = NULL , + @SkipChecksDatabase NVARCHAR(256) = NULL , + @SkipChecksSchema NVARCHAR(256) = NULL , + @SkipChecksTable NVARCHAR(256) = NULL , + @IgnorePrioritiesBelow INT = NULL , + @IgnorePrioritiesAbove INT = NULL , + @OutputServerName NVARCHAR(256) = NULL , + @OutputDatabaseName NVARCHAR(256) = NULL , + @OutputSchemaName NVARCHAR(256) = NULL , + @OutputTableName NVARCHAR(256) = NULL , + @OutputXMLasNVARCHAR TINYINT = 0 , + @EmailRecipients VARCHAR(MAX) = NULL , + @EmailProfile sysname = NULL , + @SummaryMode TINYINT = 0 , + @BringThePain TINYINT = 0 , + @UsualDBOwner sysname = NULL , + @SkipBlockingChecks TINYINT = 1 , + @Debug TINYINT = 0 , + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS + SET NOCOUNT ON; + SET STATISTICS XML OFF; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + + SELECT @Version = '8.16', @VersionDate = '20230820'; + SET @OutputType = UPPER(@OutputType); + + IF(@VersionCheckMode = 1) + BEGIN + RETURN; + END; + + IF @Help = 1 + BEGIN + PRINT ' + /* + sp_Blitz from http://FirstResponderKit.org + + This script checks the health of your SQL Server and gives you a prioritized + to-do list of the most urgent things you should consider fixing. + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. + - If a database name has a question mark in it, some tests will fail. Gotta + love that unsupported sp_MSforeachdb. + - If you have offline databases, sp_Blitz fails the first time you run it, + but does work the second time. (Hoo, boy, this will be fun to debug.) + - @OutputServerName will output QueryPlans as NVARCHAR(MAX) since Microsoft + has refused to support XML columns in Linked Server queries. The bug is now + 16 years old! *~ \o/ ~* + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + Parameter explanations: + + @CheckUserDatabaseObjects 1=review user databases for triggers, heaps, etc. Takes more time for more databases and objects. + @CheckServerInfo 1=show server info like CPUs, memory, virtualization + @CheckProcedureCache 1=top 20-50 resource-intensive cache plans and analyze them for common performance issues. + @OutputProcedureCache 1=output the top 20-50 resource-intensive plans even if they did not trigger an alarm + @CheckProcedureCacheFilter ''CPU'' | ''Reads'' | ''Duration'' | ''ExecCount'' + @OutputType ''TABLE''=table | ''COUNT''=row with number found | ''MARKDOWN''=bulleted list (including server info, excluding security findings) | ''SCHEMA''=version and field list | ''XML'' =table output as XML | ''NONE'' = none + @IgnorePrioritiesBelow 50=ignore priorities below 50 + @IgnorePrioritiesAbove 50=ignore priorities above 50 + For the rest of the parameters, see https://www.BrentOzar.com/blitz/documentation for details. + + MIT License + + Copyright for portions of sp_Blitz are held by Microsoft as part of project + tigertoolbox and are provided under the MIT license: + https://github.com/Microsoft/tigertoolbox + + All other copyrights for sp_Blitz are held by Brent Ozar Unlimited. + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */'; + RETURN; + END; /* @Help = 1 */ + + ELSE IF @OutputType = 'SCHEMA' + BEGIN + SELECT FieldList = '[Priority] TINYINT, [FindingsGroup] VARCHAR(50), [Finding] VARCHAR(200), [DatabaseName] NVARCHAR(128), [URL] VARCHAR(200), [Details] NVARCHAR(4000), [QueryPlan] NVARCHAR(MAX), [QueryPlanFiltered] NVARCHAR(MAX), [CheckID] INT'; + + END;/* IF @OutputType = 'SCHEMA' */ + ELSE + BEGIN + + DECLARE @StringToExecute NVARCHAR(4000) + ,@curr_tracefilename NVARCHAR(500) + ,@base_tracefilename NVARCHAR(500) + ,@indx int + ,@query_result_separator CHAR(1) + ,@EmailSubject NVARCHAR(255) + ,@EmailBody NVARCHAR(MAX) + ,@EmailAttachmentFilename NVARCHAR(255) + ,@ProductVersion NVARCHAR(128) + ,@ProductVersionMajor DECIMAL(10,2) + ,@ProductVersionMinor DECIMAL(10,2) + ,@CurrentName NVARCHAR(128) + ,@CurrentDefaultValue NVARCHAR(200) + ,@CurrentCheckID INT + ,@CurrentPriority INT + ,@CurrentFinding VARCHAR(200) + ,@CurrentURL VARCHAR(200) + ,@CurrentDetails NVARCHAR(4000) + ,@MsSinceWaitsCleared DECIMAL(38,0) + ,@CpuMsSinceWaitsCleared DECIMAL(38,0) + ,@ResultText NVARCHAR(MAX) + ,@crlf NVARCHAR(2) + ,@Processors int + ,@NUMANodes int + ,@MinServerMemory bigint + ,@MaxServerMemory bigint + ,@ColumnStoreIndexesInUse bit + ,@TraceFileIssue bit + -- Flag for Windows OS to help with Linux support + ,@IsWindowsOperatingSystem BIT + ,@DaysUptime NUMERIC(23,2) + /* For First Responder Kit consistency check:*/ + ,@spBlitzFullName VARCHAR(1024) + ,@BlitzIsOutdatedComparedToOthers BIT + ,@tsql NVARCHAR(MAX) + ,@VersionCheckModeExistsTSQL NVARCHAR(MAX) + ,@BlitzProcDbName VARCHAR(256) + ,@ExecRet INT + ,@InnerExecRet INT + ,@TmpCnt INT + ,@PreviousComponentName VARCHAR(256) + ,@PreviousComponentFullPath VARCHAR(1024) + ,@CurrentStatementId INT + ,@CurrentComponentSchema VARCHAR(256) + ,@CurrentComponentName VARCHAR(256) + ,@CurrentComponentType VARCHAR(256) + ,@CurrentComponentVersionDate DATETIME2 + ,@CurrentComponentFullName VARCHAR(1024) + ,@CurrentComponentMandatory BIT + ,@MaximumVersionDate DATETIME + ,@StatementCheckName VARCHAR(256) + ,@StatementOutputsCounter BIT + ,@OutputCounterExpectedValue INT + ,@StatementOutputsExecRet BIT + ,@StatementOutputsDateTime BIT + ,@CurrentComponentMandatoryCheckOK BIT + ,@CurrentComponentVersionCheckModeOK BIT + ,@canExitLoop BIT + ,@frkIsConsistent BIT + ,@NeedToTurnNumericRoundabortBackOn BIT + ,@sa bit = 1 + ,@SUSER_NAME sysname = SUSER_SNAME() + ,@SkipDBCC bit = 0 + ,@SkipTrace bit = 0 + ,@SkipXPRegRead bit = 0 + ,@SkipXPFixedDrives bit = 0 + ,@SkipXPCMDShell bit = 0 + ,@SkipMaster bit = 0 + ,@SkipMSDB bit = 0 + ,@SkipModel bit = 0 + ,@SkipTempDB bit = 0 + ,@SkipValidateLogins bit = 0; + + DECLARE + @db_perms table + ( + database_name sysname, + permission_name sysname + ); + + INSERT + @db_perms + ( + database_name, + permission_name + ) + SELECT + database_name = + DB_NAME(d.database_id), + fmp.permission_name + FROM sys.databases AS d + CROSS APPLY fn_my_permissions(d.name, 'DATABASE') AS fmp + WHERE fmp.permission_name = N'SELECT' /*Databases where we don't have read permissions*/ + + /* End of declarations for First Responder Kit consistency check:*/ + ; + + /*Starting permissions checks here, but only if we're not a sysadmin*/ + IF + ( + SELECT + sa = + ISNULL + ( + IS_SRVROLEMEMBER(N'sysadmin'), + 0 + ) + ) = 0 + BEGIN + IF @Debug IN (1, 2) RAISERROR('User not SA, checking permissions', 0, 1) WITH NOWAIT; + + SET @sa = 0; /*Setting this to 0 to skip DBCC COMMANDS*/ + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM sys.fn_my_permissions(NULL, NULL) AS fmp + WHERE fmp.permission_name = N'VIEW SERVER STATE' + ) + BEGIN + RAISERROR('The user %s does not have VIEW SERVER STATE permissions.', 0, 11, @SUSER_NAME) WITH NOWAIT; + RETURN; + END; /*If we don't have this, we can't do anything at all.*/ + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM fn_my_permissions(N'sys.traces', N'OBJECT') AS fmp + WHERE fmp.permission_name = N'ALTER' + ) + BEGIN + SET @SkipTrace = 1; + END; /*We need this permission to execute trace stuff, apparently*/ + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM fn_my_permissions(N'xp_regread', N'OBJECT') AS fmp + WHERE fmp.permission_name = N'EXECUTE' + ) + BEGIN + SET @SkipXPRegRead = 1; + END; /*Need execute on xp_regread*/ + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM fn_my_permissions(N'xp_fixeddrives', N'OBJECT') AS fmp + WHERE fmp.permission_name = N'EXECUTE' + ) + BEGIN + SET @SkipXPFixedDrives = 1; + END; /*Need execute on xp_fixeddrives*/ + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM fn_my_permissions(N'xp_cmdshell', N'OBJECT') AS fmp + WHERE fmp.permission_name = N'EXECUTE' + ) + BEGIN + SET @SkipXPCMDShell = 1; + END; /*Need execute on xp_cmdshell*/ + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM fn_my_permissions(N'sp_validatelogins', N'OBJECT') AS fmp + WHERE fmp.permission_name = N'EXECUTE' + ) + BEGIN + SET @SkipValidateLogins = 1; + END; /*Need execute on sp_validatelogins*/ + + END; + + SET @crlf = NCHAR(13) + NCHAR(10); + SET @ResultText = 'sp_Blitz Results: ' + @crlf; + + /* Last startup */ + SELECT @DaysUptime = CAST(DATEDIFF(HOUR, create_date, GETDATE()) / 24. AS NUMERIC(23, 2)) + FROM sys.databases + WHERE database_id = 2; + + IF @DaysUptime = 0 + SET @DaysUptime = .01; + + /* + Set the session state of Numeric_RoundAbort to off if any databases have Numeric Round-Abort enabled. + Stops arithmetic overflow errors during data conversion. See Github issue #2302 for more info. + */ + IF ( (8192 & @@OPTIONS) = 8192 ) /* Numeric RoundAbort is currently on, so we may need to turn it off temporarily */ + BEGIN + IF EXISTS (SELECT 1 + FROM sys.databases + WHERE is_numeric_roundabort_on = 1) /* A database has it turned on */ + BEGIN + SET @NeedToTurnNumericRoundabortBackOn = 1; + SET NUMERIC_ROUNDABORT OFF; + END; + END; + + + + + /* + --TOURSTOP01-- + See https://www.BrentOzar.com/go/blitztour for a guided tour. + + We start by creating #BlitzResults. It's a temp table that will store all of + the results from our checks. Throughout the rest of this stored procedure, + we're running a series of checks looking for dangerous things inside the SQL + Server. When we find a problem, we insert rows into #BlitzResults. At the + end, we return these results to the end user. + + #BlitzResults has a CheckID field, but there's no Check table. As we do + checks, we insert data into this table, and we manually put in the CheckID. + For a list of checks, visit http://FirstResponderKit.org. + */ + IF OBJECT_ID('tempdb..#BlitzResults') IS NOT NULL + DROP TABLE #BlitzResults; + CREATE TABLE #BlitzResults + ( + ID INT IDENTITY(1, 1) , + CheckID INT , + DatabaseName NVARCHAR(128) , + Priority TINYINT , + FindingsGroup VARCHAR(50) , + Finding VARCHAR(200) , + URL VARCHAR(200) , + Details NVARCHAR(4000) , + QueryPlan [XML] NULL , + QueryPlanFiltered [NVARCHAR](MAX) NULL + ); + + IF OBJECT_ID('tempdb..#TemporaryDatabaseResults') IS NOT NULL + DROP TABLE #TemporaryDatabaseResults; + CREATE TABLE #TemporaryDatabaseResults + ( + DatabaseName NVARCHAR(128) , + Finding NVARCHAR(128) + ); + + /* First Responder Kit consistency (temporary tables) */ + + IF(OBJECT_ID('tempdb..#FRKObjects') IS NOT NULL) + BEGIN + EXEC sp_executesql N'DROP TABLE #FRKObjects;'; + END; + + -- this one represents FRK objects + CREATE TABLE #FRKObjects ( + DatabaseName VARCHAR(256) NOT NULL, + ObjectSchemaName VARCHAR(256) NULL, + ObjectName VARCHAR(256) NOT NULL, + ObjectType VARCHAR(256) NOT NULL, + MandatoryComponent BIT NOT NULL + ); + + + IF(OBJECT_ID('tempdb..#StatementsToRun4FRKVersionCheck') IS NOT NULL) + BEGIN + EXEC sp_executesql N'DROP TABLE #StatementsToRun4FRKVersionCheck;'; + END; + + + -- This one will contain the statements to be executed + -- order: 1- Mandatory, 2- VersionCheckMode, 3- VersionCheck + + CREATE TABLE #StatementsToRun4FRKVersionCheck ( + StatementId INT IDENTITY(1,1), + CheckName VARCHAR(256), + SubjectName VARCHAR(256), + SubjectFullPath VARCHAR(1024), + StatementText NVARCHAR(MAX), + StatementOutputsCounter BIT, + OutputCounterExpectedValue INT, + StatementOutputsExecRet BIT, + StatementOutputsDateTime BIT + ); + + /* End of First Responder Kit consistency (temporary tables) */ + + + /* + You can build your own table with a list of checks to skip. For example, you + might have some databases that you don't care about, or some checks you don't + want to run. Then, when you run sp_Blitz, you can specify these parameters: + @SkipChecksDatabase = 'DBAtools', + @SkipChecksSchema = 'dbo', + @SkipChecksTable = 'BlitzChecksToSkip' + Pass in the database, schema, and table that contains the list of checks you + want to skip. This part of the code checks those parameters, gets the list, + and then saves those in a temp table. As we run each check, we'll see if we + need to skip it. + */ + /* --TOURSTOP07-- */ + IF OBJECT_ID('tempdb..#SkipChecks') IS NOT NULL + DROP TABLE #SkipChecks; + CREATE TABLE #SkipChecks + ( + DatabaseName NVARCHAR(128) , + CheckID INT , + ServerName NVARCHAR(128) + ); + CREATE CLUSTERED INDEX IX_CheckID_DatabaseName ON #SkipChecks(CheckID, DatabaseName); + + INSERT INTO #SkipChecks + (DatabaseName) + SELECT + DB_NAME(d.database_id) + FROM sys.databases AS d + WHERE (DB_NAME(d.database_id) LIKE 'rdsadmin%' + OR LOWER(d.name) IN ('dbatools', 'dbadmin', 'dbmaintenance')) + OPTION(RECOMPILE); + + /*Skip checks for database where we don't have read permissions*/ + INSERT INTO + #SkipChecks + ( + DatabaseName + ) + SELECT + DB_NAME(d.database_id) + FROM sys.databases AS d + WHERE NOT EXISTS + ( + SELECT + 1/0 + FROM @db_perms AS dp + WHERE dp.database_name = DB_NAME(d.database_id) + ); + + /*Skip individial checks where we don't have permissions*/ + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 29, NULL)) AS v (DatabaseName, CheckID, ServerName) /*Looks for user tables in model*/ + WHERE NOT EXISTS (SELECT 1/0 FROM @db_perms AS dp WHERE dp.database_name = 'model'); + + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 68, NULL)) AS v (DatabaseName, CheckID, ServerName) /*DBCC command*/ + WHERE @sa = 0; + + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 69, NULL)) AS v (DatabaseName, CheckID, ServerName) /*DBCC command*/ + WHERE @sa = 0; + + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 92, NULL)) AS v (DatabaseName, CheckID, ServerName) /*xp_fixeddrives*/ + WHERE @SkipXPFixedDrives = 1; + + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 211, NULL)) AS v (DatabaseName, CheckID, ServerName) /*xp_regread*/ + WHERE @SkipXPRegRead = 1; + + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 212, NULL)) AS v (DatabaseName, CheckID, ServerName) /*xp_regread*/ + WHERE @SkipXPCMDShell = 1; + + INSERT #SkipChecks (DatabaseName, CheckID, ServerName) + SELECT + v.* + FROM (VALUES(NULL, 2301, NULL)) AS v (DatabaseName, CheckID, ServerName) /*sp_validatelogins*/ + WHERE @SkipValidateLogins = 1 + + IF(OBJECT_ID('tempdb..#InvalidLogins') IS NOT NULL) + BEGIN + EXEC sp_executesql N'DROP TABLE #InvalidLogins;'; + END; + + CREATE TABLE #InvalidLogins ( + LoginSID varbinary(85), + LoginName VARCHAR(256) + ); + + IF @SkipChecksTable IS NOT NULL + AND @SkipChecksSchema IS NOT NULL + AND @SkipChecksDatabase IS NOT NULL + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Inserting SkipChecks', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'INSERT INTO #SkipChecks(DatabaseName, CheckID, ServerName ) + SELECT DISTINCT DatabaseName, CheckID, ServerName + FROM ' + IF LTRIM(RTRIM(@SkipChecksServer)) <> '' + BEGIN + SET @StringToExecute += QUOTENAME(@SkipChecksServer) + N'.'; + END + SET @StringToExecute += QUOTENAME(@SkipChecksDatabase) + N'.' + QUOTENAME(@SkipChecksSchema) + N'.' + QUOTENAME(@SkipChecksTable) + + N' WHERE ServerName IS NULL OR ServerName = CAST(SERVERPROPERTY(''ServerName'') AS NVARCHAR(128)) OPTION (RECOMPILE);'; + EXEC(@StringToExecute); + END; + + -- Flag for Windows OS to help with Linux support + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_os_host_info' ) + BEGIN + SELECT @IsWindowsOperatingSystem = CASE WHEN host_platform = 'Windows' THEN 1 ELSE 0 END FROM sys.dm_os_host_info ; + END; + ELSE + BEGIN + SELECT @IsWindowsOperatingSystem = 1 ; + END; + + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 106 ) + AND (select convert(int,value_in_use) from sys.configurations where name = 'default trace enabled' ) = 1 + BEGIN + + select @curr_tracefilename = [path] from sys.traces where is_default = 1 ; + set @curr_tracefilename = reverse(@curr_tracefilename); + + -- Set the trace file path separator based on underlying OS + IF (@IsWindowsOperatingSystem = 1) AND @curr_tracefilename IS NOT NULL + BEGIN + select @indx = patindex('%\%', @curr_tracefilename) ; + set @curr_tracefilename = reverse(@curr_tracefilename) ; + set @base_tracefilename = left( @curr_tracefilename,len(@curr_tracefilename) - @indx) + '\log.trc' ; + END; + ELSE + BEGIN + select @indx = patindex('%/%', @curr_tracefilename) ; + set @curr_tracefilename = reverse(@curr_tracefilename) ; + set @base_tracefilename = left( @curr_tracefilename,len(@curr_tracefilename) - @indx) + '/log.trc' ; + END; + + END; + + /* If the server has any databases on Antiques Roadshow, skip the checks that would break due to CTEs. */ + IF @CheckUserDatabaseObjects = 1 AND EXISTS(SELECT * FROM sys.databases WHERE compatibility_level < 90) + BEGIN + SET @CheckUserDatabaseObjects = 0; + PRINT 'Databases with compatibility level < 90 found, so setting @CheckUserDatabaseObjects = 0.'; + PRINT 'The database-level checks rely on CTEs, which are not supported in SQL 2000 compat level databases.'; + PRINT 'Get with the cool kids and switch to a current compatibility level, Grandpa. To find the problems, run:'; + PRINT 'SELECT * FROM sys.databases WHERE compatibility_level < 90;'; + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 204 AS CheckID , + 0 AS Priority , + 'Informational' AS FindingsGroup , + '@CheckUserDatabaseObjects Disabled' AS Finding , + 'https://www.BrentOzar.com/blitz/' AS URL , + 'Since you have databases with compatibility_level < 90, we can''t run @CheckUserDatabaseObjects = 1. To find them: SELECT * FROM sys.databases WHERE compatibility_level < 90' AS Details; + END; + + /* --TOURSTOP08-- */ + /* If the server is Amazon RDS, skip checks that it doesn't allow */ + IF LEFT(CAST(COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),SERVERPROPERTY('ServerName')) AS VARCHAR(8000)), 8) = 'EC2AMAZ-' + AND LEFT(CAST(SERVERPROPERTY('MachineName') AS VARCHAR(8000)), 8) = 'EC2AMAZ-' + AND db_id('rdsadmin') IS NOT NULL + AND EXISTS(SELECT * FROM master.sys.all_objects WHERE name IN ('rds_startup_tasks', 'rds_help_revlogin', 'rds_hexadecimal', 'rds_failover_tracking', 'rds_database_tracking', 'rds_track_change')) + BEGIN + INSERT INTO #SkipChecks (CheckID) VALUES (6); /* Security - Jobs Owned By Users per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + INSERT INTO #SkipChecks (CheckID) VALUES (29); /* tables in model database created by users - not allowed */ + INSERT INTO #SkipChecks (CheckID) VALUES (40); /* TempDB only has one data file in RDS */ + INSERT INTO #SkipChecks (CheckID) VALUES (62); /* Database compatibility level - cannot change in RDS */ + INSERT INTO #SkipChecks (CheckID) VALUES (68); /*Check for the last good DBCC CHECKDB date - can't run DBCC DBINFO() */ + INSERT INTO #SkipChecks (CheckID) VALUES (69); /* High VLF check - requires DBCC LOGINFO permission */ + INSERT INTO #SkipChecks (CheckID) VALUES (73); /* No Failsafe Operator Configured check */ + INSERT INTO #SkipChecks (CheckID) VALUES (92); /* Drive info check - requires xp_Fixeddrives permission */ + INSERT INTO #SkipChecks (CheckID) VALUES (100); /* Remote DAC disabled */ + INSERT INTO #SkipChecks (CheckID) VALUES (177); /* Disabled Internal Monitoring Features check - requires dm_server_registry access */ + INSERT INTO #SkipChecks (CheckID) VALUES (180); /* 180/181 are maintenance plans checks - Maint plans not available in RDS*/ + INSERT INTO #SkipChecks (CheckID) VALUES (181); /*Find repetitive maintenance tasks*/ + + -- can check errorlog using rdsadmin.dbo.rds_read_error_log, so allow this check + --INSERT INTO #SkipChecks (CheckID) VALUES (193); /* xp_readerrorlog checking for IFI */ + + INSERT INTO #SkipChecks (CheckID) VALUES (211); /* xp_regread not allowed - checking for power saving */ + INSERT INTO #SkipChecks (CheckID) VALUES (212); /* xp_regread not allowed - checking for additional instances */ + INSERT INTO #SkipChecks (CheckID) VALUES (2301); /* sp_validatelogins called by Invalid login defined with Windows Authentication */ + + -- Following are skipped due to limited permissions in msdb/SQLAgent in RDS + INSERT INTO #SkipChecks (CheckID) VALUES (30); /* SQL Server Agent alerts not configured */ + INSERT INTO #SkipChecks (CheckID) VALUES (31); /* check whether we have NO ENABLED operators */ + INSERT INTO #SkipChecks (CheckID) VALUES (57); /* SQL Agent Job Runs at Startup */ + INSERT INTO #SkipChecks (CheckID) VALUES (59); /* Alerts Configured without Follow Up */ + INSERT INTO #SkipChecks (CheckID) VALUES (61); /*SQL Server Agent alerts do not exist for severity levels 19 through 25*/ + INSERT INTO #SkipChecks (CheckID) VALUES (79); /* Shrink Database Job check */ + INSERT INTO #SkipChecks (CheckID) VALUES (94); /* job failure without operator notification check */ + INSERT INTO #SkipChecks (CheckID) VALUES (96); /* Agent alerts for corruption */ + INSERT INTO #SkipChecks (CheckID) VALUES (98); /* check for disabled alerts */ + INSERT INTO #SkipChecks (CheckID) VALUES (123); /* Agent Jobs Starting Simultaneously */ + INSERT INTO #SkipChecks (CheckID) VALUES (219); /* check for alerts that do NOT include event descriptions in their outputs via email/pager/net-send */ + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 223 AS CheckID , + 0 AS Priority , + 'Informational' AS FindingsGroup , + 'Some Checks Skipped' AS Finding , + 'https://aws.amazon.com/rds/sqlserver/' AS URL , + 'Amazon RDS detected, so we skipped some checks that are not currently possible, relevant, or practical there.' AS Details; + END; /* Amazon RDS skipped checks */ + + /* If the server is ExpressEdition, skip checks that it doesn't allow */ + IF CAST(SERVERPROPERTY('Edition') AS NVARCHAR(1000)) LIKE N'%Express%' + BEGIN + INSERT INTO #SkipChecks (CheckID) VALUES (30); /* Alerts not configured */ + INSERT INTO #SkipChecks (CheckID) VALUES (31); /* Operators not configured */ + INSERT INTO #SkipChecks (CheckID) VALUES (61); /* Agent alerts 19-25 */ + INSERT INTO #SkipChecks (CheckID) VALUES (73); /* Failsafe operator */ + INSERT INTO #SkipChecks (CheckID) VALUES (96); /* Agent alerts for corruption */ + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 223 AS CheckID , + 0 AS Priority , + 'Informational' AS FindingsGroup , + 'Some Checks Skipped' AS Finding , + 'https://stackoverflow.com/questions/1169634/limitations-of-sql-server-express' AS URL , + 'Express Edition detected, so we skipped some checks that are not currently possible, relevant, or practical there.' AS Details; + END; /* Express Edition skipped checks */ + + /* If the server is an Azure Managed Instance, skip checks that it doesn't allow */ + IF SERVERPROPERTY('EngineEdition') = 8 + BEGIN + INSERT INTO #SkipChecks (CheckID) VALUES (1); /* Full backups - because of the MI GUID name bug mentioned here: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1481 */ + INSERT INTO #SkipChecks (CheckID) VALUES (2); /* Log backups - because of the MI GUID name bug mentioned here: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1481 */ + INSERT INTO #SkipChecks (CheckID) VALUES (6); /* Security - Jobs Owned By Users per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + INSERT INTO #SkipChecks (CheckID) VALUES (21); /* Informational - Database Encrypted per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + INSERT INTO #SkipChecks (CheckID) VALUES (24); /* File Configuration - System Database on C Drive per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + INSERT INTO #SkipChecks (CheckID) VALUES (50); /* Max Server Memory Set Too High - because they max it out */ + INSERT INTO #SkipChecks (CheckID) VALUES (55); /* Security - Database Owner <> sa per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + INSERT INTO #SkipChecks (CheckID) VALUES (74); /* TraceFlag On - because Azure Managed Instances go wild and crazy with the trace flags */ + INSERT INTO #SkipChecks (CheckID) VALUES (97); /* Unusual SQL Server Edition */ + INSERT INTO #SkipChecks (CheckID) VALUES (100); /* Remote DAC disabled - but it's working anyway, details here: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1481 */ + INSERT INTO #SkipChecks (CheckID) VALUES (186); /* MSDB Backup History Purged Too Frequently */ + INSERT INTO #SkipChecks (CheckID) VALUES (199); /* Default trace, details here: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1481 */ + INSERT INTO #SkipChecks (CheckID) VALUES (211); /*Power Plan */ + INSERT INTO #SkipChecks (CheckID, DatabaseName) VALUES (80, 'master'); /* Max file size set */ + INSERT INTO #SkipChecks (CheckID, DatabaseName) VALUES (80, 'model'); /* Max file size set */ + INSERT INTO #SkipChecks (CheckID, DatabaseName) VALUES (80, 'msdb'); /* Max file size set */ + INSERT INTO #SkipChecks (CheckID, DatabaseName) VALUES (80, 'tempdb'); /* Max file size set */ + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 223 AS CheckID , + 0 AS Priority , + 'Informational' AS FindingsGroup , + 'Some Checks Skipped' AS Finding , + 'https://docs.microsoft.com/en-us/azure/sql-database/sql-database-managed-instance-index' AS URL , + 'Managed Instance detected, so we skipped some checks that are not currently possible, relevant, or practical there.' AS Details; + END; /* Azure Managed Instance skipped checks */ + + /* + That's the end of the SkipChecks stuff. + The next several tables are used by various checks later. + */ + IF OBJECT_ID('tempdb..#ConfigurationDefaults') IS NOT NULL + DROP TABLE #ConfigurationDefaults; + CREATE TABLE #ConfigurationDefaults + ( + name NVARCHAR(128) , + DefaultValue BIGINT, + CheckID INT + ); + + IF OBJECT_ID ('tempdb..#Recompile') IS NOT NULL + DROP TABLE #Recompile; + CREATE TABLE #Recompile( + DBName varchar(200), + ProcName varchar(300), + RecompileFlag varchar(1), + SPSchema varchar(50) + ); + + IF OBJECT_ID('tempdb..#DatabaseDefaults') IS NOT NULL + DROP TABLE #DatabaseDefaults; + CREATE TABLE #DatabaseDefaults + ( + name NVARCHAR(128) , + DefaultValue NVARCHAR(200), + CheckID INT, + Priority INT, + Finding VARCHAR(200), + URL VARCHAR(200), + Details NVARCHAR(4000) + ); + + IF OBJECT_ID('tempdb..#DatabaseScopedConfigurationDefaults') IS NOT NULL + DROP TABLE #DatabaseScopedConfigurationDefaults; + CREATE TABLE #DatabaseScopedConfigurationDefaults + (ID INT IDENTITY(1,1), configuration_id INT, [name] NVARCHAR(60), default_value sql_variant, default_value_for_secondary sql_variant, CheckID INT, ); + + IF OBJECT_ID('tempdb..#DBCCs') IS NOT NULL + DROP TABLE #DBCCs; + CREATE TABLE #DBCCs + ( + ID INT IDENTITY(1, 1) + PRIMARY KEY , + ParentObject VARCHAR(255) , + Object VARCHAR(255) , + Field VARCHAR(255) , + Value VARCHAR(255) , + DbName NVARCHAR(128) NULL + ); + + IF OBJECT_ID('tempdb..#LogInfo2012') IS NOT NULL + DROP TABLE #LogInfo2012; + CREATE TABLE #LogInfo2012 + ( + recoveryunitid INT , + FileID SMALLINT , + FileSize BIGINT , + StartOffset BIGINT , + FSeqNo BIGINT , + [Status] TINYINT , + Parity TINYINT , + CreateLSN NUMERIC(38) + ); + + IF OBJECT_ID('tempdb..#LogInfo') IS NOT NULL + DROP TABLE #LogInfo; + CREATE TABLE #LogInfo + ( + FileID SMALLINT , + FileSize BIGINT , + StartOffset BIGINT , + FSeqNo BIGINT , + [Status] TINYINT , + Parity TINYINT , + CreateLSN NUMERIC(38) + ); + + IF OBJECT_ID('tempdb..#partdb') IS NOT NULL + DROP TABLE #partdb; + CREATE TABLE #partdb + ( + dbname NVARCHAR(128) , + objectname NVARCHAR(200) , + type_desc NVARCHAR(128) + ); + + IF OBJECT_ID('tempdb..#TraceStatus') IS NOT NULL + DROP TABLE #TraceStatus; + CREATE TABLE #TraceStatus + ( + TraceFlag VARCHAR(10) , + status BIT , + Global BIT , + Session BIT + ); + + IF OBJECT_ID('tempdb..#driveInfo') IS NOT NULL + DROP TABLE #driveInfo; + CREATE TABLE #driveInfo + ( + drive NVARCHAR(2), + logical_volume_name NVARCHAR(36), --Limit is 32 for NTFS, 11 for FAT + available_MB DECIMAL(18, 0), + total_MB DECIMAL(18, 0), + used_percent DECIMAL(18, 2) + ); + + IF OBJECT_ID('tempdb..#dm_exec_query_stats') IS NOT NULL + DROP TABLE #dm_exec_query_stats; + CREATE TABLE #dm_exec_query_stats + ( + [id] [int] NOT NULL + IDENTITY(1, 1) , + [sql_handle] [varbinary](64) NOT NULL , + [statement_start_offset] [int] NOT NULL , + [statement_end_offset] [int] NOT NULL , + [plan_generation_num] [bigint] NOT NULL , + [plan_handle] [varbinary](64) NOT NULL , + [creation_time] [datetime] NOT NULL , + [last_execution_time] [datetime] NOT NULL , + [execution_count] [bigint] NOT NULL , + [total_worker_time] [bigint] NOT NULL , + [last_worker_time] [bigint] NOT NULL , + [min_worker_time] [bigint] NOT NULL , + [max_worker_time] [bigint] NOT NULL , + [total_physical_reads] [bigint] NOT NULL , + [last_physical_reads] [bigint] NOT NULL , + [min_physical_reads] [bigint] NOT NULL , + [max_physical_reads] [bigint] NOT NULL , + [total_logical_writes] [bigint] NOT NULL , + [last_logical_writes] [bigint] NOT NULL , + [min_logical_writes] [bigint] NOT NULL , + [max_logical_writes] [bigint] NOT NULL , + [total_logical_reads] [bigint] NOT NULL , + [last_logical_reads] [bigint] NOT NULL , + [min_logical_reads] [bigint] NOT NULL , + [max_logical_reads] [bigint] NOT NULL , + [total_clr_time] [bigint] NOT NULL , + [last_clr_time] [bigint] NOT NULL , + [min_clr_time] [bigint] NOT NULL , + [max_clr_time] [bigint] NOT NULL , + [total_elapsed_time] [bigint] NOT NULL , + [last_elapsed_time] [bigint] NOT NULL , + [min_elapsed_time] [bigint] NOT NULL , + [max_elapsed_time] [bigint] NOT NULL , + [query_hash] [binary](8) NULL , + [query_plan_hash] [binary](8) NULL , + [query_plan] [xml] NULL , + [query_plan_filtered] [nvarchar](MAX) NULL , + [text] [nvarchar](MAX) COLLATE SQL_Latin1_General_CP1_CI_AS + NULL , + [text_filtered] [nvarchar](MAX) COLLATE SQL_Latin1_General_CP1_CI_AS + NULL + ); + + IF OBJECT_ID('tempdb..#ErrorLog') IS NOT NULL + DROP TABLE #ErrorLog; + CREATE TABLE #ErrorLog + ( + LogDate DATETIME , + ProcessInfo NVARCHAR(20) , + [Text] NVARCHAR(1000) + ); + + IF OBJECT_ID('tempdb..#fnTraceGettable') IS NOT NULL + DROP TABLE #fnTraceGettable; + CREATE TABLE #fnTraceGettable + ( + TextData NVARCHAR(4000) , + DatabaseName NVARCHAR(256) , + EventClass INT , + Severity INT , + StartTime DATETIME , + EndTime DATETIME , + Duration BIGINT , + NTUserName NVARCHAR(256) , + NTDomainName NVARCHAR(256) , + HostName NVARCHAR(256) , + ApplicationName NVARCHAR(256) , + LoginName NVARCHAR(256) , + DBUserName NVARCHAR(256) + ); + + IF OBJECT_ID('tempdb..#Instances') IS NOT NULL + DROP TABLE #Instances; + CREATE TABLE #Instances + ( + Instance_Number NVARCHAR(MAX) , + Instance_Name NVARCHAR(MAX) , + Data_Field NVARCHAR(MAX) + ); + + IF OBJECT_ID('tempdb..#IgnorableWaits') IS NOT NULL + DROP TABLE #IgnorableWaits; + CREATE TABLE #IgnorableWaits (wait_type NVARCHAR(60)); + INSERT INTO #IgnorableWaits VALUES ('BROKER_EVENTHANDLER'); + INSERT INTO #IgnorableWaits VALUES ('BROKER_RECEIVE_WAITFOR'); + INSERT INTO #IgnorableWaits VALUES ('BROKER_TASK_STOP'); + INSERT INTO #IgnorableWaits VALUES ('BROKER_TO_FLUSH'); + INSERT INTO #IgnorableWaits VALUES ('BROKER_TRANSMITTER'); + INSERT INTO #IgnorableWaits VALUES ('CHECKPOINT_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('CLR_AUTO_EVENT'); + INSERT INTO #IgnorableWaits VALUES ('CLR_MANUAL_EVENT'); + INSERT INTO #IgnorableWaits VALUES ('CLR_SEMAPHORE'); + INSERT INTO #IgnorableWaits VALUES ('DBMIRROR_DBM_EVENT'); + INSERT INTO #IgnorableWaits VALUES ('DBMIRROR_DBM_MUTEX'); + INSERT INTO #IgnorableWaits VALUES ('DBMIRROR_EVENTS_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('DBMIRROR_WORKER_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('DBMIRRORING_CMD'); + INSERT INTO #IgnorableWaits VALUES ('DIRTY_PAGE_POLL'); + INSERT INTO #IgnorableWaits VALUES ('DISPATCHER_QUEUE_SEMAPHORE'); + INSERT INTO #IgnorableWaits VALUES ('FT_IFTS_SCHEDULER_IDLE_WAIT'); + INSERT INTO #IgnorableWaits VALUES ('FT_IFTSHC_MUTEX'); + INSERT INTO #IgnorableWaits VALUES ('HADR_CLUSAPI_CALL'); + INSERT INTO #IgnorableWaits VALUES ('HADR_FABRIC_CALLBACK'); + INSERT INTO #IgnorableWaits VALUES ('HADR_FILESTREAM_IOMGR_IOCOMPLETION'); + INSERT INTO #IgnorableWaits VALUES ('HADR_LOGCAPTURE_WAIT'); + INSERT INTO #IgnorableWaits VALUES ('HADR_NOTIFICATION_DEQUEUE'); + INSERT INTO #IgnorableWaits VALUES ('HADR_TIMER_TASK'); + INSERT INTO #IgnorableWaits VALUES ('HADR_WORK_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('LAZYWRITER_SLEEP'); + INSERT INTO #IgnorableWaits VALUES ('LOGMGR_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('ONDEMAND_TASK_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('PARALLEL_REDO_DRAIN_WORKER'); + INSERT INTO #IgnorableWaits VALUES ('PARALLEL_REDO_LOG_CACHE'); + INSERT INTO #IgnorableWaits VALUES ('PARALLEL_REDO_TRAN_LIST'); + INSERT INTO #IgnorableWaits VALUES ('PARALLEL_REDO_WORKER_SYNC'); + INSERT INTO #IgnorableWaits VALUES ('PARALLEL_REDO_WORKER_WAIT_WORK'); + INSERT INTO #IgnorableWaits VALUES ('POPULATE_LOCK_ORDINALS'); + INSERT INTO #IgnorableWaits VALUES ('PREEMPTIVE_HADR_LEASE_MECHANISM'); + INSERT INTO #IgnorableWaits VALUES ('PREEMPTIVE_SP_SERVER_DIAGNOSTICS'); + INSERT INTO #IgnorableWaits VALUES ('QDS_ASYNC_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP'); + INSERT INTO #IgnorableWaits VALUES ('QDS_PERSIST_TASK_MAIN_LOOP_SLEEP'); + INSERT INTO #IgnorableWaits VALUES ('QDS_SHUTDOWN_QUEUE'); + INSERT INTO #IgnorableWaits VALUES ('REDO_THREAD_PENDING_WORK'); + INSERT INTO #IgnorableWaits VALUES ('REQUEST_FOR_DEADLOCK_SEARCH'); + INSERT INTO #IgnorableWaits VALUES ('SLEEP_SYSTEMTASK'); + INSERT INTO #IgnorableWaits VALUES ('SLEEP_TASK'); + INSERT INTO #IgnorableWaits VALUES ('SOS_WORK_DISPATCHER'); + INSERT INTO #IgnorableWaits VALUES ('SP_SERVER_DIAGNOSTICS_SLEEP'); + INSERT INTO #IgnorableWaits VALUES ('SQLTRACE_BUFFER_FLUSH'); + INSERT INTO #IgnorableWaits VALUES ('SQLTRACE_INCREMENTAL_FLUSH_SLEEP'); + INSERT INTO #IgnorableWaits VALUES ('UCS_SESSION_REGISTRATION'); + INSERT INTO #IgnorableWaits VALUES ('WAIT_XTP_OFFLINE_CKPT_NEW_LOG'); + INSERT INTO #IgnorableWaits VALUES ('WAITFOR'); + INSERT INTO #IgnorableWaits VALUES ('XE_DISPATCHER_WAIT'); + INSERT INTO #IgnorableWaits VALUES ('XE_LIVE_TARGET_TVF'); + INSERT INTO #IgnorableWaits VALUES ('XE_TIMER_EVENT'); + + IF @Debug IN (1, 2) RAISERROR('Setting @MsSinceWaitsCleared', 0, 1) WITH NOWAIT; + + SELECT @MsSinceWaitsCleared = DATEDIFF(MINUTE, create_date, CURRENT_TIMESTAMP) * 60000.0 + FROM sys.databases + WHERE name = 'tempdb'; + + /* Have they cleared wait stats? Using a 10% fudge factor */ + IF @MsSinceWaitsCleared * .9 > (SELECT MAX(wait_time_ms) FROM sys.dm_os_wait_stats WHERE wait_type IN ('SP_SERVER_DIAGNOSTICS_SLEEP', 'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP', 'REQUEST_FOR_DEADLOCK_SEARCH', 'HADR_FILESTREAM_IOMGR_IOCOMPLETION', 'LAZYWRITER_SLEEP', 'SQLTRACE_INCREMENTAL_FLUSH_SLEEP', 'DIRTY_PAGE_POLL', 'LOGMGR_QUEUE')) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 185) WITH NOWAIT; + + SET @MsSinceWaitsCleared = (SELECT MAX(wait_time_ms) FROM sys.dm_os_wait_stats WHERE wait_type IN ('SP_SERVER_DIAGNOSTICS_SLEEP', 'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP', 'REQUEST_FOR_DEADLOCK_SEARCH', 'HADR_FILESTREAM_IOMGR_IOCOMPLETION', 'LAZYWRITER_SLEEP', 'SQLTRACE_INCREMENTAL_FLUSH_SLEEP', 'DIRTY_PAGE_POLL', 'LOGMGR_QUEUE')); + IF @MsSinceWaitsCleared = 0 SET @MsSinceWaitsCleared = 1; + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES( 185, + 240, + 'Wait Stats', + 'Wait Stats Have Been Cleared', + 'https://www.brentozar.com/go/waits', + 'Someone ran DBCC SQLPERF to clear sys.dm_os_wait_stats at approximately: ' + + CONVERT(NVARCHAR(100), + DATEADD(MINUTE, (-1. * (@MsSinceWaitsCleared) / 1000. / 60.), GETDATE()), 120)); + END; + + /* @CpuMsSinceWaitsCleared is used for waits stats calculations */ + + IF @Debug IN (1, 2) RAISERROR('Setting @CpuMsSinceWaitsCleared', 0, 1) WITH NOWAIT; + + SELECT @CpuMsSinceWaitsCleared = @MsSinceWaitsCleared * scheduler_count + FROM sys.dm_os_sys_info; + + /* If we're outputting CSV or Markdown, don't bother checking the plan cache because we cannot export plans. */ + IF @OutputType = 'CSV' OR @OutputType = 'MARKDOWN' + SET @CheckProcedureCache = 0; + + /* If we're posting a question on Stack, include background info on the server */ + IF @OutputType = 'MARKDOWN' + SET @CheckServerInfo = 1; + + /* Only run CheckUserDatabaseObjects if there are less than 50 databases. */ + IF @BringThePain = 0 AND 50 <= (SELECT COUNT(*) FROM sys.databases) AND @CheckUserDatabaseObjects = 1 + BEGIN + SET @CheckUserDatabaseObjects = 0; + PRINT 'Running sp_Blitz @CheckUserDatabaseObjects = 1 on a server with 50+ databases may cause temporary insanity for the server and/or user.'; + PRINT 'If you''re sure you want to do this, run again with the parameter @BringThePain = 1.'; + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 201 AS CheckID , + 0 AS Priority , + 'Informational' AS FindingsGroup , + '@CheckUserDatabaseObjects Disabled' AS Finding , + 'https://www.BrentOzar.com/blitz/' AS URL , + 'If you want to check 50+ databases, you have to also use @BringThePain = 1.' AS Details; + END; + + /* Sanitize our inputs */ + SELECT + @OutputServerName = QUOTENAME(@OutputServerName), + @OutputDatabaseName = QUOTENAME(@OutputDatabaseName), + @OutputSchemaName = QUOTENAME(@OutputSchemaName), + @OutputTableName = QUOTENAME(@OutputTableName); + + /* Get the major and minor build numbers */ + + IF @Debug IN (1, 2) RAISERROR('Getting version information.', 0, 1) WITH NOWAIT; + + SET @ProductVersion = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)); + SELECT @ProductVersionMajor = SUBSTRING(@ProductVersion, 1,CHARINDEX('.', @ProductVersion) + 1 ), + @ProductVersionMinor = PARSENAME(CONVERT(varchar(32), @ProductVersion), 2); + + /* + Whew! we're finally done with the setup, and we can start doing checks. + First, let's make sure we're actually supposed to do checks on this server. + The user could have passed in a SkipChecks table that specified to skip ALL + checks on this server, so let's check for that: + */ + IF ( ( SERVERPROPERTY('ServerName') NOT IN ( SELECT ServerName + FROM #SkipChecks + WHERE DatabaseName IS NULL + AND CheckID IS NULL ) ) + OR ( @SkipChecksTable IS NULL ) + ) + BEGIN + + /* + Extract DBCC DBINFO data from the server. This data is used for check 2 using + the dbi_LastLogBackupTime field and check 68 using the dbi_LastKnownGood field. + NB: DBCC DBINFO is not available on AWS RDS databases so if the server is RDS + (which will have previously triggered inserting a checkID 223 record) and at + least one of the relevant checks is not being skipped then we can extract the + dbinfo information. + */ + IF NOT EXISTS ( SELECT 1 + FROM #BlitzResults + WHERE CheckID = 223 AND URL = 'https://aws.amazon.com/rds/sqlserver/') + AND ( + NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 2 ) + OR NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 68 ) + ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Extracting DBCC DBINFO data (used in checks 2 and 68).', 0, 1, 68) WITH NOWAIT; + + EXEC sp_MSforeachdb N'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT #DBCCs + (ParentObject, + Object, + Field, + Value) + EXEC (''DBCC DBInfo() With TableResults, NO_INFOMSGS''); + UPDATE #DBCCs SET DbName = N''?'' WHERE DbName IS NULL OPTION (RECOMPILE);'; + END + + /* + Our very first check! We'll put more comments in this one just to + explain exactly how it works. First, we check to see if we're + supposed to skip CheckID 1 (that's the check we're working on.) + */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 1 ) + BEGIN + + /* + Below, we check master.sys.databases looking for databases + that haven't had a backup in the last week. If we find any, + we insert them into #BlitzResults, the temp table that + tracks our server's problems. Note that if the check does + NOT find any problems, we don't save that. We're only + saving the problems, not the successful checks. + */ + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 1) WITH NOWAIT; + + IF SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances need a special query */ + BEGIN + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 1 AS CheckID , + d.[name] AS DatabaseName , + 1 AS Priority , + 'Backup' AS FindingsGroup , + 'Backups Not Performed Recently' AS Finding , + 'https://www.brentozar.com/go/nobak' AS URL , + 'Last backed up: ' + + COALESCE(CAST(MAX(b.backup_finish_date) AS VARCHAR(25)),'never') AS Details + FROM master.sys.databases d + LEFT OUTER JOIN msdb.dbo.backupset b ON d.name COLLATE SQL_Latin1_General_CP1_CI_AS = b.database_name COLLATE SQL_Latin1_General_CP1_CI_AS + AND b.type = 'D' + AND b.server_name = SERVERPROPERTY('ServerName') /*Backupset ran on current server */ + WHERE d.database_id <> 2 /* Bonus points if you know what that means */ + AND d.state NOT IN(1, 6, 10) /* Not currently offline or restoring, like log shipping databases */ + AND d.is_in_standby = 0 /* Not a log shipping target database */ + AND d.source_database_id IS NULL /* Excludes database snapshots */ + AND d.name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 1) + /* + The above NOT IN filters out the databases we're not supposed to check. + */ + GROUP BY d.name + HAVING MAX(b.backup_finish_date) <= DATEADD(dd, + -7, GETDATE()) + OR MAX(b.backup_finish_date) IS NULL; + END; + + ELSE /* SERVERPROPERTY('EngineName') must be 8, Azure Managed Instances */ + BEGIN + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 1 AS CheckID , + d.[name] AS DatabaseName , + 1 AS Priority , + 'Backup' AS FindingsGroup , + 'Backups Not Performed Recently' AS Finding , + 'https://www.brentozar.com/go/nobak' AS URL , + 'Last backed up: ' + + COALESCE(CAST(MAX(b.backup_finish_date) AS VARCHAR(25)),'never') AS Details + FROM master.sys.databases d + LEFT OUTER JOIN msdb.dbo.backupset b ON d.name COLLATE SQL_Latin1_General_CP1_CI_AS = b.database_name COLLATE SQL_Latin1_General_CP1_CI_AS + AND b.type = 'D' + WHERE d.database_id <> 2 /* Bonus points if you know what that means */ + AND d.state NOT IN(1, 6, 10) /* Not currently offline or restoring, like log shipping databases */ + AND d.is_in_standby = 0 /* Not a log shipping target database */ + AND d.source_database_id IS NULL /* Excludes database snapshots */ + AND d.name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 1) + /* + The above NOT IN filters out the databases we're not supposed to check. + */ + GROUP BY d.name + HAVING MAX(b.backup_finish_date) <= DATEADD(dd, + -7, GETDATE()) + OR MAX(b.backup_finish_date) IS NULL; + END; + + + + /* + And there you have it. The rest of this stored procedure works the same + way: it asks: + - Should I skip this check? + - If not, do I find problems? + - Insert the results into #BlitzResults + */ + + END; + + /* + And that's the end of CheckID #1. + + CheckID #2 is a little simpler because it only involves one query, and it's + more typical for queries that people contribute. But keep reading, because + the next check gets more complex again. + */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 2 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 2) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 2 AS CheckID , + d.name AS DatabaseName , + 1 AS Priority , + 'Backup' AS FindingsGroup , + 'Full Recovery Model w/o Log Backups' AS Finding , + 'https://www.brentozar.com/go/biglogs' AS URL , + ( 'The ' + CAST(CAST((SELECT ((SUM([mf].[size]) * 8.) / 1024.) FROM sys.[master_files] AS [mf] WHERE [mf].[database_id] = d.[database_id] AND [mf].[type_desc] = 'LOG') AS DECIMAL(18,2)) AS VARCHAR(30)) + 'MB log file has not been backed up in the last week.' ) AS Details + FROM master.sys.databases d + LEFT JOIN #DBCCs ll On ll.DbName = d.name And ll.Field = 'dbi_LastLogBackupTime' + WHERE d.recovery_model IN ( 1, 2 ) + AND d.database_id NOT IN ( 2, 3 ) + AND d.source_database_id IS NULL + AND d.state NOT IN(1, 6, 10) /* Not currently offline or restoring, like log shipping databases */ + AND d.is_in_standby = 0 /* Not a log shipping target database */ + AND d.source_database_id IS NULL /* Excludes database snapshots */ + AND d.name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 2) + AND ( + ( + /* We couldn't get a value from the DBCC DBINFO data so let's check the msdb backup history information */ + [ll].[Value] Is Null + AND NOT EXISTS ( SELECT * + FROM msdb.dbo.backupset b + WHERE d.name COLLATE SQL_Latin1_General_CP1_CI_AS = b.database_name COLLATE SQL_Latin1_General_CP1_CI_AS + AND b.type = 'L' + AND b.backup_finish_date >= DATEADD(dd,-7, GETDATE()) + ) + ) + OR + ( + Convert(datetime,ll.Value,21) < DATEADD(dd,-7, GETDATE()) + ) + + ); + END; + + /* + CheckID #256 is searching for backups to NUL. + */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 256 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 2) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 256 AS CheckID , + d.name AS DatabaseName, + 1 AS Priority , + 'Backup' AS FindingsGroup , + 'Log Backups to NUL' AS Finding , + 'https://www.brentozar.com/go/nul' AS URL , + N'The transaction log file has been backed up ' + CAST((SELECT count(*) + FROM msdb.dbo.backupset AS b INNER JOIN + msdb.dbo.backupmediafamily AS bmf + ON b.media_set_id = bmf.media_set_id + WHERE b.database_name COLLATE SQL_Latin1_General_CP1_CI_AS = d.name COLLATE SQL_Latin1_General_CP1_CI_AS + AND bmf.physical_device_name = 'NUL' + AND b.type = 'L' + AND b.backup_finish_date >= DATEADD(dd, + -7, GETDATE())) AS NVARCHAR(8)) + ' time(s) to ''NUL'' in the last week, which means the backup does not exist. This breaks point-in-time recovery.' AS Details + FROM master.sys.databases AS d + WHERE d.recovery_model IN ( 1, 2 ) + AND d.database_id NOT IN ( 2, 3 ) + AND d.source_database_id IS NULL + AND d.state NOT IN(1, 6, 10) /* Not currently offline or restoring, like log shipping databases */ + AND d.is_in_standby = 0 /* Not a log shipping target database */ + AND d.source_database_id IS NULL /* Excludes database snapshots */ + --AND d.name NOT IN ( SELECT DISTINCT + -- DatabaseName + -- FROM #SkipChecks + -- WHERE CheckID IS NULL OR CheckID = 2) + AND EXISTS ( SELECT * + FROM msdb.dbo.backupset AS b INNER JOIN + msdb.dbo.backupmediafamily AS bmf + ON b.media_set_id = bmf.media_set_id + WHERE d.name COLLATE SQL_Latin1_General_CP1_CI_AS = b.database_name COLLATE SQL_Latin1_General_CP1_CI_AS + AND bmf.physical_device_name = 'NUL' + AND b.type = 'L' + AND b.backup_finish_date >= DATEADD(dd, + -7, GETDATE()) ); + END; + + /* + Next up, we've got CheckID 8. (These don't have to go in order.) This one + won't work on SQL Server 2005 because it relies on a new DMV that didn't + exist prior to SQL Server 2008. This means we have to check the SQL Server + version first, then build a dynamic string with the query we want to run: + */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 8 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 8) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, Priority, + FindingsGroup, + Finding, URL, + Details) + SELECT 8 AS CheckID, + 230 AS Priority, + ''Security'' AS FindingsGroup, + ''Server Audits Running'' AS Finding, + ''https://www.brentozar.com/go/audits'' AS URL, + (''SQL Server built-in audit functionality is being used by server audit: '' + [name]) AS Details FROM sys.dm_server_audit_status OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + /* + But what if you need to run a query in every individual database? + Hop down to the @CheckUserDatabaseObjects section. + + And that's the basic idea! You can read through the rest of the + checks if you like - some more exciting stuff happens closer to the + end of the stored proc, where we start doing things like checking + the plan cache, but those aren't as cleanly commented. + + If you'd like to contribute your own check, use one of the check + formats shown above and email it to Help@BrentOzar.com. You don't + have to pick a CheckID or a link - we'll take care of that when we + test and publish the code. Thanks! + */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 93 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 93) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 93 AS CheckID , + 1 AS Priority , + 'Backup' AS FindingsGroup , + 'Backing Up to Same Drive Where Databases Reside' AS Finding , + 'https://www.brentozar.com/go/backup' AS URL , + CAST(COUNT(1) AS VARCHAR(50)) + ' backups done on drive ' + + UPPER(LEFT(bmf.physical_device_name, 3)) + + ' in the last two weeks, where database files also live. This represents a serious risk if that array fails.' Details + FROM msdb.dbo.backupmediafamily AS bmf + INNER JOIN msdb.dbo.backupset AS bs ON bmf.media_set_id = bs.media_set_id + AND bs.backup_start_date >= ( DATEADD(dd, + -14, GETDATE()) ) + /* Filter out databases that were recently restored: */ + LEFT OUTER JOIN msdb.dbo.restorehistory rh ON bs.database_name = rh.destination_database_name AND rh.restore_date > DATEADD(dd, -14, GETDATE()) + WHERE UPPER(LEFT(bmf.physical_device_name, 3)) <> 'HTT' AND + bmf.physical_device_name NOT LIKE '\\%' AND -- GitHub Issue #2141 + @IsWindowsOperatingSystem = 1 AND -- GitHub Issue #1995 + UPPER(LEFT(bmf.physical_device_name COLLATE SQL_Latin1_General_CP1_CI_AS, 3)) IN ( + SELECT DISTINCT + UPPER(LEFT(mf.physical_name COLLATE SQL_Latin1_General_CP1_CI_AS, 3)) + FROM sys.master_files AS mf + WHERE mf.database_id <> 2 ) + AND rh.destination_database_name IS NULL + GROUP BY UPPER(LEFT(bmf.physical_device_name, 3)); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 119 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + WHERE o.name = 'dm_database_encryption_keys' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 119) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, DatabaseName, URL, Details) + SELECT 119 AS CheckID, + 1 AS Priority, + ''Backup'' AS FindingsGroup, + ''TDE Certificate Not Backed Up Recently'' AS Finding, + db_name(dek.database_id) AS DatabaseName, + ''https://www.brentozar.com/go/tde'' AS URL, + ''The certificate '' + c.name + '' is used to encrypt database '' + db_name(dek.database_id) + ''. Last backup date: '' + COALESCE(CAST(c.pvt_key_last_backup_date AS VARCHAR(100)), ''Never'') AS Details + FROM master.sys.certificates c INNER JOIN sys.dm_database_encryption_keys dek ON c.thumbprint = dek.encryptor_thumbprint + WHERE pvt_key_last_backup_date IS NULL OR pvt_key_last_backup_date <= DATEADD(dd, -30, GETDATE()) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 202 ) + AND EXISTS ( SELECT * + FROM sys.all_columns c + WHERE c.name = 'pvt_key_last_backup_date' ) + AND EXISTS ( SELECT * + FROM msdb.INFORMATION_SCHEMA.COLUMNS c + WHERE c.TABLE_NAME = 'backupset' AND c.COLUMN_NAME = 'encryptor_thumbprint' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 202) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT DISTINCT 202 AS CheckID, + 1 AS Priority, + ''Backup'' AS FindingsGroup, + ''Encryption Certificate Not Backed Up Recently'' AS Finding, + ''https://www.brentozar.com/go/tde'' AS URL, + ''The certificate '' + c.name + '' is used to encrypt database backups. Last backup date: '' + COALESCE(CAST(c.pvt_key_last_backup_date AS VARCHAR(100)), ''Never'') AS Details + FROM sys.certificates c + INNER JOIN msdb.dbo.backupset bs ON c.thumbprint = bs.encryptor_thumbprint + WHERE pvt_key_last_backup_date IS NULL OR pvt_key_last_backup_date <= DATEADD(dd, -30, GETDATE()) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 3 ) + BEGIN + IF DATEADD(dd, -60, GETDATE()) > (SELECT TOP 1 backup_start_date FROM msdb.dbo.backupset ORDER BY backup_start_date) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 3) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT TOP 1 + 3 AS CheckID , + 'msdb' , + 200 AS Priority , + 'Backup' AS FindingsGroup , + 'MSDB Backup History Not Purged' AS Finding , + 'https://www.brentozar.com/go/history' AS URL , + ( 'Database backup history retained back to ' + + CAST(bs.backup_start_date AS VARCHAR(20)) ) AS Details + FROM msdb.dbo.backupset bs + LEFT OUTER JOIN msdb.dbo.restorehistory rh ON bs.database_name = rh.destination_database_name + WHERE rh.destination_database_name IS NULL + ORDER BY bs.backup_start_date ASC; + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 186 ) + BEGIN + IF DATEADD(dd, -2, GETDATE()) < (SELECT TOP 1 backup_start_date FROM msdb.dbo.backupset ORDER BY backup_start_date) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 186) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT TOP 1 + 186 AS CheckID , + 'msdb' , + 200 AS Priority , + 'Backup' AS FindingsGroup , + 'MSDB Backup History Purged Too Frequently' AS Finding , + 'https://www.brentozar.com/go/history' AS URL , + ( 'Database backup history only retained back to ' + + CAST(bs.backup_start_date AS VARCHAR(20)) ) AS Details + FROM msdb.dbo.backupset bs + ORDER BY backup_start_date ASC; + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 178 ) + AND EXISTS (SELECT * + FROM msdb.dbo.backupset bs + WHERE bs.type = 'D' + AND bs.backup_size >= 50000000000 /* At least 50GB */ + AND DATEDIFF(SECOND, bs.backup_start_date, bs.backup_finish_date) <= 60 /* Backup took less than 60 seconds */ + AND bs.backup_finish_date >= DATEADD(DAY, -14, GETDATE()) /* In the last 2 weeks */) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 178) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 178 AS CheckID , + 200 AS Priority , + 'Performance' AS FindingsGroup , + 'Snapshot Backups Occurring' AS Finding , + 'https://www.brentozar.com/go/snaps' AS URL , + ( CAST(COUNT(*) AS VARCHAR(20)) + ' snapshot-looking backups have occurred in the last two weeks, indicating that IO may be freezing up.') AS Details + FROM msdb.dbo.backupset bs + WHERE bs.type = 'D' + AND bs.backup_size >= 50000000000 /* At least 50GB */ + AND DATEDIFF(SECOND, bs.backup_start_date, bs.backup_finish_date) <= 60 /* Backup took less than 60 seconds */ + AND bs.backup_finish_date >= DATEADD(DAY, -14, GETDATE()); /* In the last 2 weeks */ + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 236 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 236) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT TOP 1 236 AS CheckID , + 50 AS Priority , + 'Performance' AS FindingsGroup , + 'Snapshotting Too Many Databases' AS Finding , + 'https://www.brentozar.com/go/toomanysnaps' AS URL , + ( CAST(SUM(1) AS VARCHAR(20)) + ' databases snapshotted at once in the last two weeks, indicating that IO may be freezing up. Microsoft does not recommend VSS snaps for 35 or more databases.') AS Details + FROM msdb.dbo.backupset bs + WHERE bs.type = 'D' + AND bs.backup_finish_date >= DATEADD(DAY, -14, GETDATE()) /* In the last 2 weeks */ + GROUP BY bs.backup_finish_date + HAVING SUM(1) >= 35 + ORDER BY SUM(1) DESC; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 4 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 4) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 4 AS CheckID , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Sysadmins' AS Finding , + 'https://www.brentozar.com/go/sa' AS URL , + ( 'Login [' + l.name + + '] is a sysadmin - meaning they can do absolutely anything in SQL Server, including dropping databases or hiding their tracks.' ) AS Details + FROM master.sys.syslogins l + WHERE l.sysadmin = 1 + AND l.name <> SUSER_SNAME(0x01) + AND l.denylogin = 0 + AND l.name NOT LIKE 'NT SERVICE\%' + AND l.name <> 'l_certSignSmDetach'; /* Added in SQL 2016 */ + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE CheckID = 2301 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 2301) WITH NOWAIT; + + INSERT INTO #InvalidLogins + EXEC sp_validatelogins + ; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 2301 AS CheckID , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Invalid login defined with Windows Authentication' AS Finding , + 'https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-validatelogins-transact-sql' AS URL , + ( 'Windows user or group ' + QUOTENAME(LoginName) + ' is mapped to a SQL Server principal but no longer exists in the Windows environment.') AS Details + FROM #InvalidLogins + ; + END; + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 5 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 5) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 5 AS CheckID , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Security Admins' AS Finding , + 'https://www.brentozar.com/go/sa' AS URL , + ( 'Login [' + l.name + + '] is a security admin - meaning they can give themselves permission to do absolutely anything in SQL Server, including dropping databases or hiding their tracks.' ) AS Details + FROM master.sys.syslogins l + WHERE l.securityadmin = 1 + AND l.name <> SUSER_SNAME(0x01) + AND l.denylogin = 0; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 104 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 104) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] + ) + SELECT 104 AS [CheckID] , + 230 AS [Priority] , + 'Security' AS [FindingsGroup] , + 'Login Can Control Server' AS [Finding] , + 'https://www.brentozar.com/go/sa' AS [URL] , + 'Login [' + pri.[name] + + '] has the CONTROL SERVER permission - meaning they can do absolutely anything in SQL Server, including dropping databases or hiding their tracks.' AS [Details] + FROM sys.server_principals AS pri + WHERE pri.[principal_id] IN ( + SELECT p.[grantee_principal_id] + FROM sys.server_permissions AS p + WHERE p.[state] IN ( 'G', 'W' ) + AND p.[class] = 100 + AND p.[type] = 'CL' ) + AND pri.[name] NOT LIKE '##%##'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 6 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 6) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 6 AS CheckID , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Jobs Owned By Users' AS Finding , + 'https://www.brentozar.com/go/owners' AS URL , + ( 'Job [' + j.name + '] is owned by [' + + SUSER_SNAME(j.owner_sid) + + '] - meaning if their login is disabled or not available due to Active Directory problems, the job will stop working.' ) AS Details + FROM msdb.dbo.sysjobs j + WHERE j.enabled = 1 + AND SUSER_SNAME(j.owner_sid) <> SUSER_SNAME(0x01); + END; + + /* --TOURSTOP06-- */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 7 ) + BEGIN + /* --TOURSTOP02-- */ + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 7) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 7 AS CheckID , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Stored Procedure Runs at Startup' AS Finding , + 'https://www.brentozar.com/go/startup' AS URL , + ( 'Stored procedure [master].[' + + r.SPECIFIC_SCHEMA + '].[' + + r.SPECIFIC_NAME + + '] runs automatically when SQL Server starts up. Make sure you know exactly what this stored procedure is doing, because it could pose a security risk.' ) AS Details + FROM master.INFORMATION_SCHEMA.ROUTINES r + WHERE OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME), + 'ExecIsStartup') = 1; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 10 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 10) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 10 AS CheckID, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''Resource Governor Enabled'' AS Finding, + ''https://www.brentozar.com/go/rg'' AS URL, + (''Resource Governor is enabled. Queries may be throttled. Make sure you understand how the Classifier Function is configured.'') AS Details FROM sys.resource_governor_configuration WHERE is_enabled = 1 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 11 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 11) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 11 AS CheckID, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''Server Triggers Enabled'' AS Finding, + ''https://www.brentozar.com/go/logontriggers/'' AS URL, + (''Server Trigger ['' + [name] ++ ''] is enabled. Make sure you understand what that trigger is doing - the less work it does, the better.'') AS Details FROM sys.server_triggers WHERE is_disabled = 0 AND is_ms_shipped = 0 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 12 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 12) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 12 AS CheckID , + [name] AS DatabaseName , + 10 AS Priority , + 'Performance' AS FindingsGroup , + 'Auto-Close Enabled' AS Finding , + 'https://www.brentozar.com/go/autoclose' AS URL , + ( 'Database [' + [name] + + '] has auto-close enabled. This setting can dramatically decrease performance.' ) AS Details + FROM sys.databases + WHERE is_auto_close_on = 1 + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 12); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 13 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 13) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 13 AS CheckID , + [name] AS DatabaseName , + 10 AS Priority , + 'Performance' AS FindingsGroup , + 'Auto-Shrink Enabled' AS Finding , + 'https://www.brentozar.com/go/autoshrink' AS URL , + ( 'Database [' + [name] + + '] has auto-shrink enabled. This setting can dramatically decrease performance.' ) AS Details + FROM sys.databases + WHERE is_auto_shrink_on = 1 + AND state <> 6 /* Offline */ + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 13); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 14 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 14) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 14 AS CheckID, + [name] as DatabaseName, + 50 AS Priority, + ''Reliability'' AS FindingsGroup, + ''Page Verification Not Optimal'' AS Finding, + ''https://www.brentozar.com/go/torn'' AS URL, + (''Database ['' + [name] + ''] has '' + [page_verify_option_desc] + '' for page verification. SQL Server may have a harder time recognizing and recovering from storage corruption. Consider using CHECKSUM instead.'') COLLATE database_default AS Details + FROM sys.databases + WHERE page_verify_option < 2 + AND name <> ''tempdb'' + AND state NOT IN (1, 6) /* Restoring, Offline */ + and name not in (select distinct DatabaseName from #SkipChecks WHERE CheckID IS NULL OR CheckID = 14) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 15 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 15) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 15 AS CheckID , + [name] AS DatabaseName , + 110 AS Priority , + 'Performance' AS FindingsGroup , + 'Auto-Create Stats Disabled' AS Finding , + 'https://www.brentozar.com/go/acs' AS URL , + ( 'Database [' + [name] + + '] has auto-create-stats disabled. SQL Server uses statistics to build better execution plans, and without the ability to automatically create more, performance may suffer.' ) AS Details + FROM sys.databases + WHERE is_auto_create_stats_on = 0 + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 15); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 16 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 16) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 16 AS CheckID , + [name] AS DatabaseName , + 110 AS Priority , + 'Performance' AS FindingsGroup , + 'Auto-Update Stats Disabled' AS Finding , + 'https://www.brentozar.com/go/aus' AS URL , + ( 'Database [' + [name] + + '] has auto-update-stats disabled. SQL Server uses statistics to build better execution plans, and without the ability to automatically update them, performance may suffer.' ) AS Details + FROM sys.databases + WHERE is_auto_update_stats_on = 0 + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 16); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 17 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 17) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 17 AS CheckID , + [name] AS DatabaseName , + 150 AS Priority , + 'Performance' AS FindingsGroup , + 'Stats Updated Asynchronously' AS Finding , + 'https://www.brentozar.com/go/asyncstats' AS URL , + ( 'Database [' + [name] + + '] has auto-update-stats-async enabled. When SQL Server gets a query for a table with out-of-date statistics, it will run the query with the stats it has - while updating stats to make later queries better. The initial run of the query may suffer, though.' ) AS Details + FROM sys.databases + WHERE is_auto_update_stats_async_on = 1 + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 17); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 20 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 20) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 20 AS CheckID , + [name] AS DatabaseName , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Date Correlation On' AS Finding , + 'https://www.brentozar.com/go/corr' AS URL , + ( 'Database [' + [name] + + '] has date correlation enabled. This is not a default setting, and it has some performance overhead. It tells SQL Server that date fields in two tables are related, and SQL Server maintains statistics showing that relation.' ) AS Details + FROM sys.databases + WHERE is_date_correlation_on = 1 + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 20); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 21 ) + BEGIN + /* --TOURSTOP04-- */ + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 21) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 21 AS CheckID, + [name] as DatabaseName, + 200 AS Priority, + ''Informational'' AS FindingsGroup, + ''Database Encrypted'' AS Finding, + ''https://www.brentozar.com/go/tde'' AS URL, + (''Database ['' + [name] + ''] has Transparent Data Encryption enabled. Make absolutely sure you have backed up the certificate and private key, or else you will not be able to restore this database.'') AS Details + FROM sys.databases + WHERE is_encrypted = 1 + and name not in (select distinct DatabaseName from #SkipChecks WHERE CheckID IS NULL OR CheckID = 21) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + /* + Believe it or not, SQL Server doesn't track the default values + for sp_configure options! We'll make our own list here. + */ + + IF @Debug IN (1, 2) RAISERROR('Generating default configuration values', 0, 1) WITH NOWAIT; + + INSERT INTO #ConfigurationDefaults + VALUES ( 'access check cache bucket count', 0, 1001 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'access check cache quota', 0, 1002 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'Ad Hoc Distributed Queries', 0, 1003 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'affinity I/O mask', 0, 1004 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'affinity mask', 0, 1005 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'affinity64 mask', 0, 1066 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'affinity64 I/O mask', 0, 1067 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'Agent XPs', 0, 1071 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'allow updates', 0, 1007 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'awe enabled', 0, 1008 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'backup checksum default', 0, 1070 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'backup compression default', 0, 1073 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'blocked process threshold', 0, 1009 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'blocked process threshold (s)', 0, 1009 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'c2 audit mode', 0, 1010 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'clr enabled', 0, 1011 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'common criteria compliance enabled', 0, 1074 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'contained database authentication', 0, 1068 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'cost threshold for parallelism', 5, 1012 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'cross db ownership chaining', 0, 1013 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'cursor threshold', -1, 1014 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'Database Mail XPs', 0, 1072 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'default full-text language', 1033, 1016 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'default language', 0, 1017 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'default trace enabled', 1, 1018 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'disallow results from triggers', 0, 1019 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'EKM provider enabled', 0, 1075 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'filestream access level', 0, 1076 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'fill factor (%)', 0, 1020 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'ft crawl bandwidth (max)', 100, 1021 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'ft crawl bandwidth (min)', 0, 1022 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'ft notify bandwidth (max)', 100, 1023 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'ft notify bandwidth (min)', 0, 1024 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'index create memory (KB)', 0, 1025 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'in-doubt xact resolution', 0, 1026 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'lightweight pooling', 0, 1027 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'locks', 0, 1028 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'max degree of parallelism', 0, 1029 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'max full-text crawl range', 4, 1030 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'max server memory (MB)', 2147483647, 1031 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'max text repl size (B)', 65536, 1032 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'max worker threads', 0, 1033 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'media retention', 0, 1034 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'min memory per query (KB)', 1024, 1035 ); + /* Accepting both 0 and 16 below because both have been seen in the wild as defaults. */ + IF EXISTS ( SELECT * + FROM sys.configurations + WHERE name = 'min server memory (MB)' + AND value_in_use IN ( 0, 16 ) ) + INSERT INTO #ConfigurationDefaults + SELECT 'min server memory (MB)' , + CAST(value_in_use AS BIGINT), 1036 + FROM sys.configurations + WHERE name = 'min server memory (MB)'; + ELSE + INSERT INTO #ConfigurationDefaults + VALUES ( 'min server memory (MB)', 0, 1036 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'nested triggers', 1, 1037 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'network packet size (B)', 4096, 1038 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'Ole Automation Procedures', 0, 1039 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'open objects', 0, 1040 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'optimize for ad hoc workloads', 0, 1041 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'PH timeout (s)', 60, 1042 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'precompute rank', 0, 1043 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'priority boost', 0, 1044 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'query governor cost limit', 0, 1045 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'query wait (s)', -1, 1046 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'recovery interval (min)', 0, 1047 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'remote access', 1, 1048 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'remote admin connections', 0, 1049 ); + /* SQL Server 2012 changes a configuration default */ + IF @@VERSION LIKE '%Microsoft SQL Server 2005%' + OR @@VERSION LIKE '%Microsoft SQL Server 2008%' + BEGIN + INSERT INTO #ConfigurationDefaults + VALUES ( 'remote login timeout (s)', 20, 1069 ); + END; + ELSE + BEGIN + INSERT INTO #ConfigurationDefaults + VALUES ( 'remote login timeout (s)', 10, 1069 ); + END; + INSERT INTO #ConfigurationDefaults + VALUES ( 'remote proc trans', 0, 1050 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'remote query timeout (s)', 600, 1051 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'Replication XPs', 0, 1052 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'RPC parameter data validation', 0, 1053 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'scan for startup procs', 0, 1054 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'server trigger recursion', 1, 1055 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'set working set size', 0, 1056 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'show advanced options', 0, 1057 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'SMO and DMO XPs', 1, 1058 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'SQL Mail XPs', 0, 1059 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'transform noise words', 0, 1060 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'two digit year cutoff', 2049, 1061 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'user connections', 0, 1062 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'user options', 0, 1063 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'Web Assistant Procedures', 0, 1064 ); + INSERT INTO #ConfigurationDefaults + VALUES ( 'xp_cmdshell', 0, 1065 ); + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 22 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 22) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT cd.CheckID , + 200 AS Priority , + 'Non-Default Server Config' AS FindingsGroup , + cr.name AS Finding , + 'https://www.brentozar.com/go/conf' AS URL , + ( 'This sp_configure option has been changed. Its default value is ' + + COALESCE(CAST(cd.[DefaultValue] AS VARCHAR(100)), + '(unknown)') + + ' and it has been set to ' + + CAST(cr.value_in_use AS VARCHAR(100)) + + '.' ) AS Details + FROM sys.configurations cr + INNER JOIN #ConfigurationDefaults cd ON cd.name = cr.name + LEFT OUTER JOIN #ConfigurationDefaults cdUsed ON cdUsed.name = cr.name + AND cdUsed.DefaultValue = cr.value_in_use + WHERE cdUsed.name IS NULL; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 190 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Setting @MinServerMemory and @MaxServerMemory', 0, 1) WITH NOWAIT; + + SELECT @MinServerMemory = CAST(value_in_use as BIGINT) FROM sys.configurations WHERE name = 'min server memory (MB)'; + SELECT @MaxServerMemory = CAST(value_in_use as BIGINT) FROM sys.configurations WHERE name = 'max server memory (MB)'; + + IF (@MinServerMemory = @MaxServerMemory) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 190) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES + ( 190, + 200, + 'Performance', + 'Non-Dynamic Memory', + 'https://www.brentozar.com/go/memory', + 'Minimum Server Memory setting is the same as the Maximum (both set to ' + CAST(@MinServerMemory AS NVARCHAR(50)) + '). This will not allow dynamic memory. Please revise memory settings' + ); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 188 ) + BEGIN + + /* Let's set variables so that our query is still SARGable */ + + IF @Debug IN (1, 2) RAISERROR('Setting @Processors.', 0, 1) WITH NOWAIT; + + SET @Processors = (SELECT cpu_count FROM sys.dm_os_sys_info); + + IF @Debug IN (1, 2) RAISERROR('Setting @NUMANodes', 0, 1) WITH NOWAIT; + + SET @NUMANodes = (SELECT COUNT(1) + FROM sys.dm_os_performance_counters pc + WHERE pc.object_name LIKE '%Buffer Node%' + AND counter_name = 'Page life expectancy'); + /* If Cost Threshold for Parallelism is default then flag as a potential issue */ + /* If MAXDOP is default and processors > 8 or NUMA nodes > 1 then flag as potential issue */ + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 188) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 188 AS CheckID , + 200 AS Priority , + 'Performance' AS FindingsGroup , + cr.name AS Finding , + 'https://www.brentozar.com/go/cxpacket' AS URL , + ( 'Set to ' + CAST(cr.value_in_use AS NVARCHAR(50)) + ', its default value. Changing this sp_configure setting may reduce CXPACKET waits.') + FROM sys.configurations cr + INNER JOIN #ConfigurationDefaults cd ON cd.name = cr.name + AND cr.value_in_use = cd.DefaultValue + WHERE cr.name = 'cost threshold for parallelism' + OR (cr.name = 'max degree of parallelism' AND (@NUMANodes > 1 OR @Processors > 8)); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 24 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 24) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 24 AS CheckID , + DB_NAME(database_id) AS DatabaseName , + 170 AS Priority , + 'File Configuration' AS FindingsGroup , + 'System Database on C Drive' AS Finding , + 'https://www.brentozar.com/go/cdrive' AS URL , + ( 'The ' + DB_NAME(database_id) + + ' database has a file on the C drive. Putting system databases on the C drive runs the risk of crashing the server when it runs out of space.' ) AS Details + FROM sys.master_files + WHERE UPPER(LEFT(physical_name, 1)) = 'C' + AND DB_NAME(database_id) IN ( 'master', + 'model', 'msdb' ); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 25 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 25) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT TOP 1 + 25 AS CheckID , + 'tempdb' , + 20 AS Priority , + 'File Configuration' AS FindingsGroup , + 'TempDB on C Drive' AS Finding , + 'https://www.brentozar.com/go/cdrive' AS URL , + CASE WHEN growth > 0 + THEN ( 'The tempdb database has files on the C drive. TempDB frequently grows unpredictably, putting your server at risk of running out of C drive space and crashing hard. C is also often much slower than other drives, so performance may be suffering.' ) + ELSE ( 'The tempdb database has files on the C drive. TempDB is not set to Autogrow, hopefully it is big enough. C is also often much slower than other drives, so performance may be suffering.' ) + END AS Details + FROM sys.master_files + WHERE UPPER(LEFT(physical_name, 1)) = 'C' + AND DB_NAME(database_id) = 'tempdb'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 26 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 26) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 26 AS CheckID , + DB_NAME(database_id) AS DatabaseName , + 20 AS Priority , + 'Reliability' AS FindingsGroup , + 'User Databases on C Drive' AS Finding , + 'https://www.brentozar.com/go/cdrive' AS URL , + ( 'The ' + DB_NAME(database_id) + + ' database has a file on the C drive. Putting databases on the C drive runs the risk of crashing the server when it runs out of space.' ) AS Details + FROM sys.master_files + WHERE UPPER(LEFT(physical_name, 1)) = 'C' + AND DB_NAME(database_id) NOT IN ( 'master', + 'model', 'msdb', + 'tempdb' ) + AND DB_NAME(database_id) NOT IN ( + SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 26 ); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 27 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 27) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 27 AS CheckID , + 'master' AS DatabaseName , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Tables in the Master Database' AS Finding , + 'https://www.brentozar.com/go/mastuser' AS URL , + ( 'The ' + name + + ' table in the master database was created by end users on ' + + CAST(create_date AS VARCHAR(20)) + + '. Tables in the master database may not be restored in the event of a disaster.' ) AS Details + FROM master.sys.tables + WHERE is_ms_shipped = 0 + AND name NOT IN ('CommandLog','SqlServerVersions','$ndo$srvproperty'); + /* That last one is the Dynamics NAV licensing table: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2426 */ + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 28 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 28) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 28 AS CheckID , + 'msdb' AS DatabaseName , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Tables in the MSDB Database' AS Finding , + 'https://www.brentozar.com/go/msdbuser' AS URL , + ( 'The ' + name + + ' table in the msdb database was created by end users on ' + + CAST(create_date AS VARCHAR(20)) + + '. Tables in the msdb database may not be restored in the event of a disaster.' ) AS Details + FROM msdb.sys.tables + WHERE is_ms_shipped = 0 AND name NOT LIKE '%DTA_%'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 29 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 29) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 29 AS CheckID , + 'model' AS DatabaseName , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Tables in the Model Database' AS Finding , + 'https://www.brentozar.com/go/model' AS URL , + ( 'The ' + name + + ' table in the model database was created by end users on ' + + CAST(create_date AS VARCHAR(20)) + + '. Tables in the model database are automatically copied into all new databases.' ) AS Details + FROM model.sys.tables + WHERE is_ms_shipped = 0; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 30 ) + BEGIN + IF ( SELECT COUNT(*) + FROM msdb.dbo.sysalerts + WHERE severity BETWEEN 19 AND 25 + ) < 7 + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 30) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 30 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'Not All Alerts Configured' AS Finding , + 'https://www.brentozar.com/go/alert' AS URL , + ( 'Not all SQL Server Agent alerts have been configured. This is a free, easy way to get notified of corruption, job failures, or major outages even before monitoring systems pick it up.' ) AS Details; + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 59 ) + BEGIN + IF EXISTS ( SELECT * + FROM msdb.dbo.sysalerts + WHERE enabled = 1 + AND COALESCE(has_notification, 0) = 0 + AND (job_id IS NULL OR job_id = 0x)) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 59) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 59 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'Alerts Configured without Follow Up' AS Finding , + 'https://www.brentozar.com/go/alert' AS URL , + ( 'SQL Server Agent alerts have been configured but they either do not notify anyone or else they do not take any action. This is a free, easy way to get notified of corruption, job failures, or major outages even before monitoring systems pick it up.' ) AS Details; + + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 96 ) + BEGIN + IF NOT EXISTS ( SELECT * + FROM msdb.dbo.sysalerts + WHERE message_id IN ( 823, 824, 825 ) ) + + BEGIN; + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 96) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 96 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'No Alerts for Corruption' AS Finding , + 'https://www.brentozar.com/go/alert' AS URL , + ( 'SQL Server Agent alerts do not exist for errors 823, 824, and 825. These three errors can give you notification about early hardware failure. Enabling them can prevent you a lot of heartbreak.' ) AS Details; + + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 61 ) + BEGIN + IF NOT EXISTS ( SELECT * + FROM msdb.dbo.sysalerts + WHERE severity BETWEEN 19 AND 25 ) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 61) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 61 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'No Alerts for Sev 19-25' AS Finding , + 'https://www.brentozar.com/go/alert' AS URL , + ( 'SQL Server Agent alerts do not exist for severity levels 19 through 25. These are some very severe SQL Server errors. Knowing that these are happening may let you recover from errors faster.' ) AS Details; + + END; + + END; + + --check for disabled alerts + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 98 ) + BEGIN + IF EXISTS ( SELECT name + FROM msdb.dbo.sysalerts + WHERE enabled = 0 ) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 98) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 98 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'Alerts Disabled' AS Finding , + 'https://www.brentozar.com/go/alert' AS URL , + ( 'The following Alert is disabled, please review and enable if desired: ' + + name ) AS Details + FROM msdb.dbo.sysalerts + WHERE enabled = 0; + END; + END; + + --check for alerts that do NOT include event descriptions in their outputs via email/pager/net-send + IF NOT EXISTS ( + SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL + AND CheckID = 219 + ) + BEGIN; + IF @Debug IN (1, 2) + BEGIN; + RAISERROR ('Running CheckId [%d].', 0, 1, 219) WITH NOWAIT; + END; + + INSERT INTO #BlitzResults ( + CheckID + ,[Priority] + ,FindingsGroup + ,Finding + ,[URL] + ,Details + ) + SELECT 219 AS CheckID + ,200 AS [Priority] + ,'Monitoring' AS FindingsGroup + ,'Alerts Without Event Descriptions' AS Finding + ,'https://www.brentozar.com/go/alert' AS [URL] + ,('The following Alert is not including detailed event descriptions in its output messages: ' + QUOTENAME([name]) + + '. You can fix it by ticking the relevant boxes in its Properties --> Options page.') AS Details + FROM msdb.dbo.sysalerts + WHERE [enabled] = 1 + AND include_event_description = 0 --bitmask: 1 = email, 2 = pager, 4 = net send + ; + END; + + --check whether we have NO ENABLED operators! + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 31 ) + BEGIN; + IF NOT EXISTS ( SELECT * + FROM msdb.dbo.sysoperators + WHERE enabled = 1 ) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 31) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 31 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'No Operators Configured/Enabled' AS Finding , + 'https://www.brentozar.com/go/op' AS URL , + ( 'No SQL Server Agent operators (emails) have been configured. This is a free, easy way to get notified of corruption, job failures, or major outages even before monitoring systems pick it up.' ) AS Details; + + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 34 ) + BEGIN + IF EXISTS ( SELECT * + FROM sys.all_objects + WHERE name = 'dm_db_mirroring_auto_page_repair' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 34) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT + 34 AS CheckID , + db.name , + 1 AS Priority , + ''Corruption'' AS FindingsGroup , + ''Database Corruption Detected'' AS Finding , + ''https://www.brentozar.com/go/repair'' AS URL , + ( ''Database mirroring has automatically repaired at least one corrupt page in the last 30 days. For more information, query the DMV sys.dm_db_mirroring_auto_page_repair.'' ) AS Details + FROM (SELECT rp2.database_id, rp2.modification_time + FROM sys.dm_db_mirroring_auto_page_repair rp2 + WHERE rp2.[database_id] not in ( + SELECT db2.[database_id] + FROM sys.databases as db2 + WHERE db2.[state] = 1 + ) ) as rp + INNER JOIN master.sys.databases db ON rp.database_id = db.database_id + WHERE rp.modification_time >= DATEADD(dd, -30, GETDATE()) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 89 ) + BEGIN + IF EXISTS ( SELECT * + FROM sys.all_objects + WHERE name = 'dm_hadr_auto_page_repair' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 89) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT + 89 AS CheckID , + db.name , + 1 AS Priority , + ''Corruption'' AS FindingsGroup , + ''Database Corruption Detected'' AS Finding , + ''https://www.brentozar.com/go/repair'' AS URL , + ( ''Availability Groups has automatically repaired at least one corrupt page in the last 30 days. For more information, query the DMV sys.dm_hadr_auto_page_repair.'' ) AS Details + FROM sys.dm_hadr_auto_page_repair rp + INNER JOIN master.sys.databases db ON rp.database_id = db.database_id + WHERE rp.modification_time >= DATEADD(dd, -30, GETDATE()) OPTION (RECOMPILE) ;'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 90 ) + BEGIN + IF EXISTS ( SELECT * + FROM msdb.sys.all_objects + WHERE name = 'suspect_pages' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 90) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT + 90 AS CheckID , + db.name , + 1 AS Priority , + ''Corruption'' AS FindingsGroup , + ''Database Corruption Detected'' AS Finding , + ''https://www.brentozar.com/go/repair'' AS URL , + ( ''SQL Server has detected at least one corrupt page in the last 30 days. For more information, query the system table msdb.dbo.suspect_pages.'' ) AS Details + FROM msdb.dbo.suspect_pages sp + INNER JOIN master.sys.databases db ON sp.database_id = db.database_id + WHERE sp.last_update_date >= DATEADD(dd, -30, GETDATE()) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 36 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 36) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 36 AS CheckID , + 150 AS Priority , + 'Performance' AS FindingsGroup , + 'Slow Storage Reads on Drive ' + + UPPER(LEFT(mf.physical_name, 1)) AS Finding , + 'https://www.brentozar.com/go/slow' AS URL , + 'Reads are averaging longer than 200ms for at least one database on this drive. For specific database file speeds, run the query from the information link.' AS Details + FROM sys.dm_io_virtual_file_stats(NULL, NULL) + AS fs + INNER JOIN sys.master_files AS mf ON fs.database_id = mf.database_id + AND fs.[file_id] = mf.[file_id] + WHERE ( io_stall_read_ms / ( 1.0 + num_of_reads ) ) > 200 + AND num_of_reads > 100000; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 37 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 37) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 37 AS CheckID , + 150 AS Priority , + 'Performance' AS FindingsGroup , + 'Slow Storage Writes on Drive ' + + UPPER(LEFT(mf.physical_name, 1)) AS Finding , + 'https://www.brentozar.com/go/slow' AS URL , + 'Writes are averaging longer than 100ms for at least one database on this drive. For specific database file speeds, run the query from the information link.' AS Details + FROM sys.dm_io_virtual_file_stats(NULL, NULL) + AS fs + INNER JOIN sys.master_files AS mf ON fs.database_id = mf.database_id + AND fs.[file_id] = mf.[file_id] + WHERE ( io_stall_write_ms / ( 1.0 + + num_of_writes ) ) > 100 + AND num_of_writes > 100000; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 40 ) + BEGIN + IF ( SELECT COUNT(*) + FROM tempdb.sys.database_files + WHERE type_desc = 'ROWS' + ) = 1 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 40) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES ( 40 , + 'tempdb' , + 170 , + 'File Configuration' , + 'TempDB Only Has 1 Data File' , + 'https://www.brentozar.com/go/tempdb' , + 'TempDB is only configured with one data file. More data files are usually required to alleviate SGAM contention.' + ); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 183 ) + + BEGIN + + IF ( SELECT COUNT (distinct [size]) + FROM tempdb.sys.database_files + WHERE type_desc = 'ROWS' + HAVING MAX((size * 8) / (1024. * 1024)) - MIN((size * 8) / (1024. * 1024)) > 1. + ) <> 1 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 183) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES ( 183 , + 'tempdb' , + 170 , + 'File Configuration' , + 'TempDB Unevenly Sized Data Files' , + 'https://www.brentozar.com/go/tempdb' , + 'TempDB data files are not configured with the same size. Unevenly sized tempdb data files will result in unevenly sized workloads.' + ); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 44 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 44) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 44 AS CheckID , + 150 AS Priority , + 'Performance' AS FindingsGroup , + 'Queries Forcing Order Hints' AS Finding , + 'https://www.brentozar.com/go/hints' AS URL , + CAST(occurrence AS VARCHAR(10)) + + ' instances of order hinting have been recorded since restart. This means queries are bossing the SQL Server optimizer around, and if they don''t know what they''re doing, this can cause more harm than good. This can also explain why DBA tuning efforts aren''t working.' AS Details + FROM sys.dm_exec_query_optimizer_info + WHERE counter = 'order hint' + AND occurrence > 1000; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 45 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 45) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 45 AS CheckID , + 150 AS Priority , + 'Performance' AS FindingsGroup , + 'Queries Forcing Join Hints' AS Finding , + 'https://www.brentozar.com/go/hints' AS URL , + CAST(occurrence AS VARCHAR(10)) + + ' instances of join hinting have been recorded since restart. This means queries are bossing the SQL Server optimizer around, and if they don''t know what they''re doing, this can cause more harm than good. This can also explain why DBA tuning efforts aren''t working.' AS Details + FROM sys.dm_exec_query_optimizer_info + WHERE counter = 'join hint' + AND occurrence > 1000; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 49 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 49) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 49 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Linked Server Configured' AS Finding , + 'https://www.brentozar.com/go/link' AS URL , + +CASE WHEN l.remote_name = 'sa' + THEN COALESCE(s.data_source, s.provider) + + ' is configured as a linked server. Check its security configuration as it is connecting with sa, because any user who queries it will get admin-level permissions.' + ELSE COALESCE(s.data_source, s.provider) + + ' is configured as a linked server. Check its security configuration to make sure it isn''t connecting with SA or some other bone-headed administrative login, because any user who queries it might get admin-level permissions.' + END AS Details + FROM sys.servers s + INNER JOIN sys.linked_logins l ON s.server_id = l.server_id + WHERE s.is_linked = 1; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 50 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 50) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 50 AS CheckID , + 100 AS Priority , + ''Performance'' AS FindingsGroup , + ''Max Memory Set Too High'' AS Finding , + ''https://www.brentozar.com/go/max'' AS URL , + ''SQL Server max memory is set to '' + + CAST(c.value_in_use AS VARCHAR(20)) + + '' megabytes, but the server only has '' + + CAST(( CAST(m.total_physical_memory_kb AS BIGINT) / 1024 ) AS VARCHAR(20)) + + '' megabytes. SQL Server may drain the system dry of memory, and under certain conditions, this can cause Windows to swap to disk.'' AS Details + FROM sys.dm_os_sys_memory m + INNER JOIN sys.configurations c ON c.name = ''max server memory (MB)'' + WHERE CAST(m.total_physical_memory_kb AS BIGINT) < ( CAST(c.value_in_use AS BIGINT) * 1024 ) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 51 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 51) WITH NOWAIT + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 51 AS CheckID , + 1 AS Priority , + ''Performance'' AS FindingsGroup , + ''Memory Dangerously Low'' AS Finding , + ''https://www.brentozar.com/go/max'' AS URL , + ''The server has '' + CAST(( CAST(m.total_physical_memory_kb AS BIGINT) / 1024 ) AS VARCHAR(20)) + '' megabytes of physical memory, but only '' + CAST(( CAST(m.available_physical_memory_kb AS BIGINT) / 1024 ) AS VARCHAR(20)) + + '' megabytes are available. As the server runs out of memory, there is danger of swapping to disk, which will kill performance.'' AS Details + FROM sys.dm_os_sys_memory m + WHERE CAST(m.available_physical_memory_kb AS BIGINT) < 262144 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 159 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 159) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT DISTINCT 159 AS CheckID , + 1 AS Priority , + ''Performance'' AS FindingsGroup , + ''Memory Dangerously Low in NUMA Nodes'' AS Finding , + ''https://www.brentozar.com/go/max'' AS URL , + ''At least one NUMA node is reporting THREAD_RESOURCES_LOW in sys.dm_os_nodes and can no longer create threads.'' AS Details + FROM sys.dm_os_nodes m + WHERE node_state_desc LIKE ''%THREAD_RESOURCES_LOW%'' OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 53 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 53) WITH NOWAIT; + + --INSERT INTO #BlitzResults + -- ( CheckID , + -- Priority , + -- FindingsGroup , + -- Finding , + -- URL , + -- Details + -- ) + -- SELECT TOP 1 + -- 53 AS CheckID , + -- 200 AS Priority , + -- 'Informational' AS FindingsGroup , + -- 'Cluster Node' AS Finding , + -- 'https://BrentOzar.com/go/node' AS URL , + -- 'This is a node in a cluster.' AS Details + -- FROM sys.dm_os_cluster_nodes; + + DECLARE @AOFCI AS INT, @AOAG AS INT, @HAType AS VARCHAR(10), @errmsg AS VARCHAR(200) + + SELECT @AOAG = CAST(SERVERPROPERTY('IsHadrEnabled') AS INT) + SELECT @AOFCI = CAST(SERVERPROPERTY('IsClustered') AS INT) + + IF (SELECT COUNT(DISTINCT join_state_desc) FROM sys.dm_hadr_availability_replica_cluster_states) = 2 + BEGIN --if count is 2 both JOINED_STANDALONE and JOINED_FCI is configured + SET @AOFCI = 1 + END + + SELECT @HAType = + CASE + WHEN @AOFCI = 1 AND @AOAG =1 THEN 'FCIAG' + WHEN @AOFCI = 1 AND @AOAG =0 THEN 'FCI' + WHEN @AOFCI = 0 AND @AOAG =1 THEN 'AG' + ELSE 'STANDALONE' + END + + IF (@HAType IN ('FCIAG','FCI','AG')) + BEGIN + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + + SELECT 53 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Cluster Node' AS Finding , + 'https://BrentOzar.com/go/node' AS URL , + 'This is a node in a cluster.' AS Details + + IF @HAType = 'AG' + BEGIN + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 53 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Cluster Node Info' AS Finding , + 'https://BrentOzar.com/go/node' AS URL, + 'The cluster nodes are: ' + STUFF((SELECT ', ' + CASE ar.replica_server_name WHEN dhags.primary_replica THEN 'PRIMARY' + ELSE 'SECONDARY' + END + '=' + UPPER(ar.replica_server_name) + FROM sys.availability_groups AS ag + LEFT OUTER JOIN sys.availability_replicas AS ar ON ag.group_id = ar.group_id + LEFT OUTER JOIN sys.dm_hadr_availability_group_states as dhags ON ag.group_id = dhags.group_id + ORDER BY 1 + FOR XML PATH ('') + ), 1, 1, '') + ' - High Availability solution used is AlwaysOn Availability Group (AG)' As Details + END + + IF @HAType = 'FCI' + BEGIN + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 53 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Cluster Node Info' AS Finding , + 'https://BrentOzar.com/go/node' AS URL, + 'The cluster nodes are: ' + STUFF((SELECT ', ' + CASE is_current_owner + WHEN 1 THEN 'PRIMARY' + ELSE 'SECONDARY' + END + '=' + UPPER(NodeName) + FROM sys.dm_os_cluster_nodes + ORDER BY 1 + FOR XML PATH ('') + ), 1, 1, '') + ' - High Availability solution used is AlwaysOn Failover Cluster Instance (FCI)' As Details + END + + IF @HAType = 'FCIAG' + BEGIN + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 53 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Cluster Node Info' AS Finding , + 'https://BrentOzar.com/go/node' AS URL, + 'The cluster nodes are: ' + STUFF((SELECT ', ' + HighAvailabilityRoleDesc + '=' + ServerName + FROM (SELECT UPPER(ar.replica_server_name) AS ServerName + ,CASE ar.replica_server_name WHEN dhags.primary_replica THEN 'PRIMARY' + ELSE 'SECONDARY' + END AS HighAvailabilityRoleDesc + FROM sys.availability_groups AS ag + LEFT OUTER JOIN sys.availability_replicas AS ar ON ag.group_id = ar.group_id + LEFT OUTER JOIN sys.dm_hadr_availability_group_states as dhags ON ag.group_id = dhags.group_id + WHERE UPPER(CAST(SERVERPROPERTY('ServerName') AS VARCHAR)) <> ar.replica_server_name + UNION ALL + SELECT UPPER(NodeName) AS ServerName + ,CASE is_current_owner + WHEN 1 THEN 'PRIMARY' + ELSE 'SECONDARY' + END AS HighAvailabilityRoleDesc + FROM sys.dm_os_cluster_nodes) AS Z + ORDER BY 1 + FOR XML PATH ('') + ), 1, 1, '') + ' - High Availability solution used is AlwaysOn FCI with AG (Failover Cluster Instance with Availability Group).'As Details + END + END + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 55 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 55) WITH NOWAIT; + + IF @UsualDBOwner IS NULL + SET @UsualDBOwner = SUSER_SNAME(0x01); + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 55 AS CheckID , + [name] AS DatabaseName , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Database Owner <> ' + @UsualDBOwner AS Finding , + 'https://www.brentozar.com/go/owndb' AS URL , + ( 'Database name: ' + [name] + ' ' + + 'Owner name: ' + SUSER_SNAME(owner_sid) ) AS Details + FROM sys.databases + WHERE (((SUSER_SNAME(owner_sid) <> SUSER_SNAME(0x01)) AND (name IN (N'master', N'model', N'msdb', N'tempdb'))) + OR ((SUSER_SNAME(owner_sid) <> @UsualDBOwner) AND (name NOT IN (N'master', N'model', N'msdb', N'tempdb'))) + ) + AND name NOT IN ( SELECT DISTINCT DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 55); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 213 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 213) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 213 AS CheckID , + [name] AS DatabaseName , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'Database Owner is Unknown' AS Finding , + '' AS URL , + ( 'Database name: ' + [name] + ' ' + + 'Owner name: ' + ISNULL(SUSER_SNAME(owner_sid),'~~ UNKNOWN ~~') ) AS Details + FROM sys.databases + WHERE SUSER_SNAME(owner_sid) is NULL + AND name NOT IN ( SELECT DISTINCT DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 213); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 57 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 57) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 57 AS CheckID , + 230 AS Priority , + 'Security' AS FindingsGroup , + 'SQL Agent Job Runs at Startup' AS Finding , + 'https://www.brentozar.com/go/startup' AS URL , + ( 'Job [' + j.name + + '] runs automatically when SQL Server Agent starts up. Make sure you know exactly what this job is doing, because it could pose a security risk.' ) AS Details + FROM msdb.dbo.sysschedules sched + JOIN msdb.dbo.sysjobschedules jsched ON sched.schedule_id = jsched.schedule_id + JOIN msdb.dbo.sysjobs j ON jsched.job_id = j.job_id + WHERE sched.freq_type = 64 + AND sched.enabled = 1; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 97 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 97) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 97 AS CheckID , + 100 AS Priority , + 'Performance' AS FindingsGroup , + 'Unusual SQL Server Edition' AS Finding , + 'https://www.brentozar.com/go/workgroup' AS URL , + ( 'This server is using ' + + CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) + + ', which is capped at low amounts of CPU and memory.' ) AS Details + WHERE CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Standard%' + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Enterprise%' + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Data Center%' + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Developer%' + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Business Intelligence%'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 154 ) + AND SERVERPROPERTY('EngineEdition') <> 8 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 154) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 154 AS CheckID , + 10 AS Priority , + 'Performance' AS FindingsGroup , + '32-bit SQL Server Installed' AS Finding , + 'https://www.brentozar.com/go/32bit' AS URL , + ( 'This server uses the 32-bit x86 binaries for SQL Server instead of the 64-bit x64 binaries. The amount of memory available for query workspace and execution plans is heavily limited.' ) AS Details + WHERE CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%64%'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 62 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 62) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 62 AS CheckID , + [name] AS DatabaseName , + 200 AS Priority , + 'Performance' AS FindingsGroup , + 'Old Compatibility Level' AS Finding , + 'https://www.brentozar.com/go/compatlevel' AS URL , + ( 'Database ' + [name] + + ' is compatibility level ' + + CAST(compatibility_level AS VARCHAR(20)) + + ', which may cause unwanted results when trying to run queries that have newer T-SQL features.' ) AS Details + FROM sys.databases + WHERE name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 62) + AND compatibility_level <= 90; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 94 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 94) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 94 AS CheckID , + 200 AS [Priority] , + 'Monitoring' AS FindingsGroup , + 'Agent Jobs Without Failure Emails' AS Finding , + 'https://www.brentozar.com/go/alerts' AS URL , + 'The job ' + [name] + + ' has not been set up to notify an operator if it fails.' AS Details + FROM msdb.[dbo].[sysjobs] j + WHERE j.enabled = 1 + AND j.notify_email_operator_id = 0 + AND j.notify_netsend_operator_id = 0 + AND j.notify_page_operator_id = 0 + AND j.category_id <> 100; /* Exclude SSRS category */ + END; + + IF EXISTS ( SELECT 1 + FROM sys.configurations + WHERE name = 'remote admin connections' + AND value_in_use = 0 ) + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 100 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 100) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 100 AS CheckID , + 170 AS Priority , + 'Reliability' AS FindingGroup , + 'Remote DAC Disabled' AS Finding , + 'https://www.brentozar.com/go/dac' AS URL , + 'Remote access to the Dedicated Admin Connection (DAC) is not enabled. The DAC can make remote troubleshooting much easier when SQL Server is unresponsive.'; + END; + + IF EXISTS ( SELECT * + FROM sys.dm_os_schedulers + WHERE is_online = 0 ) + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 101 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 101) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 101 AS CheckID , + 50 AS Priority , + 'Performance' AS FindingGroup , + 'CPU Schedulers Offline' AS Finding , + 'https://www.brentozar.com/go/schedulers' AS URL , + 'Some CPU cores are not accessible to SQL Server due to affinity masking or licensing problems.'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 110 ) + AND EXISTS (SELECT * FROM master.sys.all_objects WHERE name = 'dm_os_memory_nodes') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 110) WITH NOWAIT; + + SET @StringToExecute = 'IF EXISTS (SELECT * + FROM sys.dm_os_nodes n + INNER JOIN sys.dm_os_memory_nodes m ON n.memory_node_id = m.memory_node_id + WHERE n.node_state_desc = ''OFFLINE'') + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 110 AS CheckID , + 50 AS Priority , + ''Performance'' AS FindingGroup , + ''Memory Nodes Offline'' AS Finding , + ''https://www.brentozar.com/go/schedulers'' AS URL , + ''Due to affinity masking or licensing problems, some of the memory may not be available.'' OPTION (RECOMPILE)'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF EXISTS ( SELECT * + FROM sys.databases + WHERE state > 1 ) + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 102 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 102) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 102 AS CheckID , + [name] , + 20 AS Priority , + 'Reliability' AS FindingGroup , + 'Unusual Database State: ' + [state_desc] AS Finding , + 'https://www.brentozar.com/go/repair' AS URL , + 'This database may not be online.' + FROM sys.databases + WHERE state > 1; + END; + + IF EXISTS ( SELECT * + FROM master.sys.extended_procedures ) + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 105 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 105) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 105 AS CheckID , + 'master' , + 200 AS Priority , + 'Reliability' AS FindingGroup , + 'Extended Stored Procedures in Master' AS Finding , + 'https://www.brentozar.com/go/clr' AS URL , + 'The [' + name + + '] extended stored procedure is in the master database. CLR may be in use, and the master database now needs to be part of your backup/recovery planning.' + FROM master.sys.extended_procedures; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 107 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 107) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 107 AS CheckID , + 50 AS Priority , + 'Performance' AS FindingGroup , + 'Poison Wait Detected: ' + wait_type AS Finding , + 'https://www.brentozar.com/go/poison/#' + wait_type AS URL , + CONVERT(VARCHAR(10), (SUM([wait_time_ms]) / 1000) / 86400) + ':' + CONVERT(VARCHAR(20), DATEADD(s, (SUM([wait_time_ms]) / 1000), 0), 108) + ' of this wait have been recorded. This wait often indicates killer performance problems.' + FROM sys.[dm_os_wait_stats] + WHERE wait_type IN('IO_QUEUE_LIMIT', 'IO_RETRY', 'LOG_RATE_GOVERNOR', 'POOL_LOG_RATE_GOVERNOR', 'PREEMPTIVE_DEBUG', 'RESMGR_THROTTLED', 'RESOURCE_SEMAPHORE', 'RESOURCE_SEMAPHORE_QUERY_COMPILE','SE_REPL_CATCHUP_THROTTLE','SE_REPL_COMMIT_ACK','SE_REPL_COMMIT_TURN','SE_REPL_ROLLBACK_ACK','SE_REPL_SLOW_SECONDARY_THROTTLE','THREADPOOL') + GROUP BY wait_type + HAVING SUM([wait_time_ms]) > (SELECT 5000 * datediff(HH,create_date,CURRENT_TIMESTAMP) AS hours_since_startup FROM sys.databases WHERE name='tempdb') + AND SUM([wait_time_ms]) > 60000; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 121 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 121) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 121 AS CheckID , + 50 AS Priority , + 'Performance' AS FindingGroup , + 'Poison Wait Detected: Serializable Locking' AS Finding , + 'https://www.brentozar.com/go/serializable' AS URL , + CONVERT(VARCHAR(10), (SUM([wait_time_ms]) / 1000) / 86400) + ':' + CONVERT(VARCHAR(20), DATEADD(s, (SUM([wait_time_ms]) / 1000), 0), 108) + ' of LCK_M_R% waits have been recorded. This wait often indicates killer performance problems.' + FROM sys.[dm_os_wait_stats] + WHERE wait_type IN ('LCK_M_RS_S', 'LCK_M_RS_U', 'LCK_M_RIn_NL','LCK_M_RIn_S', 'LCK_M_RIn_U','LCK_M_RIn_X', 'LCK_M_RX_S', 'LCK_M_RX_U','LCK_M_RX_X') + HAVING SUM([wait_time_ms]) > (SELECT 5000 * datediff(HH,create_date,CURRENT_TIMESTAMP) AS hours_since_startup FROM sys.databases WHERE name='tempdb') + AND SUM([wait_time_ms]) > 60000; + END; + + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 111 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 111) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + DatabaseName , + URL , + Details + ) + SELECT 111 AS CheckID , + 50 AS Priority , + 'Reliability' AS FindingGroup , + 'Possibly Broken Log Shipping' AS Finding , + d.[name] , + 'https://www.brentozar.com/go/shipping' AS URL , + d.[name] + ' is in a restoring state, but has not had a backup applied in the last two days. This is a possible indication of a broken transaction log shipping setup.' + FROM [master].sys.databases d + INNER JOIN [master].sys.database_mirroring dm ON d.database_id = dm.database_id + AND dm.mirroring_role IS NULL + WHERE ( d.[state] = 1 + OR (d.[state] = 0 AND d.[is_in_standby] = 1) ) + AND NOT EXISTS(SELECT * FROM msdb.dbo.restorehistory rh + INNER JOIN msdb.dbo.backupset bs ON rh.backup_set_id = bs.backup_set_id + WHERE d.[name] COLLATE SQL_Latin1_General_CP1_CI_AS = rh.destination_database_name COLLATE SQL_Latin1_General_CP1_CI_AS + AND rh.restore_date >= DATEADD(dd, -2, GETDATE())); + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 112 ) + AND EXISTS (SELECT * FROM master.sys.all_objects WHERE name = 'change_tracking_databases') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 112) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + DatabaseName, + URL, + Details) + SELECT 112 AS CheckID, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''Change Tracking Enabled'' AS Finding, + d.[name], + ''https://www.brentozar.com/go/tracking'' AS URL, + ( d.[name] + '' has change tracking enabled. This is not a default setting, and it has some performance overhead. It keeps track of changes to rows in tables that have change tracking turned on.'' ) AS Details FROM sys.change_tracking_databases AS ctd INNER JOIN sys.databases AS d ON ctd.database_id = d.database_id OPTION (RECOMPILE)'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 116 ) + AND EXISTS (SELECT * FROM msdb.sys.all_columns WHERE name = 'compressed_backup_size') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 116) WITH NOWAIT + + SET @StringToExecute = 'INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 116 AS CheckID , + 200 AS Priority , + ''Informational'' AS FindingGroup , + ''Backup Compression Default Off'' AS Finding , + ''https://www.brentozar.com/go/backup'' AS URL , + ''Uncompressed full backups have happened recently, and backup compression is not turned on at the server level. Backup compression is included with SQL Server 2008R2 & newer, even in Standard Edition. We recommend turning backup compression on by default so that ad-hoc backups will get compressed.'' + FROM sys.configurations + WHERE configuration_id = 1579 AND CAST(value_in_use AS INT) = 0 + AND EXISTS (SELECT * FROM msdb.dbo.backupset WHERE backup_size = compressed_backup_size AND type = ''D'' AND backup_finish_date >= DATEADD(DD, -14, GETDATE())) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 117 ) + AND EXISTS (SELECT * FROM master.sys.all_objects WHERE name = 'dm_exec_query_resource_semaphores') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 117) WITH NOWAIT; + + SET @StringToExecute = 'IF 0 < (SELECT SUM([forced_grant_count]) FROM sys.dm_exec_query_resource_semaphores WHERE [forced_grant_count] IS NOT NULL) + INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 117 AS CheckID, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''Memory Pressure Affecting Queries'' AS Finding, + ''https://www.brentozar.com/go/grants'' AS URL, + CAST(SUM(forced_grant_count) AS NVARCHAR(100)) + '' forced grants reported in the DMV sys.dm_exec_query_resource_semaphores, indicating memory pressure has affected query runtimes.'' + FROM sys.dm_exec_query_resource_semaphores WHERE [forced_grant_count] IS NOT NULL OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 124 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 124) WITH NOWAIT; + + INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 124, + 150, + 'Performance', + 'Deadlocks Happening Daily', + 'https://www.brentozar.com/go/deadlocks', + CAST(CAST(p.cntr_value / @DaysUptime AS BIGINT) AS NVARCHAR(100)) + ' average deadlocks per day. To find them, run sp_BlitzLock.' AS Details + FROM sys.dm_os_performance_counters p + INNER JOIN sys.databases d ON d.name = 'tempdb' + WHERE RTRIM(p.counter_name) = 'Number of Deadlocks/sec' + AND RTRIM(p.instance_name) = '_Total' + AND p.cntr_value > 0 + AND (1.0 * p.cntr_value / NULLIF(datediff(DD,create_date,CURRENT_TIMESTAMP),0)) > 10; + END; + + IF DATEADD(mi, -15, GETDATE()) < (SELECT TOP 1 creation_time FROM sys.dm_exec_query_stats ORDER BY creation_time) + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 125 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 125) WITH NOWAIT; + + DECLARE @user_perm_sql NVARCHAR(MAX) = N''; + DECLARE @user_perm_gb_out DECIMAL(38,2); + + IF @ProductVersionMajor >= 11 + + BEGIN + + SET @user_perm_sql += N' + SELECT @user_perm_gb = CASE WHEN (pages_kb / 1024. / 1024.) >= 2. + THEN CONVERT(DECIMAL(38, 2), (pages_kb / 1024. / 1024.)) + ELSE NULL + END + FROM sys.dm_os_memory_clerks + WHERE type = ''USERSTORE_TOKENPERM'' + AND name = ''TokenAndPermUserStore'' + '; + + END + + IF @ProductVersionMajor < 11 + + BEGIN + SET @user_perm_sql += N' + SELECT @user_perm_gb = CASE WHEN ((single_pages_kb + multi_pages_kb) / 1024.0 / 1024.) >= 2. + THEN CONVERT(DECIMAL(38, 2), ((single_pages_kb + multi_pages_kb) / 1024.0 / 1024.)) + ELSE NULL + END + FROM sys.dm_os_memory_clerks + WHERE type = ''USERSTORE_TOKENPERM'' + AND name = ''TokenAndPermUserStore'' + '; + + END + + EXEC sys.sp_executesql @user_perm_sql, + N'@user_perm_gb DECIMAL(38,2) OUTPUT', + @user_perm_gb = @user_perm_gb_out OUTPUT + + INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT TOP 1 125, 10, 'Performance', 'Plan Cache Erased Recently', 'https://www.brentozar.com/askbrent/plan-cache-erased-recently/', + 'The oldest query in the plan cache was created at ' + CAST(creation_time AS NVARCHAR(50)) + + CASE WHEN @user_perm_gb_out IS NULL + THEN '. Someone ran DBCC FREEPROCCACHE, restarted SQL Server, or it is under horrific memory pressure.' + ELSE '. You also have ' + CONVERT(NVARCHAR(20), @user_perm_gb_out) + ' GB of USERSTORE_TOKENPERM, which could indicate unusual memory consumption.' + END + FROM sys.dm_exec_query_stats WITH (NOLOCK) + ORDER BY creation_time; + END; + + IF EXISTS (SELECT * FROM sys.configurations WHERE name = 'priority boost' AND (value = 1 OR value_in_use = 1)) + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 126 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 126) WITH NOWAIT; + + INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + VALUES(126, 5, 'Reliability', 'Priority Boost Enabled', 'https://www.brentozar.com/go/priorityboost/', + 'Priority Boost sounds awesome, but it can actually cause your SQL Server to crash.'); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 128 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + + IF (@ProductVersionMajor = 15 AND @ProductVersionMinor < 2000) OR + (@ProductVersionMajor = 14 AND @ProductVersionMinor < 1000) OR + (@ProductVersionMajor = 13 AND @ProductVersionMinor < 6300) OR + (@ProductVersionMajor = 12 AND @ProductVersionMinor < 6024) OR + (@ProductVersionMajor = 11 /*AND @ProductVersionMinor < 7001)*/) OR + (@ProductVersionMajor = 10.5 /*AND @ProductVersionMinor < 6000*/) OR + (@ProductVersionMajor = 10 /*AND @ProductVersionMinor < 6000*/) OR + (@ProductVersionMajor = 9 /*AND @ProductVersionMinor <= 5000*/) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 128) WITH NOWAIT; + + INSERT INTO #BlitzResults(CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES(128, 20, 'Reliability', 'Unsupported Build of SQL Server', 'https://www.brentozar.com/go/unsupported', + 'Version ' + CAST(@ProductVersionMajor AS VARCHAR(100)) + + CASE WHEN @ProductVersionMajor >= 12 THEN + '.' + CAST(@ProductVersionMinor AS VARCHAR(100)) + ' is no longer supported by Microsoft. You need to apply a service pack.' + ELSE ' is no longer supported by Microsoft. You should be making plans to upgrade to a modern version of SQL Server.' END); + END; + + END; + + /* Reliability - Dangerous Build of SQL Server (Corruption) */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 129 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + IF (@ProductVersionMajor = 11 AND @ProductVersionMinor >= 3000 AND @ProductVersionMinor <= 3436) OR + (@ProductVersionMajor = 11 AND @ProductVersionMinor = 5058) OR + (@ProductVersionMajor = 12 AND @ProductVersionMinor >= 2000 AND @ProductVersionMinor <= 2342) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 129) WITH NOWAIT; + + INSERT INTO #BlitzResults(CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES(129, 20, 'Reliability', 'Dangerous Build of SQL Server (Corruption)', 'http://sqlperformance.com/2014/06/sql-indexes/hotfix-sql-2012-rebuilds', + 'There are dangerous known bugs with version ' + CAST(@ProductVersionMajor AS VARCHAR(100)) + '.' + CAST(@ProductVersionMinor AS VARCHAR(100)) + '. Check the URL for details and apply the right service pack or hotfix.'); + END; + + END; + + /* Reliability - Dangerous Build of SQL Server (Security) */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 157 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + IF (@ProductVersionMajor = 10 AND @ProductVersionMinor >= 5500 AND @ProductVersionMinor <= 5512) OR + (@ProductVersionMajor = 10 AND @ProductVersionMinor >= 5750 AND @ProductVersionMinor <= 5867) OR + (@ProductVersionMajor = 10.5 AND @ProductVersionMinor >= 4000 AND @ProductVersionMinor <= 4017) OR + (@ProductVersionMajor = 10.5 AND @ProductVersionMinor >= 4251 AND @ProductVersionMinor <= 4319) OR + (@ProductVersionMajor = 11 AND @ProductVersionMinor >= 3000 AND @ProductVersionMinor <= 3129) OR + (@ProductVersionMajor = 11 AND @ProductVersionMinor >= 3300 AND @ProductVersionMinor <= 3447) OR + (@ProductVersionMajor = 12 AND @ProductVersionMinor >= 2000 AND @ProductVersionMinor <= 2253) OR + (@ProductVersionMajor = 12 AND @ProductVersionMinor >= 2300 AND @ProductVersionMinor <= 2370) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 157) WITH NOWAIT; + + INSERT INTO #BlitzResults(CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES(157, 20, 'Reliability', 'Dangerous Build of SQL Server (Security)', 'https://technet.microsoft.com/en-us/library/security/MS14-044', + 'There are dangerous known bugs with version ' + CAST(@ProductVersionMajor AS VARCHAR(100)) + '.' + CAST(@ProductVersionMinor AS VARCHAR(100)) + '. Check the URL for details and apply the right service pack or hotfix.'); + END; + + END; + + /* Check if SQL 2016 Standard Edition but not SP1 */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 189 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + IF (@ProductVersionMajor = 13 AND @ProductVersionMinor < 4001 AND @@VERSION LIKE '%Standard Edition%') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 189) WITH NOWAIT; + + INSERT INTO #BlitzResults(CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES(189, 100, 'Features', 'Missing Features', 'https://blogs.msdn.microsoft.com/sqlreleaseservices/sql-server-2016-service-pack-1-sp1-released/', + 'SQL 2016 Standard Edition is being used but not Service Pack 1. Check the URL for a list of Enterprise Features that are included in Standard Edition as of SP1.'); + END; + + END; + + /* Check if SQL 2017 but not CU3 */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 216 ) + AND SERVERPROPERTY('EngineEdition') <> 8 /* Azure Managed Instances */ + BEGIN + IF (@ProductVersionMajor = 14 AND @ProductVersionMinor < 3015) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 216) WITH NOWAIT; + + INSERT INTO #BlitzResults(CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES(216, 100, 'Features', 'Missing Features', 'https://support.microsoft.com/en-us/help/4041814', + 'SQL 2017 is being used but not Cumulative Update 3. We''d recommend patching to take advantage of increased analytics when running BlitzCache.'); + END; + + END; + + /* Cumulative Update Available */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 217 ) + AND SERVERPROPERTY('EngineEdition') NOT IN (5,8) /* Azure Managed Instances and Azure SQL DB*/ + AND EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'SqlServerVersions' AND TABLE_TYPE = 'BASE TABLE') + AND NOT EXISTS (SELECT * FROM #BlitzResults WHERE CheckID IN (128, 129, 157, 189, 216)) /* Other version checks */ + BEGIN + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 217) WITH NOWAIT; + + INSERT INTO #BlitzResults(CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT TOP 1 217, 100, 'Reliability', 'Cumulative Update Available', COALESCE(v.Url, 'https://SQLServerUpdates.com/'), + v.MinorVersionName + ' was released on ' + CAST(CONVERT(DATETIME, v.ReleaseDate, 112) AS VARCHAR(100)) + FROM dbo.SqlServerVersions v + WHERE v.MajorVersionNumber = @ProductVersionMajor + AND v.MinorVersionNumber > @ProductVersionMinor + ORDER BY v.MinorVersionNumber DESC; + END; + + /* Performance - High Memory Use for In-Memory OLTP (Hekaton) */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 145 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + WHERE o.name = 'dm_db_xtp_table_memory_stats' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 145) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 145 AS CheckID, + 10 AS Priority, + ''Performance'' AS FindingsGroup, + ''High Memory Use for In-Memory OLTP (Hekaton)'' AS Finding, + ''https://www.brentozar.com/go/hekaton'' AS URL, + CAST(CAST((SUM(mem.pages_kb / 1024.0) / CAST(value_in_use AS INT) * 100) AS INT) AS NVARCHAR(100)) + ''% of your '' + CAST(CAST((CAST(value_in_use AS DECIMAL(38,1)) / 1024) AS MONEY) AS NVARCHAR(100)) + ''GB of your max server memory is being used for in-memory OLTP tables (Hekaton). Microsoft recommends having 2X your Hekaton table space available in memory just for Hekaton, with a max of 250GB of in-memory data regardless of your server memory capacity.'' AS Details + FROM sys.configurations c INNER JOIN sys.dm_os_memory_clerks mem ON mem.type = ''MEMORYCLERK_XTP'' + WHERE c.name = ''max server memory (MB)'' + GROUP BY c.value_in_use + HAVING CAST(value_in_use AS DECIMAL(38,2)) * .25 < SUM(mem.pages_kb / 1024.0) + OR SUM(mem.pages_kb / 1024.0) > 250000 OPTION (RECOMPILE)'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* Performance - In-Memory OLTP (Hekaton) In Use */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 146 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + WHERE o.name = 'dm_db_xtp_table_memory_stats' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 146) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 146 AS CheckID, + 200 AS Priority, + ''Performance'' AS FindingsGroup, + ''In-Memory OLTP (Hekaton) In Use'' AS Finding, + ''https://www.brentozar.com/go/hekaton'' AS URL, + CAST(CAST((SUM(mem.pages_kb / 1024.0) / CAST(value_in_use AS INT) * 100) AS DECIMAL(6,1)) AS NVARCHAR(100)) + ''% of your '' + CAST(CAST((CAST(value_in_use AS DECIMAL(38,1)) / 1024) AS MONEY) AS NVARCHAR(100)) + ''GB of your max server memory is being used for in-memory OLTP tables (Hekaton).'' AS Details + FROM sys.configurations c INNER JOIN sys.dm_os_memory_clerks mem ON mem.type = ''MEMORYCLERK_XTP'' + WHERE c.name = ''max server memory (MB)'' + GROUP BY c.value_in_use + HAVING SUM(mem.pages_kb / 1024.0) > 1000 OPTION (RECOMPILE)'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* In-Memory OLTP (Hekaton) - Transaction Errors */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 147 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + WHERE o.name = 'dm_xtp_transaction_stats' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 147) WITH NOWAIT + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 147 AS CheckID, + 100 AS Priority, + ''In-Memory OLTP (Hekaton)'' AS FindingsGroup, + ''Transaction Errors'' AS Finding, + ''https://www.brentozar.com/go/hekaton'' AS URL, + ''Since restart: '' + CAST(validation_failures AS NVARCHAR(100)) + '' validation failures, '' + CAST(dependencies_failed AS NVARCHAR(100)) + '' dependency failures, '' + CAST(write_conflicts AS NVARCHAR(100)) + '' write conflicts, '' + CAST(unique_constraint_violations AS NVARCHAR(100)) + '' unique constraint violations.'' AS Details + FROM sys.dm_xtp_transaction_stats + WHERE validation_failures <> 0 + OR dependencies_failed <> 0 + OR write_conflicts <> 0 + OR unique_constraint_violations <> 0 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* Reliability - Database Files on Network File Shares */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 148 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 148) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT 148 AS CheckID , + d.[name] AS DatabaseName , + 170 AS Priority , + 'Reliability' AS FindingsGroup , + 'Database Files on Network File Shares' AS Finding , + 'https://www.brentozar.com/go/nas' AS URL , + ( 'Files for this database are on: ' + LEFT(mf.physical_name, 30)) AS Details + FROM sys.databases d + INNER JOIN sys.master_files mf ON d.database_id = mf.database_id + WHERE mf.physical_name LIKE '\\%' + AND d.name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 148); + END; + + /* Reliability - Database Files Stored in Azure */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 149 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 149) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT 149 AS CheckID , + d.[name] AS DatabaseName , + 170 AS Priority , + 'Reliability' AS FindingsGroup , + 'Database Files Stored in Azure' AS Finding , + 'https://www.brentozar.com/go/azurefiles' AS URL , + ( 'Files for this database are on: ' + LEFT(mf.physical_name, 30)) AS Details + FROM sys.databases d + INNER JOIN sys.master_files mf ON d.database_id = mf.database_id + WHERE mf.physical_name LIKE 'http://%' + AND d.name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 149); + END; + + /* Reliability - Errors Logged Recently in the Default Trace */ + + /* First, let's check that there aren't any issues with the trace files */ + BEGIN TRY + + IF @SkipTrace = 0 + BEGIN + INSERT INTO #fnTraceGettable + ( TextData , + DatabaseName , + EventClass , + Severity , + StartTime , + EndTime , + Duration , + NTUserName , + NTDomainName , + HostName , + ApplicationName , + LoginName , + DBUserName + ) + SELECT TOP 20000 + CONVERT(NVARCHAR(4000),t.TextData) , + t.DatabaseName , + t.EventClass , + t.Severity , + t.StartTime , + t.EndTime , + t.Duration , + t.NTUserName , + t.NTDomainName , + t.HostName , + t.ApplicationName , + t.LoginName , + t.DBUserName + FROM sys.fn_trace_gettable(@base_tracefilename, DEFAULT) t + WHERE + ( + t.EventClass = 22 + AND t.Severity >= 17 + AND t.StartTime > DATEADD(dd, -30, GETDATE()) + ) + OR + ( + t.EventClass IN (92, 93) + AND t.StartTime > DATEADD(dd, -30, GETDATE()) + AND t.Duration > 15000000 + ) + OR + ( + t.EventClass IN (94, 95, 116) + ) + END; + + SET @TraceFileIssue = 0 + + END TRY + BEGIN CATCH + + SET @TraceFileIssue = 1 + + END CATCH + + IF @TraceFileIssue = 1 + BEGIN + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 199 ) + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + '199' AS CheckID , + '' AS DatabaseName , + 50 AS Priority , + 'Reliability' AS FindingsGroup , + 'There Is An Error With The Default Trace' AS Finding , + 'https://www.brentozar.com/go/defaulttrace' AS URL , + 'Somebody has been messing with your trace files. Check the files are present at ' + @base_tracefilename AS Details + END + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 150 ) + AND @base_tracefilename IS NOT NULL + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 150) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT 150 AS CheckID , + t.DatabaseName, + 170 AS Priority , + 'Reliability' AS FindingsGroup , + 'Errors Logged Recently in the Default Trace' AS Finding , + 'https://www.brentozar.com/go/defaulttrace' AS URL , + CAST(t.TextData AS NVARCHAR(4000)) AS Details + FROM #fnTraceGettable t + WHERE t.EventClass = 22 + /* Removed these as they're unnecessary, we filter this when inserting data into #fnTraceGettable */ + --AND t.Severity >= 17 + --AND t.StartTime > DATEADD(dd, -30, GETDATE()); + END; + + /* Performance - File Growths Slow */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 151 ) + AND @base_tracefilename IS NOT NULL + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 151) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT 151 AS CheckID , + t.DatabaseName, + 50 AS Priority , + 'Performance' AS FindingsGroup , + 'File Growths Slow' AS Finding , + 'https://www.brentozar.com/go/filegrowth' AS URL , + CAST(COUNT(*) AS NVARCHAR(100)) + ' growths took more than 15 seconds each. Consider setting file autogrowth to a smaller increment.' AS Details + FROM #fnTraceGettable t + WHERE t.EventClass IN (92, 93) + /* Removed these as they're unnecessary, we filter this when inserting data into #fnTraceGettable */ + --AND t.StartTime > DATEADD(dd, -30, GETDATE()) + --AND t.Duration > 15000000 + GROUP BY t.DatabaseName + HAVING COUNT(*) > 1; + END; + + /* Performance - Many Plans for One Query */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 160 ) + AND EXISTS (SELECT * FROM sys.all_columns WHERE name = 'query_hash') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 160) WITH NOWAIT; + + SET @StringToExecute = N'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT TOP 1 160 AS CheckID, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''Many Plans for One Query'' AS Finding, + ''https://www.brentozar.com/go/parameterization'' AS URL, + CAST(COUNT(DISTINCT plan_handle) AS NVARCHAR(50)) + '' plans are present for a single query in the plan cache - meaning we probably have parameterization issues.'' AS Details + FROM sys.dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) pa + WHERE pa.attribute = ''dbid'' + GROUP BY qs.query_hash, pa.value + HAVING COUNT(DISTINCT plan_handle) > '; + + IF 50 > (SELECT COUNT(*) FROM sys.databases) + SET @StringToExecute = @StringToExecute + N' 50 '; + ELSE + SELECT @StringToExecute = @StringToExecute + CAST(COUNT(*) * 2 AS NVARCHAR(50)) FROM sys.databases; + + SET @StringToExecute = @StringToExecute + N' ORDER BY COUNT(DISTINCT plan_handle) DESC OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* Performance - High Number of Cached Plans */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 161 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 161) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT TOP 1 161 AS CheckID, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''High Number of Cached Plans'' AS Finding, + ''https://www.brentozar.com/go/planlimits'' AS URL, + ''Your server configuration is limited to '' + CAST(ht.buckets_count * 4 AS VARCHAR(20)) + '' '' + ht.name + '', and you are currently caching '' + CAST(cc.entries_count AS VARCHAR(20)) + ''.'' AS Details + FROM sys.dm_os_memory_cache_hash_tables ht + INNER JOIN sys.dm_os_memory_cache_counters cc ON ht.name = cc.name AND ht.type = cc.type + where ht.name IN ( ''SQL Plans'' , ''Object Plans'' , ''Bound Trees'' ) + AND cc.entries_count >= (3 * ht.buckets_count) OPTION (RECOMPILE)'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* Performance - Too Much Free Memory */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 165 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 165) WITH NOWAIT; + + INSERT INTO #BlitzResults + (CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 165, 50, 'Performance', 'Too Much Free Memory', 'https://www.brentozar.com/go/freememory', + CAST((CAST(cFree.cntr_value AS BIGINT) / 1024 / 1024 ) AS NVARCHAR(100)) + N'GB of free memory inside SQL Server''s buffer pool, which is ' + CAST((CAST(cTotal.cntr_value AS BIGINT) / 1024 / 1024) AS NVARCHAR(100)) + N'GB. You would think lots of free memory would be good, but check out the URL for more information.' AS Details + FROM sys.dm_os_performance_counters cFree + INNER JOIN sys.dm_os_performance_counters cTotal ON cTotal.object_name LIKE N'%Memory Manager%' + AND cTotal.counter_name = N'Total Server Memory (KB) ' + WHERE cFree.object_name LIKE N'%Memory Manager%' + AND cFree.counter_name = N'Free Memory (KB) ' + AND CAST(cTotal.cntr_value AS BIGINT) > 20480000000 + AND CAST(cTotal.cntr_value AS BIGINT) * .3 <= CAST(cFree.cntr_value AS BIGINT) + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Standard%'; + + END; + + /* Outdated sp_Blitz - sp_Blitz is Over 6 Months Old */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 155 ) + AND DATEDIFF(MM, @VersionDate, GETDATE()) > 6 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 155) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 155 AS CheckID , + 0 AS Priority , + 'Outdated sp_Blitz' AS FindingsGroup , + 'sp_Blitz is Over 6 Months Old' AS Finding , + 'http://FirstResponderKit.org/' AS URL , + 'Some things get better with age, like fine wine and your T-SQL. However, sp_Blitz is not one of those things - time to go download the current one.' AS Details; + END; + + /* Populate a list of database defaults. I'm doing this kind of oddly - + it reads like a lot of work, but this way it compiles & runs on all + versions of SQL Server. + */ + + IF @Debug IN (1, 2) RAISERROR('Generating database defaults.', 0, 1) WITH NOWAIT; + + INSERT INTO #DatabaseDefaults + SELECT 'is_supplemental_logging_enabled', 0, 131, 210, 'Supplemental Logging Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_supplemental_logging_enabled' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'snapshot_isolation_state', 0, 132, 210, 'Snapshot Isolation Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'snapshot_isolation_state' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_read_committed_snapshot_on', + CASE WHEN SERVERPROPERTY('EngineEdition') = 5 THEN 1 ELSE 0 END, /* RCSI is always enabled in Azure SQL DB per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + 133, 210, CASE WHEN SERVERPROPERTY('EngineEdition') = 5 THEN 'Read Committed Snapshot Isolation Disabled' ELSE 'Read Committed Snapshot Isolation Enabled' END, 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_read_committed_snapshot_on' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_auto_create_stats_incremental_on', 0, 134, 210, 'Auto Create Stats Incremental Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_auto_create_stats_incremental_on' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_ansi_null_default_on', 0, 135, 210, 'ANSI NULL Default Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_ansi_null_default_on' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_recursive_triggers_on', 0, 136, 210, 'Recursive Triggers Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_recursive_triggers_on' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_trustworthy_on', 0, 137, 210, 'Trustworthy Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_trustworthy_on' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_broker_enabled', 0, 230, 210, 'Broker Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_broker_enabled' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_honor_broker_priority_on', 0, 231, 210, 'Honor Broker Priority Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_honor_broker_priority_on' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_parameterization_forced', 0, 138, 210, 'Forced Parameterization Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_parameterization_forced' AND object_id = OBJECT_ID('sys.databases'); + /* Not alerting for this since we actually want it and we have a separate check for it: + INSERT INTO #DatabaseDefaults + SELECT 'is_query_store_on', 0, 139, 210, 'Query Store Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_query_store_on' AND object_id = OBJECT_ID('sys.databases'); + */ + INSERT INTO #DatabaseDefaults + SELECT 'is_cdc_enabled', 0, 140, 210, 'Change Data Capture Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_cdc_enabled' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'containment', 0, 141, 210, 'Containment Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'containment' AND object_id = OBJECT_ID('sys.databases'); + --INSERT INTO #DatabaseDefaults + -- SELECT 'target_recovery_time_in_seconds', 0, 142, 210, 'Target Recovery Time Changed', 'https://www.brentozar.com/go/dbdefaults', NULL + -- FROM sys.all_columns + -- WHERE name = 'target_recovery_time_in_seconds' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'delayed_durability', 0, 143, 210, 'Delayed Durability Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'delayed_durability' AND object_id = OBJECT_ID('sys.databases'); + INSERT INTO #DatabaseDefaults + SELECT 'is_memory_optimized_elevate_to_snapshot_on', 0, 144, 210, 'Memory Optimized Enabled', 'https://www.brentozar.com/go/dbdefaults', NULL + FROM sys.all_columns + WHERE name = 'is_memory_optimized_elevate_to_snapshot_on' AND object_id = OBJECT_ID('sys.databases') + AND SERVERPROPERTY('EngineEdition') <> 8; /* Hekaton is always enabled in Managed Instances per https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1919 */ + + DECLARE DatabaseDefaultsLoop CURSOR FOR + SELECT name, DefaultValue, CheckID, Priority, Finding, URL, Details + FROM #DatabaseDefaults; + + OPEN DatabaseDefaultsLoop; + FETCH NEXT FROM DatabaseDefaultsLoop into @CurrentName, @CurrentDefaultValue, @CurrentCheckID, @CurrentPriority, @CurrentFinding, @CurrentURL, @CurrentDetails; + WHILE @@FETCH_STATUS = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, @CurrentCheckID) WITH NOWAIT; + + /* Target Recovery Time (142) can be either 0 or 60 due to a number of bugs */ + IF @CurrentCheckID = 142 + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + SELECT ' + CAST(@CurrentCheckID AS NVARCHAR(200)) + ', d.[name], ' + CAST(@CurrentPriority AS NVARCHAR(200)) + ', ''Non-Default Database Config'', ''' + @CurrentFinding + ''',''' + @CurrentURL + ''',''' + COALESCE(@CurrentDetails, 'This database setting is not the default.') + ''' + FROM sys.databases d + WHERE d.database_id > 4 AND d.state = 0 AND (d.[' + @CurrentName + '] NOT IN (0, 60) OR d.[' + @CurrentName + '] IS NULL) OPTION (RECOMPILE);'; + ELSE + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + SELECT ' + CAST(@CurrentCheckID AS NVARCHAR(200)) + ', d.[name], ' + CAST(@CurrentPriority AS NVARCHAR(200)) + ', ''Non-Default Database Config'', ''' + @CurrentFinding + ''',''' + @CurrentURL + ''',''' + COALESCE(@CurrentDetails, 'This database setting is not the default.') + ''' + FROM sys.databases d + WHERE d.database_id > 4 AND d.state = 0 AND (d.[' + @CurrentName + '] <> ' + @CurrentDefaultValue + ' OR d.[' + @CurrentName + '] IS NULL) OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXEC (@StringToExecute); + + FETCH NEXT FROM DatabaseDefaultsLoop into @CurrentName, @CurrentDefaultValue, @CurrentCheckID, @CurrentPriority, @CurrentFinding, @CurrentURL, @CurrentDetails; + END; + + CLOSE DatabaseDefaultsLoop; + DEALLOCATE DatabaseDefaultsLoop; + +/* Check if target recovery interval <> 60 */ +IF + @ProductVersionMajor >= 10 + AND NOT EXISTS + ( + SELECT + 1/0 + FROM #SkipChecks AS sc + WHERE sc.DatabaseName IS NULL + AND sc.CheckID = 257 + ) + BEGIN + IF EXISTS + ( + SELECT + 1/0 + FROM sys.all_columns AS ac + WHERE ac.name = 'target_recovery_time_in_seconds' + AND ac.object_id = OBJECT_ID('sys.databases') + ) + BEGIN + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 257) WITH NOWAIT; + + DECLARE + @tri nvarchar(max) = N' + SELECT + DatabaseName = + d.name, + CheckId = + 257, + Priority = + 50, + FindingsGroup = + N''Performance'', + Finding = + N''Recovery Interval Not Optimal'', + URL = + N''https://sqlperformance.com/2020/05/system-configuration/0-to-60-switching-to-indirect-checkpoints'', + Details = + N''The database '' + + QUOTENAME(d.name) + + N'' has a target recovery interval of '' + + RTRIM(d.target_recovery_time_in_seconds) + + CASE + WHEN d.target_recovery_time_in_seconds = 0 + THEN N'', which is a legacy default, and should be changed to 60.'' + WHEN d.target_recovery_time_in_seconds <> 0 + THEN N'', which is probably a mistake, and should be changed to 60.'' + END + FROM sys.databases AS d + WHERE d.database_id > 4 + AND d.is_read_only = 0 + AND d.is_in_standby = 0 + AND d.target_recovery_time_in_seconds <> 60; + '; + + INSERT INTO + #BlitzResults + ( + DatabaseName, + CheckID, + Priority, + FindingsGroup, + Finding, + URL, + Details + ) + EXEC sys.sp_executesql + @tri; + + END; + END; + + +/*This checks to see if Agent is Offline*/ +IF @ProductVersionMajor >= 10 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 167 ) + BEGIN + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_services' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 167) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 167 AS [CheckID] , + 250 AS [Priority] , + 'Server Info' AS [FindingsGroup] , + 'Agent is Currently Offline' AS [Finding] , + '' AS [URL] , + ( 'Oops! It looks like the ' + [servicename] + ' service is ' + [status_desc] + '. The startup type is ' + [startup_type_desc] + '.' + ) AS [Details] + FROM + [sys].[dm_server_services] + WHERE [status_desc] <> 'Running' + AND [servicename] LIKE 'SQL Server Agent%' + AND CAST(SERVERPROPERTY('Edition') AS VARCHAR(1000)) NOT LIKE '%xpress%'; + + END; + END; + +/*This checks to see if the Full Text thingy is offline*/ +IF @ProductVersionMajor >= 10 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 168 ) + BEGIN + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_services' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 168) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 168 AS [CheckID] , + 250 AS [Priority] , + 'Server Info' AS [FindingsGroup] , + 'Full-text Filter Daemon Launcher is Currently Offline' AS [Finding] , + '' AS [URL] , + ( 'Oops! It looks like the ' + [servicename] + ' service is ' + [status_desc] + '. The startup type is ' + [startup_type_desc] + '.' + ) AS [Details] + FROM + [sys].[dm_server_services] + WHERE [status_desc] <> 'Running' + AND [servicename] LIKE 'SQL Full-text Filter Daemon Launcher%'; + + END; + END; + +/*This checks which service account SQL Server is running as.*/ +IF @ProductVersionMajor >= 10 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 169 ) + + BEGIN + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_services' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 169) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 169 AS [CheckID] , + 250 AS [Priority] , + 'Informational' AS [FindingsGroup] , + 'SQL Server is running under an NT Service account' AS [Finding] , + 'https://www.brentozar.com/go/setup' AS [URL] , + ( 'I''m running as ' + [service_account] + '.' + ) AS [Details] + FROM + [sys].[dm_server_services] + WHERE [service_account] LIKE 'NT Service%' + AND [servicename] LIKE 'SQL Server%' + AND [servicename] NOT LIKE 'SQL Server Agent%' + AND [servicename] NOT LIKE 'SQL Server Launchpad%'; + + END; + END; + +/*This checks which service account SQL Agent is running as.*/ +IF @ProductVersionMajor >= 10 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 170 ) + + BEGIN + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_services' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 170) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 170 AS [CheckID] , + 250 AS [Priority] , + 'Informational' AS [FindingsGroup] , + 'SQL Server Agent is running under an NT Service account' AS [Finding] , + 'https://www.brentozar.com/go/setup' AS [URL] , + ( 'I''m running as ' + [service_account] + '.' + ) AS [Details] + FROM + [sys].[dm_server_services] + WHERE [service_account] LIKE 'NT Service%' + AND [servicename] LIKE 'SQL Server Agent%'; + + END; + END; + +/*This checks that First Responder Kit is consistent. +It assumes that all the objects of the kit resides in the same database, the one in which this SP is stored +It also is ready to check for installation in another schema. +*/ +IF( + NOT EXISTS ( + SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 226 + ) +) +BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running check with id %d',0,1,2000); + + SET @spBlitzFullName = QUOTENAME(DB_NAME()) + '.' +QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)) + '.' + QUOTENAME(OBJECT_NAME(@@PROCID)); + SET @BlitzIsOutdatedComparedToOthers = 0; + SET @tsql = NULL; + SET @VersionCheckModeExistsTSQL = NULL; + SET @BlitzProcDbName = DB_NAME(); + SET @ExecRet = NULL; + SET @InnerExecRet = NULL; + SET @TmpCnt = NULL; + + SET @PreviousComponentName = NULL; + SET @PreviousComponentFullPath = NULL; + SET @CurrentStatementId = NULL; + SET @CurrentComponentSchema = NULL; + SET @CurrentComponentName = NULL; + SET @CurrentComponentType = NULL; + SET @CurrentComponentVersionDate = NULL; + SET @CurrentComponentFullName = NULL; + SET @CurrentComponentMandatory = NULL; + SET @MaximumVersionDate = NULL; + + SET @StatementCheckName = NULL; + SET @StatementOutputsCounter = NULL; + SET @OutputCounterExpectedValue = NULL; + SET @StatementOutputsExecRet = NULL; + SET @StatementOutputsDateTime = NULL; + + SET @CurrentComponentMandatoryCheckOK = NULL; + SET @CurrentComponentVersionCheckModeOK = NULL; + + SET @canExitLoop = 0; + SET @frkIsConsistent = 0; + + + SET @tsql = 'USE ' + QUOTENAME(@BlitzProcDbName) + ';' + @crlf + + 'WITH FRKComponents (' + @crlf + + ' ObjectName,' + @crlf + + ' ObjectType,' + @crlf + + ' MandatoryComponent' + @crlf + + ')' + @crlf + + 'AS (' + @crlf + + ' SELECT ''sp_AllNightLog'',''P'' ,0' + @crlf + + ' UNION ALL' + @crlf + + ' SELECT ''sp_AllNightLog_Setup'', ''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_Blitz'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_BlitzBackups'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_BlitzCache'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_BlitzFirst'',''P'',0' + @crlf + + ' UNION ALL' + @crlf + + ' SELECT ''sp_BlitzIndex'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_BlitzLock'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_BlitzQueryStore'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_BlitzWho'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_DatabaseRestore'',''P'',0' + @crlf + + ' UNION ALL ' + @crlf + + ' SELECT ''sp_ineachdb'',''P'',0' + @crlf + + ' UNION ALL' + @crlf + + ' SELECT ''SqlServerVersions'',''U'',0' + @crlf + + ')' + @crlf + + 'INSERT INTO #FRKObjects (' + @crlf + + ' DatabaseName,ObjectSchemaName,ObjectName, ObjectType,MandatoryComponent' + @crlf + + ')' + @crlf + + 'SELECT DB_NAME(),SCHEMA_NAME(o.schema_id), c.ObjectName,c.ObjectType,c.MandatoryComponent' + @crlf + + 'FROM ' + @crlf + + ' FRKComponents c' + @crlf + + 'LEFT JOIN ' + @crlf + + ' sys.objects o' + @crlf + + 'ON c.ObjectName = o.[name]' + @crlf + + 'AND c.ObjectType = o.[type]' + @crlf + + --'WHERE o.schema_id IS NOT NULL' + @crlf + + ';' + ; + + EXEC @ExecRet = sp_executesql @tsql ; + + -- TODO: add check for statement success + + -- TODO: based on SP requirements and presence (SchemaName is not null) ==> update MandatoryComponent column + + -- Filling #StatementsToRun4FRKVersionCheck + INSERT INTO #StatementsToRun4FRKVersionCheck ( + CheckName,StatementText,SubjectName,SubjectFullPath, StatementOutputsCounter,OutputCounterExpectedValue,StatementOutputsExecRet,StatementOutputsDateTime + ) + SELECT + 'Mandatory', + 'SELECT @cnt = COUNT(*) FROM #FRKObjects WHERE ObjectSchemaName IS NULL AND ObjectName = ''' + ObjectName + ''' AND MandatoryComponent = 1;', + ObjectName, + QUOTENAME(DatabaseName) + '.' + QUOTENAME(ObjectSchemaName) + '.' + QUOTENAME(ObjectName), + 1, + 0, + 0, + 0 + FROM #FRKObjects + UNION ALL + SELECT + 'VersionCheckMode', + 'SELECT @cnt = COUNT(*) FROM ' + + QUOTENAME(DatabaseName) + '.sys.all_parameters ' + + 'where object_id = OBJECT_ID(''' + QUOTENAME(DatabaseName) + '.' + QUOTENAME(ObjectSchemaName) + '.' + QUOTENAME(ObjectName) + ''') AND [name] = ''@VersionCheckMode'';', + ObjectName, + QUOTENAME(DatabaseName) + '.' + QUOTENAME(ObjectSchemaName) + '.' + QUOTENAME(ObjectName), + 1, + 1, + 0, + 0 + FROM #FRKObjects + WHERE ObjectType = 'P' + AND ObjectSchemaName IS NOT NULL + UNION ALL + SELECT + 'VersionCheck', + 'EXEC @ExecRet = ' + QUOTENAME(DatabaseName) + '.' + QUOTENAME(ObjectSchemaName) + '.' + QUOTENAME(ObjectName) + ' @VersionCheckMode = 1 , @VersionDate = @ObjDate OUTPUT;', + ObjectName, + QUOTENAME(DatabaseName) + '.' + QUOTENAME(ObjectSchemaName) + '.' + QUOTENAME(ObjectName), + 0, + 0, + 1, + 1 + FROM #FRKObjects + WHERE ObjectType = 'P' + AND ObjectSchemaName IS NOT NULL + ; + IF(@Debug in (1,2)) + BEGIN + SELECT * + FROM #StatementsToRun4FRKVersionCheck ORDER BY SubjectName,SubjectFullPath,StatementId -- in case of schema change ; + END; + + + -- loop on queries... + WHILE(@canExitLoop = 0) + BEGIN + SET @CurrentStatementId = NULL; + + SELECT TOP 1 + @StatementCheckName = CheckName, + @CurrentStatementId = StatementId , + @CurrentComponentName = SubjectName, + @CurrentComponentFullName = SubjectFullPath, + @tsql = StatementText, + @StatementOutputsCounter = StatementOutputsCounter, + @OutputCounterExpectedValue = OutputCounterExpectedValue , + @StatementOutputsExecRet = StatementOutputsExecRet, + @StatementOutputsDateTime = StatementOutputsDateTime + FROM #StatementsToRun4FRKVersionCheck + ORDER BY SubjectName, SubjectFullPath,StatementId /* in case of schema change */ + ; + + -- loop exit condition + IF(@CurrentStatementId IS NULL) + BEGIN + BREAK; + END; + + IF @Debug IN (1, 2) RAISERROR(' Statement: %s',0,1,@tsql); + + -- we start a new component + IF(@PreviousComponentName IS NULL OR + (@PreviousComponentName IS NOT NULL AND @PreviousComponentName <> @CurrentComponentName) OR + (@PreviousComponentName IS NOT NULL AND @PreviousComponentName = @CurrentComponentName AND @PreviousComponentFullPath <> @CurrentComponentFullName) + ) + BEGIN + -- reset variables + SET @CurrentComponentMandatoryCheckOK = 0; + SET @CurrentComponentVersionCheckModeOK = 0; + SET @PreviousComponentName = @CurrentComponentName; + SET @PreviousComponentFullPath = @CurrentComponentFullName ; + END; + + IF(@StatementCheckName NOT IN ('Mandatory','VersionCheckMode','VersionCheck')) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (code generator changed)' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Your version check failed because a change has been made to the version check code generator.' + @crlf + + 'Error: No handler for check with name "' + ISNULL(@StatementCheckName,'') + '"' AS Details + ; + + -- we will stop the test because it's possible to get the same message for other components + SET @canExitLoop = 1; + CONTINUE; + END; + + IF(@StatementCheckName = 'Mandatory') + BEGIN + -- outputs counter + EXEC @ExecRet = sp_executesql @tsql, N'@cnt INT OUTPUT',@cnt = @TmpCnt OUTPUT; + + IF(@ExecRet <> 0) + BEGIN + + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (dynamic query failure)' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Your version check failed due to dynamic query failure.' + @crlf + + 'Error: following query failed at execution (check if component [' + ISNULL(@CurrentComponentName,@CurrentComponentName) + '] is mandatory and missing)' + @crlf + + @tsql AS Details + ; + + -- we will stop the test because it's possible to get the same message for other components + SET @canExitLoop = 1; + CONTINUE; + END; + + IF(@TmpCnt <> @OutputCounterExpectedValue) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 227 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Component Missing: ' + @CurrentComponentName AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated version of the First Responder Kit to install it.' AS Details + ; + + -- as it's missing, no value for SubjectFullPath + DELETE FROM #StatementsToRun4FRKVersionCheck WHERE SubjectName = @CurrentComponentName ; + CONTINUE; + END; + + SET @CurrentComponentMandatoryCheckOK = 1; + END; + + IF(@StatementCheckName = 'VersionCheckMode') + BEGIN + IF(@CurrentComponentMandatoryCheckOK = 0) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (unexpectedly modified checks ordering)' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Version check failed because "Mandatory" check has not been completed before for current component' + @crlf + + 'Error: version check mode happenned before "Mandatory" check for component called "' + @CurrentComponentFullName + '"' + ; + + -- we will stop the test because it's possible to get the same message for other components + SET @canExitLoop = 1; + CONTINUE; + END; + + -- outputs counter + EXEC @ExecRet = sp_executesql @tsql, N'@cnt INT OUTPUT',@cnt = @TmpCnt OUTPUT; + + IF(@ExecRet <> 0) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (dynamic query failure)' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Version check failed because a change has been made to the code generator.' + @crlf + + 'Error: following query failed at execution (check if component [' + @CurrentComponentFullName + '] can run in VersionCheckMode)' + @crlf + + @tsql AS Details + ; + + -- we will stop the test because it's possible to get the same message for other components + SET @canExitLoop = 1; + CONTINUE; + END; + + IF(@TmpCnt <> @OutputCounterExpectedValue) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 228 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Component Outdated: ' + @CurrentComponentFullName AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Component ' + @CurrentComponentFullName + ' is not at the minimum version required to run this procedure' + @crlf + + 'VersionCheckMode has been introduced in component version date after "20190320". This means its version is lower than or equal to that date.' AS Details; + ; + + DELETE FROM #StatementsToRun4FRKVersionCheck WHERE SubjectFullPath = @CurrentComponentFullName ; + CONTINUE; + END; + + SET @CurrentComponentVersionCheckModeOK = 1; + END; + + IF(@StatementCheckName = 'VersionCheck') + BEGIN + IF(@CurrentComponentMandatoryCheckOK = 0 OR @CurrentComponentVersionCheckModeOK = 0) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (unexpectedly modified checks ordering)' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Version check failed because "VersionCheckMode" check has not been completed before for component called "' + @CurrentComponentFullName + '"' + @crlf + + 'Error: VersionCheck happenned before "VersionCheckMode" check for component called "' + @CurrentComponentFullName + '"' + ; + + -- we will stop the test because it's possible to get the same message for other components + SET @canExitLoop = 1; + CONTINUE; + END; + + EXEC @ExecRet = sp_executesql @tsql , N'@ExecRet INT OUTPUT, @ObjDate DATETIME OUTPUT', @ExecRet = @InnerExecRet OUTPUT, @ObjDate = @CurrentComponentVersionDate OUTPUT; + + IF(@ExecRet <> 0) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (dynamic query failure)' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. The version check failed because a change has been made to the code generator.' + @crlf + + 'Error: following query failed at execution (check if component [' + @CurrentComponentFullName + '] is at the expected version)' + @crlf + + @tsql AS Details + ; + + -- we will stop the test because it's possible to get the same message for other components + SET @canExitLoop = 1; + CONTINUE; + END; + + + IF(@InnerExecRet <> 0) + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 226 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Version Check Failed (Failed dynamic SP call to ' + @CurrentComponentFullName + ')' AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download an updated First Responder Kit. Error: following query failed at execution (check if component [' + @CurrentComponentFullName + '] is at the expected version)' + @crlf + + 'Return code: ' + CONVERT(VARCHAR(10),@InnerExecRet) + @crlf + + 'T-SQL Query: ' + @crlf + + @tsql AS Details + ; + + -- advance to next component + DELETE FROM #StatementsToRun4FRKVersionCheck WHERE SubjectFullPath = @CurrentComponentFullName ; + CONTINUE; + END; + + IF(@CurrentComponentVersionDate < @VersionDate) + BEGIN + + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 228 AS CheckID , + 253 AS Priority , + 'First Responder Kit' AS FindingsGroup , + 'Component Outdated: ' + @CurrentComponentFullName AS Finding , + 'http://FirstResponderKit.org' AS URL , + 'Download and install the latest First Responder Kit - you''re running some older code, and it doesn''t get better with age.' AS Details + ; + + RAISERROR('Component %s is outdated',10,1,@CurrentComponentFullName); + -- advance to next component + DELETE FROM #StatementsToRun4FRKVersionCheck WHERE SubjectFullPath = @CurrentComponentFullName ; + CONTINUE; + END; + + ELSE IF(@CurrentComponentVersionDate > @VersionDate AND @BlitzIsOutdatedComparedToOthers = 0) + BEGIN + SET @BlitzIsOutdatedComparedToOthers = 1; + RAISERROR('Procedure %s is outdated',10,1,@spBlitzFullName); + IF(@MaximumVersionDate IS NULL OR @MaximumVersionDate < @CurrentComponentVersionDate) + BEGIN + SET @MaximumVersionDate = @CurrentComponentVersionDate; + END; + END; + /* Kept for debug purpose: + ELSE + BEGIN + INSERT INTO #BlitzResults( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 2000 AS CheckID , + 250 AS Priority , + 'Informational' AS FindingsGroup , + 'First Responder kit component ' + @CurrentComponentFullName + ' is at the expected version' AS Finding , + 'https://www.BrentOzar.com/blitz/' AS URL , + 'Version date is: ' + CONVERT(VARCHAR(32),@CurrentComponentVersionDate,121) AS Details + ; + END; + */ + END; + + -- could be performed differently to minimize computation + DELETE FROM #StatementsToRun4FRKVersionCheck WHERE StatementId = @CurrentStatementId ; + END; +END; + + +/*This counts memory dumps and gives min and max date of in view*/ +IF @ProductVersionMajor >= 10 + AND NOT (@ProductVersionMajor = 10.5 AND @ProductVersionMinor < 4297) /* Skip due to crash bug: https://support.microsoft.com/en-us/help/2908087 */ + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 171 ) + BEGIN + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_memory_dumps' ) + BEGIN + IF EXISTS (SELECT * FROM [sys].[dm_server_memory_dumps] WHERE [creation_time] >= DATEADD(YEAR, -1, GETDATE())) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 171) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 171 AS [CheckID] , + 20 AS [Priority] , + 'Reliability' AS [FindingsGroup] , + 'Memory Dumps Have Occurred' AS [Finding] , + 'https://www.brentozar.com/go/dump' AS [URL] , + ( 'That ain''t good. I''ve had ' + + CAST(COUNT(*) AS VARCHAR(100)) + ' memory dumps between ' + + CAST(CAST(MIN([creation_time]) AS DATETIME) AS VARCHAR(100)) + + ' and ' + + CAST(CAST(MAX([creation_time]) AS DATETIME) AS VARCHAR(100)) + + '!' + ) AS [Details] + FROM + [sys].[dm_server_memory_dumps] + WHERE [creation_time] >= DATEADD(year, -1, GETDATE()); + + END; + END; + END; + +/*Checks to see if you're on Developer or Evaluation*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 173 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 173) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 173 AS [CheckID] , + 200 AS [Priority] , + 'Licensing' AS [FindingsGroup] , + 'Non-Production License' AS [Finding] , + 'https://www.brentozar.com/go/licensing' AS [URL] , + ( 'We''re not the licensing police, but if this is supposed to be a production server, and you''re running ' + + CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) + + ' the good folks at Microsoft might get upset with you. Better start counting those cores.' + ) AS [Details] + WHERE CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) LIKE '%Developer%' + OR CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) LIKE '%Evaluation%'; + + END; + +/*Checks to see if Buffer Pool Extensions are in use*/ + IF @ProductVersionMajor >= 12 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 174 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 174) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 174 AS [CheckID] , + 200 AS [Priority] , + 'Performance' AS [FindingsGroup] , + 'Buffer Pool Extensions Enabled' AS [Finding] , + 'https://www.brentozar.com/go/bpe' AS [URL] , + ( 'You have Buffer Pool Extensions enabled, and one lives here: ' + + [path] + + '. It''s currently ' + + CASE WHEN [current_size_in_kb] / 1024. / 1024. > 0 + THEN CAST([current_size_in_kb] / 1024. / 1024. AS VARCHAR(100)) + + ' GB' + ELSE CAST([current_size_in_kb] / 1024. AS VARCHAR(100)) + + ' MB' + END + + '. Did you know that BPEs only provide single threaded access 8KB (one page) at a time?' + ) AS [Details] + FROM sys.dm_os_buffer_pool_extension_configuration + WHERE [state_description] <> 'BUFFER POOL EXTENSION DISABLED'; + + END; + +/*Check for too many tempdb files*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 175 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 175) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 175 AS CheckID , + 'TempDB' AS DatabaseName , + 170 AS Priority , + 'File Configuration' AS FindingsGroup , + 'TempDB Has >16 Data Files' AS Finding , + 'https://www.brentozar.com/go/tempdb' AS URL , + 'Woah, Nelly! TempDB has ' + CAST(COUNT_BIG(*) AS VARCHAR(30)) + '. Did you forget to terminate a loop somewhere?' AS Details + FROM sys.[master_files] AS [mf] + WHERE [mf].[database_id] = 2 AND [mf].[type] = 0 + HAVING COUNT_BIG(*) > 16; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 176 ) + BEGIN + + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_xe_sessions' ) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 176) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 176 AS CheckID , + '' AS DatabaseName , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'Extended Events Hyperextension' AS Finding , + 'https://www.brentozar.com/go/xe' AS URL , + 'Hey big spender, you have ' + CAST(COUNT_BIG(*) AS VARCHAR(30)) + ' Extended Events sessions running. You sure you meant to do that?' AS Details + FROM sys.dm_xe_sessions + WHERE [name] NOT IN + ( 'AlwaysOn_health', + 'system_health', + 'telemetry_xevents', + 'sp_server_diagnostics', + 'sp_server_diagnostics session', + 'hkenginexesession' ) + AND name NOT LIKE '%$A%' + HAVING COUNT_BIG(*) >= 2; + END; + END; + + /*Harmful startup parameter*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 177 ) + BEGIN + + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_server_registry' ) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 177) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 177 AS CheckID , + '' AS DatabaseName , + 5 AS Priority , + 'Monitoring' AS FindingsGroup , + 'Disabled Internal Monitoring Features' AS Finding , + 'https://msdn.microsoft.com/en-us/library/ms190737.aspx' AS URL , + 'You have -x as a startup parameter. You should head to the URL and read more about what it does to your system.' AS Details + FROM + [sys].[dm_server_registry] AS [dsr] + WHERE + [dsr].[registry_key] LIKE N'%MSSQLServer\Parameters' + AND [dsr].[value_data] = '-x';; + END; + END; + + + /* Reliability - Dangerous Third Party Modules - 179 */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 179 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 179) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 179 AS [CheckID] , + 5 AS [Priority] , + 'Reliability' AS [FindingsGroup] , + 'Dangerous Third Party Modules' AS [Finding] , + 'https://support.microsoft.com/en-us/kb/2033238' AS [URL] , + ( COALESCE(company, '') + ' - ' + COALESCE(description, '') + ' - ' + COALESCE(name, '') + ' - suspected dangerous third party module is installed.') AS [Details] + FROM sys.dm_os_loaded_modules + WHERE UPPER(name) LIKE UPPER('%\ENTAPI.DLL') OR UPPER(name) LIKE '%MFEBOPK.SYS' /* McAfee VirusScan Enterprise */ + OR UPPER(name) LIKE UPPER('%\HIPI.DLL') OR UPPER(name) LIKE UPPER('%\HcSQL.dll') OR UPPER(name) LIKE UPPER('%\HcApi.dll') OR UPPER(name) LIKE UPPER('%\HcThe.dll') /* McAfee Host Intrusion */ + OR UPPER(name) LIKE UPPER('%\SOPHOS_DETOURED.DLL') OR UPPER(name) LIKE UPPER('%\SOPHOS_DETOURED_x64.DLL') OR UPPER(name) LIKE UPPER('%\SWI_IFSLSP_64.dll') OR UPPER(name) LIKE UPPER('%\SOPHOS~%.dll') /* Sophos AV */ + OR UPPER(name) LIKE UPPER('%\PIOLEDB.DLL') OR UPPER(name) LIKE UPPER('%\PISDK.DLL') /* OSISoft PI data access */ + OR UPPER(name) LIKE UPPER('%ScriptControl%.dll') OR UPPER(name) LIKE UPPER('%umppc%.dll') /* CrowdStrike */ + OR UPPER(name) LIKE UPPER('%perfiCrcPerfMonMgr.DLL') /* Trend Micro OfficeScan */ + OR UPPER(name) LIKE UPPER('%NLEMSQL.SYS') /* NetLib Encryptionizer-Software. */ + OR UPPER(name) LIKE UPPER('%MFETDIK.SYS') /* McAfee Anti-Virus Mini-Firewall */ + OR UPPER(name) LIKE UPPER('%ANTIVIRUS%'); /* To pick up sqlmaggieAntiVirus_64.dll (malware) or anything else labelled AntiVirus */ + /* MS docs link for blacklisted modules: https://learn.microsoft.com/en-us/troubleshoot/sql/performance/performance-consistency-issues-filter-drivers-modules */ + END; + + /*Find shrink database tasks*/ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 180 ) + AND CONVERT(VARCHAR(128), SERVERPROPERTY ('productversion')) LIKE '1%' /* Only run on 2008+ */ + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 180) WITH NOWAIT; + + WITH XMLNAMESPACES ('www.microsoft.com/SqlServer/Dts' AS [dts]) + ,[maintenance_plan_steps] AS ( + SELECT [name] + , [id] -- ID required to link maintenace plan with jobs and jobhistory (sp_Blitz Issue #776) + , CAST(CAST([packagedata] AS VARBINARY(MAX)) AS XML) AS [maintenance_plan_xml] + FROM [msdb].[dbo].[sysssispackages] + WHERE [packagetype] = 6 + ) + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT + 180 AS [CheckID] , + -- sp_Blitz Issue #776 + -- Job has history and was executed in the last 30 days + CASE WHEN (cast(datediff(dd, substring(cast(sjh.run_date as nvarchar(10)), 1, 4) + '-' + substring(cast(sjh.run_date as nvarchar(10)), 5, 2) + '-' + substring(cast(sjh.run_date as nvarchar(10)), 7, 2), GETDATE()) AS INT) < 30) OR (j.[enabled] = 1 AND ssc.[enabled] = 1 )THEN + 100 + ELSE -- no job history (implicit) AND job not run in the past 30 days AND (Job disabled OR Job Schedule disabled) + 200 + END AS Priority, + 'Performance' AS [FindingsGroup] , + 'Shrink Database Step In Maintenance Plan' AS [Finding] , + 'https://www.brentozar.com/go/autoshrink' AS [URL] , + 'The maintenance plan ' + [mps].[name] + ' has a step to shrink databases in it. Shrinking databases is as outdated as maintenance plans.' + + CASE WHEN COALESCE(ssc.name,'0') != '0' THEN + ' (Schedule: [' + ssc.name + '])' ELSE + '' END AS [Details] + FROM [maintenance_plan_steps] [mps] + CROSS APPLY [maintenance_plan_xml].[nodes]('//dts:Executables/dts:Executable') [t]([c]) + join msdb.dbo.sysmaintplan_subplans as sms + on mps.id = sms.plan_id + JOIN msdb.dbo.sysjobs j + on sms.job_id = j.job_id + LEFT OUTER JOIN msdb.dbo.sysjobsteps AS step + ON j.job_id = step.job_id + LEFT OUTER JOIN msdb.dbo.sysjobschedules AS sjsc + ON j.job_id = sjsc.job_id + LEFT OUTER JOIN msdb.dbo.sysschedules AS ssc + ON sjsc.schedule_id = ssc.schedule_id + AND sjsc.job_id = j.job_id + LEFT OUTER JOIN msdb.dbo.sysjobhistory AS sjh + ON j.job_id = sjh.job_id + AND step.step_id = sjh.step_id + AND sjh.run_date IN (SELECT max(sjh2.run_date) FROM msdb.dbo.sysjobhistory AS sjh2 WHERE sjh2.job_id = j.job_id) -- get the latest entry date + AND sjh.run_time IN (SELECT max(sjh3.run_time) FROM msdb.dbo.sysjobhistory AS sjh3 WHERE sjh3.job_id = j.job_id AND sjh3.run_date = sjh.run_date) -- get the latest entry time + WHERE [c].[value]('(@dts:ObjectName)', 'VARCHAR(128)') = 'Shrink Database Task'; + + END; + + /*Find repetitive maintenance tasks*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 181 ) + AND CONVERT(VARCHAR(128), SERVERPROPERTY ('productversion')) LIKE '1%' /* Only run on 2008+ */ + BEGIN + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 181) WITH NOWAIT; + + WITH XMLNAMESPACES ('www.microsoft.com/SqlServer/Dts' AS [dts]) + ,[maintenance_plan_steps] AS ( + SELECT [name] + , CAST(CAST([packagedata] AS VARBINARY(MAX)) AS XML) AS [maintenance_plan_xml] + FROM [msdb].[dbo].[sysssispackages] + WHERE [packagetype] = 6 + ), [maintenance_plan_table] AS ( + SELECT [mps].[name] + ,[c].[value]('(@dts:ObjectName)', 'NVARCHAR(128)') AS [step_name] + FROM [maintenance_plan_steps] [mps] + CROSS APPLY [maintenance_plan_xml].[nodes]('//dts:Executables/dts:Executable') [t]([c]) + ), [mp_steps_pretty] AS (SELECT DISTINCT [m1].[name] , + STUFF((SELECT N', ' + [m2].[step_name] FROM [maintenance_plan_table] AS [m2] WHERE [m1].[name] = [m2].[name] + FOR XML PATH(N'')), 1, 2, N'') AS [maintenance_plan_steps] + FROM [maintenance_plan_table] AS [m1]) + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 181 AS [CheckID] , + 100 AS [Priority] , + 'Performance' AS [FindingsGroup] , + 'Repetitive Steps In Maintenance Plans' AS [Finding] , + 'https://ola.hallengren.com/' AS [URL] , + 'The maintenance plan ' + [m].[name] + ' is doing repetitive work on indexes and statistics. Perhaps it''s time to try something more modern?' AS [Details] + FROM [mp_steps_pretty] m + WHERE m.[maintenance_plan_steps] LIKE '%Rebuild%Reorganize%' + OR m.[maintenance_plan_steps] LIKE '%Rebuild%Update%'; + + END; + + + /* Reliability - No Failover Cluster Nodes Available - 184 */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 184 ) + AND CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)) NOT LIKE '10%' + AND CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)) NOT LIKE '9%' + BEGIN + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 184) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT TOP 1 + 184 AS CheckID , + 20 AS Priority , + ''Reliability'' AS FindingsGroup , + ''No Failover Cluster Nodes Available'' AS Finding , + ''https://www.brentozar.com/go/node'' AS URL , + ''There are no failover cluster nodes available if the active node fails'' AS Details + FROM ( + SELECT SUM(CASE WHEN [status] = 0 AND [is_current_owner] = 0 THEN 1 ELSE 0 END) AS [available_nodes] + FROM sys.dm_os_cluster_nodes + ) a + WHERE [available_nodes] < 1 OPTION (RECOMPILE)'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* Reliability - TempDB File Error */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 191 ) + AND (SELECT COUNT(*) FROM sys.master_files WHERE database_id = 2) <> (SELECT COUNT(*) FROM tempdb.sys.database_files) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 191) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 191 AS [CheckID] , + 50 AS [Priority] , + 'Reliability' AS [FindingsGroup] , + 'TempDB File Error' AS [Finding] , + 'https://www.brentozar.com/go/tempdboops' AS [URL] , + 'Mismatch between the number of TempDB files in sys.master_files versus tempdb.sys.database_files' AS [Details]; + END; + +/*Perf - Odd number of cores in a socket*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL + AND CheckID = 198 ) + AND EXISTS ( SELECT 1 + FROM sys.dm_os_schedulers + WHERE is_online = 1 + AND scheduler_id < 255 + AND parent_node_id < 64 + GROUP BY parent_node_id, + is_online + HAVING ( COUNT(cpu_id) + 2 ) % 2 = 1 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 198) WITH NOWAIT + + INSERT INTO #BlitzResults + ( + CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details + ) + SELECT 198 AS CheckID, + NULL AS DatabaseName, + 10 AS Priority, + 'Performance' AS FindingsGroup, + 'CPU w/Odd Number of Cores' AS Finding, + 'https://www.brentozar.com/go/oddity' AS URL, + 'Node ' + CONVERT(VARCHAR(10), parent_node_id) + ' has ' + CONVERT(VARCHAR(10), COUNT(cpu_id)) + + CASE WHEN COUNT(cpu_id) = 1 THEN ' core assigned to it. This is a really bad NUMA configuration.' + ELSE ' cores assigned to it. This is a really bad NUMA configuration.' + END AS Details + FROM sys.dm_os_schedulers + WHERE is_online = 1 + AND scheduler_id < 255 + AND parent_node_id < 64 + AND EXISTS ( + SELECT 1 + FROM ( SELECT memory_node_id, SUM(online_scheduler_count) AS schedulers + FROM sys.dm_os_nodes + WHERE memory_node_id < 64 + GROUP BY memory_node_id ) AS nodes + HAVING MIN(nodes.schedulers) <> MAX(nodes.schedulers) + ) + GROUP BY parent_node_id, + is_online + HAVING ( COUNT(cpu_id) + 2 ) % 2 = 1; + + END; + +/*Begin: checking default trace for odd DBCC activity*/ + + --Grab relevant event data + IF @TraceFileIssue = 0 + BEGIN + SELECT UPPER( + REPLACE( + SUBSTRING(CONVERT(NVARCHAR(MAX), t.TextData), 0, + ISNULL( + NULLIF( + CHARINDEX('(', CONVERT(NVARCHAR(MAX), t.TextData)), + 0), + LEN(CONVERT(NVARCHAR(MAX), t.TextData)) + 1 )) --This replaces everything up to an open paren, if one exists. + , SUBSTRING(CONVERT(NVARCHAR(MAX), t.TextData), + ISNULL( + NULLIF( + CHARINDEX(' WITH ',CONVERT(NVARCHAR(MAX), t.TextData)) + , 0), + LEN(CONVERT(NVARCHAR(MAX), t.TextData)) + 1), + LEN(CONVERT(NVARCHAR(MAX), t.TextData)) + 1 ) + , '') --This replaces any optional WITH clause to a DBCC command, like tableresults. + ) AS [dbcc_event_trunc_upper], + UPPER( + REPLACE( + CONVERT(NVARCHAR(MAX), t.TextData), SUBSTRING(CONVERT(NVARCHAR(MAX), t.TextData), + ISNULL( + NULLIF( + CHARINDEX(' WITH ',CONVERT(NVARCHAR(MAX), t.TextData)) + , 0), + LEN(CONVERT(NVARCHAR(MAX), t.TextData)) + 1), + LEN(CONVERT(NVARCHAR(MAX), t.TextData)) + 1 ), '')) AS [dbcc_event_full_upper], + MIN(t.StartTime) OVER (PARTITION BY CONVERT(NVARCHAR(128), t.TextData)) AS min_start_time, + MAX(t.StartTime) OVER (PARTITION BY CONVERT(NVARCHAR(128), t.TextData)) AS max_start_time, + t.NTUserName AS [nt_user_name], + t.NTDomainName AS [nt_domain_name], + t.HostName AS [host_name], + t.ApplicationName AS [application_name], + t.LoginName [login_name], + t.DBUserName AS [db_user_name] + INTO #dbcc_events_from_trace + FROM #fnTraceGettable AS t + WHERE t.EventClass = 116 + OPTION(RECOMPILE) + END; + + /*Overall count of DBCC events excluding silly stuff*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 203 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 203) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT 203 AS CheckID , + 50 AS Priority , + 'DBCC Events' AS FindingsGroup , + 'Overall Events' AS Finding , + 'https://www.BrentOzar.com/go/dbcc' AS URL , + CAST(COUNT(*) AS NVARCHAR(100)) + ' DBCC events have taken place between ' + CONVERT(NVARCHAR(30), MIN(d.min_start_time)) + ' and ' + CONVERT(NVARCHAR(30), MAX(d.max_start_time)) + + '. This does not include CHECKDB and other usually benign DBCC events.' + AS Details + FROM #dbcc_events_from_trace d + /* This WHERE clause below looks horrible, but it's because users can run stuff like + DBCC LOGINFO + with lots of spaces (or carriage returns, or comments) in between the DBCC and the + command they're trying to run. See Github issues 1062, 1074, 1075. + */ + WHERE d.dbcc_event_full_upper NOT LIKE '%DBCC%ADDINSTANCE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%AUTOPILOT%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKALLOC%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKCATALOG%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKCONSTRAINTS%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKDB%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKFILEGROUP%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKIDENT%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKPRIMARYFILE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CHECKTABLE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%CLEANTABLE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%DBINFO%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%ERRORLOG%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%INCREMENTINSTANCE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%INPUTBUFFER%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%LOGINFO%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%OPENTRAN%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%SETINSTANCE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%SHOWFILESTATS%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%SHOW_STATISTICS%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%SQLPERF%NETSTATS%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%SQLPERF%LOGSPACE%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%TRACEON%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%TRACEOFF%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%TRACESTATUS%' + AND d.dbcc_event_full_upper NOT LIKE '%DBCC%USEROPTIONS%' + AND d.application_name NOT LIKE 'Critical Care(R) Collector' + AND d.application_name NOT LIKE '%Red Gate Software Ltd SQL Prompt%' + AND d.application_name NOT LIKE '%Spotlight Diagnostic Server%' + AND d.application_name NOT LIKE '%SQL Diagnostic Manager%' + AND d.application_name NOT LIKE 'SQL Server Checkup%' + AND d.application_name NOT LIKE '%Sentry%' + AND d.application_name NOT LIKE '%LiteSpeed%' + AND d.application_name NOT LIKE '%SQL Monitor - Monitoring%' + + + HAVING COUNT(*) > 0; + + END; + + /*Check for someone running drop clean buffers*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 207 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 207) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT 207 AS CheckID , + 10 AS Priority , + 'Performance' AS FindingsGroup , + 'DBCC DROPCLEANBUFFERS Ran Recently' AS Finding , + 'https://www.BrentOzar.com/go/dbcc' AS URL , + 'The user ' + COALESCE(d.nt_user_name, d.login_name) + ' has run DBCC DROPCLEANBUFFERS ' + CAST(COUNT(*) AS NVARCHAR(100)) + ' times between ' + CONVERT(NVARCHAR(30), MIN(d.min_start_time)) + ' and ' + CONVERT(NVARCHAR(30), MAX(d.max_start_time)) + + '. If this is a production box, know that you''re clearing all data out of memory when this happens. What kind of monster would do that?' + AS Details + FROM #dbcc_events_from_trace d + WHERE d.dbcc_event_full_upper = N'DBCC DROPCLEANBUFFERS' + GROUP BY COALESCE(d.nt_user_name, d.login_name) + HAVING COUNT(*) > 0; + + END; + + /*Check for someone running free proc cache*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 208 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 208) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT 208 AS CheckID , + 10 AS Priority , + 'DBCC Events' AS FindingsGroup , + 'DBCC FREEPROCCACHE Ran Recently' AS Finding , + 'https://www.BrentOzar.com/go/dbcc' AS URL , + 'The user ' + COALESCE(d.nt_user_name, d.login_name) + ' has run DBCC FREEPROCCACHE ' + CAST(COUNT(*) AS NVARCHAR(100)) + ' times between ' + CONVERT(NVARCHAR(30), MIN(d.min_start_time)) + ' and ' + CONVERT(NVARCHAR(30), MAX(d.max_start_time)) + + '. This has bad idea jeans written all over its butt, like most other bad idea jeans.' + AS Details + FROM #dbcc_events_from_trace d + WHERE d.dbcc_event_full_upper = N'DBCC FREEPROCCACHE' + GROUP BY COALESCE(d.nt_user_name, d.login_name) + HAVING COUNT(*) > 0; + + END; + + /*Check for someone clearing wait stats*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 205 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 205) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT 205 AS CheckID , + 50 AS Priority , + 'Performance' AS FindingsGroup , + 'Wait Stats Cleared Recently' AS Finding , + 'https://www.BrentOzar.com/go/dbcc' AS URL , + 'The user ' + COALESCE(d.nt_user_name, d.login_name) + ' has run DBCC SQLPERF(''SYS.DM_OS_WAIT_STATS'',CLEAR) ' + CAST(COUNT(*) AS NVARCHAR(100)) + ' times between ' + CONVERT(NVARCHAR(30), MIN(d.min_start_time)) + ' and ' + CONVERT(NVARCHAR(30), MAX(d.max_start_time)) + + '. Why are you clearing wait stats? What are you hiding?' + AS Details + FROM #dbcc_events_from_trace d + WHERE d.dbcc_event_full_upper = N'DBCC SQLPERF(''SYS.DM_OS_WAIT_STATS'',CLEAR)' + GROUP BY COALESCE(d.nt_user_name, d.login_name) + HAVING COUNT(*) > 0; + + END; + + /*Check for someone writing to pages. Yeah, right?*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 209 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 209) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT 209 AS CheckID , + 10 AS Priority , + 'Reliability' AS FindingsGroup , + 'DBCC WRITEPAGE Used Recently' AS Finding , + 'https://www.BrentOzar.com/go/dbcc' AS URL , + 'The user ' + COALESCE(d.nt_user_name, d.login_name) + ' has run DBCC WRITEPAGE ' + CAST(COUNT(*) AS NVARCHAR(100)) + ' times between ' + CONVERT(NVARCHAR(30), MIN(d.min_start_time)) + ' and ' + CONVERT(NVARCHAR(30), MAX(d.max_start_time)) + + '. So, uh, are they trying to fix corruption, or cause corruption?' + AS Details + FROM #dbcc_events_from_trace d + WHERE d.dbcc_event_trunc_upper = N'DBCC WRITEPAGE' + GROUP BY COALESCE(d.nt_user_name, d.login_name) + HAVING COUNT(*) > 0; + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 210 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 210) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT 210 AS CheckID , + 10 AS Priority , + 'Performance' AS FindingsGroup , + 'DBCC SHRINK% Ran Recently' AS Finding , + 'https://www.BrentOzar.com/go/dbcc' AS URL , + 'The user ' + COALESCE(d.nt_user_name, d.login_name) + ' has run file shrinks ' + CAST(COUNT(*) AS NVARCHAR(100)) + ' times between ' + CONVERT(NVARCHAR(30), MIN(d.min_start_time)) + ' and ' + CONVERT(NVARCHAR(30), MAX(d.max_start_time)) + + '. So, uh, are they trying to cause bad performance on purpose?' + AS Details + FROM #dbcc_events_from_trace d + WHERE d.dbcc_event_trunc_upper LIKE N'DBCC SHRINK%' + GROUP BY COALESCE(d.nt_user_name, d.login_name) + HAVING COUNT(*) > 0; + + END; + +/*End: checking default trace for odd DBCC activity*/ + + /*Begin check for autoshrink events*/ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 206 ) + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 206) WITH NOWAIT + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT 206 AS CheckID , + 10 AS Priority , + 'Performance' AS FindingsGroup , + 'Auto-Shrink Ran Recently' AS Finding , + '' AS URL , + N'The database ' + QUOTENAME(t.DatabaseName) + N' has had ' + + CONVERT(NVARCHAR(10), COUNT(*)) + + N' auto shrink events between ' + + CONVERT(NVARCHAR(30), MIN(t.StartTime)) + ' and ' + CONVERT(NVARCHAR(30), MAX(t.StartTime)) + + ' that lasted on average ' + + CONVERT(NVARCHAR(10), AVG(DATEDIFF(SECOND, t.StartTime, t.EndTime))) + + ' seconds.' AS Details + FROM #fnTraceGettable AS t + WHERE t.EventClass IN (94, 95) + GROUP BY t.DatabaseName + HAVING AVG(DATEDIFF(SECOND, t.StartTime, t.EndTime)) > 5; + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 215 ) + AND @TraceFileIssue = 0 + AND EXISTS (SELECT * FROM sys.all_columns WHERE name = 'database_id' AND object_id = OBJECT_ID('sys.dm_exec_sessions')) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 215) WITH NOWAIT + + SET @StringToExecute = 'INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [DatabaseName] , + [URL] , + [Details] ) + + SELECT 215 AS CheckID , + 100 AS Priority , + ''Performance'' AS FindingsGroup , + ''Implicit Transactions'' AS Finding , + DB_NAME(s.database_id) AS DatabaseName, + ''https://www.brentozar.com/go/ImplicitTransactions/'' AS URL , + N''The database '' + + DB_NAME(s.database_id) + + '' has '' + + CONVERT(NVARCHAR(20), COUNT_BIG(*)) + + '' open implicit transactions with an oldest begin time of '' + + CONVERT(NVARCHAR(30), MIN(tat.transaction_begin_time)) + + '' Run sp_BlitzWho and check the is_implicit_transaction column to see the culprits.'' AS details + FROM sys.dm_tran_active_transactions AS tat + LEFT JOIN sys.dm_tran_session_transactions AS tst + ON tst.transaction_id = tat.transaction_id + LEFT JOIN sys.dm_exec_sessions AS s + ON s.session_id = tst.session_id + WHERE tat.name = ''implicit_transaction'' + GROUP BY DB_NAME(s.database_id), transaction_type, transaction_state;'; + + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + + + + END; + + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 221 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 221) WITH NOWAIT; + + WITH reboot_airhorn + AS + ( + SELECT create_date + FROM sys.databases + WHERE database_id = 2 + UNION ALL + SELECT CAST(DATEADD(SECOND, ( ms_ticks / 1000 ) * ( -1 ), GETDATE()) AS DATETIME) + FROM sys.dm_os_sys_info + ) + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 221 AS CheckID, + 10 AS Priority, + 'Reliability' AS FindingsGroup, + 'Server restarted in last 24 hours' AS Finding, + '' AS URL, + 'Surprise! Your server was last restarted on: ' + CONVERT(VARCHAR(30), MAX(reboot_airhorn.create_date)) AS details + FROM reboot_airhorn + HAVING MAX(reboot_airhorn.create_date) >= DATEADD(HOUR, -24, GETDATE()); + + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 229 ) + AND CAST(SERVERPROPERTY('Edition') AS NVARCHAR(4000)) LIKE '%Evaluation%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 229) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 229 AS CheckID, + 1 AS Priority, + 'Reliability' AS FindingsGroup, + 'Evaluation Edition' AS Finding, + 'https://www.BrentOzar.com/go/workgroup' AS URL, + 'This server will stop working on: ' + CAST(CONVERT(DATETIME, DATEADD(DD, 180, create_date), 102) AS VARCHAR(100)) AS details + FROM sys.server_principals + WHERE sid = 0x010100000000000512000000; + + END; + + + + + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 233 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 233) WITH NOWAIT; + + + IF EXISTS (SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('sys.dm_os_memory_clerks') AND name = 'pages_kb') + BEGIN + /* SQL 2012+ version */ + SET @StringToExecute = N' + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 233 AS CheckID, + 50 AS Priority, + ''Performance'' AS FindingsGroup, + ''Memory Leak in USERSTORE_TOKENPERM Cache'' AS Finding, + ''https://www.BrentOzar.com/go/userstore'' AS URL, + N''UserStore_TokenPerm clerk is using '' + CAST(CAST(SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN pages_kb * 1.0 ELSE 0.0 END) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + + N''GB RAM, total buffer pool is '' + CAST(CAST(SUM(pages_kb) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + N''GB.'' + AS details + FROM sys.dm_os_memory_clerks + HAVING SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN pages_kb * 1.0 ELSE 0.0 END) / SUM(pages_kb) >= 0.1 + AND SUM(pages_kb) / 1024.0 / 1024.0 >= 1; /* At least 1GB RAM overall */'; + EXEC sp_executesql @StringToExecute; + END + ELSE + BEGIN + /* Antiques Roadshow SQL 2008R2 - version */ + SET @StringToExecute = N' + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 233 AS CheckID, + 50 AS Priority, + ''Performance'' AS FindingsGroup, + ''Memory Leak in USERSTORE_TOKENPERM Cache'' AS Finding, + ''https://www.BrentOzar.com/go/userstore'' AS URL, + N''UserStore_TokenPerm clerk is using '' + CAST(CAST(SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN single_pages_kb + multi_pages_kb * 1.0 ELSE 0.0 END) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + + N''GB RAM, total buffer pool is '' + CAST(CAST(SUM(single_pages_kb + multi_pages_kb) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + N''GB.'' + AS details + FROM sys.dm_os_memory_clerks + HAVING SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN single_pages_kb + multi_pages_kb * 1.0 ELSE 0.0 END) / SUM(single_pages_kb + multi_pages_kb) >= 0.1 + AND SUM(single_pages_kb + multi_pages_kb) / 1024.0 / 1024.0 >= 1; /* At least 1GB RAM overall */'; + EXEC sp_executesql @StringToExecute; + END + + END; + + + + + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 234 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 234) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + DatabaseName , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 234 AS CheckID, + 100 AS Priority, + db_name(f.database_id) AS DatabaseName, + 'Reliability' AS FindingsGroup, + 'SQL Server Update May Fail' AS Finding, + 'https://desertdba.com/failovers-cant-serve-two-masters/' AS URL, + 'This database has a file with a logical name of ''master'', which can break SQL Server updates. Rename it in SSMS by right-clicking on the database, go into Properties, and rename the file. Takes effect instantly.' AS details + FROM master.sys.master_files f + WHERE (f.name = N'master') + AND f.database_id > 4 + AND db_name(f.database_id) <> 'master'; /* Thanks Michaels3 for catching this */ + END; + + + + + IF @CheckUserDatabaseObjects = 1 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Starting @CheckUserDatabaseObjects section.', 0, 1) WITH NOWAIT + + /* + But what if you need to run a query in every individual database? + Check out CheckID 99 below. Yes, it uses sp_MSforeachdb, and no, + we're not happy about that. sp_MSforeachdb is known to have a lot + of issues, like skipping databases sometimes. However, this is the + only built-in option that we have. If you're writing your own code + for database maintenance, consider Aaron Bertrand's alternative: + http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/ + We don't include that as part of sp_Blitz, of course, because + copying and distributing copyrighted code from others without their + written permission isn't a good idea. + */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 99 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 99) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; IF EXISTS (SELECT * FROM sys.tables WITH (NOLOCK) WHERE name = ''sysmergepublications'' ) IF EXISTS ( SELECT * FROM sysmergepublications WITH (NOLOCK) WHERE retention = 0) INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) SELECT DISTINCT 99, DB_NAME(), 110, ''Performance'', ''Infinite merge replication metadata retention period'', ''https://www.brentozar.com/go/merge'', (''The ['' + DB_NAME() + ''] database has merge replication metadata retention period set to infinite - this can be the case of significant performance issues.'')'; + END; + /* + Note that by using sp_MSforeachdb, we're running the query in all + databases. We're not checking #SkipChecks here for each database to + see if we should run the check in this database. That means we may + still run a skipped check if it involves sp_MSforeachdb. We just + don't output those results in the last step. + */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 163 ) + AND EXISTS(SELECT * FROM sys.all_objects WHERE name = 'database_query_store_options') + BEGIN + /* --TOURSTOP03-- */ + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 163) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT TOP 1 163, + N''?'', + 200, + ''Performance'', + ''Query Store Disabled'', + ''https://www.brentozar.com/go/querystore'', + (''The new SQL Server 2016 Query Store feature has not been enabled on this database.'') + FROM [?].sys.database_query_store_options WHERE desired_state = 0 + AND N''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''DWConfiguration'', ''DWDiagnostics'', ''DWQueue'', ''ReportServer'', ''ReportServerTempDB'') OPTION (RECOMPILE)'; + END; + + + IF @ProductVersionMajor = 13 AND @ProductVersionMinor < 2149 --2016 CU1 has the fix in it + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 182 ) + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Enterprise%' + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Developer%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 182) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT TOP 1 + 182, + ''Server'', + 20, + ''Reliability'', + ''Query Store Cleanup Disabled'', + ''https://www.brentozar.com/go/cleanup'', + (''SQL 2016 RTM has a bug involving dumps that happen every time Query Store cleanup jobs run. This is fixed in CU1 and later: https://sqlserverupdates.com/sql-server-2016-updates/'') + FROM sys.databases AS d + WHERE d.is_query_store_on = 1 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 235 ) + AND EXISTS(SELECT * FROM sys.all_objects WHERE name = 'database_query_store_options') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 235) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT TOP 1 235, + N''?'', + 150, + ''Performance'', + ''Inconsistent Query Store metadata'', + '''', + (''Query store state in master metadata and database specific metadata not in sync.'') + FROM [?].sys.database_query_store_options dqso + join master.sys.databases D on D.name = N''?'' + WHERE ((dqso.actual_state = 0 AND D.is_query_store_on = 1) OR (dqso.actual_state <> 0 AND D.is_query_store_on = 0)) + AND N''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''DWConfiguration'', ''DWDiagnostics'', ''DWQueue'', ''ReportServer'', ''ReportServerTempDB'') OPTION (RECOMPILE)'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 41 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 41) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'use [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 41, + N''?'', + 170, + ''File Configuration'', + ''Multiple Log Files on One Drive'', + ''https://www.brentozar.com/go/manylogs'', + (''The ['' + DB_NAME() + ''] database has multiple log files on the '' + LEFT(physical_name, 1) + '' drive. This is not a performance booster because log file access is sequential, not parallel.'') + FROM [?].sys.database_files WHERE type_desc = ''LOG'' + AND N''?'' <> ''[tempdb]'' + GROUP BY LEFT(physical_name, 1) + HAVING COUNT(*) > 1 + AND SUM(size) < 268435456 OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 42 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 42) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'use [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT 42, + N''?'', + 170, + ''File Configuration'', + ''Uneven File Growth Settings in One Filegroup'', + ''https://www.brentozar.com/go/grow'', + (''The ['' + DB_NAME() + ''] database has multiple data files in one filegroup, but they are not all set up to grow in identical amounts. This can lead to uneven file activity inside the filegroup.'') + FROM [?].sys.database_files + WHERE type_desc = ''ROWS'' + GROUP BY data_space_id + HAVING COUNT(DISTINCT growth) > 1 OR COUNT(DISTINCT is_percent_growth) > 1 OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 82 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 82) WITH NOWAIT; + + EXEC sp_MSforeachdb 'use [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, Details) + SELECT DISTINCT 82 AS CheckID, + N''?'' as DatabaseName, + 170 AS Priority, + ''File Configuration'' AS FindingsGroup, + ''File growth set to percent'', + ''https://www.brentozar.com/go/percentgrowth'' AS URL, + ''The ['' + DB_NAME() + ''] database file '' + f.physical_name + '' has grown to '' + CONVERT(NVARCHAR(10), CONVERT(NUMERIC(38, 2), (f.size / 128.) / 1024.)) + '' GB, and is using percent filegrowth settings. This can lead to slow performance during growths if Instant File Initialization is not enabled.'' + FROM [?].sys.database_files f + WHERE is_percent_growth = 1 and size > 128000 OPTION (RECOMPILE);'; + END; + + /* addition by Henrik Staun Poulsen, Stovi Software */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 158 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 158) WITH NOWAIT; + + EXEC sp_MSforeachdb 'use [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, Details) + SELECT DISTINCT 158 AS CheckID, + N''?'' as DatabaseName, + 170 AS Priority, + ''File Configuration'' AS FindingsGroup, + ''File growth set to 1MB'', + ''https://www.brentozar.com/go/percentgrowth'' AS URL, + ''The ['' + DB_NAME() + ''] database file '' + f.physical_name + '' is using 1MB filegrowth settings, but it has grown to '' + CAST((CAST(f.size AS BIGINT) * 8 / 1000000) AS NVARCHAR(10)) + '' GB. Time to up the growth amount.'' + FROM [?].sys.database_files f + WHERE is_percent_growth = 0 and growth=128 and size > 128000 OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 33 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + AND @SkipBlockingChecks = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 33) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT 33, + db_name(), + 200, + ''Licensing'', + ''Enterprise Edition Features In Use'', + ''https://www.brentozar.com/go/ee'', + (''The ['' + DB_NAME() + ''] database is using '' + feature_name + ''. If this database is restored onto a Standard Edition server, the restore will fail on versions prior to 2016 SP1.'') + FROM [?].sys.dm_db_persisted_sku_features OPTION (RECOMPILE);'; + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 19 ) + BEGIN + /* Method 1: Check sys.databases parameters */ + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 19) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + + SELECT 19 AS CheckID , + [name] AS DatabaseName , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Replication In Use' AS Finding , + 'https://www.brentozar.com/go/repl' AS URL , + ( 'Database [' + [name] + + '] is a replication publisher, subscriber, or distributor.' ) AS Details + FROM sys.databases + WHERE name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 19) + AND (is_published = 1 + OR is_subscribed = 1 + OR is_merge_published = 1 + OR is_distributor = 1); + + /* Method B: check subscribers for MSreplication_objects tables */ + EXEC dbo.sp_MSforeachdb 'USE [?]; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT 19, + db_name(), + 200, + ''Informational'', + ''Replication In Use'', + ''https://www.brentozar.com/go/repl'', + (''['' + DB_NAME() + ''] has MSreplication_objects tables in it, indicating it is a replication subscriber.'') + FROM [?].sys.tables + WHERE name = ''dbo.MSreplication_objects'' AND ''?'' <> ''master'' OPTION (RECOMPILE)'; + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 32 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 32) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 32, + N''?'', + 150, + ''Performance'', + ''Triggers on Tables'', + ''https://www.brentozar.com/go/trig'', + (''The ['' + DB_NAME() + ''] database has '' + CAST(SUM(1) AS NVARCHAR(50)) + '' triggers.'') + FROM [?].sys.triggers t INNER JOIN [?].sys.objects o ON t.parent_id = o.object_id + INNER JOIN [?].sys.schemas s ON o.schema_id = s.schema_id WHERE t.is_ms_shipped = 0 AND DB_NAME() != ''ReportServer'' + HAVING SUM(1) > 0 OPTION (RECOMPILE)'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 164 ) + AND EXISTS(SELECT * FROM sys.all_objects WHERE name = 'fn_validate_plan_guide') + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 164) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SET QUOTED_IDENTIFIER ON; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT 164, + N''?'', + 100, + ''Reliability'', + ''Plan Guides Failing'', + ''https://www.brentozar.com/go/misguided'', + (''The ['' + DB_NAME() + ''] database has plan guides that are no longer valid, so the queries involved may be failing silently.'') + FROM [?].sys.plan_guides g CROSS APPLY fn_validate_plan_guide(g.plan_guide_id) OPTION (RECOMPILE)'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 46 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 46) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 46, + N''?'', + 150, + ''Performance'', + ''Leftover Fake Indexes From Wizards'', + ''https://www.brentozar.com/go/hypo'', + (''The index ['' + DB_NAME() + ''].['' + s.name + ''].['' + o.name + ''].['' + i.name + ''] is a leftover hypothetical index from the Index Tuning Wizard or Database Tuning Advisor. This index is not actually helping performance and should be removed.'') + from [?].sys.indexes i INNER JOIN [?].sys.objects o ON i.object_id = o.object_id INNER JOIN [?].sys.schemas s ON o.schema_id = s.schema_id + WHERE i.is_hypothetical = 1 OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 47 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 47) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 47, + N''?'', + 100, + ''Performance'', + ''Indexes Disabled'', + ''https://www.brentozar.com/go/ixoff'', + (''The index ['' + DB_NAME() + ''].['' + s.name + ''].['' + o.name + ''].['' + i.name + ''] is disabled. This index is not actually helping performance and should either be enabled or removed.'') + from [?].sys.indexes i INNER JOIN [?].sys.objects o ON i.object_id = o.object_id INNER JOIN [?].sys.schemas s ON o.schema_id = s.schema_id + WHERE i.is_disabled = 1 OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 48 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 48) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT 48, + N''?'', + 150, + ''Performance'', + ''Foreign Keys Not Trusted'', + ''https://www.brentozar.com/go/trust'', + (''The ['' + DB_NAME() + ''] database has foreign keys that were probably disabled, data was changed, and then the key was enabled again. Simply enabling the key is not enough for the optimizer to use this key - we have to alter the table using the WITH CHECK CHECK CONSTRAINT parameter.'') + from [?].sys.foreign_keys i INNER JOIN [?].sys.objects o ON i.parent_object_id = o.object_id INNER JOIN [?].sys.schemas s ON o.schema_id = s.schema_id + WHERE i.is_not_trusted = 1 AND i.is_not_for_replication = 0 AND i.is_disabled = 0 AND N''?'' NOT IN (''master'', ''model'', ''msdb'', ''ReportServer'', ''ReportServerTempDB'') OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 56 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 56) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 56, + N''?'', + 150, + ''Performance'', + ''Check Constraint Not Trusted'', + ''https://www.brentozar.com/go/trust'', + (''The check constraint ['' + DB_NAME() + ''].['' + s.name + ''].['' + o.name + ''].['' + i.name + ''] is not trusted - meaning, it was disabled, data was changed, and then the constraint was enabled again. Simply enabling the constraint is not enough for the optimizer to use this constraint - we have to alter the table using the WITH CHECK CHECK CONSTRAINT parameter.'') + from [?].sys.check_constraints i INNER JOIN [?].sys.objects o ON i.parent_object_id = o.object_id + INNER JOIN [?].sys.schemas s ON o.schema_id = s.schema_id + WHERE i.is_not_trusted = 1 AND i.is_not_for_replication = 0 AND i.is_disabled = 0 OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 95 ) + BEGIN + IF @@VERSION NOT LIKE '%Microsoft SQL Server 2000%' + AND @@VERSION NOT LIKE '%Microsoft SQL Server 2005%' + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 95) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT TOP 1 95 AS CheckID, + N''?'' as DatabaseName, + 110 AS Priority, + ''Performance'' AS FindingsGroup, + ''Plan Guides Enabled'' AS Finding, + ''https://www.brentozar.com/go/guides'' AS URL, + (''Database ['' + DB_NAME() + ''] has query plan guides so a query will always get a specific execution plan. If you are having trouble getting query performance to improve, it might be due to a frozen plan. Review the DMV sys.plan_guides to learn more about the plan guides in place on this server.'') AS Details + FROM [?].sys.plan_guides WHERE is_disabled = 0 OPTION (RECOMPILE);'; + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 60 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 60) WITH NOWAIT; + + EXEC sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 60 AS CheckID, + N''?'' as DatabaseName, + 100 AS Priority, + ''Performance'' AS FindingsGroup, + ''Fill Factor Changed'', + ''https://www.brentozar.com/go/fillfactor'' AS URL, + ''The ['' + DB_NAME() + ''] database has '' + CAST(SUM(1) AS NVARCHAR(50)) + '' objects with fill factor = '' + CAST(fill_factor AS NVARCHAR(5)) + ''%. This can cause memory and storage performance problems, but may also prevent page splits.'' + FROM [?].sys.indexes + WHERE fill_factor <> 0 AND fill_factor < 80 AND is_disabled = 0 AND is_hypothetical = 0 + GROUP BY fill_factor OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 78 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 78) WITH NOWAIT; + + EXECUTE master.sys.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #Recompile + SELECT DISTINCT DBName = DB_Name(), SPName = SO.name, SM.is_recompiled, ISR.SPECIFIC_SCHEMA + FROM sys.sql_modules AS SM + LEFT OUTER JOIN master.sys.databases AS sDB ON SM.object_id = DB_id() + LEFT OUTER JOIN dbo.sysobjects AS SO ON SM.object_id = SO.id and type = ''P'' + LEFT OUTER JOIN INFORMATION_SCHEMA.ROUTINES AS ISR on ISR.Routine_Name = SO.name AND ISR.SPECIFIC_CATALOG = DB_Name() + WHERE SM.is_recompiled=1 OPTION (RECOMPILE); /* oh the rich irony of recompile here */ + '; + INSERT INTO #BlitzResults + (Priority, + FindingsGroup, + Finding, + DatabaseName, + URL, + Details, + CheckID) + SELECT [Priority] = '100', + FindingsGroup = 'Performance', + Finding = 'Stored Procedure WITH RECOMPILE', + DatabaseName = DBName, + URL = 'https://www.brentozar.com/go/recompile', + Details = '[' + DBName + '].[' + SPSchema + '].[' + ProcName + '] has WITH RECOMPILE in the stored procedure code, which may cause increased CPU usage due to constant recompiles of the code.', + CheckID = '78' + FROM #Recompile AS TR + WHERE ProcName NOT LIKE 'sp_AllNightLog%' AND ProcName NOT LIKE 'sp_AskBrent%' AND ProcName NOT LIKE 'sp_Blitz%' AND ProcName NOT LIKE 'sp_PressureDetector' + AND DBName NOT IN ('master', 'model', 'msdb', 'tempdb'); + DROP TABLE #Recompile; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 86 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 86) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) SELECT DISTINCT 86, DB_NAME(), 230, ''Security'', ''Elevated Permissions on a Database'', ''https://www.brentozar.com/go/elevated'', (''In ['' + DB_NAME() + ''], user ['' + u.name + ''] has the role ['' + g.name + '']. This user can perform tasks beyond just reading and writing data.'') FROM (SELECT memberuid = convert(int, member_principal_id), groupuid = convert(int, role_principal_id) FROM [?].sys.database_role_members) m inner join [?].dbo.sysusers u on m.memberuid = u.uid inner join sysusers g on m.groupuid = g.uid where u.name <> ''dbo'' and g.name in (''db_owner'' , ''db_accessadmin'' , ''db_securityadmin'' , ''db_ddladmin'') OPTION (RECOMPILE);'; + END; + + /*Check for non-aligned indexes in partioned databases*/ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 72 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 72) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + insert into #partdb(dbname, objectname, type_desc) + SELECT distinct db_name(DB_ID()) as DBName,o.name Object_Name,ds.type_desc + FROM sys.objects AS o JOIN sys.indexes AS i ON o.object_id = i.object_id + JOIN sys.data_spaces ds on ds.data_space_id = i.data_space_id + LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s ON i.object_id = s.object_id AND i.index_id = s.index_id AND s.database_id = DB_ID() + WHERE o.type = ''u'' + -- Clustered and Non-Clustered indexes + AND i.type IN (1, 2) + AND o.object_id in + ( + SELECT a.object_id from + (SELECT ob.object_id, ds.type_desc from sys.objects ob JOIN sys.indexes ind on ind.object_id = ob.object_id join sys.data_spaces ds on ds.data_space_id = ind.data_space_id + GROUP BY ob.object_id, ds.type_desc ) a group by a.object_id having COUNT (*) > 1 + ) OPTION (RECOMPILE);'; + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT DISTINCT + 72 AS CheckID , + dbname AS DatabaseName , + 100 AS Priority , + 'Performance' AS FindingsGroup , + 'The partitioned database ' + dbname + + ' may have non-aligned indexes' AS Finding , + 'https://www.brentozar.com/go/aligned' AS URL , + 'Having non-aligned indexes on partitioned tables may cause inefficient query plans and CPU pressure' AS Details + FROM #partdb + WHERE dbname IS NOT NULL + AND dbname NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 72); + DROP TABLE #partdb; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 113 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 113) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT DISTINCT 113, + N''?'', + 50, + ''Reliability'', + ''Full Text Indexes Not Updating'', + ''https://www.brentozar.com/go/fulltext'', + (''At least one full text index in this database has not been crawled in the last week.'') + from [?].sys.fulltext_indexes i WHERE change_tracking_state_desc <> ''AUTO'' AND i.is_enabled = 1 AND i.crawl_end_date < DATEADD(dd, -7, GETDATE()) OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 115 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 115) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 115, + N''?'', + 110, + ''Performance'', + ''Parallelism Rocket Surgery'', + ''https://www.brentozar.com/go/makeparallel'', + (''['' + DB_NAME() + ''] has a make_parallel function, indicating that an advanced developer may be manhandling SQL Server into forcing queries to go parallel.'') + from [?].INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = ''make_parallel'' AND ROUTINE_TYPE = ''FUNCTION'' OPTION (RECOMPILE);'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 122 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 122) WITH NOWAIT; + + /* SQL Server 2012 and newer uses temporary stats for Availability Groups, and those show up as user-created */ + IF EXISTS (SELECT * + FROM sys.all_columns c + INNER JOIN sys.all_objects o ON c.object_id = o.object_id + WHERE c.name = 'is_temporary' AND o.name = 'stats') + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT TOP 1 122, + N''?'', + 200, + ''Performance'', + ''User-Created Statistics In Place'', + ''https://www.brentozar.com/go/userstats'', + (''['' + DB_NAME() + ''] has '' + CAST(SUM(1) AS NVARCHAR(10)) + '' user-created statistics. This indicates that someone is being a rocket scientist with the stats, and might actually be slowing things down, especially during stats updates.'') + from [?].sys.stats WHERE user_created = 1 AND is_temporary = 0 + HAVING SUM(1) > 0 OPTION (RECOMPILE);'; + + ELSE + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults + (CheckID, + DatabaseName, + Priority, + FindingsGroup, + Finding, + URL, + Details) + SELECT 122, + N''?'', + 200, + ''Performance'', + ''User-Created Statistics In Place'', + ''https://www.brentozar.com/go/userstats'', + (''['' + DB_NAME() + ''] has '' + CAST(SUM(1) AS NVARCHAR(10)) + '' user-created statistics. This indicates that someone is being a rocket scientist with the stats, and might actually be slowing things down, especially during stats updates.'') + from [?].sys.stats WHERE user_created = 1 + HAVING SUM(1) > 0 OPTION (RECOMPILE);'; + + END; /* IF NOT EXISTS ( SELECT 1 */ + + /*Check for high VLF count: this will omit any database snapshots*/ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 69 ) + BEGIN + IF @ProductVersionMajor >= 11 + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d] (2012 version of Log Info).', 0, 1, 69) WITH NOWAIT; + + EXEC sp_MSforeachdb N'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #LogInfo2012 + EXEC sp_executesql N''DBCC LogInfo() WITH NO_INFOMSGS''; + IF @@ROWCOUNT > 999 + BEGIN + INSERT INTO #BlitzResults + ( CheckID + ,DatabaseName + ,Priority + ,FindingsGroup + ,Finding + ,URL + ,Details) + SELECT 69 + ,DB_NAME() + ,170 + ,''File Configuration'' + ,''High VLF Count'' + ,''https://www.brentozar.com/go/vlf'' + ,''The ['' + DB_NAME() + ''] database has '' + CAST(COUNT(*) as VARCHAR(20)) + '' virtual log files (VLFs). This may be slowing down startup, restores, and even inserts/updates/deletes.'' + FROM #LogInfo2012 + WHERE EXISTS (SELECT name FROM master.sys.databases + WHERE source_database_id is null) OPTION (RECOMPILE); + END + TRUNCATE TABLE #LogInfo2012;'; + DROP TABLE #LogInfo2012; + END; + ELSE + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d] (pre-2012 version of Log Info).', 0, 1, 69) WITH NOWAIT; + + EXEC sp_MSforeachdb N'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #LogInfo + EXEC sp_executesql N''DBCC LogInfo() WITH NO_INFOMSGS''; + IF @@ROWCOUNT > 999 + BEGIN + INSERT INTO #BlitzResults + ( CheckID + ,DatabaseName + ,Priority + ,FindingsGroup + ,Finding + ,URL + ,Details) + SELECT 69 + ,DB_NAME() + ,170 + ,''File Configuration'' + ,''High VLF Count'' + ,''https://www.brentozar.com/go/vlf'' + ,''The ['' + DB_NAME() + ''] database has '' + CAST(COUNT(*) as VARCHAR(20)) + '' virtual log files (VLFs). This may be slowing down startup, restores, and even inserts/updates/deletes.'' + FROM #LogInfo + WHERE EXISTS (SELECT name FROM master.sys.databases + WHERE source_database_id is null) OPTION (RECOMPILE); + END + TRUNCATE TABLE #LogInfo;'; + DROP TABLE #LogInfo; + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 80 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 80) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + SELECT DISTINCT 80, DB_NAME(), 170, ''Reliability'', ''Max File Size Set'', ''https://www.brentozar.com/go/maxsize'', + (''The ['' + DB_NAME() + ''] database file '' + df.name + '' has a max file size set to '' + + CAST(CAST(df.max_size AS BIGINT) * 8 / 1024 AS VARCHAR(100)) + + ''MB. If it runs out of space, the database will stop working even though there may be drive space available.'') + FROM sys.database_files df + WHERE 0 = (SELECT is_read_only FROM sys.databases WHERE name = ''?'') + AND df.max_size <> 268435456 + AND df.max_size <> -1 + AND df.type <> 2 + AND df.growth > 0 + AND df.name <> ''DWDiagnostics'' OPTION (RECOMPILE);'; + + DELETE br + FROM #BlitzResults br + INNER JOIN #SkipChecks sc ON sc.CheckID = 80 AND br.DatabaseName = sc.DatabaseName; + END; + + + /* Check if columnstore indexes are in use - for Github issue #615 */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 74 ) /* Trace flags */ + BEGIN + TRUNCATE TABLE #TemporaryDatabaseResults; + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 74) WITH NOWAIT; + + EXEC dbo.sp_MSforeachdb 'USE [?]; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; IF EXISTS(SELECT * FROM sys.indexes WHERE type IN (5,6)) INSERT INTO #TemporaryDatabaseResults (DatabaseName, Finding) VALUES (DB_NAME(), ''Yup'') OPTION (RECOMPILE);'; + IF EXISTS (SELECT * FROM #TemporaryDatabaseResults) SET @ColumnStoreIndexesInUse = 1; + END; + + /* Non-Default Database Scoped Config - Github issue #598 */ + IF EXISTS ( SELECT * FROM sys.all_objects WHERE [name] = 'database_scoped_configurations' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d] through [%d] and [%d] through [%d].', 0, 1, 194, 197, 237, 255) WITH NOWAIT; + + INSERT INTO #DatabaseScopedConfigurationDefaults (configuration_id, [name], default_value, default_value_for_secondary, CheckID) + SELECT 1, 'MAXDOP', '0', NULL, 194 + UNION ALL + SELECT 2, 'LEGACY_CARDINALITY_ESTIMATION', '0', NULL, 195 + UNION ALL + SELECT 3, 'PARAMETER_SNIFFING', '1', NULL, 196 + UNION ALL + SELECT 4, 'QUERY_OPTIMIZER_HOTFIXES', '0', NULL, 197 + UNION ALL + SELECT 6, 'IDENTITY_CACHE', '1', NULL, 237 + UNION ALL + SELECT 7, 'INTERLEAVED_EXECUTION_TVF', '1', NULL, 238 + UNION ALL + SELECT 8, 'BATCH_MODE_MEMORY_GRANT_FEEDBACK', '1', NULL, 239 + UNION ALL + SELECT 9, 'BATCH_MODE_ADAPTIVE_JOINS', '1', NULL, 240 + UNION ALL + SELECT 10, 'TSQL_SCALAR_UDF_INLINING', '1', NULL, 241 + UNION ALL + SELECT 11, 'ELEVATE_ONLINE', 'OFF', NULL, 242 + UNION ALL + SELECT 12, 'ELEVATE_RESUMABLE', 'OFF', NULL, 243 + UNION ALL + SELECT 13, 'OPTIMIZE_FOR_AD_HOC_WORKLOADS', '0', NULL, 244 + UNION ALL + SELECT 14, 'XTP_PROCEDURE_EXECUTION_STATISTICS', '0', NULL, 245 + UNION ALL + SELECT 15, 'XTP_QUERY_EXECUTION_STATISTICS', '0', NULL, 246 + UNION ALL + SELECT 16, 'ROW_MODE_MEMORY_GRANT_FEEDBACK', '1', NULL, 247 + UNION ALL + SELECT 17, 'ISOLATE_SECURITY_POLICY_CARDINALITY', '0', NULL, 248 + UNION ALL + SELECT 18, 'BATCH_MODE_ON_ROWSTORE', '1', NULL, 249 + UNION ALL + SELECT 19, 'DEFERRED_COMPILATION_TV', '1', NULL, 250 + UNION ALL + SELECT 20, 'ACCELERATED_PLAN_FORCING', '1', NULL, 251 + UNION ALL + SELECT 21, 'GLOBAL_TEMPORARY_TABLE_AUTO_DROP', '1', NULL, 252 + UNION ALL + SELECT 22, 'LIGHTWEIGHT_QUERY_PROFILING', '1', NULL, 253 + UNION ALL + SELECT 23, 'VERBOSE_TRUNCATION_WARNINGS', '1', NULL, 254 + UNION ALL + SELECT 24, 'LAST_QUERY_PLAN_STATS', '0', NULL, 255; + EXEC dbo.sp_MSforeachdb 'USE [?]; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + SELECT def1.CheckID, DB_NAME(), 210, ''Non-Default Database Scoped Config'', dsc.[name], ''https://www.brentozar.com/go/dbscope'', (''Set value: '' + COALESCE(CAST(dsc.value AS NVARCHAR(100)),''Empty'') + '' Default: '' + COALESCE(CAST(def1.default_value AS NVARCHAR(100)),''Empty'') + '' Set value for secondary: '' + COALESCE(CAST(dsc.value_for_secondary AS NVARCHAR(100)),''Empty'') + '' Default value for secondary: '' + COALESCE(CAST(def1.default_value_for_secondary AS NVARCHAR(100)),''Empty'')) + FROM [?].sys.database_scoped_configurations dsc + INNER JOIN #DatabaseScopedConfigurationDefaults def1 ON dsc.configuration_id = def1.configuration_id + LEFT OUTER JOIN #DatabaseScopedConfigurationDefaults def ON dsc.configuration_id = def.configuration_id AND (cast(dsc.value as nvarchar(100)) = cast(def.default_value as nvarchar(100)) OR dsc.value IS NULL) AND (dsc.value_for_secondary = def.default_value_for_secondary OR dsc.value_for_secondary IS NULL) + LEFT OUTER JOIN #SkipChecks sk ON (sk.CheckID IS NULL OR def.CheckID = sk.CheckID) AND (sk.DatabaseName IS NULL OR sk.DatabaseName = DB_NAME()) + WHERE def.configuration_id IS NULL AND sk.CheckID IS NULL ORDER BY 1 + OPTION (RECOMPILE);'; + END; + + /* Check 218 - Show me the dodgy SET Options */ + IF NOT EXISTS ( + SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL + AND CheckID = 218 + ) + BEGIN + IF @Debug IN (1,2) + BEGIN + RAISERROR ('Running CheckId [%d].',0,1,218) WITH NOWAIT; + END + + EXECUTE sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + SELECT 218 AS CheckID + ,''?'' AS DatabaseName + ,150 AS Priority + ,''Performance'' AS FindingsGroup + ,''Objects created with dangerous SET Options'' AS Finding + ,''https://www.brentozar.com/go/badset'' AS URL + ,''The '' + QUOTENAME(DB_NAME()) + + '' database has '' + CONVERT(VARCHAR(20),COUNT(1)) + + '' objects that were created with dangerous ANSI_NULL or QUOTED_IDENTIFIER options.'' + + '' These objects can break when using filtered indexes, indexed views'' + + '' and other advanced SQL features.'' AS Details + FROM sys.sql_modules sm + JOIN sys.objects o ON o.[object_id] = sm.[object_id] + AND ( + sm.uses_ansi_nulls <> 1 + OR sm.uses_quoted_identifier <> 1 + ) + AND o.is_ms_shipped = 0 + HAVING COUNT(1) > 0;'; + END; --of Check 218. + + /* Check 225 - Reliability - Resumable Index Operation Paused */ + IF NOT EXISTS ( + SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL + AND CheckID = 225 + ) + AND EXISTS (SELECT * FROM sys.all_objects WHERE name = 'index_resumable_operations') + BEGIN + IF @Debug IN (1,2) + BEGIN + RAISERROR ('Running CheckId [%d].',0,1,218) WITH NOWAIT; + END + + EXECUTE sp_MSforeachdb 'USE [?]; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + SELECT 225 AS CheckID + ,''?'' AS DatabaseName + ,200 AS Priority + ,''Reliability'' AS FindingsGroup + ,''Resumable Index Operation Paused'' AS Finding + ,''https://www.brentozar.com/go/resumable'' AS URL + ,iro.state_desc + N'' since '' + CONVERT(NVARCHAR(50), last_pause_time, 120) + '', '' + + CAST(iro.percent_complete AS NVARCHAR(20)) + ''% complete: '' + + CAST(iro.sql_text AS NVARCHAR(1000)) AS Details + FROM sys.index_resumable_operations iro + JOIN sys.objects o ON iro.[object_id] = o.[object_id] + WHERE iro.state <> 0;'; + END; --of Check 225. + + --/* Check 220 - Statistics Without Histograms */ + --IF NOT EXISTS ( + -- SELECT 1 + -- FROM #SkipChecks + -- WHERE DatabaseName IS NULL + -- AND CheckID = 220 + -- ) + -- AND EXISTS (SELECT * FROM sys.all_objects WHERE name = 'dm_db_stats_histogram') + --BEGIN + -- IF @Debug IN (1,2) + -- BEGIN + -- RAISERROR ('Running CheckId [%d].',0,1,220) WITH NOWAIT; + -- END + + -- EXECUTE sp_MSforeachdb 'USE [?]; + -- SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + -- INSERT INTO #BlitzResults (CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details) + -- SELECT 220 AS CheckID + -- ,DB_NAME() AS DatabaseName + -- ,110 AS Priority + -- ,''Performance'' AS FindingsGroup + -- ,''Statistics Without Histograms'' AS Finding + -- ,''https://www.brentozar.com/go/brokenstats'' AS URL + -- ,CAST(COUNT(DISTINCT o.object_id) AS VARCHAR(100)) + '' tables have statistics that have not been updated since the database was restored or upgraded,'' + -- + '' and have no data in their histogram. See the More Info URL for a script to update them. '' AS Details + -- FROM sys.all_objects o + -- INNER JOIN sys.stats s ON o.object_id = s.object_id AND s.has_filter = 0 + -- OUTER APPLY sys.dm_db_stats_histogram(o.object_id, s.stats_id) h + -- WHERE o.is_ms_shipped = 0 AND o.type_desc = ''USER_TABLE'' + -- AND h.object_id IS NULL + -- AND 0 < (SELECT SUM(row_count) FROM sys.dm_db_partition_stats ps WHERE ps.object_id = o.object_id) + -- AND ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'') + -- HAVING COUNT(DISTINCT o.object_id) > 0;'; + --END; --of Check 220. + + /*Check for the last good DBCC CHECKDB date */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 68 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 68) WITH NOWAIT; + + /* Removed as populating the #DBCCs table now done in advance as data is uses for multiple checks*/ + --EXEC sp_MSforeachdb N'USE [?]; + --SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + --INSERT #DBCCs + -- (ParentObject, + -- Object, + -- Field, + -- Value) + --EXEC (''DBCC DBInfo() With TableResults, NO_INFOMSGS''); + --UPDATE #DBCCs SET DbName = N''?'' WHERE DbName IS NULL OPTION (RECOMPILE);'; + + WITH DB2 + AS ( SELECT DISTINCT + Field , + Value , + DbName + FROM #DBCCs + INNER JOIN sys.databases d ON #DBCCs.DbName = d.name + WHERE Field = 'dbi_dbccLastKnownGood' + AND d.create_date < DATEADD(dd, -14, GETDATE()) + ) + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 68 AS CheckID , + DB2.DbName AS DatabaseName , + 1 AS PRIORITY , + 'Reliability' AS FindingsGroup , + 'Last good DBCC CHECKDB over 2 weeks old' AS Finding , + 'https://www.brentozar.com/go/checkdb' AS URL , + 'Last successful CHECKDB: ' + + CASE DB2.Value + WHEN '1900-01-01 00:00:00.000' + THEN ' never.' + ELSE DB2.Value + END AS Details + FROM DB2 + WHERE DB2.DbName <> 'tempdb' + AND DB2.DbName NOT IN ( SELECT DISTINCT + DatabaseName + FROM + #SkipChecks + WHERE CheckID IS NULL OR CheckID = 68) + AND DB2.DbName NOT IN ( SELECT name + FROM sys.databases + WHERE is_read_only = 1) + AND CONVERT(DATETIME, DB2.Value, 121) < DATEADD(DD, + -14, + CURRENT_TIMESTAMP); + END; + + END; /* IF @CheckUserDatabaseObjects = 1 */ + + IF @CheckProcedureCache = 1 + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Begin checking procedure cache', 0, 1) WITH NOWAIT; + + BEGIN + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 35 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 35) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 35 AS CheckID , + 100 AS Priority , + 'Performance' AS FindingsGroup , + 'Single-Use Plans in Procedure Cache' AS Finding , + 'https://www.brentozar.com/go/single' AS URL , + ( CAST(COUNT(*) AS VARCHAR(10)) + + ' query plans are taking up memory in the procedure cache. This may be wasted memory if we cache plans for queries that never get called again. This may be a good use case for SQL Server 2008''s Optimize for Ad Hoc or for Forced Parameterization.' ) AS Details + FROM sys.dm_exec_cached_plans AS cp + WHERE cp.usecounts = 1 + AND cp.objtype = 'Adhoc' + AND EXISTS ( SELECT + 1 + FROM sys.configurations + WHERE + name = 'optimize for ad hoc workloads' + AND value_in_use = 0 ) + HAVING COUNT(*) > 1; + END; + + /* Set up the cache tables. Different on 2005 since it doesn't support query_hash, query_plan_hash. */ + IF @@VERSION LIKE '%Microsoft SQL Server 2005%' + BEGIN + IF @CheckProcedureCacheFilter = 'CPU' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.total_worker_time DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + IF @CheckProcedureCacheFilter = 'Reads' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.total_logical_reads DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + IF @CheckProcedureCacheFilter = 'ExecCount' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.execution_count DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + IF @CheckProcedureCacheFilter = 'Duration' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.total_elapsed_time DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + END; + IF @ProductVersionMajor >= 10 + BEGIN + IF @CheckProcedureCacheFilter = 'CPU' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.total_worker_time DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + IF @CheckProcedureCacheFilter = 'Reads' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.total_logical_reads DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + IF @CheckProcedureCacheFilter = 'ExecCount' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.execution_count DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + IF @CheckProcedureCacheFilter = 'Duration' + OR @CheckProcedureCacheFilter IS NULL + BEGIN + SET @StringToExecute = 'WITH queries ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + AS (SELECT TOP 20 qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM sys.dm_exec_query_stats qs + ORDER BY qs.total_elapsed_time DESC) + INSERT INTO #dm_exec_query_stats ([sql_handle],[statement_start_offset],[statement_end_offset],[plan_generation_num],[plan_handle],[creation_time],[last_execution_time],[execution_count],[total_worker_time],[last_worker_time],[min_worker_time],[max_worker_time],[total_physical_reads],[last_physical_reads],[min_physical_reads],[max_physical_reads],[total_logical_writes],[last_logical_writes],[min_logical_writes],[max_logical_writes],[total_logical_reads],[last_logical_reads],[min_logical_reads],[max_logical_reads],[total_clr_time],[last_clr_time],[min_clr_time],[max_clr_time],[total_elapsed_time],[last_elapsed_time],[min_elapsed_time],[max_elapsed_time],[query_hash],[query_plan_hash]) + SELECT qs.[sql_handle],qs.[statement_start_offset],qs.[statement_end_offset],qs.[plan_generation_num],qs.[plan_handle],qs.[creation_time],qs.[last_execution_time],qs.[execution_count],qs.[total_worker_time],qs.[last_worker_time],qs.[min_worker_time],qs.[max_worker_time],qs.[total_physical_reads],qs.[last_physical_reads],qs.[min_physical_reads],qs.[max_physical_reads],qs.[total_logical_writes],qs.[last_logical_writes],qs.[min_logical_writes],qs.[max_logical_writes],qs.[total_logical_reads],qs.[last_logical_reads],qs.[min_logical_reads],qs.[max_logical_reads],qs.[total_clr_time],qs.[last_clr_time],qs.[min_clr_time],qs.[max_clr_time],qs.[total_elapsed_time],qs.[last_elapsed_time],qs.[min_elapsed_time],qs.[max_elapsed_time],qs.[query_hash],qs.[query_plan_hash] + FROM queries qs + LEFT OUTER JOIN #dm_exec_query_stats qsCaught ON qs.sql_handle = qsCaught.sql_handle AND qs.plan_handle = qsCaught.plan_handle AND qs.statement_start_offset = qsCaught.statement_start_offset + WHERE qsCaught.sql_handle IS NULL OPTION (RECOMPILE);'; + EXECUTE(@StringToExecute); + END; + + /* Populate the query_plan_filtered field. Only works in 2005SP2+, but we're just doing it in 2008 to be safe. */ + UPDATE #dm_exec_query_stats + SET query_plan_filtered = qp.query_plan + FROM #dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_text_query_plan(qs.plan_handle, + qs.statement_start_offset, + qs.statement_end_offset) + AS qp; + + END; + + /* Populate the additional query_plan, text, and text_filtered fields */ + UPDATE #dm_exec_query_stats + SET query_plan = qp.query_plan , + [text] = st.[text] , + text_filtered = SUBSTRING(st.text, + ( qs.statement_start_offset + / 2 ) + 1, + ( ( CASE qs.statement_end_offset + WHEN -1 + THEN DATALENGTH(st.text) + ELSE qs.statement_end_offset + END + - qs.statement_start_offset ) + / 2 ) + 1) + FROM #dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st + CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) + AS qp; + + /* Dump instances of our own script. We're not trying to tune ourselves. */ + DELETE #dm_exec_query_stats + WHERE text LIKE '%sp_Blitz%' + OR text LIKE '%#BlitzResults%'; + + /* Look for implicit conversions */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 63 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 63) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details , + QueryPlan , + QueryPlanFiltered + ) + SELECT 63 AS CheckID , + 120 AS Priority , + 'Query Plans' AS FindingsGroup , + 'Implicit Conversion' AS Finding , + 'https://www.brentozar.com/go/implicit' AS URL , + ( 'One of the top resource-intensive queries is comparing two fields that are not the same datatype.' ) AS Details , + qs.query_plan , + qs.query_plan_filtered + FROM #dm_exec_query_stats qs + WHERE COALESCE(qs.query_plan_filtered, + CAST(qs.query_plan AS NVARCHAR(MAX))) LIKE '%CONVERT_IMPLICIT%' + AND COALESCE(qs.query_plan_filtered, + CAST(qs.query_plan AS NVARCHAR(MAX))) LIKE '%PhysicalOp="Index Scan"%'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 64 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 64) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details , + QueryPlan , + QueryPlanFiltered + ) + SELECT 64 AS CheckID , + 120 AS Priority , + 'Query Plans' AS FindingsGroup , + 'Implicit Conversion Affecting Cardinality' AS Finding , + 'https://www.brentozar.com/go/implicit' AS URL , + ( 'One of the top resource-intensive queries has an implicit conversion that is affecting cardinality estimation.' ) AS Details , + qs.query_plan , + qs.query_plan_filtered + FROM #dm_exec_query_stats qs + WHERE COALESCE(qs.query_plan_filtered, + CAST(qs.query_plan AS NVARCHAR(MAX))) LIKE '%= 10 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 187 ) + + IF SERVERPROPERTY('IsHadrEnabled') = 1 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 187) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT + 187 AS [CheckID] , + 230 AS [Priority] , + 'Security' AS [FindingsGroup] , + 'Endpoints Owned by Users' AS [Finding] , + 'https://www.brentozar.com/go/owners' AS [URL] , + ( 'Endpoint ' + ep.[name] + ' is owned by ' + SUSER_NAME(ep.principal_id) + '. If the endpoint owner login is disabled or not available due to Active Directory problems, the high availability will stop working.' + ) AS [Details] + FROM sys.database_mirroring_endpoints ep + LEFT OUTER JOIN sys.dm_server_services s ON SUSER_NAME(ep.principal_id) = s.service_account + WHERE s.service_account IS NULL AND ep.principal_id <> 1; + END; + + + /*Verify that the servername is set */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 70 ) + BEGIN + IF @@SERVERNAME IS NULL + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 70) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 70 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + '@@Servername Not Set' AS Finding , + 'https://www.brentozar.com/go/servername' AS URL , + '@@Servername variable is null. You can fix it by executing: "sp_addserver '''', local"' AS Details; + END; + + IF /* @@SERVERNAME IS set */ + (@@SERVERNAME IS NOT NULL + AND + /* not a named instance */ + CHARINDEX(CHAR(92),CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))) = 0 + AND + /* not clustered, when computername may be different than the servername */ + SERVERPROPERTY('IsClustered') = 0 + AND + /* @@SERVERNAME is different than the computer name */ + @@SERVERNAME <> CAST(ISNULL(COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),SERVERPROPERTY('ServerName')),@@SERVERNAME) AS NVARCHAR(128)) ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 70) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 70 AS CheckID , + 200 AS Priority , + 'Configuration' AS FindingsGroup , + '@@Servername Not Correct' AS Finding , + 'https://www.brentozar.com/go/servername' AS URL , + 'The @@Servername is different than the computer name, which may trigger certificate errors.' AS Details; + END; + + END; + /*Check to see if a failsafe operator has been configured*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 73 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 73) WITH NOWAIT; + + DECLARE @AlertInfo TABLE + ( + FailSafeOperator NVARCHAR(255) , + NotificationMethod INT , + ForwardingServer NVARCHAR(255) , + ForwardingSeverity INT , + PagerToTemplate NVARCHAR(255) , + PagerCCTemplate NVARCHAR(255) , + PagerSubjectTemplate NVARCHAR(255) , + PagerSendSubjectOnly NVARCHAR(255) , + ForwardAlways INT + ); + INSERT INTO @AlertInfo + EXEC [master].[dbo].[sp_MSgetalertinfo] @includeaddresses = 0; + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 73 AS CheckID , + 200 AS Priority , + 'Monitoring' AS FindingsGroup , + 'No Failsafe Operator Configured' AS Finding , + 'https://www.brentozar.com/go/failsafe' AS URL , + ( 'No failsafe operator is configured on this server. This is a good idea just in-case there are issues with the [msdb] database that prevents alerting.' ) AS Details + FROM @AlertInfo + WHERE FailSafeOperator IS NULL; + END; + + /*Identify globally enabled trace flags*/ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 74 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 74) WITH NOWAIT; + + INSERT INTO #TraceStatus + EXEC ( ' DBCC TRACESTATUS(-1) WITH NO_INFOMSGS' + ); + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 74 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Trace Flag On' AS Finding , + CASE WHEN [T].[TraceFlag] = '834' AND @ColumnStoreIndexesInUse = 1 THEN 'https://support.microsoft.com/en-us/kb/3210239' + ELSE'https://www.BrentOzar.com/go/traceflags/' END AS URL , + 'Trace flag ' + + CASE WHEN [T].[TraceFlag] = '652' THEN '652 enabled globally, which disables pre-fetching during index scans. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '661' THEN '661 enabled globally, which disables ghost record removal, causing the database to grow in size. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '834' AND @ColumnStoreIndexesInUse = 1 THEN '834 is enabled globally, but you also have columnstore indexes. That combination is not recommended by Microsoft.' + WHEN [T].[TraceFlag] = '1117' THEN '1117 enabled globally, which grows all files in a filegroup at the same time.' + WHEN [T].[TraceFlag] = '1118' THEN '1118 enabled globally, which tries to reduce SGAM waits.' + WHEN [T].[TraceFlag] = '1211' THEN '1211 enabled globally, which disables lock escalation when you least expect it. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '1204' THEN '1204 enabled globally, which captures deadlock graphs in the error log.' + WHEN [T].[TraceFlag] = '1222' THEN '1222 enabled globally, which captures deadlock graphs in the error log.' + WHEN [T].[TraceFlag] = '1224' THEN '1224 enabled globally, which disables lock escalation until the server has memory pressure. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '1806' THEN '1806 enabled globally, which disables Instant File Initialization, causing restores and file growths to take longer. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '2330' THEN '2330 enabled globally, which disables missing index requests. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '2371' THEN '2371 enabled globally, which changes the auto update stats threshold.' + WHEN [T].[TraceFlag] = '3023' THEN '3023 enabled globally, which performs checksums by default when doing database backups.' + WHEN [T].[TraceFlag] = '3226' THEN '3226 enabled globally, which keeps the event log clean by not reporting successful backups.' + WHEN [T].[TraceFlag] = '3505' THEN '3505 enabled globally, which disables Checkpoints. This is usually a very bad idea.' + WHEN [T].[TraceFlag] = '4199' THEN '4199 enabled globally, which enables non-default Query Optimizer fixes, changing query plans from the default behaviors.' + WHEN [T].[TraceFlag] = '8048' THEN '8048 enabled globally, which tries to reduce CMEMTHREAD waits on servers with a lot of logical processors.' + WHEN [T].[TraceFlag] = '8017' AND (CAST(SERVERPROPERTY('Edition') AS NVARCHAR(1000)) LIKE N'%Express%') THEN '8017 is enabled globally, but this is the default for Express Edition.' + WHEN [T].[TraceFlag] = '8017' AND (CAST(SERVERPROPERTY('Edition') AS NVARCHAR(1000)) NOT LIKE N'%Express%') THEN '8017 is enabled globally, which disables the creation of schedulers for all logical processors.' + WHEN [T].[TraceFlag] = '8649' THEN '8649 enabled globally, which cost threshold for parallelism down to 0. This is usually a very bad idea.' + ELSE [T].[TraceFlag] + ' is enabled globally.' END + AS Details + FROM #TraceStatus T; + END; + + /* High CMEMTHREAD waits that could need trace flag 8048. + This check has to be run AFTER the globally enabled trace flag check, + since it uses the #TraceStatus table to know if flags are enabled. + */ + IF @ProductVersionMajor >= 11 AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 162 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 162) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 162 AS CheckID , + 50 AS Priority , + 'Performance' AS FindingGroup , + 'Poison Wait Detected: CMEMTHREAD & NUMA' AS Finding , + 'https://www.brentozar.com/go/poison' AS URL , + CONVERT(VARCHAR(10), (MAX([wait_time_ms]) / 1000) / 86400) + ':' + CONVERT(VARCHAR(20), DATEADD(s, (MAX([wait_time_ms]) / 1000), 0), 108) + ' of this wait have been recorded' + + CASE WHEN ts.status = 1 THEN ' despite enabling trace flag 8048 already.' + ELSE '. In servers with over 8 cores per NUMA node, when CMEMTHREAD waits are a bottleneck, trace flag 8048 may be needed.' + END + FROM sys.dm_os_nodes n + INNER JOIN sys.[dm_os_wait_stats] w ON w.wait_type = 'CMEMTHREAD' + LEFT OUTER JOIN #TraceStatus ts ON ts.TraceFlag = 8048 AND ts.status = 1 + WHERE n.node_id = 0 AND n.online_scheduler_count >= 8 + AND EXISTS (SELECT * FROM sys.dm_os_nodes WHERE node_id > 0 AND node_state_desc NOT LIKE '%DAC') + GROUP BY w.wait_type, ts.status + HAVING SUM([wait_time_ms]) > (SELECT 5000 * datediff(HH,create_date,CURRENT_TIMESTAMP) AS hours_since_startup FROM sys.databases WHERE name='tempdb') + AND SUM([wait_time_ms]) > 60000; + END; + + + /*Check for transaction log file larger than data file */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 75 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 75) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 75 AS CheckID , + DB_NAME(a.database_id) , + 50 AS Priority , + 'Reliability' AS FindingsGroup , + 'Transaction Log Larger than Data File' AS Finding , + 'https://www.brentozar.com/go/biglog' AS URL , + 'The database [' + DB_NAME(a.database_id) + + '] has a ' + CAST((CAST(a.size AS BIGINT) * 8 / 1000000) AS NVARCHAR(20)) + ' GB transaction log file, larger than the total data file sizes. This may indicate that transaction log backups are not being performed or not performed often enough.' AS Details + FROM sys.master_files a + WHERE a.type = 1 + AND DB_NAME(a.database_id) NOT IN ( + SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID = 75 OR CheckID IS NULL) + AND a.size > 125000 /* Size is measured in pages here, so this gets us log files over 1GB. */ + AND a.size > ( SELECT SUM(CAST(b.size AS BIGINT)) + FROM sys.master_files b + WHERE a.database_id = b.database_id + AND b.type = 0 + ) + AND a.database_id IN ( + SELECT database_id + FROM sys.databases + WHERE source_database_id IS NULL ); + END; + + /*Check for collation conflicts between user databases and tempdb */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 76 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 76) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 76 AS CheckID , + name AS DatabaseName , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Collation is ' + collation_name AS Finding , + 'https://www.brentozar.com/go/collate' AS URL , + 'Collation differences between user databases and tempdb can cause conflicts especially when comparing string values' AS Details + FROM sys.databases + WHERE name NOT IN ( 'master', 'model', 'msdb') + AND name NOT LIKE 'ReportServer%' + AND name NOT IN ( SELECT DISTINCT + DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL OR CheckID = 76) + AND collation_name <> ( SELECT + collation_name + FROM + sys.databases + WHERE + name = 'tempdb' + ); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 77 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 77) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + DatabaseName , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 77 AS CheckID , + dSnap.[name] AS DatabaseName , + 170 AS Priority , + 'Reliability' AS FindingsGroup , + 'Database Snapshot Online' AS Finding , + 'https://www.brentozar.com/go/snapshot' AS URL , + 'Database [' + dSnap.[name] + + '] is a snapshot of [' + + dOriginal.[name] + + ']. Make sure you have enough drive space to maintain the snapshot as the original database grows.' AS Details + FROM sys.databases dSnap + INNER JOIN sys.databases dOriginal ON dSnap.source_database_id = dOriginal.database_id + AND dSnap.name NOT IN ( + SELECT DISTINCT DatabaseName + FROM #SkipChecks + WHERE CheckID = 77 OR CheckID IS NULL); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 79 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 79) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 79 AS CheckID , + -- sp_Blitz Issue #776 + -- Job has history and was executed in the last 30 days OR Job is enabled AND Job Schedule is enabled + CASE WHEN (cast(datediff(dd, substring(cast(sjh.run_date as nvarchar(10)), 1, 4) + '-' + substring(cast(sjh.run_date as nvarchar(10)), 5, 2) + '-' + substring(cast(sjh.run_date as nvarchar(10)), 7, 2), GETDATE()) AS INT) < 30) OR (j.[enabled] = 1 AND ssc.[enabled] = 1 )THEN + 100 + ELSE -- no job history (implicit) AND job not run in the past 30 days AND (Job disabled OR Job Schedule disabled) + 200 + END AS Priority, + 'Performance' AS FindingsGroup , + 'Shrink Database Job' AS Finding , + 'https://www.brentozar.com/go/autoshrink' AS URL , + 'In the [' + j.[name] + '] job, step [' + + step.[step_name] + + '] has SHRINKDATABASE or SHRINKFILE, which may be causing database fragmentation.' + + CASE WHEN COALESCE(ssc.name,'0') != '0' THEN + ' (Schedule: [' + ssc.name + '])' ELSE + '' END AS Details + FROM msdb.dbo.sysjobs j + INNER JOIN msdb.dbo.sysjobsteps step ON j.job_id = step.job_id + LEFT OUTER JOIN msdb.dbo.sysjobschedules AS sjsc + ON j.job_id = sjsc.job_id + LEFT OUTER JOIN msdb.dbo.sysschedules AS ssc + ON sjsc.schedule_id = ssc.schedule_id + AND sjsc.job_id = j.job_id + LEFT OUTER JOIN msdb.dbo.sysjobhistory AS sjh + ON j.job_id = sjh.job_id + AND step.step_id = sjh.step_id + AND sjh.run_date IN (SELECT max(sjh2.run_date) FROM msdb.dbo.sysjobhistory AS sjh2 WHERE sjh2.job_id = j.job_id) -- get the latest entry date + AND sjh.run_time IN (SELECT max(sjh3.run_time) FROM msdb.dbo.sysjobhistory AS sjh3 WHERE sjh3.job_id = j.job_id AND sjh3.run_date = sjh.run_date) -- get the latest entry time + WHERE step.command LIKE N'%SHRINKDATABASE%' + OR step.command LIKE N'%SHRINKFILE%'; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 81 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 81) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 81 AS CheckID , + 200 AS Priority , + 'Non-Active Server Config' AS FindingsGroup , + cr.name AS Finding , + 'https://www.BrentOzar.com/blitz/sp_configure/' AS URL , + ( 'This sp_configure option isn''t running under its set value. Its set value is ' + + CAST(cr.[value] AS VARCHAR(100)) + + ' and its running value is ' + + CAST(cr.value_in_use AS VARCHAR(100)) + + '. When someone does a RECONFIGURE or restarts the instance, this setting will start taking effect.' ) AS Details + FROM sys.configurations cr + WHERE cr.value <> cr.value_in_use + AND NOT (cr.name = 'min server memory (MB)' AND cr.value IN (0,16) AND cr.value_in_use IN (0,16)); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 123 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 123) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT TOP 1 123 AS CheckID , + 200 AS Priority , + 'Informational' AS FindingsGroup , + 'Agent Jobs Starting Simultaneously' AS Finding , + 'https://www.brentozar.com/go/busyagent/' AS URL , + ( 'Multiple SQL Server Agent jobs are configured to start simultaneously. For detailed schedule listings, see the query in the URL.' ) AS Details + FROM msdb.dbo.sysjobactivity + WHERE start_execution_date > DATEADD(dd, -14, GETDATE()) + GROUP BY start_execution_date HAVING COUNT(*) > 1; + END; + + IF @CheckServerInfo = 1 + BEGIN + +/*This checks Windows version. It would be better if Microsoft gave everything a separate build number, but whatever.*/ +IF @ProductVersionMajor >= 10 + AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 172 ) + BEGIN + -- sys.dm_os_host_info includes both Windows and Linux info + IF EXISTS (SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_os_host_info' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 172) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 172 AS [CheckID] , + 250 AS [Priority] , + 'Server Info' AS [FindingsGroup] , + 'Operating System Version' AS [Finding] , + ( CASE WHEN @IsWindowsOperatingSystem = 1 + THEN 'https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions' + ELSE 'https://en.wikipedia.org/wiki/List_of_Linux_distributions' + END + ) AS [URL] , + ( CASE + WHEN [ohi].[host_platform] = 'Linux' THEN 'You''re running the ' + CAST([ohi].[host_distribution] AS VARCHAR(35)) + ' distribution of ' + CAST([ohi].[host_platform] AS VARCHAR(35)) + ', version ' + CAST([ohi].[host_release] AS VARCHAR(5)) + WHEN [ohi].[host_platform] = 'Windows' AND [ohi].[host_release] = '5' THEN 'You''re running Windows 2000, version ' + CAST([ohi].[host_release] AS VARCHAR(5)) + WHEN [ohi].[host_platform] = 'Windows' AND [ohi].[host_release] > '5' THEN 'You''re running ' + CAST([ohi].[host_distribution] AS VARCHAR(50)) + ', version ' + CAST([ohi].[host_release] AS VARCHAR(5)) + ELSE 'You''re running ' + CAST([ohi].[host_distribution] AS VARCHAR(35)) + ', version ' + CAST([ohi].[host_release] AS VARCHAR(5)) + END + ) AS [Details] + FROM [sys].[dm_os_host_info] [ohi]; + END; + ELSE + BEGIN + -- Otherwise, stick with Windows-only detection + + IF EXISTS ( SELECT 1 + FROM sys.all_objects + WHERE name = 'dm_os_windows_info' ) + + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 172) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + + SELECT + 172 AS [CheckID] , + 250 AS [Priority] , + 'Server Info' AS [FindingsGroup] , + 'Windows Version' AS [Finding] , + 'https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions' AS [URL] , + ( CASE + WHEN [owi].[windows_release] = '5' THEN 'You''re running Windows 2000, version ' + CAST([owi].[windows_release] AS VARCHAR(5)) + WHEN [owi].[windows_release] > '5' AND [owi].[windows_release] < '6' THEN 'You''re running Windows Server 2003/2003R2 era, version ' + CAST([owi].[windows_release] AS VARCHAR(5)) + WHEN [owi].[windows_release] >= '6' AND [owi].[windows_release] <= '6.1' THEN 'You''re running Windows Server 2008/2008R2 era, version ' + CAST([owi].[windows_release] AS VARCHAR(5)) + WHEN [owi].[windows_release] >= '6.2' AND [owi].[windows_release] <= '6.3' THEN 'You''re running Windows Server 2012/2012R2 era, version ' + CAST([owi].[windows_release] AS VARCHAR(5)) + WHEN [owi].[windows_release] = '10.0' THEN 'You''re running Windows Server 2016/2019 era, version ' + CAST([owi].[windows_release] AS VARCHAR(5)) + ELSE 'You''re running Windows Server, version ' + CAST([owi].[windows_release] AS VARCHAR(5)) + END + ) AS [Details] + FROM [sys].[dm_os_windows_info] [owi]; + + END; + END; + END; + +/* +This check hits the dm_os_process_memory system view +to see if locked_page_allocations_kb is > 0, +which could indicate that locked pages in memory is enabled. +*/ +IF @ProductVersionMajor >= 10 AND NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 166 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 166) WITH NOWAIT; + + INSERT INTO [#BlitzResults] + ( [CheckID] , + [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + [Details] ) + SELECT + 166 AS [CheckID] , + 250 AS [Priority] , + 'Server Info' AS [FindingsGroup] , + 'Locked Pages In Memory Enabled' AS [Finding] , + 'https://www.brentozar.com/go/lpim' AS [URL] , + ( 'You currently have ' + + CASE WHEN [dopm].[locked_page_allocations_kb] / 1024. / 1024. > 0 + THEN CAST([dopm].[locked_page_allocations_kb] / 1024 / 1024 AS VARCHAR(100)) + + ' GB' + ELSE CAST([dopm].[locked_page_allocations_kb] / 1024 AS VARCHAR(100)) + + ' MB' + END + ' of pages locked in memory.' ) AS [Details] + FROM + [sys].[dm_os_process_memory] AS [dopm] + WHERE + [dopm].[locked_page_allocations_kb] > 0; + END; + + /* Server Info - Locked Pages In Memory Enabled - Check 166 - SQL Server 2016 SP1 and newer */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 166 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_sys_info' + AND c.name = 'sql_memory_model' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 166) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 166 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Memory Model Unconventional'' AS Finding , + ''https://www.brentozar.com/go/lpim'' AS URL , + ''Memory Model: '' + CAST(sql_memory_model_desc AS NVARCHAR(100)) + FROM sys.dm_os_sys_info WHERE sql_memory_model <> 1 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* + Starting with SQL Server 2014 SP2, Instant File Initialization + is logged in the SQL Server Error Log. + */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 193 ) + AND ((@ProductVersionMajor >= 13) OR (@ProductVersionMajor = 12 AND @ProductVersionMinor >= 5000)) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 193) WITH NOWAIT; + + -- If this is Amazon RDS, use rdsadmin.dbo.rds_read_error_log + IF LEFT(CAST(COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),SERVERPROPERTY('ServerName')) AS VARCHAR(8000)), 8) = 'EC2AMAZ-' + AND LEFT(CAST(SERVERPROPERTY('MachineName') AS VARCHAR(8000)), 8) = 'EC2AMAZ-' + AND db_id('rdsadmin') IS NOT NULL + AND EXISTS(SELECT * FROM master.sys.all_objects WHERE name IN ('rds_startup_tasks', 'rds_help_revlogin', 'rds_hexadecimal', 'rds_failover_tracking', 'rds_database_tracking', 'rds_track_change')) + BEGIN + INSERT INTO #ErrorLog + EXEC rdsadmin.dbo.rds_read_error_log 0, 1, N'Database Instant File Initialization: enabled'; + END + ELSE + BEGIN + INSERT INTO #ErrorLog + EXEC sys.xp_readerrorlog 0, 1, N'Database Instant File Initialization: enabled'; + END + + IF @@ROWCOUNT > 0 + begin + INSERT INTO #BlitzResults + ( CheckID , + [Priority] , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 193 AS [CheckID] , + 250 AS [Priority] , + 'Server Info' AS [FindingsGroup] , + 'Instant File Initialization Enabled' AS [Finding] , + 'https://www.brentozar.com/go/instant' AS [URL] , + 'The service account has the Perform Volume Maintenance Tasks permission.'; + end + else -- if version of sql server has instant_file_initialization_enabled column in dm_server_services, check that too + -- in the event the error log has been cycled and the startup messages are not in the current error log + begin + if EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_server_services' + AND c.name = 'instant_file_initialization_enabled' ) + begin + SET @StringToExecute = N' + INSERT INTO #BlitzResults + ( CheckID , + [Priority] , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 193 AS [CheckID] , + 250 AS [Priority] , + ''Server Info'' AS [FindingsGroup] , + ''Instant File Initialization Enabled'' AS [Finding] , + ''https://www.brentozar.com/go/instant'' AS [URL] , + ''The service account has the Perform Volume Maintenance Tasks permission.'' + where exists (select 1 FROM sys.dm_server_services + WHERE instant_file_initialization_enabled = ''Y'' + AND filename LIKE ''%sqlservr.exe%'') + OPTION (RECOMPILE);'; + EXEC(@StringToExecute); + end; + end; + END; + + /* Server Info - Instant File Initialization Not Enabled - Check 192 - SQL Server 2016 SP1 and newer */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 192 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_server_services' + AND c.name = 'instant_file_initialization_enabled' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 192) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 192 AS CheckID , + 50 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Instant File Initialization Not Enabled'' AS Finding , + ''https://www.brentozar.com/go/instant'' AS URL , + ''Consider enabling IFI for faster restores and data file growths.'' + FROM sys.dm_server_services WHERE instant_file_initialization_enabled <> ''Y'' AND filename LIKE ''%sqlservr.exe%'' OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 130 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 130) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 130 AS CheckID , + 250 AS Priority , + 'Server Info' AS FindingsGroup , + 'Server Name' AS Finding , + 'https://www.brentozar.com/go/servername' AS URL , + @@SERVERNAME AS Details + WHERE @@SERVERNAME IS NOT NULL; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 83 ) + BEGIN + IF EXISTS ( SELECT * + FROM sys.all_objects + WHERE name = 'dm_server_services' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 83) WITH NOWAIT; + + -- DATETIMEOFFSET and DATETIME have different minimum values, so there's + -- a small workaround here to force 1753-01-01 if the minimum is detected + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 83 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Services'' AS Finding , + '''' AS URL , + N''Service: '' + servicename + N'' runs under service account '' + service_account + N''. Last startup time: '' + COALESCE(CAST(CASE WHEN YEAR(last_startup_time) <= 1753 THEN CAST(''17530101'' as datetime) ELSE CAST(last_startup_time AS DATETIME) END AS VARCHAR(50)), ''not shown.'') + ''. Startup type: '' + startup_type_desc + N'', currently '' + status_desc + ''.'' + FROM sys.dm_server_services OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + /* Check 84 - SQL Server 2012 */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 84 ) + BEGIN + IF EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_sys_info' + AND c.name = 'physical_memory_kb' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 84) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 84 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Hardware'' AS Finding , + '''' AS URL , + ''Logical processors: '' + CAST(cpu_count AS VARCHAR(50)) + ''. Physical memory: '' + CAST( CAST(ROUND((physical_memory_kb / 1024.0 / 1024), 1) AS INT) AS VARCHAR(50)) + ''GB.'' + FROM sys.dm_os_sys_info OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* Check 84 - SQL Server 2008 */ + IF EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_sys_info' + AND c.name = 'physical_memory_in_bytes' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 84) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 84 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Hardware'' AS Finding , + '''' AS URL , + ''Logical processors: '' + CAST(cpu_count AS VARCHAR(50)) + ''. Physical memory: '' + CAST( CAST(ROUND((physical_memory_in_bytes / 1024.0 / 1024 / 1024), 1) AS INT) AS VARCHAR(50)) + ''GB.'' + FROM sys.dm_os_sys_info OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 85 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 85) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 85 AS CheckID , + 250 AS Priority , + 'Server Info' AS FindingsGroup , + 'SQL Server Service' AS Finding , + '' AS URL , + N'Version: ' + + CAST(SERVERPROPERTY('productversion') AS NVARCHAR(100)) + + N'. Patch Level: ' + + CAST(SERVERPROPERTY('productlevel') AS NVARCHAR(100)) + + CASE WHEN SERVERPROPERTY('ProductUpdateLevel') IS NULL + THEN N'' + ELSE N'. Cumulative Update: ' + + CAST(SERVERPROPERTY('ProductUpdateLevel') AS NVARCHAR(100)) + END + + N'. Edition: ' + + CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) + + N'. Availability Groups Enabled: ' + + CAST(COALESCE(SERVERPROPERTY('IsHadrEnabled'), + 0) AS VARCHAR(100)) + + N'. Availability Groups Manager Status: ' + + CAST(COALESCE(SERVERPROPERTY('HadrManagerStatus'), + 0) AS VARCHAR(100)); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 88 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 88) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 88 AS CheckID , + 250 AS Priority , + 'Server Info' AS FindingsGroup , + 'SQL Server Last Restart' AS Finding , + '' AS URL , + CAST(create_date AS VARCHAR(100)) + FROM sys.databases + WHERE database_id = 2; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 91 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 91) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 91 AS CheckID , + 250 AS Priority , + 'Server Info' AS FindingsGroup , + 'Server Last Restart' AS Finding , + '' AS URL , + CAST(DATEADD(SECOND, (ms_ticks/1000)*(-1), GETDATE()) AS nvarchar(25)) + FROM sys.dm_os_sys_info; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 92 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 92) WITH NOWAIT; + + INSERT INTO #driveInfo + ( drive, available_MB ) + EXEC master..xp_fixeddrives; + + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'dm_os_volume_stats') + BEGIN + SET @StringToExecute = 'Update #driveInfo + SET + logical_volume_name = v.logical_volume_name, + total_MB = v.total_MB, + used_percent = v.used_percent + FROM + #driveInfo + inner join ( + SELECT DISTINCT + SUBSTRING(volume_mount_point, 1, 1) AS volume_mount_point + ,CASE WHEN ISNULL(logical_volume_name,'''') = '''' THEN '''' ELSE ''('' + logical_volume_name + '')'' END AS logical_volume_name + ,total_bytes/1024/1024 AS total_MB + ,available_bytes/1024/1024 AS available_MB + ,(CONVERT(DECIMAL(5,2),(total_bytes/1.0 - available_bytes)/total_bytes * 100)) AS used_percent + FROM + (SELECT TOP 1 WITH TIES + database_id + ,file_id + ,SUBSTRING(physical_name,1,1) AS Drive + FROM sys.master_files + ORDER BY ROW_NUMBER() OVER(PARTITION BY SUBSTRING(physical_name,1,1) ORDER BY database_id) + ) f + CROSS APPLY + sys.dm_os_volume_stats(f.database_id, f.file_id) + ) as v on #driveInfo.drive = v.volume_mount_point;'; + EXECUTE(@StringToExecute); + END; + + SET @StringToExecute ='INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 92 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Drive '' + i.drive + '' Space'' AS Finding , + '''' AS URL , + CASE WHEN i.total_MB IS NULL THEN + CAST(CAST(i.available_MB/1024 AS NUMERIC(18,2)) AS VARCHAR(30)) + + '' GB free on '' + i.drive + + '' drive'' + ELSE CAST(CAST(i.available_MB/1024 AS NUMERIC(18,2)) AS VARCHAR(30)) + + '' GB free on '' + i.drive + + '' drive '' + i.logical_volume_name + + '' out of '' + CAST(CAST(i.total_MB/1024 AS NUMERIC(18,2)) AS VARCHAR(30)) + + '' GB total ('' + CAST(i.used_percent AS VARCHAR(30)) + ''% used)'' END + AS Details + FROM #driveInfo AS i;' + + IF (@ProductVersionMajor >= 11) + BEGIN + SET @StringToExecute=REPLACE(@StringToExecute,'CAST(i.available_MB/1024 AS NUMERIC(18,2))','FORMAT(i.available_MB/1024,''N2'')'); + SET @StringToExecute=REPLACE(@StringToExecute,'CAST(i.total_MB/1024 AS NUMERIC(18,2))','FORMAT(i.total_MB/1024,''N2'')'); + END; + + EXECUTE(@StringToExecute); + + DROP TABLE #driveInfo; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 103 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_sys_info' + AND c.name = 'virtual_machine_type_desc' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 103) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 103 AS CheckID, + 250 AS Priority, + ''Server Info'' AS FindingsGroup, + ''Virtual Server'' AS Finding, + ''https://www.brentozar.com/go/virtual'' AS URL, + ''Type: ('' + virtual_machine_type_desc + '')'' AS Details + FROM sys.dm_os_sys_info + WHERE virtual_machine_type <> 0 OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 214 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_sys_info' + AND c.name = 'container_type_desc' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 214) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 214 AS CheckID, + 250 AS Priority, + ''Server Info'' AS FindingsGroup, + ''Container'' AS Finding, + ''https://www.brentozar.com/go/virtual'' AS URL, + ''Type: ('' + container_type_desc + '')'' AS Details + FROM sys.dm_os_sys_info + WHERE container_type_desc <> ''NONE'' OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 114 ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + WHERE o.name = 'dm_os_memory_nodes' ) + AND EXISTS ( SELECT * + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_nodes' + AND c.name = 'processor_group' ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 114) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 114 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Hardware - NUMA Config'' AS Finding , + '''' AS URL , + ''Node: '' + CAST(n.node_id AS NVARCHAR(10)) + '' State: '' + node_state_desc + + '' Online schedulers: '' + CAST(n.online_scheduler_count AS NVARCHAR(10)) + '' Offline schedulers: '' + CAST(oac.offline_schedulers AS VARCHAR(100)) + '' Processor Group: '' + CAST(n.processor_group AS NVARCHAR(10)) + + '' Memory node: '' + CAST(n.memory_node_id AS NVARCHAR(10)) + '' Memory VAS Reserved GB: '' + CAST(CAST((m.virtual_address_space_reserved_kb / 1024.0 / 1024) AS INT) AS NVARCHAR(100)) + FROM sys.dm_os_nodes n + INNER JOIN sys.dm_os_memory_nodes m ON n.memory_node_id = m.memory_node_id + OUTER APPLY (SELECT + COUNT(*) AS [offline_schedulers] + FROM sys.dm_os_schedulers dos + WHERE n.node_id = dos.parent_node_id + AND dos.status = ''VISIBLE OFFLINE'' + ) oac + WHERE n.node_state_desc NOT LIKE ''%DAC%'' + ORDER BY n.node_id OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 211 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 211) WITH NOWAIT; + + DECLARE @outval VARCHAR(36); + /* Get power plan if set by group policy [Git Hub Issue #1620] */ + EXEC master.sys.xp_regread @rootkey = 'HKEY_LOCAL_MACHINE', + @key = 'SOFTWARE\Policies\Microsoft\Power\PowerSettings', + @value_name = 'ActivePowerScheme', + @value = @outval OUTPUT, + @no_output = 'no_output'; + + IF @outval IS NULL /* If power plan was not set by group policy, get local value [Git Hub Issue #1620]*/ + EXEC master.sys.xp_regread @rootkey = 'HKEY_LOCAL_MACHINE', + @key = 'SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes', + @value_name = 'ActivePowerScheme', + @value = @outval OUTPUT; + + DECLARE @cpu_speed_mhz int, + @cpu_speed_ghz decimal(18,2); + + EXEC master.sys.xp_regread @rootkey = 'HKEY_LOCAL_MACHINE', + @key = 'HARDWARE\DESCRIPTION\System\CentralProcessor\0', + @value_name = '~MHz', + @value = @cpu_speed_mhz OUTPUT; + + SELECT @cpu_speed_ghz = CAST(CAST(@cpu_speed_mhz AS DECIMAL) / 1000 AS DECIMAL(18,2)); + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 211 AS CheckId, + 250 AS Priority, + 'Server Info' AS FindingsGroup, + 'Power Plan' AS Finding, + 'https://www.brentozar.com/blitz/power-mode/' AS URL, + 'Your server has ' + + CAST(@cpu_speed_ghz as VARCHAR(4)) + + 'GHz CPUs, and is in ' + + CASE @outval + WHEN 'a1841308-3541-4fab-bc81-f71556f20b4a' + THEN 'power saving mode -- are you sure this is a production SQL Server?' + WHEN '381b4222-f694-41f0-9685-ff5bb260df2e' + THEN 'balanced power mode -- Uh... you want your CPUs to run at full speed, right?' + WHEN '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + THEN 'high performance power mode' + WHEN 'e9a42b02-d5df-448d-aa00-03f14749eb61' + THEN 'ultimate performance power mode' + ELSE 'an unknown power mode.' + END AS Details + + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 212 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 212) WITH NOWAIT; + + INSERT INTO #Instances (Instance_Number, Instance_Name, Data_Field) + EXEC master.sys.xp_regread @rootkey = 'HKEY_LOCAL_MACHINE', + @key = 'SOFTWARE\Microsoft\Microsoft SQL Server', + @value_name = 'InstalledInstances' + + IF (SELECT COUNT(*) FROM #Instances) > 1 + BEGIN + + DECLARE @InstanceCount NVARCHAR(MAX) + SELECT @InstanceCount = COUNT(*) FROM #Instances + + INSERT INTO #BlitzResults + ( + CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 212 AS CheckId , + 250 AS Priority , + 'Server Info' AS FindingsGroup , + 'Instance Stacking' AS Finding , + 'https://www.brentozar.com/go/babygotstacked/' AS URL , + 'Your Server has ' + @InstanceCount + ' Instances of SQL Server installed. More than one is usually a bad idea. Read the URL for more info.' + END; + END; + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 106 ) + AND (select convert(int,value_in_use) from sys.configurations where name = 'default trace enabled' ) = 1 + AND DATALENGTH( COALESCE( @base_tracefilename, '' ) ) > DATALENGTH('.TRC') + AND @TraceFileIssue = 0 + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 106) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 106 AS CheckID + ,250 AS Priority + ,'Server Info' AS FindingsGroup + ,'Default Trace Contents' AS Finding + ,'https://www.brentozar.com/go/trace' AS URL + ,'The default trace holds '+cast(DATEDIFF(hour,MIN(StartTime),GETDATE())as VARCHAR(30))+' hours of data' + +' between '+cast(Min(StartTime) as VARCHAR(30))+' and '+cast(GETDATE()as VARCHAR(30)) + +('. The default trace files are located in: '+left( @curr_tracefilename,len(@curr_tracefilename) - @indx) + ) as Details + FROM ::fn_trace_gettable( @base_tracefilename, default ) + WHERE EventClass BETWEEN 65500 and 65600; + END; /* CheckID 106 */ + + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 152 ) + BEGIN + IF EXISTS (SELECT * FROM sys.dm_os_wait_stats ws + LEFT OUTER JOIN #IgnorableWaits i ON ws.wait_type = i.wait_type + WHERE wait_time_ms > .1 * @CpuMsSinceWaitsCleared AND waiting_tasks_count > 0 + AND i.wait_type IS NULL) + BEGIN + /* Check for waits that have had more than 10% of the server's wait time */ + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 152) WITH NOWAIT; + + WITH os(wait_type, waiting_tasks_count, wait_time_ms, max_wait_time_ms, signal_wait_time_ms) + AS + (SELECT ws.wait_type, waiting_tasks_count, wait_time_ms, max_wait_time_ms, signal_wait_time_ms + FROM sys.dm_os_wait_stats ws + LEFT OUTER JOIN #IgnorableWaits i ON ws.wait_type = i.wait_type + WHERE i.wait_type IS NULL + AND wait_time_ms > .1 * @CpuMsSinceWaitsCleared + AND waiting_tasks_count > 0) + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT TOP 9 + 152 AS CheckID + ,240 AS Priority + ,'Wait Stats' AS FindingsGroup + , CAST(ROW_NUMBER() OVER(ORDER BY os.wait_time_ms DESC) AS NVARCHAR(10)) + N' - ' + os.wait_type AS Finding + ,'https://www.sqlskills.com/help/waits/' + LOWER(os.wait_type) + '/' AS URL + , Details = CAST(CAST(SUM(os.wait_time_ms / 1000.0 / 60 / 60) OVER (PARTITION BY os.wait_type) AS NUMERIC(18,1)) AS NVARCHAR(20)) + N' hours of waits, ' + + CAST(CAST((SUM(60.0 * os.wait_time_ms) OVER (PARTITION BY os.wait_type) ) / @MsSinceWaitsCleared AS NUMERIC(18,1)) AS NVARCHAR(20)) + N' minutes average wait time per hour, ' + + /* CAST(CAST( + 100.* SUM(os.wait_time_ms) OVER (PARTITION BY os.wait_type) + / (1. * SUM(os.wait_time_ms) OVER () ) + AS NUMERIC(18,1)) AS NVARCHAR(40)) + N'% of waits, ' + */ + CAST(CAST( + 100. * SUM(os.signal_wait_time_ms) OVER (PARTITION BY os.wait_type) + / (1. * SUM(os.wait_time_ms) OVER ()) + AS NUMERIC(18,1)) AS NVARCHAR(40)) + N'% signal wait, ' + + CAST(SUM(os.waiting_tasks_count) OVER (PARTITION BY os.wait_type) AS NVARCHAR(40)) + N' waiting tasks, ' + + CAST(CASE WHEN SUM(os.waiting_tasks_count) OVER (PARTITION BY os.wait_type) > 0 + THEN + CAST( + SUM(os.wait_time_ms) OVER (PARTITION BY os.wait_type) + / (1. * SUM(os.waiting_tasks_count) OVER (PARTITION BY os.wait_type)) + AS NUMERIC(18,1)) + ELSE 0 END AS NVARCHAR(40)) + N' ms average wait time.' + FROM os + ORDER BY SUM(os.wait_time_ms / 1000.0 / 60 / 60) OVER (PARTITION BY os.wait_type) DESC; + END; /* IF EXISTS (SELECT * FROM sys.dm_os_wait_stats WHERE wait_time_ms > 0 AND waiting_tasks_count > 0) */ + + /* If no waits were found, add a note about that */ + IF NOT EXISTS (SELECT * FROM #BlitzResults WHERE CheckID IN (107, 108, 109, 121, 152, 162)) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 153) WITH NOWAIT; + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES (153, 240, 'Wait Stats', 'No Significant Waits Detected', 'https://www.brentozar.com/go/waits', 'This server might be just sitting around idle, or someone may have cleared wait stats recently.'); + END; + END; /* CheckID 152 */ + + /* CheckID 222 - Server Info - Azure Managed Instance */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 222 ) + AND 4 = ( SELECT COUNT(*) + FROM sys.all_objects o + INNER JOIN sys.all_columns c ON o.object_id = c.object_id + WHERE o.name = 'dm_os_job_object' + AND c.name IN ('cpu_rate', 'memory_limit_mb', 'process_memory_limit_mb', 'workingset_limit_mb' )) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 222) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #BlitzResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT 222 AS CheckID , + 250 AS Priority , + ''Server Info'' AS FindingsGroup , + ''Azure Managed Instance'' AS Finding , + ''https://www.BrentOzar.com/go/azurevm'' AS URL , + ''cpu_rate: '' + CAST(COALESCE(cpu_rate, 0) AS VARCHAR(20)) + + '', memory_limit_mb: '' + CAST(COALESCE(memory_limit_mb, 0) AS NVARCHAR(20)) + + '', process_memory_limit_mb: '' + CAST(COALESCE(process_memory_limit_mb, 0) AS NVARCHAR(20)) + + '', workingset_limit_mb: '' + CAST(COALESCE(workingset_limit_mb, 0) AS NVARCHAR(20)) + FROM sys.dm_os_job_object OPTION (RECOMPILE);'; + + IF @Debug = 2 AND @StringToExecute IS NOT NULL PRINT @StringToExecute; + IF @Debug = 2 AND @StringToExecute IS NULL PRINT '@StringToExecute has gone NULL, for some reason.'; + + EXECUTE(@StringToExecute); + END; + + /* CheckID 224 - Performance - SSRS/SSAS/SSIS Installed */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 224 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 224) WITH NOWAIT; + + IF (SELECT value_in_use FROM sys.configurations WHERE [name] = 'xp_cmdshell') = 1 + BEGIN + + IF OBJECT_ID('tempdb..#services') IS NOT NULL DROP TABLE #services; + CREATE TABLE #services (cmdshell_output varchar(max)); + + INSERT INTO #services + EXEC /**/xp_cmdshell/**/ 'net start' /* added comments around command since some firewalls block this string TL 20210221 */ + + IF EXISTS (SELECT 1 + FROM #services + WHERE cmdshell_output LIKE '%SQL Server Reporting Services%' + OR cmdshell_output LIKE '%SQL Server Integration Services%' + OR cmdshell_output LIKE '%SQL Server Analysis Services%') + BEGIN + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT + 224 AS CheckID + ,200 AS Priority + ,'Performance' AS FindingsGroup + ,'SSAS/SSIS/SSRS Installed' AS Finding + ,'https://www.BrentOzar.com/go/services' AS URL + ,'Did you know you have other SQL Server services installed on this box other than the engine? It can be a real performance pain.' as Details + + END; + + END; + END; + + /* CheckID 232 - Server Info - Data Size */ + IF NOT EXISTS ( SELECT 1 + FROM #SkipChecks + WHERE DatabaseName IS NULL AND CheckID = 232 ) + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Running CheckId [%d].', 0, 1, 232) WITH NOWAIT; + + IF OBJECT_ID('tempdb..#MasterFiles') IS NOT NULL + DROP TABLE #MasterFiles; + CREATE TABLE #MasterFiles (database_id INT, file_id INT, type_desc NVARCHAR(50), name NVARCHAR(255), physical_name NVARCHAR(255), size BIGINT); + /* Azure SQL Database doesn't have sys.master_files, so we have to build our own. */ + IF ((SERVERPROPERTY('Edition')) = 'SQL Azure' + AND (OBJECT_ID('sys.master_files') IS NULL)) + SET @StringToExecute = 'INSERT INTO #MasterFiles (database_id, file_id, type_desc, name, physical_name, size) SELECT DB_ID(), file_id, type_desc, name, physical_name, size FROM sys.database_files;'; + ELSE + SET @StringToExecute = 'INSERT INTO #MasterFiles (database_id, file_id, type_desc, name, physical_name, size) SELECT database_id, file_id, type_desc, name, physical_name, size FROM sys.master_files;'; + EXEC(@StringToExecute); + + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 232 AS CheckID + ,250 AS Priority + ,'Server Info' AS FindingsGroup + ,'Data Size' AS Finding + ,'' AS URL + ,CAST(COUNT(DISTINCT database_id) AS NVARCHAR(100)) + N' databases, ' + CAST(CAST(SUM (CAST(size AS BIGINT)*8./1024./1024.) AS MONEY) AS VARCHAR(100)) + ' GB total file size' as Details + FROM #MasterFiles + WHERE database_id > 4; + + END; + + + END; /* IF @CheckServerInfo = 1 */ + END; /* IF ( ( SERVERPROPERTY('ServerName') NOT IN ( SELECT ServerName */ + + /* Delete priorites they wanted to skip. */ + IF @IgnorePrioritiesAbove IS NOT NULL + DELETE #BlitzResults + WHERE [Priority] > @IgnorePrioritiesAbove AND CheckID <> -1; + + IF @IgnorePrioritiesBelow IS NOT NULL + DELETE #BlitzResults + WHERE [Priority] < @IgnorePrioritiesBelow AND CheckID <> -1; + + /* Delete checks they wanted to skip. */ + IF @SkipChecksTable IS NOT NULL + BEGIN + DELETE FROM #BlitzResults + WHERE DatabaseName IN ( SELECT DatabaseName + FROM #SkipChecks + WHERE CheckID IS NULL + AND (ServerName IS NULL OR ServerName = SERVERPROPERTY('ServerName'))); + DELETE FROM #BlitzResults + WHERE CheckID IN ( SELECT CheckID + FROM #SkipChecks + WHERE DatabaseName IS NULL + AND (ServerName IS NULL OR ServerName = SERVERPROPERTY('ServerName'))); + DELETE r FROM #BlitzResults r + INNER JOIN #SkipChecks c ON r.DatabaseName = c.DatabaseName and r.CheckID = c.CheckID + AND (ServerName IS NULL OR ServerName = SERVERPROPERTY('ServerName')); + END; + + /* Add summary mode */ + IF @SummaryMode > 0 + BEGIN + UPDATE #BlitzResults + SET Finding = br.Finding + ' (' + CAST(brTotals.recs AS NVARCHAR(20)) + ')' + FROM #BlitzResults br + INNER JOIN (SELECT FindingsGroup, Finding, Priority, COUNT(*) AS recs FROM #BlitzResults GROUP BY FindingsGroup, Finding, Priority) brTotals ON br.FindingsGroup = brTotals.FindingsGroup AND br.Finding = brTotals.Finding AND br.Priority = brTotals.Priority + WHERE brTotals.recs > 1; + + DELETE br + FROM #BlitzResults br + WHERE EXISTS (SELECT * FROM #BlitzResults brLower WHERE br.FindingsGroup = brLower.FindingsGroup AND br.Finding = brLower.Finding AND br.Priority = brLower.Priority AND br.ID > brLower.ID); + + END; + + /* Add credits for the nice folks who put so much time into building and maintaining this for free: */ + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES ( -1 , + 255 , + 'Thanks!' , + 'From Your Community Volunteers' , + 'http://FirstResponderKit.org' , + 'We hope you found this tool useful.' + ); + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + + ) + VALUES ( -1 , + 0 , + 'sp_Blitz ' + CAST(CONVERT(DATETIME, @VersionDate, 102) AS VARCHAR(100)), + 'SQL Server First Responder Kit' , + 'http://FirstResponderKit.org/' , + 'To get help or add your own contributions, join us at http://FirstResponderKit.org.' + + ); + + INSERT INTO #BlitzResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + + ) + SELECT 156 , + 254 , + 'Rundate' , + GETDATE() , + 'http://FirstResponderKit.org/' , + 'Captain''s log: stardate something and something...'; + + IF @EmailRecipients IS NOT NULL + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Sending an email.', 0, 1) WITH NOWAIT; + + /* Database mail won't work off a local temp table. I'm not happy about this hacky workaround either. */ + IF (OBJECT_ID('tempdb..##BlitzResults', 'U') IS NOT NULL) DROP TABLE ##BlitzResults; + SELECT * INTO ##BlitzResults FROM #BlitzResults; + SET @query_result_separator = char(9); + SET @StringToExecute = 'SET NOCOUNT ON;SELECT [Priority] , [FindingsGroup] , [Finding] , [DatabaseName] , [URL] , [Details] , CheckID FROM ##BlitzResults ORDER BY Priority , FindingsGroup , Finding , DatabaseName , Details; SET NOCOUNT OFF;'; + SET @EmailSubject = 'sp_Blitz Results for ' + @@SERVERNAME; + SET @EmailBody = 'sp_Blitz ' + CAST(CONVERT(DATETIME, @VersionDate, 102) AS VARCHAR(100)) + '. http://FirstResponderKit.org'; + IF @EmailProfile IS NULL + EXEC msdb.dbo.sp_send_dbmail + @recipients = @EmailRecipients, + @subject = @EmailSubject, + @body = @EmailBody, + @query_attachment_filename = 'sp_Blitz-Results.csv', + @attach_query_result_as_file = 1, + @query_result_header = 1, + @query_result_width = 32767, + @append_query_error = 1, + @query_result_no_padding = 1, + @query_result_separator = @query_result_separator, + @query = @StringToExecute; + ELSE + EXEC msdb.dbo.sp_send_dbmail + @profile_name = @EmailProfile, + @recipients = @EmailRecipients, + @subject = @EmailSubject, + @body = @EmailBody, + @query_attachment_filename = 'sp_Blitz-Results.csv', + @attach_query_result_as_file = 1, + @query_result_header = 1, + @query_result_width = 32767, + @append_query_error = 1, + @query_result_no_padding = 1, + @query_result_separator = @query_result_separator, + @query = @StringToExecute; + IF (OBJECT_ID('tempdb..##BlitzResults', 'U') IS NOT NULL) DROP TABLE ##BlitzResults; + END; + + /* Checks if @OutputServerName is populated with a valid linked server, and that the database name specified is valid */ + DECLARE @ValidOutputServer BIT; + DECLARE @ValidOutputLocation BIT; + DECLARE @LinkedServerDBCheck NVARCHAR(2000); + DECLARE @ValidLinkedServerDB INT; + DECLARE @tmpdbchk table (cnt int); + IF @OutputServerName IS NOT NULL + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Outputting to a remote server.', 0, 1) WITH NOWAIT; + + IF EXISTS (SELECT server_id FROM sys.servers WHERE QUOTENAME([name]) = @OutputServerName) + BEGIN + SET @LinkedServerDBCheck = 'SELECT 1 WHERE EXISTS (SELECT * FROM '+@OutputServerName+'.master.sys.databases WHERE QUOTENAME([name]) = '''+@OutputDatabaseName+''')'; + INSERT INTO @tmpdbchk EXEC sys.sp_executesql @LinkedServerDBCheck; + SET @ValidLinkedServerDB = (SELECT COUNT(*) FROM @tmpdbchk); + IF (@ValidLinkedServerDB > 0) + BEGIN + SET @ValidOutputServer = 1; + SET @ValidOutputLocation = 1; + END; + ELSE + RAISERROR('The specified database was not found on the output server', 16, 0); + END; + ELSE + BEGIN + RAISERROR('The specified output server was not found', 16, 0); + END; + END; + ELSE + BEGIN + IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + SET @ValidOutputLocation = 1; + END; + ELSE IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND NOT EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + RAISERROR('The specified output database was not found on this server', 16, 0); + END; + ELSE + BEGIN + SET @ValidOutputLocation = 0; + END; + END; + + /* @OutputTableName lets us export the results to a permanent table */ + IF @ValidOutputLocation = 1 + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + ''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + ''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableName + ''') CREATE TABLE ' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + Priority TINYINT , + FindingsGroup VARCHAR(50) , + Finding VARCHAR(200) , + DatabaseName NVARCHAR(128), + URL VARCHAR(200) , + Details NVARCHAR(4000) , + QueryPlan [XML] NULL , + QueryPlanFiltered [NVARCHAR](MAX) NULL, + CheckID INT , + CONSTRAINT [PK_' + CAST(NEWID() AS CHAR(36)) + '] PRIMARY KEY CLUSTERED (ID ASC));'; + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,''''+@OutputSchemaName+'''',''''''+@OutputSchemaName+''''''); + SET @StringToExecute = REPLACE(@StringToExecute,''''+@OutputTableName+'''',''''''+@OutputTableName+''''''); + SET @StringToExecute = REPLACE(@StringToExecute,'[XML]','[NVARCHAR](MAX)'); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + IF @OutputXMLasNVARCHAR = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'[XML]','[NVARCHAR](MAX)'); + END; + EXEC(@StringToExecute); + END; + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputServerName + '.' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputServerName + '.' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, QueryPlan, QueryPlanFiltered) SELECT ''' + + CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)) + + ''', SYSDATETIMEOFFSET(), CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, CAST(QueryPlan AS NVARCHAR(MAX)), QueryPlanFiltered FROM #BlitzResults ORDER BY Priority , FindingsGroup , Finding , DatabaseName , Details'; + + EXEC(@StringToExecute); + END; + ELSE + BEGIN + IF @OutputXMLasNVARCHAR = 1 + BEGIN + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, QueryPlan, QueryPlanFiltered) SELECT ''' + + CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)) + + ''', SYSDATETIMEOFFSET(), CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, CAST(QueryPlan AS NVARCHAR(MAX)), QueryPlanFiltered FROM #BlitzResults ORDER BY Priority , FindingsGroup , Finding , DatabaseName , Details'; + END; + ELSE + begin + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, QueryPlan, QueryPlanFiltered) SELECT ''' + + CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)) + + ''', SYSDATETIMEOFFSET(), CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, QueryPlan, QueryPlanFiltered FROM #BlitzResults ORDER BY Priority , FindingsGroup , Finding , DatabaseName , Details'; + END; + EXEC(@StringToExecute); + + END; + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 2) = '##') + BEGIN + IF @ValidOutputServer = 1 + BEGIN + RAISERROR('Due to the nature of temporary tables, outputting to a linked server requires a permanent table.', 16, 0); + END; + ELSE + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''tempdb..' + + @OutputTableName + + ''') IS NOT NULL) DROP TABLE ' + @OutputTableName + ';' + + 'CREATE TABLE ' + + @OutputTableName + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + Priority TINYINT , + FindingsGroup VARCHAR(50) , + Finding VARCHAR(200) , + DatabaseName NVARCHAR(128), + URL VARCHAR(200) , + Details NVARCHAR(4000) , + QueryPlan [XML] NULL , + QueryPlanFiltered [NVARCHAR](MAX) NULL, + CheckID INT , + CONSTRAINT [PK_' + CAST(NEWID() AS CHAR(36)) + '] PRIMARY KEY CLUSTERED (ID ASC));' + + ' INSERT ' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, QueryPlan, QueryPlanFiltered) SELECT ''' + + CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)) + + ''', SYSDATETIMEOFFSET(), CheckID, DatabaseName, Priority, FindingsGroup, Finding, URL, Details, QueryPlan, QueryPlanFiltered FROM #BlitzResults ORDER BY Priority , FindingsGroup , Finding , DatabaseName , Details'; + + EXEC(@StringToExecute); + END; + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); + END; + + DECLARE @separator AS VARCHAR(1); + IF @OutputType = 'RSV' + SET @separator = CHAR(31); + ELSE + SET @separator = ','; + + IF @OutputType = 'COUNT' + BEGIN + SELECT COUNT(*) AS Warnings + FROM #BlitzResults; + END; + ELSE + IF @OutputType IN ( 'CSV', 'RSV' ) + BEGIN + + SELECT Result = CAST([Priority] AS NVARCHAR(100)) + + @separator + CAST(CheckID AS NVARCHAR(100)) + + @separator + COALESCE([FindingsGroup], + '(N/A)') + @separator + + COALESCE([Finding], '(N/A)') + @separator + + COALESCE(DatabaseName, '(N/A)') + @separator + + COALESCE([URL], '(N/A)') + @separator + + COALESCE([Details], '(N/A)') + FROM #BlitzResults + ORDER BY Priority , + FindingsGroup , + Finding , + DatabaseName , + Details; + END; + ELSE IF @OutputXMLasNVARCHAR = 1 AND @OutputType <> 'NONE' + BEGIN + SELECT [Priority] , + [FindingsGroup] , + [Finding] , + [DatabaseName] , + [URL] , + [Details] , + CAST([QueryPlan] AS NVARCHAR(MAX)) AS QueryPlan, + [QueryPlanFiltered] , + CheckID + FROM #BlitzResults + ORDER BY Priority , + FindingsGroup , + Finding , + DatabaseName , + Details; + END; + ELSE IF @OutputType = 'MARKDOWN' + BEGIN + WITH Results AS (SELECT row_number() OVER (ORDER BY Priority, FindingsGroup, Finding, DatabaseName, Details) AS rownum, * + FROM #BlitzResults + WHERE Priority > 0 AND Priority < 255 AND FindingsGroup IS NOT NULL AND Finding IS NOT NULL + AND FindingsGroup <> 'Security' /* Specifically excluding security checks for public exports */) + SELECT + CASE + WHEN r.Priority <> COALESCE(rPrior.Priority, 0) OR r.FindingsGroup <> rPrior.FindingsGroup THEN @crlf + N'**Priority ' + CAST(COALESCE(r.Priority,N'') AS NVARCHAR(5)) + N': ' + COALESCE(r.FindingsGroup,N'') + N'**:' + @crlf + @crlf + ELSE N'' + END + + CASE WHEN r.Finding <> COALESCE(rPrior.Finding,N'') AND r.Finding <> COALESCE(rNext.Finding,N'') THEN N'- ' + COALESCE(r.Finding,N'') + N' ' + COALESCE(r.DatabaseName, N'') + N' - ' + COALESCE(r.Details,N'') + @crlf + WHEN r.Finding <> COALESCE(rPrior.Finding,N'') AND r.Finding = rNext.Finding AND r.Details = rNext.Details THEN N'- ' + COALESCE(r.Finding,N'') + N' - ' + COALESCE(r.Details,N'') + @crlf + @crlf + N' * ' + COALESCE(r.DatabaseName, N'') + @crlf + WHEN r.Finding <> COALESCE(rPrior.Finding,N'') AND r.Finding = rNext.Finding THEN N'- ' + COALESCE(r.Finding,N'') + @crlf + CASE WHEN r.DatabaseName IS NULL THEN N'' ELSE N' * ' + COALESCE(r.DatabaseName,N'') END + CASE WHEN r.Details <> rPrior.Details THEN N' - ' + COALESCE(r.Details,N'') + @crlf ELSE '' END + ELSE CASE WHEN r.DatabaseName IS NULL THEN N'' ELSE N' * ' + COALESCE(r.DatabaseName,N'') END + CASE WHEN r.Details <> rPrior.Details THEN N' - ' + COALESCE(r.Details,N'') + @crlf ELSE N'' + @crlf END + END + @crlf + FROM Results r + LEFT OUTER JOIN Results rPrior ON r.rownum = rPrior.rownum + 1 + LEFT OUTER JOIN Results rNext ON r.rownum = rNext.rownum - 1 + ORDER BY r.rownum FOR XML PATH(N''); + END; + ELSE IF @OutputType = 'XML' + BEGIN + /* --TOURSTOP05-- */ + SELECT [Priority] , + [FindingsGroup] , + [Finding] , + [DatabaseName] , + [URL] , + [Details] , + [QueryPlanFiltered] , + CheckID + FROM #BlitzResults + ORDER BY Priority , + FindingsGroup , + Finding , + DatabaseName , + Details + FOR XML PATH('Result'), ROOT('sp_Blitz_Output'); + END; + ELSE IF @OutputType <> 'NONE' + BEGIN + /* --TOURSTOP05-- */ + SELECT [Priority] , + [FindingsGroup] , + [Finding] , + [DatabaseName] , + [URL] , + [Details] , + [QueryPlan] , + [QueryPlanFiltered] , + CheckID + FROM #BlitzResults + ORDER BY Priority , + FindingsGroup , + Finding , + DatabaseName , + Details; + END; + + DROP TABLE #BlitzResults; + + IF @OutputProcedureCache = 1 + AND @CheckProcedureCache = 1 + SELECT TOP 20 + total_worker_time / execution_count AS AvgCPU , + total_worker_time AS TotalCPU , + CAST(ROUND(100.00 * total_worker_time + / ( SELECT SUM(total_worker_time) + FROM sys.dm_exec_query_stats + ), 2) AS MONEY) AS PercentCPU , + total_elapsed_time / execution_count AS AvgDuration , + total_elapsed_time AS TotalDuration , + CAST(ROUND(100.00 * total_elapsed_time + / ( SELECT SUM(total_elapsed_time) + FROM sys.dm_exec_query_stats + ), 2) AS MONEY) AS PercentDuration , + total_logical_reads / execution_count AS AvgReads , + total_logical_reads AS TotalReads , + CAST(ROUND(100.00 * total_logical_reads + / ( SELECT SUM(total_logical_reads) + FROM sys.dm_exec_query_stats + ), 2) AS MONEY) AS PercentReads , + execution_count , + CAST(ROUND(100.00 * execution_count + / ( SELECT SUM(execution_count) + FROM sys.dm_exec_query_stats + ), 2) AS MONEY) AS PercentExecutions , + CASE WHEN DATEDIFF(mi, creation_time, + qs.last_execution_time) = 0 THEN 0 + ELSE CAST(( 1.00 * execution_count / DATEDIFF(mi, + creation_time, + qs.last_execution_time) ) AS MONEY) + END AS executions_per_minute , + qs.creation_time AS plan_creation_time , + qs.last_execution_time , + text , + text_filtered , + query_plan , + query_plan_filtered , + sql_handle , + query_hash , + plan_handle , + query_plan_hash + FROM #dm_exec_query_stats qs + ORDER BY CASE UPPER(@CheckProcedureCacheFilter) + WHEN 'CPU' THEN total_worker_time + WHEN 'READS' THEN total_logical_reads + WHEN 'EXECCOUNT' THEN execution_count + WHEN 'DURATION' THEN total_elapsed_time + ELSE total_worker_time + END DESC; + + END; /* ELSE -- IF @OutputType = 'SCHEMA' */ + + /* + Cleanups - drop temporary tables that have been created by this SP. + */ + + IF(OBJECT_ID('tempdb..#InvalidLogins') IS NOT NULL) + BEGIN + EXEC sp_executesql N'DROP TABLE #InvalidLogins;'; + END; + + /* + Reset the Nmumeric_RoundAbort session state back to enabled if it was disabled earlier. + See Github issue #2302 for more info. + */ + IF @NeedToTurnNumericRoundabortBackOn = 1 + SET NUMERIC_ROUNDABORT ON; + + SET NOCOUNT OFF; +GO + +/* +--Sample execution call with the most common parameters: +EXEC [dbo].[sp_Blitz] + @CheckUserDatabaseObjects = 1 , + @CheckProcedureCache = 0 , + @OutputType = 'TABLE' , + @OutputProcedureCache = 0 , + @CheckProcedureCacheFilter = NULL, + @CheckServerInfo = 1 +*/ +SET ANSI_NULLS ON; +SET QUOTED_IDENTIFIER ON + +IF NOT EXISTS (SELECT * FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[sp_BlitzAnalysis]') AND [type] in (N'P', N'PC')) +BEGIN +EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sp_BlitzAnalysis] AS' +END +GO + +ALTER PROCEDURE [dbo].[sp_BlitzAnalysis] ( +@Help TINYINT = 0, +@StartDate DATETIMEOFFSET(7) = NULL, +@EndDate DATETIMEOFFSET(7) = NULL, +@OutputDatabaseName NVARCHAR(256) = 'DBAtools', +@OutputSchemaName NVARCHAR(256) = N'dbo', +@OutputTableNameBlitzFirst NVARCHAR(256) = N'BlitzFirst', +@OutputTableNameFileStats NVARCHAR(256) = N'BlitzFirst_FileStats', +@OutputTableNamePerfmonStats NVARCHAR(256) = N'BlitzFirst_PerfmonStats', +@OutputTableNameWaitStats NVARCHAR(256) = N'BlitzFirst_WaitStats', +@OutputTableNameBlitzCache NVARCHAR(256) = N'BlitzCache', +@OutputTableNameBlitzWho NVARCHAR(256) = N'BlitzWho', +@Servername NVARCHAR(128) = @@SERVERNAME, +@Databasename NVARCHAR(128) = NULL, +@BlitzCacheSortorder NVARCHAR(20) = N'cpu', +@MaxBlitzFirstPriority INT = 249, +@ReadLatencyThreshold INT = 100, +@WriteLatencyThreshold INT = 100, +@WaitStatsTop TINYINT = 10, +@Version VARCHAR(30) = NULL OUTPUT, +@VersionDate DATETIME = NULL OUTPUT, +@VersionCheckMode BIT = 0, +@BringThePain BIT = 0, +@Maxdop INT = 1, +@Debug BIT = 0 +) +AS +SET NOCOUNT ON; +SET STATISTICS XML OFF; + +SELECT @Version = '8.16', @VersionDate = '20230820'; + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + +IF (@Help = 1) +BEGIN + PRINT 'EXEC sp_BlitzAnalysis +@StartDate = NULL, /* Specify a datetime or NULL will get an hour ago */ +@EndDate = NULL, /* Specify a datetime or NULL will get an hour of data since @StartDate */ +@OutputDatabaseName = N''DBA'', /* Specify the database name where where we can find your logged blitz data */ +@OutputSchemaName = N''dbo'', /* Specify the schema */ +@OutputTableNameBlitzFirst = N''BlitzFirst'', /* Table name where you are storing sp_BlitzFirst output, Set to NULL to ignore */ +@OutputTableNameFileStats = N''BlitzFirst_FileStats'', /* Table name where you are storing sp_BlitzFirst filestats output, Set to NULL to ignore */ +@OutputTableNamePerfmonStats = N''BlitzFirst_PerfmonStats'', /* Table name where you are storing sp_BlitzFirst Perfmon output, Set to NULL to ignore */ +@OutputTableNameWaitStats = N''BlitzFirst_WaitStats'', /* Table name where you are storing sp_BlitzFirst Wait stats output, Set to NULL to ignore */ +@OutputTableNameBlitzCache = N''BlitzCache'', /* Table name where you are storing sp_BlitzCache output, Set to NULL to ignore */ +@OutputTableNameBlitzWho = N''BlitzWho'', /* Table name where you are storing sp_BlitzWho output, Set to NULL to ignore */ +@Databasename = NULL, /* Filters results for BlitzCache, FileStats (will also include tempdb), BlitzWho. Leave as NULL for all databases */ +@MaxBlitzFirstPriority = 249, /* Max priority to include in the results */ +@BlitzCacheSortorder = ''cpu'', /* Accepted values ''all'' ''cpu'' ''reads'' ''writes'' ''duration'' ''executions'' ''memory grant'' ''spills'' */ +@WaitStatsTop = 3, /* Controls the top for wait stats only */ +@Maxdop = 1, /* Control the degree of parallelism that the queries within this proc can use if they want to*/ +@Debug = 0; /* Show sp_BlitzAnalysis SQL commands in the messages tab as they execute */ + +/* +Additional parameters: +@ReadLatencyThreshold INT /* Default: 100 - Sets the threshold in ms to compare against io_stall_read_average_ms in your filestats table */ +@WriteLatencyThreshold INT /* Default: 100 - Sets the threshold in ms to compare against io_stall_write_average_ms in your filestats table */ +@BringThePain BIT /* Default: 0 - If you are getting more than 4 hours of data with blitzcachesortorder set to ''all'' you will need to set BringThePain to 1 */ +*/'; + RETURN; +END + +/* Declare all local variables required */ +DECLARE @FullOutputTableNameBlitzFirst NVARCHAR(1000); +DECLARE @FullOutputTableNameFileStats NVARCHAR(1000); +DECLARE @FullOutputTableNamePerfmonStats NVARCHAR(1000); +DECLARE @FullOutputTableNameWaitStats NVARCHAR(1000); +DECLARE @FullOutputTableNameBlitzCache NVARCHAR(1000); +DECLARE @FullOutputTableNameBlitzWho NVARCHAR(1000); +DECLARE @Sql NVARCHAR(MAX); +DECLARE @NewLine NVARCHAR(2) = CHAR(13); +DECLARE @IncludeMemoryGrants BIT; +DECLARE @IncludeSpills BIT; + +/* Validate the database name */ +IF (DB_ID(@OutputDatabaseName) IS NULL) +BEGIN + RAISERROR('Invalid database name provided for parameter @OutputDatabaseName: %s',11,0,@OutputDatabaseName); + RETURN; +END + +/* Set fully qualified table names */ +SET @FullOutputTableNameBlitzFirst = QUOTENAME(@OutputDatabaseName)+N'.'+QUOTENAME(@OutputSchemaName)+N'.'+QUOTENAME(@OutputTableNameBlitzFirst); +SET @FullOutputTableNameFileStats = QUOTENAME(@OutputDatabaseName)+N'.'+QUOTENAME(@OutputSchemaName)+N'.'+QUOTENAME(@OutputTableNameFileStats+N'_Deltas'); +SET @FullOutputTableNamePerfmonStats = QUOTENAME(@OutputDatabaseName)+N'.'+QUOTENAME(@OutputSchemaName)+N'.'+QUOTENAME(@OutputTableNamePerfmonStats+N'_Actuals'); +SET @FullOutputTableNameWaitStats = QUOTENAME(@OutputDatabaseName)+N'.'+QUOTENAME(@OutputSchemaName)+N'.'+QUOTENAME(@OutputTableNameWaitStats+N'_Deltas'); +SET @FullOutputTableNameBlitzCache = QUOTENAME(@OutputDatabaseName)+N'.'+QUOTENAME(@OutputSchemaName)+N'.'+QUOTENAME(@OutputTableNameBlitzCache); +SET @FullOutputTableNameBlitzWho = QUOTENAME(@OutputDatabaseName)+N'.'+QUOTENAME(@OutputSchemaName)+N'.'+QUOTENAME(@OutputTableNameBlitzWho+N'_Deltas'); + +IF OBJECT_ID('tempdb.dbo.#BlitzFirstCounts') IS NOT NULL +BEGIN + DROP TABLE #BlitzFirstCounts; +END + +CREATE TABLE #BlitzFirstCounts ( + [Priority] TINYINT NOT NULL, + [FindingsGroup] VARCHAR(50) NOT NULL, + [Finding] VARCHAR(200) NOT NULL, + [TotalOccurrences] INT NULL, + [FirstOccurrence] DATETIMEOFFSET(7) NULL, + [LastOccurrence] DATETIMEOFFSET(7) NULL +); + +/* Validate variables and set defaults as required */ +IF (@BlitzCacheSortorder IS NULL) +BEGIN + SET @BlitzCacheSortorder = N'cpu'; +END + +SET @BlitzCacheSortorder = LOWER(@BlitzCacheSortorder); + +IF (@OutputTableNameBlitzCache IS NOT NULL AND @BlitzCacheSortorder NOT IN (N'all',N'cpu',N'reads',N'writes',N'duration',N'executions',N'memory grant',N'spills')) +BEGIN + RAISERROR('Invalid sort option specified for @BlitzCacheSortorder, supported values are ''all'', ''cpu'', ''reads'', ''writes'', ''duration'', ''executions'', ''memory grant'', ''spills''',11,0) WITH NOWAIT; + RETURN; +END + +/* Set @Maxdop to 1 if NULL was passed in */ +IF (@Maxdop IS NULL) +BEGIN + SET @Maxdop = 1; +END + +/* iF @Maxdop is set higher than the core count just set it to 0 */ +IF (@Maxdop > (SELECT CAST(cpu_count AS INT) FROM sys.dm_os_sys_info)) +BEGIN + SET @Maxdop = 0; +END + +/* We need to check if your SQL version has memory grant and spills columns in sys.dm_exec_query_stats */ +SELECT @IncludeMemoryGrants = + CASE + WHEN (EXISTS(SELECT * FROM sys.all_columns WHERE [object_id] = OBJECT_ID('sys.dm_exec_query_stats') AND name = 'max_grant_kb')) THEN 1 + ELSE 0 + END; + +SELECT @IncludeSpills = + CASE + WHEN (EXISTS(SELECT * FROM sys.all_columns WHERE [object_id] = OBJECT_ID('sys.dm_exec_query_stats') AND name = 'max_spills')) THEN 1 + ELSE 0 + END; + + +IF (@StartDate IS NULL) +BEGIN + RAISERROR('Setting @StartDate to: 1 hour ago',0,0) WITH NOWAIT; + /* Set StartDate to be an hour ago */ + SET @StartDate = DATEADD(HOUR,-1,SYSDATETIMEOFFSET()); + + IF (@EndDate IS NULL) + BEGIN + RAISERROR('Setting @EndDate to: Now',0,0) WITH NOWAIT; + /* Get data right up to now */ + SET @EndDate = SYSDATETIMEOFFSET(); + END +END + +IF (@EndDate IS NULL) +BEGIN + /* Default to an hour of data or SYSDATETIMEOFFSET() if now is earlier than the hour added to @StartDate */ + IF(DATEADD(HOUR,1,@StartDate) < SYSDATETIMEOFFSET()) + BEGIN + RAISERROR('@EndDate was NULL - Setting to return 1 hour of information, if you want more then set @EndDate aswell',0,0) WITH NOWAIT; + SET @EndDate = DATEADD(HOUR,1,@StartDate); + END + ELSE + BEGIN + RAISERROR('@EndDate was NULL - Setting to SYSDATETIMEOFFSET()',0,0) WITH NOWAIT; + SET @EndDate = SYSDATETIMEOFFSET(); + END +END + +/* Default to dbo schema if NULL is passed in */ +IF (@OutputSchemaName IS NULL) +BEGIN + SET @OutputSchemaName = 'dbo'; +END + +/* Prompt the user for @BringThePain = 1 if they are searching a timeframe greater than 4 hours and they are using BlitzCacheSortorder = 'all' */ +IF(@BlitzCacheSortorder = 'all' AND DATEDIFF(HOUR,@StartDate,@EndDate) > 4 AND @BringThePain = 0) +BEGIN + RAISERROR('Wow! hold up now, are you sure you wanna do this? Are sure you want to query over 4 hours of data with @BlitzCacheSortorder set to ''all''? IF you do then set @BringThePain = 1 but I gotta warn you this might hurt a bit!',11,1) WITH NOWAIT; + RETURN; +END + +/* Output report window information */ +SELECT + @Servername AS [ServerToReportOn], + CAST(1 AS NVARCHAR(20)) + N' - '+ CAST(@MaxBlitzFirstPriority AS NVARCHAR(20)) AS [PrioritesToInclude], + @StartDate AS [StartDatetime], + @EndDate AS [EndDatetime];; + + +/* BlitzFirst data */ +SET @Sql = N' +INSERT INTO #BlitzFirstCounts ([Priority],[FindingsGroup],[Finding],[TotalOccurrences],[FirstOccurrence],[LastOccurrence]) +SELECT +[Priority], +[FindingsGroup], +[Finding], +COUNT(*) AS [TotalOccurrences], +MIN(CheckDate) AS [FirstOccurrence], +MAX(CheckDate) AS [LastOccurrence] +FROM '+@FullOutputTableNameBlitzFirst+N' +WHERE [ServerName] = @Servername +AND [Priority] BETWEEN 1 AND @MaxBlitzFirstPriority +AND CheckDate BETWEEN @StartDate AND @EndDate +AND [CheckID] > -1 +GROUP BY [Priority],[FindingsGroup],[Finding]; + +IF EXISTS(SELECT 1 FROM #BlitzFirstCounts) +BEGIN + SELECT + [Priority], + [FindingsGroup], + [Finding], + [TotalOccurrences], + [FirstOccurrence], + [LastOccurrence] + FROM #BlitzFirstCounts + ORDER BY [Priority] ASC,[TotalOccurrences] DESC; +END +ELSE +BEGIN + SELECT N''No findings with a priority between 1 and ''+CAST(@MaxBlitzFirstPriority AS NVARCHAR(10))+N'' found for this period''; +END +SELECT + [ServerName] +,[CheckDate] +,[CheckID] +,[Priority] +,[Finding] +,[URL] +,[Details] +,[HowToStopIt] +,[QueryPlan] +,[QueryText] +FROM '+@FullOutputTableNameBlitzFirst+N' Findings +WHERE [ServerName] = @Servername +AND [Priority] BETWEEN 1 AND @MaxBlitzFirstPriority +AND [CheckDate] BETWEEN @StartDate AND @EndDate +AND [CheckID] > -1 +ORDER BY CheckDate ASC,[Priority] ASC +OPTION (RECOMPILE, MAXDOP '+CAST(@Maxdop AS NVARCHAR(2))+N');'; + + +RAISERROR('Getting BlitzFirst info from %s',0,0,@FullOutputTableNameBlitzFirst) WITH NOWAIT; + +IF (@Debug = 1) +BEGIN + PRINT @Sql; +END + +IF (OBJECT_ID(@FullOutputTableNameBlitzFirst) IS NULL) +BEGIN + IF (@OutputTableNameBlitzFirst IS NULL) + BEGIN + RAISERROR('BlitzFirst data skipped',10,0); + SELECT N'Skipped logged BlitzFirst data as NULL was passed to parameter @OutputTableNameBlitzFirst'; + END + ELSE + BEGIN + RAISERROR('Table provided for BlitzFirst data: %s does not exist',10,0,@FullOutputTableNameBlitzFirst); + SELECT N'No BlitzFirst data available as the table cannot be found'; + END + +END +ELSE /* Table exists then run the query */ +BEGIN + EXEC sp_executesql @Sql, + N'@StartDate DATETIMEOFFSET(7), + @EndDate DATETIMEOFFSET(7), + @Servername NVARCHAR(128), + @MaxBlitzFirstPriority INT', + @StartDate=@StartDate, + @EndDate=@EndDate, + @Servername=@Servername, + @MaxBlitzFirstPriority = @MaxBlitzFirstPriority; +END + +/* Blitz WaitStats data */ +SET @Sql = N'SELECT +[ServerName], +[CheckDate], +[wait_type], +[WaitsRank], +[WaitCategory], +[Ignorable], +[ElapsedSeconds], +[wait_time_ms_delta], +[wait_time_minutes_delta], +[wait_time_minutes_per_minute], +[signal_wait_time_ms_delta], +[waiting_tasks_count_delta], +ISNULL((CAST([wait_time_ms_delta] AS DECIMAL(38,2))/NULLIF(CAST([waiting_tasks_count_delta] AS DECIMAL(38,2)),0)),0) AS [wait_time_ms_per_wait] +FROM +( + SELECT + [ServerName], + [CheckDate], + [wait_type], + [WaitCategory], + [Ignorable], + [ElapsedSeconds], + [wait_time_ms_delta], + [wait_time_minutes_delta], + [wait_time_minutes_per_minute], + [signal_wait_time_ms_delta], + [waiting_tasks_count_delta], + ROW_NUMBER() OVER(PARTITION BY [CheckDate] ORDER BY [CheckDate] ASC,[wait_time_ms_delta] DESC) AS [WaitsRank] + FROM '+@FullOutputTableNameWaitStats+N' AS [Waits] + WHERE [ServerName] = @Servername + AND [CheckDate] BETWEEN @StartDate AND @EndDate +) TopWaits +WHERE [WaitsRank] <= @WaitStatsTop +ORDER BY +[CheckDate] ASC, +[wait_time_ms_delta] DESC +OPTION(RECOMPILE, MAXDOP '+CAST(@Maxdop AS NVARCHAR(2))+N');' + +RAISERROR('Getting wait stats info from %s',0,0,@FullOutputTableNameWaitStats) WITH NOWAIT; + +IF (@Debug = 1) +BEGIN + PRINT @Sql; +END + +IF (OBJECT_ID(@FullOutputTableNameWaitStats) IS NULL) +BEGIN + IF (@OutputTableNameWaitStats IS NULL) + BEGIN + RAISERROR('Wait stats data skipped',10,0); + SELECT N'Skipped logged wait stats data as NULL was passed to parameter @OutputTableNameWaitStats'; + END + ELSE + BEGIN + RAISERROR('Table provided for wait stats data: %s does not exist',10,0,@FullOutputTableNameWaitStats); + SELECT N'No wait stats data available as the table cannot be found'; + END +END +ELSE /* Table exists then run the query */ +BEGIN + EXEC sp_executesql @Sql, + N'@StartDate DATETIMEOFFSET(7), + @EndDate DATETIMEOFFSET(7), + @Servername NVARCHAR(128), + @WaitStatsTop TINYINT', + @StartDate=@StartDate, + @EndDate=@EndDate, + @Servername=@Servername, + @WaitStatsTop=@WaitStatsTop; +END + +/* BlitzFileStats info */ +SET @Sql = N' +SELECT +[ServerName], +[CheckDate], +CASE + WHEN MAX([io_stall_read_ms_average]) > @ReadLatencyThreshold THEN ''Yes'' + WHEN MAX([io_stall_write_ms_average]) > @WriteLatencyThreshold THEN ''Yes'' + ELSE ''No'' +END AS [io_stall_ms_breached], +LEFT([PhysicalName],LEN([PhysicalName])-CHARINDEX(''\'',REVERSE([PhysicalName]))+1) AS [PhysicalPath], +SUM([SizeOnDiskMB]) AS [SizeOnDiskMB], +SUM([SizeOnDiskMBgrowth]) AS [SizeOnDiskMBgrowth], +MAX([io_stall_read_ms]) AS [max_io_stall_read_ms], +MAX([io_stall_read_ms_average]) AS [max_io_stall_read_ms_average], +@ReadLatencyThreshold AS [is_stall_read_ms_threshold], +SUM([num_of_reads]) AS [num_of_reads], +SUM([megabytes_read]) AS [megabytes_read], +MAX([io_stall_write_ms]) AS [max_io_stall_write_ms], +MAX([io_stall_write_ms_average]) AS [max_io_stall_write_ms_average], +@WriteLatencyThreshold AS [io_stall_write_ms_average], +SUM([num_of_writes]) AS [num_of_writes], +SUM([megabytes_written]) AS [megabytes_written] +FROM '+@FullOutputTableNameFileStats+N' +WHERE [ServerName] = @Servername +AND [CheckDate] BETWEEN @StartDate AND @EndDate +' ++CASE + WHEN @Databasename IS NOT NULL THEN N'AND [DatabaseName] IN (N''tempdb'',@Databasename) +' + ELSE N'' +END ++N'GROUP BY +[ServerName], +[CheckDate], +LEFT([PhysicalName],LEN([PhysicalName])-CHARINDEX(''\'',REVERSE([PhysicalName]))+1) +ORDER BY +[CheckDate] ASC +OPTION (RECOMPILE, MAXDOP '+CAST(@Maxdop AS NVARCHAR(2))+N');' + +RAISERROR('Getting FileStats info from %s',0,0,@FullOutputTableNameFileStats) WITH NOWAIT; + +IF (@Debug = 1) +BEGIN + PRINT @Sql; +END + +IF (OBJECT_ID(@FullOutputTableNameFileStats) IS NULL) +BEGIN + IF (@OutputTableNameFileStats IS NULL) + BEGIN + RAISERROR('File stats data skipped',10,0); + SELECT N'Skipped logged File stats data as NULL was passed to parameter @OutputTableNameFileStats'; + END + ELSE + BEGIN + RAISERROR('Table provided for FileStats data: %s does not exist',10,0,@FullOutputTableNameFileStats); + SELECT N'No File stats data available as the table cannot be found'; + END +END +ELSE /* Table exists then run the query */ +BEGIN + EXEC sp_executesql @Sql, + N'@StartDate DATETIMEOFFSET(7), + @EndDate DATETIMEOFFSET(7), + @Servername NVARCHAR(128), + @Databasename NVARCHAR(128), + @ReadLatencyThreshold INT, + @WriteLatencyThreshold INT', + @StartDate=@StartDate, + @EndDate=@EndDate, + @Servername=@Servername, + @Databasename = @Databasename, + @ReadLatencyThreshold = @ReadLatencyThreshold, + @WriteLatencyThreshold = @WriteLatencyThreshold; +END + +/* Blitz Perfmon stats*/ +SET @Sql = N' +SELECT + [ServerName] + ,[CheckDate] + ,[counter_name] + ,[object_name] + ,[instance_name] + ,[cntr_value] +FROM '+@FullOutputTableNamePerfmonStats+N' +WHERE [ServerName] = @Servername +AND CheckDate BETWEEN @StartDate AND @EndDate +ORDER BY + [CheckDate] ASC, + [counter_name] ASC +OPTION (RECOMPILE, MAXDOP '+CAST(@Maxdop AS NVARCHAR(2))+N');' + +RAISERROR('Getting Perfmon info from %s',0,0,@FullOutputTableNamePerfmonStats) WITH NOWAIT; + +IF (@Debug = 1) +BEGIN + PRINT @Sql; +END + +IF (OBJECT_ID(@FullOutputTableNamePerfmonStats) IS NULL) +BEGIN + IF (@OutputTableNamePerfmonStats IS NULL) + BEGIN + RAISERROR('Perfmon stats data skipped',10,0); + SELECT N'Skipped logged Perfmon stats data as NULL was passed to parameter @OutputTableNamePerfmonStats'; + END + ELSE + BEGIN + RAISERROR('Table provided for Perfmon stats data: %s does not exist',10,0,@FullOutputTableNamePerfmonStats); + SELECT N'No Perfmon data available as the table cannot be found'; + END +END +ELSE /* Table exists then run the query */ +BEGIN + EXEC sp_executesql @Sql, + N'@StartDate DATETIMEOFFSET(7), + @EndDate DATETIMEOFFSET(7), + @Servername NVARCHAR(128)', + @StartDate=@StartDate, + @EndDate=@EndDate, + @Servername=@Servername; +END + +/* Blitz cache data */ +RAISERROR('Sortorder for BlitzCache data: %s',0,0,@BlitzCacheSortorder) WITH NOWAIT; + +/* Set intial CTE */ +SET @Sql = N'WITH CheckDates AS ( +SELECT DISTINCT CheckDate +FROM ' ++@FullOutputTableNameBlitzCache ++N' +WHERE [ServerName] = @Servername +AND [CheckDate] BETWEEN @StartDate AND @EndDate' ++@NewLine ++CASE + WHEN @Databasename IS NOT NULL THEN N'AND [DatabaseName] = @Databasename'+@NewLine + ELSE N'' +END ++N')' +; + +SET @Sql += @NewLine; + +/* Append additional CTEs based on sortorder */ +SET @Sql += ( +SELECT CAST(N',' AS NVARCHAR(MAX)) ++[SortOptions].[Aliasname]+N' AS ( +SELECT + [ServerName] + ,'+[SortOptions].[Aliasname]+N'.[CheckDate] + ,[Sortorder] + ,[TimeFrameRank] + ,ROW_NUMBER() OVER(ORDER BY ['+[SortOptions].[Columnname]+N'] DESC) AS [OverallRank] + ,'+[SortOptions].[Aliasname]+N'.[QueryType] + ,'+[SortOptions].[Aliasname]+N'.[QueryText] + ,'+[SortOptions].[Aliasname]+N'.[DatabaseName] + ,'+[SortOptions].[Aliasname]+N'.[AverageCPU] + ,'+[SortOptions].[Aliasname]+N'.[TotalCPU] + ,'+[SortOptions].[Aliasname]+N'.[PercentCPUByType] + ,'+[SortOptions].[Aliasname]+N'.[AverageDuration] + ,'+[SortOptions].[Aliasname]+N'.[TotalDuration] + ,'+[SortOptions].[Aliasname]+N'.[PercentDurationByType] + ,'+[SortOptions].[Aliasname]+N'.[AverageReads] + ,'+[SortOptions].[Aliasname]+N'.[TotalReads] + ,'+[SortOptions].[Aliasname]+N'.[PercentReadsByType] + ,'+[SortOptions].[Aliasname]+N'.[AverageWrites] + ,'+[SortOptions].[Aliasname]+N'.[TotalWrites] + ,'+[SortOptions].[Aliasname]+N'.[PercentWritesByType] + ,'+[SortOptions].[Aliasname]+N'.[ExecutionCount] + ,'+[SortOptions].[Aliasname]+N'.[ExecutionWeight] + ,'+[SortOptions].[Aliasname]+N'.[PercentExecutionsByType] + ,'+[SortOptions].[Aliasname]+N'.[ExecutionsPerMinute] + ,'+[SortOptions].[Aliasname]+N'.[PlanCreationTime] + ,'+[SortOptions].[Aliasname]+N'.[PlanCreationTimeHours] + ,'+[SortOptions].[Aliasname]+N'.[LastExecutionTime] + ,'+[SortOptions].[Aliasname]+N'.[PlanHandle] + ,'+[SortOptions].[Aliasname]+N'.[SqlHandle] + ,'+[SortOptions].[Aliasname]+N'.[SQL Handle More Info] + ,'+[SortOptions].[Aliasname]+N'.[QueryHash] + ,'+[SortOptions].[Aliasname]+N'.[Query Hash More Info] + ,'+[SortOptions].[Aliasname]+N'.[QueryPlanHash] + ,'+[SortOptions].[Aliasname]+N'.[StatementStartOffset] + ,'+[SortOptions].[Aliasname]+N'.[StatementEndOffset] + ,'+[SortOptions].[Aliasname]+N'.[MinReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[MaxReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[AverageReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[TotalReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[QueryPlan] + ,'+[SortOptions].[Aliasname]+N'.[NumberOfPlans] + ,'+[SortOptions].[Aliasname]+N'.[NumberOfDistinctPlans] + ,'+[SortOptions].[Aliasname]+N'.[MinGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[MaxGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[MinUsedGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[MaxUsedGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[PercentMemoryGrantUsed] + ,'+[SortOptions].[Aliasname]+N'.[AvgMaxMemoryGrant] + ,'+[SortOptions].[Aliasname]+N'.[MinSpills] + ,'+[SortOptions].[Aliasname]+N'.[MaxSpills] + ,'+[SortOptions].[Aliasname]+N'.[TotalSpills] + ,'+[SortOptions].[Aliasname]+N'.[AvgSpills] + ,'+[SortOptions].[Aliasname]+N'.[QueryPlanCost] +FROM CheckDates +CROSS APPLY ( + SELECT TOP (5) + [ServerName] + ,'+[SortOptions].[Aliasname]+N'.[CheckDate] + ,'+QUOTENAME(UPPER([SortOptions].[Sortorder]),N'''')+N' AS [Sortorder] + ,ROW_NUMBER() OVER(ORDER BY ['+[SortOptions].[Columnname]+N'] DESC) AS [TimeFrameRank] + ,'+[SortOptions].[Aliasname]+N'.[QueryType] + ,'+[SortOptions].[Aliasname]+N'.[QueryText] + ,'+[SortOptions].[Aliasname]+N'.[DatabaseName] + ,'+[SortOptions].[Aliasname]+N'.[AverageCPU] + ,'+[SortOptions].[Aliasname]+N'.[TotalCPU] + ,'+[SortOptions].[Aliasname]+N'.[PercentCPUByType] + ,'+[SortOptions].[Aliasname]+N'.[AverageDuration] + ,'+[SortOptions].[Aliasname]+N'.[TotalDuration] + ,'+[SortOptions].[Aliasname]+N'.[PercentDurationByType] + ,'+[SortOptions].[Aliasname]+N'.[AverageReads] + ,'+[SortOptions].[Aliasname]+N'.[TotalReads] + ,'+[SortOptions].[Aliasname]+N'.[PercentReadsByType] + ,'+[SortOptions].[Aliasname]+N'.[AverageWrites] + ,'+[SortOptions].[Aliasname]+N'.[TotalWrites] + ,'+[SortOptions].[Aliasname]+N'.[PercentWritesByType] + ,'+[SortOptions].[Aliasname]+N'.[ExecutionCount] + ,'+[SortOptions].[Aliasname]+N'.[ExecutionWeight] + ,'+[SortOptions].[Aliasname]+N'.[PercentExecutionsByType] + ,'+[SortOptions].[Aliasname]+N'.[ExecutionsPerMinute] + ,'+[SortOptions].[Aliasname]+N'.[PlanCreationTime] + ,'+[SortOptions].[Aliasname]+N'.[PlanCreationTimeHours] + ,'+[SortOptions].[Aliasname]+N'.[LastExecutionTime] + ,'+[SortOptions].[Aliasname]+N'.[PlanHandle] + ,'+[SortOptions].[Aliasname]+N'.[SqlHandle] + ,'+[SortOptions].[Aliasname]+N'.[SQL Handle More Info] + ,'+[SortOptions].[Aliasname]+N'.[QueryHash] + ,'+[SortOptions].[Aliasname]+N'.[Query Hash More Info] + ,'+[SortOptions].[Aliasname]+N'.[QueryPlanHash] + ,'+[SortOptions].[Aliasname]+N'.[StatementStartOffset] + ,'+[SortOptions].[Aliasname]+N'.[StatementEndOffset] + ,'+[SortOptions].[Aliasname]+N'.[MinReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[MaxReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[AverageReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[TotalReturnedRows] + ,'+[SortOptions].[Aliasname]+N'.[QueryPlan] + ,'+[SortOptions].[Aliasname]+N'.[NumberOfPlans] + ,'+[SortOptions].[Aliasname]+N'.[NumberOfDistinctPlans] + ,'+[SortOptions].[Aliasname]+N'.[MinGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[MaxGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[MinUsedGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[MaxUsedGrantKB] + ,'+[SortOptions].[Aliasname]+N'.[PercentMemoryGrantUsed] + ,'+[SortOptions].[Aliasname]+N'.[AvgMaxMemoryGrant] + ,'+[SortOptions].[Aliasname]+N'.[MinSpills] + ,'+[SortOptions].[Aliasname]+N'.[MaxSpills] + ,'+[SortOptions].[Aliasname]+N'.[TotalSpills] + ,'+[SortOptions].[Aliasname]+N'.[AvgSpills] + ,'+[SortOptions].[Aliasname]+N'.[QueryPlanCost] + FROM '+@FullOutputTableNameBlitzCache+N' AS '+[SortOptions].[Aliasname]+N' + WHERE [ServerName] = @Servername + AND [CheckDate] BETWEEN @StartDate AND @EndDate + AND ['+[SortOptions].[Aliasname]+N'].[CheckDate] = [CheckDates].[CheckDate]' + +@NewLine + +CASE + WHEN @Databasename IS NOT NULL THEN N'AND ['+[SortOptions].[Aliasname]+N'].[DatabaseName] = @Databasename'+@NewLine + ELSE N'' + END + +CASE + WHEN [Sortorder] = N'cpu' THEN N'AND [TotalCPU] > 0' + WHEN [Sortorder] = N'reads' THEN N'AND [TotalReads] > 0' + WHEN [Sortorder] = N'writes' THEN N'AND [TotalWrites] > 0' + WHEN [Sortorder] = N'duration' THEN N'AND [TotalDuration] > 0' + WHEN [Sortorder] = N'executions' THEN N'AND [ExecutionCount] > 0' + WHEN [Sortorder] = N'memory grant' THEN N'AND [MaxGrantKB] > 0' + WHEN [Sortorder] = N'spills' THEN N'AND [MaxSpills] > 0' + ELSE N'' + END + +N' + ORDER BY ['+[SortOptions].[Columnname]+N'] DESC) '+[SortOptions].[Aliasname]+N' +)' +FROM (VALUES + (N'cpu',N'TopCPU',N'TotalCPU'), + (N'reads',N'TopReads',N'TotalReads'), + (N'writes',N'TopWrites',N'TotalWrites'), + (N'duration',N'TopDuration',N'TotalDuration'), + (N'executions',N'TopExecutions',N'ExecutionCount'), + (N'memory grant',N'TopMemoryGrants',N'MaxGrantKB'), + (N'spills',N'TopSpills',N'MaxSpills') + ) SortOptions(Sortorder,Aliasname,Columnname) +WHERE + CASE /* for spills and memory grant sorts make sure the underlying columns exist in the DMV otherwise do not include them */ + WHEN (@IncludeMemoryGrants = 0 OR @IncludeMemoryGrants IS NULL) AND ([SortOptions].[Sortorder] = N'memory grant' OR [SortOptions].[Sortorder] = N'all') THEN NULL + WHEN (@IncludeSpills = 0 OR @IncludeSpills IS NULL) AND ([SortOptions].[Sortorder] = N'spills' OR [SortOptions].[Sortorder] = N'all') THEN NULL + ELSE [SortOptions].[Sortorder] + END = ISNULL(NULLIF(@BlitzCacheSortorder,N'all'),[SortOptions].[Sortorder]) +FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'); + +SET @Sql += @NewLine; + +/* Build the select statements to return the data after CTE declarations */ +SET @Sql += ( +SELECT STUFF(( +SELECT @NewLine ++N'UNION ALL' ++@NewLine ++N'SELECT * +FROM '+[SortOptions].[Aliasname] +FROM (VALUES + (N'cpu',N'TopCPU',N'TotalCPU'), + (N'reads',N'TopReads',N'TotalReads'), + (N'writes',N'TopWrites',N'TotalWrites'), + (N'duration',N'TopDuration',N'TotalDuration'), + (N'executions',N'TopExecutions',N'ExecutionCount'), + (N'memory grant',N'TopMemoryGrants',N'MaxGrantKB'), + (N'spills',N'TopSpills',N'MaxSpills') + ) SortOptions(Sortorder,Aliasname,Columnname) +WHERE + CASE /* for spills and memory grant sorts make sure the underlying columns exist in the DMV otherwise do not include them */ + WHEN (@IncludeMemoryGrants = 0 OR @IncludeMemoryGrants IS NULL) AND ([SortOptions].[Sortorder] = N'memory grant' OR [SortOptions].[Sortorder] = N'all') THEN NULL + WHEN (@IncludeSpills = 0 OR @IncludeSpills IS NULL) AND ([SortOptions].[Sortorder] = N'spills' OR [SortOptions].[Sortorder] = N'all') THEN NULL + ELSE [SortOptions].[Sortorder] + END = ISNULL(NULLIF(@BlitzCacheSortorder,N'all'),[SortOptions].[Sortorder]) +FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'),1,11,N'') +); + +/* Append Order By */ +SET @Sql += @NewLine ++N'ORDER BY + [Sortorder] ASC, + [CheckDate] ASC, + [TimeFrameRank] ASC'; + +/* Append OPTION(RECOMPILE, MAXDOP) to complete the statement */ +SET @Sql += @NewLine ++N'OPTION(RECOMPILE, MAXDOP '+CAST(@Maxdop AS NVARCHAR(2))+N');'; + +RAISERROR('Getting BlitzCache info from %s',0,0,@FullOutputTableNameBlitzCache) WITH NOWAIT; + +IF (@Debug = 1) +BEGIN + PRINT SUBSTRING(@Sql, 0, 4000); + PRINT SUBSTRING(@Sql, 4000, 8000); + PRINT SUBSTRING(@Sql, 8000, 12000); + PRINT SUBSTRING(@Sql, 12000, 16000); + PRINT SUBSTRING(@Sql, 16000, 20000); + PRINT SUBSTRING(@Sql, 20000, 24000); + PRINT SUBSTRING(@Sql, 24000, 28000); +END + +IF (OBJECT_ID(@FullOutputTableNameBlitzCache) IS NULL) +BEGIN + IF (@OutputTableNameBlitzCache IS NULL) + BEGIN + RAISERROR('BlitzCache data skipped',10,0); + SELECT N'Skipped logged BlitzCache data as NULL was passed to parameter @OutputTableNameBlitzCache'; + END + ELSE + BEGIN + RAISERROR('Table provided for BlitzCache data: %s does not exist',10,0,@FullOutputTableNameBlitzCache); + SELECT N'No BlitzCache data available as the table cannot be found'; + END +END +ELSE /* Table exists then run the query */ +BEGIN + EXEC sp_executesql @Sql, + N'@Servername NVARCHAR(128), + @Databasename NVARCHAR(128), + @BlitzCacheSortorder NVARCHAR(20), + @StartDate DATETIMEOFFSET(7), + @EndDate DATETIMEOFFSET(7)', + @Servername = @Servername, + @Databasename = @Databasename, + @BlitzCacheSortorder = @BlitzCacheSortorder, + @StartDate = @StartDate, + @EndDate = @EndDate; +END + + + +/* BlitzWho data */ +SET @Sql = N' +SELECT [ServerName] + ,[CheckDate] + ,[elapsed_time] + ,[session_id] + ,[database_name] + ,[query_text_snippet] + ,[query_plan] + ,[live_query_plan] + ,[query_cost] + ,[status] + ,[wait_info] + ,[top_session_waits] + ,[blocking_session_id] + ,[open_transaction_count] + ,[is_implicit_transaction] + ,[nt_domain] + ,[host_name] + ,[login_name] + ,[nt_user_name] + ,[program_name] + ,[fix_parameter_sniffing] + ,[client_interface_name] + ,[login_time] + ,[start_time] + ,[request_time] + ,[request_cpu_time] + ,[degree_of_parallelism] + ,[request_logical_reads] + ,[Logical_Reads_MB] + ,[request_writes] + ,[Logical_Writes_MB] + ,[request_physical_reads] + ,[Physical_reads_MB] + ,[session_cpu] + ,[session_logical_reads] + ,[session_logical_reads_MB] + ,[session_physical_reads] + ,[session_physical_reads_MB] + ,[session_writes] + ,[session_writes_MB] + ,[tempdb_allocations_mb] + ,[memory_usage] + ,[estimated_completion_time] + ,[percent_complete] + ,[deadlock_priority] + ,[transaction_isolation_level] + ,[last_dop] + ,[min_dop] + ,[max_dop] + ,[last_grant_kb] + ,[min_grant_kb] + ,[max_grant_kb] + ,[last_used_grant_kb] + ,[min_used_grant_kb] + ,[max_used_grant_kb] + ,[last_ideal_grant_kb] + ,[min_ideal_grant_kb] + ,[max_ideal_grant_kb] + ,[last_reserved_threads] + ,[min_reserved_threads] + ,[max_reserved_threads] + ,[last_used_threads] + ,[min_used_threads] + ,[max_used_threads] + ,[grant_time] + ,[requested_memory_kb] + ,[grant_memory_kb] + ,[is_request_granted] + ,[required_memory_kb] + ,[query_memory_grant_used_memory_kb] + ,[ideal_memory_kb] + ,[is_small] + ,[timeout_sec] + ,[resource_semaphore_id] + ,[wait_order] + ,[wait_time_ms] + ,[next_candidate_for_memory_grant] + ,[target_memory_kb] + ,[max_target_memory_kb] + ,[total_memory_kb] + ,[available_memory_kb] + ,[granted_memory_kb] + ,[query_resource_semaphore_used_memory_kb] + ,[grantee_count] + ,[waiter_count] + ,[timeout_error_count] + ,[forced_grant_count] + ,[workload_group_name] + ,[resource_pool_name] + ,[context_info] + ,[query_hash] + ,[query_plan_hash] + ,[sql_handle] + ,[plan_handle] + ,[statement_start_offset] + ,[statement_end_offset] + FROM '+@FullOutputTableNameBlitzWho+N' + WHERE [ServerName] = @Servername + AND ([CheckDate] BETWEEN @StartDate AND @EndDate OR [start_time] BETWEEN CAST(@StartDate AS DATETIME) AND CAST(@EndDate AS DATETIME)) + ' + +CASE + WHEN @Databasename IS NOT NULL THEN N'AND [database_name] = @Databasename + ' + ELSE N'' + END ++N'ORDER BY [CheckDate] ASC + OPTION (RECOMPILE, MAXDOP '+CAST(@Maxdop AS NVARCHAR(2))+N');'; + +RAISERROR('Getting BlitzWho info from %s',0,0,@FullOutputTableNameBlitzWho) WITH NOWAIT; + +IF (@Debug = 1) +BEGIN + PRINT @Sql; +END + +IF (OBJECT_ID(@FullOutputTableNameBlitzWho) IS NULL) +BEGIN + IF (@OutputTableNameBlitzWho IS NULL) + BEGIN + RAISERROR('BlitzWho data skipped',10,0); + SELECT N'Skipped logged BlitzWho data as NULL was passed to parameter @OutputTableNameBlitzWho'; + END + ELSE + BEGIN + RAISERROR('Table provided for BlitzWho data: %s does not exist',10,0,@FullOutputTableNameBlitzWho); + SELECT N'No BlitzWho data available as the table cannot be found'; + END +END +ELSE +BEGIN + EXEC sp_executesql @Sql, + N'@StartDate DATETIMEOFFSET(7), + @EndDate DATETIMEOFFSET(7), + @Servername NVARCHAR(128), + @Databasename NVARCHAR(128)', + @StartDate=@StartDate, + @EndDate=@EndDate, + @Servername=@Servername, + @Databasename = @Databasename; +END + + +GO +IF OBJECT_ID('dbo.sp_BlitzBackups') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_BlitzBackups AS RETURN 0;'); +GO +ALTER PROCEDURE [dbo].[sp_BlitzBackups] + @Help TINYINT = 0 , + @HoursBack INT = 168, + @MSDBName NVARCHAR(256) = 'msdb', + @AGName NVARCHAR(256) = NULL, + @RestoreSpeedFullMBps INT = NULL, + @RestoreSpeedDiffMBps INT = NULL, + @RestoreSpeedLogMBps INT = NULL, + @Debug TINYINT = 0, + @PushBackupHistoryToListener BIT = 0, + @WriteBackupsToListenerName NVARCHAR(256) = NULL, + @WriteBackupsToDatabaseName NVARCHAR(256) = NULL, + @WriteBackupsLastHours INT = 168, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS + BEGIN + SET NOCOUNT ON; + SET STATISTICS XML OFF; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + SELECT @Version = '8.16', @VersionDate = '20230820'; + + IF(@VersionCheckMode = 1) + BEGIN + RETURN; + END; + + IF @Help = 1 PRINT ' + /* + sp_BlitzBackups from http://FirstResponderKit.org + + This script checks your backups to see how much data you might lose when + this server fails, and how long it might take to recover. + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + + Parameter explanations: + + @HoursBack INT = 168 How many hours of history to examine, back from now. + You can check just the last 24 hours of backups, for example. + @MSDBName NVARCHAR(255) You can restore MSDB from different servers and check them + centrally. Also useful if you create a DBA utility database + and merge data from several servers in an AG into one DB. + @RestoreSpeedFullMBps INT By default, we use the backup speed from MSDB to guesstimate + how fast your restores will go. If you have done performance + tuning and testing of your backups (or if they horribly go even + slower in your DR environment, and you want to account for + that), then you can pass in different numbers here. + @RestoreSpeedDiffMBps INT See above. + @RestoreSpeedLogMBps INT See above. + + For more documentation: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + + + + */'; +ELSE +BEGIN +DECLARE @StringToExecute NVARCHAR(MAX) = N'', + @InnerStringToExecute NVARCHAR(MAX) = N'', + @ProductVersion NVARCHAR(128), + @ProductVersionMajor DECIMAL(10, 2), + @ProductVersionMinor DECIMAL(10, 2), + @StartTime DATETIME2, @ResultText NVARCHAR(MAX), + @crlf NVARCHAR(2), + @MoreInfoHeader NVARCHAR(100), + @MoreInfoFooter NVARCHAR(100); + +IF @HoursBack > 0 + SET @HoursBack = @HoursBack * -1; + +IF @WriteBackupsLastHours > 0 + SET @WriteBackupsLastHours = @WriteBackupsLastHours * -1; + +SELECT @crlf = NCHAR(13) + NCHAR(10), + @StartTime = DATEADD(hh, @HoursBack, GETDATE()), + @MoreInfoHeader = N''; + +SET @ProductVersion = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)); +SELECT @ProductVersionMajor = SUBSTRING(@ProductVersion, 1, CHARINDEX('.', @ProductVersion) + 1), + @ProductVersionMinor = PARSENAME(CONVERT(VARCHAR(32), @ProductVersion), 2); + +CREATE TABLE #Backups +( + id INT IDENTITY(1, 1), + database_name NVARCHAR(128), + database_guid UNIQUEIDENTIFIER, + RPOWorstCaseMinutes DECIMAL(18, 1), + RTOWorstCaseMinutes DECIMAL(18, 1), + RPOWorstCaseBackupSetID INT, + RPOWorstCaseBackupSetFinishTime DATETIME, + RPOWorstCaseBackupSetIDPrior INT, + RPOWorstCaseBackupSetPriorFinishTime DATETIME, + RPOWorstCaseMoreInfoQuery XML, + RTOWorstCaseBackupFileSizeMB DECIMAL(18, 2), + RTOWorstCaseMoreInfoQuery XML, + FullMBpsAvg DECIMAL(18, 2), + FullMBpsMin DECIMAL(18, 2), + FullMBpsMax DECIMAL(18, 2), + FullSizeMBAvg DECIMAL(18, 2), + FullSizeMBMin DECIMAL(18, 2), + FullSizeMBMax DECIMAL(18, 2), + FullCompressedSizeMBAvg DECIMAL(18, 2), + FullCompressedSizeMBMin DECIMAL(18, 2), + FullCompressedSizeMBMax DECIMAL(18, 2), + DiffMBpsAvg DECIMAL(18, 2), + DiffMBpsMin DECIMAL(18, 2), + DiffMBpsMax DECIMAL(18, 2), + DiffSizeMBAvg DECIMAL(18, 2), + DiffSizeMBMin DECIMAL(18, 2), + DiffSizeMBMax DECIMAL(18, 2), + DiffCompressedSizeMBAvg DECIMAL(18, 2), + DiffCompressedSizeMBMin DECIMAL(18, 2), + DiffCompressedSizeMBMax DECIMAL(18, 2), + LogMBpsAvg DECIMAL(18, 2), + LogMBpsMin DECIMAL(18, 2), + LogMBpsMax DECIMAL(18, 2), + LogSizeMBAvg DECIMAL(18, 2), + LogSizeMBMin DECIMAL(18, 2), + LogSizeMBMax DECIMAL(18, 2), + LogCompressedSizeMBAvg DECIMAL(18, 2), + LogCompressedSizeMBMin DECIMAL(18, 2), + LogCompressedSizeMBMax DECIMAL(18, 2) +); + +CREATE TABLE #RTORecoveryPoints +( + id INT IDENTITY(1, 1), + database_name NVARCHAR(128), + database_guid UNIQUEIDENTIFIER, + rto_worst_case_size_mb AS + ( COALESCE(log_file_size_mb, 0) + COALESCE(diff_file_size_mb, 0) + COALESCE(full_file_size_mb, 0)), + rto_worst_case_time_seconds AS + ( COALESCE(log_time_seconds, 0) + COALESCE(diff_time_seconds, 0) + COALESCE(full_time_seconds, 0)), + full_backup_set_id INT, + full_last_lsn NUMERIC(25, 0), + full_backup_set_uuid UNIQUEIDENTIFIER, + full_time_seconds BIGINT, + full_file_size_mb DECIMAL(18, 2), + diff_backup_set_id INT, + diff_last_lsn NUMERIC(25, 0), + diff_time_seconds BIGINT, + diff_file_size_mb DECIMAL(18, 2), + log_backup_set_id INT, + log_last_lsn NUMERIC(25, 0), + log_time_seconds BIGINT, + log_file_size_mb DECIMAL(18, 2), + log_backups INT +); + +CREATE TABLE #Recoverability + ( + Id INT IDENTITY , + DatabaseName NVARCHAR(128), + DatabaseGUID UNIQUEIDENTIFIER, + LastBackupRecoveryModel NVARCHAR(60), + FirstFullBackupSizeMB DECIMAL (18,2), + FirstFullBackupDate DATETIME, + LastFullBackupSizeMB DECIMAL (18,2), + LastFullBackupDate DATETIME, + AvgFullBackupThroughputMB DECIMAL (18,2), + AvgFullBackupDurationSeconds INT, + AvgDiffBackupThroughputMB DECIMAL (18,2), + AvgDiffBackupDurationSeconds INT, + AvgLogBackupThroughputMB DECIMAL (18,2), + AvgLogBackupDurationSeconds INT, + AvgFullSizeMB DECIMAL (18,2), + AvgDiffSizeMB DECIMAL (18,2), + AvgLogSizeMB DECIMAL (18,2), + IsBigDiff AS CASE WHEN (AvgFullSizeMB > 10240. AND ((AvgDiffSizeMB * 100.) / AvgFullSizeMB >= 40.)) THEN 1 ELSE 0 END, + IsBigLog AS CASE WHEN (AvgFullSizeMB > 10240. AND ((AvgLogSizeMB * 100.) / AvgFullSizeMB >= 20.)) THEN 1 ELSE 0 END + ); + +CREATE TABLE #Trending +( + DatabaseName NVARCHAR(128), + DatabaseGUID UNIQUEIDENTIFIER, + [0] DECIMAL(18, 2), + [-1] DECIMAL(18, 2), + [-2] DECIMAL(18, 2), + [-3] DECIMAL(18, 2), + [-4] DECIMAL(18, 2), + [-5] DECIMAL(18, 2), + [-6] DECIMAL(18, 2), + [-7] DECIMAL(18, 2), + [-8] DECIMAL(18, 2), + [-9] DECIMAL(18, 2), + [-10] DECIMAL(18, 2), + [-11] DECIMAL(18, 2), + [-12] DECIMAL(18, 2) +); + + +CREATE TABLE #Warnings +( + Id INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + CheckId INT, + Priority INT, + DatabaseName VARCHAR(128), + Finding VARCHAR(256), + Warning VARCHAR(8000) +); + +IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = @MSDBName) + BEGIN + RAISERROR('@MSDBName was specified, but the database does not exist.', 16, 1) WITH NOWAIT; + RETURN; + END + +IF @PushBackupHistoryToListener = 1 +GOTO PushBackupHistoryToListener + + + RAISERROR('Inserting to #Backups', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'WITH Backups AS (SELECT bs.database_name, bs.database_guid, bs.type AS backup_type ' + @crlf + + ' , MBpsAvg = CAST(AVG(( bs.backup_size / ( CASE WHEN DATEDIFF(ss, bs.backup_start_date, bs.backup_finish_date) = 0 THEN 1 ELSE DATEDIFF(ss, bs.backup_start_date, bs.backup_finish_date) END ) / 1048576 )) AS INT) ' + @crlf + + ' , MBpsMin = CAST(MIN(( bs.backup_size / ( CASE WHEN DATEDIFF(ss, bs.backup_start_date, bs.backup_finish_date) = 0 THEN 1 ELSE DATEDIFF(ss, bs.backup_start_date, bs.backup_finish_date) END ) / 1048576 )) AS INT) ' + @crlf + + ' , MBpsMax = CAST(MAX(( bs.backup_size / ( CASE WHEN DATEDIFF(ss, bs.backup_start_date, bs.backup_finish_date) = 0 THEN 1 ELSE DATEDIFF(ss, bs.backup_start_date, bs.backup_finish_date) END ) / 1048576 )) AS INT) ' + @crlf + + ' , SizeMBAvg = AVG(backup_size / 1048576.0) ' + @crlf + + ' , SizeMBMin = MIN(backup_size / 1048576.0) ' + @crlf + + ' , SizeMBMax = MAX(backup_size / 1048576.0) ' + @crlf + + ' , CompressedSizeMBAvg = AVG(compressed_backup_size / 1048576.0) ' + @crlf + + ' , CompressedSizeMBMin = MIN(compressed_backup_size / 1048576.0) ' + @crlf + + ' , CompressedSizeMBMax = MAX(compressed_backup_size / 1048576.0) ' + @crlf; + + + SET @StringToExecute += N' FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bs ' + @crlf + + N' WHERE bs.backup_finish_date >= @StartTime AND bs.is_damaged = 0 ' + @crlf + + N' GROUP BY bs.database_name, bs.database_guid, bs.type)' + @crlf; + + SET @StringToExecute += + N'INSERT INTO #Backups(database_name, database_guid, ' + @crlf + + N' FullMBpsAvg, FullMBpsMin, FullMBpsMax, FullSizeMBAvg, FullSizeMBMin, FullSizeMBMax, FullCompressedSizeMBAvg, FullCompressedSizeMBMin, FullCompressedSizeMBMax, ' + @crlf + + N' DiffMBpsAvg, DiffMBpsMin, DiffMBpsMax, DiffSizeMBAvg, DiffSizeMBMin, DiffSizeMBMax, DiffCompressedSizeMBAvg, DiffCompressedSizeMBMin, DiffCompressedSizeMBMax, ' + @crlf + + N' LogMBpsAvg, LogMBpsMin, LogMBpsMax, LogSizeMBAvg, LogSizeMBMin, LogSizeMBMax, LogCompressedSizeMBAvg, LogCompressedSizeMBMin, LogCompressedSizeMBMax ) ' + @crlf + + N'SELECT bF.database_name, bF.database_guid ' + @crlf + + N' , bF.MBpsAvg AS FullMBpsAvg ' + @crlf + + N' , bF.MBpsMin AS FullMBpsMin ' + @crlf + + N' , bF.MBpsMax AS FullMBpsMax ' + @crlf + + N' , bF.SizeMBAvg AS FullSizeMBAvg ' + @crlf + + N' , bF.SizeMBMin AS FullSizeMBMin ' + @crlf + + N' , bF.SizeMBMax AS FullSizeMBMax ' + @crlf + + N' , bF.CompressedSizeMBAvg AS FullCompressedSizeMBAvg ' + @crlf + + N' , bF.CompressedSizeMBMin AS FullCompressedSizeMBMin ' + @crlf + + N' , bF.CompressedSizeMBMax AS FullCompressedSizeMBMax ' + @crlf + + N' , bD.MBpsAvg AS DiffMBpsAvg ' + @crlf + + N' , bD.MBpsMin AS DiffMBpsMin ' + @crlf + + N' , bD.MBpsMax AS DiffMBpsMax ' + @crlf + + N' , bD.SizeMBAvg AS DiffSizeMBAvg ' + @crlf + + N' , bD.SizeMBMin AS DiffSizeMBMin ' + @crlf + + N' , bD.SizeMBMax AS DiffSizeMBMax ' + @crlf + + N' , bD.CompressedSizeMBAvg AS DiffCompressedSizeMBAvg ' + @crlf + + N' , bD.CompressedSizeMBMin AS DiffCompressedSizeMBMin ' + @crlf + + N' , bD.CompressedSizeMBMax AS DiffCompressedSizeMBMax ' + @crlf + + N' , bL.MBpsAvg AS LogMBpsAvg ' + @crlf + + N' , bL.MBpsMin AS LogMBpsMin ' + @crlf + + N' , bL.MBpsMax AS LogMBpsMax ' + @crlf + + N' , bL.SizeMBAvg AS LogSizeMBAvg ' + @crlf + + N' , bL.SizeMBMin AS LogSizeMBMin ' + @crlf + + N' , bL.SizeMBMax AS LogSizeMBMax ' + @crlf + + N' , bL.CompressedSizeMBAvg AS LogCompressedSizeMBAvg ' + @crlf + + N' , bL.CompressedSizeMBMin AS LogCompressedSizeMBMin ' + @crlf + + N' , bL.CompressedSizeMBMax AS LogCompressedSizeMBMax ' + @crlf + + N' FROM Backups bF ' + @crlf + + N' LEFT OUTER JOIN Backups bD ON bF.database_name = bD.database_name AND bF.database_guid = bD.database_guid AND bD.backup_type = ''I''' + @crlf + + N' LEFT OUTER JOIN Backups bL ON bF.database_name = bL.database_name AND bF.database_guid = bL.database_guid AND bL.backup_type = ''L''' + @crlf + + N' WHERE bF.backup_type = ''D''; ' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + + + RAISERROR('Updating #Backups with worst RPO case', 0, 1) WITH NOWAIT; + + SET @StringToExecute =N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + SELECT bs.database_name, bs.database_guid, bs.backup_set_id, bsPrior.backup_set_id AS backup_set_id_prior, + bs.backup_finish_date, bsPrior.backup_finish_date AS backup_finish_date_prior, + DATEDIFF(ss, bsPrior.backup_finish_date, bs.backup_finish_date) AS backup_gap_seconds + INTO #backup_gaps + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS bs + CROSS APPLY ( + SELECT TOP 1 bs1.backup_set_id, bs1.backup_finish_date + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS bs1 + WHERE bs.database_name = bs1.database_name + AND bs.database_guid = bs1.database_guid + AND bs.backup_finish_date > bs1.backup_finish_date + AND bs.backup_set_id > bs1.backup_set_id + ORDER BY bs1.backup_finish_date DESC, bs1.backup_set_id DESC + ) bsPrior + WHERE bs.backup_finish_date > @StartTime + + CREATE CLUSTERED INDEX cx_backup_gaps ON #backup_gaps (database_name, database_guid, backup_set_id, backup_finish_date, backup_gap_seconds); + + WITH max_gaps AS ( + SELECT g.database_name, g.database_guid, g.backup_set_id, g.backup_set_id_prior, g.backup_finish_date_prior, + g.backup_finish_date, MAX(g.backup_gap_seconds) AS max_backup_gap_seconds + FROM #backup_gaps AS g + GROUP BY g.database_name, g.database_guid, g.backup_set_id, g.backup_set_id_prior, g.backup_finish_date_prior, g.backup_finish_date + ) + UPDATE #Backups + SET RPOWorstCaseMinutes = bg.max_backup_gap_seconds / 60.0 + , RPOWorstCaseBackupSetID = bg.backup_set_id + , RPOWorstCaseBackupSetFinishTime = bg.backup_finish_date + , RPOWorstCaseBackupSetIDPrior = bg.backup_set_id_prior + , RPOWorstCaseBackupSetPriorFinishTime = bg.backup_finish_date_prior + FROM #Backups b + INNER HASH JOIN max_gaps bg ON b.database_name = bg.database_name AND b.database_guid = bg.database_guid + LEFT OUTER HASH JOIN max_gaps bgBigger ON bg.database_name = bgBigger.database_name AND bg.database_guid = bgBigger.database_guid AND bg.max_backup_gap_seconds < bgBigger.max_backup_gap_seconds + WHERE bgBigger.backup_set_id IS NULL; + '; + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + + RAISERROR('Updating #Backups with worst RPO case queries', 0, 1) WITH NOWAIT; + + UPDATE #Backups + SET RPOWorstCaseMoreInfoQuery = @MoreInfoHeader + N'SELECT * ' + @crlf + + N' FROM ' + QUOTENAME(@MSDBName) + '.dbo.backupset ' + @crlf + + N' WHERE database_name = ''' + database_name + ''' ' + @crlf + + N' AND database_guid = ''' + CAST(database_guid AS NVARCHAR(50)) + ''' ' + @crlf + + N' AND backup_finish_date >= DATEADD(hh, -2, ''' + CAST(CONVERT(DATETIME, RPOWorstCaseBackupSetPriorFinishTime, 102) AS NVARCHAR(100)) + ''') ' + @crlf + + N' AND backup_finish_date <= DATEADD(hh, 2, ''' + CAST(CONVERT(DATETIME, RPOWorstCaseBackupSetPriorFinishTime, 102) AS NVARCHAR(100)) + ''') ' + @crlf + + N' ORDER BY backup_finish_date;' + + @MoreInfoFooter; + + +/* RTO */ + +RAISERROR('Gathering RTO information', 0, 1) WITH NOWAIT; + + + SET @StringToExecute =N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + INSERT INTO #RTORecoveryPoints(database_name, database_guid, log_last_lsn) + SELECT database_name, database_guid, MAX(last_lsn) AS log_last_lsn + FROM ' + QUOTENAME(@MSDBName) + '.dbo.backupset bLastLog + WHERE type = ''L'' + AND bLastLog.backup_finish_date >= @StartTime + GROUP BY database_name, database_guid; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + +/* Find the most recent full backups for those logs */ + +RAISERROR('Updating #RTORecoveryPoints', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE #RTORecoveryPoints + SET log_backup_set_id = bLasted.backup_set_id + ,full_backup_set_id = bLasted.backup_set_id + ,full_last_lsn = bLasted.last_lsn + ,full_backup_set_uuid = bLasted.backup_set_uuid + FROM #RTORecoveryPoints rp + CROSS APPLY ( + SELECT TOP 1 bLog.backup_set_id AS backup_set_id_log, bLastFull.backup_set_id, bLastFull.last_lsn, bLastFull.backup_set_uuid, bLastFull.database_guid, bLastFull.database_name + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bLog + INNER JOIN ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bLastFull + ON bLog.database_guid = bLastFull.database_guid + AND bLog.database_name = bLastFull.database_name + AND bLog.first_lsn > bLastFull.last_lsn + AND bLastFull.type = ''D'' + WHERE rp.database_guid = bLog.database_guid + AND rp.database_name = bLog.database_name + ) bLasted + LEFT OUTER JOIN ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bLaterFulls ON bLasted.database_guid = bLaterFulls.database_guid AND bLasted.database_name = bLaterFulls.database_name + AND bLasted.last_lsn < bLaterFulls.last_lsn + AND bLaterFulls.first_lsn < bLasted.last_lsn + AND bLaterFulls.type = ''D'' + WHERE bLaterFulls.backup_set_id IS NULL; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute; + +/* Add any full backups in the StartDate range that weren't part of the above log backup chain */ + +RAISERROR('Add any full backups in the StartDate range that weren''t part of the above log backup chain', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + INSERT INTO #RTORecoveryPoints(database_name, database_guid, full_backup_set_id, full_last_lsn, full_backup_set_uuid) + SELECT bFull.database_name, bFull.database_guid, bFull.backup_set_id, bFull.last_lsn, bFull.backup_set_uuid + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bFull + LEFT OUTER JOIN #RTORecoveryPoints rp ON bFull.backup_set_uuid = rp.full_backup_set_uuid + WHERE bFull.type = ''D'' + AND bFull.backup_finish_date IS NOT NULL + AND rp.full_backup_set_uuid IS NULL + AND bFull.backup_finish_date >= @StartTime; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + +/* Fill out the most recent log for that full, but before the next full */ + +RAISERROR('Fill out the most recent log for that full, but before the next full', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE rp + SET log_last_lsn = (SELECT MAX(last_lsn) FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bLog WHERE bLog.first_lsn >= rp.full_last_lsn AND bLog.first_lsn <= rpNextFull.full_last_lsn AND bLog.type = ''L'') + FROM #RTORecoveryPoints rp + INNER JOIN #RTORecoveryPoints rpNextFull ON rp.database_guid = rpNextFull.database_guid AND rp.database_name = rpNextFull.database_name + AND rp.full_last_lsn < rpNextFull.full_last_lsn + LEFT OUTER JOIN #RTORecoveryPoints rpEarlierFull ON rp.database_guid = rpEarlierFull.database_guid AND rp.database_name = rpEarlierFull.database_name + AND rp.full_last_lsn < rpEarlierFull.full_last_lsn + AND rpNextFull.full_last_lsn > rpEarlierFull.full_last_lsn + WHERE rpEarlierFull.full_backup_set_id IS NULL; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute; + +/* Fill out a diff in that range */ + +RAISERROR('Fill out a diff in that range', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE #RTORecoveryPoints + SET diff_last_lsn = (SELECT TOP 1 bDiff.last_lsn FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bDiff + WHERE rp.database_guid = bDiff.database_guid AND rp.database_name = bDiff.database_name + AND bDiff.type = ''I'' + AND bDiff.last_lsn < rp.log_last_lsn + AND rp.full_backup_set_uuid = bDiff.differential_base_guid + ORDER BY bDiff.last_lsn DESC) + FROM #RTORecoveryPoints rp + WHERE diff_last_lsn IS NULL; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute; + +/* Get time & size totals for full & diff */ + +RAISERROR('Get time & size totals for full & diff', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE #RTORecoveryPoints + SET full_time_seconds = DATEDIFF(ss,bFull.backup_start_date, bFull.backup_finish_date) + , full_file_size_mb = bFull.backup_size / 1048576.0 + , diff_backup_set_id = bDiff.backup_set_id + , diff_time_seconds = DATEDIFF(ss,bDiff.backup_start_date, bDiff.backup_finish_date) + , diff_file_size_mb = bDiff.backup_size / 1048576.0 + FROM #RTORecoveryPoints rp + INNER JOIN ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bFull ON rp.database_guid = bFull.database_guid AND rp.database_name = bFull.database_name AND rp.full_last_lsn = bFull.last_lsn + LEFT OUTER JOIN ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bDiff ON rp.database_guid = bDiff.database_guid AND rp.database_name = bDiff.database_name AND rp.diff_last_lsn = bDiff.last_lsn AND bDiff.last_lsn IS NOT NULL; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute; + + +/* Get time & size totals for logs */ + +RAISERROR('Get time & size totals for logs', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + WITH LogTotals AS ( + SELECT rp.id, log_time_seconds = SUM(DATEDIFF(ss,bLog.backup_start_date, bLog.backup_finish_date)) + , log_file_size = SUM(bLog.backup_size) + , SUM(1) AS log_backups + FROM #RTORecoveryPoints rp + INNER JOIN ' + QUOTENAME(@MSDBName) + N'.dbo.backupset bLog ON rp.database_guid = bLog.database_guid AND rp.database_name = bLog.database_name AND bLog.type = ''L'' + AND bLog.first_lsn > COALESCE(rp.diff_last_lsn, rp.full_last_lsn) + AND bLog.first_lsn <= rp.log_last_lsn + GROUP BY rp.id + ) + UPDATE #RTORecoveryPoints + SET log_time_seconds = lt.log_time_seconds + , log_file_size_mb = lt.log_file_size / 1048576.0 + , log_backups = lt.log_backups + FROM #RTORecoveryPoints rp + INNER JOIN LogTotals lt ON rp.id = lt.id; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute; + +RAISERROR('Gathering RTO worst cases', 0, 1) WITH NOWAIT; + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + WITH WorstCases AS ( + SELECT rp.* + FROM #RTORecoveryPoints rp + LEFT OUTER JOIN #RTORecoveryPoints rpNewer + ON rp.database_guid = rpNewer.database_guid + AND rp.database_name = rpNewer.database_name + AND rp.full_last_lsn < rpNewer.full_last_lsn + AND rpNewer.rto_worst_case_size_mb = (SELECT TOP 1 rto_worst_case_size_mb FROM #RTORecoveryPoints s WHERE rp.database_guid = s.database_guid AND rp.database_name = s.database_name ORDER BY rto_worst_case_size_mb DESC) + WHERE rp.rto_worst_case_size_mb = (SELECT TOP 1 rto_worst_case_size_mb FROM #RTORecoveryPoints s WHERE rp.database_guid = s.database_guid AND rp.database_name = s.database_name ORDER BY rto_worst_case_size_mb DESC) + /* OR rp.rto_worst_case_time_seconds = (SELECT TOP 1 rto_worst_case_time_seconds FROM #RTORecoveryPoints s WHERE rp.database_guid = s.database_guid AND rp.database_name = s.database_name ORDER BY rto_worst_case_time_seconds DESC) */ + AND rpNewer.database_guid IS NULL + ) + UPDATE #Backups + SET RTOWorstCaseMinutes = + /* Fulls */ + (CASE WHEN @RestoreSpeedFullMBps IS NULL + THEN wc.full_time_seconds / 60.0 + ELSE @RestoreSpeedFullMBps / wc.full_file_size_mb + END) + + /* Diffs, which might not have been taken */ + + (CASE WHEN @RestoreSpeedDiffMBps IS NOT NULL AND wc.diff_file_size_mb IS NOT NULL + THEN @RestoreSpeedDiffMBps / wc.diff_file_size_mb + ELSE COALESCE(wc.diff_time_seconds,0) / 60.0 + END) + + /* Logs, which might not have been taken */ + + (CASE WHEN @RestoreSpeedLogMBps IS NOT NULL AND wc.log_file_size_mb IS NOT NULL + THEN @RestoreSpeedLogMBps / wc.log_file_size_mb + ELSE COALESCE(wc.log_time_seconds,0) / 60.0 + END) + , RTOWorstCaseBackupFileSizeMB = wc.rto_worst_case_size_mb + FROM #Backups b + INNER JOIN WorstCases wc + ON b.database_guid = wc.database_guid + AND b.database_name = wc.database_name; + '; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@RestoreSpeedFullMBps INT, @RestoreSpeedDiffMBps INT, @RestoreSpeedLogMBps INT', @RestoreSpeedFullMBps, @RestoreSpeedDiffMBps, @RestoreSpeedLogMBps; + + + +/*Populating Recoverability*/ + + + /*Get distinct list of databases*/ + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + SELECT DISTINCT b.database_name, database_guid + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b;' + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Recoverability ( DatabaseName, DatabaseGUID ) + EXEC sys.sp_executesql @StringToExecute; + + + /*Find most recent recovery model, backup size, and backup date*/ + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE r + SET r.LastBackupRecoveryModel = ca.recovery_model, + r.LastFullBackupSizeMB = ca.compressed_backup_size, + r.LastFullBackupDate = ca.backup_finish_date + FROM #Recoverability r + CROSS APPLY ( + SELECT TOP 1 b.recovery_model, (b.compressed_backup_size / 1048576.0) AS compressed_backup_size, b.backup_finish_date + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''D'' + AND b.backup_finish_date > @StartTime + ORDER BY b.backup_finish_date DESC + ) ca;' + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + + /*Find first backup size and date*/ + SET @StringToExecute =N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE r + SET r.FirstFullBackupSizeMB = ca.compressed_backup_size, + r.FirstFullBackupDate = ca.backup_finish_date + FROM #Recoverability r + CROSS APPLY ( + SELECT TOP 1 (b.compressed_backup_size / 1048576.0) AS compressed_backup_size, b.backup_finish_date + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''D'' + AND b.backup_finish_date > @StartTime + ORDER BY b.backup_finish_date ASC + ) ca;' + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + + + /*Find average backup throughputs for full, diff, and log*/ + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE r + SET r.AvgFullBackupThroughputMB = ca_full.AvgFullSpeed, + r.AvgDiffBackupThroughputMB = ca_diff.AvgDiffSpeed, + r.AvgLogBackupThroughputMB = ca_log.AvgLogSpeed, + r.AvgFullBackupDurationSeconds = AvgFullDuration, + r.AvgDiffBackupDurationSeconds = AvgDiffDuration, + r.AvgLogBackupDurationSeconds = AvgLogDuration + FROM #Recoverability AS r + OUTER APPLY ( + SELECT b.database_name, + AVG( b.compressed_backup_size / ( DATEDIFF(ss, b.backup_start_date, b.backup_finish_date) ) / 1048576.0 ) AS AvgFullSpeed, + AVG( DATEDIFF(ss, b.backup_start_date, b.backup_finish_date) ) AS AvgFullDuration + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''D'' + AND DATEDIFF(SECOND, b.backup_start_date, b.backup_finish_date) > 0 + AND b.backup_finish_date > @StartTime + GROUP BY b.database_name + ) ca_full + OUTER APPLY ( + SELECT b.database_name, + AVG( b.compressed_backup_size / ( DATEDIFF(ss, b.backup_start_date, b.backup_finish_date) ) / 1048576.0 ) AS AvgDiffSpeed, + AVG( DATEDIFF(ss, b.backup_start_date, b.backup_finish_date) ) AS AvgDiffDuration + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''I'' + AND DATEDIFF(SECOND, b.backup_start_date, b.backup_finish_date) > 0 + AND b.backup_finish_date > @StartTime + GROUP BY b.database_name + ) ca_diff + OUTER APPLY ( + SELECT b.database_name, + AVG( b.compressed_backup_size / ( DATEDIFF(ss, b.backup_start_date, b.backup_finish_date) ) / 1048576.0 ) AS AvgLogSpeed, + AVG( DATEDIFF(ss, b.backup_start_date, b.backup_finish_date) ) AS AvgLogDuration + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''L'' + AND DATEDIFF(SECOND, b.backup_start_date, b.backup_finish_date) > 0 + AND b.backup_finish_date > @StartTime + GROUP BY b.database_name + ) ca_log;' + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + + + /*Find max and avg diff and log sizes*/ + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + UPDATE r + SET r.AvgFullSizeMB = fulls.avg_full_size, + r.AvgDiffSizeMB = diffs.avg_diff_size, + r.AvgLogSizeMB = logs.avg_log_size + FROM #Recoverability AS r + OUTER APPLY ( + SELECT b.database_name, AVG(b.compressed_backup_size / 1048576.0) AS avg_full_size + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''D'' + AND b.backup_finish_date > @StartTime + GROUP BY b.database_name + ) AS fulls + OUTER APPLY ( + SELECT b.database_name, AVG(b.compressed_backup_size / 1048576.0) AS avg_diff_size + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''I'' + AND b.backup_finish_date > @StartTime + GROUP BY b.database_name + ) AS diffs + OUTER APPLY ( + SELECT b.database_name, AVG(b.compressed_backup_size / 1048576.0) AS avg_log_size + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE r.DatabaseName = b.database_name + AND r.DatabaseGUID = b.database_guid + AND b.type = ''L'' + AND b.backup_finish_date > @StartTime + GROUP BY b.database_name + ) AS logs;' + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + +/*Trending - only works if backupfile is populated, which means in msdb */ +IF @MSDBName = N'msdb' +BEGIN + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' --+ @crlf; + + SET @StringToExecute += N' + SELECT p.DatabaseName, + p.DatabaseGUID, + p.[0], + p.[-1], + p.[-2], + p.[-3], + p.[-4], + p.[-5], + p.[-6], + p.[-7], + p.[-8], + p.[-9], + p.[-10], + p.[-11], + p.[-12] + FROM ( SELECT b.database_name AS DatabaseName, + b.database_guid AS DatabaseGUID, + DATEDIFF(MONTH, @StartTime, b.backup_start_date) AS MonthsAgo , + CONVERT(DECIMAL(18, 2), AVG(bf.file_size / 1048576.0)) AS AvgSizeMB + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + INNER JOIN ' + QUOTENAME(@MSDBName) + N'.dbo.backupfile AS bf + ON b.backup_set_id = bf.backup_set_id + WHERE b.database_name NOT IN ( ''master'', ''msdb'', ''model'', ''tempdb'' ) + AND bf.file_type = ''D'' + AND b.backup_start_date >= DATEADD(YEAR, -1, @StartTime) + AND b.backup_start_date <= SYSDATETIME() + GROUP BY b.database_name, + b.database_guid, + DATEDIFF(mm, @StartTime, b.backup_start_date) + ) AS bckstat PIVOT ( SUM(bckstat.AvgSizeMB) FOR bckstat.MonthsAgo IN ( [0], [-1], [-2], [-3], [-4], [-5], [-6], [-7], [-8], [-9], [-10], [-11], [-12] ) ) AS p + ORDER BY p.DatabaseName; + ' + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Trending ( DatabaseName, DatabaseGUID, [0], [-1], [-2], [-3], [-4], [-5], [-6], [-7], [-8], [-9], [-10], [-11], [-12] ) + EXEC sys.sp_executesql @StringToExecute, N'@StartTime DATETIME2', @StartTime; + +END + +/*End Trending*/ + +/*End populating Recoverability*/ + +RAISERROR('Returning data', 0, 1) WITH NOWAIT; + + SELECT b.* + FROM #Backups AS b + ORDER BY b.database_name; + + SELECT r.*, + t.[0], t.[-1], t.[-2], t.[-3], t.[-4], t.[-5], t.[-6], t.[-7], t.[-8], t.[-9], t.[-10], t.[-11], t.[-12] + FROM #Recoverability AS r + LEFT JOIN #Trending t + ON r.DatabaseName = t.DatabaseName + AND r.DatabaseGUID = t.DatabaseGUID + WHERE r.LastBackupRecoveryModel IS NOT NULL + ORDER BY r.DatabaseName + + +RAISERROR('Rules analysis starting', 0, 1) WITH NOWAIT; + +/*Looking for out of band backups by finding most common backup operator user_name and noting backups taken by other user_names*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + WITH common_people AS ( + SELECT TOP 1 b.user_name, COUNT_BIG(*) AS Records + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + GROUP BY b.user_name + ORDER BY Records DESC + ) + SELECT + 1 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Non-Agent backups taken'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has been backed up by '' + QUOTENAME(b.user_name) + '' '' + CONVERT(VARCHAR(10), COUNT(*)) + '' times.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.user_name NOT LIKE ''%Agent%'' AND b.user_name NOT LIKE ''%AGENT%'' + AND NOT EXISTS ( + SELECT 1 + FROM common_people AS cp + WHERE cp.user_name = b.user_name + ) + GROUP BY b.database_name, b.user_name + HAVING COUNT(*) > 1;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings (CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Looking for compatibility level changing. Only looking for databases that have changed more than twice (It''s possible someone may have changed up, had CE problems, and then changed back)*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 2 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Compatibility level changing'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has changed compatibility levels '' + CONVERT(VARCHAR(10), COUNT(DISTINCT b.compatibility_level)) + '' times.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + GROUP BY b.database_name + HAVING COUNT(DISTINCT b.compatibility_level) > 2;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Looking for password protected backups. This hasn''t been a popular option ever, and was largely replaced by encrypted backups, but it''s simple to check for.*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 3 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Password backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has been backed up with a password '' + CONVERT(VARCHAR(10), COUNT(*)) + '' times. Who has the password?'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.is_password_protected = 1 + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Looking for snapshot backups. There are legit reasons for these, but we should flag them so the questions get asked. What questions? Good question.*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 4 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Snapshot backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has had '' + CONVERT(VARCHAR(10), COUNT(*)) + '' snapshot backups. This message is purely informational.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.is_snapshot = 1 + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*It''s fine to take backups of read only databases, but it''s not always necessary (there''s no new data, after all).*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 5 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Read only state backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has been backed up '' + CONVERT(VARCHAR(10), COUNT(*)) + '' times while in a read-only state. This can be normal if it''''s a secondary, but a bit odd otherwise.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.is_readonly = 1 + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*So, I''ve come across people who think they need to change their database to single user mode to take a backup. Or that doing that will help something. I just need to know, here.*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 6 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Single user mode backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has been backed up '' + CONVERT(VARCHAR(10), COUNT(*)) + '' times while in single-user mode. This is really weird! Make sure your backup process doesn''''t include a mode change anywhere.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.is_single_user = 1 + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*C''mon, it''s 2017. Take your backups with CHECKSUMS, people.*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 7 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''No CHECKSUMS'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has been backed up '' + CONVERT(VARCHAR(10), COUNT(*)) + '' times without CHECKSUMS in the past 30 days. CHECKSUMS can help alert you to corruption errors.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.has_backup_checksums = 0 + AND b.backup_finish_date >= DATEADD(DAY, -30, SYSDATETIME()) + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Damaged is a Black Flag album. You don''t want your backups to be like a Black Flag album. */ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 8 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Damaged backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has had '' + CONVERT(VARCHAR(10), COUNT(*)) + '' damaged backups taken without stopping to throw an error. This is done by specifying CONTINUE_AFTER_ERROR in your BACKUP commands.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.is_damaged = 1 + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Checking for encrypted backups and the last backup of the encryption key.*/ + + /*2014 ONLY*/ + +IF @ProductVersionMajor >= 12 + BEGIN + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 9 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Encrypted backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has had '' + CONVERT(VARCHAR(10), COUNT(*)) + '' '' + b.encryptor_type + '' backups, and the last time a certificate was backed up is ' + + CASE WHEN LOWER(@MSDBName) <> N'msdb' + THEN + N'...well, that information is on another server, anyway.'' AS [Warning]' + ELSE + CONVERT(VARCHAR(30), (SELECT MAX(c.pvt_key_last_backup_date) FROM sys.certificates AS c WHERE c.name NOT LIKE '##%')) + N'.'' AS [Warning]' + END + + N' + FROM ' + QUOTENAME(@MSDBName) + N'.dbo.backupset AS b + WHERE b.encryptor_type IS NOT NULL + GROUP BY b.database_name, b.encryptor_type;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + END + + /*Looking for backups that have BULK LOGGED data in them -- this can screw up point in time LOG recovery.*/ + + SET @StringToExecute =N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 10 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Bulk logged backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has had '' + CONVERT(VARCHAR(10), COUNT(*)) + '' backups with bulk logged data. This can make point in time recovery awkward. '' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + '.dbo.backupset AS b + WHERE b.has_bulk_logged_data = 1 + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Looking for recovery model being switched between FULL and SIMPLE, because it''s a bad practice.*/ + + SET @StringToExecute =N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 11 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Recovery model switched'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has changed recovery models from between FULL and SIMPLE '' + CONVERT(VARCHAR(10), COUNT(DISTINCT b.recovery_model)) + '' times. This breaks the log chain and is generally a bad idea.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + '.dbo.backupset AS b + WHERE b.recovery_model <> ''BULK-LOGGED'' + GROUP BY b.database_name + HAVING COUNT(DISTINCT b.recovery_model) > 4;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + + /*Looking for uncompressed backups.*/ + + SET @StringToExecute =N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT + 12 AS CheckId, + 100 AS [Priority], + b.database_name AS [Database Name], + ''Uncompressed backups'' AS [Finding], + ''The database '' + QUOTENAME(b.database_name) + '' has had '' + CONVERT(VARCHAR(10), COUNT(*)) + '' uncompressed backups in the last 30 days. This is a free way to save time and space. And SPACETIME. If your version of SQL supports it.'' AS [Warning] + FROM ' + QUOTENAME(@MSDBName) + '.dbo.backupset AS b + WHERE backup_size = compressed_backup_size AND type = ''D'' + AND b.backup_finish_date >= DATEADD(DAY, -30, SYSDATETIME()) + GROUP BY b.database_name;' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + EXEC sys.sp_executesql @StringToExecute; + +RAISERROR('Rules analysis starting on temp tables', 0, 1) WITH NOWAIT; + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + SELECT + 13 AS CheckId, + 100 AS Priority, + r.DatabaseName as [DatabaseName], + 'Big Diffs' AS [Finding], + 'On average, Differential backups for this database are >=40% of the size of the average Full backup.' AS [Warning] + FROM #Recoverability AS r + WHERE r.IsBigDiff = 1 + + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + SELECT + 13 AS CheckId, + 100 AS Priority, + r.DatabaseName as [DatabaseName], + 'Big Logs' AS [Finding], + 'On average, Log backups for this database are >=20% of the size of the average Full backup.' AS [Warning] + FROM #Recoverability AS r + WHERE r.IsBigLog = 1 + + + +/*Insert thank you stuff last*/ + INSERT #Warnings ( CheckId, Priority, DatabaseName, Finding, Warning ) + + SELECT + 2147483647 AS [CheckId], + 2147483647 AS [Priority], + 'From Your Community Volunteers' AS [DatabaseName], + 'sp_BlitzBackups Version: ' + @Version + ', Version Date: ' + CONVERT(VARCHAR(30), @VersionDate) + '.' AS [Finding], + 'Thanks for using our stored procedure. We hope you find it useful! Check out our other free SQL Server scripts at firstresponderkit.org!' AS [Warning]; + +RAISERROR('Rules analysis finished', 0, 1) WITH NOWAIT; + +SELECT w.CheckId, w.Priority, w.DatabaseName, w.Finding, w.Warning +FROM #Warnings AS w +ORDER BY w.Priority, w.CheckId; + +DROP TABLE #Backups, #Warnings, #Recoverability, #RTORecoveryPoints + + +RETURN; + +PushBackupHistoryToListener: + +RAISERROR('Pushing backup history to listener', 0, 1) WITH NOWAIT; + +DECLARE @msg NVARCHAR(4000) = N''; +DECLARE @RemoteCheck TABLE (c INT NULL); + + +IF @WriteBackupsToDatabaseName IS NULL + BEGIN + RAISERROR('@WriteBackupsToDatabaseName can''t be NULL.', 16, 1) WITH NOWAIT + RETURN; + END + +IF LOWER(@WriteBackupsToDatabaseName) = N'msdb' + BEGIN + RAISERROR('We can''t write to the real msdb, we have to write to a fake msdb.', 16, 1) WITH NOWAIT + RETURN; + END + +IF @WriteBackupsToListenerName IS NULL +BEGIN + IF @AGName IS NULL + BEGIN + RAISERROR('@WriteBackupsToListenerName and @AGName can''t both be NULL.', 16, 1) WITH NOWAIT; + RETURN; + END + ELSE + BEGIN + SELECT @WriteBackupsToListenerName = dns_name + FROM sys.availability_groups AS ag + JOIN sys.availability_group_listeners AS agl + ON ag.group_id = agl.group_id + WHERE name = @AGName; + END + +END + +IF @WriteBackupsToListenerName IS NOT NULL +BEGIN + IF NOT EXISTS + ( + SELECT * + FROM sys.servers s + WHERE name = @WriteBackupsToListenerName + ) + BEGIN + SET @msg = N'We need a linked server to write data across. Please set one up for ' + @WriteBackupsToListenerName + N'.'; + RAISERROR(@msg, 16, 1) WITH NOWAIT; + RETURN; + END +END + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT TOP 1 1 FROM ' + + QUOTENAME(@WriteBackupsToListenerName) + N'.master.sys.databases d WHERE d.name = @i_WriteBackupsToDatabaseName;' + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT @RemoteCheck (c) + EXEC sp_executesql @StringToExecute, N'@i_WriteBackupsToDatabaseName NVARCHAR(256)', @i_WriteBackupsToDatabaseName = @WriteBackupsToDatabaseName; + + IF @@ROWCOUNT = 0 + BEGIN + SET @msg = N'The database ' + @WriteBackupsToDatabaseName + N' doesn''t appear to exist on that server.' + RAISERROR(@msg, 16, 1) WITH NOWAIT + RETURN; + END + + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'SELECT TOP 1 1 FROM ' + + QUOTENAME(@WriteBackupsToListenerName) + '.' + QUOTENAME(@WriteBackupsToDatabaseName) + '.sys.tables WHERE name = ''backupset'' AND SCHEMA_NAME(schema_id) = ''dbo''; + ' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + INSERT @RemoteCheck (c) + EXEC sp_executesql @StringToExecute; + + IF @@ROWCOUNT = 0 + BEGIN + + SET @msg = N'The database ' + @WriteBackupsToDatabaseName + N' doesn''t appear to have a table called dbo.backupset in it.' + RAISERROR(@msg, 0, 1) WITH NOWAIT + RAISERROR('Don''t worry, we''ll create it for you!', 0, 1) WITH NOWAIT + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'CREATE TABLE ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.dbo.backupset + ( backup_set_id INT IDENTITY(1, 1), backup_set_uuid UNIQUEIDENTIFIER, media_set_id INT, first_family_number TINYINT, first_media_number SMALLINT, + last_family_number TINYINT, last_media_number SMALLINT, catalog_family_number TINYINT, catalog_media_number SMALLINT, position INT, expiration_date DATETIME, + software_vendor_id INT, name NVARCHAR(128), description NVARCHAR(255), user_name NVARCHAR(128), software_major_version TINYINT, software_minor_version TINYINT, + software_build_version SMALLINT, time_zone SMALLINT, mtf_minor_version TINYINT, first_lsn NUMERIC(25, 0), last_lsn NUMERIC(25, 0), checkpoint_lsn NUMERIC(25, 0), + database_backup_lsn NUMERIC(25, 0), database_creation_date DATETIME, backup_start_date DATETIME, backup_finish_date DATETIME, type CHAR(1), sort_order SMALLINT, + code_page SMALLINT, compatibility_level TINYINT, database_version INT, backup_size NUMERIC(20, 0), database_name NVARCHAR(128), server_name NVARCHAR(128), + machine_name NVARCHAR(128), flags INT, unicode_locale INT, unicode_compare_style INT, collation_name NVARCHAR(128), is_password_protected BIT, recovery_model NVARCHAR(60), + has_bulk_logged_data BIT, is_snapshot BIT, is_readonly BIT, is_single_user BIT, has_backup_checksums BIT, is_damaged BIT, begins_log_chain BIT, has_incomplete_metadata BIT, + is_force_offline BIT, is_copy_only BIT, first_recovery_fork_guid UNIQUEIDENTIFIER, last_recovery_fork_guid UNIQUEIDENTIFIER, fork_point_lsn NUMERIC(25, 0), database_guid UNIQUEIDENTIFIER, + family_guid UNIQUEIDENTIFIER, differential_base_lsn NUMERIC(25, 0), differential_base_guid UNIQUEIDENTIFIER, compressed_backup_size NUMERIC(20, 0), key_algorithm NVARCHAR(32), + encryptor_thumbprint VARBINARY(20) , encryptor_type NVARCHAR(32) + ); + ' + @crlf; + + SET @InnerStringToExecute = N'EXEC( ''' + @StringToExecute + ''' ) AT ' + QUOTENAME(@WriteBackupsToListenerName) + N';' + + IF @Debug = 1 + PRINT @InnerStringToExecute; + + EXEC sp_executesql @InnerStringToExecute + + + RAISERROR('We''ll even make the indexes!', 0, 1) WITH NOWAIT + + /*Checking for and creating the PK/CX*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + + IF NOT EXISTS ( + SELECT t.name, i.name + FROM ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.tables AS t + JOIN ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.indexes AS i + ON t.object_id = i.object_id + WHERE t.name = ? + AND i.name LIKE ? + ) + + BEGIN + ALTER TABLE ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.[dbo].[backupset] ADD PRIMARY KEY CLUSTERED ([backup_set_id] ASC) + END + ' + + SET @InnerStringToExecute = N'EXEC( ''' + @StringToExecute + ''', ''backupset'', ''PK[_][_]%'' ) AT ' + QUOTENAME(@WriteBackupsToListenerName) + N';' + + IF @Debug = 1 + PRINT @InnerStringToExecute; + + EXEC sp_executesql @InnerStringToExecute + + + + /*Checking for and creating index on backup_set_uuid*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'IF NOT EXISTS ( + SELECT t.name, i.name + FROM ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.tables AS t + JOIN ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.indexes AS i + ON t.object_id = i.object_id + WHERE t.name = ? + AND i.name = ? + ) + + BEGIN + CREATE NONCLUSTERED INDEX [backupsetuuid] ON ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.[dbo].[backupset] ([backup_set_uuid] ASC) + END + ' + + SET @InnerStringToExecute = N'EXEC( ''' + @StringToExecute + ''', ''backupset'', ''backupsetuuid'' ) AT ' + QUOTENAME(@WriteBackupsToListenerName) + N';' + + IF @Debug = 1 + PRINT @InnerStringToExecute; + + EXEC sp_executesql @InnerStringToExecute + + + + /*Checking for and creating index on media_set_id*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += 'IF NOT EXISTS ( + SELECT t.name, i.name + FROM ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.tables AS t + JOIN ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.indexes AS i + ON t.object_id = i.object_id + WHERE t.name = ? + AND i.name = ? + ) + + BEGIN + CREATE NONCLUSTERED INDEX [backupsetMediaSetId] ON ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.[dbo].[backupset] ([media_set_id] ASC) + END + ' + + SET @InnerStringToExecute = N'EXEC( ''' + @StringToExecute + ''', ''backupset'', ''backupsetMediaSetId'' ) AT ' + QUOTENAME(@WriteBackupsToListenerName) + N';' + + IF @Debug = 1 + PRINT @InnerStringToExecute; + + EXEC sp_executesql @InnerStringToExecute + + + + /*Checking for and creating index on backup_finish_date*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'IF NOT EXISTS ( + SELECT t.name, i.name + FROM ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.tables AS t + JOIN ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.indexes AS i + ON t.object_id = i.object_id + WHERE t.name = ? + AND i.name = ? + ) + + BEGIN + CREATE NONCLUSTERED INDEX [backupsetDate] ON ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.[dbo].[backupset] ([backup_finish_date] ASC) + END + ' + + SET @InnerStringToExecute = N'EXEC( ''' + @StringToExecute + ''', ''backupset'', ''backupsetDate'' ) AT ' + QUOTENAME(@WriteBackupsToListenerName) + N';' + + IF @Debug = 1 + PRINT @InnerStringToExecute; + + EXEC sp_executesql @InnerStringToExecute + + + + /*Checking for and creating index on database_name*/ + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N'IF NOT EXISTS ( + SELECT t.name, i.name + FROM ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.tables AS t + JOIN ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.sys.indexes AS i + ON t.object_id = i.object_id + WHERE t.name = ? + AND i.name = ? + ) + + BEGIN + CREATE NONCLUSTERED INDEX [backupsetDatabaseName] ON ' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.[dbo].[backupset] ([database_name] ASC ) INCLUDE ([backup_set_id], [media_set_id]) + END + + ' + + SET @InnerStringToExecute = N'EXEC( ''' + @StringToExecute + ''', ''backupset'', ''backupsetDatabaseName'' ) AT ' + QUOTENAME(@WriteBackupsToListenerName) + N';' + + IF @Debug = 1 + PRINT @InnerStringToExecute; + + EXEC sp_executesql @InnerStringToExecute + + RAISERROR('Table and indexes created! You''re welcome!', 0, 1) WITH NOWAIT + END + + + RAISERROR('Beginning inserts', 0, 1) WITH NOWAIT; + RAISERROR(@crlf, 0, 1) WITH NOWAIT; + + /* + Batching code comes from the lovely and talented Michael J. Swart + http://michaeljswart.com/2014/09/take-care-when-scripting-batches/ + If you're ever in Canada, he says you can stay at his house, too. + */ + + + SET @StringToExecute = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + @crlf; + + SET @StringToExecute += N' + DECLARE + @StartDate DATETIME = DATEADD(HOUR, @i_WriteBackupsLastHours, SYSDATETIME()), + @StartDateNext DATETIME, + @RC INT = 1, + @msg NVARCHAR(4000) = N''''; + + SELECT @StartDate = MIN(b.backup_start_date) + FROM msdb.dbo.backupset b + WHERE b.backup_start_date >= @StartDate + AND NOT EXISTS ( + SELECT 1 + FROM ' + QUOTENAME(@WriteBackupsToListenerName) + N'.' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.dbo.backupset b2 + WHERE b.backup_set_uuid = b2.backup_set_uuid + AND b2.backup_start_date >= @StartDate + ) + + SET @StartDateNext = DATEADD(MINUTE, 10, @StartDate); + + IF + ( @StartDate IS NULL ) + BEGIN + SET @msg = N''No data to move, exiting.'' + RAISERROR(@msg, 0, 1) WITH NOWAIT + + RETURN; + END + + RAISERROR(''Starting insert loop'', 0, 1) WITH NOWAIT; + + WHILE EXISTS ( + SELECT 1 + FROM msdb.dbo.backupset b + WHERE NOT EXISTS ( + SELECT 1 + FROM ' + QUOTENAME(@WriteBackupsToListenerName) + N'.' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.dbo.backupset b2 + WHERE b.backup_set_uuid = b2.backup_set_uuid + AND b2.backup_start_date >= @StartDate + ) + ) + BEGIN + + SET @msg = N''Inserting data for '' + CONVERT(NVARCHAR(30), @StartDate) + '' through '' + + CONVERT(NVARCHAR(30), @StartDateNext) + ''.'' + RAISERROR(@msg, 0, 1) WITH NOWAIT + + ' + + SET @StringToExecute += N'INSERT ' + QUOTENAME(@WriteBackupsToListenerName) + N'.' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.dbo.backupset + ' + SET @StringToExecute += N' (database_name, database_guid, backup_set_uuid, type, backup_size, backup_start_date, backup_finish_date, media_set_id, time_zone, + compressed_backup_size, recovery_model, server_name, machine_name, first_lsn, last_lsn, user_name, compatibility_level, + is_password_protected, is_snapshot, is_readonly, is_single_user, has_backup_checksums, is_damaged, ' + CASE WHEN @ProductVersionMajor >= 12 + THEN + N'encryptor_type, has_bulk_logged_data)' + @crlf + ELSE + N'has_bulk_logged_data)' + @crlf + END + + SET @StringToExecute +=N' + SELECT database_name, database_guid, backup_set_uuid, type, backup_size, backup_start_date, backup_finish_date, media_set_id, time_zone, + compressed_backup_size, recovery_model, server_name, machine_name, first_lsn, last_lsn, user_name, compatibility_level, + is_password_protected, is_snapshot, is_readonly, is_single_user, has_backup_checksums, is_damaged, ' + CASE WHEN @ProductVersionMajor >= 12 + THEN + N'encryptor_type, has_bulk_logged_data' + @crlf + ELSE + N'has_bulk_logged_data' + @crlf + END + SET @StringToExecute +=N' + FROM msdb.dbo.backupset b + WHERE 1=1 + AND b.backup_start_date >= @StartDate + AND b.backup_start_date < @StartDateNext + AND NOT EXISTS ( + SELECT 1 + FROM ' + QUOTENAME(@WriteBackupsToListenerName) + N'.' + QUOTENAME(@WriteBackupsToDatabaseName) + N'.dbo.backupset b2 + WHERE b.backup_set_uuid = b2.backup_set_uuid + AND b2.backup_start_date >= @StartDate + )' + @crlf; + + + SET @StringToExecute +=N' + SET @RC = @@ROWCOUNT; + + SET @msg = N''Inserted '' + CONVERT(NVARCHAR(30), @RC) + '' rows for ''+ CONVERT(NVARCHAR(30), @StartDate) + '' through '' + CONVERT(NVARCHAR(30), @StartDateNext) + ''.'' + RAISERROR(@msg, 0, 1) WITH NOWAIT + + SET @StartDate = @StartDateNext; + SET @StartDateNext = DATEADD(MINUTE, 10, @StartDate); + + IF + ( @StartDate > SYSDATETIME() ) + BEGIN + + SET @msg = N''No more data to move, exiting.'' + RAISERROR(@msg, 0, 1) WITH NOWAIT + + BREAK; + + END + END' + @crlf; + + IF @Debug = 1 + PRINT @StringToExecute; + + EXEC sp_executesql @StringToExecute, N'@i_WriteBackupsLastHours INT', @i_WriteBackupsLastHours = @WriteBackupsLastHours; + +END; + +END; + +GO +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; +SET ANSI_WARNINGS ON; +SET ARITHABORT ON; +SET CONCAT_NULL_YIELDS_NULL ON; +SET QUOTED_IDENTIFIER ON; +SET STATISTICS IO OFF; +SET STATISTICS TIME OFF; +GO + +IF ( +SELECT + CASE + WHEN CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')) LIKE '8%' THEN 0 + WHEN CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')) LIKE '9%' THEN 0 + ELSE 1 + END +) = 0 +BEGIN + DECLARE @msg VARCHAR(8000); + SELECT @msg = 'Sorry, sp_BlitzCache doesn''t work on versions of SQL prior to 2008.' + REPLICATE(CHAR(13), 7933); + PRINT @msg; + RETURN; +END; + +IF OBJECT_ID('dbo.sp_BlitzCache') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_BlitzCache AS RETURN 0;'); +GO + +IF OBJECT_ID('dbo.sp_BlitzCache') IS NOT NULL AND OBJECT_ID('tempdb.dbo.##BlitzCacheProcs', 'U') IS NOT NULL + EXEC ('DROP TABLE ##BlitzCacheProcs;'); +GO + +IF OBJECT_ID('dbo.sp_BlitzCache') IS NOT NULL AND OBJECT_ID('tempdb.dbo.##BlitzCacheResults', 'U') IS NOT NULL + EXEC ('DROP TABLE ##BlitzCacheResults;'); +GO + +CREATE TABLE ##BlitzCacheResults ( + SPID INT, + ID INT IDENTITY(1,1), + CheckID INT, + Priority TINYINT, + FindingsGroup VARCHAR(50), + Finding VARCHAR(500), + URL VARCHAR(200), + Details VARCHAR(4000) +); + +CREATE TABLE ##BlitzCacheProcs ( + SPID INT , + QueryType NVARCHAR(258), + DatabaseName sysname, + AverageCPU DECIMAL(38,4), + AverageCPUPerMinute DECIMAL(38,4), + TotalCPU DECIMAL(38,4), + PercentCPUByType MONEY, + PercentCPU MONEY, + AverageDuration DECIMAL(38,4), + TotalDuration DECIMAL(38,4), + PercentDuration MONEY, + PercentDurationByType MONEY, + AverageReads BIGINT, + TotalReads BIGINT, + PercentReads MONEY, + PercentReadsByType MONEY, + ExecutionCount BIGINT, + PercentExecutions MONEY, + PercentExecutionsByType MONEY, + ExecutionsPerMinute MONEY, + TotalWrites BIGINT, + AverageWrites MONEY, + PercentWrites MONEY, + PercentWritesByType MONEY, + WritesPerMinute MONEY, + PlanCreationTime DATETIME, + PlanCreationTimeHours AS DATEDIFF(HOUR, PlanCreationTime, SYSDATETIME()), + LastExecutionTime DATETIME, + LastCompletionTime DATETIME, + PlanHandle VARBINARY(64), + [Remove Plan Handle From Cache] AS + CASE WHEN [PlanHandle] IS NOT NULL + THEN 'DBCC FREEPROCCACHE (' + CONVERT(VARCHAR(128), [PlanHandle], 1) + ');' + ELSE 'N/A' END, + SqlHandle VARBINARY(64), + [Remove SQL Handle From Cache] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN 'DBCC FREEPROCCACHE (' + CONVERT(VARCHAR(128), [SqlHandle], 1) + ');' + ELSE 'N/A' END, + [SQL Handle More Info] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN 'EXEC sp_BlitzCache @OnlySqlHandles = ''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + '''; ' + ELSE 'N/A' END, + QueryHash BINARY(8), + [Query Hash More Info] AS + CASE WHEN [QueryHash] IS NOT NULL + THEN 'EXEC sp_BlitzCache @OnlyQueryHashes = ''' + CONVERT(VARCHAR(32), [QueryHash], 1) + '''; ' + ELSE 'N/A' END, + QueryPlanHash BINARY(8), + StatementStartOffset INT, + StatementEndOffset INT, + PlanGenerationNum BIGINT, + MinReturnedRows BIGINT, + MaxReturnedRows BIGINT, + AverageReturnedRows MONEY, + TotalReturnedRows BIGINT, + LastReturnedRows BIGINT, + /*The Memory Grant columns are only supported + in certain versions, giggle giggle. + */ + MinGrantKB BIGINT, + MaxGrantKB BIGINT, + MinUsedGrantKB BIGINT, + MaxUsedGrantKB BIGINT, + PercentMemoryGrantUsed MONEY, + AvgMaxMemoryGrant MONEY, + MinSpills BIGINT, + MaxSpills BIGINT, + TotalSpills BIGINT, + AvgSpills MONEY, + QueryText NVARCHAR(MAX), + QueryPlan XML, + /* these next four columns are the total for the type of query. + don't actually use them for anything apart from math by type. + */ + TotalWorkerTimeForType BIGINT, + TotalElapsedTimeForType BIGINT, + TotalReadsForType DECIMAL(30), + TotalExecutionCountForType BIGINT, + TotalWritesForType DECIMAL(30), + NumberOfPlans INT, + NumberOfDistinctPlans INT, + SerialDesiredMemory FLOAT, + SerialRequiredMemory FLOAT, + CachedPlanSize FLOAT, + CompileTime FLOAT, + CompileCPU FLOAT , + CompileMemory FLOAT , + MaxCompileMemory FLOAT , + min_worker_time BIGINT, + max_worker_time BIGINT, + is_forced_plan BIT, + is_forced_parameterized BIT, + is_cursor BIT, + is_optimistic_cursor BIT, + is_forward_only_cursor BIT, + is_fast_forward_cursor BIT, + is_cursor_dynamic BIT, + is_parallel BIT, + is_forced_serial BIT, + is_key_lookup_expensive BIT, + key_lookup_cost FLOAT, + is_remote_query_expensive BIT, + remote_query_cost FLOAT, + frequent_execution BIT, + parameter_sniffing BIT, + unparameterized_query BIT, + near_parallel BIT, + plan_warnings BIT, + plan_multiple_plans INT, + long_running BIT, + downlevel_estimator BIT, + implicit_conversions BIT, + busy_loops BIT, + tvf_join BIT, + tvf_estimate BIT, + compile_timeout BIT, + compile_memory_limit_exceeded BIT, + warning_no_join_predicate BIT, + QueryPlanCost FLOAT, + missing_index_count INT, + unmatched_index_count INT, + min_elapsed_time BIGINT, + max_elapsed_time BIGINT, + age_minutes MONEY, + age_minutes_lifetime MONEY, + is_trivial BIT, + trace_flags_session VARCHAR(1000), + is_unused_grant BIT, + function_count INT, + clr_function_count INT, + is_table_variable BIT, + no_stats_warning BIT, + relop_warnings BIT, + is_table_scan BIT, + backwards_scan BIT, + forced_index BIT, + forced_seek BIT, + forced_scan BIT, + columnstore_row_mode BIT, + is_computed_scalar BIT , + is_sort_expensive BIT, + sort_cost FLOAT, + is_computed_filter BIT, + op_name VARCHAR(100) NULL, + index_insert_count INT NULL, + index_update_count INT NULL, + index_delete_count INT NULL, + cx_insert_count INT NULL, + cx_update_count INT NULL, + cx_delete_count INT NULL, + table_insert_count INT NULL, + table_update_count INT NULL, + table_delete_count INT NULL, + index_ops AS (index_insert_count + index_update_count + index_delete_count + + cx_insert_count + cx_update_count + cx_delete_count + + table_insert_count + table_update_count + table_delete_count), + is_row_level BIT, + is_spatial BIT, + index_dml BIT, + table_dml BIT, + long_running_low_cpu BIT, + low_cost_high_cpu BIT, + stale_stats BIT, + is_adaptive BIT, + index_spool_cost FLOAT, + index_spool_rows FLOAT, + table_spool_cost FLOAT, + table_spool_rows FLOAT, + is_spool_expensive BIT, + is_spool_more_rows BIT, + is_table_spool_expensive BIT, + is_table_spool_more_rows BIT, + estimated_rows FLOAT, + is_bad_estimate BIT, + is_paul_white_electric BIT, + is_row_goal BIT, + is_big_spills BIT, + is_mstvf BIT, + is_mm_join BIT, + is_nonsargable BIT, + select_with_writes BIT, + implicit_conversion_info XML, + cached_execution_parameters XML, + missing_indexes XML, + SetOptions VARCHAR(MAX), + Warnings VARCHAR(MAX), + Pattern NVARCHAR(20) + ); +GO + +ALTER PROCEDURE dbo.sp_BlitzCache + @Help BIT = 0, + @Top INT = NULL, + @SortOrder VARCHAR(50) = 'CPU', + @UseTriggersAnyway BIT = NULL, + @ExportToExcel BIT = 0, + @ExpertMode TINYINT = 0, + @OutputType VARCHAR(20) = 'TABLE' , + @OutputServerName NVARCHAR(258) = NULL , + @OutputDatabaseName NVARCHAR(258) = NULL , + @OutputSchemaName NVARCHAR(258) = NULL , + @OutputTableName NVARCHAR(258) = NULL , -- do NOT use ##BlitzCacheResults or ##BlitzCacheProcs as they are used as work tables in this procedure + @ConfigurationDatabaseName NVARCHAR(128) = NULL , + @ConfigurationSchemaName NVARCHAR(258) = NULL , + @ConfigurationTableName NVARCHAR(258) = NULL , + @DurationFilter DECIMAL(38,4) = NULL , + @HideSummary BIT = 0 , + @IgnoreSystemDBs BIT = 1 , + @OnlyQueryHashes VARCHAR(MAX) = NULL , + @IgnoreQueryHashes VARCHAR(MAX) = NULL , + @OnlySqlHandles VARCHAR(MAX) = NULL , + @IgnoreSqlHandles VARCHAR(MAX) = NULL , + @QueryFilter VARCHAR(10) = 'ALL' , + @DatabaseName NVARCHAR(128) = NULL , + @StoredProcName NVARCHAR(128) = NULL, + @SlowlySearchPlansFor NVARCHAR(4000) = NULL, + @Reanalyze BIT = 0 , + @SkipAnalysis BIT = 0 , + @BringThePain BIT = 0 , + @MinimumExecutionCount INT = 0, + @Debug BIT = 0, + @CheckDateOverride DATETIMEOFFSET = NULL, + @MinutesBack INT = NULL, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS +BEGIN +SET NOCOUNT ON; +SET STATISTICS XML OFF; +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + +SELECT @Version = '8.16', @VersionDate = '20230820'; +SET @OutputType = UPPER(@OutputType); + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + +DECLARE @nl NVARCHAR(2) = NCHAR(13) + NCHAR(10) ; + +IF @Help = 1 + BEGIN + PRINT ' + sp_BlitzCache from http://FirstResponderKit.org + + This script displays your most resource-intensive queries from the plan cache, + and points to ways you can tune these queries to make them faster. + + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - SQL Server 2008 and 2008R2 have a bug in trigger stats, so that output is + excluded by default. + - @IgnoreQueryHashes and @OnlyQueryHashes require a CSV list of hashes + with no spaces between the hash values. + + Unknown limitations of this version: + - May or may not be vulnerable to the wick effect. + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + '; + + + SELECT N'@Help' AS [Parameter Name] , + N'BIT' AS [Data Type] , + N'Displays this help message.' AS [Parameter Description] + + UNION ALL + SELECT N'@Top', + N'INT', + N'The number of records to retrieve and analyze from the plan cache. The following DMVs are used as the plan cache: dm_exec_query_stats, dm_exec_procedure_stats, dm_exec_trigger_stats.' + + UNION ALL + SELECT N'@SortOrder', + N'VARCHAR(10)', + N'Data processing and display order. @SortOrder will still be used, even when preparing output for a table or for excel. Possible values are: "CPU", "Reads", "Writes", "Duration", "Executions", "Recent Compilations", "Memory Grant", "Unused Grant", "Spills", "Query Hash". Additionally, the word "Average" or "Avg" can be used to sort on averages rather than total. "Executions per minute" and "Executions / minute" can be used to sort by execution per minute. For the truly lazy, "xpm" can also be used. Note that when you use all or all avg, the only parameters you can use are @Top and @DatabaseName. All others will be ignored.' + + UNION ALL + SELECT N'@UseTriggersAnyway', + N'BIT', + N'On SQL Server 2008R2 and earlier, trigger execution count is incorrect - trigger execution count is incremented once per execution of a SQL agent job. If you still want to see relative execution count of triggers, then you can force sp_BlitzCache to include this information.' + + UNION ALL + SELECT N'@ExportToExcel', + N'BIT', + N'Prepare output for exporting to Excel. Newlines and additional whitespace are removed from query text and the execution plan is not displayed.' + + UNION ALL + SELECT N'@ExpertMode', + N'TINYINT', + N'Default 0. When set to 1, results include more columns. When 2, mode is optimized for Opserver, the open source dashboard.' + UNION ALL + SELECT N'@OutputType', + N'NVARCHAR(258)', + N'If set to "NONE", this will tell this procedure not to run any query leading to a results set thrown to caller.' + + UNION ALL + SELECT N'@OutputDatabaseName', + N'NVARCHAR(128)', + N'The output database. If this does not exist SQL Server will divide by zero and everything will fall apart.' + + UNION ALL + SELECT N'@OutputSchemaName', + N'NVARCHAR(258)', + N'The output schema. If this does not exist SQL Server will divide by zero and everything will fall apart.' + + UNION ALL + SELECT N'@OutputTableName', + N'NVARCHAR(258)', + N'The output table. If this does not exist, it will be created for you.' + + UNION ALL + SELECT N'@DurationFilter', + N'DECIMAL(38,4)', + N'Excludes queries with an average duration (in seconds) less than @DurationFilter.' + + UNION ALL + SELECT N'@HideSummary', + N'BIT', + N'Hides the findings summary result set.' + + UNION ALL + SELECT N'@IgnoreSystemDBs', + N'BIT', + N'Ignores plans found in the system databases (master, model, msdb, tempdb, dbmaintenance, dbadmin, dbatools and resourcedb)' + + UNION ALL + SELECT N'@OnlyQueryHashes', + N'VARCHAR(MAX)', + N'A list of query hashes to query. All other query hashes will be ignored. Stored procedures and triggers will be ignored.' + + UNION ALL + SELECT N'@IgnoreQueryHashes', + N'VARCHAR(MAX)', + N'A list of query hashes to ignore.' + + UNION ALL + SELECT N'@OnlySqlHandles', + N'VARCHAR(MAX)', + N'One or more sql_handles to use for filtering results.' + + UNION ALL + SELECT N'@IgnoreSqlHandles', + N'VARCHAR(MAX)', + N'One or more sql_handles to ignore.' + + UNION ALL + SELECT N'@DatabaseName', + N'NVARCHAR(128)', + N'A database name which is used for filtering results.' + + UNION ALL + SELECT N'@StoredProcName', + N'NVARCHAR(128)', + N'Name of stored procedure you want to find plans for.' + + UNION ALL + SELECT N'@SlowlySearchPlansFor', + N'NVARCHAR(4000)', + N'String to search for in plan text. % wildcards allowed.' + + UNION ALL + SELECT N'@BringThePain', + N'BIT', + N'When using @SortOrder = ''all'' and @Top > 10, we require you to set @BringThePain = 1 so you understand that sp_BlitzCache will take a while to run.' + + UNION ALL + SELECT N'@QueryFilter', + N'VARCHAR(10)', + N'Filter out stored procedures or statements. The default value is ''ALL''. Allowed values are ''procedures'', ''statements'', ''functions'', or ''all'' (any variation in capitalization is acceptable).' + + UNION ALL + SELECT N'@Reanalyze', + N'BIT', + N'The default is 0. When set to 0, sp_BlitzCache will re-evalute the plan cache. Set this to 1 to reanalyze existing results' + + UNION ALL + SELECT N'@MinimumExecutionCount', + N'INT', + N'Queries with fewer than this number of executions will be omitted from results.' + + UNION ALL + SELECT N'@Debug', + N'BIT', + N'Setting this to 1 will print dynamic SQL and select data from all tables used.' + + UNION ALL + SELECT N'@MinutesBack', + N'INT', + N'How many minutes back to begin plan cache analysis. If you put in a positive number, we''ll flip it to negtive.'; + + + /* Column definitions */ + SELECT N'# Executions' AS [Column Name], + N'BIGINT' AS [Data Type], + N'The number of executions of this particular query. This is computed across statements, procedures, and triggers and aggregated by the SQL handle.' AS [Column Description] + + UNION ALL + SELECT N'Executions / Minute', + N'MONEY', + N'Number of executions per minute - calculated for the life of the current plan. Plan life is the last execution time minus the plan creation time.' + + UNION ALL + SELECT N'Execution Weight', + N'MONEY', + N'An arbitrary metric of total "execution-ness". A weight of 2 is "one more" than a weight of 1.' + + UNION ALL + SELECT N'Database', + N'sysname', + N'The name of the database where the plan was encountered. If the database name cannot be determined for some reason, a value of NA will be substituted. A value of 32767 indicates the plan comes from ResourceDB.' + + UNION ALL + SELECT N'Total CPU', + N'BIGINT', + N'Total CPU time, reported in milliseconds, that was consumed by all executions of this query since the last compilation.' + + UNION ALL + SELECT N'Avg CPU', + N'BIGINT', + N'Average CPU time, reported in milliseconds, consumed by each execution of this query since the last compilation.' + + UNION ALL + SELECT N'CPU Weight', + N'MONEY', + N'An arbitrary metric of total "CPU-ness". A weight of 2 is "one more" than a weight of 1.' + + UNION ALL + SELECT N'Total Duration', + N'BIGINT', + N'Total elapsed time, reported in milliseconds, consumed by all executions of this query since last compilation.' + + UNION ALL + SELECT N'Avg Duration', + N'BIGINT', + N'Average elapsed time, reported in milliseconds, consumed by each execution of this query since the last compilation.' + + UNION ALL + SELECT N'Duration Weight', + N'MONEY', + N'An arbitrary metric of total "Duration-ness". A weight of 2 is "one more" than a weight of 1.' + + UNION ALL + SELECT N'Total Reads', + N'BIGINT', + N'Total logical reads performed by this query since last compilation.' + + UNION ALL + SELECT N'Average Reads', + N'BIGINT', + N'Average logical reads performed by each execution of this query since the last compilation.' + + UNION ALL + SELECT N'Read Weight', + N'MONEY', + N'An arbitrary metric of "Read-ness". A weight of 2 is "one more" than a weight of 1.' + + UNION ALL + SELECT N'Total Writes', + N'BIGINT', + N'Total logical writes performed by this query since last compilation.' + + UNION ALL + SELECT N'Average Writes', + N'BIGINT', + N'Average logical writes performed by each execution this query since last compilation.' + + UNION ALL + SELECT N'Write Weight', + N'MONEY', + N'An arbitrary metric of "Write-ness". A weight of 2 is "one more" than a weight of 1.' + + UNION ALL + SELECT N'Query Type', + N'NVARCHAR(258)', + N'The type of query being examined. This can be "Procedure", "Statement", or "Trigger".' + + UNION ALL + SELECT N'Query Text', + N'NVARCHAR(4000)', + N'The text of the query. This may be truncated by either SQL Server or by sp_BlitzCache(tm) for display purposes.' + + UNION ALL + SELECT N'% Executions (Type)', + N'MONEY', + N'Percent of executions relative to the type of query - e.g. 17.2% of all stored procedure executions.' + + UNION ALL + SELECT N'% CPU (Type)', + N'MONEY', + N'Percent of CPU time consumed by this query for a given type of query - e.g. 22% of CPU of all stored procedures executed.' + + UNION ALL + SELECT N'% Duration (Type)', + N'MONEY', + N'Percent of elapsed time consumed by this query for a given type of query - e.g. 12% of all statements executed.' + + UNION ALL + SELECT N'% Reads (Type)', + N'MONEY', + N'Percent of reads consumed by this query for a given type of query - e.g. 34.2% of all stored procedures executed.' + + UNION ALL + SELECT N'% Writes (Type)', + N'MONEY', + N'Percent of writes performed by this query for a given type of query - e.g. 43.2% of all statements executed.' + + UNION ALL + SELECT N'Total Rows', + N'BIGINT', + N'Total number of rows returned for all executions of this query. This only applies to query level stats, not stored procedures or triggers.' + + UNION ALL + SELECT N'Average Rows', + N'MONEY', + N'Average number of rows returned by each execution of the query.' + + UNION ALL + SELECT N'Min Rows', + N'BIGINT', + N'The minimum number of rows returned by any execution of this query.' + + UNION ALL + SELECT N'Max Rows', + N'BIGINT', + N'The maximum number of rows returned by any execution of this query.' + + UNION ALL + SELECT N'MinGrantKB', + N'BIGINT', + N'The minimum memory grant the query received in kb.' + + UNION ALL + SELECT N'MaxGrantKB', + N'BIGINT', + N'The maximum memory grant the query received in kb.' + + UNION ALL + SELECT N'MinUsedGrantKB', + N'BIGINT', + N'The minimum used memory grant the query received in kb.' + + UNION ALL + SELECT N'MaxUsedGrantKB', + N'BIGINT', + N'The maximum used memory grant the query received in kb.' + + UNION ALL + SELECT N'MinSpills', + N'BIGINT', + N'The minimum amount this query has spilled to tempdb in 8k pages.' + + UNION ALL + SELECT N'MaxSpills', + N'BIGINT', + N'The maximum amount this query has spilled to tempdb in 8k pages.' + + UNION ALL + SELECT N'TotalSpills', + N'BIGINT', + N'The total amount this query has spilled to tempdb in 8k pages.' + + UNION ALL + SELECT N'AvgSpills', + N'BIGINT', + N'The average amount this query has spilled to tempdb in 8k pages.' + + UNION ALL + SELECT N'PercentMemoryGrantUsed', + N'MONEY', + N'Result of dividing the maximum grant used by the minimum granted.' + + UNION ALL + SELECT N'AvgMaxMemoryGrant', + N'MONEY', + N'The average maximum memory grant for a query.' + + UNION ALL + SELECT N'# Plans', + N'INT', + N'The total number of execution plans found that match a given query.' + + UNION ALL + SELECT N'# Distinct Plans', + N'INT', + N'The number of distinct execution plans that match a given query. ' + + NCHAR(13) + NCHAR(10) + + N'This may be caused by running the same query across multiple databases or because of a lack of proper parameterization in the database.' + + UNION ALL + SELECT N'Created At', + N'DATETIME', + N'Time that the execution plan was last compiled.' + + UNION ALL + SELECT N'Last Execution', + N'DATETIME', + N'The last time that this query was executed.' + + UNION ALL + SELECT N'Query Plan', + N'XML', + N'The query plan. Click to display a graphical plan or, if you need to patch SSMS, a pile of XML.' + + UNION ALL + SELECT N'Plan Handle', + N'VARBINARY(64)', + N'An arbitrary identifier referring to the compiled plan this query is a part of.' + + UNION ALL + SELECT N'SQL Handle', + N'VARBINARY(64)', + N'An arbitrary identifier referring to a batch or stored procedure that this query is a part of.' + + UNION ALL + SELECT N'Query Hash', + N'BINARY(8)', + N'A hash of the query. Queries with the same query hash have similar logic but only differ by literal values or database.' + + UNION ALL + SELECT N'Warnings', + N'VARCHAR(MAX)', + N'A list of individual warnings generated by this query.' ; + + + + /* Configuration table description */ + SELECT N'Frequent Execution Threshold' AS [Configuration Parameter] , + N'100' AS [Default Value] , + N'Executions / Minute' AS [Unit of Measure] , + N'Executions / Minute before a "Frequent Execution Threshold" warning is triggered.' AS [Description] + + UNION ALL + SELECT N'Parameter Sniffing Variance Percent' , + N'30' , + N'Percent' , + N'Variance required between min/max values and average values before a "Parameter Sniffing" warning is triggered. Applies to worker time and returned rows.' + + UNION ALL + SELECT N'Parameter Sniffing IO Threshold' , + N'100,000' , + N'Logical reads' , + N'Minimum number of average logical reads before parameter sniffing checks are evaluated.' + + UNION ALL + SELECT N'Cost Threshold for Parallelism Warning' AS [Configuration Parameter] , + N'10' , + N'Percent' , + N'Trigger a "Nearly Parallel" warning when a query''s cost is within X percent of the cost threshold for parallelism.' + + UNION ALL + SELECT N'Long Running Query Warning' AS [Configuration Parameter] , + N'300' , + N'Seconds' , + N'Triggers a "Long Running Query Warning" when average duration, max CPU time, or max clock time is higher than this number.' + + UNION ALL + SELECT N'Unused Memory Grant Warning' AS [Configuration Parameter] , + N'10' , + N'Percent' , + N'Triggers an "Unused Memory Grant Warning" when a query uses >= X percent of its memory grant.'; + RETURN; + END; /* IF @Help = 1 */ + + + +/*Validate version*/ +IF ( +SELECT + CASE + WHEN CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')) LIKE '8%' THEN 0 + WHEN CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')) LIKE '9%' THEN 0 + ELSE 1 + END +) = 0 +BEGIN + DECLARE @version_msg VARCHAR(8000); + SELECT @version_msg = 'Sorry, sp_BlitzCache doesn''t work on versions of SQL prior to 2008.' + REPLICATE(CHAR(13), 7933); + PRINT @version_msg; + RETURN; +END; + +IF(@OutputType = 'NONE' AND (@OutputTableName IS NULL OR @OutputSchemaName IS NULL OR @OutputDatabaseName IS NULL)) +BEGIN + RAISERROR('This procedure should be called with a value for all @Output* parameters, as @OutputType is set to NONE',12,1); + RETURN; +END; + +IF(@OutputType = 'NONE') +BEGIN + SET @HideSummary = 1; +END; + + +/* Lets get @SortOrder set to lower case here for comparisons later */ +SET @SortOrder = LOWER(@SortOrder); + +/* If they want to sort by query hash, populate the @OnlyQueryHashes list for them */ +IF @SortOrder LIKE 'query hash%' + BEGIN + RAISERROR('Beginning query hash sort', 0, 1) WITH NOWAIT; + + SELECT qs.query_hash, + MAX(qs.max_worker_time) AS max_worker_time, + COUNT_BIG(*) AS records + INTO #query_hash_grouped + FROM sys.dm_exec_query_stats AS qs + CROSS APPLY ( SELECT pa.value + FROM sys.dm_exec_plan_attributes(qs.plan_handle) AS pa + WHERE pa.attribute = 'dbid' ) AS ca + GROUP BY qs.query_hash, ca.value + HAVING COUNT_BIG(*) > 1 + ORDER BY max_worker_time DESC, + records DESC; + + SELECT TOP (1) + @OnlyQueryHashes = STUFF((SELECT DISTINCT N',' + CONVERT(NVARCHAR(MAX), qhg.query_hash, 1) + FROM #query_hash_grouped AS qhg + WHERE qhg.query_hash <> 0x00 + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + OPTION(RECOMPILE); + + /* When they ran it, @SortOrder probably looked like 'query hash, cpu', so strip the first sort order out: */ + SELECT @SortOrder = LTRIM(REPLACE(REPLACE(@SortOrder,'query hash', ''), ',', '')); + + /* If they just called it with @SortOrder = 'query hash', set it to 'cpu' for backwards compatibility: */ + IF @SortOrder = '' SET @SortOrder = 'cpu'; + + END + + +/* Set @Top based on sort */ +IF ( + @Top IS NULL + AND @SortOrder IN ( 'all', 'all sort' ) + ) + BEGIN + SET @Top = 5; + END; + +IF ( + @Top IS NULL + AND @SortOrder NOT IN ( 'all', 'all sort' ) + ) + BEGIN + SET @Top = 10; + END; + +/* validate user inputs */ +IF @Top IS NULL + OR @SortOrder IS NULL + OR @QueryFilter IS NULL + OR @Reanalyze IS NULL +BEGIN + RAISERROR(N'Several parameters (@Top, @SortOrder, @QueryFilter, @renalyze) are required. Do not set them to NULL. Please try again.', 16, 1) WITH NOWAIT; + RETURN; +END; + +RAISERROR(N'Checking @MinutesBack validity.', 0, 1) WITH NOWAIT; +IF @MinutesBack IS NOT NULL + BEGIN + IF @MinutesBack > 0 + BEGIN + RAISERROR(N'Setting @MinutesBack to a negative number', 0, 1) WITH NOWAIT; + SET @MinutesBack *=-1; + END; + IF @MinutesBack = 0 + BEGIN + RAISERROR(N'@MinutesBack can''t be 0, setting to -1', 0, 1) WITH NOWAIT; + SET @MinutesBack = -1; + END; + END; + + +RAISERROR(N'Creating temp tables for results and warnings.', 0, 1) WITH NOWAIT; + + +IF OBJECT_ID('tempdb.dbo.##BlitzCacheResults') IS NULL +BEGIN + CREATE TABLE ##BlitzCacheResults ( + SPID INT, + ID INT IDENTITY(1,1), + CheckID INT, + Priority TINYINT, + FindingsGroup VARCHAR(50), + Finding VARCHAR(500), + URL VARCHAR(200), + Details VARCHAR(4000) + ); +END; + +IF OBJECT_ID('tempdb.dbo.##BlitzCacheProcs') IS NULL +BEGIN + CREATE TABLE ##BlitzCacheProcs ( + SPID INT , + QueryType NVARCHAR(258), + DatabaseName sysname, + AverageCPU DECIMAL(38,4), + AverageCPUPerMinute DECIMAL(38,4), + TotalCPU DECIMAL(38,4), + PercentCPUByType MONEY, + PercentCPU MONEY, + AverageDuration DECIMAL(38,4), + TotalDuration DECIMAL(38,4), + PercentDuration MONEY, + PercentDurationByType MONEY, + AverageReads BIGINT, + TotalReads BIGINT, + PercentReads MONEY, + PercentReadsByType MONEY, + ExecutionCount BIGINT, + PercentExecutions MONEY, + PercentExecutionsByType MONEY, + ExecutionsPerMinute MONEY, + TotalWrites BIGINT, + AverageWrites MONEY, + PercentWrites MONEY, + PercentWritesByType MONEY, + WritesPerMinute MONEY, + PlanCreationTime DATETIME, + PlanCreationTimeHours AS DATEDIFF(HOUR, PlanCreationTime, SYSDATETIME()), + LastExecutionTime DATETIME, + LastCompletionTime DATETIME, + PlanHandle VARBINARY(64), + [Remove Plan Handle From Cache] AS + CASE WHEN [PlanHandle] IS NOT NULL + THEN 'DBCC FREEPROCCACHE (' + CONVERT(VARCHAR(128), [PlanHandle], 1) + ');' + ELSE 'N/A' END, + SqlHandle VARBINARY(64), + [Remove SQL Handle From Cache] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN 'DBCC FREEPROCCACHE (' + CONVERT(VARCHAR(128), [SqlHandle], 1) + ');' + ELSE 'N/A' END, + [SQL Handle More Info] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN 'EXEC sp_BlitzCache @OnlySqlHandles = ''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + '''; ' + ELSE 'N/A' END, + QueryHash BINARY(8), + [Query Hash More Info] AS + CASE WHEN [QueryHash] IS NOT NULL + THEN 'EXEC sp_BlitzCache @OnlyQueryHashes = ''' + CONVERT(VARCHAR(32), [QueryHash], 1) + '''; ' + ELSE 'N/A' END, + QueryPlanHash BINARY(8), + StatementStartOffset INT, + StatementEndOffset INT, + PlanGenerationNum BIGINT, + MinReturnedRows BIGINT, + MaxReturnedRows BIGINT, + AverageReturnedRows MONEY, + TotalReturnedRows BIGINT, + LastReturnedRows BIGINT, + MinGrantKB BIGINT, + MaxGrantKB BIGINT, + MinUsedGrantKB BIGINT, + MaxUsedGrantKB BIGINT, + PercentMemoryGrantUsed MONEY, + AvgMaxMemoryGrant MONEY, + MinSpills BIGINT, + MaxSpills BIGINT, + TotalSpills BIGINT, + AvgSpills MONEY, + QueryText NVARCHAR(MAX), + QueryPlan XML, + /* these next four columns are the total for the type of query. + don't actually use them for anything apart from math by type. + */ + TotalWorkerTimeForType BIGINT, + TotalElapsedTimeForType BIGINT, + TotalReadsForType BIGINT, + TotalExecutionCountForType BIGINT, + TotalWritesForType BIGINT, + NumberOfPlans INT, + NumberOfDistinctPlans INT, + SerialDesiredMemory FLOAT, + SerialRequiredMemory FLOAT, + CachedPlanSize FLOAT, + CompileTime FLOAT, + CompileCPU FLOAT , + CompileMemory FLOAT , + MaxCompileMemory FLOAT , + min_worker_time BIGINT, + max_worker_time BIGINT, + is_forced_plan BIT, + is_forced_parameterized BIT, + is_cursor BIT, + is_optimistic_cursor BIT, + is_forward_only_cursor BIT, + is_fast_forward_cursor BIT, + is_cursor_dynamic BIT, + is_parallel BIT, + is_forced_serial BIT, + is_key_lookup_expensive BIT, + key_lookup_cost FLOAT, + is_remote_query_expensive BIT, + remote_query_cost FLOAT, + frequent_execution BIT, + parameter_sniffing BIT, + unparameterized_query BIT, + near_parallel BIT, + plan_warnings BIT, + plan_multiple_plans INT, + long_running BIT, + downlevel_estimator BIT, + implicit_conversions BIT, + busy_loops BIT, + tvf_join BIT, + tvf_estimate BIT, + compile_timeout BIT, + compile_memory_limit_exceeded BIT, + warning_no_join_predicate BIT, + QueryPlanCost FLOAT, + missing_index_count INT, + unmatched_index_count INT, + min_elapsed_time BIGINT, + max_elapsed_time BIGINT, + age_minutes MONEY, + age_minutes_lifetime MONEY, + is_trivial BIT, + trace_flags_session VARCHAR(1000), + is_unused_grant BIT, + function_count INT, + clr_function_count INT, + is_table_variable BIT, + no_stats_warning BIT, + relop_warnings BIT, + is_table_scan BIT, + backwards_scan BIT, + forced_index BIT, + forced_seek BIT, + forced_scan BIT, + columnstore_row_mode BIT, + is_computed_scalar BIT , + is_sort_expensive BIT, + sort_cost FLOAT, + is_computed_filter BIT, + op_name VARCHAR(100) NULL, + index_insert_count INT NULL, + index_update_count INT NULL, + index_delete_count INT NULL, + cx_insert_count INT NULL, + cx_update_count INT NULL, + cx_delete_count INT NULL, + table_insert_count INT NULL, + table_update_count INT NULL, + table_delete_count INT NULL, + index_ops AS (index_insert_count + index_update_count + index_delete_count + + cx_insert_count + cx_update_count + cx_delete_count + + table_insert_count + table_update_count + table_delete_count), + is_row_level BIT, + is_spatial BIT, + index_dml BIT, + table_dml BIT, + long_running_low_cpu BIT, + low_cost_high_cpu BIT, + stale_stats BIT, + is_adaptive BIT, + index_spool_cost FLOAT, + index_spool_rows FLOAT, + table_spool_cost FLOAT, + table_spool_rows FLOAT, + is_spool_expensive BIT, + is_spool_more_rows BIT, + is_table_spool_expensive BIT, + is_table_spool_more_rows BIT, + estimated_rows FLOAT, + is_bad_estimate BIT, + is_paul_white_electric BIT, + is_row_goal BIT, + is_big_spills BIT, + is_mstvf BIT, + is_mm_join BIT, + is_nonsargable BIT, + select_with_writes BIT, + implicit_conversion_info XML, + cached_execution_parameters XML, + missing_indexes XML, + SetOptions VARCHAR(MAX), + Warnings VARCHAR(MAX), + Pattern NVARCHAR(20) + ); +END; + +DECLARE @DurationFilter_i INT, + @MinMemoryPerQuery INT, + @msg NVARCHAR(4000), + @NoobSaibot BIT = 0, + @VersionShowsAirQuoteActualPlans BIT, + @ObjectFullName NVARCHAR(2000), + @user_perm_sql NVARCHAR(MAX) = N'', + @user_perm_gb_out DECIMAL(10,2), + @common_version DECIMAL(10,2), + @buffer_pool_memory_gb DECIMAL(10,2), + @user_perm_percent DECIMAL(10,2), + @is_tokenstore_big BIT = 0, + @sort NVARCHAR(MAX) = N'', + @sort_filter NVARCHAR(MAX) = N''; + + +IF @SortOrder = 'sp_BlitzIndex' +BEGIN + RAISERROR(N'OUTSTANDING!', 0, 1) WITH NOWAIT; + SET @SortOrder = 'reads'; + SET @NoobSaibot = 1; + +END + + +/* Change duration from seconds to milliseconds */ +IF @DurationFilter IS NOT NULL + BEGIN + RAISERROR(N'Converting Duration Filter to milliseconds', 0, 1) WITH NOWAIT; + SET @DurationFilter_i = CAST((@DurationFilter * 1000.0) AS INT); + END; + +RAISERROR(N'Checking database validity', 0, 1) WITH NOWAIT; +SET @DatabaseName = LTRIM(RTRIM(@DatabaseName)) ; + +IF SERVERPROPERTY('EngineEdition') IN (5, 6) AND DB_NAME() <> @DatabaseName +BEGIN + RAISERROR('You specified a database name other than the current database, but Azure SQL DB does not allow you to change databases. Execute sp_BlitzCache from the database you want to analyze.', 16, 1); + RETURN; +END; +IF (DB_ID(@DatabaseName)) IS NULL AND @DatabaseName <> N'' +BEGIN + RAISERROR('The database you specified does not exist. Please check the name and try again.', 16, 1); + RETURN; +END; +IF (SELECT DATABASEPROPERTYEX(ISNULL(@DatabaseName, 'master'), 'Collation')) IS NULL AND SERVERPROPERTY('EngineEdition') NOT IN (5, 6, 8) +BEGIN + RAISERROR('The database you specified is not readable. Please check the name and try again. Better yet, check your server.', 16, 1); + RETURN; +END; + +SELECT @MinMemoryPerQuery = CONVERT(INT, c.value) FROM sys.configurations AS c WHERE c.name = 'min memory per query (KB)'; + +SET @SortOrder = REPLACE(REPLACE(@SortOrder, 'average', 'avg'), '.', ''); + +SET @SortOrder = CASE + WHEN @SortOrder IN ('executions per minute','execution per minute','executions / minute','execution / minute','xpm') THEN 'avg executions' + WHEN @SortOrder IN ('recent compilations','recent compilation','compile') THEN 'compiles' + WHEN @SortOrder IN ('read') THEN 'reads' + WHEN @SortOrder IN ('avg read') THEN 'avg reads' + WHEN @SortOrder IN ('write') THEN 'writes' + WHEN @SortOrder IN ('avg write') THEN 'avg writes' + WHEN @SortOrder IN ('memory grants') THEN 'memory grant' + WHEN @SortOrder IN ('avg memory grants') THEN 'avg memory grant' + WHEN @SortOrder IN ('unused grants','unused memory', 'unused memory grant', 'unused memory grants') THEN 'unused grant' + WHEN @SortOrder IN ('spill') THEN 'spills' + WHEN @SortOrder IN ('avg spill') THEN 'avg spills' + WHEN @SortOrder IN ('execution') THEN 'executions' + ELSE @SortOrder END + +RAISERROR(N'Checking sort order', 0, 1) WITH NOWAIT; +IF @SortOrder NOT IN ('cpu', 'avg cpu', 'reads', 'avg reads', 'writes', 'avg writes', + 'duration', 'avg duration', 'executions', 'avg executions', + 'compiles', 'memory grant', 'avg memory grant', 'unused grant', + 'spills', 'avg spills', 'all', 'all avg', 'sp_BlitzIndex', + 'query hash') + BEGIN + RAISERROR(N'Invalid sort order chosen, reverting to cpu', 16, 1) WITH NOWAIT; + SET @SortOrder = 'cpu'; + END; + +SET @QueryFilter = LOWER(@QueryFilter); + +IF LEFT(@QueryFilter, 3) NOT IN ('all', 'sta', 'pro', 'fun') + BEGIN + RAISERROR(N'Invalid query filter chosen. Reverting to all.', 0, 1) WITH NOWAIT; + SET @QueryFilter = 'all'; + END; + +IF @SkipAnalysis = 1 + BEGIN + RAISERROR(N'Skip Analysis set to 1, hiding Summary', 0, 1) WITH NOWAIT; + SET @HideSummary = 1; + END; + +DECLARE @AllSortSql NVARCHAR(MAX) = N''; +DECLARE @VersionShowsMemoryGrants BIT; +IF EXISTS(SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('sys.dm_exec_query_stats') AND name = 'max_grant_kb') + SET @VersionShowsMemoryGrants = 1; +ELSE + SET @VersionShowsMemoryGrants = 0; + +DECLARE @VersionShowsSpills BIT; +IF EXISTS(SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('sys.dm_exec_query_stats') AND name = 'max_spills') + SET @VersionShowsSpills = 1; +ELSE + SET @VersionShowsSpills = 0; + +IF EXISTS(SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('sys.dm_exec_query_plan_stats') AND name = 'query_plan') + SET @VersionShowsAirQuoteActualPlans = 1; +ELSE + SET @VersionShowsAirQuoteActualPlans = 0; + +IF @Reanalyze = 1 AND OBJECT_ID('tempdb..##BlitzCacheResults') IS NULL + BEGIN + RAISERROR(N'##BlitzCacheResults does not exist, can''t reanalyze', 0, 1) WITH NOWAIT; + SET @Reanalyze = 0; + END; + +IF @Reanalyze = 0 + BEGIN + RAISERROR(N'Cleaning up old warnings for your SPID', 0, 1) WITH NOWAIT; + DELETE ##BlitzCacheResults + WHERE SPID = @@SPID + OPTION (RECOMPILE) ; + RAISERROR(N'Cleaning up old plans for your SPID', 0, 1) WITH NOWAIT; + DELETE ##BlitzCacheProcs + WHERE SPID = @@SPID + OPTION (RECOMPILE) ; + END; + +IF @Reanalyze = 1 + BEGIN + RAISERROR(N'Reanalyzing current data, skipping to results', 0, 1) WITH NOWAIT; + GOTO Results; + END; + + + + +IF @SortOrder IN ('all', 'all avg') + BEGIN + RAISERROR(N'Checking all sort orders, please be patient', 0, 1) WITH NOWAIT; + GOTO AllSorts; + END; + +RAISERROR(N'Creating temp tables for internal processing', 0, 1) WITH NOWAIT; +IF OBJECT_ID('tempdb..#only_query_hashes') IS NOT NULL + DROP TABLE #only_query_hashes ; + +IF OBJECT_ID('tempdb..#ignore_query_hashes') IS NOT NULL + DROP TABLE #ignore_query_hashes ; + +IF OBJECT_ID('tempdb..#only_sql_handles') IS NOT NULL + DROP TABLE #only_sql_handles ; + +IF OBJECT_ID('tempdb..#ignore_sql_handles') IS NOT NULL + DROP TABLE #ignore_sql_handles ; + +IF OBJECT_ID('tempdb..#p') IS NOT NULL + DROP TABLE #p; + +IF OBJECT_ID ('tempdb..#checkversion') IS NOT NULL + DROP TABLE #checkversion; + +IF OBJECT_ID ('tempdb..#configuration') IS NOT NULL + DROP TABLE #configuration; + +IF OBJECT_ID ('tempdb..#stored_proc_info') IS NOT NULL + DROP TABLE #stored_proc_info; + +IF OBJECT_ID ('tempdb..#plan_creation') IS NOT NULL + DROP TABLE #plan_creation; + +IF OBJECT_ID ('tempdb..#est_rows') IS NOT NULL + DROP TABLE #est_rows; + +IF OBJECT_ID ('tempdb..#plan_cost') IS NOT NULL + DROP TABLE #plan_cost; + +IF OBJECT_ID ('tempdb..#proc_costs') IS NOT NULL + DROP TABLE #proc_costs; + +IF OBJECT_ID ('tempdb..#stats_agg') IS NOT NULL + DROP TABLE #stats_agg; + +IF OBJECT_ID ('tempdb..#trace_flags') IS NOT NULL + DROP TABLE #trace_flags; + +IF OBJECT_ID('tempdb..#variable_info') IS NOT NULL + DROP TABLE #variable_info; + +IF OBJECT_ID('tempdb..#conversion_info') IS NOT NULL + DROP TABLE #conversion_info; + +IF OBJECT_ID('tempdb..#missing_index_xml') IS NOT NULL + DROP TABLE #missing_index_xml; + +IF OBJECT_ID('tempdb..#missing_index_schema') IS NOT NULL + DROP TABLE #missing_index_schema; + +IF OBJECT_ID('tempdb..#missing_index_usage') IS NOT NULL + DROP TABLE #missing_index_usage; + +IF OBJECT_ID('tempdb..#missing_index_detail') IS NOT NULL + DROP TABLE #missing_index_detail; + +IF OBJECT_ID('tempdb..#missing_index_pretty') IS NOT NULL + DROP TABLE #missing_index_pretty; + +IF OBJECT_ID('tempdb..#index_spool_ugly') IS NOT NULL + DROP TABLE #index_spool_ugly; + +IF OBJECT_ID('tempdb..#ReadableDBs') IS NOT NULL + DROP TABLE #ReadableDBs; + +IF OBJECT_ID('tempdb..#plan_usage') IS NOT NULL + DROP TABLE #plan_usage; + +CREATE TABLE #only_query_hashes ( + query_hash BINARY(8) +); + +CREATE TABLE #ignore_query_hashes ( + query_hash BINARY(8) +); + +CREATE TABLE #only_sql_handles ( + sql_handle VARBINARY(64) +); + +CREATE TABLE #ignore_sql_handles ( + sql_handle VARBINARY(64) +); + +CREATE TABLE #p ( + SqlHandle VARBINARY(64), + TotalCPU BIGINT, + TotalDuration BIGINT, + TotalReads BIGINT, + TotalWrites BIGINT, + ExecutionCount BIGINT +); + +CREATE TABLE #checkversion ( + version NVARCHAR(128), + common_version AS SUBSTRING(version, 1, CHARINDEX('.', version) + 1 ), + major AS PARSENAME(CONVERT(VARCHAR(32), version), 4), + minor AS PARSENAME(CONVERT(VARCHAR(32), version), 3), + build AS PARSENAME(CONVERT(VARCHAR(32), version), 2), + revision AS PARSENAME(CONVERT(VARCHAR(32), version), 1) +); + +CREATE TABLE #configuration ( + parameter_name VARCHAR(100), + value DECIMAL(38,0) +); + +CREATE TABLE #plan_creation +( + percent_24 DECIMAL(5, 2), + percent_4 DECIMAL(5, 2), + percent_1 DECIMAL(5, 2), + total_plans INT, + SPID INT +); + +CREATE TABLE #est_rows +( + QueryHash BINARY(8), + estimated_rows FLOAT +); + +CREATE TABLE #plan_cost +( + QueryPlanCost FLOAT, + SqlHandle VARBINARY(64), + PlanHandle VARBINARY(64), + QueryHash BINARY(8), + QueryPlanHash BINARY(8) +); + +CREATE TABLE #proc_costs +( + PlanTotalQuery FLOAT, + PlanHandle VARBINARY(64), + SqlHandle VARBINARY(64) +); + +CREATE TABLE #stats_agg +( + SqlHandle VARBINARY(64), + LastUpdate DATETIME2(7), + ModificationCount BIGINT, + SamplingPercent FLOAT, + [Statistics] NVARCHAR(258), + [Table] NVARCHAR(258), + [Schema] NVARCHAR(258), + [Database] NVARCHAR(258), +); + +CREATE TABLE #trace_flags +( + SqlHandle VARBINARY(64), + QueryHash BINARY(8), + global_trace_flags VARCHAR(1000), + session_trace_flags VARCHAR(1000) +); + +CREATE TABLE #stored_proc_info +( + SPID INT, + SqlHandle VARBINARY(64), + QueryHash BINARY(8), + variable_name NVARCHAR(258), + variable_datatype NVARCHAR(258), + converted_column_name NVARCHAR(258), + compile_time_value NVARCHAR(258), + proc_name NVARCHAR(1000), + column_name NVARCHAR(4000), + converted_to NVARCHAR(258), + set_options NVARCHAR(1000) +); + +CREATE TABLE #variable_info +( + SPID INT, + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + proc_name NVARCHAR(1000), + variable_name NVARCHAR(258), + variable_datatype NVARCHAR(258), + compile_time_value NVARCHAR(258) +); + +CREATE TABLE #conversion_info +( + SPID INT, + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + proc_name NVARCHAR(258), + expression NVARCHAR(4000), + at_charindex AS CHARINDEX('@', expression), + bracket_charindex AS CHARINDEX(']', expression, CHARINDEX('@', expression)) - CHARINDEX('@', expression), + comma_charindex AS CHARINDEX(',', expression) + 1, + second_comma_charindex AS + CHARINDEX(',', expression, CHARINDEX(',', expression) + 1) - CHARINDEX(',', expression) - 1, + equal_charindex AS CHARINDEX('=', expression) + 1, + paren_charindex AS CHARINDEX('(', expression) + 1, + comma_paren_charindex AS + CHARINDEX(',', expression, CHARINDEX('(', expression) + 1) - CHARINDEX('(', expression) - 1, + convert_implicit_charindex AS CHARINDEX('=CONVERT_IMPLICIT', expression) +); + + +CREATE TABLE #missing_index_xml +( + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + impact FLOAT, + index_xml XML +); + + +CREATE TABLE #missing_index_schema +( + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + index_xml XML +); + + +CREATE TABLE #missing_index_usage +( + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + usage NVARCHAR(128), + index_xml XML +); + + +CREATE TABLE #missing_index_detail +( + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + usage NVARCHAR(128), + column_name NVARCHAR(128) +); + + +CREATE TABLE #missing_index_pretty +( + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + equality NVARCHAR(MAX), + inequality NVARCHAR(MAX), + [include] NVARCHAR(MAX), + executions NVARCHAR(128), + query_cost NVARCHAR(128), + creation_hours NVARCHAR(128), + is_spool BIT, + details AS N'/* ' + + CHAR(10) + + CASE is_spool + WHEN 0 + THEN N'The Query Processor estimates that implementing the ' + ELSE N'We estimate that implementing the ' + END + + N'following index could improve query cost (' + query_cost + N')' + + CHAR(10) + + N'by ' + + CONVERT(NVARCHAR(30), impact) + + N'% for ' + executions + N' executions of the query' + + N' over the last ' + + CASE WHEN creation_hours < 24 + THEN creation_hours + N' hours.' + WHEN creation_hours = 24 + THEN ' 1 day.' + WHEN creation_hours > 24 + THEN (CONVERT(NVARCHAR(128), creation_hours / 24)) + N' days.' + ELSE N'' + END + + CHAR(10) + + N'*/' + + CHAR(10) + CHAR(13) + + N'/* ' + + CHAR(10) + + N'USE ' + + database_name + + CHAR(10) + + N'GO' + + CHAR(10) + CHAR(13) + + N'CREATE NONCLUSTERED INDEX ix_' + + ISNULL(REPLACE(REPLACE(REPLACE(equality,'[', ''), ']', ''), ', ', '_'), '') + + ISNULL(REPLACE(REPLACE(REPLACE(inequality,'[', ''), ']', ''), ', ', '_'), '') + + CASE WHEN [include] IS NOT NULL THEN + N'_Includes' ELSE N'' END + + CHAR(10) + + N' ON ' + + schema_name + + N'.' + + table_name + + N' (' + + + CASE WHEN equality IS NOT NULL + THEN equality + + CASE WHEN inequality IS NOT NULL + THEN N', ' + inequality + ELSE N'' + END + ELSE inequality + END + + N')' + + CHAR(10) + + CASE WHEN include IS NOT NULL + THEN N'INCLUDE (' + include + N') WITH (FILLFACTOR=100, ONLINE=?, SORT_IN_TEMPDB=?, DATA_COMPRESSION=?);' + ELSE N' WITH (FILLFACTOR=100, ONLINE=?, SORT_IN_TEMPDB=?, DATA_COMPRESSION=?);' + END + + CHAR(10) + + N'GO' + + CHAR(10) + + N'*/' +); + + +CREATE TABLE #index_spool_ugly +( + QueryHash BINARY(8), + SqlHandle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + equality NVARCHAR(MAX), + inequality NVARCHAR(MAX), + [include] NVARCHAR(MAX), + executions NVARCHAR(128), + query_cost NVARCHAR(128), + creation_hours NVARCHAR(128) +); + + +CREATE TABLE #ReadableDBs +( +database_id INT +); + + +CREATE TABLE #plan_usage +( + duplicate_plan_hashes BIGINT NULL, + percent_duplicate DECIMAL(9, 2) NULL, + single_use_plan_count BIGINT NULL, + percent_single DECIMAL(9, 2) NULL, + total_plans BIGINT NULL, + spid INT +); + + +IF EXISTS (SELECT * FROM sys.all_objects o WHERE o.name = 'dm_hadr_database_replica_states') +BEGIN + RAISERROR('Checking for Read intent databases to exclude',0,0) WITH NOWAIT; + + EXEC('INSERT INTO #ReadableDBs (database_id) SELECT DBs.database_id FROM sys.databases DBs INNER JOIN sys.availability_replicas Replicas ON DBs.replica_id = Replicas.replica_id WHERE replica_server_name NOT IN (SELECT DISTINCT primary_replica FROM sys.dm_hadr_availability_group_states States) AND Replicas.secondary_role_allow_connections_desc = ''READ_ONLY'' AND replica_server_name = @@SERVERNAME OPTION (RECOMPILE);'); + EXEC('INSERT INTO #ReadableDBs VALUES (32767) ;'); -- Exclude internal resource database as well +END + +RAISERROR(N'Checking plan cache age', 0, 1) WITH NOWAIT; +WITH x AS ( +SELECT SUM(CASE WHEN DATEDIFF(HOUR, deqs.creation_time, SYSDATETIME()) <= 24 THEN 1 ELSE 0 END) AS [plans_24], + SUM(CASE WHEN DATEDIFF(HOUR, deqs.creation_time, SYSDATETIME()) <= 4 THEN 1 ELSE 0 END) AS [plans_4], + SUM(CASE WHEN DATEDIFF(HOUR, deqs.creation_time, SYSDATETIME()) <= 1 THEN 1 ELSE 0 END) AS [plans_1], + COUNT(deqs.creation_time) AS [total_plans] +FROM sys.dm_exec_query_stats AS deqs +) +INSERT INTO #plan_creation ( percent_24, percent_4, percent_1, total_plans, SPID ) +SELECT CONVERT(DECIMAL(5,2), NULLIF(x.plans_24, 0) / (1. * NULLIF(x.total_plans, 0))) * 100 AS [percent_24], + CONVERT(DECIMAL(5,2), NULLIF(x.plans_4 , 0) / (1. * NULLIF(x.total_plans, 0))) * 100 AS [percent_4], + CONVERT(DECIMAL(5,2), NULLIF(x.plans_1 , 0) / (1. * NULLIF(x.total_plans, 0))) * 100 AS [percent_1], + x.total_plans, + @@SPID AS SPID +FROM x +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for single use plans and plans with many queries', 0, 1) WITH NOWAIT; +WITH total_plans AS +( + SELECT + COUNT_BIG(deqs.query_plan_hash) AS total_plans + FROM sys.dm_exec_query_stats AS deqs +), + many_plans AS +( + SELECT + SUM(x.duplicate_plan_hashes) AS duplicate_plan_hashes + FROM + ( + SELECT + COUNT_BIG(qs.query_plan_hash) AS duplicate_plan_hashes + FROM sys.dm_exec_query_stats qs + LEFT JOIN sys.dm_exec_procedure_stats ps ON qs.plan_handle = ps.plan_handle + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) pa + WHERE pa.attribute = N'dbid' + AND pa.value <> 32767 /*Omit Resource database-based queries, we're not going to "fix" them no matter what. Addresses #3314*/ + AND qs.query_plan_hash <> 0x0000000000000000 + GROUP BY + /* qs.query_plan_hash, BGO 20210524 commenting this out to fix #2909 */ + qs.query_hash, + ps.object_id, + pa.value + HAVING COUNT_BIG(qs.query_plan_hash) > 5 + ) AS x +), + single_use_plans AS +( + SELECT + COUNT_BIG(*) AS single_use_plan_count + FROM sys.dm_exec_cached_plans AS cp + WHERE cp.usecounts = 1 + AND cp.objtype = N'Adhoc' + AND EXISTS + ( + SELECT + 1/0 + FROM sys.configurations AS c + WHERE c.name = N'optimize for ad hoc workloads' + AND c.value_in_use = 0 + ) + HAVING COUNT_BIG(*) > 1 +) +INSERT + #plan_usage +( + duplicate_plan_hashes, + percent_duplicate, + single_use_plan_count, + percent_single, + total_plans, + spid +) +SELECT + m.duplicate_plan_hashes, + CONVERT + ( + decimal(5,2), + m.duplicate_plan_hashes + / (1. * NULLIF(t.total_plans, 0)) + ) * 100. AS percent_duplicate, + s.single_use_plan_count, + CONVERT + ( + decimal(5,2), + s.single_use_plan_count + / (1. * NULLIF(t.total_plans, 0)) + ) * 100. AS percent_single, + t.total_plans, + @@SPID +FROM many_plans AS m +CROSS JOIN single_use_plans AS s +CROSS JOIN total_plans AS t; + + +/* +Erik Darling: + Quoting this out to see if the above query fixes the issue + 2021-05-17, Issue #2909 + +UPDATE #plan_usage + SET percent_duplicate = CASE WHEN percent_duplicate > 100 THEN 100 ELSE percent_duplicate END, + percent_single = CASE WHEN percent_duplicate > 100 THEN 100 ELSE percent_duplicate END; +*/ + +SET @OnlySqlHandles = LTRIM(RTRIM(@OnlySqlHandles)) ; +SET @OnlyQueryHashes = LTRIM(RTRIM(@OnlyQueryHashes)) ; +SET @IgnoreQueryHashes = LTRIM(RTRIM(@IgnoreQueryHashes)) ; + +DECLARE @individual VARCHAR(100) ; + +IF (@OnlySqlHandles IS NOT NULL AND @IgnoreSqlHandles IS NOT NULL) +BEGIN +RAISERROR('You shouldn''t need to ignore and filter on SqlHandle at the same time.', 0, 1) WITH NOWAIT; +RETURN; +END; + +IF (@StoredProcName IS NOT NULL AND (@OnlySqlHandles IS NOT NULL OR @IgnoreSqlHandles IS NOT NULL)) +BEGIN +RAISERROR('You can''t filter on stored procedure name and SQL Handle.', 0, 1) WITH NOWAIT; +RETURN; +END; + +IF @OnlySqlHandles IS NOT NULL + AND LEN(@OnlySqlHandles) > 0 +BEGIN + RAISERROR(N'Processing SQL Handles', 0, 1) WITH NOWAIT; + SET @individual = ''; + + WHILE LEN(@OnlySqlHandles) > 0 + BEGIN + IF PATINDEX('%,%', @OnlySqlHandles) > 0 + BEGIN + SET @individual = SUBSTRING(@OnlySqlHandles, 0, PATINDEX('%,%',@OnlySqlHandles)) ; + + INSERT INTO #only_sql_handles + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + --SELECT CAST(SUBSTRING(@individual, 1, 2) AS BINARY(8)); + + SET @OnlySqlHandles = SUBSTRING(@OnlySqlHandles, LEN(@individual + ',') + 1, LEN(@OnlySqlHandles)) ; + END; + ELSE + BEGIN + SET @individual = @OnlySqlHandles; + SET @OnlySqlHandles = NULL; + + INSERT INTO #only_sql_handles + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + --SELECT CAST(SUBSTRING(@individual, 1, 2) AS VARBINARY(MAX)) ; + END; + END; +END; + +IF @IgnoreSqlHandles IS NOT NULL + AND LEN(@IgnoreSqlHandles) > 0 +BEGIN + RAISERROR(N'Processing SQL Handles To Ignore', 0, 1) WITH NOWAIT; + SET @individual = ''; + + WHILE LEN(@IgnoreSqlHandles) > 0 + BEGIN + IF PATINDEX('%,%', @IgnoreSqlHandles) > 0 + BEGIN + SET @individual = SUBSTRING(@IgnoreSqlHandles, 0, PATINDEX('%,%',@IgnoreSqlHandles)) ; + + INSERT INTO #ignore_sql_handles + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + --SELECT CAST(SUBSTRING(@individual, 1, 2) AS BINARY(8)); + + SET @IgnoreSqlHandles = SUBSTRING(@IgnoreSqlHandles, LEN(@individual + ',') + 1, LEN(@IgnoreSqlHandles)) ; + END; + ELSE + BEGIN + SET @individual = @IgnoreSqlHandles; + SET @IgnoreSqlHandles = NULL; + + INSERT INTO #ignore_sql_handles + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + --SELECT CAST(SUBSTRING(@individual, 1, 2) AS VARBINARY(MAX)) ; + END; + END; +END; + +IF @StoredProcName IS NOT NULL AND @StoredProcName <> N'' + +BEGIN + RAISERROR(N'Setting up filter for stored procedure name', 0, 1) WITH NOWAIT; + + DECLARE @function_search_sql NVARCHAR(MAX) = N'' + + INSERT #only_sql_handles + ( sql_handle ) + SELECT ISNULL(deps.sql_handle, CONVERT(VARBINARY(64),'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) + FROM sys.dm_exec_procedure_stats AS deps + WHERE OBJECT_NAME(deps.object_id, deps.database_id) = @StoredProcName + + UNION ALL + + SELECT ISNULL(dets.sql_handle, CONVERT(VARBINARY(64),'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')) + FROM sys.dm_exec_trigger_stats AS dets + WHERE OBJECT_NAME(dets.object_id, dets.database_id) = @StoredProcName + OPTION (RECOMPILE); + + IF EXISTS (SELECT 1/0 FROM sys.all_objects AS o WHERE o.name = 'dm_exec_function_stats') + BEGIN + SET @function_search_sql = @function_search_sql + N' + SELECT ISNULL(defs.sql_handle, CONVERT(VARBINARY(64),''0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'')) + FROM sys.dm_exec_function_stats AS defs + WHERE OBJECT_NAME(defs.object_id, defs.database_id) = @i_StoredProcName + OPTION (RECOMPILE); + ' + INSERT #only_sql_handles ( sql_handle ) + EXEC sys.sp_executesql @function_search_sql, N'@i_StoredProcName NVARCHAR(128)', @StoredProcName + END + + IF (SELECT COUNT(*) FROM #only_sql_handles) = 0 + BEGIN + RAISERROR(N'No information for that stored procedure was found.', 0, 1) WITH NOWAIT; + RETURN; + END; + +END; + + + +IF ((@OnlyQueryHashes IS NOT NULL AND LEN(@OnlyQueryHashes) > 0) + OR (@IgnoreQueryHashes IS NOT NULL AND LEN(@IgnoreQueryHashes) > 0)) + AND LEFT(@QueryFilter, 3) IN ('pro', 'fun') +BEGIN + RAISERROR('You cannot limit by query hash and filter by stored procedure', 16, 1); + RETURN; +END; + +/* If the user is attempting to limit by query hash, set up the + #only_query_hashes temp table. This will be used to narrow down + results. + + Just a reminder: Using @OnlyQueryHashes will ignore stored + procedures and triggers. + */ +IF @OnlyQueryHashes IS NOT NULL + AND LEN(@OnlyQueryHashes) > 0 +BEGIN + RAISERROR(N'Setting up filter for Query Hashes', 0, 1) WITH NOWAIT; + SET @individual = ''; + + WHILE LEN(@OnlyQueryHashes) > 0 + BEGIN + IF PATINDEX('%,%', @OnlyQueryHashes) > 0 + BEGIN + SET @individual = SUBSTRING(@OnlyQueryHashes, 0, PATINDEX('%,%',@OnlyQueryHashes)) ; + + INSERT INTO #only_query_hashes + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + --SELECT CAST(SUBSTRING(@individual, 1, 2) AS BINARY(8)); + + SET @OnlyQueryHashes = SUBSTRING(@OnlyQueryHashes, LEN(@individual + ',') + 1, LEN(@OnlyQueryHashes)) ; + END; + ELSE + BEGIN + SET @individual = @OnlyQueryHashes; + SET @OnlyQueryHashes = NULL; + + INSERT INTO #only_query_hashes + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + --SELECT CAST(SUBSTRING(@individual, 1, 2) AS VARBINARY(MAX)) ; + END; + END; +END; + +/* If the user is setting up a list of query hashes to ignore, those + values will be inserted into #ignore_query_hashes. This is used to + exclude values from query results. + + Just a reminder: Using @IgnoreQueryHashes will ignore stored + procedures and triggers. + */ +IF @IgnoreQueryHashes IS NOT NULL + AND LEN(@IgnoreQueryHashes) > 0 +BEGIN + RAISERROR(N'Setting up filter to ignore query hashes', 0, 1) WITH NOWAIT; + SET @individual = '' ; + + WHILE LEN(@IgnoreQueryHashes) > 0 + BEGIN + IF PATINDEX('%,%', @IgnoreQueryHashes) > 0 + BEGIN + SET @individual = SUBSTRING(@IgnoreQueryHashes, 0, PATINDEX('%,%',@IgnoreQueryHashes)) ; + + INSERT INTO #ignore_query_hashes + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + + SET @IgnoreQueryHashes = SUBSTRING(@IgnoreQueryHashes, LEN(@individual + ',') + 1, LEN(@IgnoreQueryHashes)) ; + END; + ELSE + BEGIN + SET @individual = @IgnoreQueryHashes ; + SET @IgnoreQueryHashes = NULL ; + + INSERT INTO #ignore_query_hashes + SELECT CAST('' AS XML).value('xs:hexBinary( substring(sql:variable("@individual"), sql:column("t.pos")) )', 'varbinary(max)') + FROM (SELECT CASE SUBSTRING(@individual, 1, 2) WHEN '0x' THEN 3 ELSE 0 END) AS t(pos) + OPTION (RECOMPILE) ; + END; + END; +END; + +IF @ConfigurationDatabaseName IS NOT NULL +BEGIN + RAISERROR(N'Reading values from Configuration Database', 0, 1) WITH NOWAIT; + DECLARE @config_sql NVARCHAR(MAX) = N'INSERT INTO #configuration SELECT parameter_name, value FROM ' + + QUOTENAME(@ConfigurationDatabaseName) + + '.' + QUOTENAME(@ConfigurationSchemaName) + + '.' + QUOTENAME(@ConfigurationTableName) + + ' ; ' ; + EXEC(@config_sql); +END; + +RAISERROR(N'Setting up variables', 0, 1) WITH NOWAIT; +DECLARE @sql NVARCHAR(MAX) = N'', + @insert_list NVARCHAR(MAX) = N'', + @plans_triggers_select_list NVARCHAR(MAX) = N'', + @body NVARCHAR(MAX) = N'', + @body_where NVARCHAR(MAX) = N'WHERE 1 = 1 ' + @nl, + @body_order NVARCHAR(MAX) = N'ORDER BY #sortable# DESC OPTION (RECOMPILE) ', + + @q NVARCHAR(1) = N'''', + @pv VARCHAR(20), + @pos TINYINT, + @v DECIMAL(6,2), + @build INT; + + +RAISERROR (N'Determining SQL Server version.',0,1) WITH NOWAIT; + +INSERT INTO #checkversion (version) +SELECT CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)) +OPTION (RECOMPILE); + + +SELECT @v = common_version , + @build = build +FROM #checkversion +OPTION (RECOMPILE); + +IF (@SortOrder IN ('memory grant', 'avg memory grant')) AND @VersionShowsMemoryGrants = 0 +BEGIN + RAISERROR('Your version of SQL does not support sorting by memory grant or average memory grant. Please use another sort order.', 16, 1); + RETURN; +END; + +IF (@SortOrder IN ('spills', 'avg spills') AND @VersionShowsSpills = 0) +BEGIN + RAISERROR('Your version of SQL does not support sorting by spills. Please use another sort order.', 16, 1); + RETURN; +END; + +IF ((LEFT(@QueryFilter, 3) = 'fun') AND (@v < 13)) +BEGIN + RAISERROR('Your version of SQL does not support filtering by functions. Please use another filter.', 16, 1); + RETURN; +END; + +RAISERROR (N'Creating dynamic SQL based on SQL Server version.',0,1) WITH NOWAIT; + +SET @insert_list += N' +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; +INSERT INTO ##BlitzCacheProcs (SPID, QueryType, DatabaseName, AverageCPU, TotalCPU, AverageCPUPerMinute, PercentCPUByType, PercentDurationByType, + PercentReadsByType, PercentExecutionsByType, AverageDuration, TotalDuration, AverageReads, TotalReads, ExecutionCount, + ExecutionsPerMinute, TotalWrites, AverageWrites, PercentWritesByType, WritesPerMinute, PlanCreationTime, + LastExecutionTime, LastCompletionTime, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, + LastReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, + QueryText, QueryPlan, TotalWorkerTimeForType, TotalElapsedTimeForType, TotalReadsForType, + TotalExecutionCountForType, TotalWritesForType, SqlHandle, PlanHandle, QueryHash, QueryPlanHash, + min_worker_time, max_worker_time, is_parallel, min_elapsed_time, max_elapsed_time, age_minutes, age_minutes_lifetime, Pattern) ' ; + +SET @body += N' +FROM (SELECT TOP (@Top) x.*, xpa.*, + CAST((CASE WHEN DATEDIFF(mi, cached_time, GETDATE()) > 0 AND execution_count > 1 + THEN DATEDIFF(mi, cached_time, GETDATE()) + ELSE NULL END) as MONEY) as age_minutes, + CAST((CASE WHEN DATEDIFF(mi, cached_time, last_execution_time) > 0 AND execution_count > 1 + THEN DATEDIFF(mi, cached_time, last_execution_time) + ELSE Null END) as MONEY) as age_minutes_lifetime + FROM sys.#view# x + CROSS APPLY (SELECT * FROM sys.dm_exec_plan_attributes(x.plan_handle) AS ixpa + WHERE ixpa.attribute = ''dbid'') AS xpa ' + @nl ; + + +IF @VersionShowsAirQuoteActualPlans = 1 + BEGIN + SET @body += N' CROSS APPLY sys.dm_exec_query_plan_stats(x.plan_handle) AS deqps ' + @nl ; + END + +SET @body += N' WHERE 1 = 1 ' + @nl ; + + IF EXISTS (SELECT * FROM sys.all_objects o WHERE o.name = 'dm_hadr_database_replica_states') + BEGIN + RAISERROR(N'Ignoring readable secondaries databases by default', 0, 1) WITH NOWAIT; + SET @body += N' AND CAST(xpa.value AS INT) NOT IN (SELECT database_id FROM #ReadableDBs)' + @nl ; + END + +IF @IgnoreSystemDBs = 1 + BEGIN + RAISERROR(N'Ignoring system databases by default', 0, 1) WITH NOWAIT; + SET @body += N' AND COALESCE(LOWER(DB_NAME(CAST(xpa.value AS INT))), '''') NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''32767'', ''dbmaintenance'', ''dbadmin'', ''dbatools'') AND COALESCE(DB_NAME(CAST(xpa.value AS INT)), '''') NOT IN (SELECT name FROM sys.databases WHERE is_distributor = 1)' + @nl ; + END; + +IF @DatabaseName IS NOT NULL OR @DatabaseName <> N'' + BEGIN + RAISERROR(N'Filtering database name chosen', 0, 1) WITH NOWAIT; + SET @body += N' AND CAST(xpa.value AS BIGINT) = DB_ID(N' + + QUOTENAME(@DatabaseName, N'''') + + N') ' + @nl; + END; + +IF (SELECT COUNT(*) FROM #only_sql_handles) > 0 +BEGIN + RAISERROR(N'Including only chosen SQL Handles', 0, 1) WITH NOWAIT; + SET @body += N' AND EXISTS(SELECT 1/0 FROM #only_sql_handles q WHERE q.sql_handle = x.sql_handle) ' + @nl ; +END; + +IF (SELECT COUNT(*) FROM #ignore_sql_handles) > 0 +BEGIN + RAISERROR(N'Including only chosen SQL Handles', 0, 1) WITH NOWAIT; + SET @body += N' AND NOT EXISTS(SELECT 1/0 FROM #ignore_sql_handles q WHERE q.sql_handle = x.sql_handle) ' + @nl ; +END; + +IF (SELECT COUNT(*) FROM #only_query_hashes) > 0 + AND (SELECT COUNT(*) FROM #ignore_query_hashes) = 0 + AND (SELECT COUNT(*) FROM #only_sql_handles) = 0 + AND (SELECT COUNT(*) FROM #ignore_sql_handles) = 0 +BEGIN + RAISERROR(N'Including only chosen Query Hashes', 0, 1) WITH NOWAIT; + SET @body += N' AND EXISTS(SELECT 1/0 FROM #only_query_hashes q WHERE q.query_hash = x.query_hash) ' + @nl ; +END; + +/* filtering for query hashes */ +IF (SELECT COUNT(*) FROM #ignore_query_hashes) > 0 + AND (SELECT COUNT(*) FROM #only_query_hashes) = 0 +BEGIN + RAISERROR(N'Excluding chosen Query Hashes', 0, 1) WITH NOWAIT; + SET @body += N' AND NOT EXISTS(SELECT 1/0 FROM #ignore_query_hashes iq WHERE iq.query_hash = x.query_hash) ' + @nl ; +END; +/* end filtering for query hashes */ + + +IF @DurationFilter IS NOT NULL + BEGIN + RAISERROR(N'Setting duration filter', 0, 1) WITH NOWAIT; + SET @body += N' AND (total_elapsed_time / 1000.0) / execution_count > @min_duration ' + @nl ; + END; + +IF @MinutesBack IS NOT NULL + BEGIN + RAISERROR(N'Setting minutes back filter', 0, 1) WITH NOWAIT; + SET @body += N' AND DATEADD(SECOND, (x.last_elapsed_time / 1000000.), x.last_execution_time) >= DATEADD(MINUTE, @min_back, GETDATE()) ' + @nl ; + END; + +IF @SlowlySearchPlansFor IS NOT NULL + BEGIN + RAISERROR(N'Setting string search for @SlowlySearchPlansFor, so remember, this is gonna be slow', 0, 1) WITH NOWAIT; + SET @SlowlySearchPlansFor = REPLACE((REPLACE((REPLACE((REPLACE(@SlowlySearchPlansFor, N'[', N'_')), N']', N'_')), N'^', N'_')), N'''', N''''''); + SET @body_where += N' AND CAST(qp.query_plan AS NVARCHAR(MAX)) LIKE N''%' + @SlowlySearchPlansFor + N'%'' ' + @nl; + END + + +/* Apply the sort order here to only grab relevant plans. + This should make it faster to process since we'll be pulling back fewer + plans for processing. + */ +RAISERROR(N'Applying chosen sort order', 0, 1) WITH NOWAIT; +SELECT @body += N' ORDER BY ' + + CASE @SortOrder WHEN N'cpu' THEN N'total_worker_time' + WHEN N'reads' THEN N'total_logical_reads' + WHEN N'writes' THEN N'total_logical_writes' + WHEN N'duration' THEN N'total_elapsed_time' + WHEN N'executions' THEN N'execution_count' + WHEN N'compiles' THEN N'cached_time' + WHEN N'memory grant' THEN N'max_grant_kb' + WHEN N'unused grant' THEN N'max_grant_kb - max_used_grant_kb' + WHEN N'spills' THEN N'max_spills' + /* And now the averages */ + WHEN N'avg cpu' THEN N'total_worker_time / execution_count' + WHEN N'avg reads' THEN N'total_logical_reads / execution_count' + WHEN N'avg writes' THEN N'total_logical_writes / execution_count' + WHEN N'avg duration' THEN N'total_elapsed_time / execution_count' + WHEN N'avg memory grant' THEN N'CASE WHEN max_grant_kb = 0 THEN 0 ELSE max_grant_kb / execution_count END' + WHEN N'avg spills' THEN N'CASE WHEN total_spills = 0 THEN 0 ELSE total_spills / execution_count END' + WHEN N'avg executions' THEN 'CASE WHEN execution_count = 0 THEN 0 + WHEN COALESCE(CAST((CASE WHEN DATEDIFF(mi, cached_time, GETDATE()) > 0 AND execution_count > 1 + THEN DATEDIFF(mi, cached_time, GETDATE()) + ELSE NULL END) as MONEY), CAST((CASE WHEN DATEDIFF(mi, cached_time, last_execution_time) > 0 AND execution_count > 1 + THEN DATEDIFF(mi, cached_time, last_execution_time) + ELSE Null END) as MONEY), 0) = 0 THEN 0 + ELSE CAST((1.00 * execution_count / COALESCE(CAST((CASE WHEN DATEDIFF(mi, cached_time, GETDATE()) > 0 AND execution_count > 1 + THEN DATEDIFF(mi, cached_time, GETDATE()) + ELSE NULL END) as MONEY), CAST((CASE WHEN DATEDIFF(mi, cached_time, last_execution_time) > 0 AND execution_count > 1 + THEN DATEDIFF(mi, cached_time, last_execution_time) + ELSE Null END) as MONEY))) AS money) + END ' + END + N' DESC ' + @nl ; + + + +SET @body += N') AS qs + CROSS JOIN(SELECT SUM(execution_count) AS t_TotalExecs, + SUM(CAST(total_elapsed_time AS BIGINT) / 1000.0) AS t_TotalElapsed, + SUM(CAST(total_worker_time AS BIGINT) / 1000.0) AS t_TotalWorker, + SUM(CAST(total_logical_reads AS DECIMAL(30))) AS t_TotalReads, + SUM(CAST(total_logical_writes AS DECIMAL(30))) AS t_TotalWrites + FROM sys.#view#) AS t + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS pa + CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st + CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp ' + @nl ; + +IF @VersionShowsAirQuoteActualPlans = 1 + BEGIN + SET @body += N' CROSS APPLY sys.dm_exec_query_plan_stats(qs.plan_handle) AS deqps ' + @nl ; + END + +SET @body_where += N' AND pa.attribute = ' + QUOTENAME('dbid', @q ) + @nl ; + + +IF @NoobSaibot = 1 +BEGIN + SET @body_where += N' AND qp.query_plan.exist(''declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";//p:StmtSimple//p:MissingIndex'') = 1' + @nl ; +END + +SET @plans_triggers_select_list += N' +SELECT TOP (@Top) + @@SPID , + ''Procedure or Function: '' + + QUOTENAME(COALESCE(OBJECT_SCHEMA_NAME(qs.object_id, qs.database_id),'''')) + + ''.'' + + QUOTENAME(COALESCE(OBJECT_NAME(qs.object_id, qs.database_id),'''')) AS QueryType, + COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), N''-- N/A --'') AS DatabaseName, + (total_worker_time / 1000.0) / execution_count AS AvgCPU , + (total_worker_time / 1000.0) AS TotalCPU , + CASE WHEN total_worker_time = 0 THEN 0 + WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 + ELSE CAST((total_worker_time / 1000.0) / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time)) AS MONEY) + END AS AverageCPUPerMinute , + CASE WHEN t.t_TotalWorker = 0 THEN 0 + ELSE CAST(ROUND(100.00 * (total_worker_time / 1000.0) / t.t_TotalWorker, 2) AS MONEY) + END AS PercentCPUByType, + CASE WHEN t.t_TotalElapsed = 0 THEN 0 + ELSE CAST(ROUND(100.00 * (total_elapsed_time / 1000.0) / t.t_TotalElapsed, 2) AS MONEY) + END AS PercentDurationByType, + CASE WHEN t.t_TotalReads = 0 THEN 0 + ELSE CAST(ROUND(100.00 * total_logical_reads / t.t_TotalReads, 2) AS MONEY) + END AS PercentReadsByType, + CASE WHEN t.t_TotalExecs = 0 THEN 0 + ELSE CAST(ROUND(100.00 * execution_count / t.t_TotalExecs, 2) AS MONEY) + END AS PercentExecutionsByType, + (total_elapsed_time / 1000.0) / execution_count AS AvgDuration , + (total_elapsed_time / 1000.0) AS TotalDuration , + total_logical_reads / execution_count AS AvgReads , + total_logical_reads AS TotalReads , + execution_count AS ExecutionCount , + CASE WHEN execution_count = 0 THEN 0 + WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 + ELSE CAST((1.00 * execution_count / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time))) AS money) + END AS ExecutionsPerMinute , + total_logical_writes AS TotalWrites , + total_logical_writes / execution_count AS AverageWrites , + CASE WHEN t.t_TotalWrites = 0 THEN 0 + ELSE CAST(ROUND(100.00 * total_logical_writes / t.t_TotalWrites, 2) AS MONEY) + END AS PercentWritesByType, + CASE WHEN total_logical_writes = 0 THEN 0 + WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0) = 0 THEN 0 + ELSE CAST((1.00 * total_logical_writes / COALESCE(age_minutes, DATEDIFF(mi, qs.cached_time, qs.last_execution_time), 0)) AS money) + END AS WritesPerMinute, + qs.cached_time AS PlanCreationTime, + qs.last_execution_time AS LastExecutionTime, + DATEADD(SECOND, (qs.last_elapsed_time / 1000000.), qs.last_execution_time) AS LastCompletionTime, + NULL AS StatementStartOffset, + NULL AS StatementEndOffset, + NULL AS PlanGenerationNum, + NULL AS MinReturnedRows, + NULL AS MaxReturnedRows, + NULL AS AvgReturnedRows, + NULL AS TotalReturnedRows, + NULL AS LastReturnedRows, + NULL AS MinGrantKB, + NULL AS MaxGrantKB, + NULL AS MinUsedGrantKB, + NULL AS MaxUsedGrantKB, + NULL AS PercentMemoryGrantUsed, + NULL AS AvgMaxMemoryGrant,'; + + IF @VersionShowsSpills = 1 + BEGIN + RAISERROR(N'Getting spill information for newer versions of SQL', 0, 1) WITH NOWAIT; + SET @plans_triggers_select_list += N' + min_spills AS MinSpills, + max_spills AS MaxSpills, + total_spills AS TotalSpills, + CAST(ISNULL(NULLIF(( total_spills * 1. ), 0) / NULLIF(execution_count, 0), 0) AS MONEY) AS AvgSpills, '; + END; + ELSE + BEGIN + RAISERROR(N'Substituting NULLs for spill columns in older versions of SQL', 0, 1) WITH NOWAIT; + SET @plans_triggers_select_list += N' + NULL AS MinSpills, + NULL AS MaxSpills, + NULL AS TotalSpills, + NULL AS AvgSpills, ' ; + END; + + SET @plans_triggers_select_list += + N'st.text AS QueryText ,'; + + IF @VersionShowsAirQuoteActualPlans = 1 + BEGIN + SET @plans_triggers_select_list += N' CASE WHEN DATALENGTH(COALESCE(deqps.query_plan,'''')) > DATALENGTH(COALESCE(qp.query_plan,'''')) THEN deqps.query_plan ELSE qp.query_plan END AS QueryPlan, ' + @nl ; + END; + ELSE + BEGIN + SET @plans_triggers_select_list += N' qp.query_plan AS QueryPlan, ' + @nl ; + END; + + SET @plans_triggers_select_list += + N't.t_TotalWorker, + t.t_TotalElapsed, + t.t_TotalReads, + t.t_TotalExecs, + t.t_TotalWrites, + qs.sql_handle AS SqlHandle, + qs.plan_handle AS PlanHandle, + NULL AS QueryHash, + NULL AS QueryPlanHash, + qs.min_worker_time / 1000.0, + qs.max_worker_time / 1000.0, + CASE WHEN qp.query_plan.value(''declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";max(//p:RelOp/@Parallel)'', ''float'') > 0 THEN 1 ELSE 0 END, + qs.min_elapsed_time / 1000.0, + qs.max_elapsed_time / 1000.0, + age_minutes, + age_minutes_lifetime, + @SortOrder '; + + +IF LEFT(@QueryFilter, 3) IN ('all', 'sta') +BEGIN + SET @sql += @insert_list; + + SET @sql += N' + SELECT TOP (@Top) + @@SPID , + ''Statement'' AS QueryType, + COALESCE(DB_NAME(CAST(pa.value AS INT)), N''-- N/A --'') AS DatabaseName, + (total_worker_time / 1000.0) / execution_count AS AvgCPU , + (total_worker_time / 1000.0) AS TotalCPU , + CASE WHEN total_worker_time = 0 THEN 0 + WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.creation_time, qs.last_execution_time), 0) = 0 THEN 0 + ELSE CAST((total_worker_time / 1000.0) / COALESCE(age_minutes, DATEDIFF(mi, qs.creation_time, qs.last_execution_time)) AS MONEY) + END AS AverageCPUPerMinute , + CASE WHEN t.t_TotalWorker = 0 THEN 0 + ELSE CAST(ROUND(100.00 * total_worker_time / t.t_TotalWorker, 2) AS MONEY) + END AS PercentCPUByType, + CASE WHEN t.t_TotalElapsed = 0 THEN 0 + ELSE CAST(ROUND(100.00 * total_elapsed_time / t.t_TotalElapsed, 2) AS MONEY) + END AS PercentDurationByType, + CASE WHEN t.t_TotalReads = 0 THEN 0 + ELSE CAST(ROUND(100.00 * total_logical_reads / t.t_TotalReads, 2) AS MONEY) + END AS PercentReadsByType, + CAST(ROUND(100.00 * execution_count / t.t_TotalExecs, 2) AS MONEY) AS PercentExecutionsByType, + (total_elapsed_time / 1000.0) / execution_count AS AvgDuration , + (total_elapsed_time / 1000.0) AS TotalDuration , + total_logical_reads / execution_count AS AvgReads , + total_logical_reads AS TotalReads , + execution_count AS ExecutionCount , + CASE WHEN execution_count = 0 THEN 0 + WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.creation_time, qs.last_execution_time), 0) = 0 THEN 0 + ELSE CAST((1.00 * execution_count / COALESCE(age_minutes, DATEDIFF(mi, qs.creation_time, qs.last_execution_time))) AS money) + END AS ExecutionsPerMinute , + total_logical_writes AS TotalWrites , + total_logical_writes / execution_count AS AverageWrites , + CASE WHEN t.t_TotalWrites = 0 THEN 0 + ELSE CAST(ROUND(100.00 * total_logical_writes / t.t_TotalWrites, 2) AS MONEY) + END AS PercentWritesByType, + CASE WHEN total_logical_writes = 0 THEN 0 + WHEN COALESCE(age_minutes, DATEDIFF(mi, qs.creation_time, qs.last_execution_time), 0) = 0 THEN 0 + ELSE CAST((1.00 * total_logical_writes / COALESCE(age_minutes, DATEDIFF(mi, qs.creation_time, qs.last_execution_time), 0)) AS money) + END AS WritesPerMinute, + qs.creation_time AS PlanCreationTime, + qs.last_execution_time AS LastExecutionTime, + DATEADD(SECOND, (qs.last_elapsed_time / 1000000.), qs.last_execution_time) AS LastCompletionTime, + qs.statement_start_offset AS StatementStartOffset, + qs.statement_end_offset AS StatementEndOffset, + qs.plan_generation_num AS PlanGenerationNum, '; + + IF (@v >= 11) OR (@v >= 10.5 AND @build >= 2500) + BEGIN + RAISERROR(N'Adding additional info columns for newer versions of SQL', 0, 1) WITH NOWAIT; + SET @sql += N' + qs.min_rows AS MinReturnedRows, + qs.max_rows AS MaxReturnedRows, + CAST(qs.total_rows as MONEY) / execution_count AS AvgReturnedRows, + qs.total_rows AS TotalReturnedRows, + qs.last_rows AS LastReturnedRows, ' ; + END; + ELSE + BEGIN + RAISERROR(N'Substituting NULLs for more info columns in older versions of SQL', 0, 1) WITH NOWAIT; + SET @sql += N' + NULL AS MinReturnedRows, + NULL AS MaxReturnedRows, + NULL AS AvgReturnedRows, + NULL AS TotalReturnedRows, + NULL AS LastReturnedRows, ' ; + END; + + IF @VersionShowsMemoryGrants = 1 + BEGIN + RAISERROR(N'Getting memory grant information for newer versions of SQL', 0, 1) WITH NOWAIT; + SET @sql += N' + min_grant_kb AS MinGrantKB, + max_grant_kb AS MaxGrantKB, + min_used_grant_kb AS MinUsedGrantKB, + max_used_grant_kb AS MaxUsedGrantKB, + CAST(ISNULL(NULLIF(( total_used_grant_kb * 1.00 ), 0) / NULLIF(total_grant_kb, 0), 0) * 100. AS MONEY) AS PercentMemoryGrantUsed, + CAST(ISNULL(NULLIF(( total_grant_kb * 1. ), 0) / NULLIF(execution_count, 0), 0) AS MONEY) AS AvgMaxMemoryGrant, '; + END; + ELSE + BEGIN + RAISERROR(N'Substituting NULLs for memory grant columns in older versions of SQL', 0, 1) WITH NOWAIT; + SET @sql += N' + NULL AS MinGrantKB, + NULL AS MaxGrantKB, + NULL AS MinUsedGrantKB, + NULL AS MaxUsedGrantKB, + NULL AS PercentMemoryGrantUsed, + NULL AS AvgMaxMemoryGrant, ' ; + END; + + IF @VersionShowsSpills = 1 + BEGIN + RAISERROR(N'Getting spill information for newer versions of SQL', 0, 1) WITH NOWAIT; + SET @sql += N' + min_spills AS MinSpills, + max_spills AS MaxSpills, + total_spills AS TotalSpills, + CAST(ISNULL(NULLIF(( total_spills * 1. ), 0) / NULLIF(execution_count, 0), 0) AS MONEY) AS AvgSpills,'; + END; + ELSE + BEGIN + RAISERROR(N'Substituting NULLs for spill columns in older versions of SQL', 0, 1) WITH NOWAIT; + SET @sql += N' + NULL AS MinSpills, + NULL AS MaxSpills, + NULL AS TotalSpills, + NULL AS AvgSpills, ' ; + END; + + SET @sql += N' + SUBSTRING(st.text, ( qs.statement_start_offset / 2 ) + 1, ( ( CASE qs.statement_end_offset + WHEN -1 THEN DATALENGTH(st.text) + ELSE qs.statement_end_offset + END - qs.statement_start_offset ) / 2 ) + 1) AS QueryText , ' + @nl ; + + + IF @VersionShowsAirQuoteActualPlans = 1 + BEGIN + SET @sql += N' CASE WHEN DATALENGTH(COALESCE(deqps.query_plan,'''')) > DATALENGTH(COALESCE(qp.query_plan,'''')) THEN deqps.query_plan ELSE qp.query_plan END AS QueryPlan, ' + @nl ; + END + ELSE + BEGIN + SET @sql += N' query_plan AS QueryPlan, ' + @nl ; + END + + SET @sql += N' + t.t_TotalWorker, + t.t_TotalElapsed, + t.t_TotalReads, + t.t_TotalExecs, + t.t_TotalWrites, + qs.sql_handle AS SqlHandle, + qs.plan_handle AS PlanHandle, + qs.query_hash AS QueryHash, + qs.query_plan_hash AS QueryPlanHash, + qs.min_worker_time / 1000.0, + qs.max_worker_time / 1000.0, + CASE WHEN qp.query_plan.value(''declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";max(//p:RelOp/@Parallel)'', ''float'') > 0 THEN 1 ELSE 0 END, + qs.min_elapsed_time / 1000.0, + qs.max_worker_time / 1000.0, + age_minutes, + age_minutes_lifetime, + @SortOrder '; + + SET @sql += REPLACE(REPLACE(@body, '#view#', 'dm_exec_query_stats'), 'cached_time', 'creation_time') ; + + SET @sort_filter += CASE @SortOrder WHEN N'cpu' THEN N'AND total_worker_time > 0' + WHEN N'reads' THEN N'AND total_logical_reads > 0' + WHEN N'writes' THEN N'AND total_logical_writes > 0' + WHEN N'duration' THEN N'AND total_elapsed_time > 0' + WHEN N'executions' THEN N'AND execution_count > 0' + /* WHEN N'compiles' THEN N'AND (age_minutes + age_minutes_lifetime) > 0' BGO 2021-01-24 commenting out for https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2772 */ + WHEN N'memory grant' THEN N'AND max_grant_kb > 0' + WHEN N'unused grant' THEN N'AND max_grant_kb > 0' + WHEN N'spills' THEN N'AND max_spills > 0' + /* And now the averages */ + WHEN N'avg cpu' THEN N'AND (total_worker_time / execution_count) > 0' + WHEN N'avg reads' THEN N'AND (total_logical_reads / execution_count) > 0' + WHEN N'avg writes' THEN N'AND (total_logical_writes / execution_count) > 0' + WHEN N'avg duration' THEN N'AND (total_elapsed_time / execution_count) > 0' + WHEN N'avg memory grant' THEN N'AND CASE WHEN max_grant_kb = 0 THEN 0 ELSE (max_grant_kb / execution_count) END > 0' + WHEN N'avg spills' THEN N'AND CASE WHEN total_spills = 0 THEN 0 ELSE (total_spills / execution_count) END > 0' + WHEN N'avg executions' THEN N'AND CASE WHEN execution_count = 0 THEN 0 + WHEN COALESCE(age_minutes, age_minutes_lifetime, 0) = 0 THEN 0 + ELSE CAST((1.00 * execution_count / COALESCE(age_minutes, age_minutes_lifetime)) AS money) + END > 0' + ELSE N' /* No minimum threshold set */ ' + END; + + SET @sql += REPLACE(@body_where, 'cached_time', 'creation_time') ; + + SET @sql += @sort_filter + @nl; + + SET @sql += @body_order + @nl + @nl + @nl; + + IF @SortOrder = 'compiles' + BEGIN + RAISERROR(N'Sorting by compiles', 0, 1) WITH NOWAIT; + SET @sql = REPLACE(@sql, '#sortable#', 'creation_time'); + END; +END; + + +IF (@QueryFilter = 'all' + AND (SELECT COUNT(*) FROM #only_query_hashes) = 0 + AND (SELECT COUNT(*) FROM #ignore_query_hashes) = 0) + AND (@SortOrder NOT IN ('memory grant', 'avg memory grant', 'unused grant')) + OR (LEFT(@QueryFilter, 3) = 'pro') +BEGIN + SET @sql += @insert_list; + SET @sql += REPLACE(@plans_triggers_select_list, '#query_type#', 'Stored Procedure') ; + + SET @sql += REPLACE(@body, '#view#', 'dm_exec_procedure_stats') ; + SET @sql += @body_where ; + + IF @IgnoreSystemDBs = 1 + SET @sql += N' AND COALESCE(LOWER(DB_NAME(database_id)), LOWER(CAST(pa.value AS sysname)), '''') NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''32767'', ''dbmaintenance'', ''dbadmin'', ''dbatools'') AND COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '''') NOT IN (SELECT name FROM sys.databases WHERE is_distributor = 1)' + @nl ; + + SET @sql += @sort_filter + @nl; + + SET @sql += @body_order + @nl + @nl + @nl ; +END; + +IF (@v >= 13 + AND @QueryFilter = 'all' + AND (SELECT COUNT(*) FROM #only_query_hashes) = 0 + AND (SELECT COUNT(*) FROM #ignore_query_hashes) = 0) + AND (@SortOrder NOT IN ('memory grant', 'avg memory grant', 'unused grant')) + AND (@SortOrder NOT IN ('spills', 'avg spills')) + OR (LEFT(@QueryFilter, 3) = 'fun') +BEGIN + SET @sql += @insert_list; + SET @sql += REPLACE(REPLACE(@plans_triggers_select_list, '#query_type#', 'Function') + , N' + min_spills AS MinSpills, + max_spills AS MaxSpills, + total_spills AS TotalSpills, + CAST(ISNULL(NULLIF(( total_spills * 1. ), 0) / NULLIF(execution_count, 0), 0) AS MONEY) AS AvgSpills, ', + N' + NULL AS MinSpills, + NULL AS MaxSpills, + NULL AS TotalSpills, + NULL AS AvgSpills, ') ; + + SET @sql += REPLACE(@body, '#view#', 'dm_exec_function_stats') ; + SET @sql += @body_where ; + + IF @IgnoreSystemDBs = 1 + SET @sql += N' AND COALESCE(LOWER(DB_NAME(database_id)), LOWER(CAST(pa.value AS sysname)), '''') NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''32767'', ''dbmaintenance'', ''dbadmin'', ''dbatools'') AND COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '''') NOT IN (SELECT name FROM sys.databases WHERE is_distributor = 1)' + @nl ; + + SET @sql += @sort_filter + @nl; + + SET @sql += @body_order + @nl + @nl + @nl ; +END; + +/******************************************************************************* + * + * Because the trigger execution count in SQL Server 2008R2 and earlier is not + * correct, we ignore triggers for these versions of SQL Server. If you'd like + * to include trigger numbers, just know that the ExecutionCount, + * PercentExecutions, and ExecutionsPerMinute are wildly inaccurate for + * triggers on these versions of SQL Server. + * + * This is why we can't have nice things. + * + ******************************************************************************/ +IF (@UseTriggersAnyway = 1 OR @v >= 11) + AND (SELECT COUNT(*) FROM #only_query_hashes) = 0 + AND (SELECT COUNT(*) FROM #ignore_query_hashes) = 0 + AND (@QueryFilter = 'all') + AND (@SortOrder NOT IN ('memory grant', 'avg memory grant', 'unused grant')) +BEGIN + RAISERROR (N'Adding SQL to collect trigger stats.',0,1) WITH NOWAIT; + + /* Trigger level information from the plan cache */ + SET @sql += @insert_list ; + + SET @sql += REPLACE(@plans_triggers_select_list, '#query_type#', 'Trigger') ; + + SET @sql += REPLACE(@body, '#view#', 'dm_exec_trigger_stats') ; + + SET @sql += @body_where ; + + IF @IgnoreSystemDBs = 1 + SET @sql += N' AND COALESCE(LOWER(DB_NAME(database_id)), LOWER(CAST(pa.value AS sysname)), '''') NOT IN (''master'', ''model'', ''msdb'', ''tempdb'', ''32767'', ''dbmaintenance'', ''dbadmin'', ''dbatools'') AND COALESCE(DB_NAME(database_id), CAST(pa.value AS sysname), '''') NOT IN (SELECT name FROM sys.databases WHERE is_distributor = 1)' + @nl ; + + SET @sql += @sort_filter + @nl; + + SET @sql += @body_order + @nl + @nl + @nl ; +END; + + + +SELECT @sort = CASE @SortOrder WHEN N'cpu' THEN N'total_worker_time' + WHEN N'reads' THEN N'total_logical_reads' + WHEN N'writes' THEN N'total_logical_writes' + WHEN N'duration' THEN N'total_elapsed_time' + WHEN N'executions' THEN N'execution_count' + WHEN N'compiles' THEN N'cached_time' + WHEN N'memory grant' THEN N'max_grant_kb' + WHEN N'unused grant' THEN N'max_grant_kb - max_used_grant_kb' + WHEN N'spills' THEN N'max_spills' + /* And now the averages */ + WHEN N'avg cpu' THEN N'total_worker_time / execution_count' + WHEN N'avg reads' THEN N'total_logical_reads / execution_count' + WHEN N'avg writes' THEN N'total_logical_writes / execution_count' + WHEN N'avg duration' THEN N'total_elapsed_time / execution_count' + WHEN N'avg memory grant' THEN N'CASE WHEN max_grant_kb = 0 THEN 0 ELSE max_grant_kb / execution_count END' + WHEN N'avg spills' THEN N'CASE WHEN total_spills = 0 THEN 0 ELSE total_spills / execution_count END' + WHEN N'avg executions' THEN N'CASE WHEN execution_count = 0 THEN 0 + WHEN COALESCE(age_minutes, age_minutes_lifetime, 0) = 0 THEN 0 + ELSE CAST((1.00 * execution_count / COALESCE(age_minutes, age_minutes_lifetime)) AS money) + END' + END ; + +SELECT @sql = REPLACE(@sql, '#sortable#', @sort); + +SET @sql += N' +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; +INSERT INTO #p (SqlHandle, TotalCPU, TotalReads, TotalDuration, TotalWrites, ExecutionCount) +SELECT SqlHandle, + TotalCPU, + TotalReads, + TotalDuration, + TotalWrites, + ExecutionCount +FROM (SELECT SqlHandle, + TotalCPU, + TotalReads, + TotalDuration, + TotalWrites, + ExecutionCount, + ROW_NUMBER() OVER (PARTITION BY SqlHandle ORDER BY #sortable# DESC) AS rn + FROM ##BlitzCacheProcs + WHERE SPID = @@SPID) AS x +WHERE x.rn = 1 +OPTION (RECOMPILE); + +/* + This block was used to delete duplicate queries, but has been removed. + For more info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2026 +WITH d AS ( +SELECT SPID, + ROW_NUMBER() OVER (PARTITION BY SqlHandle, QueryHash ORDER BY #sortable# DESC) AS rn +FROM ##BlitzCacheProcs +WHERE SPID = @@SPID +) +DELETE d +WHERE d.rn > 1 +AND SPID = @@SPID +OPTION (RECOMPILE); +*/ +'; + +SELECT @sort = CASE @SortOrder WHEN N'cpu' THEN N'TotalCPU' + WHEN N'reads' THEN N'TotalReads' + WHEN N'writes' THEN N'TotalWrites' + WHEN N'duration' THEN N'TotalDuration' + WHEN N'executions' THEN N'ExecutionCount' + WHEN N'compiles' THEN N'PlanCreationTime' + WHEN N'memory grant' THEN N'MaxGrantKB' + WHEN N'unused grant' THEN N'MaxGrantKB - MaxUsedGrantKB' + WHEN N'spills' THEN N'MaxSpills' + /* And now the averages */ + WHEN N'avg cpu' THEN N'TotalCPU / ExecutionCount' + WHEN N'avg reads' THEN N'TotalReads / ExecutionCount' + WHEN N'avg writes' THEN N'TotalWrites / ExecutionCount' + WHEN N'avg duration' THEN N'TotalDuration / ExecutionCount' + WHEN N'avg memory grant' THEN N'AvgMaxMemoryGrant' + WHEN N'avg spills' THEN N'AvgSpills' + WHEN N'avg executions' THEN N'CASE WHEN ExecutionCount = 0 THEN 0 + WHEN COALESCE(age_minutes, age_minutes_lifetime, 0) = 0 THEN 0 + ELSE CAST((1.00 * ExecutionCount / COALESCE(age_minutes, age_minutes_lifetime)) AS money) + END' + END ; + +SELECT @sql = REPLACE(@sql, '#sortable#', @sort); + + +IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@sql, 0, 4000); + PRINT SUBSTRING(@sql, 4000, 8000); + PRINT SUBSTRING(@sql, 8000, 12000); + PRINT SUBSTRING(@sql, 12000, 16000); + PRINT SUBSTRING(@sql, 16000, 20000); + PRINT SUBSTRING(@sql, 20000, 24000); + PRINT SUBSTRING(@sql, 24000, 28000); + PRINT SUBSTRING(@sql, 28000, 32000); + PRINT SUBSTRING(@sql, 32000, 36000); + PRINT SUBSTRING(@sql, 36000, 40000); + END; + +IF @Reanalyze = 0 +BEGIN + RAISERROR('Collecting execution plan information.', 0, 1) WITH NOWAIT; + + EXEC sp_executesql @sql, N'@Top INT, @min_duration INT, @min_back INT, @SortOrder NVARCHAR(20)', @Top, @DurationFilter_i, @MinutesBack, @SortOrder; +END; + +IF @SkipAnalysis = 1 + BEGIN + RAISERROR(N'Skipping analysis, going to results', 0, 1) WITH NOWAIT; + GOTO Results ; + END; + + +/* Update ##BlitzCacheProcs to get Stored Proc info + * This should get totals for all statements in a Stored Proc + */ +RAISERROR(N'Attempting to aggregate stored proc info from separate statements', 0, 1) WITH NOWAIT; +;WITH agg AS ( + SELECT + b.SqlHandle, + SUM(b.MinReturnedRows) AS MinReturnedRows, + SUM(b.MaxReturnedRows) AS MaxReturnedRows, + SUM(b.AverageReturnedRows) AS AverageReturnedRows, + SUM(b.TotalReturnedRows) AS TotalReturnedRows, + SUM(b.LastReturnedRows) AS LastReturnedRows, + SUM(b.MinGrantKB) AS MinGrantKB, + SUM(b.MaxGrantKB) AS MaxGrantKB, + SUM(b.MinUsedGrantKB) AS MinUsedGrantKB, + SUM(b.MaxUsedGrantKB) AS MaxUsedGrantKB, + SUM(b.MinSpills) AS MinSpills, + SUM(b.MaxSpills) AS MaxSpills, + SUM(b.TotalSpills) AS TotalSpills + FROM ##BlitzCacheProcs b + WHERE b.SPID = @@SPID + AND b.QueryHash IS NOT NULL + GROUP BY b.SqlHandle +) +UPDATE b + SET + b.MinReturnedRows = b2.MinReturnedRows, + b.MaxReturnedRows = b2.MaxReturnedRows, + b.AverageReturnedRows = b2.AverageReturnedRows, + b.TotalReturnedRows = b2.TotalReturnedRows, + b.LastReturnedRows = b2.LastReturnedRows, + b.MinGrantKB = b2.MinGrantKB, + b.MaxGrantKB = b2.MaxGrantKB, + b.MinUsedGrantKB = b2.MinUsedGrantKB, + b.MaxUsedGrantKB = b2.MaxUsedGrantKB, + b.MinSpills = b2.MinSpills, + b.MaxSpills = b2.MaxSpills, + b.TotalSpills = b2.TotalSpills +FROM ##BlitzCacheProcs b +JOIN agg b2 +ON b2.SqlHandle = b.SqlHandle +WHERE b.QueryHash IS NULL +AND b.SPID = @@SPID +OPTION (RECOMPILE) ; + +/* Compute the total CPU, etc across our active set of the plan cache. + * Yes, there's a flaw - this doesn't include anything outside of our @Top + * metric. + */ +RAISERROR('Computing CPU, duration, read, and write metrics', 0, 1) WITH NOWAIT; +DECLARE @total_duration BIGINT, + @total_cpu BIGINT, + @total_reads BIGINT, + @total_writes BIGINT, + @total_execution_count BIGINT; + +SELECT @total_cpu = SUM(TotalCPU), + @total_duration = SUM(TotalDuration), + @total_reads = SUM(TotalReads), + @total_writes = SUM(TotalWrites), + @total_execution_count = SUM(ExecutionCount) +FROM #p +OPTION (RECOMPILE) ; + +DECLARE @cr NVARCHAR(1) = NCHAR(13); +DECLARE @lf NVARCHAR(1) = NCHAR(10); +DECLARE @tab NVARCHAR(1) = NCHAR(9); + +/* Update CPU percentage for stored procedures */ +RAISERROR(N'Update CPU percentage for stored procedures', 0, 1) WITH NOWAIT; +UPDATE ##BlitzCacheProcs +SET PercentCPU = y.PercentCPU, + PercentDuration = y.PercentDuration, + PercentReads = y.PercentReads, + PercentWrites = y.PercentWrites, + PercentExecutions = y.PercentExecutions, + ExecutionsPerMinute = y.ExecutionsPerMinute, + /* Strip newlines and tabs. Tabs are replaced with multiple spaces + so that the later whitespace trim will completely eliminate them + */ + QueryText = REPLACE(REPLACE(REPLACE(QueryText, @cr, ' '), @lf, ' '), @tab, ' ') +FROM ( + SELECT PlanHandle, + CASE @total_cpu WHEN 0 THEN 0 + ELSE CAST((100. * TotalCPU) / @total_cpu AS MONEY) END AS PercentCPU, + CASE @total_duration WHEN 0 THEN 0 + ELSE CAST((100. * TotalDuration) / @total_duration AS MONEY) END AS PercentDuration, + CASE @total_reads WHEN 0 THEN 0 + ELSE CAST((100. * TotalReads) / @total_reads AS MONEY) END AS PercentReads, + CASE @total_writes WHEN 0 THEN 0 + ELSE CAST((100. * TotalWrites) / @total_writes AS MONEY) END AS PercentWrites, + CASE @total_execution_count WHEN 0 THEN 0 + ELSE CAST((100. * ExecutionCount) / @total_execution_count AS MONEY) END AS PercentExecutions, + CASE DATEDIFF(mi, PlanCreationTime, LastExecutionTime) + WHEN 0 THEN 0 + ELSE CAST((1.00 * ExecutionCount / DATEDIFF(mi, PlanCreationTime, LastExecutionTime)) AS MONEY) + END AS ExecutionsPerMinute + FROM ( + SELECT PlanHandle, + TotalCPU, + TotalDuration, + TotalReads, + TotalWrites, + ExecutionCount, + PlanCreationTime, + LastExecutionTime + FROM ##BlitzCacheProcs + WHERE PlanHandle IS NOT NULL + AND SPID = @@SPID + GROUP BY PlanHandle, + TotalCPU, + TotalDuration, + TotalReads, + TotalWrites, + ExecutionCount, + PlanCreationTime, + LastExecutionTime + ) AS x +) AS y +WHERE ##BlitzCacheProcs.PlanHandle = y.PlanHandle + AND ##BlitzCacheProcs.PlanHandle IS NOT NULL + AND ##BlitzCacheProcs.SPID = @@SPID +OPTION (RECOMPILE) ; + + +RAISERROR(N'Gather percentage information from grouped results', 0, 1) WITH NOWAIT; +UPDATE ##BlitzCacheProcs +SET PercentCPU = y.PercentCPU, + PercentDuration = y.PercentDuration, + PercentReads = y.PercentReads, + PercentWrites = y.PercentWrites, + PercentExecutions = y.PercentExecutions, + ExecutionsPerMinute = y.ExecutionsPerMinute, + /* Strip newlines and tabs. Tabs are replaced with multiple spaces + so that the later whitespace trim will completely eliminate them + */ + QueryText = REPLACE(REPLACE(REPLACE(QueryText, @cr, ' '), @lf, ' '), @tab, ' ') +FROM ( + SELECT DatabaseName, + SqlHandle, + QueryHash, + CASE @total_cpu WHEN 0 THEN 0 + ELSE CAST((100. * TotalCPU) / @total_cpu AS MONEY) END AS PercentCPU, + CASE @total_duration WHEN 0 THEN 0 + ELSE CAST((100. * TotalDuration) / @total_duration AS MONEY) END AS PercentDuration, + CASE @total_reads WHEN 0 THEN 0 + ELSE CAST((100. * TotalReads) / @total_reads AS MONEY) END AS PercentReads, + CASE @total_writes WHEN 0 THEN 0 + ELSE CAST((100. * TotalWrites) / @total_writes AS MONEY) END AS PercentWrites, + CASE @total_execution_count WHEN 0 THEN 0 + ELSE CAST((100. * ExecutionCount) / @total_execution_count AS MONEY) END AS PercentExecutions, + CASE DATEDIFF(mi, PlanCreationTime, LastExecutionTime) + WHEN 0 THEN 0 + ELSE CAST((1.00 * ExecutionCount / DATEDIFF(mi, PlanCreationTime, LastExecutionTime)) AS MONEY) + END AS ExecutionsPerMinute + FROM ( + SELECT DatabaseName, + SqlHandle, + QueryHash, + TotalCPU, + TotalDuration, + TotalReads, + TotalWrites, + ExecutionCount, + PlanCreationTime, + LastExecutionTime + FROM ##BlitzCacheProcs + WHERE SPID = @@SPID + GROUP BY DatabaseName, + SqlHandle, + QueryHash, + TotalCPU, + TotalDuration, + TotalReads, + TotalWrites, + ExecutionCount, + PlanCreationTime, + LastExecutionTime + ) AS x +) AS y +WHERE ##BlitzCacheProcs.SqlHandle = y.SqlHandle + AND ##BlitzCacheProcs.QueryHash = y.QueryHash + AND ##BlitzCacheProcs.DatabaseName = y.DatabaseName + AND ##BlitzCacheProcs.PlanHandle IS NULL +OPTION (RECOMPILE) ; + + + +/* Testing using XML nodes to speed up processing */ +RAISERROR(N'Begin XML nodes processing', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +SELECT QueryHash , + SqlHandle , + PlanHandle, + q.n.query('.') AS statement, + 0 AS is_cursor +INTO #statements +FROM ##BlitzCacheProcs p + CROSS APPLY p.QueryPlan.nodes('//p:StmtSimple') AS q(n) +WHERE p.SPID = @@SPID +OPTION (RECOMPILE) ; + +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #statements +SELECT QueryHash , + SqlHandle , + PlanHandle, + q.n.query('.') AS statement, + 1 AS is_cursor +FROM ##BlitzCacheProcs p + CROSS APPLY p.QueryPlan.nodes('//p:StmtCursor') AS q(n) +WHERE p.SPID = @@SPID +OPTION (RECOMPILE) ; + +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +SELECT QueryHash , + SqlHandle , + q.n.query('.') AS query_plan +INTO #query_plan +FROM #statements p + CROSS APPLY p.statement.nodes('//p:QueryPlan') AS q(n) +OPTION (RECOMPILE) ; + +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +SELECT QueryHash , + SqlHandle , + q.n.query('.') AS relop +INTO #relop +FROM #query_plan p + CROSS APPLY p.query_plan.nodes('//p:RelOp') AS q(n) +OPTION (RECOMPILE) ; + +-- high level plan stuff +RAISERROR(N'Gathering high level plan information', 0, 1) WITH NOWAIT; +UPDATE ##BlitzCacheProcs +SET NumberOfDistinctPlans = distinct_plan_count, + NumberOfPlans = number_of_plans , + plan_multiple_plans = CASE WHEN distinct_plan_count < number_of_plans THEN number_of_plans END +FROM + ( + SELECT + DatabaseName = + DB_NAME(CONVERT(int, pa.value)), + QueryHash = + qs.query_hash, + number_of_plans = + COUNT_BIG(qs.query_plan_hash), + distinct_plan_count = + COUNT_BIG(DISTINCT qs.query_plan_hash) + FROM sys.dm_exec_query_stats AS qs + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) pa + WHERE pa.attribute = 'dbid' + GROUP BY + DB_NAME(CONVERT(int, pa.value)), + qs.query_hash +) AS x +WHERE ##BlitzCacheProcs.QueryHash = x.QueryHash +AND ##BlitzCacheProcs.DatabaseName = x.DatabaseName +OPTION (RECOMPILE) ; + +-- query level checks +RAISERROR(N'Performing query level checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET missing_index_count = query_plan.value('count(//p:QueryPlan/p:MissingIndexes/p:MissingIndexGroup)', 'int') , + unmatched_index_count = CASE WHEN is_trivial <> 1 THEN query_plan.value('count(//p:QueryPlan/p:UnmatchedIndexes/p:Parameterization/p:Object)', 'int') END , + SerialDesiredMemory = query_plan.value('sum(//p:QueryPlan/p:MemoryGrantInfo/@SerialDesiredMemory)', 'float') , + SerialRequiredMemory = query_plan.value('sum(//p:QueryPlan/p:MemoryGrantInfo/@SerialRequiredMemory)', 'float'), + CachedPlanSize = query_plan.value('sum(//p:QueryPlan/@CachedPlanSize)', 'float') , + CompileTime = query_plan.value('sum(//p:QueryPlan/@CompileTime)', 'float') , + CompileCPU = query_plan.value('sum(//p:QueryPlan/@CompileCPU)', 'float') , + CompileMemory = query_plan.value('sum(//p:QueryPlan/@CompileMemory)', 'float'), + MaxCompileMemory = query_plan.value('sum(//p:QueryPlan/p:OptimizerHardwareDependentProperties/@MaxCompileMemory)', 'float') +FROM #query_plan qp +WHERE qp.QueryHash = ##BlitzCacheProcs.QueryHash +AND qp.SqlHandle = ##BlitzCacheProcs.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); + +-- statement level checks +RAISERROR(N'Performing compile timeout checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET compile_timeout = 1 +FROM #statements s +JOIN ##BlitzCacheProcs b +ON s.QueryHash = b.QueryHash +AND SPID = @@SPID +WHERE statement.exist('/p:StmtSimple/@StatementOptmEarlyAbortReason[.="TimeOut"]') = 1 +OPTION (RECOMPILE); + +RAISERROR(N'Performing compile memory limit exceeded checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET compile_memory_limit_exceeded = 1 +FROM #statements s +JOIN ##BlitzCacheProcs b +ON s.QueryHash = b.QueryHash +AND SPID = @@SPID +WHERE statement.exist('/p:StmtSimple/@StatementOptmEarlyAbortReason[.="MemoryLimitExceeded"]') = 1 +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Performing unparameterized query checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +unparameterized_query AS ( + SELECT s.QueryHash, + unparameterized_query = CASE WHEN statement.exist('//p:StmtSimple[@StatementOptmLevel[.="FULL"]]/p:QueryPlan/p:ParameterList') = 1 AND + statement.exist('//p:StmtSimple[@StatementOptmLevel[.="FULL"]]/p:QueryPlan/p:ParameterList/p:ColumnReference') = 0 THEN 1 + WHEN statement.exist('//p:StmtSimple[@StatementOptmLevel[.="FULL"]]/p:QueryPlan/p:ParameterList') = 0 AND + statement.exist('//p:StmtSimple[@StatementOptmLevel[.="FULL"]]/*/p:RelOp/descendant::p:ScalarOperator/p:Identifier/p:ColumnReference[contains(@Column, "@")]') = 1 THEN 1 + END + FROM #statements AS s + ) +UPDATE b +SET b.unparameterized_query = u.unparameterized_query +FROM ##BlitzCacheProcs b +JOIN unparameterized_query u +ON u.QueryHash = b.QueryHash +AND SPID = @@SPID +WHERE u.unparameterized_query = 1 +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Performing index DML checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +index_dml AS ( + SELECT s.QueryHash, + index_dml = CASE WHEN statement.exist('//p:StmtSimple/@StatementType[.="CREATE INDEX"]') = 1 THEN 1 + WHEN statement.exist('//p:StmtSimple/@StatementType[.="DROP INDEX"]') = 1 THEN 1 + END + FROM #statements s + ) + UPDATE b + SET b.index_dml = i.index_dml + FROM ##BlitzCacheProcs AS b + JOIN index_dml i + ON i.QueryHash = b.QueryHash + WHERE i.index_dml = 1 + AND b.SPID = @@SPID + OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Performing table DML checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +table_dml AS ( + SELECT s.QueryHash, + table_dml = CASE WHEN statement.exist('//p:StmtSimple/@StatementType[.="CREATE TABLE"]') = 1 THEN 1 + WHEN statement.exist('//p:StmtSimple/@StatementType[.="DROP OBJECT"]') = 1 THEN 1 + END + FROM #statements AS s + ) + UPDATE b + SET b.table_dml = t.table_dml + FROM ##BlitzCacheProcs AS b + JOIN table_dml t + ON t.QueryHash = b.QueryHash + WHERE t.table_dml = 1 + AND b.SPID = @@SPID + OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Gathering row estimates', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +INSERT INTO #est_rows +SELECT DISTINCT + CONVERT(BINARY(8), RIGHT('0000000000000000' + SUBSTRING(c.n.value('@QueryHash', 'VARCHAR(18)'), 3, 18), 16), 2) AS QueryHash, + c.n.value('(/p:StmtSimple/@StatementEstRows)[1]', 'FLOAT') AS estimated_rows +FROM #statements AS s +CROSS APPLY s.statement.nodes('/p:StmtSimple') AS c(n) +WHERE c.n.exist('/p:StmtSimple[@StatementEstRows > 0]') = 1; + + UPDATE b + SET b.estimated_rows = er.estimated_rows + FROM ##BlitzCacheProcs AS b + JOIN #est_rows er + ON er.QueryHash = b.QueryHash + WHERE b.SPID = @@SPID + AND b.QueryType = 'Statement' + OPTION (RECOMPILE); +END; + +RAISERROR(N'Gathering trivial plans', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +UPDATE b +SET b.is_trivial = 1 +FROM ##BlitzCacheProcs AS b +JOIN ( +SELECT s.SqlHandle +FROM #statements AS s +JOIN ( SELECT r.SqlHandle + FROM #relop AS r + WHERE r.relop.exist('//p:RelOp[contains(@LogicalOp, "Scan")]') = 1 ) AS r + ON r.SqlHandle = s.SqlHandle +WHERE s.statement.exist('//p:StmtSimple[@StatementOptmLevel[.="TRIVIAL"]]/p:QueryPlan/p:ParameterList') = 1 +) AS s +ON b.SqlHandle = s.SqlHandle +OPTION (RECOMPILE); + + +--Gather costs +RAISERROR(N'Gathering statement costs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT INTO #plan_cost ( QueryPlanCost, SqlHandle, PlanHandle, QueryHash, QueryPlanHash ) +SELECT DISTINCT + statement.value('sum(/p:StmtSimple/@StatementSubTreeCost)', 'float') QueryPlanCost, + s.SqlHandle, + s.PlanHandle, + CONVERT(BINARY(8), RIGHT('0000000000000000' + SUBSTRING(q.n.value('@QueryHash', 'VARCHAR(18)'), 3, 18), 16), 2) AS QueryHash, + CONVERT(BINARY(8), RIGHT('0000000000000000' + SUBSTRING(q.n.value('@QueryPlanHash', 'VARCHAR(18)'), 3, 18), 16), 2) AS QueryPlanHash +FROM #statements s +CROSS APPLY s.statement.nodes('/p:StmtSimple') AS q(n) +WHERE statement.value('sum(/p:StmtSimple/@StatementSubTreeCost)', 'float') > 0 +OPTION (RECOMPILE); + +RAISERROR(N'Updating statement costs', 0, 1) WITH NOWAIT; +WITH pc AS ( + SELECT SUM(DISTINCT pc.QueryPlanCost) AS QueryPlanCostSum, pc.QueryHash, pc.QueryPlanHash, pc.SqlHandle, pc.PlanHandle + FROM #plan_cost AS pc + GROUP BY pc.QueryHash, pc.QueryPlanHash, pc.SqlHandle, pc.PlanHandle +) + UPDATE b + SET b.QueryPlanCost = ISNULL(pc.QueryPlanCostSum, 0) + FROM pc + JOIN ##BlitzCacheProcs b + ON b.SqlHandle = pc.SqlHandle + AND b.QueryHash = pc.QueryHash + WHERE b.QueryType NOT LIKE '%Procedure%' + OPTION (RECOMPILE); + +IF EXISTS ( +SELECT 1 +FROM ##BlitzCacheProcs AS b +WHERE b.QueryType LIKE 'Procedure%' +) + +BEGIN + +RAISERROR(N'Gathering stored procedure costs', 0, 1) WITH NOWAIT; +;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, QueryCost AS ( + SELECT + DISTINCT + statement.value('sum(/p:StmtSimple/@StatementSubTreeCost)', 'float') AS SubTreeCost, + s.PlanHandle, + s.SqlHandle + FROM #statements AS s + WHERE PlanHandle IS NOT NULL +) +, QueryCostUpdate AS ( + SELECT + SUM(qc.SubTreeCost) OVER (PARTITION BY SqlHandle, PlanHandle) PlanTotalQuery, + qc.PlanHandle, + qc.SqlHandle + FROM QueryCost qc +) +INSERT INTO #proc_costs +SELECT qcu.PlanTotalQuery, PlanHandle, SqlHandle +FROM QueryCostUpdate AS qcu +OPTION (RECOMPILE); + + +UPDATE b + SET b.QueryPlanCost = ca.PlanTotalQuery +FROM ##BlitzCacheProcs AS b +CROSS APPLY ( + SELECT TOP 1 PlanTotalQuery + FROM #proc_costs qcu + WHERE qcu.PlanHandle = b.PlanHandle + ORDER BY PlanTotalQuery DESC +) ca +WHERE b.QueryType LIKE 'Procedure%' +AND b.SPID = @@SPID +OPTION (RECOMPILE); + +END; + +UPDATE b +SET b.QueryPlanCost = 0.0 +FROM ##BlitzCacheProcs b +WHERE b.QueryPlanCost IS NULL +AND b.SPID = @@SPID +OPTION (RECOMPILE); + +RAISERROR(N'Checking for plan warnings', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET plan_warnings = 1 +FROM #query_plan qp +WHERE qp.SqlHandle = ##BlitzCacheProcs.SqlHandle +AND SPID = @@SPID +AND query_plan.exist('/p:QueryPlan/p:Warnings') = 1 +OPTION (RECOMPILE); + +RAISERROR(N'Checking for implicit conversion', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET implicit_conversions = 1 +FROM #query_plan qp +WHERE qp.SqlHandle = ##BlitzCacheProcs.SqlHandle +AND SPID = @@SPID +AND query_plan.exist('/p:QueryPlan/p:Warnings/p:PlanAffectingConvert/@Expression[contains(., "CONVERT_IMPLICIT")]') = 1 +OPTION (RECOMPILE); + +-- operator level checks +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Performing busy loops checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE p +SET busy_loops = CASE WHEN (x.estimated_executions / 100.0) > x.estimated_rows THEN 1 END +FROM ##BlitzCacheProcs p + JOIN ( + SELECT qs.SqlHandle, + relop.value('sum(/p:RelOp/@EstimateRows)', 'float') AS estimated_rows , + relop.value('sum(/p:RelOp/@EstimateRewinds)', 'float') + relop.value('sum(/p:RelOp/@EstimateRebinds)', 'float') + 1.0 AS estimated_executions + FROM #relop qs + ) AS x ON p.SqlHandle = x.SqlHandle +WHERE SPID = @@SPID +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Performing TVF join check', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE p +SET p.tvf_join = CASE WHEN x.tvf_join = 1 THEN 1 END +FROM ##BlitzCacheProcs p + JOIN ( + SELECT r.SqlHandle, + 1 AS tvf_join + FROM #relop AS r + WHERE r.relop.exist('//p:RelOp[(@LogicalOp[.="Table-valued function"])]') = 1 + AND r.relop.exist('//p:RelOp[contains(@LogicalOp, "Join")]') = 1 + ) AS x ON p.SqlHandle = x.SqlHandle +WHERE SPID = @@SPID +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for operator warnings', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, x AS ( +SELECT r.SqlHandle, + c.n.exist('//p:Warnings[(@NoJoinPredicate[.="1"])]') AS warning_no_join_predicate, + c.n.exist('//p:ColumnsWithNoStatistics') AS no_stats_warning , + c.n.exist('//p:Warnings') AS relop_warnings +FROM #relop AS r +CROSS APPLY r.relop.nodes('/p:RelOp/p:Warnings') AS c(n) +) +UPDATE p +SET p.warning_no_join_predicate = x.warning_no_join_predicate, + p.no_stats_warning = x.no_stats_warning, + p.relop_warnings = x.relop_warnings +FROM ##BlitzCacheProcs AS p +JOIN x ON x.SqlHandle = p.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Checking for table variables', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, x AS ( +SELECT r.SqlHandle, + c.n.value('substring(@Table, 2, 1)','VARCHAR(100)') AS first_char +FROM #relop r +CROSS APPLY r.relop.nodes('//p:Object') AS c(n) +) +UPDATE p +SET is_table_variable = 1 +FROM ##BlitzCacheProcs AS p +JOIN x ON x.SqlHandle = p.SqlHandle +AND SPID = @@SPID +WHERE x.first_char = '@' +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for functions', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, x AS ( +SELECT qs.SqlHandle, + n.fn.value('count(distinct-values(//p:UserDefinedFunction[not(@IsClrFunction)]))', 'INT') AS function_count, + n.fn.value('count(distinct-values(//p:UserDefinedFunction[@IsClrFunction = "1"]))', 'INT') AS clr_function_count +FROM #relop qs +CROSS APPLY relop.nodes('/p:RelOp/p:ComputeScalar/p:DefinedValues/p:DefinedValue/p:ScalarOperator') n(fn) +) +UPDATE p +SET p.function_count = x.function_count, + p.clr_function_count = x.clr_function_count +FROM ##BlitzCacheProcs AS p +JOIN x ON x.SqlHandle = p.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Checking for expensive key lookups', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET key_lookup_cost = x.key_lookup_cost +FROM ( +SELECT + qs.SqlHandle, + MAX(relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS key_lookup_cost +FROM #relop qs +WHERE [relop].exist('/p:RelOp/p:IndexScan[(@Lookup[.="1"])]') = 1 +GROUP BY qs.SqlHandle +) AS x +WHERE ##BlitzCacheProcs.SqlHandle = x.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for expensive remote queries', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET remote_query_cost = x.remote_query_cost +FROM ( +SELECT + qs.SqlHandle, + MAX(relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS remote_query_cost +FROM #relop qs +WHERE [relop].exist('/p:RelOp[(@PhysicalOp[contains(., "Remote")])]') = 1 +GROUP BY qs.SqlHandle +) AS x +WHERE ##BlitzCacheProcs.SqlHandle = x.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); + +RAISERROR(N'Checking for expensive sorts', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET sort_cost = y.max_sort_cost +FROM ( + SELECT x.SqlHandle, MAX((x.sort_io + x.sort_cpu)) AS max_sort_cost + FROM ( + SELECT + qs.SqlHandle, + relop.value('sum(/p:RelOp/@EstimateIO)', 'float') AS sort_io, + relop.value('sum(/p:RelOp/@EstimateCPU)', 'float') AS sort_cpu + FROM #relop qs + WHERE [relop].exist('/p:RelOp[(@PhysicalOp[.="Sort"])]') = 1 + ) AS x + GROUP BY x.SqlHandle + ) AS y +WHERE ##BlitzCacheProcs.SqlHandle = y.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); + +IF NOT EXISTS(SELECT 1/0 FROM #statements AS s WHERE s.is_cursor = 1) +BEGIN + +RAISERROR(N'No cursor plans found, skipping', 0, 1) WITH NOWAIT; + +END + +IF EXISTS(SELECT 1/0 FROM #statements AS s WHERE s.is_cursor = 1) +BEGIN + +RAISERROR(N'Cursor plans found, investigating', 0, 1) WITH NOWAIT; + +RAISERROR(N'Checking for Optimistic cursors', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_optimistic_cursor = 1 +FROM ##BlitzCacheProcs b +JOIN #statements AS qs +ON b.SqlHandle = qs.SqlHandle +CROSS APPLY qs.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE SPID = @@SPID +AND n1.fn.exist('//p:CursorPlan/@CursorConcurrency[.="Optimistic"]') = 1 +AND qs.is_cursor = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Checking if cursor is Forward Only', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_forward_only_cursor = 1 +FROM ##BlitzCacheProcs b +JOIN #statements AS qs +ON b.SqlHandle = qs.SqlHandle +CROSS APPLY qs.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE SPID = @@SPID +AND n1.fn.exist('//p:CursorPlan/@ForwardOnly[.="true"]') = 1 +AND qs.is_cursor = 1 +OPTION (RECOMPILE); + +RAISERROR(N'Checking if cursor is Fast Forward', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_fast_forward_cursor = 1 +FROM ##BlitzCacheProcs b +JOIN #statements AS qs +ON b.SqlHandle = qs.SqlHandle +CROSS APPLY qs.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE SPID = @@SPID +AND n1.fn.exist('//p:CursorPlan/@CursorActualType[.="FastForward"]') = 1 +AND qs.is_cursor = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for Dynamic cursors', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_cursor_dynamic = 1 +FROM ##BlitzCacheProcs b +JOIN #statements AS qs +ON b.SqlHandle = qs.SqlHandle +CROSS APPLY qs.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE SPID = @@SPID +AND n1.fn.exist('//p:CursorPlan/@CursorActualType[.="Dynamic"]') = 1 +AND qs.is_cursor = 1 +OPTION (RECOMPILE); + +END + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for bad scans and plan forcing', 0, 1) WITH NOWAIT; +;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET +b.is_table_scan = x.is_table_scan, +b.backwards_scan = x.backwards_scan, +b.forced_index = x.forced_index, +b.forced_seek = x.forced_seek, +b.forced_scan = x.forced_scan +FROM ##BlitzCacheProcs b +JOIN ( +SELECT + qs.SqlHandle, + 0 AS is_table_scan, + q.n.exist('@ScanDirection[.="BACKWARD"]') AS backwards_scan, + q.n.value('@ForcedIndex', 'bit') AS forced_index, + q.n.value('@ForceSeek', 'bit') AS forced_seek, + q.n.value('@ForceScan', 'bit') AS forced_scan +FROM #relop qs +CROSS APPLY qs.relop.nodes('//p:IndexScan') AS q(n) +UNION ALL +SELECT + qs.SqlHandle, + 1 AS is_table_scan, + q.n.exist('@ScanDirection[.="BACKWARD"]') AS backwards_scan, + q.n.value('@ForcedIndex', 'bit') AS forced_index, + q.n.value('@ForceSeek', 'bit') AS forced_seek, + q.n.value('@ForceScan', 'bit') AS forced_scan +FROM #relop qs +CROSS APPLY qs.relop.nodes('//p:TableScan') AS q(n) +) AS x ON b.SqlHandle = x.SqlHandle +WHERE SPID = @@SPID +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for computed columns that reference scalar UDFs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET is_computed_scalar = x.computed_column_function +FROM ( +SELECT qs.SqlHandle, + n.fn.value('count(distinct-values(//p:UserDefinedFunction[not(@IsClrFunction)]))', 'INT') AS computed_column_function +FROM #relop qs +CROSS APPLY relop.nodes('/p:RelOp/p:ComputeScalar/p:DefinedValues/p:DefinedValue/p:ScalarOperator') n(fn) +WHERE n.fn.exist('/p:RelOp/p:ComputeScalar/p:DefinedValues/p:DefinedValue/p:ColumnReference[(@ComputedColumn[.="1"])]') = 1 +) AS x +WHERE ##BlitzCacheProcs.SqlHandle = x.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Checking for filters that reference scalar UDFs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET is_computed_filter = x.filter_function +FROM ( +SELECT +r.SqlHandle, +c.n.value('count(distinct-values(//p:UserDefinedFunction[not(@IsClrFunction)]))', 'INT') AS filter_function +FROM #relop AS r +CROSS APPLY r.relop.nodes('/p:RelOp/p:Filter/p:Predicate/p:ScalarOperator/p:Compare/p:ScalarOperator/p:UserDefinedFunction') c(n) +) x +WHERE ##BlitzCacheProcs.SqlHandle = x.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking modification queries that hit lots of indexes', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +IndexOps AS +( + SELECT + r.QueryHash, + c.n.value('@PhysicalOp', 'VARCHAR(100)') AS op_name, + c.n.exist('@PhysicalOp[.="Index Insert"]') AS ii, + c.n.exist('@PhysicalOp[.="Index Update"]') AS iu, + c.n.exist('@PhysicalOp[.="Index Delete"]') AS id, + c.n.exist('@PhysicalOp[.="Clustered Index Insert"]') AS cii, + c.n.exist('@PhysicalOp[.="Clustered Index Update"]') AS ciu, + c.n.exist('@PhysicalOp[.="Clustered Index Delete"]') AS cid, + c.n.exist('@PhysicalOp[.="Table Insert"]') AS ti, + c.n.exist('@PhysicalOp[.="Table Update"]') AS tu, + c.n.exist('@PhysicalOp[.="Table Delete"]') AS td + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp') c(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:ScalarInsert/p:Object') q(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:Update/p:Object') o2(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:SimpleUpdate/p:Object') o3(n) +), iops AS +( + SELECT ios.QueryHash, + SUM(CONVERT(TINYINT, ios.ii)) AS index_insert_count, + SUM(CONVERT(TINYINT, ios.iu)) AS index_update_count, + SUM(CONVERT(TINYINT, ios.id)) AS index_delete_count, + SUM(CONVERT(TINYINT, ios.cii)) AS cx_insert_count, + SUM(CONVERT(TINYINT, ios.ciu)) AS cx_update_count, + SUM(CONVERT(TINYINT, ios.cid)) AS cx_delete_count, + SUM(CONVERT(TINYINT, ios.ti)) AS table_insert_count, + SUM(CONVERT(TINYINT, ios.tu)) AS table_update_count, + SUM(CONVERT(TINYINT, ios.td)) AS table_delete_count + FROM IndexOps AS ios + WHERE ios.op_name IN ('Index Insert', 'Index Delete', 'Index Update', + 'Clustered Index Insert', 'Clustered Index Delete', 'Clustered Index Update', + 'Table Insert', 'Table Delete', 'Table Update') + GROUP BY ios.QueryHash) +UPDATE b +SET b.index_insert_count = iops.index_insert_count, + b.index_update_count = iops.index_update_count, + b.index_delete_count = iops.index_delete_count, + b.cx_insert_count = iops.cx_insert_count, + b.cx_update_count = iops.cx_update_count, + b.cx_delete_count = iops.cx_delete_count, + b.table_insert_count = iops.table_insert_count, + b.table_update_count = iops.table_update_count, + b.table_delete_count = iops.table_delete_count +FROM ##BlitzCacheProcs AS b +JOIN iops ON iops.QueryHash = b.QueryHash +WHERE SPID = @@SPID +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for Spatial index use', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET is_spatial = x.is_spatial +FROM ( +SELECT qs.SqlHandle, + 1 AS is_spatial +FROM #relop qs +CROSS APPLY relop.nodes('/p:RelOp//p:Object') n(fn) +WHERE n.fn.exist('(@IndexKind[.="Spatial"])') = 1 +) AS x +WHERE ##BlitzCacheProcs.SqlHandle = x.SqlHandle +AND SPID = @@SPID +OPTION (RECOMPILE); +END; + + +RAISERROR('Checking for wonky Index Spools', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( + 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +, selects +AS ( SELECT s.QueryHash + FROM #statements AS s + WHERE s.statement.exist('/p:StmtSimple/@StatementType[.="SELECT"]') = 1 ) +, spools +AS ( SELECT DISTINCT r.QueryHash, + c.n.value('@EstimateRows', 'FLOAT') AS estimated_rows, + c.n.value('@EstimateIO', 'FLOAT') AS estimated_io, + c.n.value('@EstimateCPU', 'FLOAT') AS estimated_cpu, + c.n.value('@EstimateRebinds', 'FLOAT') AS estimated_rebinds +FROM #relop AS r +JOIN selects AS s +ON s.QueryHash = r.QueryHash +CROSS APPLY r.relop.nodes('/p:RelOp') AS c(n) +WHERE r.relop.exist('/p:RelOp[@PhysicalOp="Index Spool" and @LogicalOp="Eager Spool"]') = 1 +) +UPDATE b + SET b.index_spool_rows = sp.estimated_rows, + b.index_spool_cost = ((sp.estimated_io * sp.estimated_cpu) * CASE WHEN sp.estimated_rebinds < 1 THEN 1 ELSE sp.estimated_rebinds END) +FROM ##BlitzCacheProcs b +JOIN spools sp +ON sp.QueryHash = b.QueryHash +OPTION (RECOMPILE); + +RAISERROR('Checking for wonky Table Spools', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( + 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +, selects +AS ( SELECT s.QueryHash + FROM #statements AS s + WHERE s.statement.exist('/p:StmtSimple/@StatementType[.="SELECT"]') = 1 ) +, spools +AS ( SELECT DISTINCT r.QueryHash, + c.n.value('@EstimateRows', 'FLOAT') AS estimated_rows, + c.n.value('@EstimateIO', 'FLOAT') AS estimated_io, + c.n.value('@EstimateCPU', 'FLOAT') AS estimated_cpu, + c.n.value('@EstimateRebinds', 'FLOAT') AS estimated_rebinds +FROM #relop AS r +JOIN selects AS s +ON s.QueryHash = r.QueryHash +CROSS APPLY r.relop.nodes('/p:RelOp') AS c(n) +WHERE r.relop.exist('/p:RelOp[@PhysicalOp="Table Spool" and @LogicalOp="Lazy Spool"]') = 1 +) +UPDATE b + SET b.table_spool_rows = (sp.estimated_rows * sp.estimated_rebinds), + b.table_spool_cost = ((sp.estimated_io * sp.estimated_cpu * sp.estimated_rows) * CASE WHEN sp.estimated_rebinds < 1 THEN 1 ELSE sp.estimated_rebinds END) +FROM ##BlitzCacheProcs b +JOIN spools sp +ON sp.QueryHash = b.QueryHash +OPTION (RECOMPILE); + + +RAISERROR('Checking for selects that cause non-spill and index spool writes', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( + 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +, selects +AS ( SELECT CONVERT(BINARY(8), + RIGHT('0000000000000000' + + SUBSTRING(s.statement.value('(/p:StmtSimple/@QueryHash)[1]', 'VARCHAR(18)'), + 3, 18), 16), 2) AS QueryHash + FROM #statements AS s + JOIN ##BlitzCacheProcs b + ON s.QueryHash = b.QueryHash + WHERE b.index_spool_rows IS NULL + AND b.index_spool_cost IS NULL + AND b.table_spool_cost IS NULL + AND b.table_spool_rows IS NULL + AND b.is_big_spills IS NULL + AND b.AverageWrites > 1024. + AND s.statement.exist('/p:StmtSimple/@StatementType[.="SELECT"]') = 1 +) +UPDATE b + SET b.select_with_writes = 1 +FROM ##BlitzCacheProcs b +JOIN selects AS s +ON s.QueryHash = b.QueryHash +AND b.AverageWrites > 1024.; + +/* 2012+ only */ +IF @v >= 11 +BEGIN + + RAISERROR(N'Checking for forced serialization', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + UPDATE ##BlitzCacheProcs + SET is_forced_serial = 1 + FROM #query_plan qp + WHERE qp.SqlHandle = ##BlitzCacheProcs.SqlHandle + AND SPID = @@SPID + AND query_plan.exist('/p:QueryPlan/@NonParallelPlanReason') = 1 + AND (##BlitzCacheProcs.is_parallel = 0 OR ##BlitzCacheProcs.is_parallel IS NULL) + OPTION (RECOMPILE); + + IF @ExpertMode > 0 + BEGIN + RAISERROR(N'Checking for ColumnStore queries operating in Row Mode instead of Batch Mode', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + UPDATE ##BlitzCacheProcs + SET columnstore_row_mode = x.is_row_mode + FROM ( + SELECT + qs.SqlHandle, + relop.exist('/p:RelOp[(@EstimatedExecutionMode[.="Row"])]') AS is_row_mode + FROM #relop qs + WHERE [relop].exist('/p:RelOp/p:IndexScan[(@Storage[.="ColumnStore"])]') = 1 + ) AS x + WHERE ##BlitzCacheProcs.SqlHandle = x.SqlHandle + AND SPID = @@SPID + OPTION (RECOMPILE); + END; + +END; + +/* 2014+ only */ +IF @v >= 12 +BEGIN + RAISERROR('Checking for downlevel cardinality estimators being used on SQL Server 2014.', 0, 1) WITH NOWAIT; + + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + UPDATE p + SET downlevel_estimator = CASE WHEN statement.value('min(//p:StmtSimple/@CardinalityEstimationModelVersion)', 'int') < (@v * 10) THEN 1 END + FROM ##BlitzCacheProcs p + JOIN #statements s ON p.QueryHash = s.QueryHash + WHERE SPID = @@SPID + OPTION (RECOMPILE); +END ; + +/* 2016+ only */ +IF @v >= 13 AND @ExpertMode > 0 +BEGIN + RAISERROR('Checking for row level security in 2016 only', 0, 1) WITH NOWAIT; + + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + UPDATE p + SET p.is_row_level = 1 + FROM ##BlitzCacheProcs p + JOIN #statements s ON p.QueryHash = s.QueryHash + WHERE SPID = @@SPID + AND statement.exist('/p:StmtSimple/@SecurityPolicyApplied[.="true"]') = 1 + OPTION (RECOMPILE); +END ; + +/* 2017+ only */ +IF @v >= 14 OR (@v = 13 AND @build >= 5026) +BEGIN + +IF @ExpertMode > 0 +BEGIN +RAISERROR('Gathering stats information', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT INTO #stats_agg +SELECT qp.SqlHandle, + x.c.value('@LastUpdate', 'DATETIME2(7)') AS LastUpdate, + x.c.value('@ModificationCount', 'BIGINT') AS ModificationCount, + x.c.value('@SamplingPercent', 'FLOAT') AS SamplingPercent, + x.c.value('@Statistics', 'NVARCHAR(258)') AS [Statistics], + x.c.value('@Table', 'NVARCHAR(258)') AS [Table], + x.c.value('@Schema', 'NVARCHAR(258)') AS [Schema], + x.c.value('@Database', 'NVARCHAR(258)') AS [Database] +FROM #query_plan AS qp +CROSS APPLY qp.query_plan.nodes('//p:OptimizerStatsUsage/p:StatisticsInfo') x (c) +OPTION (RECOMPILE); + + +RAISERROR('Checking for stale stats', 0, 1) WITH NOWAIT; +WITH stale_stats AS ( + SELECT sa.SqlHandle + FROM #stats_agg AS sa + GROUP BY sa.SqlHandle + HAVING MAX(sa.LastUpdate) <= DATEADD(DAY, -7, SYSDATETIME()) + AND AVG(sa.ModificationCount) >= 100000 +) +UPDATE b +SET stale_stats = 1 +FROM ##BlitzCacheProcs b +JOIN stale_stats os +ON b.SqlHandle = os.SqlHandle +AND b.SPID = @@SPID +OPTION (RECOMPILE); +END; + +IF @v >= 14 AND @ExpertMode > 0 +BEGIN +RAISERROR('Checking for adaptive joins', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +aj AS ( + SELECT + SqlHandle + FROM #relop AS r + CROSS APPLY r.relop.nodes('//p:RelOp') x(c) + WHERE x.c.exist('@IsAdaptive[.=1]') = 1 +) +UPDATE b +SET b.is_adaptive = 1 +FROM ##BlitzCacheProcs b +JOIN aj +ON b.SqlHandle = aj.SqlHandle +AND b.SPID = @@SPID +OPTION (RECOMPILE); +END; + +IF ((@v >= 14 + OR (@v = 13 AND @build >= 5026) + OR (@v = 12 AND @build >= 6024)) + AND @ExpertMode > 0) + +BEGIN; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +row_goals AS( +SELECT qs.QueryHash +FROM #relop qs +WHERE relop.value('sum(/p:RelOp/@EstimateRowsWithoutRowGoal)', 'float') > 0 +) +UPDATE b +SET b.is_row_goal = 1 +FROM ##BlitzCacheProcs b +JOIN row_goals +ON b.QueryHash = row_goals.QueryHash +AND b.SPID = @@SPID +OPTION (RECOMPILE); +END ; + +END; + + +/* END Testing using XML nodes to speed up processing */ + + +/* Update to grab stored procedure name for individual statements */ +RAISERROR(N'Attempting to get stored procedure name for individual statements', 0, 1) WITH NOWAIT; +UPDATE p +SET QueryType = QueryType + ' (parent ' + + + QUOTENAME(OBJECT_SCHEMA_NAME(s.object_id, s.database_id)) + + '.' + + QUOTENAME(OBJECT_NAME(s.object_id, s.database_id)) + ')' +FROM ##BlitzCacheProcs p + JOIN sys.dm_exec_procedure_stats s ON p.SqlHandle = s.sql_handle +WHERE QueryType = 'Statement' +AND SPID = @@SPID +OPTION (RECOMPILE); + +/* Update to grab stored procedure name for individual statements when PSPO is detected */ +UPDATE p +SET QueryType = QueryType + ' (parent ' + + + QUOTENAME(OBJECT_SCHEMA_NAME(s.object_id, s.database_id)) + + '.' + + QUOTENAME(OBJECT_NAME(s.object_id, s.database_id)) + ')' +FROM ##BlitzCacheProcs p + OUTER APPLY ( + SELECT REPLACE(REPLACE(REPLACE(REPLACE(p.QueryText, ' (', '('), '( ', '('), ' =', '='), '= ', '=') AS NormalizedQueryText + ) a + OUTER APPLY ( + SELECT CHARINDEX('option(PLAN PER VALUE(ObjectID=', a.NormalizedQueryText) AS OptionStart + ) b + OUTER APPLY ( + SELECT SUBSTRING(a.NormalizedQueryText, b.OptionStart + 31, LEN(a.NormalizedQueryText) - b.OptionStart - 30) AS OptionSubstring + WHERE b.OptionStart > 0 + ) c + OUTER APPLY ( + SELECT PATINDEX('%[^0-9]%', c.OptionSubstring) AS ObjectLength + ) d + OUTER APPLY ( + SELECT TRY_CAST(SUBSTRING(OptionSubstring, 1, d.ObjectLength - 1) AS INT) AS ObjectId + ) e + JOIN sys.dm_exec_procedure_stats s ON DB_ID(p.DatabaseName) = s.database_id AND e.ObjectId = s.object_id +WHERE p.QueryType = 'Statement' +AND p.SPID = @@SPID +AND s.object_id IS NOT NULL +OPTION (RECOMPILE); + +RAISERROR(N'Attempting to get function name for individual statements', 0, 1) WITH NOWAIT; +DECLARE @function_update_sql NVARCHAR(MAX) = N'' +IF EXISTS (SELECT 1/0 FROM sys.all_objects AS o WHERE o.name = 'dm_exec_function_stats') + BEGIN + SET @function_update_sql = @function_update_sql + N' + UPDATE p + SET QueryType = QueryType + '' (parent '' + + + QUOTENAME(OBJECT_SCHEMA_NAME(s.object_id, s.database_id)) + + ''.'' + + QUOTENAME(OBJECT_NAME(s.object_id, s.database_id)) + '')'' + FROM ##BlitzCacheProcs p + JOIN sys.dm_exec_function_stats s ON p.SqlHandle = s.sql_handle + WHERE QueryType = ''Statement'' + AND SPID = @@SPID + OPTION (RECOMPILE); + ' + EXEC sys.sp_executesql @function_update_sql + END + + +/* Trace Flag Checks 2012 SP3, 2014 SP2 and 2016 SP1 only)*/ +IF @v >= 11 +BEGIN + +RAISERROR(N'Trace flag checks', 0, 1) WITH NOWAIT; +;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, tf_pretty AS ( +SELECT qp.QueryHash, + qp.SqlHandle, + q.n.value('@Value', 'INT') AS trace_flag, + q.n.value('@Scope', 'VARCHAR(10)') AS scope +FROM #query_plan qp +CROSS APPLY qp.query_plan.nodes('/p:QueryPlan/p:TraceFlags/p:TraceFlag') AS q(n) +) +INSERT INTO #trace_flags +SELECT DISTINCT tf1.SqlHandle , tf1.QueryHash, + STUFF(( + SELECT DISTINCT ', ' + CONVERT(VARCHAR(5), tf2.trace_flag) + FROM tf_pretty AS tf2 + WHERE tf1.SqlHandle = tf2.SqlHandle + AND tf1.QueryHash = tf2.QueryHash + AND tf2.scope = 'Global' + FOR XML PATH(N'')), 1, 2, N'' + ) AS global_trace_flags, + STUFF(( + SELECT DISTINCT ', ' + CONVERT(VARCHAR(5), tf2.trace_flag) + FROM tf_pretty AS tf2 + WHERE tf1.SqlHandle = tf2.SqlHandle + AND tf1.QueryHash = tf2.QueryHash + AND tf2.scope = 'Session' + FOR XML PATH(N'')), 1, 2, N'' + ) AS session_trace_flags +FROM tf_pretty AS tf1 +OPTION (RECOMPILE); + +UPDATE p +SET p.trace_flags_session = tf.session_trace_flags +FROM ##BlitzCacheProcs p +JOIN #trace_flags tf ON tf.QueryHash = p.QueryHash +WHERE SPID = @@SPID +OPTION (RECOMPILE); + +END; + + +RAISERROR(N'Checking for MSTVFs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_mstvf = 1 +FROM #relop AS r +JOIN ##BlitzCacheProcs AS b +ON b.SqlHandle = r.SqlHandle +WHERE r.relop.exist('/p:RelOp[(@EstimateRows="100" or @EstimateRows="1") and @LogicalOp="Table-valued function"]') = 1 +OPTION (RECOMPILE); + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for many to many merge joins', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_mm_join = 1 +FROM #relop AS r +JOIN ##BlitzCacheProcs AS b +ON b.SqlHandle = r.SqlHandle +WHERE r.relop.exist('/p:RelOp/p:Merge/@ManyToMany[.="1"]') = 1 +OPTION (RECOMPILE); +END ; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Is Paul White Electric?', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +is_paul_white_electric AS ( +SELECT 1 AS [is_paul_white_electric], +r.SqlHandle +FROM #relop AS r +CROSS APPLY r.relop.nodes('//p:RelOp') c(n) +WHERE c.n.exist('@PhysicalOp[.="Switch"]') = 1 +) +UPDATE b +SET b.is_paul_white_electric = ipwe.is_paul_white_electric +FROM ##BlitzCacheProcs AS b +JOIN is_paul_white_electric ipwe +ON ipwe.SqlHandle = b.SqlHandle +WHERE b.SPID = @@SPID +OPTION (RECOMPILE); +END ; + + +RAISERROR(N'Checking for non-sargable predicates', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +, nsarg + AS ( SELECT r.QueryHash, 1 AS fn, 0 AS jo, 0 AS lk + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp/p:IndexScan/p:Predicate/p:ScalarOperator/p:Compare/p:ScalarOperator') AS ca(x) + WHERE ( ca.x.exist('//p:ScalarOperator/p:Intrinsic/@FunctionName') = 1 + OR ca.x.exist('//p:ScalarOperator/p:IF') = 1 ) + UNION ALL + SELECT r.QueryHash, 0 AS fn, 1 AS jo, 0 AS lk + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp//p:ScalarOperator') AS ca(x) + WHERE r.relop.exist('/p:RelOp[contains(@LogicalOp, "Join")]') = 1 + AND ca.x.exist('//p:ScalarOperator[contains(@ScalarString, "Expr")]') = 1 + UNION ALL + SELECT r.QueryHash, 0 AS fn, 0 AS jo, 1 AS lk + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp/p:IndexScan/p:Predicate/p:ScalarOperator') AS ca(x) + CROSS APPLY ca.x.nodes('//p:Const') AS co(x) + WHERE ca.x.exist('//p:ScalarOperator/p:Intrinsic/@FunctionName[.="like"]') = 1 + AND ( ( co.x.value('substring(@ConstValue, 1, 1)', 'VARCHAR(100)') <> 'N' + AND co.x.value('substring(@ConstValue, 2, 1)', 'VARCHAR(100)') = '%' ) + OR ( co.x.value('substring(@ConstValue, 1, 1)', 'VARCHAR(100)') = 'N' + AND co.x.value('substring(@ConstValue, 3, 1)', 'VARCHAR(100)') = '%' ))), + d_nsarg + AS ( SELECT DISTINCT + nsarg.QueryHash + FROM nsarg + WHERE nsarg.fn = 1 + OR nsarg.jo = 1 + OR nsarg.lk = 1 ) +UPDATE b +SET b.is_nonsargable = 1 +FROM d_nsarg AS d +JOIN ##BlitzCacheProcs AS b + ON b.QueryHash = d.QueryHash +WHERE b.SPID = @@SPID +OPTION ( RECOMPILE ); + +/*Begin implicit conversion and parameter info */ + +RAISERROR(N'Getting information about implicit conversions and stored proc parameters', 0, 1) WITH NOWAIT; + +RAISERROR(N'Getting variable info', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +INSERT #variable_info ( SPID, QueryHash, SqlHandle, proc_name, variable_name, variable_datatype, compile_time_value ) +SELECT DISTINCT @@SPID, + qp.QueryHash, + qp.SqlHandle, + b.QueryType AS proc_name, + q.n.value('@Column', 'NVARCHAR(258)') AS variable_name, + q.n.value('@ParameterDataType', 'NVARCHAR(258)') AS variable_datatype, + q.n.value('@ParameterCompiledValue', 'NVARCHAR(258)') AS compile_time_value +FROM #query_plan AS qp +JOIN ##BlitzCacheProcs AS b +ON (b.QueryType = 'adhoc' AND b.QueryHash = qp.QueryHash) +OR (b.QueryType <> 'adhoc' AND b.SqlHandle = qp.SqlHandle) +CROSS APPLY qp.query_plan.nodes('//p:QueryPlan/p:ParameterList/p:ColumnReference') AS q(n) +WHERE b.SPID = @@SPID +OPTION (RECOMPILE); + + +RAISERROR(N'Getting conversion info', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +INSERT #conversion_info ( SPID, QueryHash, SqlHandle, proc_name, expression ) +SELECT DISTINCT @@SPID, + qp.QueryHash, + qp.SqlHandle, + b.QueryType AS proc_name, + qq.c.value('@Expression', 'NVARCHAR(4000)') AS expression +FROM #query_plan AS qp +JOIN ##BlitzCacheProcs AS b +ON (b.QueryType = 'adhoc' AND b.QueryHash = qp.QueryHash) +OR (b.QueryType <> 'adhoc' AND b.SqlHandle = qp.SqlHandle) +CROSS APPLY qp.query_plan.nodes('//p:QueryPlan/p:Warnings/p:PlanAffectingConvert') AS qq(c) +WHERE qq.c.exist('@ConvertIssue[.="Seek Plan"]') = 1 + AND qp.QueryHash IS NOT NULL + AND b.implicit_conversions = 1 +AND b.SPID = @@SPID +OPTION (RECOMPILE); + + +RAISERROR(N'Parsing conversion info', 0, 1) WITH NOWAIT; +INSERT #stored_proc_info ( SPID, SqlHandle, QueryHash, proc_name, variable_name, variable_datatype, converted_column_name, column_name, converted_to, compile_time_value ) +SELECT @@SPID AS SPID, + ci.SqlHandle, + ci.QueryHash, + REPLACE(REPLACE(REPLACE(ci.proc_name, ')', ''), 'Statement (parent ', ''), 'Procedure or Function: ', '') AS proc_name, + CASE WHEN ci.at_charindex > 0 + AND ci.bracket_charindex > 0 + THEN SUBSTRING(ci.expression, ci.at_charindex, ci.bracket_charindex) + ELSE N'**no_variable**' + END AS variable_name, + N'**no_variable**' AS variable_datatype, + CASE WHEN ci.at_charindex = 0 + AND ci.comma_charindex > 0 + AND ci.second_comma_charindex > 0 + THEN SUBSTRING(ci.expression, ci.comma_charindex, ci.second_comma_charindex) + ELSE N'**no_column**' + END AS converted_column_name, + CASE WHEN ci.at_charindex = 0 + AND ci.equal_charindex > 0 + AND ci.convert_implicit_charindex = 0 + THEN SUBSTRING(ci.expression, ci.equal_charindex, 4000) + WHEN ci.at_charindex = 0 + AND (ci.equal_charindex -1) > 0 + AND ci.convert_implicit_charindex > 0 + THEN SUBSTRING(ci.expression, 0, ci.equal_charindex -1) + WHEN ci.at_charindex > 0 + AND ci.comma_charindex > 0 + AND ci.second_comma_charindex > 0 + THEN SUBSTRING(ci.expression, ci.comma_charindex, ci.second_comma_charindex) + ELSE N'**no_column **' + END AS column_name, + CASE WHEN ci.paren_charindex > 0 + AND ci.comma_paren_charindex > 0 + THEN SUBSTRING(ci.expression, ci.paren_charindex, ci.comma_paren_charindex) + END AS converted_to, + CASE WHEN ci.at_charindex = 0 + AND ci.convert_implicit_charindex = 0 + AND ci.proc_name = 'Statement' + THEN SUBSTRING(ci.expression, ci.equal_charindex, 4000) + ELSE '**idk_man**' + END AS compile_time_value +FROM #conversion_info AS ci +OPTION (RECOMPILE); + + +RAISERROR(N'Updating variables for inserted procs', 0, 1) WITH NOWAIT; +UPDATE sp +SET sp.variable_datatype = vi.variable_datatype, + sp.compile_time_value = vi.compile_time_value +FROM #stored_proc_info AS sp +JOIN #variable_info AS vi +ON (sp.proc_name = 'adhoc' AND sp.QueryHash = vi.QueryHash) +OR (sp.proc_name <> 'adhoc' AND sp.SqlHandle = vi.SqlHandle) +AND sp.variable_name = vi.variable_name +OPTION (RECOMPILE); + + +RAISERROR(N'Inserting variables for other procs', 0, 1) WITH NOWAIT; +INSERT #stored_proc_info + ( SPID, SqlHandle, QueryHash, variable_name, variable_datatype, compile_time_value, proc_name ) +SELECT vi.SPID, vi.SqlHandle, vi.QueryHash, vi.variable_name, vi.variable_datatype, vi.compile_time_value, REPLACE(REPLACE(REPLACE(vi.proc_name, ')', ''), 'Statement (parent ', ''), 'Procedure or Function: ', '') AS proc_name +FROM #variable_info AS vi +WHERE NOT EXISTS +( + SELECT * + FROM #stored_proc_info AS sp + WHERE (sp.proc_name = 'adhoc' AND sp.QueryHash = vi.QueryHash) + OR (sp.proc_name <> 'adhoc' AND sp.SqlHandle = vi.SqlHandle) +) +OPTION (RECOMPILE); + + +RAISERROR(N'Updating procs', 0, 1) WITH NOWAIT; +UPDATE s +SET s.variable_datatype = CASE WHEN s.variable_datatype LIKE '%(%)%' + THEN LEFT(s.variable_datatype, CHARINDEX('(', s.variable_datatype) - 1) + ELSE s.variable_datatype + END, + s.converted_to = CASE WHEN s.converted_to LIKE '%(%)%' + THEN LEFT(s.converted_to, CHARINDEX('(', s.converted_to) - 1) + ELSE s.converted_to + END, + s.compile_time_value = CASE WHEN s.compile_time_value LIKE '%(%)%' + THEN SUBSTRING(s.compile_time_value, + CHARINDEX('(', s.compile_time_value) + 1, + CHARINDEX(')', s.compile_time_value, CHARINDEX('(', s.compile_time_value) + 1) - 1 - CHARINDEX('(', s.compile_time_value) + ) + WHEN variable_datatype NOT IN ('bit', 'tinyint', 'smallint', 'int', 'bigint') + AND s.variable_datatype NOT LIKE '%binary%' + AND s.compile_time_value NOT LIKE 'N''%''' + AND s.compile_time_value NOT LIKE '''%''' + AND s.compile_time_value <> s.column_name + AND s.compile_time_value <> '**idk_man**' + THEN QUOTENAME(compile_time_value, '''') + ELSE s.compile_time_value + END +FROM #stored_proc_info AS s +OPTION (RECOMPILE); + + +RAISERROR(N'Updating SET options', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE s +SET set_options = set_options.ansi_set_options +FROM #stored_proc_info AS s +JOIN ( + SELECT x.SqlHandle, + N'SET ANSI_NULLS ' + CASE WHEN [ANSI_NULLS] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET ANSI_PADDING ' + CASE WHEN [ANSI_PADDING] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET ANSI_WARNINGS ' + CASE WHEN [ANSI_WARNINGS] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET ARITHABORT ' + CASE WHEN [ARITHABORT] = 'true' THEN N'ON ' ELSE N' OFF ' END + NCHAR(10) + + N'SET CONCAT_NULL_YIELDS_NULL ' + CASE WHEN [CONCAT_NULL_YIELDS_NULL] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET NUMERIC_ROUNDABORT ' + CASE WHEN [NUMERIC_ROUNDABORT] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET QUOTED_IDENTIFIER ' + CASE WHEN [QUOTED_IDENTIFIER] = 'true' THEN N'ON ' ELSE N'OFF ' + NCHAR(10) END AS [ansi_set_options] + FROM ( + SELECT + s.SqlHandle, + so.o.value('@ANSI_NULLS', 'NVARCHAR(20)') AS [ANSI_NULLS], + so.o.value('@ANSI_PADDING', 'NVARCHAR(20)') AS [ANSI_PADDING], + so.o.value('@ANSI_WARNINGS', 'NVARCHAR(20)') AS [ANSI_WARNINGS], + so.o.value('@ARITHABORT', 'NVARCHAR(20)') AS [ARITHABORT], + so.o.value('@CONCAT_NULL_YIELDS_NULL', 'NVARCHAR(20)') AS [CONCAT_NULL_YIELDS_NULL], + so.o.value('@NUMERIC_ROUNDABORT', 'NVARCHAR(20)') AS [NUMERIC_ROUNDABORT], + so.o.value('@QUOTED_IDENTIFIER', 'NVARCHAR(20)') AS [QUOTED_IDENTIFIER] + FROM #statements AS s + CROSS APPLY s.statement.nodes('//p:StatementSetOptions') AS so(o) + ) AS x +) AS set_options ON set_options.SqlHandle = s.SqlHandle +OPTION(RECOMPILE); + + +RAISERROR(N'Updating conversion XML', 0, 1) WITH NOWAIT; +WITH precheck AS ( +SELECT spi.SPID, + spi.SqlHandle, + spi.proc_name, + (SELECT + CASE WHEN spi.proc_name <> 'Statement' + THEN N'The stored procedure ' + spi.proc_name + ELSE N'This ad hoc statement' + END + + N' had the following implicit conversions: ' + + CHAR(10) + + STUFF(( + SELECT DISTINCT + @nl + + CASE WHEN spi2.variable_name <> N'**no_variable**' + THEN N'The variable ' + WHEN spi2.variable_name = N'**no_variable**' AND (spi2.column_name = spi2.converted_column_name OR spi2.column_name LIKE '%CONVERT_IMPLICIT%') + THEN N'The compiled value ' + WHEN spi2.column_name LIKE '%Expr%' + THEN 'The expression ' + ELSE N'The column ' + END + + CASE WHEN spi2.variable_name <> N'**no_variable**' + THEN spi2.variable_name + WHEN spi2.variable_name = N'**no_variable**' AND (spi2.column_name = spi2.converted_column_name OR spi2.column_name LIKE '%CONVERT_IMPLICIT%') + THEN spi2.compile_time_value + ELSE spi2.column_name + END + + N' has a data type of ' + + CASE WHEN spi2.variable_datatype = N'**no_variable**' THEN spi2.converted_to + ELSE spi2.variable_datatype + END + + N' which caused implicit conversion on the column ' + + CASE WHEN spi2.column_name LIKE N'%CONVERT_IMPLICIT%' + THEN spi2.converted_column_name + WHEN spi2.column_name = N'**no_column**' + THEN spi2.converted_column_name + WHEN spi2.converted_column_name = N'**no_column**' + THEN spi2.column_name + WHEN spi2.column_name <> spi2.converted_column_name + THEN spi2.converted_column_name + ELSE spi2.column_name + END + + CASE WHEN spi2.variable_name = N'**no_variable**' AND (spi2.column_name = spi2.converted_column_name OR spi2.column_name LIKE '%CONVERT_IMPLICIT%') + THEN N'' + WHEN spi2.column_name LIKE '%Expr%' + THEN N'' + WHEN spi2.compile_time_value NOT IN ('**declared in proc**', '**idk_man**') + AND spi2.compile_time_value <> spi2.column_name + THEN ' with the value ' + RTRIM(spi2.compile_time_value) + ELSE N'' + END + + '.' + FROM #stored_proc_info AS spi2 + WHERE spi.SqlHandle = spi2.SqlHandle + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + AS [processing-instruction(ClickMe)] FOR XML PATH(''), TYPE ) + AS implicit_conversion_info +FROM #stored_proc_info AS spi +GROUP BY spi.SPID, spi.SqlHandle, spi.proc_name +) +UPDATE b +SET b.implicit_conversion_info = pk.implicit_conversion_info +FROM ##BlitzCacheProcs AS b +JOIN precheck pk +ON pk.SqlHandle = b.SqlHandle +AND pk.SPID = b.SPID +OPTION (RECOMPILE); + + +RAISERROR(N'Updating cached parameter XML for stored procs', 0, 1) WITH NOWAIT; +WITH precheck AS ( +SELECT spi.SPID, + spi.SqlHandle, + spi.proc_name, + (SELECT + set_options + + @nl + + @nl + + N'EXEC ' + + spi.proc_name + + N' ' + + STUFF(( + SELECT DISTINCT N', ' + + CASE WHEN spi2.variable_name <> N'**no_variable**' AND spi2.compile_time_value <> N'**idk_man**' + THEN spi2.variable_name + N' = ' + ELSE @nl + N' We could not find any cached parameter values for this stored proc. ' + END + + CASE WHEN spi2.variable_name = N'**no_variable**' OR spi2.compile_time_value = N'**idk_man**' + THEN @nl + N'More info on possible reasons: https://www.brentozar.com/go/noplans ' + WHEN spi2.compile_time_value = N'NULL' + THEN spi2.compile_time_value + ELSE RTRIM(spi2.compile_time_value) + END + FROM #stored_proc_info AS spi2 + WHERE spi.SqlHandle = spi2.SqlHandle + AND spi2.proc_name <> N'Statement' + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + AS [processing-instruction(ClickMe)] FOR XML PATH(''), TYPE ) + AS cached_execution_parameters +FROM #stored_proc_info AS spi +GROUP BY spi.SPID, spi.SqlHandle, spi.proc_name, spi.set_options +) +UPDATE b +SET b.cached_execution_parameters = pk.cached_execution_parameters +FROM ##BlitzCacheProcs AS b +JOIN precheck pk +ON pk.SqlHandle = b.SqlHandle +AND pk.SPID = b.SPID +WHERE b.QueryType <> N'Statement' +OPTION (RECOMPILE); + + +RAISERROR(N'Updating cached parameter XML for statements', 0, 1) WITH NOWAIT; +WITH precheck AS ( +SELECT spi.SPID, + spi.SqlHandle, + spi.proc_name, + (SELECT + set_options + + @nl + + @nl + + N' See QueryText column for full query text' + + @nl + + @nl + + STUFF(( + SELECT DISTINCT N', ' + + CASE WHEN spi2.variable_name <> N'**no_variable**' AND spi2.compile_time_value <> N'**idk_man**' + THEN spi2.variable_name + N' = ' + ELSE @nl + N' We could not find any cached parameter values for this stored proc. ' + END + + CASE WHEN spi2.variable_name = N'**no_variable**' OR spi2.compile_time_value = N'**idk_man**' + THEN @nl + N' More info on possible reasons: https://www.brentozar.com/go/noplans ' + WHEN spi2.compile_time_value = N'NULL' + THEN spi2.compile_time_value + ELSE RTRIM(spi2.compile_time_value) + END + FROM #stored_proc_info AS spi2 + WHERE spi.SqlHandle = spi2.SqlHandle + AND spi2.proc_name = N'Statement' + AND spi2.variable_name NOT LIKE N'%msparam%' + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + AS [processing-instruction(ClickMe)] FOR XML PATH(''), TYPE ) + AS cached_execution_parameters +FROM #stored_proc_info AS spi +GROUP BY spi.SPID, spi.SqlHandle, spi.proc_name, spi.set_options +) +UPDATE b +SET b.cached_execution_parameters = pk.cached_execution_parameters +FROM ##BlitzCacheProcs AS b +JOIN precheck pk +ON pk.SqlHandle = b.SqlHandle +AND pk.SPID = b.SPID +WHERE b.QueryType = N'Statement' +OPTION (RECOMPILE); + +RAISERROR(N'Filling in implicit conversion and cached plan parameter info', 0, 1) WITH NOWAIT; +UPDATE b +SET b.implicit_conversion_info = CASE WHEN b.implicit_conversion_info IS NULL + OR CONVERT(NVARCHAR(MAX), b.implicit_conversion_info) = N'' + THEN '' + ELSE b.implicit_conversion_info END, + b.cached_execution_parameters = CASE WHEN b.cached_execution_parameters IS NULL + OR CONVERT(NVARCHAR(MAX), b.cached_execution_parameters) = N'' + THEN '' + ELSE b.cached_execution_parameters END +FROM ##BlitzCacheProcs AS b +WHERE b.SPID = @@SPID +OPTION (RECOMPILE); + +/*End implicit conversion and parameter info*/ + +/*Begin Missing Index*/ +IF EXISTS ( SELECT 1/0 + FROM ##BlitzCacheProcs AS bbcp + WHERE bbcp.missing_index_count > 0 + OR bbcp.index_spool_cost > 0 + OR bbcp.index_spool_rows > 0 + AND bbcp.SPID = @@SPID ) + + BEGIN + RAISERROR(N'Inserting to #missing_index_xml', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_xml + SELECT qp.QueryHash, + qp.SqlHandle, + c.mg.value('@Impact', 'FLOAT') AS Impact, + c.mg.query('.') AS cmg + FROM #query_plan AS qp + CROSS APPLY qp.query_plan.nodes('//p:MissingIndexes/p:MissingIndexGroup') AS c(mg) + WHERE qp.QueryHash IS NOT NULL + OPTION(RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_schema', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_schema + SELECT mix.QueryHash, mix.SqlHandle, mix.impact, + c.mi.value('@Database', 'NVARCHAR(128)'), + c.mi.value('@Schema', 'NVARCHAR(128)'), + c.mi.value('@Table', 'NVARCHAR(128)'), + c.mi.query('.') + FROM #missing_index_xml AS mix + CROSS APPLY mix.index_xml.nodes('//p:MissingIndex') AS c(mi) + OPTION(RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_usage', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_usage + SELECT ms.QueryHash, ms.SqlHandle, ms.impact, ms.database_name, ms.schema_name, ms.table_name, + c.cg.value('@Usage', 'NVARCHAR(128)'), + c.cg.query('.') + FROM #missing_index_schema ms + CROSS APPLY ms.index_xml.nodes('//p:ColumnGroup') AS c(cg) + OPTION(RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_detail', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_detail + SELECT miu.QueryHash, + miu.SqlHandle, + miu.impact, + miu.database_name, + miu.schema_name, + miu.table_name, + miu.usage, + c.c.value('@Name', 'NVARCHAR(128)') + FROM #missing_index_usage AS miu + CROSS APPLY miu.index_xml.nodes('//p:Column') AS c(c) + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to missing indexes to #missing_index_pretty', 0, 1) WITH NOWAIT; + INSERT #missing_index_pretty + ( QueryHash, SqlHandle, impact, database_name, schema_name, table_name, equality, inequality, include, executions, query_cost, creation_hours, is_spool ) + SELECT DISTINCT m.QueryHash, m.SqlHandle, m.impact, m.database_name, m.schema_name, m.table_name + , STUFF(( SELECT DISTINCT N', ' + ISNULL(m2.column_name, '') AS column_name + FROM #missing_index_detail AS m2 + WHERE m2.usage = 'EQUALITY' + AND m.QueryHash = m2.QueryHash + AND m.SqlHandle = m2.SqlHandle + AND m.impact = m2.impact + AND m.database_name = m2.database_name + AND m.schema_name = m2.schema_name + AND m.table_name = m2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS equality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(m2.column_name, '') AS column_name + FROM #missing_index_detail AS m2 + WHERE m2.usage = 'INEQUALITY' + AND m.QueryHash = m2.QueryHash + AND m.SqlHandle = m2.SqlHandle + AND m.impact = m2.impact + AND m.database_name = m2.database_name + AND m.schema_name = m2.schema_name + AND m.table_name = m2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS inequality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(m2.column_name, '') AS column_name + FROM #missing_index_detail AS m2 + WHERE m2.usage = 'INCLUDE' + AND m.QueryHash = m2.QueryHash + AND m.SqlHandle = m2.SqlHandle + AND m.impact = m2.impact + AND m.database_name = m2.database_name + AND m.schema_name = m2.schema_name + AND m.table_name = m2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS [include], + bbcp.ExecutionCount, + bbcp.QueryPlanCost, + bbcp.PlanCreationTimeHours, + 0 as is_spool + FROM #missing_index_detail AS m + JOIN ##BlitzCacheProcs AS bbcp + ON m.SqlHandle = bbcp.SqlHandle + AND m.QueryHash = bbcp.QueryHash + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to #index_spool_ugly', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + INSERT #index_spool_ugly + (QueryHash, SqlHandle, impact, database_name, schema_name, table_name, equality, inequality, include, executions, query_cost, creation_hours) + SELECT p.QueryHash, + p.SqlHandle, + (c.n.value('@EstimateIO', 'FLOAT') + (c.n.value('@EstimateCPU', 'FLOAT'))) + / ( 1 * NULLIF(p.QueryPlanCost, 0)) * 100 AS impact, + o.n.value('@Database', 'NVARCHAR(128)') AS output_database, + o.n.value('@Schema', 'NVARCHAR(128)') AS output_schema, + o.n.value('@Table', 'NVARCHAR(128)') AS output_table, + k.n.value('@Column', 'NVARCHAR(128)') AS range_column, + e.n.value('@Column', 'NVARCHAR(128)') AS expression_column, + o.n.value('@Column', 'NVARCHAR(128)') AS output_column, + p.ExecutionCount, + p.QueryPlanCost, + p.PlanCreationTimeHours + FROM #relop AS r + JOIN ##BlitzCacheProcs p + ON p.QueryHash = r.QueryHash + CROSS APPLY r.relop.nodes('/p:RelOp') AS c(n) + CROSS APPLY r.relop.nodes('/p:RelOp/p:OutputList/p:ColumnReference') AS o(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:Spool/p:SeekPredicateNew/p:SeekKeys/p:Prefix/p:RangeColumns/p:ColumnReference') AS k(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:Spool/p:SeekPredicateNew/p:SeekKeys/p:Prefix/p:RangeExpressions/p:ColumnReference') AS e(n) + WHERE r.relop.exist('/p:RelOp[@PhysicalOp="Index Spool" and @LogicalOp="Eager Spool"]') = 1 + + RAISERROR(N'Inserting to spools to #missing_index_pretty', 0, 1) WITH NOWAIT; + INSERT #missing_index_pretty + (QueryHash, SqlHandle, impact, database_name, schema_name, table_name, equality, inequality, include, executions, query_cost, creation_hours, is_spool) + SELECT DISTINCT + isu.QueryHash, + isu.SqlHandle, + isu.impact, + isu.database_name, + isu.schema_name, + isu.table_name + , STUFF(( SELECT DISTINCT N', ' + ISNULL(isu2.equality, '') AS column_name + FROM #index_spool_ugly AS isu2 + WHERE isu2.equality IS NOT NULL + AND isu.QueryHash = isu2.QueryHash + AND isu.SqlHandle = isu2.SqlHandle + AND isu.impact = isu2.impact + AND isu.database_name = isu2.database_name + AND isu.schema_name = isu2.schema_name + AND isu.table_name = isu2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS equality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(isu2.inequality, '') AS column_name + FROM #index_spool_ugly AS isu2 + WHERE isu2.inequality IS NOT NULL + AND isu.QueryHash = isu2.QueryHash + AND isu.SqlHandle = isu2.SqlHandle + AND isu.impact = isu2.impact + AND isu.database_name = isu2.database_name + AND isu.schema_name = isu2.schema_name + AND isu.table_name = isu2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS inequality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(isu2.include, '') AS column_name + FROM #index_spool_ugly AS isu2 + WHERE isu2.include IS NOT NULL + AND isu.QueryHash = isu2.QueryHash + AND isu.SqlHandle = isu2.SqlHandle + AND isu.impact = isu2.impact + AND isu.database_name = isu2.database_name + AND isu.schema_name = isu2.schema_name + AND isu.table_name = isu2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS include, + isu.executions, + isu.query_cost, + isu.creation_hours, + 1 AS is_spool + FROM #index_spool_ugly AS isu + + + RAISERROR(N'Updating missing index information', 0, 1) WITH NOWAIT; + WITH missing AS ( + SELECT DISTINCT + mip.QueryHash, + mip.SqlHandle, + mip.executions, + N'' + AS full_details + FROM #missing_index_pretty AS mip + ) + UPDATE bbcp + SET bbcp.missing_indexes = m.full_details + FROM ##BlitzCacheProcs AS bbcp + JOIN missing AS m + ON m.SqlHandle = bbcp.SqlHandle + AND m.QueryHash = bbcp.QueryHash + AND m.executions = bbcp.ExecutionCount + AND SPID = @@SPID + OPTION (RECOMPILE); + + END; + + RAISERROR(N'Filling in missing index blanks', 0, 1) WITH NOWAIT; + UPDATE b + SET b.missing_indexes = + CASE WHEN b.missing_indexes IS NULL + THEN '' + ELSE b.missing_indexes + END + FROM ##BlitzCacheProcs AS b + WHERE b.SPID = @@SPID + OPTION (RECOMPILE); + +/*End Missing Index*/ + + +/* Set configuration values */ +RAISERROR(N'Setting configuration values', 0, 1) WITH NOWAIT; +DECLARE @execution_threshold INT = 1000 , + @parameter_sniffing_warning_pct TINYINT = 30, + /* This is in average reads */ + @parameter_sniffing_io_threshold BIGINT = 100000 , + @ctp_threshold_pct TINYINT = 10, + @long_running_query_warning_seconds BIGINT = 300 * 1000 , + @memory_grant_warning_percent INT = 10; + +IF EXISTS (SELECT 1/0 FROM #configuration WHERE 'frequent execution threshold' = LOWER(parameter_name)) +BEGIN + SELECT @execution_threshold = CAST(value AS INT) + FROM #configuration + WHERE 'frequent execution threshold' = LOWER(parameter_name) ; + + SET @msg = ' Setting "frequent execution threshold" to ' + CAST(@execution_threshold AS VARCHAR(10)) ; + + RAISERROR(@msg, 0, 1) WITH NOWAIT; +END; + +IF EXISTS (SELECT 1/0 FROM #configuration WHERE 'parameter sniffing variance percent' = LOWER(parameter_name)) +BEGIN + SELECT @parameter_sniffing_warning_pct = CAST(value AS TINYINT) + FROM #configuration + WHERE 'parameter sniffing variance percent' = LOWER(parameter_name) ; + + SET @msg = ' Setting "parameter sniffing variance percent" to ' + CAST(@parameter_sniffing_warning_pct AS VARCHAR(3)) ; + + RAISERROR(@msg, 0, 1) WITH NOWAIT; +END; + +IF EXISTS (SELECT 1/0 FROM #configuration WHERE 'parameter sniffing io threshold' = LOWER(parameter_name)) +BEGIN + SELECT @parameter_sniffing_io_threshold = CAST(value AS BIGINT) + FROM #configuration + WHERE 'parameter sniffing io threshold' = LOWER(parameter_name) ; + + SET @msg = ' Setting "parameter sniffing io threshold" to ' + CAST(@parameter_sniffing_io_threshold AS VARCHAR(10)); + + RAISERROR(@msg, 0, 1) WITH NOWAIT; +END; + +IF EXISTS (SELECT 1/0 FROM #configuration WHERE 'cost threshold for parallelism warning' = LOWER(parameter_name)) +BEGIN + SELECT @ctp_threshold_pct = CAST(value AS TINYINT) + FROM #configuration + WHERE 'cost threshold for parallelism warning' = LOWER(parameter_name) ; + + SET @msg = ' Setting "cost threshold for parallelism warning" to ' + CAST(@ctp_threshold_pct AS VARCHAR(3)); + + RAISERROR(@msg, 0, 1) WITH NOWAIT; +END; + +IF EXISTS (SELECT 1/0 FROM #configuration WHERE 'long running query warning (seconds)' = LOWER(parameter_name)) +BEGIN + SELECT @long_running_query_warning_seconds = CAST(value * 1000 AS BIGINT) + FROM #configuration + WHERE 'long running query warning (seconds)' = LOWER(parameter_name) ; + + SET @msg = ' Setting "long running query warning (seconds)" to ' + CAST(@long_running_query_warning_seconds AS VARCHAR(10)); + + RAISERROR(@msg, 0, 1) WITH NOWAIT; +END; + +IF EXISTS (SELECT 1/0 FROM #configuration WHERE 'unused memory grant' = LOWER(parameter_name)) +BEGIN + SELECT @memory_grant_warning_percent = CAST(value AS INT) + FROM #configuration + WHERE 'unused memory grant' = LOWER(parameter_name) ; + + SET @msg = ' Setting "unused memory grant" to ' + CAST(@memory_grant_warning_percent AS VARCHAR(10)); + + RAISERROR(@msg, 0, 1) WITH NOWAIT; +END; + +DECLARE @ctp INT ; + +SELECT @ctp = NULLIF(CAST(value AS INT), 0) +FROM sys.configurations +WHERE name = 'cost threshold for parallelism' +OPTION (RECOMPILE); + + +/* Update to populate checks columns */ +RAISERROR('Checking for query level SQL Server issues.', 0, 1) WITH NOWAIT; + +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE ##BlitzCacheProcs +SET frequent_execution = CASE WHEN ExecutionsPerMinute > @execution_threshold THEN 1 END , + parameter_sniffing = CASE WHEN ExecutionCount > 3 AND AverageReads > @parameter_sniffing_io_threshold + AND min_worker_time < ((1.0 - (@parameter_sniffing_warning_pct / 100.0)) * AverageCPU) THEN 1 + WHEN ExecutionCount > 3 AND AverageReads > @parameter_sniffing_io_threshold + AND max_worker_time > ((1.0 + (@parameter_sniffing_warning_pct / 100.0)) * AverageCPU) THEN 1 + WHEN ExecutionCount > 3 AND AverageReads > @parameter_sniffing_io_threshold + AND MinReturnedRows < ((1.0 - (@parameter_sniffing_warning_pct / 100.0)) * AverageReturnedRows) THEN 1 + WHEN ExecutionCount > 3 AND AverageReads > @parameter_sniffing_io_threshold + AND MaxReturnedRows > ((1.0 + (@parameter_sniffing_warning_pct / 100.0)) * AverageReturnedRows) THEN 1 END , + near_parallel = CASE WHEN is_parallel <> 1 AND QueryPlanCost BETWEEN @ctp * (1 - (@ctp_threshold_pct / 100.0)) AND @ctp THEN 1 END, + long_running = CASE WHEN AverageDuration > @long_running_query_warning_seconds THEN 1 + WHEN max_worker_time > @long_running_query_warning_seconds THEN 1 + WHEN max_elapsed_time > @long_running_query_warning_seconds THEN 1 END, + is_key_lookup_expensive = CASE WHEN QueryPlanCost >= (@ctp / 2) AND key_lookup_cost >= QueryPlanCost * .5 THEN 1 END, + is_sort_expensive = CASE WHEN QueryPlanCost >= (@ctp / 2) AND sort_cost >= QueryPlanCost * .5 THEN 1 END, + is_remote_query_expensive = CASE WHEN remote_query_cost >= QueryPlanCost * .05 THEN 1 END, + is_unused_grant = CASE WHEN PercentMemoryGrantUsed <= @memory_grant_warning_percent AND MinGrantKB > @MinMemoryPerQuery THEN 1 END, + long_running_low_cpu = CASE WHEN AverageDuration > AverageCPU * 4 AND AverageCPU < 500. THEN 1 END, + low_cost_high_cpu = CASE WHEN QueryPlanCost <= 10 AND AverageCPU > 5000. THEN 1 END, + is_spool_expensive = CASE WHEN QueryPlanCost > (@ctp / 5) AND index_spool_cost >= QueryPlanCost * .1 THEN 1 END, + is_spool_more_rows = CASE WHEN index_spool_rows >= (AverageReturnedRows / ISNULL(NULLIF(ExecutionCount, 0), 1)) THEN 1 END, + is_table_spool_expensive = CASE WHEN QueryPlanCost > (@ctp / 5) AND table_spool_cost >= QueryPlanCost / 4 THEN 1 END, + is_table_spool_more_rows = CASE WHEN table_spool_rows >= (AverageReturnedRows / ISNULL(NULLIF(ExecutionCount, 0), 1)) THEN 1 END, + is_bad_estimate = CASE WHEN AverageReturnedRows > 0 AND (estimated_rows * 1000 < AverageReturnedRows OR estimated_rows > AverageReturnedRows * 1000) THEN 1 END, + is_big_spills = CASE WHEN (AvgSpills / 128.) > 499. THEN 1 END +WHERE SPID = @@SPID +OPTION (RECOMPILE); + + + +RAISERROR('Checking for forced parameterization and cursors.', 0, 1) WITH NOWAIT; + +/* Set options checks */ +UPDATE p + SET is_forced_parameterized = CASE WHEN (CAST(pa.value AS INT) & 131072 = 131072) THEN 1 END , + is_forced_plan = CASE WHEN (CAST(pa.value AS INT) & 4 = 4) THEN 1 END , + SetOptions = SUBSTRING( + CASE WHEN (CAST(pa.value AS INT) & 1 = 1) THEN ', ANSI_PADDING' ELSE '' END + + CASE WHEN (CAST(pa.value AS INT) & 8 = 8) THEN ', CONCAT_NULL_YIELDS_NULL' ELSE '' END + + CASE WHEN (CAST(pa.value AS INT) & 16 = 16) THEN ', ANSI_WARNINGS' ELSE '' END + + CASE WHEN (CAST(pa.value AS INT) & 32 = 32) THEN ', ANSI_NULLS' ELSE '' END + + CASE WHEN (CAST(pa.value AS INT) & 64 = 64) THEN ', QUOTED_IDENTIFIER' ELSE '' END + + CASE WHEN (CAST(pa.value AS INT) & 4096 = 4096) THEN ', ARITH_ABORT' ELSE '' END + + CASE WHEN (CAST(pa.value AS INT) & 8192 = 8191) THEN ', NUMERIC_ROUNDABORT' ELSE '' END + , 2, 200000) +FROM ##BlitzCacheProcs p + CROSS APPLY sys.dm_exec_plan_attributes(p.PlanHandle) pa +WHERE pa.attribute = 'set_options' +AND SPID = @@SPID +OPTION (RECOMPILE); + + +/* Cursor checks */ +UPDATE p +SET is_cursor = CASE WHEN CAST(pa.value AS INT) <> 0 THEN 1 END +FROM ##BlitzCacheProcs p + CROSS APPLY sys.dm_exec_plan_attributes(p.PlanHandle) pa +WHERE pa.attribute LIKE '%cursor%' +AND SPID = @@SPID +OPTION (RECOMPILE); + +UPDATE p +SET is_cursor = 1 +FROM ##BlitzCacheProcs p +WHERE QueryHash = 0x0000000000000000 +OR QueryPlanHash = 0x0000000000000000 +AND SPID = @@SPID +OPTION (RECOMPILE); + + + +RAISERROR('Populating Warnings column', 0, 1) WITH NOWAIT; +/* Populate warnings */ +UPDATE ##BlitzCacheProcs +SET Warnings = SUBSTRING( + CASE WHEN warning_no_join_predicate = 1 THEN ', No Join Predicate' ELSE '' END + + CASE WHEN compile_timeout = 1 THEN ', Compilation Timeout' ELSE '' END + + CASE WHEN compile_memory_limit_exceeded = 1 THEN ', Compile Memory Limit Exceeded' ELSE '' END + + CASE WHEN busy_loops = 1 THEN ', Busy Loops' ELSE '' END + + CASE WHEN is_forced_plan = 1 THEN ', Forced Plan' ELSE '' END + + CASE WHEN is_forced_parameterized = 1 THEN ', Forced Parameterization' ELSE '' END + + CASE WHEN unparameterized_query = 1 THEN ', Unparameterized Query' ELSE '' END + + CASE WHEN missing_index_count > 0 THEN ', Missing Indexes (' + CAST(missing_index_count AS VARCHAR(3)) + ')' ELSE '' END + + CASE WHEN unmatched_index_count > 0 THEN ', Unmatched Indexes (' + CAST(unmatched_index_count AS VARCHAR(3)) + ')' ELSE '' END + + CASE WHEN is_cursor = 1 THEN ', Cursor' + + CASE WHEN is_optimistic_cursor = 1 THEN '; optimistic' ELSE '' END + + CASE WHEN is_forward_only_cursor = 0 THEN '; not forward only' ELSE '' END + + CASE WHEN is_cursor_dynamic = 1 THEN '; dynamic' ELSE '' END + + CASE WHEN is_fast_forward_cursor = 1 THEN '; fast forward' ELSE '' END + ELSE '' END + + CASE WHEN is_parallel = 1 THEN ', Parallel' ELSE '' END + + CASE WHEN near_parallel = 1 THEN ', Nearly Parallel' ELSE '' END + + CASE WHEN frequent_execution = 1 THEN ', Frequent Execution' ELSE '' END + + CASE WHEN plan_warnings = 1 THEN ', Plan Warnings' ELSE '' END + + CASE WHEN parameter_sniffing = 1 THEN ', Parameter Sniffing' ELSE '' END + + CASE WHEN long_running = 1 THEN ', Long Running Query' ELSE '' END + + CASE WHEN downlevel_estimator = 1 THEN ', Downlevel CE' ELSE '' END + + CASE WHEN implicit_conversions = 1 THEN ', Implicit Conversions' ELSE '' END + + CASE WHEN tvf_join = 1 THEN ', Function Join' ELSE '' END + + CASE WHEN plan_multiple_plans > 0 THEN ', Multiple Plans' + COALESCE(' (' + CAST(plan_multiple_plans AS VARCHAR(10)) + ')', '') ELSE '' END + + CASE WHEN is_trivial = 1 THEN ', Trivial Plans' ELSE '' END + + CASE WHEN is_forced_serial = 1 THEN ', Forced Serialization' ELSE '' END + + CASE WHEN is_key_lookup_expensive = 1 THEN ', Expensive Key Lookup' ELSE '' END + + CASE WHEN is_remote_query_expensive = 1 THEN ', Expensive Remote Query' ELSE '' END + + CASE WHEN trace_flags_session IS NOT NULL THEN ', Session Level Trace Flag(s) Enabled: ' + trace_flags_session ELSE '' END + + CASE WHEN is_unused_grant = 1 THEN ', Unused Memory Grant' ELSE '' END + + CASE WHEN function_count > 0 THEN ', Calls ' + CONVERT(VARCHAR(10), function_count) + ' Function(s)' ELSE '' END + + CASE WHEN clr_function_count > 0 THEN ', Calls ' + CONVERT(VARCHAR(10), clr_function_count) + ' CLR Function(s)' ELSE '' END + + CASE WHEN PlanCreationTimeHours <= 4 THEN ', Plan created last 4hrs' ELSE '' END + + CASE WHEN is_table_variable = 1 THEN ', Table Variables' ELSE '' END + + CASE WHEN no_stats_warning = 1 THEN ', Columns With No Statistics' ELSE '' END + + CASE WHEN relop_warnings = 1 THEN ', Operator Warnings' ELSE '' END + + CASE WHEN is_table_scan = 1 THEN ', Table Scans (Heaps)' ELSE '' END + + CASE WHEN backwards_scan = 1 THEN ', Backwards Scans' ELSE '' END + + CASE WHEN forced_index = 1 THEN ', Forced Indexes' ELSE '' END + + CASE WHEN forced_seek = 1 THEN ', Forced Seeks' ELSE '' END + + CASE WHEN forced_scan = 1 THEN ', Forced Scans' ELSE '' END + + CASE WHEN columnstore_row_mode = 1 THEN ', ColumnStore Row Mode ' ELSE '' END + + CASE WHEN is_computed_scalar = 1 THEN ', Computed Column UDF ' ELSE '' END + + CASE WHEN is_sort_expensive = 1 THEN ', Expensive Sort' ELSE '' END + + CASE WHEN is_computed_filter = 1 THEN ', Filter UDF' ELSE '' END + + CASE WHEN index_ops >= 5 THEN ', >= 5 Indexes Modified' ELSE '' END + + CASE WHEN is_row_level = 1 THEN ', Row Level Security' ELSE '' END + + CASE WHEN is_spatial = 1 THEN ', Spatial Index' ELSE '' END + + CASE WHEN index_dml = 1 THEN ', Index DML' ELSE '' END + + CASE WHEN table_dml = 1 THEN ', Table DML' ELSE '' END + + CASE WHEN low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END + + CASE WHEN long_running_low_cpu = 1 THEN + ', Long Running With Low CPU' ELSE '' END + + CASE WHEN stale_stats = 1 THEN + ', Statistics used have > 100k modifications in the last 7 days' ELSE '' END + + CASE WHEN is_adaptive = 1 THEN + ', Adaptive Joins' ELSE '' END + + CASE WHEN is_spool_expensive = 1 THEN + ', Expensive Index Spool' ELSE '' END + + CASE WHEN is_spool_more_rows = 1 THEN + ', Large Index Row Spool' ELSE '' END + + CASE WHEN is_table_spool_expensive = 1 THEN + ', Expensive Table Spool' ELSE '' END + + CASE WHEN is_table_spool_more_rows = 1 THEN + ', Many Rows Table Spool' ELSE '' END + + CASE WHEN is_bad_estimate = 1 THEN + ', Row Estimate Mismatch' ELSE '' END + + CASE WHEN is_paul_white_electric = 1 THEN ', SWITCH!' ELSE '' END + + CASE WHEN is_row_goal = 1 THEN ', Row Goals' ELSE '' END + + CASE WHEN is_big_spills = 1 THEN ', >500mb Spills' ELSE '' END + + CASE WHEN is_mstvf = 1 THEN ', MSTVFs' ELSE '' END + + CASE WHEN is_mm_join = 1 THEN ', Many to Many Merge' ELSE '' END + + CASE WHEN is_nonsargable = 1 THEN ', non-SARGables' ELSE '' END + + CASE WHEN CompileTime > 5000 THEN ', Long Compile Time' ELSE '' END + + CASE WHEN CompileCPU > 5000 THEN ', High Compile CPU' ELSE '' END + + CASE WHEN CompileMemory > 1024 AND ((CompileMemory) / (1 * CASE WHEN MaxCompileMemory = 0 THEN 1 ELSE MaxCompileMemory END) * 100.) >= 10. THEN ', High Compile Memory' ELSE '' END + + CASE WHEN select_with_writes > 0 THEN ', Select w/ Writes' ELSE '' END + , 3, 200000) +WHERE SPID = @@SPID +OPTION (RECOMPILE); + + +RAISERROR('Populating Warnings column for stored procedures', 0, 1) WITH NOWAIT; +WITH statement_warnings AS + ( +SELECT DISTINCT + SqlHandle, + Warnings = SUBSTRING( + CASE WHEN warning_no_join_predicate = 1 THEN ', No Join Predicate' ELSE '' END + + CASE WHEN compile_timeout = 1 THEN ', Compilation Timeout' ELSE '' END + + CASE WHEN compile_memory_limit_exceeded = 1 THEN ', Compile Memory Limit Exceeded' ELSE '' END + + CASE WHEN busy_loops = 1 THEN ', Busy Loops' ELSE '' END + + CASE WHEN is_forced_plan = 1 THEN ', Forced Plan' ELSE '' END + + CASE WHEN is_forced_parameterized = 1 THEN ', Forced Parameterization' ELSE '' END + + --CASE WHEN unparameterized_query = 1 THEN ', Unparameterized Query' ELSE '' END + + CASE WHEN missing_index_count > 0 THEN ', Missing Indexes (' + CONVERT(VARCHAR(10), (SELECT SUM(b2.missing_index_count) FROM ##BlitzCacheProcs AS b2 WHERE b2.SqlHandle = b.SqlHandle AND b2.QueryHash IS NOT NULL AND SPID = @@SPID) ) + ')' ELSE '' END + + CASE WHEN unmatched_index_count > 0 THEN ', Unmatched Indexes (' + CONVERT(VARCHAR(10), (SELECT SUM(b2.unmatched_index_count) FROM ##BlitzCacheProcs AS b2 WHERE b2.SqlHandle = b.SqlHandle AND b2.QueryHash IS NOT NULL AND SPID = @@SPID) ) + ')' ELSE '' END + + CASE WHEN is_cursor = 1 THEN ', Cursor' + + CASE WHEN is_optimistic_cursor = 1 THEN '; optimistic' ELSE '' END + + CASE WHEN is_forward_only_cursor = 0 THEN '; not forward only' ELSE '' END + + CASE WHEN is_cursor_dynamic = 1 THEN '; dynamic' ELSE '' END + + CASE WHEN is_fast_forward_cursor = 1 THEN '; fast forward' ELSE '' END + ELSE '' END + + CASE WHEN is_parallel = 1 THEN ', Parallel' ELSE '' END + + CASE WHEN near_parallel = 1 THEN ', Nearly Parallel' ELSE '' END + + CASE WHEN frequent_execution = 1 THEN ', Frequent Execution' ELSE '' END + + CASE WHEN plan_warnings = 1 THEN ', Plan Warnings' ELSE '' END + + CASE WHEN parameter_sniffing = 1 THEN ', Parameter Sniffing' ELSE '' END + + CASE WHEN long_running = 1 THEN ', Long Running Query' ELSE '' END + + CASE WHEN downlevel_estimator = 1 THEN ', Downlevel CE' ELSE '' END + + CASE WHEN implicit_conversions = 1 THEN ', Implicit Conversions' ELSE '' END + + CASE WHEN tvf_join = 1 THEN ', Function Join' ELSE '' END + + CASE WHEN plan_multiple_plans > 0 THEN ', Multiple Plans' + COALESCE(' (' + CAST(plan_multiple_plans AS VARCHAR(10)) + ')', '') ELSE '' END + + CASE WHEN is_trivial = 1 THEN ', Trivial Plans' ELSE '' END + + CASE WHEN is_forced_serial = 1 THEN ', Forced Serialization' ELSE '' END + + CASE WHEN is_key_lookup_expensive = 1 THEN ', Expensive Key Lookup' ELSE '' END + + CASE WHEN is_remote_query_expensive = 1 THEN ', Expensive Remote Query' ELSE '' END + + CASE WHEN trace_flags_session IS NOT NULL THEN ', Session Level Trace Flag(s) Enabled: ' + trace_flags_session ELSE '' END + + CASE WHEN is_unused_grant = 1 THEN ', Unused Memory Grant' ELSE '' END + + CASE WHEN function_count > 0 THEN ', Calls ' + CONVERT(VARCHAR(10), (SELECT SUM(b2.function_count) FROM ##BlitzCacheProcs AS b2 WHERE b2.SqlHandle = b.SqlHandle AND b2.QueryHash IS NOT NULL AND SPID = @@SPID) ) + ' Function(s)' ELSE '' END + + CASE WHEN clr_function_count > 0 THEN ', Calls ' + CONVERT(VARCHAR(10), (SELECT SUM(b2.clr_function_count) FROM ##BlitzCacheProcs AS b2 WHERE b2.SqlHandle = b.SqlHandle AND b2.QueryHash IS NOT NULL AND SPID = @@SPID) ) + ' CLR Function(s)' ELSE '' END + + CASE WHEN PlanCreationTimeHours <= 4 THEN ', Plan created last 4hrs' ELSE '' END + + CASE WHEN is_table_variable = 1 THEN ', Table Variables' ELSE '' END + + CASE WHEN no_stats_warning = 1 THEN ', Columns With No Statistics' ELSE '' END + + CASE WHEN relop_warnings = 1 THEN ', Operator Warnings' ELSE '' END + + CASE WHEN is_table_scan = 1 THEN ', Table Scans' ELSE '' END + + CASE WHEN backwards_scan = 1 THEN ', Backwards Scans' ELSE '' END + + CASE WHEN forced_index = 1 THEN ', Forced Indexes' ELSE '' END + + CASE WHEN forced_seek = 1 THEN ', Forced Seeks' ELSE '' END + + CASE WHEN forced_scan = 1 THEN ', Forced Scans' ELSE '' END + + CASE WHEN columnstore_row_mode = 1 THEN ', ColumnStore Row Mode ' ELSE '' END + + CASE WHEN is_computed_scalar = 1 THEN ', Computed Column UDF ' ELSE '' END + + CASE WHEN is_sort_expensive = 1 THEN ', Expensive Sort' ELSE '' END + + CASE WHEN is_computed_filter = 1 THEN ', Filter UDF' ELSE '' END + + CASE WHEN index_ops >= 5 THEN ', >= 5 Indexes Modified' ELSE '' END + + CASE WHEN is_row_level = 1 THEN ', Row Level Security' ELSE '' END + + CASE WHEN is_spatial = 1 THEN ', Spatial Index' ELSE '' END + + CASE WHEN index_dml = 1 THEN ', Index DML' ELSE '' END + + CASE WHEN table_dml = 1 THEN ', Table DML' ELSE '' END + + CASE WHEN low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END + + CASE WHEN long_running_low_cpu = 1 THEN + ', Long Running With Low CPU' ELSE '' END + + CASE WHEN stale_stats = 1 THEN + ', Statistics used have > 100k modifications in the last 7 days' ELSE '' END + + CASE WHEN is_adaptive = 1 THEN + ', Adaptive Joins' ELSE '' END + + CASE WHEN is_spool_expensive = 1 THEN + ', Expensive Index Spool' ELSE '' END + + CASE WHEN is_spool_more_rows = 1 THEN + ', Large Index Row Spool' ELSE '' END + + CASE WHEN is_table_spool_expensive = 1 THEN + ', Expensive Table Spool' ELSE '' END + + CASE WHEN is_table_spool_more_rows = 1 THEN + ', Many Rows Table Spool' ELSE '' END + + CASE WHEN is_bad_estimate = 1 THEN + ', Row Estimate Mismatch' ELSE '' END + + CASE WHEN is_paul_white_electric = 1 THEN ', SWITCH!' ELSE '' END + + CASE WHEN is_row_goal = 1 THEN ', Row Goals' ELSE '' END + + CASE WHEN is_big_spills = 1 THEN ', >500mb spills' ELSE '' END + + CASE WHEN is_mstvf = 1 THEN ', MSTVFs' ELSE '' END + + CASE WHEN is_mm_join = 1 THEN ', Many to Many Merge' ELSE '' END + + CASE WHEN is_nonsargable = 1 THEN ', non-SARGables' ELSE '' END + + CASE WHEN CompileTime > 5000 THEN ', Long Compile Time' ELSE '' END + + CASE WHEN CompileCPU > 5000 THEN ', High Compile CPU' ELSE '' END + + CASE WHEN CompileMemory > 1024 AND ((CompileMemory) / (1 * CASE WHEN MaxCompileMemory = 0 THEN 1 ELSE MaxCompileMemory END) * 100.) >= 10. THEN ', High Compile Memory' ELSE '' END + + CASE WHEN select_with_writes > 0 THEN ', Select w/ Writes' ELSE '' END + , 3, 200000) +FROM ##BlitzCacheProcs b +WHERE SPID = @@SPID +AND QueryType LIKE 'Statement (parent%' + ) +UPDATE b +SET b.Warnings = s.Warnings +FROM ##BlitzCacheProcs AS b +JOIN statement_warnings s +ON b.SqlHandle = s.SqlHandle +WHERE QueryType LIKE 'Procedure or Function%' +AND SPID = @@SPID +OPTION (RECOMPILE); + +RAISERROR('Checking for plans with >128 levels of nesting', 0, 1) WITH NOWAIT; +WITH plan_handle AS ( +SELECT b.PlanHandle +FROM ##BlitzCacheProcs b + CROSS APPLY sys.dm_exec_text_query_plan(b.PlanHandle, 0, -1) tqp + CROSS APPLY sys.dm_exec_query_plan(b.PlanHandle) qp + WHERE tqp.encrypted = 0 + AND b.SPID = @@SPID + AND (qp.query_plan IS NULL + AND tqp.query_plan IS NOT NULL) +) +UPDATE b +SET Warnings = ISNULL('Your query plan is >128 levels of nested nodes, and can''t be converted to XML. Use SELECT * FROM sys.dm_exec_text_query_plan('+ CONVERT(VARCHAR(128), ph.PlanHandle, 1) + ', 0, -1) to get more information' + , 'We couldn''t find a plan for this query. More info on possible reasons: https://www.brentozar.com/go/noplans') +FROM ##BlitzCacheProcs b +LEFT JOIN plan_handle ph ON +b.PlanHandle = ph.PlanHandle +WHERE b.QueryPlan IS NULL +AND b.SPID = @@SPID +OPTION (RECOMPILE); + +RAISERROR('Checking for plans with no warnings', 0, 1) WITH NOWAIT; +UPDATE ##BlitzCacheProcs +SET Warnings = 'No warnings detected. ' + CASE @ExpertMode + WHEN 0 + THEN ' Try running sp_BlitzCache with @ExpertMode = 1 to find more advanced problems.' + ELSE '' + END +WHERE Warnings = '' OR Warnings IS NULL +AND SPID = @@SPID +OPTION (RECOMPILE); + + +Results: +IF @ExportToExcel = 1 +BEGIN + RAISERROR('Displaying results with Excel formatting (no plans).', 0, 1) WITH NOWAIT; + + /* excel output */ + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),' ','<>'),'><',''),'<>',' '), 1, 32000) + OPTION(RECOMPILE); + + SET @sql = N' + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT TOP (@Top) + DatabaseName AS [Database Name], + QueryPlanCost AS [Cost], + QueryText, + QueryType AS [Query Type], + Warnings, + ExecutionCount, + ExecutionsPerMinute AS [Executions / Minute], + PercentExecutions AS [Execution Weight], + PercentExecutionsByType AS [% Executions (Type)], + SerialDesiredMemory AS [Serial Desired Memory], + SerialRequiredMemory AS [Serial Required Memory], + TotalCPU AS [Total CPU (ms)], + AverageCPU AS [Avg CPU (ms)], + PercentCPU AS [CPU Weight], + PercentCPUByType AS [% CPU (Type)], + TotalDuration AS [Total Duration (ms)], + AverageDuration AS [Avg Duration (ms)], + PercentDuration AS [Duration Weight], + PercentDurationByType AS [% Duration (Type)], + TotalReads AS [Total Reads], + AverageReads AS [Average Reads], + PercentReads AS [Read Weight], + PercentReadsByType AS [% Reads (Type)], + TotalWrites AS [Total Writes], + AverageWrites AS [Average Writes], + PercentWrites AS [Write Weight], + PercentWritesByType AS [% Writes (Type)], + TotalReturnedRows, + AverageReturnedRows, + MinReturnedRows, + MaxReturnedRows, + MinGrantKB, + MaxGrantKB, + MinUsedGrantKB, + MaxUsedGrantKB, + PercentMemoryGrantUsed, + AvgMaxMemoryGrant, + MinSpills, + MaxSpills, + TotalSpills, + AvgSpills, + NumberOfPlans, + NumberOfDistinctPlans, + PlanCreationTime AS [Created At], + LastExecutionTime AS [Last Execution], + StatementStartOffset, + StatementEndOffset, + PlanGenerationNum, + PlanHandle AS [Plan Handle], + SqlHandle AS [SQL Handle], + QueryHash, + QueryPlanHash, + COALESCE(SetOptions, '''') AS [SET Options] + FROM ##BlitzCacheProcs + WHERE 1 = 1 + AND SPID = @@SPID ' + @nl; + + IF @MinimumExecutionCount IS NOT NULL + BEGIN + SET @sql += N' AND ExecutionCount >= @minimumExecutionCount '; + END; + + IF @MinutesBack IS NOT NULL + BEGIN + SET @sql += N' AND LastCompletionTime >= DATEADD(MINUTE, @min_back, GETDATE() ) '; + END; + + SELECT @sql += N' ORDER BY ' + CASE @SortOrder WHEN N'cpu' THEN N' TotalCPU ' + WHEN N'reads' THEN N' TotalReads ' + WHEN N'writes' THEN N' TotalWrites ' + WHEN N'duration' THEN N' TotalDuration ' + WHEN N'executions' THEN N' ExecutionCount ' + WHEN N'compiles' THEN N' PlanCreationTime ' + WHEN N'memory grant' THEN N' MaxGrantKB' + WHEN N'unused grant' THEN N' MaxGrantKB - MaxUsedGrantKB' + WHEN N'spills' THEN N' MaxSpills' + WHEN N'avg cpu' THEN N' AverageCPU' + WHEN N'avg reads' THEN N' AverageReads' + WHEN N'avg writes' THEN N' AverageWrites' + WHEN N'avg duration' THEN N' AverageDuration' + WHEN N'avg executions' THEN N' ExecutionsPerMinute' + WHEN N'avg memory grant' THEN N' AvgMaxMemoryGrant' + WHEN N'avg spills' THEN N' AvgSpills' + END + N' DESC '; + + SET @sql += N' OPTION (RECOMPILE) ; '; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@sql, 0, 4000); + PRINT SUBSTRING(@sql, 4000, 8000); + PRINT SUBSTRING(@sql, 8000, 12000); + PRINT SUBSTRING(@sql, 12000, 16000); + PRINT SUBSTRING(@sql, 16000, 20000); + PRINT SUBSTRING(@sql, 20000, 24000); + PRINT SUBSTRING(@sql, 24000, 28000); + PRINT SUBSTRING(@sql, 28000, 32000); + PRINT SUBSTRING(@sql, 32000, 36000); + PRINT SUBSTRING(@sql, 36000, 40000); + END; + + EXEC sp_executesql @sql, N'@Top INT, @min_duration INT, @min_back INT, @minimumExecutionCount INT', @Top, @DurationFilter_i, @MinutesBack, @MinimumExecutionCount; +END; + + +RAISERROR('Displaying analysis of plan cache.', 0, 1) WITH NOWAIT; + +DECLARE @columns NVARCHAR(MAX) = N'' ; + +IF @ExpertMode = 0 +BEGIN + RAISERROR(N'Returning ExpertMode = 0', 0, 1) WITH NOWAIT; + SET @columns = N' DatabaseName AS [Database], + QueryPlanCost AS [Cost], + QueryText AS [Query Text], + QueryType AS [Query Type], + Warnings AS [Warnings], + QueryPlan AS [Query Plan], + missing_indexes AS [Missing Indexes], + implicit_conversion_info AS [Implicit Conversion Info], + cached_execution_parameters AS [Cached Execution Parameters], + CONVERT(NVARCHAR(30), CAST((ExecutionCount) AS BIGINT), 1) AS [# Executions], + CONVERT(NVARCHAR(30), CAST((ExecutionsPerMinute) AS BIGINT), 1) AS [Executions / Minute], + CONVERT(NVARCHAR(30), CAST((PercentExecutions) AS BIGINT), 1) AS [Execution Weight], + CONVERT(NVARCHAR(30), CAST((TotalCPU) AS BIGINT), 1) AS [Total CPU (ms)], + CONVERT(NVARCHAR(30), CAST((AverageCPU) AS BIGINT), 1) AS [Avg CPU (ms)], + CONVERT(NVARCHAR(30), CAST((PercentCPU) AS BIGINT), 1) AS [CPU Weight], + CONVERT(NVARCHAR(30), CAST((TotalDuration) AS BIGINT), 1) AS [Total Duration (ms)], + CONVERT(NVARCHAR(30), CAST((AverageDuration) AS BIGINT), 1) AS [Avg Duration (ms)], + CONVERT(NVARCHAR(30), CAST((PercentDuration) AS BIGINT), 1) AS [Duration Weight], + CONVERT(NVARCHAR(30), CAST((TotalReads) AS BIGINT), 1) AS [Total Reads], + CONVERT(NVARCHAR(30), CAST((AverageReads) AS BIGINT), 1) AS [Avg Reads], + CONVERT(NVARCHAR(30), CAST((PercentReads) AS BIGINT), 1) AS [Read Weight], + CONVERT(NVARCHAR(30), CAST((TotalWrites) AS BIGINT), 1) AS [Total Writes], + CONVERT(NVARCHAR(30), CAST((AverageWrites) AS BIGINT), 1) AS [Avg Writes], + CONVERT(NVARCHAR(30), CAST((PercentWrites) AS BIGINT), 1) AS [Write Weight], + CONVERT(NVARCHAR(30), CAST((AverageReturnedRows) AS BIGINT), 1) AS [Average Rows], + CONVERT(NVARCHAR(30), CAST((MinGrantKB) AS BIGINT), 1) AS [Minimum Memory Grant KB], + CONVERT(NVARCHAR(30), CAST((MaxGrantKB) AS BIGINT), 1) AS [Maximum Memory Grant KB], + CONVERT(NVARCHAR(30), CAST((MinUsedGrantKB) AS BIGINT), 1) AS [Minimum Used Grant KB], + CONVERT(NVARCHAR(30), CAST((MaxUsedGrantKB) AS BIGINT), 1) AS [Maximum Used Grant KB], + CONVERT(NVARCHAR(30), CAST((AvgMaxMemoryGrant) AS BIGINT), 1) AS [Average Max Memory Grant], + CONVERT(NVARCHAR(30), CAST((MinSpills) AS BIGINT), 1) AS [Min Spills], + CONVERT(NVARCHAR(30), CAST((MaxSpills) AS BIGINT), 1) AS [Max Spills], + CONVERT(NVARCHAR(30), CAST((TotalSpills) AS BIGINT), 1) AS [Total Spills], + CONVERT(NVARCHAR(30), CAST((AvgSpills) AS MONEY), 1) AS [Avg Spills], + PlanCreationTime AS [Created At], + LastExecutionTime AS [Last Execution], + LastCompletionTime AS [Last Completion], + PlanHandle AS [Plan Handle], + SqlHandle AS [SQL Handle], + COALESCE(SetOptions, '''') AS [SET Options], + QueryHash AS [Query Hash], + PlanGenerationNum, + [Remove Plan Handle From Cache]'; +END; +ELSE +BEGIN + SET @columns = N' DatabaseName AS [Database], + QueryPlanCost AS [Cost], + QueryText AS [Query Text], + QueryType AS [Query Type], + Warnings AS [Warnings], + QueryPlan AS [Query Plan], + missing_indexes AS [Missing Indexes], + implicit_conversion_info AS [Implicit Conversion Info], + cached_execution_parameters AS [Cached Execution Parameters], ' + @nl; + + IF @ExpertMode = 2 /* Opserver */ + BEGIN + RAISERROR(N'Returning Expert Mode = 2', 0, 1) WITH NOWAIT; + SET @columns += N' + SUBSTRING( + CASE WHEN warning_no_join_predicate = 1 THEN '', 20'' ELSE '''' END + + CASE WHEN compile_timeout = 1 THEN '', 18'' ELSE '''' END + + CASE WHEN compile_memory_limit_exceeded = 1 THEN '', 19'' ELSE '''' END + + CASE WHEN busy_loops = 1 THEN '', 16'' ELSE '''' END + + CASE WHEN is_forced_plan = 1 THEN '', 3'' ELSE '''' END + + CASE WHEN is_forced_parameterized > 0 THEN '', 5'' ELSE '''' END + + CASE WHEN unparameterized_query = 1 THEN '', 23'' ELSE '''' END + + CASE WHEN missing_index_count > 0 THEN '', 10'' ELSE '''' END + + CASE WHEN unmatched_index_count > 0 THEN '', 22'' ELSE '''' END + + CASE WHEN is_cursor = 1 THEN '', 4'' ELSE '''' END + + CASE WHEN is_parallel = 1 THEN '', 6'' ELSE '''' END + + CASE WHEN near_parallel = 1 THEN '', 7'' ELSE '''' END + + CASE WHEN frequent_execution = 1 THEN '', 1'' ELSE '''' END + + CASE WHEN plan_warnings = 1 THEN '', 8'' ELSE '''' END + + CASE WHEN parameter_sniffing = 1 THEN '', 2'' ELSE '''' END + + CASE WHEN long_running = 1 THEN '', 9'' ELSE '''' END + + CASE WHEN downlevel_estimator = 1 THEN '', 13'' ELSE '''' END + + CASE WHEN implicit_conversions = 1 THEN '', 14'' ELSE '''' END + + CASE WHEN tvf_join = 1 THEN '', 17'' ELSE '''' END + + CASE WHEN plan_multiple_plans > 0 THEN '', 21'' ELSE '''' END + + CASE WHEN unmatched_index_count > 0 THEN '', 22'' ELSE '''' END + + CASE WHEN is_trivial = 1 THEN '', 24'' ELSE '''' END + + CASE WHEN is_forced_serial = 1 THEN '', 25'' ELSE '''' END + + CASE WHEN is_key_lookup_expensive = 1 THEN '', 26'' ELSE '''' END + + CASE WHEN is_remote_query_expensive = 1 THEN '', 28'' ELSE '''' END + + CASE WHEN trace_flags_session IS NOT NULL THEN '', 29'' ELSE '''' END + + CASE WHEN is_unused_grant = 1 THEN '', 30'' ELSE '''' END + + CASE WHEN function_count > 0 THEN '', 31'' ELSE '''' END + + CASE WHEN clr_function_count > 0 THEN '', 32'' ELSE '''' END + + CASE WHEN PlanCreationTimeHours <= 4 THEN '', 33'' ELSE '''' END + + CASE WHEN is_table_variable = 1 THEN '', 34'' ELSE '''' END + + CASE WHEN no_stats_warning = 1 THEN '', 35'' ELSE '''' END + + CASE WHEN relop_warnings = 1 THEN '', 36'' ELSE '''' END + + CASE WHEN is_table_scan = 1 THEN '', 37'' ELSE '''' END + + CASE WHEN backwards_scan = 1 THEN '', 38'' ELSE '''' END + + CASE WHEN forced_index = 1 THEN '', 39'' ELSE '''' END + + CASE WHEN forced_seek = 1 OR forced_scan = 1 THEN '', 40'' ELSE '''' END + + CASE WHEN columnstore_row_mode = 1 THEN '', 41'' ELSE '''' END + + CASE WHEN is_computed_scalar = 1 THEN '', 42'' ELSE '''' END + + CASE WHEN is_sort_expensive = 1 THEN '', 43'' ELSE '''' END + + CASE WHEN is_computed_filter = 1 THEN '', 44'' ELSE '''' END + + CASE WHEN index_ops >= 5 THEN '', 45'' ELSE '''' END + + CASE WHEN is_row_level = 1 THEN '', 46'' ELSE '''' END + + CASE WHEN is_spatial = 1 THEN '', 47'' ELSE '''' END + + CASE WHEN index_dml = 1 THEN '', 48'' ELSE '''' END + + CASE WHEN table_dml = 1 THEN '', 49'' ELSE '''' END + + CASE WHEN long_running_low_cpu = 1 THEN '', 50'' ELSE '''' END + + CASE WHEN low_cost_high_cpu = 1 THEN '', 51'' ELSE '''' END + + CASE WHEN stale_stats = 1 THEN '', 52'' ELSE '''' END + + CASE WHEN is_adaptive = 1 THEN '', 53'' ELSE '''' END + + CASE WHEN is_spool_expensive = 1 THEN + '', 54'' ELSE '''' END + + CASE WHEN is_spool_more_rows = 1 THEN + '', 55'' ELSE '''' END + + CASE WHEN is_table_spool_expensive = 1 THEN + '', 67'' ELSE '''' END + + CASE WHEN is_table_spool_more_rows = 1 THEN + '', 68'' ELSE '''' END + + CASE WHEN is_bad_estimate = 1 THEN + '', 56'' ELSE '''' END + + CASE WHEN is_paul_white_electric = 1 THEN '', 57'' ELSE '''' END + + CASE WHEN is_row_goal = 1 THEN '', 58'' ELSE '''' END + + CASE WHEN is_big_spills = 1 THEN '', 59'' ELSE '''' END + + CASE WHEN is_mstvf = 1 THEN '', 60'' ELSE '''' END + + CASE WHEN is_mm_join = 1 THEN '', 61'' ELSE '''' END + + CASE WHEN is_nonsargable = 1 THEN '', 62'' ELSE '''' END + + CASE WHEN CompileTime > 5000 THEN '', 63 '' ELSE '''' END + + CASE WHEN CompileCPU > 5000 THEN '', 64 '' ELSE '''' END + + CASE WHEN CompileMemory > 1024 AND ((CompileMemory) / (1 * CASE WHEN MaxCompileMemory = 0 THEN 1 ELSE MaxCompileMemory END) * 100.) >= 10. THEN '', 65 '' ELSE '''' END + + CASE WHEN select_with_writes > 0 THEN '', 66'' ELSE '''' END + , 3, 200000) AS opserver_warning , ' + @nl ; + END; + + SET @columns += N' + CONVERT(NVARCHAR(30), CAST((ExecutionCount) AS BIGINT), 1) AS [# Executions], + CONVERT(NVARCHAR(30), CAST((ExecutionsPerMinute) AS BIGINT), 1) AS [Executions / Minute], + CONVERT(NVARCHAR(30), CAST((PercentExecutions) AS BIGINT), 1) AS [Execution Weight], + CONVERT(NVARCHAR(30), CAST((SerialDesiredMemory) AS BIGINT), 1) AS [Serial Desired Memory], + CONVERT(NVARCHAR(30), CAST((SerialRequiredMemory) AS BIGINT), 1) AS [Serial Required Memory], + CONVERT(NVARCHAR(30), CAST((TotalCPU) AS BIGINT), 1) AS [Total CPU (ms)], + CONVERT(NVARCHAR(30), CAST((AverageCPU) AS BIGINT), 1) AS [Avg CPU (ms)], + CONVERT(NVARCHAR(30), CAST((PercentCPU) AS BIGINT), 1) AS [CPU Weight], + CONVERT(NVARCHAR(30), CAST((TotalDuration) AS BIGINT), 1) AS [Total Duration (ms)], + CONVERT(NVARCHAR(30), CAST((AverageDuration) AS BIGINT), 1) AS [Avg Duration (ms)], + CONVERT(NVARCHAR(30), CAST((PercentDuration) AS BIGINT), 1) AS [Duration Weight], + CONVERT(NVARCHAR(30), CAST((TotalReads) AS BIGINT), 1) AS [Total Reads], + CONVERT(NVARCHAR(30), CAST((AverageReads) AS BIGINT), 1) AS [Average Reads], + CONVERT(NVARCHAR(30), CAST((PercentReads) AS BIGINT), 1) AS [Read Weight], + CONVERT(NVARCHAR(30), CAST((TotalWrites) AS BIGINT), 1) AS [Total Writes], + CONVERT(NVARCHAR(30), CAST((AverageWrites) AS BIGINT), 1) AS [Average Writes], + CONVERT(NVARCHAR(30), CAST((PercentWrites) AS BIGINT), 1) AS [Write Weight], + CONVERT(NVARCHAR(30), CAST((PercentExecutionsByType) AS BIGINT), 1) AS [% Executions (Type)], + CONVERT(NVARCHAR(30), CAST((PercentCPUByType) AS BIGINT), 1) AS [% CPU (Type)], + CONVERT(NVARCHAR(30), CAST((PercentDurationByType) AS BIGINT), 1) AS [% Duration (Type)], + CONVERT(NVARCHAR(30), CAST((PercentReadsByType) AS BIGINT), 1) AS [% Reads (Type)], + CONVERT(NVARCHAR(30), CAST((PercentWritesByType) AS BIGINT), 1) AS [% Writes (Type)], + CONVERT(NVARCHAR(30), CAST((TotalReturnedRows) AS BIGINT), 1) AS [Total Rows], + CONVERT(NVARCHAR(30), CAST((AverageReturnedRows) AS BIGINT), 1) AS [Avg Rows], + CONVERT(NVARCHAR(30), CAST((MinReturnedRows) AS BIGINT), 1) AS [Min Rows], + CONVERT(NVARCHAR(30), CAST((MaxReturnedRows) AS BIGINT), 1) AS [Max Rows], + CONVERT(NVARCHAR(30), CAST((MinGrantKB) AS BIGINT), 1) AS [Minimum Memory Grant KB], + CONVERT(NVARCHAR(30), CAST((MaxGrantKB) AS BIGINT), 1) AS [Maximum Memory Grant KB], + CONVERT(NVARCHAR(30), CAST((MinUsedGrantKB) AS BIGINT), 1) AS [Minimum Used Grant KB], + CONVERT(NVARCHAR(30), CAST((MaxUsedGrantKB) AS BIGINT), 1) AS [Maximum Used Grant KB], + CONVERT(NVARCHAR(30), CAST((AvgMaxMemoryGrant) AS BIGINT), 1) AS [Average Max Memory Grant], + CONVERT(NVARCHAR(30), CAST((MinSpills) AS BIGINT), 1) AS [Min Spills], + CONVERT(NVARCHAR(30), CAST((MaxSpills) AS BIGINT), 1) AS [Max Spills], + CONVERT(NVARCHAR(30), CAST((TotalSpills) AS BIGINT), 1) AS [Total Spills], + CONVERT(NVARCHAR(30), CAST((AvgSpills) AS MONEY), 1) AS [Avg Spills], + CONVERT(NVARCHAR(30), CAST((NumberOfPlans) AS BIGINT), 1) AS [# Plans], + CONVERT(NVARCHAR(30), CAST((NumberOfDistinctPlans) AS BIGINT), 1) AS [# Distinct Plans], + PlanCreationTime AS [Created At], + LastExecutionTime AS [Last Execution], + LastCompletionTime AS [Last Completion], + CONVERT(NVARCHAR(30), CAST((CachedPlanSize) AS BIGINT), 1) AS [Cached Plan Size (KB)], + CONVERT(NVARCHAR(30), CAST((CompileTime) AS BIGINT), 1) AS [Compile Time (ms)], + CONVERT(NVARCHAR(30), CAST((CompileCPU) AS BIGINT), 1) AS [Compile CPU (ms)], + CONVERT(NVARCHAR(30), CAST((CompileMemory) AS BIGINT), 1) AS [Compile memory (KB)], + COALESCE(SetOptions, '''') AS [SET Options], + PlanHandle AS [Plan Handle], + SqlHandle AS [SQL Handle], + [SQL Handle More Info], + QueryHash AS [Query Hash], + [Query Hash More Info], + QueryPlanHash AS [Query Plan Hash], + StatementStartOffset, + StatementEndOffset, + PlanGenerationNum, + [Remove Plan Handle From Cache], + [Remove SQL Handle From Cache]'; +END; + + + +SET @sql = N' +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; +SELECT TOP (@Top) ' + @columns + @nl + N' +FROM ##BlitzCacheProcs +WHERE SPID = @spid ' + @nl; + +IF @MinimumExecutionCount IS NOT NULL + BEGIN + SET @sql += N' AND ExecutionCount >= @minimumExecutionCount ' + @nl; + END; + +IF @MinutesBack IS NOT NULL + BEGIN + SET @sql += N' AND LastCompletionTime >= DATEADD(MINUTE, @min_back, GETDATE() ) ' + @nl; + END; + +SELECT @sql += N' ORDER BY ' + CASE @SortOrder WHEN N'cpu' THEN N' TotalCPU ' + WHEN N'reads' THEN N' TotalReads ' + WHEN N'writes' THEN N' TotalWrites ' + WHEN N'duration' THEN N' TotalDuration ' + WHEN N'executions' THEN N' ExecutionCount ' + WHEN N'compiles' THEN N' PlanCreationTime ' + WHEN N'memory grant' THEN N' MaxGrantKB' + WHEN N'unused grant' THEN N' MaxGrantKB - MaxUsedGrantKB ' + WHEN N'spills' THEN N' MaxSpills' + WHEN N'avg cpu' THEN N' AverageCPU' + WHEN N'avg reads' THEN N' AverageReads' + WHEN N'avg writes' THEN N' AverageWrites' + WHEN N'avg duration' THEN N' AverageDuration' + WHEN N'avg executions' THEN N' ExecutionsPerMinute' + WHEN N'avg memory grant' THEN N' AvgMaxMemoryGrant' + WHEN N'avg spills' THEN N' AvgSpills' + END + N' DESC '; +SET @sql += N' OPTION (RECOMPILE) ; '; + +IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@sql, 0, 4000); + PRINT SUBSTRING(@sql, 4000, 8000); + PRINT SUBSTRING(@sql, 8000, 12000); + PRINT SUBSTRING(@sql, 12000, 16000); + PRINT SUBSTRING(@sql, 16000, 20000); + PRINT SUBSTRING(@sql, 20000, 24000); + PRINT SUBSTRING(@sql, 24000, 28000); + PRINT SUBSTRING(@sql, 28000, 32000); + PRINT SUBSTRING(@sql, 32000, 36000); + PRINT SUBSTRING(@sql, 36000, 40000); + END; +IF(@OutputType <> 'NONE') +BEGIN + EXEC sp_executesql @sql, N'@Top INT, @spid INT, @minimumExecutionCount INT, @min_back INT', @Top, @@SPID, @MinimumExecutionCount, @MinutesBack; +END; + +/* + +This section will check if: + * >= 30% of plans were created in the last hour + * Check on the memory_clerks DMV for space used by TokenAndPermUserStore + * Compare that to the size of the buffer pool + * If it's >10%, +*/ +IF EXISTS +( + SELECT 1/0 + FROM #plan_creation AS pc + WHERE pc.percent_1 >= 30 +) +BEGIN + +SELECT @common_version = + CONVERT(DECIMAL(10,2), c.common_version) +FROM #checkversion AS c; + +IF @common_version >= 11 + SET @user_perm_sql = N' + SET @buffer_pool_memory_gb = 0; + SELECT @buffer_pool_memory_gb = SUM(pages_kb)/ 1024. / 1024. + FROM sys.dm_os_memory_clerks + WHERE type = ''MEMORYCLERK_SQLBUFFERPOOL'';' +ELSE + SET @user_perm_sql = N' + SET @buffer_pool_memory_gb = 0; + SELECT @buffer_pool_memory_gb = SUM(single_pages_kb + multi_pages_kb)/ 1024. / 1024. + FROM sys.dm_os_memory_clerks + WHERE type = ''MEMORYCLERK_SQLBUFFERPOOL'';' + +EXEC sys.sp_executesql @user_perm_sql, + N'@buffer_pool_memory_gb DECIMAL(10,2) OUTPUT', + @buffer_pool_memory_gb = @buffer_pool_memory_gb OUTPUT; + +IF @common_version >= 11 +BEGIN + SET @user_perm_sql = N' + SELECT @user_perm_gb = CASE WHEN (pages_kb / 1024.0 / 1024.) >= 2. + THEN CONVERT(DECIMAL(38, 2), (pages_kb / 1024.0 / 1024.)) + ELSE 0 + END + FROM sys.dm_os_memory_clerks + WHERE type = ''USERSTORE_TOKENPERM'' + AND name = ''TokenAndPermUserStore'';'; +END; + +IF @common_version < 11 +BEGIN + SET @user_perm_sql = N' + SELECT @user_perm_gb = CASE WHEN ((single_pages_kb + multi_pages_kb) / 1024.0 / 1024.) >= 2. + THEN CONVERT(DECIMAL(38, 2), ((single_pages_kb + multi_pages_kb) / 1024.0 / 1024.)) + ELSE 0 + END + FROM sys.dm_os_memory_clerks + WHERE type = ''USERSTORE_TOKENPERM'' + AND name = ''TokenAndPermUserStore'';'; +END; + +EXEC sys.sp_executesql @user_perm_sql, + N'@user_perm_gb DECIMAL(10,2) OUTPUT', + @user_perm_gb = @user_perm_gb_out OUTPUT; + +IF @buffer_pool_memory_gb > 0 + BEGIN + IF (@user_perm_gb_out / (1. * @buffer_pool_memory_gb)) * 100. >= 10 + BEGIN + SET @is_tokenstore_big = 1; + SET @user_perm_percent = (@user_perm_gb_out / (1. * @buffer_pool_memory_gb)) * 100.; + END + END + +END + + + +IF @HideSummary = 0 AND @ExportToExcel = 0 +BEGIN + IF @Reanalyze = 0 + BEGIN + RAISERROR('Building query plan summary data.', 0, 1) WITH NOWAIT; + + /* Build summary data */ + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE frequent_execution = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 1, + 100, + 'Execution Pattern', + 'Frequent Execution', + 'https://www.brentozar.com/blitzcache/frequently-executed-queries/', + 'Queries are being executed more than ' + + CAST (@execution_threshold AS VARCHAR(5)) + + ' times per minute. This can put additional load on the server, even when queries are lightweight.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE parameter_sniffing = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 2, + 50, + 'Parameterization', + 'Parameter Sniffing', + 'https://www.brentozar.com/blitzcache/parameter-sniffing/', + 'There are signs of parameter sniffing (wide variance in rows return or time to execute). Investigate query patterns and tune code appropriately.') ; + + /* Forced execution plans */ + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_forced_plan = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 3, + 50, + 'Parameterization', + 'Forced Plan', + 'https://www.brentozar.com/blitzcache/forced-plans/', + 'Execution plans have been compiled with forced plans, either through FORCEPLAN, plan guides, or forced parameterization. This will make general tuning efforts less effective.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_cursor = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 4, + 200, + 'Cursors', + 'Cursor', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'There are cursors in the plan cache. This is neither good nor bad, but it is a thing. Cursors are weird in SQL Server.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_cursor = 1 + AND is_optimistic_cursor = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 4, + 200, + 'Cursors', + 'Optimistic Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'There are optimistic cursors in the plan cache, which can harm performance.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_cursor = 1 + AND is_forward_only_cursor = 0 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 4, + 200, + 'Cursors', + 'Non-forward Only Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'There are non-forward only cursors in the plan cache, which can harm performance.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_cursor = 1 + AND is_cursor_dynamic = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 4, + 200, + 'Cursors', + 'Dynamic Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'Dynamic Cursors inhibit parallelism!.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_cursor = 1 + AND is_fast_forward_cursor = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 4, + 200, + 'Cursors', + 'Fast Forward Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'Fast forward cursors inhibit parallelism!.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_forced_parameterized = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 5, + 50, + 'Parameterization', + 'Forced Parameterization', + 'https://www.brentozar.com/blitzcache/forced-parameterization/', + 'Execution plans have been compiled with forced parameterization.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_parallel = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 6, + 200, + 'Execution Plans', + 'Parallel', + 'https://www.brentozar.com/blitzcache/parallel-plans-detected/', + 'Parallel plans detected. These warrant investigation, but are neither good nor bad.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE near_parallel = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 7, + 200, + 'Execution Plans', + 'Nearly Parallel', + 'https://www.brentozar.com/blitzcache/query-cost-near-cost-threshold-parallelism/', + 'Queries near the cost threshold for parallelism. These may go parallel when you least expect it.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE plan_warnings = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 8, + 50, + 'Execution Plans', + 'Plan Warnings', + 'https://www.brentozar.com/blitzcache/query-plan-warnings/', + 'Warnings detected in execution plans. SQL Server is telling you that something bad is going on that requires your attention.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE long_running = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 9, + 50, + 'Performance', + 'Long Running Query', + 'https://www.brentozar.com/blitzcache/long-running-queries/', + 'Long running queries have been found. These are queries with an average duration longer than ' + + CAST(@long_running_query_warning_seconds / 1000 / 1000 AS VARCHAR(5)) + + ' second(s). These queries should be investigated for additional tuning options.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.missing_index_count > 0 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 10, + 50, + 'Performance', + 'Missing Indexes', + 'https://www.brentozar.com/blitzcache/missing-index-request/', + 'Queries found with missing indexes.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.downlevel_estimator = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 13, + 200, + 'Cardinality', + 'Downlevel CE', + 'https://www.brentozar.com/blitzcache/legacy-cardinality-estimator/', + 'A legacy cardinality estimator is being used by one or more queries. Investigate whether you need to be using this cardinality estimator. This may be caused by compatibility levels, global trace flags, or query level trace flags.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE implicit_conversions = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 14, + 50, + 'Performance', + 'Implicit Conversions', + 'https://www.brentozar.com/go/implicit', + 'One or more queries are comparing two fields that are not of the same data type.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE busy_loops = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 16, + 100, + 'Performance', + 'Busy Loops', + 'https://www.brentozar.com/blitzcache/busy-loops/', + 'Operations have been found that are executed 100 times more often than the number of rows returned by each iteration. This is an indicator that something is off in query execution.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE tvf_join = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 17, + 50, + 'Performance', + 'Function Join', + 'https://www.brentozar.com/blitzcache/tvf-join/', + 'Execution plans have been found that join to table valued functions (TVFs). TVFs produce inaccurate estimates of the number of rows returned and can lead to any number of query plan problems.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE compile_timeout = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 18, + 50, + 'Execution Plans', + 'Compilation Timeout', + 'https://www.brentozar.com/blitzcache/compilation-timeout/', + 'Query compilation timed out for one or more queries. SQL Server did not find a plan that meets acceptable performance criteria in the time allotted so the best guess was returned. There is a very good chance that this plan isn''t even below average - it''s probably terrible.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE compile_memory_limit_exceeded = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 19, + 50, + 'Execution Plans', + 'Compile Memory Limit Exceeded', + 'https://www.brentozar.com/blitzcache/compile-memory-limit-exceeded/', + 'The optimizer has a limited amount of memory available. One or more queries are complex enough that SQL Server was unable to allocate enough memory to fully optimize the query. A best fit plan was found, and it''s probably terrible.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE warning_no_join_predicate = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 20, + 50, + 'Execution Plans', + 'No Join Predicate', + 'https://www.brentozar.com/blitzcache/no-join-predicate/', + 'Operators in a query have no join predicate. This means that all rows from one table will be matched with all rows from anther table producing a Cartesian product. That''s a whole lot of rows. This may be your goal, but it''s important to investigate why this is happening.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE plan_multiple_plans > 0 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 21, + 200, + 'Execution Plans', + 'Multiple Plans', + 'https://www.brentozar.com/blitzcache/multiple-plans/', + 'Queries exist with multiple execution plans (as determined by query_plan_hash). Investigate possible ways to parameterize these queries or otherwise reduce the plan count.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE unmatched_index_count > 0 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 22, + 100, + 'Performance', + 'Unmatched Indexes', + 'https://www.brentozar.com/blitzcache/unmatched-indexes', + 'An index could have been used, but SQL Server chose not to use it - likely due to parameterization and filtered indexes.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE unparameterized_query = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 23, + 100, + 'Parameterization', + 'Unparameterized Query', + 'https://www.brentozar.com/blitzcache/unparameterized-queries', + 'Unparameterized queries found. These could be ad hoc queries, data exploration, or queries using "OPTIMIZE FOR UNKNOWN".'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs + WHERE is_trivial = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 24, + 100, + 'Execution Plans', + 'Trivial Plans', + 'https://www.brentozar.com/blitzcache/trivial-plans', + 'Trivial plans get almost no optimization. If you''re finding these in the top worst queries, something may be going wrong.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_forced_serial= 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 25, + 10, + 'Execution Plans', + 'Forced Serialization', + 'https://www.brentozar.com/blitzcache/forced-serialization/', + 'Something in your plan is forcing a serial query. Further investigation is needed if this is not by design.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_key_lookup_expensive= 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 26, + 100, + 'Execution Plans', + 'Expensive Key Lookup', + 'https://www.brentozar.com/blitzcache/expensive-key-lookups/', + 'There''s a key lookup in your plan that costs >=50% of the total plan cost.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_remote_query_expensive= 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 28, + 100, + 'Execution Plans', + 'Expensive Remote Query', + 'https://www.brentozar.com/blitzcache/expensive-remote-query/', + 'There''s a remote query in your plan that costs >=50% of the total plan cost.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.trace_flags_session IS NOT NULL + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 29, + 200, + 'Trace Flags', + 'Session Level Trace Flags Enabled', + 'https://www.brentozar.com/blitz/trace-flags-enabled-globally/', + 'Someone is enabling session level Trace Flags in a query.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_unused_grant IS NOT NULL + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 30, + 100, + 'Memory Grant', + 'Unused Memory Grant', + 'https://www.brentozar.com/blitzcache/unused-memory-grants/', + 'Queries have large unused memory grants. This can cause concurrency issues, if queries are waiting a long time to get memory to run.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.function_count > 0 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 31, + 100, + 'Compute Scalar That References A Function', + 'Calls Functions', + 'https://www.brentozar.com/blitzcache/compute-scalar-functions/', + 'Both of these will force queries to run serially, run at least once per row, and may result in poor cardinality estimates.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.clr_function_count > 0 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 32, + 100, + 'Compute Scalar That References A CLR Function', + 'Calls CLR Functions', + 'https://www.brentozar.com/blitzcache/compute-scalar-functions/', + 'May force queries to run serially, run at least once per row, and may result in poor cardinality estimates.') ; + + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_table_variable = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 33, + 100, + 'Table Variables detected', + 'Table Variables', + 'https://www.brentozar.com/blitzcache/table-variables/', + 'All modifications are single threaded, and selects have really low row estimates.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.no_stats_warning = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 35, + 100, + 'Statistics', + 'Columns With No Statistics', + 'https://www.brentozar.com/blitzcache/columns-no-statistics/', + 'Sometimes this happens with indexed views, other times because auto create stats is turned off.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.relop_warnings = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 36, + 100, + 'Warnings', + 'Operator Warnings', + 'https://www.brentozar.com/blitzcache/query-plan-warnings/', + 'Check the plan for more details.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_table_scan = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 37, + 100, + 'Indexes', + 'Table Scans (Heaps)', + 'https://www.brentozar.com/archive/2012/05/video-heaps/', + 'This may not be a problem. Run sp_BlitzIndex for more information.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.backwards_scan = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 38, + 200, + 'Indexes', + 'Backwards Scans', + 'https://www.brentozar.com/blitzcache/backwards-scans/', + 'This isn''t always a problem. They can cause serial zones in plans, and may need an index to match sort order.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.forced_index = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 39, + 100, + 'Indexes', + 'Forced Indexes', + 'https://www.brentozar.com/blitzcache/optimizer-forcing/', + 'This can cause inefficient plans, and will prevent missing index requests.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.forced_seek = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 40, + 100, + 'Indexes', + 'Forced Seeks', + 'https://www.brentozar.com/blitzcache/optimizer-forcing/', + 'This can cause inefficient plans by taking seek vs scan choice away from the optimizer.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.forced_scan = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 40, + 100, + 'Indexes', + 'Forced Scans', + 'https://www.brentozar.com/blitzcache/optimizer-forcing/', + 'This can cause inefficient plans by taking seek vs scan choice away from the optimizer.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.columnstore_row_mode = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 41, + 100, + 'Indexes', + 'ColumnStore Row Mode', + 'https://www.brentozar.com/blitzcache/columnstore-indexes-operating-row-mode/', + 'ColumnStore indexes operating in Row Mode indicate really poor query choices.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_computed_scalar = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 42, + 50, + 'Functions', + 'Computed Column UDF', + 'https://www.brentozar.com/blitzcache/computed-columns-referencing-functions/', + 'This can cause a whole mess of bad serializartion problems.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_sort_expensive = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 43, + 100, + 'Execution Plans', + 'Expensive Sort', + 'https://www.brentozar.com/blitzcache/expensive-sorts/', + 'There''s a sort in your plan that costs >=50% of the total plan cost.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_computed_filter = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 44, + 50, + 'Functions', + 'Filter UDF', + 'https://www.brentozar.com/blitzcache/compute-scalar-functions/', + 'Someone put a Scalar UDF in the WHERE clause!') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.index_ops >= 5 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 45, + 100, + 'Indexes', + '>= 5 Indexes Modified', + 'https://www.brentozar.com/blitzcache/many-indexes-modified/', + 'This can cause lots of hidden I/O -- Run sp_BlitzIndex for more information.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_row_level = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 46, + 200, + 'Complexity', + 'Row Level Security', + 'https://www.brentozar.com/blitzcache/row-level-security/', + 'You may see a lot of confusing junk in your query plan.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_spatial = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 47, + 200, + 'Complexity', + 'Spatial Index', + 'https://www.brentozar.com/blitzcache/spatial-indexes/', + 'Purely informational.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.index_dml = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 48, + 150, + 'Complexity', + 'Index DML', + 'https://www.brentozar.com/blitzcache/index-dml/', + 'This can cause recompiles and stuff.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.table_dml = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 49, + 150, + 'Complexity', + 'Table DML', + 'https://www.brentozar.com/blitzcache/table-dml/', + 'This can cause recompiles and stuff.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.long_running_low_cpu = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 50, + 150, + 'Blocking', + 'Long Running Low CPU', + 'https://www.brentozar.com/blitzcache/long-running-low-cpu/', + 'This can be a sign of blocking, linked servers, or poor client application code (ASYNC_NETWORK_IO).') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.low_cost_high_cpu = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 51, + 150, + 'Complexity', + 'Low Cost Query With High CPU', + 'https://www.brentozar.com/blitzcache/low-cost-high-cpu/', + 'This can be a sign of functions or Dynamic SQL that calls black-box code.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.stale_stats = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 52, + 150, + 'Statistics', + 'Statistics used have > 100k modifications in the last 7 days', + 'https://www.brentozar.com/blitzcache/stale-statistics/', + 'Ever heard of updating statistics?') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_adaptive = 1 + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 53, + 200, + 'Complexity', + 'Adaptive joins', + 'https://www.brentozar.com/blitzcache/adaptive-joins/', + 'This join will sometimes do seeks, and sometimes do scans.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_spool_expensive = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 54, + 150, + 'Indexes', + 'Expensive Index Spool', + 'https://www.brentozar.com/blitzcache/eager-index-spools/', + 'Check operator predicates and output for index definition guidance') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_spool_more_rows = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 55, + 150, + 'Indexes', + 'Large Index Row Spool', + 'https://www.brentozar.com/blitzcache/eager-index-spools/', + 'Check operator predicates and output for index definition guidance') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_bad_estimate = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 56, + 100, + 'Complexity', + 'Row Estimate Mismatch', + 'https://www.brentozar.com/blitzcache/bad-estimates/', + 'Estimated rows are different from average rows by a factor of 10000. This may indicate a performance problem if mismatches occur regularly') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_paul_white_electric = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 57, + 200, + 'Is Paul White Electric?', + 'This query has a Switch operator in it!', + 'https://www.sql.kiwi/2013/06/hello-operator-my-switch-is-bored.html', + 'You should email this query plan to Paul: SQLkiwi at gmail dot com') ; + + IF @v >= 14 OR (@v = 13 AND @build >= 5026) + BEGIN + + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT + @@SPID, + 997, + 200, + 'Database Level Statistics', + 'The database ' + sa.[Database] + ' last had a stats update on ' + CONVERT(NVARCHAR(10), CONVERT(DATE, MAX(sa.LastUpdate))) + ' and has ' + CONVERT(NVARCHAR(10), AVG(sa.ModificationCount)) + ' modifications on average.' AS [Finding], + 'https://www.brentozar.com/blitzcache/stale-statistics/' AS URL, + 'Consider updating statistics more frequently,' AS [Details] + FROM #stats_agg AS sa + GROUP BY sa.[Database] + HAVING MAX(sa.LastUpdate) <= DATEADD(DAY, -7, SYSDATETIME()) + AND AVG(sa.ModificationCount) >= 100000; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_row_goal = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 58, + 200, + 'Complexity', + 'Row Goals', + 'https://www.brentozar.com/go/rowgoals/', + 'This query had row goals introduced, which can be good or bad, and should be investigated for high read queries.') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_big_spills = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 59, + 100, + 'TempDB', + '>500mb Spills', + 'https://www.brentozar.com/blitzcache/tempdb-spills/', + 'This query spills >500mb to tempdb on average. One way or another, this query didn''t get enough memory') ; + + + END; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_mstvf = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 60, + 100, + 'Functions', + 'MSTVFs', + 'https://www.brentozar.com/blitzcache/tvf-join/', + 'Execution plans have been found that join to table valued functions (TVFs). TVFs produce inaccurate estimates of the number of rows returned and can lead to any number of query plan problems.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_mm_join = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 61, + 100, + 'Complexity', + 'Many to Many Merge', + 'https://www.brentozar.com/archive/2018/04/many-mysteries-merge-joins/', + 'These use secret worktables that could be doing lots of reads. Occurs when join inputs aren''t known to be unique. Can be really bad when parallel.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_nonsargable = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 62, + 50, + 'Non-SARGable queries', + 'non-SARGables', + 'https://www.brentozar.com/blitzcache/non-sargable-predicates/', + 'Looks for intrinsic functions and expressions as predicates, and leading wildcard LIKE searches.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE CompileTime > 5000 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 63, + 100, + 'Complexity', + 'Long Compile Time', + 'https://www.brentozar.com/blitzcache/high-compilers/', + 'Queries are taking >5 seconds to compile. This can be normal for large plans, but be careful if they compile frequently'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE CompileCPU > 5000 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 64, + 50, + 'Complexity', + 'High Compile CPU', + 'https://www.brentozar.com/blitzcache/high-compilers/', + 'Queries taking >5 seconds of CPU to compile. If CPU is high and plans like this compile frequently, they may be related'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE CompileMemory > 1024 + AND ((CompileMemory) / (1 * CASE WHEN MaxCompileMemory = 0 THEN 1 ELSE MaxCompileMemory END) * 100.) >= 10. + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 65, + 50, + 'Complexity', + 'High Compile Memory', + 'https://www.brentozar.com/blitzcache/high-compilers/', + 'Queries taking 10% of Max Compile Memory. If you see high RESOURCE_SEMAPHORE_QUERY_COMPILE waits, these may be related'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.select_with_writes = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 66, + 50, + 'Complexity', + 'Selects w/ Writes', + 'https://dba.stackexchange.com/questions/191825/', + 'This is thrown when reads cause writes that are not already flagged as big spills (2016+) or index spools.'); + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_table_spool_expensive = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 67, + 150, + 'Expensive Table Spool', + 'You have a table spool, this is usually a sign that queries are doing unnecessary work', + 'https://sqlperformance.com/2019/09/sql-performance/nested-loops-joins-performance-spools', + 'Check for non-SARGable predicates, or a lot of work being done inside a nested loops join') ; + + IF EXISTS (SELECT 1/0 + FROM ##BlitzCacheProcs p + WHERE p.is_table_spool_more_rows = 1 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 68, + 150, + 'Table Spools Many Rows', + 'You have a table spool that spools more rows than the query returns', + 'https://sqlperformance.com/2019/09/sql-performance/nested-loops-joins-performance-spools', + 'Check for non-SARGable predicates, or a lot of work being done inside a nested loops join'); + + IF EXISTS (SELECT 1/0 + FROM #plan_creation p + WHERE (p.percent_24 > 0) + AND SPID = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT SPID, + 999, + CASE WHEN ISNULL(p.percent_24, 0) > 75 THEN 1 ELSE 254 END AS Priority, + 'Plan Cache Information', + CASE WHEN ISNULL(p.percent_24, 0) > 75 THEN 'Plan Cache Instability' ELSE 'Plan Cache Stability' END AS Finding, + 'https://www.brentozar.com/archive/2018/07/tsql2sday-how-much-plan-cache-history-do-you-have/', + 'You have ' + CONVERT(NVARCHAR(10), ISNULL(p.total_plans, 0)) + + ' total plans in your cache, with ' + + CONVERT(NVARCHAR(10), ISNULL(p.percent_24, 0)) + + '% plans created in the past 24 hours, ' + + CONVERT(NVARCHAR(10), ISNULL(p.percent_4, 0)) + + '% created in the past 4 hours, and ' + + CONVERT(NVARCHAR(10), ISNULL(p.percent_1, 0)) + + '% created in the past 1 hour. ' + + 'When these percentages are high, it may be a sign of memory pressure or plan cache instability.' + FROM #plan_creation p ; + + IF EXISTS (SELECT 1/0 + FROM #plan_usage p + WHERE p.percent_duplicate > 5 + AND spid = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT spid, + 999, + CASE WHEN ISNULL(p.percent_duplicate, 0) > 75 THEN 1 ELSE 254 END AS Priority, + 'Plan Cache Information', + CASE WHEN ISNULL(p.percent_duplicate, 0) > 75 THEN 'Many Duplicate Plans' ELSE 'Duplicate Plans' END AS Finding, + 'https://www.brentozar.com/archive/2018/03/why-multiple-plans-for-one-query-are-bad/', + 'You have ' + CONVERT(NVARCHAR(10), p.total_plans) + + ' plans in your cache, and ' + + CONVERT(NVARCHAR(10), p.percent_duplicate) + + '% are duplicates with more than 5 entries' + + ', meaning similar queries are generating the same plan repeatedly.' + + ' Forced Parameterization may fix the issue. To find troublemakers, use: EXEC sp_BlitzCache @SortOrder = ''query hash''; ' + FROM #plan_usage AS p ; + + IF EXISTS (SELECT 1/0 + FROM #plan_usage p + WHERE p.percent_single > 5 + AND spid = @@SPID) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT spid, + 999, + CASE WHEN ISNULL(p.percent_single, 0) > 75 THEN 1 ELSE 254 END AS Priority, + 'Plan Cache Information', + CASE WHEN ISNULL(p.percent_single, 0) > 75 THEN 'Many Single-Use Plans' ELSE 'Single-Use Plans' END AS Finding, + 'https://www.brentozar.com/blitz/single-use-plans-procedure-cache/', + 'You have ' + CONVERT(NVARCHAR(10), p.total_plans) + + ' plans in your cache, and ' + + CONVERT(NVARCHAR(10), p.percent_single) + + '% are single use plans' + + ', meaning SQL Server thinks it''s seeing a lot of "new" queries and creating plans for them.' + + ' Forced Parameterization and/or Optimize For Ad Hoc Workloads may fix the issue.' + + 'To find troublemakers, use: EXEC sp_BlitzCache @SortOrder = ''query hash''; ' + FROM #plan_usage AS p ; + + IF @is_tokenstore_big = 1 + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT @@SPID, + 69, + 10, + N'Large USERSTORE_TOKENPERM cache: ' + CONVERT(NVARCHAR(11), @user_perm_gb_out) + N'GB', + N'The USERSTORE_TOKENPERM is taking up ' + CONVERT(NVARCHAR(11), @user_perm_percent) + + N'% of the buffer pool, and your plan cache seems to be unstable', + N'https://www.brentozar.com/go/userstore', + N'A growing USERSTORE_TOKENPERM cache can cause the plan cache to clear out' + + IF @v >= 11 + BEGIN + IF EXISTS (SELECT 1/0 + FROM #trace_flags AS tf + WHERE tf.global_trace_flags IS NOT NULL + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 1000, + 255, + 'Global Trace Flags Enabled', + 'You have Global Trace Flags enabled on your server', + 'https://www.brentozar.com/blitz/trace-flags-enabled-globally/', + 'You have the following Global Trace Flags enabled: ' + (SELECT TOP 1 tf.global_trace_flags FROM #trace_flags AS tf WHERE tf.global_trace_flags IS NOT NULL)) ; + END; + + IF NOT EXISTS (SELECT 1/0 + FROM ##BlitzCacheResults AS bcr + WHERE bcr.Priority = 2147483646 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 2147483646, + 255, + 'Need more help?' , + 'Paste your plan on the internet!', + 'http://pastetheplan.com', + 'This makes it easy to share plans and post them to Q&A sites like https://dba.stackexchange.com/!') ; + + + + IF NOT EXISTS (SELECT 1/0 + FROM ##BlitzCacheResults AS bcr + WHERE bcr.Priority = 2147483647 + ) + INSERT INTO ##BlitzCacheResults (SPID, CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (@@SPID, + 2147483647, + 255, + 'Thanks for using sp_BlitzCache!' , + 'From Your Community Volunteers', + 'http://FirstResponderKit.org', + 'We hope you found this tool useful. Current version: ' + @Version + ' released on ' + CONVERT(NVARCHAR(30), @VersionDate) + '.') ; + + END; + + + SELECT Priority, + FindingsGroup, + Finding, + URL, + Details, + CheckID + FROM ##BlitzCacheResults + WHERE SPID = @@SPID + GROUP BY Priority, + FindingsGroup, + Finding, + URL, + Details, + CheckID + ORDER BY Priority ASC, FindingsGroup, Finding, CheckID ASC + OPTION (RECOMPILE); +END; + +IF @Debug = 1 + BEGIN + + SELECT '##BlitzCacheResults' AS table_name, * + FROM ##BlitzCacheResults + OPTION ( RECOMPILE ); + + SELECT '##BlitzCacheProcs' AS table_name, * + FROM ##BlitzCacheProcs + OPTION ( RECOMPILE ); + + SELECT '#statements' AS table_name, * + FROM #statements AS s + OPTION (RECOMPILE); + + SELECT '#query_plan' AS table_name, * + FROM #query_plan AS qp + OPTION (RECOMPILE); + + SELECT '#relop' AS table_name, * + FROM #relop AS r + OPTION (RECOMPILE); + + SELECT '#only_query_hashes' AS table_name, * + FROM #only_query_hashes + OPTION ( RECOMPILE ); + + SELECT '#ignore_query_hashes' AS table_name, * + FROM #ignore_query_hashes + OPTION ( RECOMPILE ); + + SELECT '#only_sql_handles' AS table_name, * + FROM #only_sql_handles + OPTION ( RECOMPILE ); + + SELECT '#ignore_sql_handles' AS table_name, * + FROM #ignore_sql_handles + OPTION ( RECOMPILE ); + + SELECT '#p' AS table_name, * + FROM #p + OPTION ( RECOMPILE ); + + SELECT '#checkversion' AS table_name, * + FROM #checkversion + OPTION ( RECOMPILE ); + + SELECT '#configuration' AS table_name, * + FROM #configuration + OPTION ( RECOMPILE ); + + SELECT '#stored_proc_info' AS table_name, * + FROM #stored_proc_info + OPTION ( RECOMPILE ); + + SELECT '#conversion_info' AS table_name, * + FROM #conversion_info AS ci + OPTION ( RECOMPILE ); + + SELECT '#variable_info' AS table_name, * + FROM #variable_info AS vi + OPTION ( RECOMPILE ); + + SELECT '#missing_index_xml' AS table_name, * + FROM #missing_index_xml AS mix + OPTION ( RECOMPILE ); + + SELECT '#missing_index_schema' AS table_name, * + FROM #missing_index_schema AS mis + OPTION ( RECOMPILE ); + + SELECT '#missing_index_usage' AS table_name, * + FROM #missing_index_usage AS miu + OPTION ( RECOMPILE ); + + SELECT '#missing_index_detail' AS table_name, * + FROM #missing_index_detail AS mid + OPTION ( RECOMPILE ); + + SELECT '#missing_index_pretty' AS table_name, * + FROM #missing_index_pretty AS mip + OPTION ( RECOMPILE ); + + SELECT '#plan_creation' AS table_name, * + FROM #plan_creation + OPTION ( RECOMPILE ); + + SELECT '#plan_cost' AS table_name, * + FROM #plan_cost + OPTION ( RECOMPILE ); + + SELECT '#proc_costs' AS table_name, * + FROM #proc_costs + OPTION ( RECOMPILE ); + + SELECT '#stats_agg' AS table_name, * + FROM #stats_agg + OPTION ( RECOMPILE ); + + SELECT '#trace_flags' AS table_name, * + FROM #trace_flags + OPTION ( RECOMPILE ); + + SELECT '#plan_usage' AS table_name, * + FROM #plan_usage + OPTION ( RECOMPILE ); + + END; + + IF @OutputTableName IS NOT NULL + --Allow for output to ##DB so don't check for DB or schema name here + GOTO OutputResultsToTable; +RETURN; --Avoid going into the AllSort GOTO + +/*Begin code to sort by all*/ +AllSorts: +RAISERROR('Beginning all sort loop', 0, 1) WITH NOWAIT; + + +IF ( + @Top > 10 + AND @SkipAnalysis = 0 + AND @BringThePain = 0 + ) + BEGIN + RAISERROR( + ' + You''ve chosen a value greater than 10 to sort the whole plan cache by. + That can take a long time and harm performance. + Please choose a number <= 10, or set @BringThePain = 1 to signify you understand this might be a bad idea. + ', 0, 1) WITH NOWAIT; + RETURN; + END; + + +IF OBJECT_ID('tempdb..#checkversion_allsort') IS NULL + BEGIN + CREATE TABLE #checkversion_allsort + ( + version NVARCHAR(128), + common_version AS SUBSTRING(version, 1, CHARINDEX('.', version) + 1), + major AS PARSENAME(CONVERT(VARCHAR(32), version), 4), + minor AS PARSENAME(CONVERT(VARCHAR(32), version), 3), + build AS PARSENAME(CONVERT(VARCHAR(32), version), 2), + revision AS PARSENAME(CONVERT(VARCHAR(32), version), 1) + ); + + INSERT INTO #checkversion_allsort + (version) + SELECT CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)) + OPTION ( RECOMPILE ); + END; + + +SELECT @v = common_version, + @build = build +FROM #checkversion_allsort +OPTION ( RECOMPILE ); + +IF OBJECT_ID('tempdb.. #bou_allsort') IS NULL + BEGIN + CREATE TABLE #bou_allsort + ( + Id INT IDENTITY(1, 1), + DatabaseName NVARCHAR(128), + Cost FLOAT, + QueryText NVARCHAR(MAX), + QueryType NVARCHAR(258), + Warnings VARCHAR(MAX), + QueryPlan XML, + missing_indexes XML, + implicit_conversion_info XML, + cached_execution_parameters XML, + ExecutionCount NVARCHAR(30), + ExecutionsPerMinute MONEY, + ExecutionWeight MONEY, + TotalCPU NVARCHAR(30), + AverageCPU NVARCHAR(30), + CPUWeight MONEY, + TotalDuration NVARCHAR(30), + AverageDuration NVARCHAR(30), + DurationWeight MONEY, + TotalReads NVARCHAR(30), + AverageReads NVARCHAR(30), + ReadWeight MONEY, + TotalWrites NVARCHAR(30), + AverageWrites NVARCHAR(30), + WriteWeight MONEY, + AverageReturnedRows MONEY, + MinGrantKB NVARCHAR(30), + MaxGrantKB NVARCHAR(30), + MinUsedGrantKB NVARCHAR(30), + MaxUsedGrantKB NVARCHAR(30), + AvgMaxMemoryGrant MONEY, + MinSpills NVARCHAR(30), + MaxSpills NVARCHAR(30), + TotalSpills NVARCHAR(30), + AvgSpills MONEY, + PlanCreationTime DATETIME, + LastExecutionTime DATETIME, + LastCompletionTime DATETIME, + PlanHandle VARBINARY(64), + SqlHandle VARBINARY(64), + SetOptions VARCHAR(MAX), + QueryHash BINARY(8), + PlanGenerationNum NVARCHAR(30), + RemovePlanHandleFromCache NVARCHAR(200), + Pattern NVARCHAR(20) + ); + END; + + +IF @SortOrder = 'all' +BEGIN +RAISERROR('Beginning for ALL', 0, 1) WITH NOWAIT; +SET @AllSortSql += N' + DECLARE @ISH NVARCHAR(MAX) = N'''' + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''cpu'', + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''cpu'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''reads'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''reads'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''writes'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''writes'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''duration'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''duration'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''executions'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''executions'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + '; + + IF @VersionShowsMemoryGrants = 0 + BEGIN + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF @VersionShowsMemoryGrants = 1 + BEGIN + SET @AllSortSql += N' SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''memory grant'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''memory grant'' WHERE Pattern IS NULL OPTION(RECOMPILE);'; + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF @VersionShowsSpills = 0 + BEGIN + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF @VersionShowsSpills = 1 + BEGIN + SET @AllSortSql += N' SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''spills'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''spills'' WHERE Pattern IS NULL OPTION(RECOMPILE);'; + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF(@OutputType <> 'NONE') + BEGIN + SET @AllSortSql += N' SELECT DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters,ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache, Pattern + FROM #bou_allsort + ORDER BY Id + OPTION(RECOMPILE); '; + END; +END; + + +IF @SortOrder = 'all avg' +BEGIN +RAISERROR('Beginning for ALL AVG', 0, 1) WITH NOWAIT; +SET @AllSortSql += N' + DECLARE @ISH NVARCHAR(MAX) = N'''' + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg cpu'', + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg cpu'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg reads'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg reads'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg writes'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg writes'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg duration'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg duration'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg executions'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg executions'' WHERE Pattern IS NULL OPTION(RECOMPILE); + + '; + + IF @VersionShowsMemoryGrants = 0 + BEGIN + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF @VersionShowsMemoryGrants = 1 + BEGIN + SET @AllSortSql += N' SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg memory grant'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg memory grant'' WHERE Pattern IS NULL OPTION(RECOMPILE);'; + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF @VersionShowsSpills = 0 + BEGIN + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF @VersionShowsSpills = 1 + BEGIN + SET @AllSortSql += N' SELECT TOP 1 @ISH = STUFF((SELECT DISTINCT N'','' + CONVERT(NVARCHAR(MAX),b2.SqlHandle, 1) FROM #bou_allsort AS b2 FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 1, N'''') OPTION(RECOMPILE); + + INSERT #bou_allsort ( DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters, ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache ) + + EXEC sp_BlitzCache @ExpertMode = 0, @HideSummary = 1, @Top = @i_Top, @SortOrder = ''avg spills'', @IgnoreSqlHandles = @ISH, + @DatabaseName = @i_DatabaseName, @SkipAnalysis = @i_SkipAnalysis, @OutputDatabaseName = @i_OutputDatabaseName, @OutputSchemaName = @i_OutputSchemaName, @OutputTableName = @i_OutputTableName, @CheckDateOverride = @i_CheckDateOverride, @MinutesBack = @i_MinutesBack WITH RECOMPILE; + + UPDATE #bou_allsort SET Pattern = ''avg memory grant'' WHERE Pattern IS NULL OPTION(RECOMPILE);'; + IF @ExportToExcel = 1 + BEGIN + SET @AllSortSql += N' UPDATE #bou_allsort + SET + QueryPlan = NULL, + implicit_conversion_info = NULL, + cached_execution_parameters = NULL, + missing_indexes = NULL + OPTION (RECOMPILE); + + UPDATE ##BlitzCacheProcs + SET QueryText = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(QueryText)),'' '',''<>''),''><'',''''),''<>'','' ''), 1, 32000) + OPTION(RECOMPILE);'; + END; + + END; + + IF(@OutputType <> 'NONE') + BEGIN + SET @AllSortSql += N' SELECT DatabaseName, Cost, QueryText, QueryType, Warnings, QueryPlan, missing_indexes, implicit_conversion_info, cached_execution_parameters,ExecutionCount, ExecutionsPerMinute, ExecutionWeight, + TotalCPU, AverageCPU, CPUWeight, TotalDuration, AverageDuration, DurationWeight, TotalReads, AverageReads, + ReadWeight, TotalWrites, AverageWrites, WriteWeight, AverageReturnedRows, MinGrantKB, MaxGrantKB, MinUsedGrantKB, + MaxUsedGrantKB, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, SetOptions, QueryHash, PlanGenerationNum, RemovePlanHandleFromCache, Pattern + FROM #bou_allsort + ORDER BY Id + OPTION(RECOMPILE); '; + END; +END; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@AllSortSql, 0, 4000); + PRINT SUBSTRING(@AllSortSql, 4000, 8000); + PRINT SUBSTRING(@AllSortSql, 8000, 12000); + PRINT SUBSTRING(@AllSortSql, 12000, 16000); + PRINT SUBSTRING(@AllSortSql, 16000, 20000); + PRINT SUBSTRING(@AllSortSql, 20000, 24000); + PRINT SUBSTRING(@AllSortSql, 24000, 28000); + PRINT SUBSTRING(@AllSortSql, 28000, 32000); + PRINT SUBSTRING(@AllSortSql, 32000, 36000); + PRINT SUBSTRING(@AllSortSql, 36000, 40000); + END; + + EXEC sys.sp_executesql @stmt = @AllSortSql, @params = N'@i_DatabaseName NVARCHAR(128), @i_Top INT, @i_SkipAnalysis BIT, @i_OutputDatabaseName NVARCHAR(258), @i_OutputSchemaName NVARCHAR(258), @i_OutputTableName NVARCHAR(258), @i_CheckDateOverride DATETIMEOFFSET, @i_MinutesBack INT ', + @i_DatabaseName = @DatabaseName, @i_Top = @Top, @i_SkipAnalysis = @SkipAnalysis, @i_OutputDatabaseName = @OutputDatabaseName, @i_OutputSchemaName = @OutputSchemaName, @i_OutputTableName = @OutputTableName, @i_CheckDateOverride = @CheckDateOverride, @i_MinutesBack = @MinutesBack; + +/*End of AllSort section*/ + + +/*Begin code to write results to table */ +OutputResultsToTable: + +RAISERROR('Writing results to table.', 0, 1) WITH NOWAIT; + +SELECT @OutputServerName = QUOTENAME(@OutputServerName), + @OutputDatabaseName = QUOTENAME(@OutputDatabaseName), + @OutputSchemaName = QUOTENAME(@OutputSchemaName), + @OutputTableName = QUOTENAME(@OutputTableName); + +/* Checks if @OutputServerName is populated with a valid linked server, and that the database name specified is valid */ +DECLARE @ValidOutputServer BIT; +DECLARE @ValidOutputLocation BIT; +DECLARE @LinkedServerDBCheck NVARCHAR(2000); +DECLARE @ValidLinkedServerDB INT; +DECLARE @tmpdbchk table (cnt int); +IF @OutputServerName IS NOT NULL + BEGIN + + IF @Debug IN (1, 2) RAISERROR('Outputting to a remote server.', 0, 1) WITH NOWAIT; + + IF EXISTS (SELECT server_id FROM sys.servers WHERE QUOTENAME([name]) = @OutputServerName) + BEGIN + SET @LinkedServerDBCheck = 'SELECT 1 WHERE EXISTS (SELECT * FROM '+@OutputServerName+'.master.sys.databases WHERE QUOTENAME([name]) = '''+@OutputDatabaseName+''')'; + INSERT INTO @tmpdbchk EXEC sys.sp_executesql @LinkedServerDBCheck; + SET @ValidLinkedServerDB = (SELECT COUNT(*) FROM @tmpdbchk); + IF (@ValidLinkedServerDB > 0) + BEGIN + SET @ValidOutputServer = 1; + SET @ValidOutputLocation = 1; + END; + ELSE + RAISERROR('The specified database was not found on the output server', 16, 0); + END; + ELSE + BEGIN + RAISERROR('The specified output server was not found', 16, 0); + END; + END; +ELSE + BEGIN + IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + SET @ValidOutputLocation = 1; + END; + ELSE IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND NOT EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + RAISERROR('The specified output database was not found on this server', 16, 0); + END; + ELSE + BEGIN + SET @ValidOutputLocation = 0; + END; + END; + + /* @OutputTableName lets us export the results to a permanent table */ + DECLARE @StringToExecute NVARCHAR(MAX) = N'' ; + + IF @ValidOutputLocation = 1 + BEGIN + SET @StringToExecute = N'USE ' + + @OutputDatabaseName + + N'; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + N'.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + N''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + N'.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + N''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableName + N''') CREATE TABLE ' + + @OutputSchemaName + N'.' + + @OutputTableName + + CONVERT + ( + nvarchar(MAX), + N'(ID bigint NOT NULL IDENTITY(1,1), + ServerName NVARCHAR(258), + CheckDate DATETIMEOFFSET, + Version NVARCHAR(258), + QueryType NVARCHAR(258), + Warnings varchar(max), + DatabaseName sysname, + SerialDesiredMemory float, + SerialRequiredMemory float, + AverageCPU bigint, + TotalCPU bigint, + PercentCPUByType money, + CPUWeight money, + AverageDuration bigint, + TotalDuration bigint, + DurationWeight money, + PercentDurationByType money, + AverageReads bigint, + TotalReads bigint, + ReadWeight money, + PercentReadsByType money, + AverageWrites bigint, + TotalWrites bigint, + WriteWeight money, + PercentWritesByType money, + ExecutionCount bigint, + ExecutionWeight money, + PercentExecutionsByType money, + ExecutionsPerMinute money, + PlanCreationTime datetime,' + N' + PlanCreationTimeHours AS DATEDIFF(HOUR,CONVERT(DATETIMEOFFSET(7),[PlanCreationTime]),[CheckDate]), + LastExecutionTime datetime, + LastCompletionTime datetime, + PlanHandle varbinary(64), + [Remove Plan Handle From Cache] AS + CASE WHEN [PlanHandle] IS NOT NULL + THEN ''DBCC FREEPROCCACHE ('' + CONVERT(VARCHAR(128), [PlanHandle], 1) + '');'' + ELSE ''N/A'' END, + SqlHandle varbinary(64), + [Remove SQL Handle From Cache] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN ''DBCC FREEPROCCACHE ('' + CONVERT(VARCHAR(128), [SqlHandle], 1) + '');'' + ELSE ''N/A'' END, + [SQL Handle More Info] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN ''EXEC sp_BlitzCache @OnlySqlHandles = '''''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + ''''''; '' + ELSE ''N/A'' END, + QueryHash binary(8), + [Query Hash More Info] AS + CASE WHEN [QueryHash] IS NOT NULL + THEN ''EXEC sp_BlitzCache @OnlyQueryHashes = '''''' + CONVERT(VARCHAR(32), [QueryHash], 1) + ''''''; '' + ELSE ''N/A'' END, + QueryPlanHash binary(8), + StatementStartOffset int, + StatementEndOffset int, + PlanGenerationNum bigint, + MinReturnedRows bigint, + MaxReturnedRows bigint, + AverageReturnedRows money, + TotalReturnedRows bigint, + QueryText nvarchar(max), + QueryPlan xml, + NumberOfPlans int, + NumberOfDistinctPlans int, + MinGrantKB BIGINT, + MaxGrantKB BIGINT, + MinUsedGrantKB BIGINT, + MaxUsedGrantKB BIGINT, + PercentMemoryGrantUsed MONEY, + AvgMaxMemoryGrant MONEY, + MinSpills BIGINT, + MaxSpills BIGINT, + TotalSpills BIGINT, + AvgSpills MONEY, + QueryPlanCost FLOAT, + Pattern NVARCHAR(20), + JoinKey AS ServerName + Cast(CheckDate AS NVARCHAR(50)), + CONSTRAINT [PK_' + REPLACE(REPLACE(@OutputTableName,N'[',N''),N']',N'') + N'] PRIMARY KEY CLUSTERED(ID ASC));' + ); + + SET @StringToExecute += N'IF EXISTS(SELECT * FROM ' + +@OutputDatabaseName + +N'.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + +@OutputSchemaName + +N''') AND EXISTS (SELECT * FROM ' + +@OutputDatabaseName+ + N'.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + +@OutputSchemaName + +N''' AND QUOTENAME(TABLE_NAME) = ''' + +@OutputTableName + +N''') AND EXISTS (SELECT * FROM ' + +@OutputDatabaseName+ + N'.sys.computed_columns WHERE [name] = N''PlanCreationTimeHours'' AND QUOTENAME(OBJECT_NAME(object_id)) = N''' + +@OutputTableName + +N''' AND [definition] = N''(datediff(hour,[PlanCreationTime],sysdatetime()))'') +BEGIN + RAISERROR(''We noticed that you are running an old computed column definition for PlanCreationTimeHours, fixing that now'',0,0) WITH NOWAIT; + ALTER TABLE '+@OutputDatabaseName+N'.'+@OutputSchemaName+N'.'+@OutputTableName+N' DROP COLUMN [PlanCreationTimeHours]; + ALTER TABLE '+@OutputDatabaseName+N'.'+@OutputSchemaName+N'.'+@OutputTableName+N' ADD [PlanCreationTimeHours] AS DATEDIFF(HOUR,CONVERT(DATETIMEOFFSET(7),[PlanCreationTime]),[CheckDate]); +END '; + + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,''''+@OutputSchemaName+'''',''''''+@OutputSchemaName+''''''); + SET @StringToExecute = REPLACE(@StringToExecute,''''+@OutputTableName+'''',''''''+@OutputTableName+''''''); + SET @StringToExecute = REPLACE(@StringToExecute,'xml','nvarchar(max)'); + SET @StringToExecute = REPLACE(@StringToExecute,'''DBCC FREEPROCCACHE ('' + CONVERT(VARCHAR(128), [PlanHandle], 1) + '');''','''''DBCC FREEPROCCACHE ('''' + CONVERT(VARCHAR(128), [PlanHandle], 1) + '''');'''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''DBCC FREEPROCCACHE ('' + CONVERT(VARCHAR(128), [SqlHandle], 1) + '');''','''''DBCC FREEPROCCACHE ('''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + '''');'''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''EXEC sp_BlitzCache @OnlySqlHandles = '''''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + ''''''; ''','''''EXEC sp_BlitzCache @OnlySqlHandles = '''''''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + ''''''''; '''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''EXEC sp_BlitzCache @OnlyQueryHashes = '''''' + CONVERT(VARCHAR(32), [QueryHash], 1) + ''''''; ''','''''EXEC sp_BlitzCache @OnlyQueryHashes = '''''''' + CONVERT(VARCHAR(32), [QueryHash], 1) + ''''''''; '''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''N/A''','''''N/A'''''); + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@StringToExecute, 0, 4000); + PRINT SUBSTRING(@StringToExecute, 4000, 8000); + PRINT SUBSTRING(@StringToExecute, 8000, 12000); + PRINT SUBSTRING(@StringToExecute, 12000, 16000); + PRINT SUBSTRING(@StringToExecute, 16000, 20000); + PRINT SUBSTRING(@StringToExecute, 20000, 24000); + PRINT SUBSTRING(@StringToExecute, 24000, 28000); + PRINT SUBSTRING(@StringToExecute, 28000, 32000); + PRINT SUBSTRING(@StringToExecute, 32000, 36000); + PRINT SUBSTRING(@StringToExecute, 36000, 40000); + END; + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@StringToExecute, 0, 4000); + PRINT SUBSTRING(@StringToExecute, 4000, 8000); + PRINT SUBSTRING(@StringToExecute, 8000, 12000); + PRINT SUBSTRING(@StringToExecute, 12000, 16000); + PRINT SUBSTRING(@StringToExecute, 16000, 20000); + PRINT SUBSTRING(@StringToExecute, 20000, 24000); + PRINT SUBSTRING(@StringToExecute, 24000, 28000); + PRINT SUBSTRING(@StringToExecute, 28000, 32000); + PRINT SUBSTRING(@StringToExecute, 32000, 36000); + PRINT SUBSTRING(@StringToExecute, 36000, 40000); + END; + EXEC(@StringToExecute); + END; + + /* If the table doesn't have the new LastCompletionTime column, add it. See Github #2377. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + ''')) AND name = ''LastCompletionTime'') + ALTER TABLE ' + @ObjectFullName + N' ADD LastCompletionTime DATETIME NULL;'; + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''LastCompletionTime''','''''LastCompletionTime'''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''' + @ObjectFullName + '''','''''' + @ObjectFullName + ''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END; + + /* If the table doesn't have the new PlanGenerationNum column, add it. See Github #2514. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''PlanGenerationNum'') + ALTER TABLE ' + @ObjectFullName + N' ADD PlanGenerationNum BIGINT NULL;'; + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''PlanGenerationNum''','''''PlanGenerationNum'''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''' + @ObjectFullName + '''','''''' + @ObjectFullName + ''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END; + + /* If the table doesn't have the new Pattern column, add it */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''Pattern'') + ALTER TABLE ' + @ObjectFullName + N' ADD Pattern NVARCHAR(20) NULL;'; + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''Pattern''','''''Pattern'''''); + SET @StringToExecute = REPLACE(@StringToExecute,'''' + @ObjectFullName + '''','''''' + @ObjectFullName + ''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END + + IF @CheckDateOverride IS NULL + BEGIN + SET @CheckDateOverride = SYSDATETIMEOFFSET(); + END; + + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputServerName + '.' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputServerName + '.' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, Version, QueryType, DatabaseName, AverageCPU, TotalCPU, PercentCPUByType, CPUWeight, AverageDuration, TotalDuration, DurationWeight, PercentDurationByType, AverageReads, TotalReads, ReadWeight, PercentReadsByType, ' + + ' AverageWrites, TotalWrites, WriteWeight, PercentWritesByType, ExecutionCount, ExecutionWeight, PercentExecutionsByType, ' + + ' ExecutionsPerMinute, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, QueryHash, QueryPlanHash, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, QueryText, QueryPlan, NumberOfPlans, NumberOfDistinctPlans, Warnings, ' + + ' SerialRequiredMemory, SerialDesiredMemory, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, QueryPlanCost, Pattern ) ' + + 'SELECT TOP (@Top) ' + + QUOTENAME(CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)), '''') + ', @CheckDateOverride, ' + + QUOTENAME(CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)), '''') + ', ' + + ' QueryType, DatabaseName, AverageCPU, TotalCPU, PercentCPUByType, PercentCPU, AverageDuration, TotalDuration, PercentDuration, PercentDurationByType, AverageReads, TotalReads, PercentReads, PercentReadsByType, ' + + ' AverageWrites, TotalWrites, PercentWrites, PercentWritesByType, ExecutionCount, PercentExecutions, PercentExecutionsByType, ' + + ' ExecutionsPerMinute, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, QueryHash, QueryPlanHash, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, QueryText, CAST(QueryPlan AS NVARCHAR(MAX)), NumberOfPlans, NumberOfDistinctPlans, Warnings, ' + + ' SerialRequiredMemory, SerialDesiredMemory, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, QueryPlanCost, Pattern ' + + ' FROM ##BlitzCacheProcs ' + + ' WHERE 1=1 '; + + IF @MinimumExecutionCount IS NOT NULL + BEGIN + SET @StringToExecute += N' AND ExecutionCount >= @MinimumExecutionCount '; + END; + IF @MinutesBack IS NOT NULL + BEGIN + SET @StringToExecute += N' AND LastCompletionTime >= DATEADD(MINUTE, @min_back, GETDATE() ) '; + END; + SET @StringToExecute += N' AND SPID = @@SPID '; + SELECT @StringToExecute += N' ORDER BY ' + CASE @SortOrder WHEN 'cpu' THEN N' TotalCPU ' + WHEN N'reads' THEN N' TotalReads ' + WHEN N'writes' THEN N' TotalWrites ' + WHEN N'duration' THEN N' TotalDuration ' + WHEN N'executions' THEN N' ExecutionCount ' + WHEN N'compiles' THEN N' PlanCreationTime ' + WHEN N'memory grant' THEN N' MaxGrantKB' + WHEN N'spills' THEN N' MaxSpills' + WHEN N'avg cpu' THEN N' AverageCPU' + WHEN N'avg reads' THEN N' AverageReads' + WHEN N'avg writes' THEN N' AverageWrites' + WHEN N'avg duration' THEN N' AverageDuration' + WHEN N'avg executions' THEN N' ExecutionsPerMinute' + WHEN N'avg memory grant' THEN N' AvgMaxMemoryGrant' + WHEN N'avg spills' THEN N' AvgSpills' + WHEN N'unused grant' THEN N' MaxGrantKB - MaxUsedGrantKB' + ELSE N' TotalCPU ' + END + N' DESC '; + SET @StringToExecute += N' OPTION (RECOMPILE) ; '; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@StringToExecute, 0, 4000); + PRINT SUBSTRING(@StringToExecute, 4000, 8000); + PRINT SUBSTRING(@StringToExecute, 8000, 12000); + PRINT SUBSTRING(@StringToExecute, 12000, 16000); + PRINT SUBSTRING(@StringToExecute, 16000, 20000); + PRINT SUBSTRING(@StringToExecute, 20000, 24000); + PRINT SUBSTRING(@StringToExecute, 24000, 28000); + PRINT SUBSTRING(@StringToExecute, 28000, 32000); + PRINT SUBSTRING(@StringToExecute, 32000, 36000); + PRINT SUBSTRING(@StringToExecute, 36000, 40000); + END; + + EXEC sp_executesql @StringToExecute, N'@Top INT, @min_duration INT, @min_back INT, @CheckDateOverride DATETIMEOFFSET, @MinimumExecutionCount INT', @Top, @DurationFilter_i, @MinutesBack, @CheckDateOverride, @MinimumExecutionCount; + END; + ELSE + BEGIN + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, Version, QueryType, DatabaseName, AverageCPU, TotalCPU, PercentCPUByType, CPUWeight, AverageDuration, TotalDuration, DurationWeight, PercentDurationByType, AverageReads, TotalReads, ReadWeight, PercentReadsByType, ' + + ' AverageWrites, TotalWrites, WriteWeight, PercentWritesByType, ExecutionCount, ExecutionWeight, PercentExecutionsByType, ' + + ' ExecutionsPerMinute, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, QueryHash, QueryPlanHash, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, QueryText, QueryPlan, NumberOfPlans, NumberOfDistinctPlans, Warnings, ' + + ' SerialRequiredMemory, SerialDesiredMemory, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, QueryPlanCost, Pattern ) ' + + 'SELECT TOP (@Top) ' + + QUOTENAME(CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)), '''') + ', @CheckDateOverride, ' + + QUOTENAME(CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)), '''') + ', ' + + ' QueryType, DatabaseName, AverageCPU, TotalCPU, PercentCPUByType, PercentCPU, AverageDuration, TotalDuration, PercentDuration, PercentDurationByType, AverageReads, TotalReads, PercentReads, PercentReadsByType, ' + + ' AverageWrites, TotalWrites, PercentWrites, PercentWritesByType, ExecutionCount, PercentExecutions, PercentExecutionsByType, ' + + ' ExecutionsPerMinute, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, QueryHash, QueryPlanHash, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, QueryText, QueryPlan, NumberOfPlans, NumberOfDistinctPlans, Warnings, ' + + ' SerialRequiredMemory, SerialDesiredMemory, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, QueryPlanCost, Pattern ' + + ' FROM ##BlitzCacheProcs ' + + ' WHERE 1=1 '; + + IF @MinimumExecutionCount IS NOT NULL + BEGIN + SET @StringToExecute += N' AND ExecutionCount >= @MinimumExecutionCount '; + END; + IF @MinutesBack IS NOT NULL + BEGIN + SET @StringToExecute += N' AND LastCompletionTime >= DATEADD(MINUTE, @min_back, GETDATE() ) '; + END; + SET @StringToExecute += N' AND SPID = @@SPID '; + SELECT @StringToExecute += N' ORDER BY ' + CASE @SortOrder WHEN 'cpu' THEN N' TotalCPU ' + WHEN N'reads' THEN N' TotalReads ' + WHEN N'writes' THEN N' TotalWrites ' + WHEN N'duration' THEN N' TotalDuration ' + WHEN N'executions' THEN N' ExecutionCount ' + WHEN N'compiles' THEN N' PlanCreationTime ' + WHEN N'memory grant' THEN N' MaxGrantKB' + WHEN N'spills' THEN N' MaxSpills' + WHEN N'avg cpu' THEN N' AverageCPU' + WHEN N'avg reads' THEN N' AverageReads' + WHEN N'avg writes' THEN N' AverageWrites' + WHEN N'avg duration' THEN N' AverageDuration' + WHEN N'avg executions' THEN N' ExecutionsPerMinute' + WHEN N'avg memory grant' THEN N' AvgMaxMemoryGrant' + WHEN N'avg spills' THEN N' AvgSpills' + WHEN N'unused grant' THEN N' MaxGrantKB - MaxUsedGrantKB' + ELSE N' TotalCPU ' + END + N' DESC '; + SET @StringToExecute += N' OPTION (RECOMPILE) ; '; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@StringToExecute, 0, 4000); + PRINT SUBSTRING(@StringToExecute, 4000, 8000); + PRINT SUBSTRING(@StringToExecute, 8000, 12000); + PRINT SUBSTRING(@StringToExecute, 12000, 16000); + PRINT SUBSTRING(@StringToExecute, 16000, 20000); + PRINT SUBSTRING(@StringToExecute, 20000, 24000); + PRINT SUBSTRING(@StringToExecute, 24000, 28000); + PRINT SUBSTRING(@StringToExecute, 28000, 32000); + PRINT SUBSTRING(@StringToExecute, 32000, 36000); + PRINT SUBSTRING(@StringToExecute, 36000, 40000); + END; + + EXEC sp_executesql @StringToExecute, N'@Top INT, @min_duration INT, @min_back INT, @CheckDateOverride DATETIMEOFFSET, @MinimumExecutionCount INT', @Top, @DurationFilter_i, @MinutesBack, @CheckDateOverride, @MinimumExecutionCount; + END; + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 2) = '##') + BEGIN + IF @ValidOutputServer = 1 + BEGIN + RAISERROR('Due to the nature of temporary tables, outputting to a linked server requires a permanent table.', 16, 0); + END; + ELSE IF @OutputTableName IN ('##BlitzCacheProcs','##BlitzCacheResults') + BEGIN + RAISERROR('OutputTableName is a reserved name for this procedure. We only use ##BlitzCacheProcs and ##BlitzCacheResults, please choose another table name.', 16, 0); + END; + ELSE + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''tempdb..' + + @OutputTableName + + ''') IS NOT NULL) DROP TABLE ' + @OutputTableName + ';' + + 'CREATE TABLE ' + + @OutputTableName + + ' (ID bigint NOT NULL IDENTITY(1,1), + ServerName NVARCHAR(258), + CheckDate DATETIMEOFFSET, + Version NVARCHAR(258), + QueryType NVARCHAR(258), + Warnings varchar(max), + DatabaseName sysname, + SerialDesiredMemory float, + SerialRequiredMemory float, + AverageCPU bigint, + TotalCPU bigint, + PercentCPUByType money, + CPUWeight money, + AverageDuration bigint, + TotalDuration bigint, + DurationWeight money, + PercentDurationByType money, + AverageReads bigint, + TotalReads bigint, + ReadWeight money, + PercentReadsByType money, + AverageWrites bigint, + TotalWrites bigint, + WriteWeight money, + PercentWritesByType money, + ExecutionCount bigint, + ExecutionWeight money, + PercentExecutionsByType money, + ExecutionsPerMinute money, + PlanCreationTime datetime,' + N' + PlanCreationTimeHours AS DATEDIFF(HOUR, PlanCreationTime, SYSDATETIME()), + LastExecutionTime datetime, + LastCompletionTime datetime, + PlanHandle varbinary(64), + [Remove Plan Handle From Cache] AS + CASE WHEN [PlanHandle] IS NOT NULL + THEN ''DBCC FREEPROCCACHE ('' + CONVERT(VARCHAR(128), [PlanHandle], 1) + '');'' + ELSE ''N/A'' END, + SqlHandle varbinary(64), + [Remove SQL Handle From Cache] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN ''DBCC FREEPROCCACHE ('' + CONVERT(VARCHAR(128), [SqlHandle], 1) + '');'' + ELSE ''N/A'' END, + [SQL Handle More Info] AS + CASE WHEN [SqlHandle] IS NOT NULL + THEN ''EXEC sp_BlitzCache @OnlySqlHandles = '''''' + CONVERT(VARCHAR(128), [SqlHandle], 1) + ''''''; '' + ELSE ''N/A'' END, + QueryHash binary(8), + [Query Hash More Info] AS + CASE WHEN [QueryHash] IS NOT NULL + THEN ''EXEC sp_BlitzCache @OnlyQueryHashes = '''''' + CONVERT(VARCHAR(32), [QueryHash], 1) + ''''''; '' + ELSE ''N/A'' END, + QueryPlanHash binary(8), + StatementStartOffset int, + StatementEndOffset int, + PlanGenerationNum bigint, + MinReturnedRows bigint, + MaxReturnedRows bigint, + AverageReturnedRows money, + TotalReturnedRows bigint, + QueryText nvarchar(max), + QueryPlan xml, + NumberOfPlans int, + NumberOfDistinctPlans int, + MinGrantKB BIGINT, + MaxGrantKB BIGINT, + MinUsedGrantKB BIGINT, + MaxUsedGrantKB BIGINT, + PercentMemoryGrantUsed MONEY, + AvgMaxMemoryGrant MONEY, + MinSpills BIGINT, + MaxSpills BIGINT, + TotalSpills BIGINT, + AvgSpills MONEY, + QueryPlanCost FLOAT, + Pattern NVARCHAR(20), + JoinKey AS ServerName + Cast(CheckDate AS NVARCHAR(50)), + CONSTRAINT [PK_' + REPLACE(REPLACE(@OutputTableName,'[',''),']','') + '] PRIMARY KEY CLUSTERED(ID ASC));'; + SET @StringToExecute += N' INSERT ' + + @OutputTableName + + ' (ServerName, CheckDate, Version, QueryType, DatabaseName, AverageCPU, TotalCPU, PercentCPUByType, CPUWeight, AverageDuration, TotalDuration, DurationWeight, PercentDurationByType, AverageReads, TotalReads, ReadWeight, PercentReadsByType, ' + + ' AverageWrites, TotalWrites, WriteWeight, PercentWritesByType, ExecutionCount, ExecutionWeight, PercentExecutionsByType, ' + + ' ExecutionsPerMinute, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, QueryHash, QueryPlanHash, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, QueryText, QueryPlan, NumberOfPlans, NumberOfDistinctPlans, Warnings, ' + + ' SerialRequiredMemory, SerialDesiredMemory, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, QueryPlanCost, Pattern ) ' + + 'SELECT TOP (@Top) ' + + QUOTENAME(CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)), '''') + ', @CheckDateOverride, ' + + QUOTENAME(CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)), '''') + ', ' + + ' QueryType, DatabaseName, AverageCPU, TotalCPU, PercentCPUByType, PercentCPU, AverageDuration, TotalDuration, PercentDuration, PercentDurationByType, AverageReads, TotalReads, PercentReads, PercentReadsByType, ' + + ' AverageWrites, TotalWrites, PercentWrites, PercentWritesByType, ExecutionCount, PercentExecutions, PercentExecutionsByType, ' + + ' ExecutionsPerMinute, PlanCreationTime, LastExecutionTime, LastCompletionTime, PlanHandle, SqlHandle, QueryHash, QueryPlanHash, StatementStartOffset, StatementEndOffset, PlanGenerationNum, MinReturnedRows, MaxReturnedRows, AverageReturnedRows, TotalReturnedRows, QueryText, QueryPlan, NumberOfPlans, NumberOfDistinctPlans, Warnings, ' + + ' SerialRequiredMemory, SerialDesiredMemory, MinGrantKB, MaxGrantKB, MinUsedGrantKB, MaxUsedGrantKB, PercentMemoryGrantUsed, AvgMaxMemoryGrant, MinSpills, MaxSpills, TotalSpills, AvgSpills, QueryPlanCost, Pattern ' + + ' FROM ##BlitzCacheProcs ' + + ' WHERE 1=1 '; + + IF @MinimumExecutionCount IS NOT NULL + BEGIN + SET @StringToExecute += N' AND ExecutionCount >= @MinimumExecutionCount '; + END; + + IF @MinutesBack IS NOT NULL + BEGIN + SET @StringToExecute += N' AND LastCompletionTime >= DATEADD(MINUTE, @min_back, GETDATE() ) '; + END; + + SET @StringToExecute += N' AND SPID = @@SPID '; + + SELECT @StringToExecute += N' ORDER BY ' + CASE @SortOrder WHEN 'cpu' THEN N' TotalCPU ' + WHEN N'reads' THEN N' TotalReads ' + WHEN N'writes' THEN N' TotalWrites ' + WHEN N'duration' THEN N' TotalDuration ' + WHEN N'executions' THEN N' ExecutionCount ' + WHEN N'compiles' THEN N' PlanCreationTime ' + WHEN N'memory grant' THEN N' MaxGrantKB' + WHEN N'spills' THEN N' MaxSpills' + WHEN N'avg cpu' THEN N' AverageCPU' + WHEN N'avg reads' THEN N' AverageReads' + WHEN N'avg writes' THEN N' AverageWrites' + WHEN N'avg duration' THEN N' AverageDuration' + WHEN N'avg executions' THEN N' ExecutionsPerMinute' + WHEN N'avg memory grant' THEN N' AvgMaxMemoryGrant' + WHEN N'avg spills' THEN N' AvgSpills' + WHEN N'unused grant' THEN N' MaxGrantKB - MaxUsedGrantKB' + ELSE N' TotalCPU ' + END + N' DESC '; + + SET @StringToExecute += N' OPTION (RECOMPILE) ; '; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@StringToExecute, 0, 4000); + PRINT SUBSTRING(@StringToExecute, 4000, 8000); + PRINT SUBSTRING(@StringToExecute, 8000, 12000); + PRINT SUBSTRING(@StringToExecute, 12000, 16000); + PRINT SUBSTRING(@StringToExecute, 16000, 20000); + PRINT SUBSTRING(@StringToExecute, 20000, 24000); + PRINT SUBSTRING(@StringToExecute, 24000, 28000); + PRINT SUBSTRING(@StringToExecute, 28000, 32000); + PRINT SUBSTRING(@StringToExecute, 32000, 36000); + PRINT SUBSTRING(@StringToExecute, 36000, 40000); + PRINT SUBSTRING(@StringToExecute, 34000, 40000); + END; + EXEC sp_executesql @StringToExecute, N'@Top INT, @min_duration INT, @min_back INT, @CheckDateOverride DATETIMEOFFSET, @MinimumExecutionCount INT', @Top, @DurationFilter_i, @MinutesBack, @CheckDateOverride, @MinimumExecutionCount; + END; + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); +END; /* End of writing results to table */ + +END; /*Final End*/ + +GO +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; +SET ANSI_WARNINGS ON; +SET ARITHABORT ON; +SET CONCAT_NULL_YIELDS_NULL ON; +SET QUOTED_IDENTIFIER ON; +SET STATISTICS IO OFF; +SET STATISTICS TIME OFF; +GO + +IF OBJECT_ID('dbo.sp_BlitzIndex') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_BlitzIndex AS RETURN 0;'); +GO + +ALTER PROCEDURE dbo.sp_BlitzIndex + @DatabaseName NVARCHAR(128) = NULL, /*Defaults to current DB if not specified*/ + @SchemaName NVARCHAR(128) = NULL, /*Requires table_name as well.*/ + @TableName NVARCHAR(128) = NULL, /*Requires schema_name as well.*/ + @Mode TINYINT=0, /*0=Diagnose, 1=Summarize, 2=Index Usage Detail, 3=Missing Index Detail, 4=Diagnose Details*/ + /*Note:@Mode doesn't matter if you're specifying schema_name and @TableName.*/ + @Filter TINYINT = 0, /* 0=no filter (default). 1=No low-usage warnings for objects with 0 reads. 2=Only warn for objects >= 500MB */ + /*Note:@Filter doesn't do anything unless @Mode=0*/ + @SkipPartitions BIT = 0, + @SkipStatistics BIT = 1, + @GetAllDatabases BIT = 0, + @ShowColumnstoreOnly BIT = 0, /* Will show only the Row Group and Segment details for a table with a columnstore index. */ + @BringThePain BIT = 0, + @IgnoreDatabases NVARCHAR(MAX) = NULL, /* Comma-delimited list of databases you want to skip */ + @ThresholdMB INT = 250 /* Number of megabytes that an object must be before we include it in basic results */, + @OutputType VARCHAR(20) = 'TABLE' , + @OutputServerName NVARCHAR(256) = NULL , + @OutputDatabaseName NVARCHAR(256) = NULL , + @OutputSchemaName NVARCHAR(256) = NULL , + @OutputTableName NVARCHAR(256) = NULL , + @IncludeInactiveIndexes BIT = 0 /* Will skip indexes with no reads or writes */, + @ShowAllMissingIndexRequests BIT = 0 /*Will make all missing index requests show up*/, + @ShowPartitionRanges BIT = 0 /* Will add partition range values column to columnstore visualization */, + @SortOrder NVARCHAR(50) = NULL, /* Only affects @Mode = 2. */ + @SortDirection NVARCHAR(4) = 'DESC', /* Only affects @Mode = 2. */ + @Help TINYINT = 0, + @Debug BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS +SET NOCOUNT ON; +SET STATISTICS XML OFF; +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + +SELECT @Version = '8.16', @VersionDate = '20230820'; +SET @OutputType = UPPER(@OutputType); + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + +IF @Help = 1 +BEGIN +PRINT ' +/* +sp_BlitzIndex from http://FirstResponderKit.org + +This script analyzes the design and performance of your indexes. + +To learn more, visit http://FirstResponderKit.org where you can download new +versions for free, watch training videos on how it works, get more info on +the findings, contribute your own code, and more. + +Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. + - Index create statements are just to give you a rough idea of the syntax. It includes filters and fillfactor. + -- Example 1: index creates use ONLINE=? instead of ONLINE=ON / ONLINE=OFF. This is because it is important + for the user to understand if it is going to be offline and not just run a script. + -- Example 2: they do not include all the options the index may have been created with (padding, compression + filegroup/partition scheme etc.) + -- (The compression and filegroup index create syntax is not trivial because it is set at the partition + level and is not trivial to code.) + - Does not advise you about data modeling for clustered indexes and primary keys (primarily looks for signs of insanity.) + +Unknown limitations of this version: + - We knew them once, but we forgot. + + +MIT License + +Copyright (c) Brent Ozar Unlimited + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +'; +RETURN; +END; /* @Help = 1 */ + +DECLARE @ScriptVersionName NVARCHAR(50); +DECLARE @DaysUptime NUMERIC(23,2); +DECLARE @DatabaseID INT; +DECLARE @ObjectID INT; +DECLARE @dsql NVARCHAR(MAX); +DECLARE @params NVARCHAR(MAX); +DECLARE @msg NVARCHAR(4000); +DECLARE @ErrorSeverity INT; +DECLARE @ErrorState INT; +DECLARE @Rowcount BIGINT; +DECLARE @SQLServerProductVersion NVARCHAR(128); +DECLARE @SQLServerEdition INT; +DECLARE @FilterMB INT; +DECLARE @collation NVARCHAR(256); +DECLARE @NumDatabases INT; +DECLARE @LineFeed NVARCHAR(5); +DECLARE @DaysUptimeInsertValue NVARCHAR(256); +DECLARE @DatabaseToIgnore NVARCHAR(MAX); +DECLARE @ColumnList NVARCHAR(MAX); +DECLARE @ColumnListWithApostrophes NVARCHAR(MAX); +DECLARE @PartitionCount INT; +DECLARE @OptimizeForSequentialKey BIT = 0; +DECLARE @StringToExecute NVARCHAR(MAX); + + +/* Let's get @SortOrder set to lower case here for comparisons later */ +SET @SortOrder = REPLACE(LOWER(@SortOrder), N' ', N'_'); +SET @SortDirection = LOWER(@SortDirection); + +SET @LineFeed = CHAR(13) + CHAR(10); +SELECT @SQLServerProductVersion = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)); +SELECT @SQLServerEdition =CAST(SERVERPROPERTY('EngineEdition') AS INT); /* We default to online index creates where EngineEdition=3*/ +SET @FilterMB=250; +SELECT @ScriptVersionName = 'sp_BlitzIndex(TM) v' + @Version + ' - ' + DATENAME(MM, @VersionDate) + ' ' + RIGHT('0'+DATENAME(DD, @VersionDate),2) + ', ' + DATENAME(YY, @VersionDate); +SET @IgnoreDatabases = REPLACE(REPLACE(LTRIM(RTRIM(@IgnoreDatabases)), CHAR(10), ''), CHAR(13), ''); + +SELECT + @OptimizeForSequentialKey = + CASE WHEN EXISTS + ( + SELECT + 1/0 + FROM sys.all_columns AS ac + WHERE ac.object_id = OBJECT_ID('sys.indexes') + AND ac.name = N'optimize_for_sequential_key' + ) + THEN 1 + ELSE 0 + END; + +RAISERROR(N'Starting run. %s', 0,1, @ScriptVersionName) WITH NOWAIT; + + +IF(@OutputType NOT IN ('TABLE','NONE')) +BEGIN + RAISERROR('Invalid value for parameter @OutputType. Expected: (TABLE;NONE)',12,1); + RETURN; +END; + +IF(@OutputType = 'TABLE' AND NOT (@OutputTableName IS NULL AND @OutputSchemaName IS NULL AND @OutputDatabaseName IS NULL AND @OutputServerName IS NULL)) +BEGIN + RAISERROR(N'One or more output parameters specified in combination with TABLE output, changing to NONE output mode', 0,1) WITH NOWAIT; + SET @OutputType = 'NONE' +END; + +IF(@OutputType = 'NONE') +BEGIN + + IF ((@OutputServerName IS NOT NULL) AND (@OutputTableName IS NULL OR @OutputSchemaName IS NULL OR @OutputDatabaseName IS NULL)) + BEGIN + RAISERROR('Parameter @OutputServerName is specified, rest of @Output* parameters needs to also be specified',12,1); + RETURN; + END; + + IF(@OutputTableName IS NULL OR @OutputSchemaName IS NULL OR @OutputDatabaseName IS NULL) + BEGIN + RAISERROR('This procedure should be called with a value for @OutputTableName, @OutputSchemaName and @OutputDatabaseName parameters, as @OutputType is set to NONE',12,1); + RETURN; + END; + /* Output is supported for all modes, no reason to not bring pain and output + IF(@BringThePain = 1) + BEGIN + RAISERROR('Incompatible Parameters: @BringThePain set to 1 and @OutputType set to NONE',12,1); + RETURN; + END; + */ + /* Eventually limit by mode + IF(@Mode not in (0,4)) + BEGIN + RAISERROR('Incompatible Parameters: @Mode set to %d and @OutputType set to NONE',12,1,@Mode); + RETURN; + END; + */ +END; + +IF OBJECT_ID('tempdb..#IndexSanity') IS NOT NULL + DROP TABLE #IndexSanity; + +IF OBJECT_ID('tempdb..#IndexPartitionSanity') IS NOT NULL + DROP TABLE #IndexPartitionSanity; + +IF OBJECT_ID('tempdb..#IndexSanitySize') IS NOT NULL + DROP TABLE #IndexSanitySize; + +IF OBJECT_ID('tempdb..#IndexColumns') IS NOT NULL + DROP TABLE #IndexColumns; + +IF OBJECT_ID('tempdb..#MissingIndexes') IS NOT NULL + DROP TABLE #MissingIndexes; + +IF OBJECT_ID('tempdb..#ForeignKeys') IS NOT NULL + DROP TABLE #ForeignKeys; + +IF OBJECT_ID('tempdb..#UnindexedForeignKeys') IS NOT NULL + DROP TABLE #UnindexedForeignKeys; + +IF OBJECT_ID('tempdb..#BlitzIndexResults') IS NOT NULL + DROP TABLE #BlitzIndexResults; + +IF OBJECT_ID('tempdb..#IndexCreateTsql') IS NOT NULL + DROP TABLE #IndexCreateTsql; + +IF OBJECT_ID('tempdb..#DatabaseList') IS NOT NULL + DROP TABLE #DatabaseList; + +IF OBJECT_ID('tempdb..#Statistics') IS NOT NULL + DROP TABLE #Statistics; + +IF OBJECT_ID('tempdb..#PartitionCompressionInfo') IS NOT NULL + DROP TABLE #PartitionCompressionInfo; + +IF OBJECT_ID('tempdb..#ComputedColumns') IS NOT NULL + DROP TABLE #ComputedColumns; + +IF OBJECT_ID('tempdb..#TraceStatus') IS NOT NULL + DROP TABLE #TraceStatus; + +IF OBJECT_ID('tempdb..#TemporalTables') IS NOT NULL + DROP TABLE #TemporalTables; + +IF OBJECT_ID('tempdb..#CheckConstraints') IS NOT NULL + DROP TABLE #CheckConstraints; + +IF OBJECT_ID('tempdb..#FilteredIndexes') IS NOT NULL + DROP TABLE #FilteredIndexes; + +IF OBJECT_ID('tempdb..#Ignore_Databases') IS NOT NULL + DROP TABLE #Ignore_Databases + + RAISERROR (N'Create temp tables.',0,1) WITH NOWAIT; + CREATE TABLE #BlitzIndexResults + ( + blitz_result_id INT IDENTITY PRIMARY KEY, + check_id INT NOT NULL, + index_sanity_id INT NULL, + Priority INT NULL, + findings_group NVARCHAR(4000) NOT NULL, + finding NVARCHAR(200) NOT NULL, + [database_name] NVARCHAR(128) NULL, + URL NVARCHAR(200) NOT NULL, + details NVARCHAR(MAX) NOT NULL, + index_definition NVARCHAR(MAX) NOT NULL, + secret_columns NVARCHAR(MAX) NULL, + index_usage_summary NVARCHAR(MAX) NULL, + index_size_summary NVARCHAR(MAX) NULL, + create_tsql NVARCHAR(MAX) NULL, + more_info NVARCHAR(MAX) NULL, + sample_query_plan XML NULL + ); + + CREATE TABLE #IndexSanity + ( + [index_sanity_id] INT IDENTITY PRIMARY KEY CLUSTERED, + [database_id] SMALLINT NOT NULL , + [object_id] INT NOT NULL , + [index_id] INT NOT NULL , + [index_type] TINYINT NOT NULL, + [database_name] NVARCHAR(128) NOT NULL , + [schema_name] NVARCHAR(128) NOT NULL , + [object_name] NVARCHAR(128) NOT NULL , + index_name NVARCHAR(128) NULL , + key_column_names NVARCHAR(MAX) NULL , + key_column_names_with_sort_order NVARCHAR(MAX) NULL , + key_column_names_with_sort_order_no_types NVARCHAR(MAX) NULL , + count_key_columns INT NULL , + include_column_names NVARCHAR(MAX) NULL , + include_column_names_no_types NVARCHAR(MAX) NULL , + count_included_columns INT NULL , + partition_key_column_name NVARCHAR(MAX) NULL, + filter_definition NVARCHAR(MAX) NOT NULL , + optimize_for_sequential_key BIT NULL, + is_indexed_view BIT NOT NULL , + is_unique BIT NOT NULL , + is_primary_key BIT NOT NULL , + is_unique_constraint BIT NOT NULL , + is_XML bit NOT NULL, + is_spatial BIT NOT NULL, + is_NC_columnstore BIT NOT NULL, + is_CX_columnstore BIT NOT NULL, + is_in_memory_oltp BIT NOT NULL , + is_disabled BIT NOT NULL , + is_hypothetical BIT NOT NULL , + is_padded BIT NOT NULL , + fill_factor SMALLINT NOT NULL , + user_seeks BIGINT NOT NULL , + user_scans BIGINT NOT NULL , + user_lookups BIGINT NOT NULL , + user_updates BIGINT NULL , + last_user_seek DATETIME NULL , + last_user_scan DATETIME NULL , + last_user_lookup DATETIME NULL , + last_user_update DATETIME NULL , + is_referenced_by_foreign_key BIT DEFAULT(0), + secret_columns NVARCHAR(MAX) NULL, + count_secret_columns INT NULL, + create_date DATETIME NOT NULL, + modify_date DATETIME NOT NULL, + filter_columns_not_in_index NVARCHAR(MAX), + [db_schema_object_name] AS [schema_name] + N'.' + [object_name] , + [db_schema_object_indexid] AS [schema_name] + N'.' + [object_name] + + CASE WHEN [index_name] IS NOT NULL THEN N'.' + index_name + ELSE N'' + END + N' (' + CAST(index_id AS NVARCHAR(20)) + N')' , + first_key_column_name AS CASE WHEN count_key_columns > 1 + THEN LEFT(key_column_names, CHARINDEX(',', key_column_names, 0) - 1) + ELSE key_column_names + END , + index_definition AS + CASE WHEN partition_key_column_name IS NOT NULL + THEN N'[PARTITIONED BY:' + partition_key_column_name + N']' + ELSE '' + END + + CASE index_id + WHEN 0 THEN N'[HEAP] ' + WHEN 1 THEN N'[CX] ' + ELSE N'' END + CASE WHEN is_indexed_view = 1 THEN N'[VIEW] ' + ELSE N'' END + CASE WHEN is_primary_key = 1 THEN N'[PK] ' + ELSE N'' END + CASE WHEN is_XML = 1 THEN N'[XML] ' + ELSE N'' END + CASE WHEN is_spatial = 1 THEN N'[SPATIAL] ' + ELSE N'' END + CASE WHEN is_NC_columnstore = 1 THEN N'[COLUMNSTORE] ' + ELSE N'' END + CASE WHEN is_in_memory_oltp = 1 THEN N'[IN-MEMORY] ' + ELSE N'' END + CASE WHEN is_disabled = 1 THEN N'[DISABLED] ' + ELSE N'' END + CASE WHEN is_hypothetical = 1 THEN N'[HYPOTHETICAL] ' + ELSE N'' END + CASE WHEN is_unique = 1 AND is_primary_key = 0 AND is_unique_constraint = 0 THEN N'[UNIQUE] ' + ELSE N'' END + CASE WHEN is_unique_constraint = 1 AND is_primary_key = 0 THEN N'[UNIQUE CONSTRAINT] ' + ELSE N'' END + CASE WHEN count_key_columns > 0 THEN + N'[' + CAST(count_key_columns AS NVARCHAR(10)) + N' KEY' + + CASE WHEN count_key_columns > 1 THEN N'S' ELSE N'' END + + N'] ' + LTRIM(key_column_names_with_sort_order) + ELSE N'' END + CASE WHEN count_included_columns > 0 THEN + N' [' + CAST(count_included_columns AS NVARCHAR(10)) + N' INCLUDE' + + + CASE WHEN count_included_columns > 1 THEN N'S' ELSE N'' END + + N'] ' + include_column_names + ELSE N'' END + CASE WHEN filter_definition <> N'' THEN N' [FILTER] ' + filter_definition + ELSE N'' END , + [total_reads] AS user_seeks + user_scans + user_lookups, + [reads_per_write] AS CAST(CASE WHEN user_updates > 0 + THEN ( user_seeks + user_scans + user_lookups ) / (1.0 * user_updates) + ELSE 0 END AS MONEY) , + [index_usage_summary] AS + CASE WHEN is_spatial = 1 THEN N'Not Tracked' + WHEN is_disabled = 1 THEN N'Disabled' + ELSE N'Reads: ' + + REPLACE(CONVERT(NVARCHAR(30),CAST((user_seeks + user_scans + user_lookups) AS MONEY), 1), N'.00', N'') + + CASE WHEN user_seeks + user_scans + user_lookups > 0 THEN + N' (' + + RTRIM( + CASE WHEN user_seeks > 0 THEN REPLACE(CONVERT(NVARCHAR(30),CAST((user_seeks) AS MONEY), 1), N'.00', N'') + N' seek ' ELSE N'' END + + CASE WHEN user_scans > 0 THEN REPLACE(CONVERT(NVARCHAR(30),CAST((user_scans) AS MONEY), 1), N'.00', N'') + N' scan ' ELSE N'' END + + CASE WHEN user_lookups > 0 THEN REPLACE(CONVERT(NVARCHAR(30),CAST((user_lookups) AS MONEY), 1), N'.00', N'') + N' lookup' ELSE N'' END + ) + + N') ' + ELSE N' ' + END + + N'Writes: ' + + REPLACE(CONVERT(NVARCHAR(30),CAST(user_updates AS MONEY), 1), N'.00', N'') + END /* First "end" is about is_spatial */, + [more_info] AS + CASE WHEN is_in_memory_oltp = 1 + THEN N'EXEC dbo.sp_BlitzInMemoryOLTP @dbName=' + QUOTENAME([database_name],N'''') + + N', @tableName=' + QUOTENAME([object_name],N'''') + N';' + ELSE N'EXEC dbo.sp_BlitzIndex @DatabaseName=' + QUOTENAME([database_name],N'''') + + N', @SchemaName=' + QUOTENAME([schema_name],N'''') + N', @TableName=' + QUOTENAME([object_name],N'''') + N';' + END + ); + RAISERROR (N'Adding UQ index on #IndexSanity (database_id, object_id, index_id)',0,1) WITH NOWAIT; + IF NOT EXISTS(SELECT 1 FROM tempdb.sys.indexes WHERE name='uq_database_id_object_id_index_id') + CREATE UNIQUE INDEX uq_database_id_object_id_index_id ON #IndexSanity (database_id, object_id, index_id); + + + CREATE TABLE #IndexPartitionSanity + ( + [index_partition_sanity_id] INT IDENTITY, + [index_sanity_id] INT NULL , + [database_id] INT NOT NULL , + [object_id] INT NOT NULL , + [schema_name] NVARCHAR(128) NOT NULL, + [index_id] INT NOT NULL , + [partition_number] INT NOT NULL , + row_count BIGINT NOT NULL , + reserved_MB NUMERIC(29,2) NOT NULL , + reserved_LOB_MB NUMERIC(29,2) NOT NULL , + reserved_row_overflow_MB NUMERIC(29,2) NOT NULL , + reserved_dictionary_MB NUMERIC(29,2) NOT NULL , + leaf_insert_count BIGINT NULL , + leaf_delete_count BIGINT NULL , + leaf_update_count BIGINT NULL , + range_scan_count BIGINT NULL , + singleton_lookup_count BIGINT NULL , + forwarded_fetch_count BIGINT NULL , + lob_fetch_in_pages BIGINT NULL , + lob_fetch_in_bytes BIGINT NULL , + row_overflow_fetch_in_pages BIGINT NULL , + row_overflow_fetch_in_bytes BIGINT NULL , + row_lock_count BIGINT NULL , + row_lock_wait_count BIGINT NULL , + row_lock_wait_in_ms BIGINT NULL , + page_lock_count BIGINT NULL , + page_lock_wait_count BIGINT NULL , + page_lock_wait_in_ms BIGINT NULL , + index_lock_promotion_attempt_count BIGINT NULL , + index_lock_promotion_count BIGINT NULL, + data_compression_desc NVARCHAR(60) NULL, + page_latch_wait_count BIGINT NULL, + page_latch_wait_in_ms BIGINT NULL, + page_io_latch_wait_count BIGINT NULL, + page_io_latch_wait_in_ms BIGINT NULL, + lock_escalation_desc nvarchar(60) NULL + ); + + CREATE TABLE #IndexSanitySize + ( + [index_sanity_size_id] INT IDENTITY NOT NULL , + [index_sanity_id] INT NULL , + [database_id] INT NOT NULL, + [schema_name] NVARCHAR(128) NOT NULL, + partition_count INT NOT NULL , + total_rows BIGINT NOT NULL , + total_reserved_MB NUMERIC(29,2) NOT NULL , + total_reserved_LOB_MB NUMERIC(29,2) NOT NULL , + total_reserved_row_overflow_MB NUMERIC(29,2) NOT NULL , + total_reserved_dictionary_MB NUMERIC(29,2) NOT NULL , + total_leaf_delete_count BIGINT NULL, + total_leaf_update_count BIGINT NULL, + total_range_scan_count BIGINT NULL, + total_singleton_lookup_count BIGINT NULL, + total_forwarded_fetch_count BIGINT NULL, + total_row_lock_count BIGINT NULL , + total_row_lock_wait_count BIGINT NULL , + total_row_lock_wait_in_ms BIGINT NULL , + avg_row_lock_wait_in_ms BIGINT NULL , + total_page_lock_count BIGINT NULL , + total_page_lock_wait_count BIGINT NULL , + total_page_lock_wait_in_ms BIGINT NULL , + avg_page_lock_wait_in_ms BIGINT NULL , + total_index_lock_promotion_attempt_count BIGINT NULL , + total_index_lock_promotion_count BIGINT NULL , + data_compression_desc NVARCHAR(4000) NULL, + page_latch_wait_count BIGINT NULL, + page_latch_wait_in_ms BIGINT NULL, + page_io_latch_wait_count BIGINT NULL, + page_io_latch_wait_in_ms BIGINT NULL, + lock_escalation_desc nvarchar(60) NULL, + index_size_summary AS ISNULL( + CASE WHEN partition_count > 1 + THEN N'[' + CAST(partition_count AS NVARCHAR(10)) + N' PARTITIONS] ' + ELSE N'' + END + REPLACE(CONVERT(NVARCHAR(30),CAST([total_rows] AS MONEY), 1), N'.00', N'') + N' rows; ' + + CASE WHEN total_reserved_MB > 1024 THEN + CAST(CAST(total_reserved_MB/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'GB' + ELSE + CAST(CAST(total_reserved_MB AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'MB' + END + + CASE WHEN total_reserved_LOB_MB > 1024 THEN + N'; ' + CAST(CAST(total_reserved_LOB_MB/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'GB ' + CASE WHEN total_reserved_dictionary_MB = 0 THEN N'LOB' ELSE N'Columnstore' END + WHEN total_reserved_LOB_MB > 0 THEN + N'; ' + CAST(CAST(total_reserved_LOB_MB AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'MB ' + CASE WHEN total_reserved_dictionary_MB = 0 THEN N'LOB' ELSE N'Columnstore' END + ELSE '' + END + + CASE WHEN total_reserved_row_overflow_MB > 1024 THEN + N'; ' + CAST(CAST(total_reserved_row_overflow_MB/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'GB Row Overflow' + WHEN total_reserved_row_overflow_MB > 0 THEN + N'; ' + CAST(CAST(total_reserved_row_overflow_MB AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'MB Row Overflow' + ELSE '' + END + + CASE WHEN total_reserved_dictionary_MB > 1024 THEN + N'; ' + CAST(CAST(total_reserved_dictionary_MB/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'GB Dictionaries' + WHEN total_reserved_dictionary_MB > 0 THEN + N'; ' + CAST(CAST(total_reserved_dictionary_MB AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'MB Dictionaries' + ELSE '' + END , + N'Error- NULL in computed column'), + index_op_stats AS ISNULL( + ( + REPLACE(CONVERT(NVARCHAR(30),CAST(total_singleton_lookup_count AS MONEY), 1),N'.00',N'') + N' singleton lookups; ' + + REPLACE(CONVERT(NVARCHAR(30),CAST(total_range_scan_count AS MONEY), 1),N'.00',N'') + N' scans/seeks; ' + + REPLACE(CONVERT(NVARCHAR(30),CAST(total_leaf_delete_count AS MONEY), 1),N'.00',N'') + N' deletes; ' + + REPLACE(CONVERT(NVARCHAR(30),CAST(total_leaf_update_count AS MONEY), 1),N'.00',N'') + N' updates; ' + + CASE WHEN ISNULL(total_forwarded_fetch_count,0) >0 THEN + REPLACE(CONVERT(NVARCHAR(30),CAST(total_forwarded_fetch_count AS MONEY), 1),N'.00',N'') + N' forward records fetched; ' + ELSE N'' END + + /* rows will only be in this dmv when data is in memory for the table */ + ), N'Table metadata not in memory'), + index_lock_wait_summary AS ISNULL( + CASE WHEN total_row_lock_wait_count = 0 AND total_page_lock_wait_count = 0 AND + total_index_lock_promotion_attempt_count = 0 THEN N'0 lock waits; ' + + CASE WHEN lock_escalation_desc = N'DISABLE' THEN N'Lock escalation DISABLE.' + ELSE N'' + END + ELSE + CASE WHEN total_row_lock_wait_count > 0 THEN + N'Row lock waits: ' + REPLACE(CONVERT(NVARCHAR(30),CAST(total_row_lock_wait_count AS MONEY), 1), N'.00', N'') + + N'; total duration: ' + + CASE WHEN total_row_lock_wait_in_ms >= 60000 THEN /*More than 1 min*/ + REPLACE(CONVERT(NVARCHAR(30),CAST((total_row_lock_wait_in_ms/60000) AS MONEY), 1), N'.00', N'') + N' minutes; ' + ELSE + REPLACE(CONVERT(NVARCHAR(30),CAST(ISNULL(total_row_lock_wait_in_ms/1000,0) AS MONEY), 1), N'.00', N'') + N' seconds; ' + END + + N'avg duration: ' + + CASE WHEN avg_row_lock_wait_in_ms >= 60000 THEN /*More than 1 min*/ + REPLACE(CONVERT(NVARCHAR(30),CAST((avg_row_lock_wait_in_ms/60000) AS MONEY), 1), N'.00', N'') + N' minutes; ' + ELSE + REPLACE(CONVERT(NVARCHAR(30),CAST(ISNULL(avg_row_lock_wait_in_ms/1000,0) AS MONEY), 1), N'.00', N'') + N' seconds; ' + END + ELSE N'' + END + + CASE WHEN total_page_lock_wait_count > 0 THEN + N'Page lock waits: ' + REPLACE(CONVERT(NVARCHAR(30),CAST(total_page_lock_wait_count AS MONEY), 1), N'.00', N'') + + N'; total duration: ' + + CASE WHEN total_page_lock_wait_in_ms >= 60000 THEN /*More than 1 min*/ + REPLACE(CONVERT(NVARCHAR(30),CAST((total_page_lock_wait_in_ms/60000) AS MONEY), 1), N'.00', N'') + N' minutes; ' + ELSE + REPLACE(CONVERT(NVARCHAR(30),CAST(ISNULL(total_page_lock_wait_in_ms/1000,0) AS MONEY), 1), N'.00', N'') + N' seconds; ' + END + + N'avg duration: ' + + CASE WHEN avg_page_lock_wait_in_ms >= 60000 THEN /*More than 1 min*/ + REPLACE(CONVERT(NVARCHAR(30),CAST((avg_page_lock_wait_in_ms/60000) AS MONEY), 1), N'.00', N'') + N' minutes; ' + ELSE + REPLACE(CONVERT(NVARCHAR(30),CAST(ISNULL(avg_page_lock_wait_in_ms/1000,0) AS MONEY), 1), N'.00', N'') + N' seconds; ' + END + ELSE N'' + END + + CASE WHEN total_index_lock_promotion_attempt_count > 0 THEN + N'Lock escalation attempts: ' + REPLACE(CONVERT(NVARCHAR(30),CAST(total_index_lock_promotion_attempt_count AS MONEY), 1), N'.00', N'') + + N'; Actual Escalations: ' + REPLACE(CONVERT(NVARCHAR(30),CAST(ISNULL(total_index_lock_promotion_count,0) AS MONEY), 1), N'.00', N'') +N'; ' + ELSE N'' + END + + CASE WHEN lock_escalation_desc = N'DISABLE' THEN + N'Lock escalation is disabled.' + ELSE N'' + END + END + ,'Error- NULL in computed column') + ); + + CREATE TABLE #IndexColumns + ( + [database_id] INT NOT NULL, + [schema_name] NVARCHAR(128), + [object_id] INT NOT NULL , + [index_id] INT NOT NULL , + [key_ordinal] INT NULL , + is_included_column BIT NULL , + is_descending_key BIT NULL , + [partition_ordinal] INT NULL , + column_name NVARCHAR(256) NOT NULL , + system_type_name NVARCHAR(256) NOT NULL, + max_length SMALLINT NOT NULL, + [precision] TINYINT NOT NULL, + [scale] TINYINT NOT NULL, + collation_name NVARCHAR(256) NULL, + is_nullable BIT NULL, + is_identity BIT NULL, + is_computed BIT NULL, + is_replicated BIT NULL, + is_sparse BIT NULL, + is_filestream BIT NULL, + seed_value DECIMAL(38,0) NULL, + increment_value DECIMAL(38,0) NULL , + last_value DECIMAL(38,0) NULL, + is_not_for_replication BIT NULL + ); + CREATE CLUSTERED INDEX CLIX_database_id_object_id_index_id ON #IndexColumns + (database_id, object_id, index_id); + + CREATE TABLE #MissingIndexes + ([database_id] INT NOT NULL, + [object_id] INT NOT NULL, + [database_name] NVARCHAR(128) NOT NULL , + [schema_name] NVARCHAR(128) NOT NULL , + [table_name] NVARCHAR(128), + [statement] NVARCHAR(512) NOT NULL, + magic_benefit_number AS (( user_seeks + user_scans ) * avg_total_user_cost * avg_user_impact), + avg_total_user_cost NUMERIC(29,4) NOT NULL, + avg_user_impact NUMERIC(29,1) NOT NULL, + user_seeks BIGINT NOT NULL, + user_scans BIGINT NOT NULL, + unique_compiles BIGINT NULL, + equality_columns NVARCHAR(MAX), + equality_columns_with_data_type NVARCHAR(MAX), + inequality_columns NVARCHAR(MAX), + inequality_columns_with_data_type NVARCHAR(MAX), + included_columns NVARCHAR(MAX), + included_columns_with_data_type NVARCHAR(MAX), + is_low BIT, + [index_estimated_impact] AS + REPLACE(CONVERT(NVARCHAR(256),CAST(CAST( + (user_seeks + user_scans) + AS BIGINT) AS MONEY), 1), '.00', '') + N' use' + + CASE WHEN (user_seeks + user_scans) > 1 THEN N's' ELSE N'' END + +N'; Impact: ' + CAST(avg_user_impact AS NVARCHAR(30)) + + N'%; Avg query cost: ' + + CAST(avg_total_user_cost AS NVARCHAR(30)), + [missing_index_details] AS + CASE WHEN COALESCE(equality_columns_with_data_type,equality_columns) IS NOT NULL + THEN N'EQUALITY: ' + COALESCE(CAST(equality_columns_with_data_type AS NVARCHAR(MAX)), CAST(equality_columns AS NVARCHAR(MAX))) + N' ' + ELSE N'' END + + + CASE WHEN COALESCE(inequality_columns_with_data_type,inequality_columns) IS NOT NULL + THEN N'INEQUALITY: ' + COALESCE(CAST(inequality_columns_with_data_type AS NVARCHAR(MAX)), CAST(inequality_columns AS NVARCHAR(MAX))) + N' ' + ELSE N'' END + + + CASE WHEN COALESCE(included_columns_with_data_type,included_columns) IS NOT NULL + THEN N'INCLUDE: ' + COALESCE(CAST(included_columns_with_data_type AS NVARCHAR(MAX)), CAST(included_columns AS NVARCHAR(MAX))) + N' ' + ELSE N'' END, + [create_tsql] AS N'CREATE INDEX [' + + LEFT(REPLACE(REPLACE(REPLACE(REPLACE( + ISNULL(equality_columns,N'')+ + CASE WHEN equality_columns IS NOT NULL AND inequality_columns IS NOT NULL THEN N'_' ELSE N'' END + + ISNULL(inequality_columns,''),',','') + ,'[',''),']',''),' ','_') + + CASE WHEN included_columns IS NOT NULL THEN N'_Includes' ELSE N'' END, 128) + N'] ON ' + + [statement] + N' (' + ISNULL(equality_columns,N'') + + CASE WHEN equality_columns IS NOT NULL AND inequality_columns IS NOT NULL THEN N', ' ELSE N'' END + + CASE WHEN inequality_columns IS NOT NULL THEN inequality_columns ELSE N'' END + + ') ' + CASE WHEN included_columns IS NOT NULL THEN N' INCLUDE (' + included_columns + N')' ELSE N'' END + + N' WITH (' + + N'FILLFACTOR=100, ONLINE=?, SORT_IN_TEMPDB=?, DATA_COMPRESSION=?' + + N')' + + N';' + , + [more_info] AS N'EXEC dbo.sp_BlitzIndex @DatabaseName=' + QUOTENAME([database_name],'''') + + N', @SchemaName=' + QUOTENAME([schema_name],'''') + N', @TableName=' + QUOTENAME([table_name],'''') + N';', + [sample_query_plan] XML NULL + ); + + CREATE TABLE #ForeignKeys ( + [database_id] INT NOT NULL, + [database_name] NVARCHAR(128) NOT NULL , + [schema_name] NVARCHAR(128) NOT NULL , + foreign_key_name NVARCHAR(256), + parent_object_id INT, + parent_object_name NVARCHAR(256), + referenced_object_id INT, + referenced_object_name NVARCHAR(256), + is_disabled BIT, + is_not_trusted BIT, + is_not_for_replication BIT, + parent_fk_columns NVARCHAR(MAX), + referenced_fk_columns NVARCHAR(MAX), + update_referential_action_desc NVARCHAR(16), + delete_referential_action_desc NVARCHAR(60) + ); + + CREATE TABLE #UnindexedForeignKeys + ( + [database_id] INT NOT NULL, + [database_name] NVARCHAR(128) NOT NULL , + [schema_name] NVARCHAR(128) NOT NULL , + foreign_key_name NVARCHAR(256), + parent_object_name NVARCHAR(256), + parent_object_id INT, + referenced_object_name NVARCHAR(256), + referenced_object_id INT + ); + + CREATE TABLE #IndexCreateTsql ( + index_sanity_id INT NOT NULL, + create_tsql NVARCHAR(MAX) NOT NULL + ); + + CREATE TABLE #DatabaseList ( + DatabaseName NVARCHAR(256), + secondary_role_allow_connections_desc NVARCHAR(50) + + ); + + CREATE TABLE #PartitionCompressionInfo ( + [index_sanity_id] INT NULL, + [partition_compression_detail] NVARCHAR(4000) NULL + ); + + CREATE TABLE #Statistics ( + database_id INT NOT NULL, + database_name NVARCHAR(256) NOT NULL, + table_name NVARCHAR(128) NULL, + schema_name NVARCHAR(128) NULL, + index_name NVARCHAR(128) NULL, + column_names NVARCHAR(MAX) NULL, + statistics_name NVARCHAR(128) NULL, + last_statistics_update DATETIME NULL, + days_since_last_stats_update INT NULL, + rows BIGINT NULL, + rows_sampled BIGINT NULL, + percent_sampled DECIMAL(18, 1) NULL, + histogram_steps INT NULL, + modification_counter BIGINT NULL, + percent_modifications DECIMAL(18, 1) NULL, + modifications_before_auto_update INT NULL, + index_type_desc NVARCHAR(128) NULL, + table_create_date DATETIME NULL, + table_modify_date DATETIME NULL, + no_recompute BIT NULL, + has_filter BIT NULL, + filter_definition NVARCHAR(MAX) NULL + ); + + CREATE TABLE #ComputedColumns + ( + index_sanity_id INT IDENTITY(1, 1) NOT NULL, + database_name NVARCHAR(128) NULL, + database_id INT NOT NULL, + table_name NVARCHAR(128) NOT NULL, + schema_name NVARCHAR(128) NOT NULL, + column_name NVARCHAR(128) NULL, + is_nullable BIT NULL, + definition NVARCHAR(MAX) NULL, + uses_database_collation BIT NOT NULL, + is_persisted BIT NOT NULL, + is_computed BIT NOT NULL, + is_function INT NOT NULL, + column_definition NVARCHAR(MAX) NULL + ); + + CREATE TABLE #TraceStatus + ( + TraceFlag NVARCHAR(10) , + status BIT , + Global BIT , + Session BIT + ); + + CREATE TABLE #TemporalTables + ( + index_sanity_id INT IDENTITY(1, 1) NOT NULL, + database_name NVARCHAR(128) NOT NULL, + database_id INT NOT NULL, + schema_name NVARCHAR(128) NOT NULL, + table_name NVARCHAR(128) NOT NULL, + history_table_name NVARCHAR(128) NOT NULL, + history_schema_name NVARCHAR(128) NOT NULL, + start_column_name NVARCHAR(128) NOT NULL, + end_column_name NVARCHAR(128) NOT NULL, + period_name NVARCHAR(128) NOT NULL + ); + + CREATE TABLE #CheckConstraints + ( + index_sanity_id INT IDENTITY(1, 1) NOT NULL, + database_name NVARCHAR(128) NULL, + database_id INT NOT NULL, + table_name NVARCHAR(128) NOT NULL, + schema_name NVARCHAR(128) NOT NULL, + constraint_name NVARCHAR(128) NULL, + is_disabled BIT NULL, + definition NVARCHAR(MAX) NULL, + uses_database_collation BIT NOT NULL, + is_not_trusted BIT NOT NULL, + is_function INT NOT NULL, + column_definition NVARCHAR(MAX) NULL + ); + + CREATE TABLE #FilteredIndexes + ( + index_sanity_id INT IDENTITY(1, 1) NOT NULL, + database_name NVARCHAR(128) NULL, + database_id INT NOT NULL, + schema_name NVARCHAR(128) NOT NULL, + table_name NVARCHAR(128) NOT NULL, + index_name NVARCHAR(128) NULL, + column_name NVARCHAR(128) NULL + ); + + CREATE TABLE #Ignore_Databases + ( + DatabaseName NVARCHAR(128), + Reason NVARCHAR(100) + ); + +/* Sanitize our inputs */ +SELECT + @OutputServerName = QUOTENAME(@OutputServerName), + @OutputDatabaseName = QUOTENAME(@OutputDatabaseName), + @OutputSchemaName = QUOTENAME(@OutputSchemaName), + @OutputTableName = QUOTENAME(@OutputTableName); + + +IF @GetAllDatabases = 1 + BEGIN + INSERT INTO #DatabaseList (DatabaseName) + SELECT DB_NAME(database_id) + FROM sys.databases + WHERE user_access_desc = 'MULTI_USER' + AND state_desc = 'ONLINE' + AND database_id > 4 + AND DB_NAME(database_id) NOT LIKE 'ReportServer%' + AND DB_NAME(database_id) NOT LIKE 'rdsadmin%' + AND LOWER(name) NOT IN('dbatools', 'dbadmin', 'dbmaintenance') + AND is_distributor = 0 + OPTION ( RECOMPILE ); + + /* Skip non-readable databases in an AG - see Github issue #1160 */ + IF EXISTS (SELECT * FROM sys.all_objects o INNER JOIN sys.all_columns c ON o.object_id = c.object_id AND o.name = 'dm_hadr_availability_replica_states' AND c.name = 'role_desc') + BEGIN + SET @dsql = N'UPDATE #DatabaseList SET secondary_role_allow_connections_desc = ''NO'' WHERE DatabaseName IN ( + SELECT d.name + FROM sys.dm_hadr_availability_replica_states rs + INNER JOIN sys.databases d ON rs.replica_id = d.replica_id + INNER JOIN sys.availability_replicas r ON rs.replica_id = r.replica_id + WHERE rs.role_desc = ''SECONDARY'' + AND r.secondary_role_allow_connections_desc = ''NO'') + OPTION ( RECOMPILE );'; + EXEC sp_executesql @dsql; + + IF EXISTS (SELECT * FROM #DatabaseList WHERE secondary_role_allow_connections_desc = 'NO') + BEGIN + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, database_name, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( 1, + 0, + N'Skipped non-readable AG secondary databases.', + N'You are running this on an AG secondary, and some of your databases are configured as non-readable when this is a secondary node.', + N'To analyze those databases, run sp_BlitzIndex on the primary, or on a readable secondary.', + 'http://FirstResponderKit.org', '', '', '', '' + ); + END; + END; + + IF @IgnoreDatabases IS NOT NULL + AND LEN(@IgnoreDatabases) > 0 + BEGIN + RAISERROR(N'Setting up filter to ignore databases', 0, 1) WITH NOWAIT; + SET @DatabaseToIgnore = ''; + + WHILE LEN(@IgnoreDatabases) > 0 + BEGIN + IF PATINDEX('%,%', @IgnoreDatabases) > 0 + BEGIN + SET @DatabaseToIgnore = SUBSTRING(@IgnoreDatabases, 0, PATINDEX('%,%',@IgnoreDatabases)) ; + + INSERT INTO #Ignore_Databases (DatabaseName, Reason) + SELECT LTRIM(RTRIM(@DatabaseToIgnore)), 'Specified in the @IgnoreDatabases parameter' + OPTION (RECOMPILE) ; + + SET @IgnoreDatabases = SUBSTRING(@IgnoreDatabases, LEN(@DatabaseToIgnore + ',') + 1, LEN(@IgnoreDatabases)) ; + END; + ELSE + BEGIN + SET @DatabaseToIgnore = @IgnoreDatabases ; + SET @IgnoreDatabases = NULL ; + + INSERT INTO #Ignore_Databases (DatabaseName, Reason) + SELECT LTRIM(RTRIM(@DatabaseToIgnore)), 'Specified in the @IgnoreDatabases parameter' + OPTION (RECOMPILE) ; + END; + END; + + END + + END; +ELSE + BEGIN + INSERT INTO #DatabaseList + ( DatabaseName ) + SELECT CASE + WHEN @DatabaseName IS NULL OR @DatabaseName = N'' + THEN DB_NAME() + ELSE @DatabaseName END; + END; + +SET @NumDatabases = (SELECT COUNT(*) FROM #DatabaseList AS D LEFT OUTER JOIN #Ignore_Databases AS I ON D.DatabaseName = I.DatabaseName WHERE I.DatabaseName IS NULL); +SET @msg = N'Number of databases to examine: ' + CAST(@NumDatabases AS NVARCHAR(50)); +RAISERROR (@msg,0,1) WITH NOWAIT; + + + +/* Running on 50+ databases can take a reaaallly long time, so we want explicit permission to do so (and only after warning about it) */ + + +BEGIN TRY + IF @NumDatabases >= 50 AND @BringThePain != 1 AND @TableName IS NULL + BEGIN + + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( -1, + 0 , + @ScriptVersionName, + CASE WHEN @GetAllDatabases = 1 THEN N'All Databases' ELSE N'Database ' + QUOTENAME(@DatabaseName) + N' as of ' + CONVERT(NVARCHAR(16), GETDATE(), 121) END, + N'From Your Community Volunteers', + N'http://FirstResponderKit.org', + N'', + N'', + N'' + ); + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, database_name, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( 1, + 0, + N'You''re trying to run sp_BlitzIndex on a server with ' + CAST(@NumDatabases AS NVARCHAR(8)) + N' databases. ', + N'Running sp_BlitzIndex on a server with 50+ databases may cause temporary insanity for the server and/or user.', + N'If you''re sure you want to do this, run again with the parameter @BringThePain = 1.', + 'http://FirstResponderKit.org', + '', + '', + '', + '' + ); + + if(@OutputType <> 'NONE') + BEGIN + SELECT bir.blitz_result_id, + bir.check_id, + bir.index_sanity_id, + bir.Priority, + bir.findings_group, + bir.finding, + bir.database_name, + bir.URL, + bir.details, + bir.index_definition, + bir.secret_columns, + bir.index_usage_summary, + bir.index_size_summary, + bir.create_tsql, + bir.more_info + FROM #BlitzIndexResults AS bir; + RAISERROR('Running sp_BlitzIndex on a server with 50+ databases may cause temporary insanity for the server', 12, 1); + END; + + RETURN; + + END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure to execute due to number of databases.', 0,1) WITH NOWAIT; + + SELECT @msg = ERROR_MESSAGE(), + @ErrorSeverity = ERROR_SEVERITY(), + @ErrorState = ERROR_STATE(); + + RAISERROR (@msg, @ErrorSeverity, @ErrorState); + + WHILE @@trancount > 0 + ROLLBACK; + + RETURN; + END CATCH; + + +RAISERROR (N'Checking partition counts to exclude databases with over 100 partitions',0,1) WITH NOWAIT; +IF @BringThePain = 0 AND @SkipPartitions = 0 AND @TableName IS NULL + BEGIN + DECLARE partition_cursor CURSOR FOR + SELECT dl.DatabaseName + FROM #DatabaseList dl + LEFT OUTER JOIN #Ignore_Databases i ON dl.DatabaseName = i.DatabaseName + WHERE COALESCE(dl.secondary_role_allow_connections_desc, 'OK') <> 'NO' + AND i.DatabaseName IS NULL + + OPEN partition_cursor + FETCH NEXT FROM partition_cursor INTO @DatabaseName + + WHILE @@FETCH_STATUS = 0 + BEGIN + /* Count the total number of partitions */ + SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT @RowcountOUT = SUM(1) FROM ' + QUOTENAME(@DatabaseName) + '.sys.partitions WHERE partition_number > 1 OPTION ( RECOMPILE );'; + EXEC sp_executesql @dsql, N'@RowcountOUT BIGINT OUTPUT', @RowcountOUT = @Rowcount OUTPUT; + IF @Rowcount > 100 + BEGIN + RAISERROR (N'Skipping database %s because > 100 partitions were found. To check this database, you must set @BringThePain = 1.',0,1,@DatabaseName) WITH NOWAIT; + INSERT INTO #Ignore_Databases (DatabaseName, Reason) + SELECT @DatabaseName, 'Over 100 partitions found - use @BringThePain = 1 to analyze' + END; + FETCH NEXT FROM partition_cursor INTO @DatabaseName + END; + CLOSE partition_cursor + DEALLOCATE partition_cursor + + END; + +INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) +SELECT 1, 0 , + 'Database Skipped', + i.DatabaseName, + 'http://FirstResponderKit.org', + i.Reason, '', '', '' +FROM #Ignore_Databases i; + + +/* Last startup */ +IF COLUMNPROPERTY(OBJECT_ID('sys.dm_os_sys_info'),'sqlserver_start_time','ColumnID') IS NOT NULL +BEGIN + SELECT @DaysUptime = CAST(DATEDIFF(HOUR, sqlserver_start_time, GETDATE()) / 24. AS NUMERIC (23,2)) + FROM sys.dm_os_sys_info; +END +ELSE +BEGIN + SELECT @DaysUptime = CAST(DATEDIFF(HOUR, create_date, GETDATE()) / 24. AS NUMERIC (23,2)) + FROM sys.databases + WHERE database_id = 2; +END + +IF @DaysUptime = 0 OR @DaysUptime IS NULL + SET @DaysUptime = .01; + +SELECT @DaysUptimeInsertValue = 'Server: ' + (CONVERT(VARCHAR(256), (SERVERPROPERTY('ServerName')))) + ' Days Uptime: ' + RTRIM(@DaysUptime); + + +/* Permission granted or unnecessary? Ok, let's go! */ + +RAISERROR (N'Starting loop through databases',0,1) WITH NOWAIT; +DECLARE c1 CURSOR +LOCAL FAST_FORWARD +FOR +SELECT dl.DatabaseName +FROM #DatabaseList dl +LEFT OUTER JOIN #Ignore_Databases i ON dl.DatabaseName = i.DatabaseName +WHERE COALESCE(dl.secondary_role_allow_connections_desc, 'OK') <> 'NO' + AND i.DatabaseName IS NULL +ORDER BY dl.DatabaseName; + +OPEN c1; +FETCH NEXT FROM c1 INTO @DatabaseName; + WHILE @@FETCH_STATUS = 0 + +BEGIN + + RAISERROR (@LineFeed, 0, 1) WITH NOWAIT; + RAISERROR (@LineFeed, 0, 1) WITH NOWAIT; + RAISERROR (@DatabaseName, 0, 1) WITH NOWAIT; + +SELECT @DatabaseID = [database_id] +FROM sys.databases + WHERE [name] = @DatabaseName + AND user_access_desc='MULTI_USER' + AND state_desc = 'ONLINE'; + +---------------------------------------- +--STEP 1: OBSERVE THE PATIENT +--This step puts index information into temp tables. +---------------------------------------- +BEGIN TRY + BEGIN + + --Validate SQL Server Version + + IF (SELECT LEFT(@SQLServerProductVersion, + CHARINDEX('.',@SQLServerProductVersion,0)-1 + )) <= 9 + BEGIN + SET @msg=N'sp_BlitzIndex is only supported on SQL Server 2008 and higher. The version of this instance is: ' + @SQLServerProductVersion; + RAISERROR(@msg,16,1); + END; + + --Short circuit here if database name does not exist. + IF @DatabaseName IS NULL OR @DatabaseID IS NULL + BEGIN + SET @msg='Database does not exist or is not online/multi-user: cannot proceed.'; + RAISERROR(@msg,16,1); + END; + + --Validate parameters. + IF (@Mode NOT IN (0,1,2,3,4)) + BEGIN + SET @msg=N'Invalid @Mode parameter. 0=diagnose, 1=summarize, 2=index detail, 3=missing index detail, 4=diagnose detail'; + RAISERROR(@msg,16,1); + END; + + IF (@Mode <> 0 AND @TableName IS NOT NULL) + BEGIN + SET @msg=N'Setting the @Mode doesn''t change behavior if you supply @TableName. Use default @Mode=0 to see table detail.'; + RAISERROR(@msg,16,1); + END; + + IF ((@Mode <> 0 OR @TableName IS NOT NULL) AND @Filter <> 0) + BEGIN + SET @msg=N'@Filter only applies when @Mode=0 and @TableName is not specified. Please try again.'; + RAISERROR(@msg,16,1); + END; + + IF (@SchemaName IS NOT NULL AND @TableName IS NULL) + BEGIN + SET @msg='We can''t run against a whole schema! Specify a @TableName, or leave both NULL for diagnosis.'; + RAISERROR(@msg,16,1); + END; + + + IF (@TableName IS NOT NULL AND @SchemaName IS NULL) + BEGIN + SET @SchemaName=N'dbo'; + SET @msg='@SchemaName wasn''t specified-- assuming schema=dbo.'; + RAISERROR(@msg,1,1) WITH NOWAIT; + END; + + --If a table is specified, grab the object id. + --Short circuit if it doesn't exist. + IF @TableName IS NOT NULL + BEGIN + SET @dsql = N' + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT @ObjectID= OBJECT_ID + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS sc on + so.schema_id=sc.schema_id + where so.type in (''U'', ''V'') + and so.name=' + QUOTENAME(@TableName,'''')+ N' + and sc.name=' + QUOTENAME(@SchemaName,'''')+ N' + /*Has a row in sys.indexes. This lets us get indexed views.*/ + and exists ( + SELECT si.name + FROM ' + QUOTENAME(@DatabaseName) + '.sys.indexes AS si + WHERE so.object_id=si.object_id) + OPTION (RECOMPILE);'; + + SET @params='@ObjectID INT OUTPUT'; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + EXEC sp_executesql @dsql, @params, @ObjectID=@ObjectID OUTPUT; + + IF @ObjectID IS NULL + BEGIN + SET @msg=N'Oh, this is awkward. I can''t find the table or indexed view you''re looking for in that database.' + CHAR(10) + + N'Please check your parameters.'; + RAISERROR(@msg,1,1); + RETURN; + END; + END; + + --set @collation + SELECT @collation=collation_name + FROM sys.databases + WHERE database_id=@DatabaseID; + + --insert columns for clustered indexes and heaps + --collect info on identity columns for this one + SET @dsql = N'/* sp_BlitzIndex */ + SET LOCK_TIMEOUT 1000; /* To fix locking bug in sys.identity_columns. See Github issue #2176. */ + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT ' + CAST(@DatabaseID AS NVARCHAR(16)) + ', + s.name, + si.object_id, + si.index_id, + sc.key_ordinal, + sc.is_included_column, + sc.is_descending_key, + sc.partition_ordinal, + c.name as column_name, + st.name as system_type_name, + c.max_length, + c.[precision], + c.[scale], + c.collation_name, + c.is_nullable, + c.is_identity, + c.is_computed, + c.is_replicated, + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'c.is_sparse' ELSE N'NULL as is_sparse' END + N', + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'c.is_filestream' ELSE N'NULL as is_filestream' END + N', + CAST(ic.seed_value AS DECIMAL(38,0)), + CAST(ic.increment_value AS DECIMAL(38,0)), + CAST(ic.last_value AS DECIMAL(38,0)), + ic.is_not_for_replication + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.indexes si + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns c ON + si.object_id=c.object_id + LEFT JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns sc ON + sc.object_id = si.object_id + and sc.index_id=si.index_id + AND sc.column_id=c.column_id + LEFT JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.identity_columns ic ON + c.object_id=ic.object_id and + c.column_id=ic.column_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.types st ON + c.system_type_id=st.system_type_id + AND c.user_type_id=st.user_type_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so ON si.object_id = so.object_id + AND so.is_ms_shipped = 0 + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s ON s.schema_id = so.schema_id + WHERE si.index_id in (0,1) ' + + CASE WHEN @ObjectID IS NOT NULL + THEN N' AND si.object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + ELSE N'' END + + N'OPTION (RECOMPILE);'; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #IndexColumns for clustered indexes and heaps',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + BEGIN TRY + INSERT #IndexColumns ( database_id, [schema_name], [object_id], index_id, key_ordinal, is_included_column, is_descending_key, partition_ordinal, + column_name, system_type_name, max_length, precision, scale, collation_name, is_nullable, is_identity, is_computed, + is_replicated, is_sparse, is_filestream, seed_value, increment_value, last_value, is_not_for_replication ) + EXEC sp_executesql @dsql; + END TRY + BEGIN CATCH + RAISERROR (N'Failure inserting data into #IndexColumns for clustered indexes and heaps.', 0,1) WITH NOWAIT; + + IF @dsql IS NOT NULL + BEGIN + SET @msg= 'Last @dsql: ' + @dsql; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), + @ErrorSeverity = 0, @ErrorState = ERROR_STATE(); + RAISERROR (@msg,@ErrorSeverity, @ErrorState )WITH NOWAIT; + + WHILE @@trancount > 0 + ROLLBACK; + + RETURN; + END CATCH; + + + --insert columns for nonclustered indexes + --this uses a full join to sys.index_columns + --We don't collect info on identity columns here. They may be in NC indexes, but we just analyze identities in the base table. + SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT ' + CAST(@DatabaseID AS NVARCHAR(16)) + ', + s.name, + si.object_id, + si.index_id, + sc.key_ordinal, + sc.is_included_column, + sc.is_descending_key, + sc.partition_ordinal, + c.name as column_name, + st.name as system_type_name, + c.max_length, + c.[precision], + c.[scale], + c.collation_name, + c.is_nullable, + c.is_identity, + c.is_computed, + c.is_replicated, + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'c.is_sparse' ELSE N'NULL AS is_sparse' END + N', + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'c.is_filestream' ELSE N'NULL AS is_filestream' END + N' + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.indexes AS si + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c ON + si.object_id=c.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns AS sc ON + sc.object_id = si.object_id + and sc.index_id=si.index_id + AND sc.column_id=c.column_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.types AS st ON + c.system_type_id=st.system_type_id + AND c.user_type_id=st.user_type_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so ON si.object_id = so.object_id + AND so.is_ms_shipped = 0 + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s ON s.schema_id = so.schema_id + WHERE si.index_id not in (0,1) ' + + CASE WHEN @ObjectID IS NOT NULL + THEN N' AND si.object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + ELSE N'' END + + N'OPTION (RECOMPILE);'; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #IndexColumns for nonclustered indexes',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + INSERT #IndexColumns ( database_id, [schema_name], [object_id], index_id, key_ordinal, is_included_column, is_descending_key, partition_ordinal, + column_name, system_type_name, max_length, precision, scale, collation_name, is_nullable, is_identity, is_computed, + is_replicated, is_sparse, is_filestream ) + EXEC sp_executesql @dsql; + + SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT ' + CAST(@DatabaseID AS NVARCHAR(10)) + N' AS database_id, + so.object_id, + si.index_id, + si.type, + @i_DatabaseName AS database_name, + COALESCE(sc.NAME, ''Unknown'') AS [schema_name], + COALESCE(so.name, ''Unknown'') AS [object_name], + COALESCE(si.name, ''Unknown'') AS [index_name], + CASE WHEN so.[type] = CAST(''V'' AS CHAR(2)) THEN 1 ELSE 0 END, + si.is_unique, + si.is_primary_key, + si.is_unique_constraint, + CASE when si.type = 3 THEN 1 ELSE 0 END AS is_XML, + CASE when si.type = 4 THEN 1 ELSE 0 END AS is_spatial, + CASE when si.type = 6 THEN 1 ELSE 0 END AS is_NC_columnstore, + CASE when si.type = 5 then 1 else 0 end as is_CX_columnstore, + CASE when si.data_space_id = 0 then 1 else 0 end as is_in_memory_oltp, + si.is_disabled, + si.is_hypothetical, + si.is_padded, + si.fill_factor,' + + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N' + CASE WHEN si.filter_definition IS NOT NULL THEN si.filter_definition + ELSE N'''' + END AS filter_definition' ELSE N''''' AS filter_definition' END + + CASE + WHEN @OptimizeForSequentialKey = 1 + THEN N', si.optimize_for_sequential_key' + ELSE N', CONVERT(BIT, NULL) AS optimize_for_sequential_key' + END + + N', + ISNULL(us.user_seeks, 0), + ISNULL(us.user_scans, 0), + ISNULL(us.user_lookups, 0), + ISNULL(us.user_updates, 0), + us.last_user_seek, + us.last_user_scan, + us.last_user_lookup, + us.last_user_update, + so.create_date, + so.modify_date + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.indexes AS si WITH (NOLOCK) + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so WITH (NOLOCK) ON si.object_id = so.object_id + AND so.is_ms_shipped = 0 /*Exclude objects shipped by Microsoft*/ + AND so.type <> ''TF'' /*Exclude table valued functions*/ + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas sc ON so.schema_id = sc.schema_id + LEFT JOIN sys.dm_db_index_usage_stats AS us WITH (NOLOCK) ON si.[object_id] = us.[object_id] + AND si.index_id = us.index_id + AND us.database_id = ' + CAST(@DatabaseID AS NVARCHAR(10)) + N' + WHERE si.[type] IN ( 0, 1, 2, 3, 4, 5, 6 ) + /* Heaps, clustered, nonclustered, XML, spatial, Cluster Columnstore, NC Columnstore */ ' + + CASE WHEN @TableName IS NOT NULL THEN N' and so.name=' + QUOTENAME(@TableName,N'''') + N' ' ELSE N'' END + + CASE WHEN ( @IncludeInactiveIndexes = 0 + AND @Mode IN (0, 4) + AND @TableName IS NULL ) + THEN N'AND ( us.user_seeks + us.user_scans + us.user_lookups + us.user_updates ) > 0' + ELSE N'' + END + + N'OPTION ( RECOMPILE ); + '; + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #IndexSanity',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + INSERT #IndexSanity ( [database_id], [object_id], [index_id], [index_type], [database_name], [schema_name], [object_name], + index_name, is_indexed_view, is_unique, is_primary_key, is_unique_constraint, is_XML, is_spatial, is_NC_columnstore, is_CX_columnstore, is_in_memory_oltp, + is_disabled, is_hypothetical, is_padded, fill_factor, filter_definition, [optimize_for_sequential_key], user_seeks, user_scans, + user_lookups, user_updates, last_user_seek, last_user_scan, last_user_lookup, last_user_update, + create_date, modify_date ) + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + + + RAISERROR (N'Checking partition count',0,1) WITH NOWAIT; + IF @BringThePain = 0 AND @SkipPartitions = 0 AND @TableName IS NULL + BEGIN + /* Count the total number of partitions */ + SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT @RowcountOUT = SUM(1) FROM ' + QUOTENAME(@DatabaseName) + '.sys.partitions WHERE partition_number > 1 OPTION ( RECOMPILE );'; + EXEC sp_executesql @dsql, N'@RowcountOUT BIGINT OUTPUT', @RowcountOUT = @Rowcount OUTPUT; + IF @Rowcount > 100 + BEGIN + RAISERROR (N'Setting @SkipPartitions = 1 because > 100 partitions were found. To check them, you must set @BringThePain = 1.',0,1) WITH NOWAIT; + SET @SkipPartitions = 1; + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( 1, 0 , + 'Some Checks Were Skipped', + '@SkipPartitions Forced to 1', + 'http://FirstResponderKit.org', CAST(@Rowcount AS NVARCHAR(50)) + ' partitions found. To analyze them, use @BringThePain = 1.', 'We try to keep things quick - and warning, running @BringThePain = 1 can take tens of minutes.', '', '' + ); + END; + END; + + + + IF (@SkipPartitions = 0) + BEGIN + IF (SELECT LEFT(@SQLServerProductVersion, + CHARINDEX('.',@SQLServerProductVersion,0)-1 )) <= 2147483647 --Make change here + BEGIN + + RAISERROR (N'Preferring non-2012 syntax with LEFT JOIN to sys.dm_db_index_operational_stats',0,1) WITH NOWAIT; + + --NOTE: If you want to use the newer syntax for 2012+, you'll have to change 2147483647 to 11 on line ~819 + --This change was made because on a table with lots of paritions, the OUTER APPLY was crazy slow. + SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT ' + CAST(@DatabaseID AS NVARCHAR(10)) + N' AS database_id, + ps.object_id, + s.name, + ps.index_id, + ps.partition_number, + ps.row_count, + ps.reserved_page_count * 8. / 1024. AS reserved_MB, + ps.lob_reserved_page_count * 8. / 1024. AS reserved_LOB_MB, + ps.row_overflow_reserved_page_count * 8. / 1024. AS reserved_row_overflow_MB, + le.lock_escalation_desc, + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N', + SUM(os.leaf_insert_count), + SUM(os.leaf_delete_count), + SUM(os.leaf_update_count), + SUM(os.range_scan_count), + SUM(os.singleton_lookup_count), + SUM(os.forwarded_fetch_count), + SUM(os.lob_fetch_in_pages), + SUM(os.lob_fetch_in_bytes), + SUM(os.row_overflow_fetch_in_pages), + SUM(os.row_overflow_fetch_in_bytes), + SUM(os.row_lock_count), + SUM(os.row_lock_wait_count), + SUM(os.row_lock_wait_in_ms), + SUM(os.page_lock_count), + SUM(os.page_lock_wait_count), + SUM(os.page_lock_wait_in_ms), + SUM(os.index_lock_promotion_attempt_count), + SUM(os.index_lock_promotion_count), + SUM(os.page_latch_wait_count), + SUM(os.page_latch_wait_in_ms), + SUM(os.page_io_latch_wait_count), + SUM(os.page_io_latch_wait_in_ms), '; + + /* Get columnstore dictionary size - more info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2585 */ + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'column_store_dictionaries') + SET @dsql = @dsql + N' COALESCE((SELECT SUM (on_disk_size / 1024.0 / 1024) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_dictionaries dict WHERE dict.partition_id = ps.partition_id),0) AS reserved_dictionary_MB '; + ELSE + SET @dsql = @dsql + N' 0 AS reserved_dictionary_MB '; + + + SET @dsql = @dsql + N' + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_partition_stats AS ps + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partitions AS par on ps.partition_id=par.partition_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so ON ps.object_id = so.object_id + AND so.is_ms_shipped = 0 /*Exclude objects shipped by Microsoft*/ + AND so.type <> ''TF'' /*Exclude table valued functions*/ + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s ON s.schema_id = so.schema_id + LEFT JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_index_operational_stats(' + + CAST(@DatabaseID AS NVARCHAR(10)) + N', NULL, NULL,NULL) AS os ON + ps.object_id=os.object_id and ps.index_id=os.index_id and ps.partition_number=os.partition_number + OUTER APPLY (SELECT st.lock_escalation_desc + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.tables st + WHERE st.object_id = ps.object_id + AND ps.index_id < 2 ) le + WHERE 1=1 + ' + CASE WHEN @ObjectID IS NOT NULL THEN N'AND so.object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + N' ' ELSE N' ' END + N' + ' + CASE WHEN @Filter = 2 THEN N'AND ps.reserved_page_count * 8./1024. > ' + CAST(@FilterMB AS NVARCHAR(5)) + N' ' ELSE N' ' END + N' + GROUP BY ps.object_id, + s.name, + ps.index_id, + ps.partition_number, + ps.partition_id, + ps.row_count, + ps.reserved_page_count, + ps.lob_reserved_page_count, + ps.row_overflow_reserved_page_count, + le.lock_escalation_desc, + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N' + ORDER BY ps.object_id, ps.index_id, ps.partition_number + OPTION ( RECOMPILE ); + '; + END; + ELSE + BEGIN + RAISERROR (N'Using 2012 syntax to query sys.dm_db_index_operational_stats',0,1) WITH NOWAIT; + --This is the syntax that will be used if you change 2147483647 to 11 on line ~819. + --If you have a lot of paritions and this suddenly starts running for a long time, change it back. + SET @dsql = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT ' + CAST(@DatabaseID AS NVARCHAR(10)) + N' AS database_id, + ps.object_id, + s.name, + ps.index_id, + ps.partition_number, + ps.row_count, + ps.reserved_page_count * 8. / 1024. AS reserved_MB, + ps.lob_reserved_page_count * 8. / 1024. AS reserved_LOB_MB, + ps.row_overflow_reserved_page_count * 8. / 1024. AS reserved_row_overflow_MB, + le.lock_escalation_desc, + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc' END + N', + SUM(os.leaf_insert_count), + SUM(os.leaf_delete_count), + SUM(os.leaf_update_count), + SUM(os.range_scan_count), + SUM(os.singleton_lookup_count), + SUM(os.forwarded_fetch_count), + SUM(os.lob_fetch_in_pages), + SUM(os.lob_fetch_in_bytes), + SUM(os.row_overflow_fetch_in_pages), + SUM(os.row_overflow_fetch_in_bytes), + SUM(os.row_lock_count), + SUM(os.row_lock_wait_count), + SUM(os.row_lock_wait_in_ms), + SUM(os.page_lock_count), + SUM(os.page_lock_wait_count), + SUM(os.page_lock_wait_in_ms), + SUM(os.index_lock_promotion_attempt_count), + SUM(os.index_lock_promotion_count), + SUM(os.page_latch_wait_count), + SUM(os.page_latch_wait_in_ms), + SUM(os.page_io_latch_wait_count), + SUM(os.page_io_latch_wait_in_ms)'; + + /* Get columnstore dictionary size - more info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2585 */ + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'column_store_dictionaries') + SET @dsql = @dsql + N' COALESCE((SELECT SUM (on_disk_size / 1024.0 / 1024) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_dictionaries dict WHERE dict.partition_id = ps.partition_id),0) AS reserved_dictionary_MB '; + ELSE + SET @dsql = @dsql + N' 0 AS reserved_dictionary_MB '; + + + SET @dsql = @dsql + N' + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_partition_stats AS ps + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partitions AS par on ps.partition_id=par.partition_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects AS so ON ps.object_id = so.object_id + AND so.is_ms_shipped = 0 /*Exclude objects shipped by Microsoft*/ + AND so.type <> ''TF'' /*Exclude table valued functions*/ + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s ON s.schema_id = so.schema_id + OUTER APPLY ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_index_operational_stats(' + + CAST(@DatabaseID AS NVARCHAR(10)) + N', ps.object_id, ps.index_id,ps.partition_number) AS os + OUTER APPLY (SELECT st.lock_escalation_desc + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.tables st + WHERE st.object_id = ps.object_id + AND ps.index_id < 2 ) le + WHERE 1=1 + ' + CASE WHEN @ObjectID IS NOT NULL THEN N'AND so.object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + N' ' ELSE N' ' END + N' + ' + CASE WHEN @Filter = 2 THEN N'AND ps.reserved_page_count * 8./1024. > ' + CAST(@FilterMB AS NVARCHAR(5)) + N' ' ELSE N' ' END + ' + GROUP BY ps.object_id, + s.name, + ps.index_id, + ps.partition_number, + ps.partition_id, + ps.row_count, + ps.reserved_page_count, + ps.lob_reserved_page_count, + ps.row_overflow_reserved_page_count, + le.lock_escalation_desc, + ' + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' THEN N'par.data_compression_desc ' ELSE N'null as data_compression_desc ' END + N' + ORDER BY ps.object_id, ps.index_id, ps.partition_number + OPTION ( RECOMPILE ); + '; + END; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #IndexPartitionSanity',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + INSERT #IndexPartitionSanity ( [database_id], + [object_id], + [schema_name], + index_id, + partition_number, + row_count, + reserved_MB, + reserved_LOB_MB, + reserved_row_overflow_MB, + lock_escalation_desc, + data_compression_desc, + leaf_insert_count, + leaf_delete_count, + leaf_update_count, + range_scan_count, + singleton_lookup_count, + forwarded_fetch_count, + lob_fetch_in_pages, + lob_fetch_in_bytes, + row_overflow_fetch_in_pages, + row_overflow_fetch_in_bytes, + row_lock_count, + row_lock_wait_count, + row_lock_wait_in_ms, + page_lock_count, + page_lock_wait_count, + page_lock_wait_in_ms, + index_lock_promotion_attempt_count, + index_lock_promotion_count, + page_latch_wait_count, + page_latch_wait_in_ms, + page_io_latch_wait_count, + page_io_latch_wait_in_ms, + reserved_dictionary_MB) + EXEC sp_executesql @dsql; + + END; --End Check For @SkipPartitions = 0 + + + + RAISERROR (N'Inserting data into #MissingIndexes',0,1) WITH NOWAIT; + SET @dsql=N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;' + + + SET @dsql = @dsql + 'WITH ColumnNamesWithDataTypes AS(SELECT id.index_handle,id.object_id,cn.IndexColumnType,STUFF((SELECT '', '' + cn_inner.ColumnName + '' '' + + N'' {'' + CASE WHEN ty.name IN ( ''varchar'', ''char'' ) THEN ty.name + ''('' + CASE WHEN co.max_length = -1 THEN ''max'' ELSE CAST(co.max_length AS VARCHAR(25)) END + '')'' + WHEN ty.name IN ( ''nvarchar'', ''nchar'' ) THEN ty.name + ''('' + CASE WHEN co.max_length = -1 THEN ''max'' ELSE CAST(co.max_length / 2 AS VARCHAR(25)) END + '')'' + WHEN ty.name IN ( ''decimal'', ''numeric'' ) THEN ty.name + ''('' + CAST(co.precision AS VARCHAR(25)) + '', '' + CAST(co.scale AS VARCHAR(25)) + '')'' + WHEN ty.name IN ( ''datetime2'' ) THEN ty.name + ''('' + CAST(co.scale AS VARCHAR(25)) + '')'' + ELSE ty.name END + ''}'' + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_details AS id_inner + CROSS APPLY( + SELECT LTRIM(RTRIM(v.value(''(./text())[1]'', ''varchar(max)''))) AS ColumnName, ''Equality'' AS IndexColumnType + FROM (VALUES (CONVERT(XML, N'''' + REPLACE((SELECT CAST(id_inner.equality_columns AS nvarchar(max)) FOR XML PATH('''')), N'','', N'''') + N''''))) x(n) + CROSS APPLY n.nodes(''x'') node(v) + UNION ALL + SELECT LTRIM(RTRIM(v.value(N''(./text())[1]'', ''varchar(max)''))) AS ColumnName, ''Inequality'' AS IndexColumnType + FROM (VALUES (CONVERT(XML, N'''' + REPLACE((SELECT CAST(id_inner.inequality_columns AS nvarchar(max)) FOR XML PATH('''')), N'','', N'''') + N''''))) x(n) + CROSS APPLY n.nodes(''x'') node(v) + UNION ALL + SELECT LTRIM(RTRIM(v.value(''(./text())[1]'', ''varchar(max)''))) AS ColumnName, ''Included'' AS IndexColumnType + FROM (VALUES (CONVERT(XML, N'''' + REPLACE((SELECT CAST(id_inner.included_columns AS nvarchar(max)) FOR XML PATH('''')), N'','', N'''') + N''''))) x(n) + CROSS APPLY n.nodes(''x'') node(v) + )AS cn_inner' + + /*split the string otherwise dsql cuts some of it out*/ + ' JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS co ON co.object_id = id_inner.object_id AND ''['' + co.name + '']'' = cn_inner.ColumnName + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.types AS ty ON ty.user_type_id = co.user_type_id + WHERE id_inner.index_handle = id.index_handle + AND id_inner.object_id = id.object_id + AND cn_inner.IndexColumnType = cn.IndexColumnType + FOR XML PATH('''') + ),1,1,'''') AS ReplaceColumnNames + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_details AS id + CROSS APPLY( + SELECT LTRIM(RTRIM(v.value(''(./text())[1]'', ''varchar(max)''))) AS ColumnName, ''Equality'' AS IndexColumnType + FROM (VALUES (CONVERT(XML, N'''' + REPLACE((SELECT CAST(id.equality_columns AS nvarchar(max)) FOR XML PATH('''')), N'','', N'''') + N''''))) x(n) + CROSS APPLY n.nodes(''x'') node(v) + UNION ALL + SELECT LTRIM(RTRIM(v.value(''(./text())[1]'', ''varchar(max)''))) AS ColumnName, ''Inequality'' AS IndexColumnType + FROM (VALUES (CONVERT(XML, N'''' + REPLACE((SELECT CAST(id.inequality_columns AS nvarchar(max)) FOR XML PATH('''')), N'','', N'''') + N''''))) x(n) + CROSS APPLY n.nodes(''x'') node(v) + UNION ALL + SELECT LTRIM(RTRIM(v.value(''(./text())[1]'', ''varchar(max)''))) AS ColumnName, ''Included'' AS IndexColumnType + FROM (VALUES (CONVERT(XML, N'''' + REPLACE((SELECT CAST(id.included_columns AS nvarchar(max)) FOR XML PATH('''')), N'','', N'''') + N''''))) x(n) + CROSS APPLY n.nodes(''x'') node(v) + )AS cn + GROUP BY id.index_handle,id.object_id,cn.IndexColumnType + ) + SELECT id.database_id, id.object_id, @i_DatabaseName, sc.[name], so.[name], id.statement , gs.avg_total_user_cost, + gs.avg_user_impact, gs.user_seeks, gs.user_scans, gs.unique_compiles, id.equality_columns, id.inequality_columns, id.included_columns, + ( + SELECT ColumnNamesWithDataTypes.ReplaceColumnNames + FROM ColumnNamesWithDataTypes WHERE ColumnNamesWithDataTypes.index_handle = id.index_handle + AND ColumnNamesWithDataTypes.object_id = id.object_id + AND ColumnNamesWithDataTypes.IndexColumnType = ''Equality'' + ) AS equality_columns_with_data_type + ,( + SELECT ColumnNamesWithDataTypes.ReplaceColumnNames + FROM ColumnNamesWithDataTypes WHERE ColumnNamesWithDataTypes.index_handle = id.index_handle + AND ColumnNamesWithDataTypes.object_id = id.object_id + AND ColumnNamesWithDataTypes.IndexColumnType = ''Inequality'' + ) AS inequality_columns_with_data_type + ,( + SELECT ColumnNamesWithDataTypes.ReplaceColumnNames + FROM ColumnNamesWithDataTypes WHERE ColumnNamesWithDataTypes.index_handle = id.index_handle + AND ColumnNamesWithDataTypes.object_id = id.object_id + AND ColumnNamesWithDataTypes.IndexColumnType = ''Included'' + ) AS included_columns_with_data_type ' + + /* Get the sample query plan if it's available, and if there are less than 1,000 rows in the DMV: */ + IF NOT EXISTS + ( + SELECT + 1/0 + FROM sys.all_objects AS o + WHERE o.name = 'dm_db_missing_index_group_stats_query' + ) + SELECT + @dsql += N' , NULL AS sample_query_plan ' + ELSE + BEGIN + /* The DMV is only supposed to have 600 rows in it. If it's got more, + they could see performance slowdowns - see Github #3085. */ + DECLARE @MissingIndexPlans BIGINT; + SET @StringToExecute = N'SELECT @MissingIndexPlans = COUNT(*) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_group_stats_query;' + EXEC sp_executesql @StringToExecute, N'@MissingIndexPlans BIGINT OUT', @MissingIndexPlans OUT; + + IF @MissingIndexPlans > 1000 + BEGIN + SELECT @dsql += N' , NULL AS sample_query_plan /* Over 1000 plans found, skipping */ '; + RAISERROR (N'Over 1000 plans found in sys.dm_db_missing_index_group_stats_query - your SQL Server is hitting a bug: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/3085',0,1) WITH NOWAIT; + END + ELSE + SELECT + @dsql += N' + , sample_query_plan = + ( + SELECT TOP (1) + p.query_plan + FROM sys.dm_db_missing_index_group_stats gs + CROSS APPLY + ( + SELECT TOP (1) + s.plan_handle + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_group_stats_query q + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_exec_query_stats s + ON q.query_plan_hash = s.query_plan_hash + WHERE gs.group_handle = q.group_handle + ORDER BY (q.user_seeks + q.user_scans) DESC, s.total_logical_reads DESC + ) q2 + CROSS APPLY sys.dm_exec_query_plan(q2.plan_handle) p + WHERE ig.index_group_handle = gs.group_handle + ) ' + END + + + + SET @dsql = @dsql + N'FROM ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_groups ig + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_details id ON ig.index_handle = id.index_handle + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_missing_index_group_stats gs ON ig.index_group_handle = gs.group_handle + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects so on + id.object_id=so.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas sc on + so.schema_id=sc.schema_id + WHERE id.database_id = ' + CAST(@DatabaseID AS NVARCHAR(30)) + ' + ' + CASE WHEN @ObjectID IS NULL THEN N'' + ELSE N'and id.object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + END + + N'OPTION (RECOMPILE);'; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + INSERT #MissingIndexes ( [database_id], [object_id], [database_name], [schema_name], [table_name], [statement], avg_total_user_cost, + avg_user_impact, user_seeks, user_scans, unique_compiles, equality_columns, + inequality_columns, included_columns, equality_columns_with_data_type, inequality_columns_with_data_type, + included_columns_with_data_type, sample_query_plan) + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + + SET @dsql = N' + SELECT DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N') AS [database_id], + @i_DatabaseName AS database_name, + s.name, + fk_object.name AS foreign_key_name, + parent_object.[object_id] AS parent_object_id, + parent_object.name AS parent_object_name, + referenced_object.[object_id] AS referenced_object_id, + referenced_object.name AS referenced_object_name, + fk.is_disabled, + fk.is_not_trusted, + fk.is_not_for_replication, + parent.fk_columns, + referenced.fk_columns, + [update_referential_action_desc], + [delete_referential_action_desc] + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.foreign_keys fk + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects fk_object ON fk.object_id=fk_object.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects parent_object ON fk.parent_object_id=parent_object.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects referenced_object ON fk.referenced_object_id=referenced_object.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s ON fk.schema_id=s.schema_id + CROSS APPLY ( SELECT STUFF( (SELECT N'', '' + c_parent.name AS fk_columns + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.foreign_key_columns fkc + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns c_parent ON fkc.parent_object_id=c_parent.[object_id] + AND fkc.parent_column_id=c_parent.column_id + WHERE fk.parent_object_id=fkc.parent_object_id + AND fk.[object_id]=fkc.constraint_object_id + ORDER BY fkc.constraint_column_id + FOR XML PATH('''') , + TYPE).value(''.'', ''nvarchar(max)''), 1, 1, '''')/*This is how we remove the first comma*/ ) parent ( fk_columns ) + CROSS APPLY ( SELECT STUFF( (SELECT N'', '' + c_referenced.name AS fk_columns + FROM ' + QUOTENAME(@DatabaseName) + N'.sys. foreign_key_columns fkc + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns c_referenced ON fkc.referenced_object_id=c_referenced.[object_id] + AND fkc.referenced_column_id=c_referenced.column_id + WHERE fk.referenced_object_id=fkc.referenced_object_id + and fk.[object_id]=fkc.constraint_object_id + ORDER BY fkc.constraint_column_id /*order by col name, we don''t have anything better*/ + FOR XML PATH('''') , + TYPE).value(''.'', ''nvarchar(max)''), 1, 1, '''') ) referenced ( fk_columns ) + ' + CASE WHEN @ObjectID IS NOT NULL THEN + 'WHERE fk.parent_object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + N' OR fk.referenced_object_id=' + CAST(@ObjectID AS NVARCHAR(30)) + N' ' + ELSE N' ' END + ' + ORDER BY parent_object_name, foreign_key_name + OPTION (RECOMPILE);'; + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #ForeignKeys',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + INSERT #ForeignKeys ( [database_id], [database_name], [schema_name], foreign_key_name, parent_object_id,parent_object_name, referenced_object_id, referenced_object_name, + is_disabled, is_not_trusted, is_not_for_replication, parent_fk_columns, referenced_fk_columns, + [update_referential_action_desc], [delete_referential_action_desc] ) + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + + + SET @dsql = N' + SELECT + DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N') AS [database_id], + @i_DatabaseName AS database_name, + foreign_key_schema = + s.name, + foreign_key_name = + fk.name, + foreign_key_table = + OBJECT_NAME(fk.parent_object_id, DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N')), + fk.parent_object_id, + foreign_key_referenced_table = + OBJECT_NAME(fk.referenced_object_id, DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N')), + fk.referenced_object_id + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.foreign_keys fk + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s + ON s.schema_id = fk.schema_id + WHERE fk.is_disabled = 0 + AND EXISTS + ( + SELECT + 1/0 + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.foreign_key_columns fkc + WHERE fkc.constraint_object_id = fk.object_id + AND NOT EXISTS + ( + SELECT + 1/0 + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns ic + WHERE ic.object_id = fkc.parent_object_id + AND ic.column_id = fkc.parent_column_id + AND ic.index_column_id = fkc.constraint_column_id + ) + ) + OPTION (RECOMPILE);' + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #ForeignKeys',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + + INSERT + #UnindexedForeignKeys + ( + database_id, + database_name, + schema_name, + foreign_key_name, + parent_object_name, + parent_object_id, + referenced_object_name, + referenced_object_id + ) + EXEC sys.sp_executesql + @dsql, + N'@i_DatabaseName sysname', + @DatabaseName; + + + IF @SkipStatistics = 0 /* AND DB_NAME() = @DatabaseName /* Can only get stats in the current database - see https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1947 */ */ + BEGIN + IF ((PARSENAME(@SQLServerProductVersion, 4) >= 12) + OR (PARSENAME(@SQLServerProductVersion, 4) = 11 AND PARSENAME(@SQLServerProductVersion, 2) >= 3000) + OR (PARSENAME(@SQLServerProductVersion, 4) = 10 AND PARSENAME(@SQLServerProductVersion, 3) = 50 AND PARSENAME(@SQLServerProductVersion, 2) >= 2500)) + BEGIN + RAISERROR (N'Gathering Statistics Info With Newer Syntax.',0,1) WITH NOWAIT; + SET @dsql=N'USE ' + QUOTENAME(@DatabaseName) + N'; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT #Statistics ( database_id, database_name, table_name, schema_name, index_name, column_names, statistics_name, last_statistics_update, + days_since_last_stats_update, rows, rows_sampled, percent_sampled, histogram_steps, modification_counter, + percent_modifications, modifications_before_auto_update, index_type_desc, table_create_date, table_modify_date, + no_recompute, has_filter, filter_definition) + SELECT DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N') AS [database_id], + @i_DatabaseName AS database_name, + obj.name AS table_name, + sch.name AS schema_name, + ISNULL(i.name, ''System Or User Statistic'') AS index_name, + ca.column_names AS column_names, + s.name AS statistics_name, + CONVERT(DATETIME, ddsp.last_updated) AS last_statistics_update, + DATEDIFF(DAY, ddsp.last_updated, GETDATE()) AS days_since_last_stats_update, + ddsp.rows, + ddsp.rows_sampled, + CAST(ddsp.rows_sampled / ( 1. * NULLIF(ddsp.rows, 0) ) * 100 AS DECIMAL(18, 1)) AS percent_sampled, + ddsp.steps AS histogram_steps, + ddsp.modification_counter, + CASE WHEN ddsp.modification_counter > 0 + THEN CAST(ddsp.modification_counter / ( 1. * NULLIF(ddsp.rows, 0) ) * 100 AS DECIMAL(18, 1)) + ELSE ddsp.modification_counter + END AS percent_modifications, + CASE WHEN ddsp.rows < 500 THEN 500 + ELSE CAST(( ddsp.rows * .20 ) + 500 AS INT) + END AS modifications_before_auto_update, + ISNULL(i.type_desc, ''System Or User Statistic - N/A'') AS index_type_desc, + CONVERT(DATETIME, obj.create_date) AS table_create_date, + CONVERT(DATETIME, obj.modify_date) AS table_modify_date, + s.no_recompute, + s.has_filter, + s.filter_definition + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.stats AS s + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects obj + ON s.object_id = obj.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas sch + ON sch.schema_id = obj.schema_id + LEFT JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.indexes AS i + ON i.object_id = s.object_id + AND i.index_id = s.stats_id + OUTER APPLY ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_stats_properties(s.object_id, s.stats_id) AS ddsp + CROSS APPLY ( SELECT STUFF((SELECT '', '' + c.name + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.stats_columns AS sc + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c + ON sc.column_id = c.column_id AND sc.object_id = c.object_id + WHERE sc.stats_id = s.stats_id AND sc.object_id = s.object_id + ORDER BY sc.stats_column_id + FOR XML PATH(''''), TYPE).value(''.'', ''nvarchar(max)''), 1, 2, '''') + ) ca (column_names) + WHERE obj.is_ms_shipped = 0 + OPTION (RECOMPILE);'; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #Statistics',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + END; + ELSE + BEGIN + RAISERROR (N'Gathering Statistics Info With Older Syntax.',0,1) WITH NOWAIT; + SET @dsql=N'USE ' + QUOTENAME(@DatabaseName) + N'; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + INSERT #Statistics(database_id, database_name, table_name, schema_name, index_name, column_names, statistics_name, + last_statistics_update, days_since_last_stats_update, rows, modification_counter, + percent_modifications, modifications_before_auto_update, index_type_desc, table_create_date, table_modify_date, + no_recompute, has_filter, filter_definition) + SELECT DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N') AS [database_id], + @i_DatabaseName AS database_name, + obj.name AS table_name, + sch.name AS schema_name, + ISNULL(i.name, ''System Or User Statistic'') AS index_name, + ca.column_names AS column_names, + s.name AS statistics_name, + CONVERT(DATETIME, STATS_DATE(s.object_id, s.stats_id)) AS last_statistics_update, + DATEDIFF(DAY, STATS_DATE(s.object_id, s.stats_id), GETDATE()) AS days_since_last_stats_update, + si.rowcnt, + si.rowmodctr, + CASE WHEN si.rowmodctr > 0 THEN CAST(si.rowmodctr / ( 1. * NULLIF(si.rowcnt, 0) ) * 100 AS DECIMAL(18, 1)) + ELSE si.rowmodctr + END AS percent_modifications, + CASE WHEN si.rowcnt < 500 THEN 500 + ELSE CAST(( si.rowcnt * .20 ) + 500 AS INT) + END AS modifications_before_auto_update, + ISNULL(i.type_desc, ''System Or User Statistic - N/A'') AS index_type_desc, + CONVERT(DATETIME, obj.create_date) AS table_create_date, + CONVERT(DATETIME, obj.modify_date) AS table_modify_date, + s.no_recompute, + ' + + CASE WHEN @SQLServerProductVersion NOT LIKE '9%' + THEN N's.has_filter, + s.filter_definition' + ELSE N'NULL AS has_filter, + NULL AS filter_definition' END + + N' + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.stats AS s + INNER HASH JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.sysindexes si + ON si.name = s.name AND s.object_id = si.id + INNER HASH JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.objects obj + ON s.object_id = obj.object_id + INNER HASH JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas sch + ON sch.schema_id = obj.schema_id + LEFT HASH JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.indexes AS i + ON i.object_id = s.object_id + AND i.index_id = s.stats_id + CROSS APPLY ( SELECT STUFF((SELECT '', '' + c.name + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.stats_columns AS sc + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c + ON sc.column_id = c.column_id AND sc.object_id = c.object_id + WHERE sc.stats_id = s.stats_id AND sc.object_id = s.object_id + ORDER BY sc.stats_column_id + FOR XML PATH(''''), TYPE).value(''.'', ''nvarchar(max)''), 1, 2, '''') + ) ca (column_names) + WHERE obj.is_ms_shipped = 0 + AND si.rowcnt > 0 + OPTION (RECOMPILE);'; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + RAISERROR (N'Inserting data into #Statistics',0,1) WITH NOWAIT; + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + END; + + END; + + IF (PARSENAME(@SQLServerProductVersion, 4) >= 10) + BEGIN + RAISERROR (N'Gathering Computed Column Info.',0,1) WITH NOWAIT; + SET @dsql=N'SELECT DB_ID(@i_DatabaseName) AS [database_id], + @i_DatabaseName AS database_name, + t.name AS table_name, + s.name AS schema_name, + c.name AS column_name, + cc.is_nullable, + cc.definition, + cc.uses_database_collation, + cc.is_persisted, + cc.is_computed, + CASE WHEN cc.definition LIKE ''%|].|[%'' ESCAPE ''|'' THEN 1 ELSE 0 END AS is_function, + ''ALTER TABLE '' + QUOTENAME(s.name) + ''.'' + QUOTENAME(t.name) + + '' ADD '' + QUOTENAME(c.name) + '' AS '' + cc.definition + + CASE WHEN is_persisted = 1 THEN '' PERSISTED'' ELSE '''' END + '';'' COLLATE DATABASE_DEFAULT AS [column_definition] + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.computed_columns AS cc + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c + ON cc.object_id = c.object_id + AND cc.column_id = c.column_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.tables AS t + ON t.object_id = cc.object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s + ON s.schema_id = t.schema_id + OPTION (RECOMPILE);'; + + IF @dsql IS NULL RAISERROR('@dsql is null',16,1); + + INSERT #ComputedColumns + ( database_id, [database_name], table_name, schema_name, column_name, is_nullable, definition, + uses_database_collation, is_persisted, is_computed, is_function, column_definition ) + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + + END; + + RAISERROR (N'Gathering Trace Flag Information',0,1) WITH NOWAIT; + INSERT #TraceStatus + EXEC ('DBCC TRACESTATUS(-1) WITH NO_INFOMSGS'); + + IF (PARSENAME(@SQLServerProductVersion, 4) >= 13) + BEGIN + RAISERROR (N'Gathering Temporal Table Info',0,1) WITH NOWAIT; + SET @dsql=N'SELECT ' + QUOTENAME(@DatabaseName,'''') + N' AS database_name, + DB_ID(N' + QUOTENAME(@DatabaseName,'''') + N') AS [database_id], + s.name AS schema_name, + t.name AS table_name, + oa.hsn as history_schema_name, + oa.htn AS history_table_name, + c1.name AS start_column_name, + c2.name AS end_column_name, + p.name AS period_name + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.periods AS p + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.tables AS t + ON p.object_id = t.object_id + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c1 + ON t.object_id = c1.object_id + AND p.start_column_id = c1.column_id + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c2 + ON t.object_id = c2.object_id + AND p.end_column_id = c2.column_id + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s + ON t.schema_id = s.schema_id + CROSS APPLY ( SELECT s2.name as hsn, t2.name htn + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.tables AS t2 + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s2 + ON t2.schema_id = s2.schema_id + WHERE t2.object_id = t.history_table_id + AND t2.temporal_type = 1 /*History table*/ ) AS oa + WHERE t.temporal_type IN ( 2, 4 ) /*BOL currently points to these types, but has no definition for 4*/ + OPTION (RECOMPILE); + '; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + + INSERT #TemporalTables ( database_name, database_id, schema_name, table_name, history_schema_name, + history_table_name, start_column_name, end_column_name, period_name ) + + EXEC sp_executesql @dsql; + + SET @dsql=N'SELECT DB_ID(@i_DatabaseName) AS [database_id], + @i_DatabaseName AS database_name, + t.name AS table_name, + s.name AS schema_name, + cc.name AS constraint_name, + cc.is_disabled, + cc.definition, + cc.uses_database_collation, + cc.is_not_trusted, + CASE WHEN cc.definition LIKE ''%|].|[%'' ESCAPE ''|'' THEN 1 ELSE 0 END AS is_function, + ''ALTER TABLE '' + QUOTENAME(s.name) + ''.'' + QUOTENAME(t.name) + + '' ADD CONSTRAINT '' + QUOTENAME(cc.name) + '' CHECK '' + cc.definition + '';'' COLLATE DATABASE_DEFAULT AS [column_definition] + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.check_constraints AS cc + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.tables AS t + ON t.object_id = cc.parent_object_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s + ON s.schema_id = t.schema_id + OPTION (RECOMPILE);'; + + INSERT #CheckConstraints + ( database_id, [database_name], table_name, schema_name, constraint_name, is_disabled, definition, + uses_database_collation, is_not_trusted, is_function, column_definition ) + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + + + SET @dsql=N'SELECT DB_ID(@i_DatabaseName) AS [database_id], + @i_DatabaseName AS database_name, + s.name AS missing_schema_name, + t.name AS missing_table_name, + i.name AS missing_index_name, + c.name AS missing_column_name + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.sql_expression_dependencies AS sed + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.tables AS t + ON t.object_id = sed.referenced_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s + ON t.schema_id = s.schema_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.indexes AS i + ON i.object_id = sed.referenced_id + AND i.index_id = sed.referencing_minor_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns AS c + ON c.object_id = sed.referenced_id + AND c.column_id = sed.referenced_minor_id + WHERE sed.referencing_class = 7 + AND sed.referenced_class = 1 + AND i.has_filter = 1 + AND NOT EXISTS ( SELECT 1/0 + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns AS ic + WHERE ic.index_id = sed.referencing_minor_id + AND ic.column_id = sed.referenced_minor_id + AND ic.object_id = sed.referenced_id ) + OPTION(RECOMPILE);' + + INSERT #FilteredIndexes ( database_id, database_name, schema_name, table_name, index_name, column_name ) + EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName; + + + END; + +END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure populating temp tables.', 0,1) WITH NOWAIT; + + IF @dsql IS NOT NULL + BEGIN + SET @msg= 'Last @dsql: ' + @dsql; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); + RAISERROR (@msg,@ErrorSeverity, @ErrorState )WITH NOWAIT; + + + WHILE @@trancount > 0 + ROLLBACK; + + RETURN; +END CATCH; + FETCH NEXT FROM c1 INTO @DatabaseName; +END; +DEALLOCATE c1; + + + + + + +---------------------------------------- +--STEP 2: PREP THE TEMP TABLES +--EVERY QUERY AFTER THIS GOES AGAINST TEMP TABLES ONLY. +---------------------------------------- + +RAISERROR (N'Updating #IndexSanity.key_column_names',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET key_column_names = D1.key_column_names +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + c.column_name + + N' {' + system_type_name + + CASE max_length WHEN -1 THEN N' (max)' ELSE + CASE + WHEN system_type_name IN (N'char',N'varchar',N'binary',N'varbinary') THEN N' (' + CAST(max_length AS NVARCHAR(20)) + N')' + WHEN system_type_name IN (N'nchar',N'nvarchar') THEN N' (' + CAST(max_length/2 AS NVARCHAR(20)) + N')' + ELSE N' ' + CAST(max_length AS NVARCHAR(50)) + END + END + + N'}' + AS col_definition + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + AND c.is_included_column = 0 /*Just Keys*/ + AND c.key_ordinal > 0 /*Ignore non-key columns, such as partitioning keys*/ + ORDER BY c.object_id, c.index_id, c.key_ordinal + FOR XML PATH('') ,TYPE).value('.', 'nvarchar(max)'), 1, 1, '')) + ) D1 ( key_column_names ); + +RAISERROR (N'Updating #IndexSanity.partition_key_column_name',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET partition_key_column_name = D1.partition_key_column_name +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + c.column_name AS col_definition + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + AND c.partition_ordinal <> 0 /*Just Partitioned Keys*/ + ORDER BY c.object_id, c.index_id, c.key_ordinal + FOR XML PATH('') , TYPE).value('.', 'nvarchar(max)'), 1, 1,''))) D1 + ( partition_key_column_name ); + +RAISERROR (N'Updating #IndexSanity.key_column_names_with_sort_order',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET key_column_names_with_sort_order = D2.key_column_names_with_sort_order +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + c.column_name + CASE c.is_descending_key + WHEN 1 THEN N' DESC' + ELSE N'' + END + + N' {' + system_type_name + + CASE max_length WHEN -1 THEN N' (max)' ELSE + CASE + WHEN system_type_name IN (N'char',N'varchar',N'binary',N'varbinary') THEN N' (' + CAST(max_length AS NVARCHAR(20)) + N')' + WHEN system_type_name IN (N'nchar',N'nvarchar') THEN N' (' + CAST(max_length/2 AS NVARCHAR(20)) + N')' + ELSE N' ' + CAST(max_length AS NVARCHAR(50)) + END + END + + N'}' + AS col_definition + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + AND c.is_included_column = 0 /*Just Keys*/ + AND c.key_ordinal > 0 /*Ignore non-key columns, such as partitioning keys*/ + ORDER BY c.object_id, c.index_id, c.key_ordinal + FOR XML PATH('') , TYPE).value('.', 'nvarchar(max)'), 1, 1, '')) + ) D2 ( key_column_names_with_sort_order ); + +RAISERROR (N'Updating #IndexSanity.key_column_names_with_sort_order_no_types (for create tsql)',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET key_column_names_with_sort_order_no_types = D2.key_column_names_with_sort_order_no_types +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + QUOTENAME(c.column_name) + CASE c.is_descending_key + WHEN 1 THEN N' DESC' + ELSE N'' + END AS col_definition + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + AND c.is_included_column = 0 /*Just Keys*/ + AND c.key_ordinal > 0 /*Ignore non-key columns, such as partitioning keys*/ + ORDER BY c.object_id, c.index_id, c.key_ordinal + FOR XML PATH('') , TYPE).value('.', 'nvarchar(max)'), 1, 1, '')) + ) D2 ( key_column_names_with_sort_order_no_types ); + +RAISERROR (N'Updating #IndexSanity.include_column_names',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET include_column_names = D3.include_column_names +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + c.column_name + + N' {' + system_type_name + + CASE max_length WHEN -1 THEN N' (max)' ELSE + CASE + WHEN system_type_name IN (N'char',N'varchar',N'binary',N'varbinary') THEN N' (' + CAST(max_length AS NVARCHAR(20)) + N')' + WHEN system_type_name IN (N'nchar',N'nvarchar') THEN N' (' + CAST(max_length/2 AS NVARCHAR(20)) + N')' + ELSE N' ' + CAST(max_length AS NVARCHAR(50)) + END + END + + N'}' + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + AND c.is_included_column = 1 /*Just includes*/ + ORDER BY c.column_name /*Order doesn't matter in includes, + this is here to make rows easy to compare.*/ + FOR XML PATH('') , TYPE).value('.', 'nvarchar(max)'), 1, 1, '')) + ) D3 ( include_column_names ); + +RAISERROR (N'Updating #IndexSanity.include_column_names_no_types (for create tsql)',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET include_column_names_no_types = D3.include_column_names_no_types +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + QUOTENAME(c.column_name) + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + AND c.is_included_column = 1 /*Just includes*/ + ORDER BY c.column_name /*Order doesn't matter in includes, + this is here to make rows easy to compare.*/ + FOR XML PATH('') , TYPE).value('.', 'nvarchar(max)'), 1, 1, '')) + ) D3 ( include_column_names_no_types ); + +RAISERROR (N'Updating #IndexSanity.count_key_columns and count_include_columns',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET count_included_columns = D4.count_included_columns, + count_key_columns = D4.count_key_columns +FROM #IndexSanity si + CROSS APPLY ( SELECT SUM(CASE WHEN is_included_column = 'true' THEN 1 + ELSE 0 + END) AS count_included_columns, + SUM(CASE WHEN is_included_column = 'false' AND c.key_ordinal > 0 THEN 1 + ELSE 0 + END) AS count_key_columns + FROM #IndexColumns c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.object_id = si.object_id + AND c.index_id = si.index_id + ) AS D4 ( count_included_columns, count_key_columns ); + +RAISERROR (N'Updating index_sanity_id on #IndexPartitionSanity',0,1) WITH NOWAIT; +UPDATE #IndexPartitionSanity +SET index_sanity_id = i.index_sanity_id +FROM #IndexPartitionSanity ps + JOIN #IndexSanity i ON ps.[object_id] = i.[object_id] + AND ps.index_id = i.index_id + AND i.database_id = ps.database_id + AND i.schema_name = ps.schema_name; + + +RAISERROR (N'Inserting data into #IndexSanitySize',0,1) WITH NOWAIT; +INSERT #IndexSanitySize ( [index_sanity_id], [database_id], [schema_name], [lock_escalation_desc], partition_count, total_rows, total_reserved_MB, + total_reserved_LOB_MB, total_reserved_row_overflow_MB, total_reserved_dictionary_MB, total_range_scan_count, + total_singleton_lookup_count, total_leaf_delete_count, total_leaf_update_count, + total_forwarded_fetch_count,total_row_lock_count, + total_row_lock_wait_count, total_row_lock_wait_in_ms, avg_row_lock_wait_in_ms, + total_page_lock_count, total_page_lock_wait_count, total_page_lock_wait_in_ms, + avg_page_lock_wait_in_ms, total_index_lock_promotion_attempt_count, + total_index_lock_promotion_count, data_compression_desc, + page_latch_wait_count, page_latch_wait_in_ms, page_io_latch_wait_count, page_io_latch_wait_in_ms) + SELECT index_sanity_id, ipp.database_id, ipp.schema_name, ipp.lock_escalation_desc, + COUNT(*), SUM(row_count), SUM(reserved_MB), + SUM(reserved_LOB_MB) - SUM(reserved_dictionary_MB), /* Subtract columnstore dictionaries from LOB data */ + SUM(reserved_row_overflow_MB), + SUM(reserved_dictionary_MB), + SUM(range_scan_count), + SUM(singleton_lookup_count), + SUM(leaf_delete_count), + SUM(leaf_update_count), + SUM(forwarded_fetch_count), + SUM(row_lock_count), + SUM(row_lock_wait_count), + SUM(row_lock_wait_in_ms), + CASE WHEN SUM(row_lock_wait_in_ms) > 0 THEN + SUM(row_lock_wait_in_ms)/(1.*SUM(row_lock_wait_count)) + ELSE 0 END AS avg_row_lock_wait_in_ms, + SUM(page_lock_count), + SUM(page_lock_wait_count), + SUM(page_lock_wait_in_ms), + CASE WHEN SUM(page_lock_wait_in_ms) > 0 THEN + SUM(page_lock_wait_in_ms)/(1.*SUM(page_lock_wait_count)) + ELSE 0 END AS avg_page_lock_wait_in_ms, + SUM(index_lock_promotion_attempt_count), + SUM(index_lock_promotion_count), + LEFT(MAX(data_compression_info.data_compression_rollup),4000), + SUM(page_latch_wait_count), + SUM(page_latch_wait_in_ms), + SUM(page_io_latch_wait_count), + SUM(page_io_latch_wait_in_ms) + FROM #IndexPartitionSanity ipp + /* individual partitions can have distinct compression settings, just roll them into a list here*/ + OUTER APPLY (SELECT STUFF(( + SELECT N', ' + data_compression_desc + FROM #IndexPartitionSanity ipp2 + WHERE ipp.[object_id]=ipp2.[object_id] + AND ipp.[index_id]=ipp2.[index_id] + AND ipp.database_id = ipp2.database_id + AND ipp.schema_name = ipp2.schema_name + ORDER BY ipp2.partition_number + FOR XML PATH(''),TYPE).value('.', 'nvarchar(max)'), 1, 1, '')) + data_compression_info(data_compression_rollup) + GROUP BY index_sanity_id, ipp.database_id, ipp.schema_name, ipp.lock_escalation_desc + ORDER BY index_sanity_id +OPTION ( RECOMPILE ); + +RAISERROR (N'Determining index usefulness',0,1) WITH NOWAIT; +UPDATE #MissingIndexes +SET is_low = CASE WHEN (user_seeks + user_scans) < 5000 + OR unique_compiles = 1 + THEN 1 + ELSE 0 + END; + +RAISERROR (N'Updating #IndexSanity.referenced_by_foreign_key',0,1) WITH NOWAIT; +UPDATE #IndexSanity + SET is_referenced_by_foreign_key=1 +FROM #IndexSanity s +JOIN #ForeignKeys fk ON + s.object_id=fk.referenced_object_id + AND s.database_id=fk.database_id + AND LEFT(s.key_column_names,LEN(fk.referenced_fk_columns)) = fk.referenced_fk_columns; + +RAISERROR (N'Update index_secret on #IndexSanity for NC indexes.',0,1) WITH NOWAIT; +UPDATE nc +SET secret_columns= + N'[' + + CASE tb.count_key_columns WHEN 0 THEN '1' ELSE CAST(tb.count_key_columns AS NVARCHAR(10)) END + + CASE nc.is_unique WHEN 1 THEN N' INCLUDE' ELSE N' KEY' END + + CASE WHEN tb.count_key_columns > 1 THEN N'S] ' ELSE N'] ' END + + CASE tb.index_id WHEN 0 THEN '[RID]' ELSE LTRIM(tb.key_column_names) + + /* Uniquifiers only needed on non-unique clustereds-- not heaps */ + CASE tb.is_unique WHEN 0 THEN ' [UNIQUIFIER]' ELSE N'' END + END + , count_secret_columns= + CASE tb.index_id WHEN 0 THEN 1 ELSE + tb.count_key_columns + + CASE tb.is_unique WHEN 0 THEN 1 ELSE 0 END + END +FROM #IndexSanity AS nc +JOIN #IndexSanity AS tb ON nc.object_id=tb.object_id + AND nc.database_id = tb.database_id + AND nc.schema_name = tb.schema_name + AND tb.index_id IN (0,1) +WHERE nc.index_id > 1; + +RAISERROR (N'Update index_secret on #IndexSanity for heaps and non-unique clustered.',0,1) WITH NOWAIT; +UPDATE tb +SET secret_columns= CASE tb.index_id WHEN 0 THEN '[RID]' ELSE '[UNIQUIFIER]' END + , count_secret_columns = 1 +FROM #IndexSanity AS tb +WHERE tb.index_id = 0 /*Heaps-- these have the RID */ + OR (tb.index_id=1 AND tb.is_unique=0); /* Non-unique CX: has uniquifer (when needed) */ + + +RAISERROR (N'Populate #IndexCreateTsql.',0,1) WITH NOWAIT; +INSERT #IndexCreateTsql (index_sanity_id, create_tsql) +SELECT + index_sanity_id, + ISNULL ( + CASE index_id WHEN 0 THEN N'ALTER TABLE ' + QUOTENAME([database_name]) + N'.' + QUOTENAME([schema_name]) + N'.' + QUOTENAME([object_name]) + ' REBUILD;' + ELSE + CASE WHEN is_XML = 1 OR is_spatial = 1 OR is_in_memory_oltp = 1 THEN N'' /* Not even trying for these just yet...*/ + ELSE + CASE WHEN is_primary_key=1 THEN + N'ALTER TABLE ' + QUOTENAME([database_name]) + N'.' + QUOTENAME([schema_name]) + + N'.' + QUOTENAME([object_name]) + + N' ADD CONSTRAINT [' + + index_name + + N'] PRIMARY KEY ' + + CASE WHEN index_id=1 THEN N'CLUSTERED (' ELSE N'(' END + + key_column_names_with_sort_order_no_types + N' )' + WHEN is_unique_constraint = 1 AND is_primary_key = 0 + THEN + N'ALTER TABLE ' + QUOTENAME([database_name]) + N'.' + QUOTENAME([schema_name]) + + N'.' + QUOTENAME([object_name]) + + N' ADD CONSTRAINT [' + + index_name + + N'] UNIQUE ' + + CASE WHEN index_id=1 THEN N'CLUSTERED (' ELSE N'(' END + + key_column_names_with_sort_order_no_types + N' )' + WHEN is_CX_columnstore= 1 THEN + N'CREATE CLUSTERED COLUMNSTORE INDEX ' + QUOTENAME(index_name) + N' on ' + QUOTENAME([database_name]) + N'.' + QUOTENAME([schema_name]) + N'.' + QUOTENAME([object_name]) + ELSE /*Else not a PK or cx columnstore */ + N'CREATE ' + + CASE WHEN is_unique=1 THEN N'UNIQUE ' ELSE N'' END + + CASE WHEN index_id=1 THEN N'CLUSTERED ' ELSE N'' END + + CASE WHEN is_NC_columnstore=1 THEN N'NONCLUSTERED COLUMNSTORE ' + ELSE N'' END + + N'INDEX [' + + index_name + N'] ON ' + + QUOTENAME([database_name]) + N'.' + + QUOTENAME([schema_name]) + N'.' + QUOTENAME([object_name]) + + CASE WHEN is_NC_columnstore=1 THEN + N' (' + ISNULL(include_column_names_no_types,'') + N' )' + ELSE /*Else not columnstore */ + N' (' + ISNULL(key_column_names_with_sort_order_no_types,'') + N' )' + + CASE WHEN include_column_names_no_types IS NOT NULL THEN + N' INCLUDE (' + include_column_names_no_types + N')' + ELSE N'' + END + END /*End non-columnstore case */ + + CASE WHEN filter_definition <> N'' THEN N' WHERE ' + filter_definition ELSE N'' END + END /*End Non-PK index CASE */ + + CASE WHEN is_NC_columnstore=0 AND is_CX_columnstore=0 THEN + N' WITH (' + + N'FILLFACTOR=' + CASE fill_factor WHEN 0 THEN N'100' ELSE CAST(fill_factor AS NVARCHAR(5)) END + ', ' + + N'ONLINE=?, SORT_IN_TEMPDB=?, DATA_COMPRESSION=?' + + N')' + ELSE N'' END + + N';' + END /*End non-spatial and non-xml CASE */ + END, '[Unknown Error]') + AS create_tsql +FROM #IndexSanity; + +RAISERROR (N'Populate #PartitionCompressionInfo.',0,1) WITH NOWAIT; +WITH maps + AS + ( + SELECT ips.index_sanity_id, + ips.partition_number, + ips.data_compression_desc, + ips.partition_number - ROW_NUMBER() OVER ( PARTITION BY ips.index_sanity_id, ips.data_compression_desc + ORDER BY ips.partition_number ) AS rn + FROM #IndexPartitionSanity AS ips + ) +SELECT * +INTO #maps +FROM maps; + +WITH grps + AS + ( + SELECT MIN(maps.partition_number) AS MinKey, + MAX(maps.partition_number) AS MaxKey, + maps.index_sanity_id, + maps.data_compression_desc + FROM #maps AS maps + GROUP BY maps.rn, maps.index_sanity_id, maps.data_compression_desc + ) +SELECT * +INTO #grps +FROM grps; + +INSERT #PartitionCompressionInfo ( index_sanity_id, partition_compression_detail ) +SELECT DISTINCT + grps.index_sanity_id, + SUBSTRING( + ( STUFF( + ( SELECT N', ' + N' Partition' + + CASE + WHEN grps2.MinKey < grps2.MaxKey + THEN + + N's ' + CAST(grps2.MinKey AS NVARCHAR(10)) + N' - ' + + CAST(grps2.MaxKey AS NVARCHAR(10)) + N' use ' + grps2.data_compression_desc + ELSE + N' ' + CAST(grps2.MinKey AS NVARCHAR(10)) + N' uses ' + grps2.data_compression_desc + END AS Partitions + FROM #grps AS grps2 + WHERE grps2.index_sanity_id = grps.index_sanity_id + ORDER BY grps2.MinKey, grps2.MaxKey + FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'), 1, 1, '')), 0, 8000) AS partition_compression_detail +FROM #grps AS grps; + +RAISERROR (N'Update #PartitionCompressionInfo.',0,1) WITH NOWAIT; +UPDATE sz +SET sz.data_compression_desc = pci.partition_compression_detail +FROM #IndexSanitySize sz +JOIN #PartitionCompressionInfo AS pci +ON pci.index_sanity_id = sz.index_sanity_id; + +RAISERROR (N'Update #IndexSanity for filtered indexes with columns not in the index definition.',0,1) WITH NOWAIT; +UPDATE #IndexSanity +SET filter_columns_not_in_index = D1.filter_columns_not_in_index +FROM #IndexSanity si + CROSS APPLY ( SELECT RTRIM(STUFF( (SELECT N', ' + c.column_name AS col_definition + FROM #FilteredIndexes AS c + WHERE c.database_id= si.database_id + AND c.schema_name = si.schema_name + AND c.table_name = si.object_name + AND c.index_name = si.index_name + ORDER BY c.index_sanity_id + FOR XML PATH('') , TYPE).value('.', 'nvarchar(max)'), 1, 1,''))) D1 + ( filter_columns_not_in_index ); + + +IF @Debug = 1 +BEGIN + SELECT '#BlitzIndexResults' AS table_name, * FROM #BlitzIndexResults AS bir; + SELECT '#IndexSanity' AS table_name, * FROM #IndexSanity; + SELECT '#IndexPartitionSanity' AS table_name, * FROM #IndexPartitionSanity; + SELECT '#IndexSanitySize' AS table_name, * FROM #IndexSanitySize; + SELECT '#IndexColumns' AS table_name, * FROM #IndexColumns; + SELECT '#MissingIndexes' AS table_name, * FROM #MissingIndexes; + SELECT '#ForeignKeys' AS table_name, * FROM #ForeignKeys; + SELECT '#UnindexedForeignKeys' AS table_name, * FROM #UnindexedForeignKeys; + SELECT '#BlitzIndexResults' AS table_name, * FROM #BlitzIndexResults; + SELECT '#IndexCreateTsql' AS table_name, * FROM #IndexCreateTsql; + SELECT '#DatabaseList' AS table_name, * FROM #DatabaseList; + SELECT '#Statistics' AS table_name, * FROM #Statistics; + SELECT '#PartitionCompressionInfo' AS table_name, * FROM #PartitionCompressionInfo; + SELECT '#ComputedColumns' AS table_name, * FROM #ComputedColumns; + SELECT '#TraceStatus' AS table_name, * FROM #TraceStatus; + SELECT '#CheckConstraints' AS table_name, * FROM #CheckConstraints; + SELECT '#FilteredIndexes' AS table_name, * FROM #FilteredIndexes; +END + + +---------------------------------------- +--STEP 3: DIAGNOSE THE PATIENT +---------------------------------------- + + +BEGIN TRY +---------------------------------------- +--If @TableName is specified, just return information for that table. +--The @Mode parameter doesn't matter if you're looking at a specific table. +---------------------------------------- +IF @TableName IS NOT NULL +BEGIN + RAISERROR(N'@TableName specified, giving detail only on that table.', 0,1) WITH NOWAIT; + + --We do a left join here in case this is a disabled NC. + --In that case, it won't have any size info/pages allocated. + + IF (@ShowColumnstoreOnly = 0) + BEGIN + WITH table_mode_cte AS ( + SELECT + s.db_schema_object_indexid, + s.key_column_names, + s.index_definition, + ISNULL(s.secret_columns,N'') AS secret_columns, + s.fill_factor, + s.index_usage_summary, + sz.index_op_stats, + ISNULL(sz.index_size_summary,'') /*disabled NCs will be null*/ AS index_size_summary, + partition_compression_detail , + ISNULL(sz.index_lock_wait_summary,'') AS index_lock_wait_summary, + s.is_referenced_by_foreign_key, + (SELECT COUNT(*) + FROM #ForeignKeys fk WHERE fk.parent_object_id=s.object_id + AND PATINDEX (fk.parent_fk_columns, s.key_column_names)=1) AS FKs_covered_by_index, + s.last_user_seek, + s.last_user_scan, + s.last_user_lookup, + s.last_user_update, + s.create_date, + s.modify_date, + sz.page_latch_wait_count, + CONVERT(VARCHAR(10), (sz.page_latch_wait_in_ms / 1000) / 86400) + ':' + CONVERT(VARCHAR(20), DATEADD(s, (sz.page_latch_wait_in_ms / 1000), 0), 108) AS page_latch_wait_time, + sz.page_io_latch_wait_count, + CONVERT(VARCHAR(10), (sz.page_io_latch_wait_in_ms / 1000) / 86400) + ':' + CONVERT(VARCHAR(20), DATEADD(s, (sz.page_io_latch_wait_in_ms / 1000), 0), 108) AS page_io_latch_wait_time, + ct.create_tsql, + CASE + WHEN s.is_primary_key = 1 AND s.index_definition <> '[HEAP]' + THEN N'--ALTER TABLE ' + QUOTENAME(s.[database_name]) + N'.' + QUOTENAME(s.[schema_name]) + N'.' + QUOTENAME(s.[object_name]) + + N' DROP CONSTRAINT ' + QUOTENAME(s.index_name) + N';' + WHEN s.is_primary_key = 0 AND is_unique_constraint = 1 AND s.index_definition <> '[HEAP]' + THEN N'--ALTER TABLE ' + QUOTENAME(s.[database_name]) + N'.' + QUOTENAME(s.[schema_name]) + N'.' + QUOTENAME(s.[object_name]) + + N' DROP CONSTRAINT ' + QUOTENAME(s.index_name) + N';' + WHEN s.is_primary_key = 0 AND s.index_definition <> '[HEAP]' + THEN N'--DROP INDEX '+ QUOTENAME(s.index_name) + N' ON ' + QUOTENAME(s.[database_name]) + N'.' + + QUOTENAME(s.[schema_name]) + N'.' + QUOTENAME(s.[object_name]) + N';' + ELSE N'' + END AS drop_tsql, + 1 AS display_order + FROM #IndexSanity s + LEFT JOIN #IndexSanitySize sz ON + s.index_sanity_id=sz.index_sanity_id + LEFT JOIN #IndexCreateTsql ct ON + s.index_sanity_id=ct.index_sanity_id + LEFT JOIN #PartitionCompressionInfo pci ON + pci.index_sanity_id = s.index_sanity_id + WHERE s.[object_id]=@ObjectID + UNION ALL + SELECT N'Database ' + QUOTENAME(@DatabaseName) + N' as of ' + CONVERT(NVARCHAR(16),GETDATE(),121) + + N' (' + @ScriptVersionName + ')' , + N'SQL Server First Responder Kit' , + N'http://FirstResponderKit.org' , + N'From Your Community Volunteers', + NULL,@DaysUptimeInsertValue,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, + 0 AS display_order + ) + SELECT + db_schema_object_indexid AS [Details: db_schema.table.index(indexid)], + index_definition AS [Definition: [Property]] ColumnName {datatype maxbytes}], + secret_columns AS [Secret Columns], + fill_factor AS [Fillfactor], + index_usage_summary AS [Usage Stats], + index_op_stats AS [Op Stats], + index_size_summary AS [Size], + partition_compression_detail AS [Compression Type], + index_lock_wait_summary AS [Lock Waits], + is_referenced_by_foreign_key AS [Referenced by FK?], + FKs_covered_by_index AS [FK Covered by Index?], + last_user_seek AS [Last User Seek], + last_user_scan AS [Last User Scan], + last_user_lookup AS [Last User Lookup], + last_user_update AS [Last User Write], + create_date AS [Created], + modify_date AS [Last Modified], + page_latch_wait_count AS [Page Latch Wait Count], + page_latch_wait_time as [Page Latch Wait Time (D:H:M:S)], + page_io_latch_wait_count AS [Page IO Latch Wait Count], + page_io_latch_wait_time as [Page IO Latch Wait Time (D:H:M:S)], + create_tsql AS [Create TSQL], + drop_tsql AS [Drop TSQL] + FROM table_mode_cte + ORDER BY display_order ASC, key_column_names ASC + OPTION ( RECOMPILE ); + + IF (SELECT TOP 1 [object_id] FROM #MissingIndexes mi) IS NOT NULL + BEGIN; + + WITH create_date AS ( + SELECT i.database_id, + i.schema_name, + i.[object_id], + ISNULL(NULLIF(MAX(DATEDIFF(DAY, i.create_date, SYSDATETIME())), 0), 1) AS create_days + FROM #IndexSanity AS i + GROUP BY i.database_id, i.schema_name, i.object_id + ) + SELECT N'Missing index.' AS Finding , + N'https://www.brentozar.com/go/Indexaphobia' AS URL , + mi.[statement] + + ' Est. Benefit: ' + + CASE WHEN magic_benefit_number >= 922337203685477 THEN '>= 922,337,203,685,477' + ELSE REPLACE(CONVERT(NVARCHAR(256),CAST(CAST( + (magic_benefit_number / CASE WHEN cd.create_days < @DaysUptime THEN cd.create_days ELSE @DaysUptime END) + AS BIGINT) AS MONEY), 1), '.00', '') + END AS [Estimated Benefit], + missing_index_details AS [Missing Index Request] , + index_estimated_impact AS [Estimated Impact], + create_tsql AS [Create TSQL], + sample_query_plan AS [Sample Query Plan] + FROM #MissingIndexes mi + LEFT JOIN create_date AS cd + ON mi.[object_id] = cd.object_id + AND mi.database_id = cd.database_id + AND mi.schema_name = cd.schema_name + WHERE mi.[object_id] = @ObjectID + AND (@ShowAllMissingIndexRequests=1 + /* Minimum benefit threshold = 100k/day of uptime OR since table creation date, whichever is lower*/ + OR (magic_benefit_number / CASE WHEN cd.create_days < @DaysUptime THEN cd.create_days ELSE @DaysUptime END) >= 100000) + ORDER BY magic_benefit_number DESC + OPTION ( RECOMPILE ); + END; + ELSE + SELECT 'No missing indexes.' AS finding; + + SELECT + column_name AS [Column Name], + (SELECT COUNT(*) + FROM #IndexColumns c2 + WHERE c2.column_name=c.column_name + AND c2.key_ordinal IS NOT NULL) + + CASE WHEN c.index_id = 1 AND c.key_ordinal IS NOT NULL THEN + -1+ (SELECT COUNT(DISTINCT index_id) + FROM #IndexColumns c3 + WHERE c3.index_id NOT IN (0,1)) + ELSE 0 END + AS [Found In], + system_type_name + + CASE max_length WHEN -1 THEN N' (max)' ELSE + CASE + WHEN system_type_name IN (N'char',N'varchar',N'binary',N'varbinary') THEN N' (' + CAST(max_length AS NVARCHAR(20)) + N')' + WHEN system_type_name IN (N'nchar',N'nvarchar') THEN N' (' + CAST(max_length/2 AS NVARCHAR(20)) + N')' + ELSE '' + END + END + AS [Type], + CASE is_computed WHEN 1 THEN 'yes' ELSE '' END AS [Computed?], + max_length AS [Length (max bytes)], + [precision] AS [Prec], + [scale] AS [Scale], + CASE is_nullable WHEN 1 THEN 'yes' ELSE '' END AS [Nullable?], + CASE is_identity WHEN 1 THEN 'yes' ELSE '' END AS [Identity?], + CASE is_replicated WHEN 1 THEN 'yes' ELSE '' END AS [Replicated?], + CASE is_sparse WHEN 1 THEN 'yes' ELSE '' END AS [Sparse?], + CASE is_filestream WHEN 1 THEN 'yes' ELSE '' END AS [Filestream?], + collation_name AS [Collation] + FROM #IndexColumns AS c + WHERE index_id IN (0,1); + + IF (SELECT TOP 1 parent_object_id FROM #ForeignKeys) IS NOT NULL + BEGIN + SELECT [database_name] + N':' + parent_object_name + N': ' + foreign_key_name AS [Foreign Key], + parent_fk_columns AS [Foreign Key Columns], + referenced_object_name AS [Referenced Table], + referenced_fk_columns AS [Referenced Table Columns], + is_disabled AS [Is Disabled?], + is_not_trusted AS [Not Trusted?], + is_not_for_replication [Not for Replication?], + [update_referential_action_desc] AS [Cascading Updates?], + [delete_referential_action_desc] AS [Cascading Deletes?] + FROM #ForeignKeys + ORDER BY [Foreign Key] + OPTION ( RECOMPILE ); + END; + ELSE + SELECT 'No foreign keys.' AS finding; + + /* Show histograms for all stats on this table. More info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/1900 */ + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'dm_db_stats_histogram') + BEGIN + SET @dsql = N'USE ' + QUOTENAME(@DatabaseName) + N'; + SELECT s.name AS [Stat Name], c.name AS [Leading Column Name], hist.step_number AS [Step Number], + hist.range_high_key AS [Range High Key], hist.range_rows AS [Range Rows], + hist.equal_rows AS [Equal Rows], hist.distinct_range_rows AS [Distinct Range Rows], hist.average_range_rows AS [Average Range Rows], + s.auto_created AS [Auto-Created], s.user_created AS [User-Created], + props.last_updated AS [Last Updated], props.modification_counter AS [Modification Counter], props.rows AS [Table Rows], + props.rows_sampled AS [Rows Sampled], s.stats_id AS [StatsID] + FROM sys.stats AS s + INNER JOIN sys.stats_columns sc ON s.object_id = sc.object_id AND s.stats_id = sc.stats_id AND sc.stats_column_id = 1 + INNER JOIN sys.columns c ON sc.object_id = c.object_id AND sc.column_id = c.column_id + CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) AS props + CROSS APPLY sys.dm_db_stats_histogram(s.[object_id], s.stats_id) AS hist + WHERE s.object_id = @ObjectID + ORDER BY s.auto_created, s.user_created, s.name, hist.step_number;'; + EXEC sp_executesql @dsql, N'@ObjectID INT', @ObjectID; + END + END + + /* Visualize columnstore index contents. More info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2584 */ + IF 2 = (SELECT SUM(1) FROM sys.all_objects WHERE name IN ('column_store_row_groups','column_store_segments')) + BEGIN + RAISERROR(N'Visualizing columnstore index contents.', 0,1) WITH NOWAIT; + + SET @dsql = N'USE ' + QUOTENAME(@DatabaseName) + N'; + IF EXISTS(SELECT * FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_row_groups WHERE object_id = @ObjectID) + BEGIN + SELECT @ColumnList = N'''', @ColumnListWithApostrophes = N''''; + WITH DistinctColumns AS ( + SELECT DISTINCT QUOTENAME(c.name) AS column_name, QUOTENAME(c.name,'''''''') AS ColumnNameWithApostrophes, c.column_id + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.partitions p + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns c ON p.object_id = c.object_id + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns ic on ic.column_id = c.column_id and ic.object_id = c.object_id AND ic.index_id = p.index_id + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.types t ON c.system_type_id = t.system_type_id AND c.user_type_id = t.user_type_id + WHERE p.object_id = @ObjectID + AND EXISTS (SELECT * FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_segments seg WHERE p.partition_id = seg.partition_id AND seg.column_id = ic.index_column_id) + AND p.data_compression IN (3,4) + ) + SELECT @ColumnList = @ColumnList + column_name + N'', '', + @ColumnListWithApostrophes = @ColumnListWithApostrophes + ColumnNameWithApostrophes + N'', '' + FROM DistinctColumns + ORDER BY column_id; + SELECT @PartitionCount = COUNT(1) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.partitions WHERE object_id = @ObjectID AND data_compression IN (3,4); + END'; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + + EXEC sp_executesql @dsql, N'@ObjectID INT, @ColumnList NVARCHAR(MAX) OUTPUT, @ColumnListWithApostrophes NVARCHAR(MAX) OUTPUT, @PartitionCount INT OUTPUT', @ObjectID, @ColumnList OUTPUT, @ColumnListWithApostrophes OUTPUT, @PartitionCount OUTPUT; + + IF @PartitionCount < 2 + SET @ShowPartitionRanges = 0; + + IF @Debug = 1 + SELECT @ColumnList AS ColumnstoreColumnList, @ColumnListWithApostrophes AS ColumnstoreColumnListWithApostrophes, @PartitionCount AS PartitionCount, @ShowPartitionRanges AS ShowPartitionRanges; + + IF @ColumnList <> '' + BEGIN + /* Remove the trailing comma */ + SET @ColumnList = LEFT(@ColumnList, LEN(@ColumnList) - 1); + SET @ColumnListWithApostrophes = LEFT(@ColumnListWithApostrophes, LEN(@ColumnListWithApostrophes) - 1); + + SET @dsql = N'USE ' + QUOTENAME(@DatabaseName) + N'; + SELECT partition_number, ' + + CASE WHEN @ShowPartitionRanges = 1 THEN N' COALESCE(range_start_op + '' '' + range_start + '' '', '''') + COALESCE(range_end_op + '' '' + range_end, '''') AS partition_range, ' ELSE N' ' END + + N' row_group_id, total_rows, deleted_rows, ' + + @ColumnList + + CASE WHEN @ShowPartitionRanges = 1 THEN N' , + state_desc, trim_reason_desc, transition_to_compressed_state_desc, has_vertipaq_optimization + FROM ( + SELECT column_name, partition_number, row_group_id, total_rows, deleted_rows, details, + range_start_op, + CASE + WHEN format_type IS NULL THEN CAST(range_start_value AS NVARCHAR(4000)) + ELSE CONVERT(NVARCHAR(4000), range_start_value, format_type) END range_start, + range_end_op, + CASE + WHEN format_type IS NULL THEN CAST(range_end_value AS NVARCHAR(4000)) + ELSE CONVERT(NVARCHAR(4000), range_end_value, format_type) END range_end' ELSE N' ' END + N', + state_desc, trim_reason_desc, transition_to_compressed_state_desc, has_vertipaq_optimization + FROM ( + SELECT c.name AS column_name, p.partition_number, rg.row_group_id, rg.total_rows, rg.deleted_rows, + phys.state_desc, phys.trim_reason_desc, phys.transition_to_compressed_state_desc, phys.has_vertipaq_optimization, + details = CAST(seg.min_data_id AS VARCHAR(20)) + '' to '' + CAST(seg.max_data_id AS VARCHAR(20)) + '', '' + CAST(CAST(((COALESCE(d.on_disk_size,0) + COALESCE(seg.on_disk_size,0)) / 1024.0 / 1024) AS DECIMAL(18,0)) AS VARCHAR(20)) + '' MB''' + + CASE WHEN @ShowPartitionRanges = 1 THEN N', + CASE + WHEN pp.system_type_id IN (40, 41, 42, 43, 58, 61) THEN 126 + WHEN pp.system_type_id IN (59, 62) THEN 3 + WHEN pp.system_type_id IN (60, 122) THEN 2 + ELSE NULL END format_type, + CASE WHEN pf.boundary_value_on_right = 0 THEN ''>'' ELSE ''>='' END range_start_op, + prvs.value range_start_value, + CASE WHEN pf.boundary_value_on_right = 0 THEN ''<='' ELSE ''<'' END range_end_op, + prve.value range_end_value ' ELSE N' ' END + N' + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_row_groups rg + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.columns c ON rg.object_id = c.object_id + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partitions p ON rg.object_id = p.object_id AND rg.partition_number = p.partition_number + INNER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.index_columns ic on ic.column_id = c.column_id AND ic.object_id = c.object_id AND ic.index_id = p.index_id + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.dm_db_column_store_row_group_physical_stats phys ON rg.row_group_id = phys.row_group_id AND rg.object_id = phys.object_id AND rg.partition_number = phys.partition_number AND p.index_id = phys.index_id ' + CASE WHEN @ShowPartitionRanges = 1 THEN N' + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.indexes i ON i.object_id = rg.object_id AND i.index_id = rg.index_id + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_schemes ps ON ps.data_space_id = i.data_space_id + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_functions pf ON pf.function_id = ps.function_id + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_parameters pp ON pp.function_id = pf.function_id + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_range_values prvs ON prvs.function_id = pf.function_id AND prvs.boundary_id = p.partition_number - 1 + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.partition_range_values prve ON prve.function_id = pf.function_id AND prve.boundary_id = p.partition_number ' ELSE N' ' END + + N' LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_segments seg ON p.partition_id = seg.partition_id AND ic.index_column_id = seg.column_id AND rg.row_group_id = seg.segment_id + LEFT OUTER JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_dictionaries d ON p.hobt_id = d.hobt_id AND c.column_id = d.column_id AND seg.secondary_dictionary_id = d.dictionary_id + WHERE rg.object_id = @ObjectID + AND rg.state IN (1, 2, 3) + AND c.name IN ( ' + @ColumnListWithApostrophes + N')' + + CASE WHEN @ShowPartitionRanges = 1 THEN N' + ) AS y ' ELSE N' ' END + N' + ) AS x + PIVOT (MAX(details) FOR column_name IN ( ' + @ColumnList + N')) AS pivot1 + ORDER BY partition_number, row_group_id;'; + + IF @Debug = 1 + BEGIN + PRINT SUBSTRING(@dsql, 0, 4000); + PRINT SUBSTRING(@dsql, 4000, 8000); + PRINT SUBSTRING(@dsql, 8000, 12000); + PRINT SUBSTRING(@dsql, 12000, 16000); + PRINT SUBSTRING(@dsql, 16000, 20000); + PRINT SUBSTRING(@dsql, 20000, 24000); + PRINT SUBSTRING(@dsql, 24000, 28000); + PRINT SUBSTRING(@dsql, 28000, 32000); + PRINT SUBSTRING(@dsql, 32000, 36000); + PRINT SUBSTRING(@dsql, 36000, 40000); + END; + + IF @dsql IS NULL + RAISERROR('@dsql is null',16,1); + ELSE + EXEC sp_executesql @dsql, N'@ObjectID INT', @ObjectID; + END + ELSE /* No columns were found for this object */ + BEGIN + SELECT N'No compressed columnstore rowgroups were found for this object.' AS Columnstore_Visualization + UNION ALL + SELECT N'SELECT * FROM ' + QUOTENAME(@DatabaseName) + N'.sys.column_store_row_groups WHERE object_id = ' + CAST(@ObjectID AS NVARCHAR(100)); + END + RAISERROR(N'Done visualizing columnstore index contents.', 0,1) WITH NOWAIT; + END + + IF @ShowColumnstoreOnly = 1 + RETURN; + +END; /* IF @TableName IS NOT NULL */ + + + + + + + + +ELSE /* @TableName IS NULL, so we operate in normal mode 0/1/2/3/4 */ +BEGIN + +/* Validate and check table output params */ + + + /* Checks if @OutputServerName is populated with a valid linked server, and that the database name specified is valid */ + DECLARE @ValidOutputServer BIT; + DECLARE @ValidOutputLocation BIT; + DECLARE @LinkedServerDBCheck NVARCHAR(2000); + DECLARE @ValidLinkedServerDB INT; + DECLARE @tmpdbchk TABLE (cnt INT); + + IF @OutputServerName IS NOT NULL + BEGIN + IF (SUBSTRING(@OutputTableName, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of temporary tables, outputting to a linked server requires a permanent table.', 16, 0); + END; + ELSE IF EXISTS (SELECT server_id FROM sys.servers WHERE QUOTENAME([name]) = @OutputServerName) + BEGIN + SET @LinkedServerDBCheck = 'SELECT 1 WHERE EXISTS (SELECT * FROM '+@OutputServerName+'.master.sys.databases WHERE QUOTENAME([name]) = '''+@OutputDatabaseName+''')'; + INSERT INTO @tmpdbchk EXEC sys.sp_executesql @LinkedServerDBCheck; + SET @ValidLinkedServerDB = (SELECT COUNT(*) FROM @tmpdbchk); + IF (@ValidLinkedServerDB > 0) + BEGIN + SET @ValidOutputServer = 1; + SET @ValidOutputLocation = 1; + END; + ELSE + RAISERROR('The specified database was not found on the output server', 16, 0); + END; + ELSE + BEGIN + RAISERROR('The specified output server was not found', 16, 0); + END; + END; + ELSE + BEGIN + IF (SUBSTRING(@OutputTableName, 2, 2) = '##') + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''[tempdb].[dbo].@@@OutputTableName@@@'') IS NOT NULL) DROP TABLE @@@OutputTableName@@@'; + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + EXEC(@StringToExecute); + + SET @OutputServerName = QUOTENAME(CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))); + SET @OutputDatabaseName = '[tempdb]'; + SET @OutputSchemaName = '[dbo]'; + SET @ValidOutputLocation = 1; + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); + END; + ELSE IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + SET @ValidOutputLocation = 1; + SET @OutputServerName = QUOTENAME(CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))); + END; + ELSE IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND NOT EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + RAISERROR('The specified output database was not found on this server', 16, 0); + END; + ELSE + BEGIN + SET @ValidOutputLocation = 0; + END; + END; + + IF (@ValidOutputLocation = 0 AND @OutputType = 'NONE') + BEGIN + RAISERROR('Invalid output location and no output asked',12,1); + RETURN; + END; + + /* @OutputTableName lets us export the results to a permanent table */ + DECLARE @RunID UNIQUEIDENTIFIER; + SET @RunID = NEWID(); + + DECLARE @TableExists BIT; + DECLARE @SchemaExists BIT; + + DECLARE @TableExistsSql NVARCHAR(MAX); + + IF (@ValidOutputLocation = 1 AND COALESCE(@OutputServerName, @OutputDatabaseName, @OutputSchemaName, @OutputTableName) IS NOT NULL) + BEGIN + SET @StringToExecute = + N'SET @SchemaExists = 0; + SET @TableExists = 0; + IF EXISTS(SELECT * FROM @@@OutputServerName@@@.@@@OutputDatabaseName@@@.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''@@@OutputSchemaName@@@'') + SET @SchemaExists = 1 + IF EXISTS (SELECT * FROM @@@OutputServerName@@@.@@@OutputDatabaseName@@@.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''@@@OutputSchemaName@@@'' AND QUOTENAME(TABLE_NAME) = ''@@@OutputTableName@@@'') + BEGIN + SET @TableExists = 1 + IF NOT EXISTS(SELECT * FROM @@@OutputServerName@@@.@@@OutputDatabaseName@@@.INFORMATION_SCHEMA.COLUMNS WHERE QUOTENAME(TABLE_SCHEMA) = ''@@@OutputSchemaName@@@'' + AND QUOTENAME(TABLE_NAME) = ''@@@OutputTableName@@@'' AND QUOTENAME(COLUMN_NAME) = ''[total_forwarded_fetch_count]'') + EXEC @@@OutputServerName@@@.@@@OutputDatabaseName@@@.dbo.sp_executesql N''ALTER TABLE @@@OutputSchemaName@@@.@@@OutputTableName@@@ ADD [total_forwarded_fetch_count] BIGINT'' + END'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputServerName@@@', @OutputServerName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + + EXEC sp_executesql @StringToExecute, N'@TableExists BIT OUTPUT, @SchemaExists BIT OUTPUT', @TableExists OUTPUT, @SchemaExists OUTPUT; + + + SET @TableExistsSql = + N'IF EXISTS(SELECT * FROM @@@OutputServerName@@@.@@@OutputDatabaseName@@@.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''@@@OutputSchemaName@@@'') + AND NOT EXISTS (SELECT * FROM @@@OutputServerName@@@.@@@OutputDatabaseName@@@.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''@@@OutputSchemaName@@@'' AND QUOTENAME(TABLE_NAME) = ''@@@OutputTableName@@@'') + SET @TableExists = 0 + ELSE + SET @TableExists = 1'; + + SET @TableExistsSql = REPLACE(@TableExistsSql, '@@@OutputServerName@@@', @OutputServerName); + SET @TableExistsSql = REPLACE(@TableExistsSql, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @TableExistsSql = REPLACE(@TableExistsSql, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @TableExistsSql = REPLACE(@TableExistsSql, '@@@OutputTableName@@@', @OutputTableName); + + END + + + + + IF @Mode IN (0, 4) /* DIAGNOSE */ + BEGIN; + IF @Mode IN (0, 4) /* DIAGNOSE priorities 1-100 */ + BEGIN; + RAISERROR(N'@Mode=0 or 4, running rules for priorities 1-100.', 0,1) WITH NOWAIT; + + ---------------------------------------- + --Multiple Index Personalities: Check_id 0-10 + ---------------------------------------- + RAISERROR('check_id 1: Duplicate keys', 0,1) WITH NOWAIT; + WITH duplicate_indexes + AS ( SELECT [object_id], key_column_names, database_id, [schema_name] + FROM #IndexSanity AS ip + WHERE index_type IN (1,2) /* Clustered, NC only*/ + AND is_hypothetical = 0 + AND is_disabled = 0 + AND is_primary_key = 0 + AND EXISTS ( + SELECT 1/0 + FROM #IndexSanitySize ips + WHERE ip.index_sanity_id = ips.index_sanity_id + AND ip.database_id = ips.database_id + AND ip.schema_name = ips.schema_name + AND ips.total_reserved_MB >= CASE + WHEN (@GetAllDatabases = 1 OR @Mode = 0) + THEN @ThresholdMB + ELSE ips.total_reserved_MB + END + ) + GROUP BY [object_id], key_column_names, database_id, [schema_name] + HAVING COUNT(*) > 1) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 1 AS check_id, + ip.index_sanity_id, + 20 AS Priority, + 'Multiple Index Personalities' AS findings_group, + 'Duplicate keys' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/duplicateindex' AS URL, + N'Index Name: ' + ip.index_name + N' Table Name: ' + ip.db_schema_object_name AS details, + ip.index_definition, + ip.secret_columns, + ip.index_usage_summary, + ips.index_size_summary + FROM duplicate_indexes di + JOIN #IndexSanity ip ON di.[object_id] = ip.[object_id] + AND ip.database_id = di.database_id + AND ip.[schema_name] = di.[schema_name] + AND di.key_column_names = ip.key_column_names + JOIN #IndexSanitySize ips ON ip.index_sanity_id = ips.index_sanity_id + AND ip.database_id = ips.database_id + AND ip.schema_name = ips.schema_name + /* WHERE clause limits to only @ThresholdMB or larger duplicate indexes when getting all databases or using PainRelief mode */ + WHERE ips.total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0) THEN @ThresholdMB ELSE ips.total_reserved_MB END + AND ip.is_primary_key = 0 + ORDER BY ips.total_rows DESC, ip.[schema_name], ip.[object_name], ip.key_column_names_with_sort_order + OPTION ( RECOMPILE ); + + RAISERROR('check_id 2: Keys w/ identical leading columns.', 0,1) WITH NOWAIT; + WITH borderline_duplicate_indexes + AS ( SELECT DISTINCT database_id, [object_id], first_key_column_name, key_column_names, + COUNT([object_id]) OVER ( PARTITION BY database_id, [object_id], first_key_column_name ) AS number_dupes + FROM #IndexSanity + WHERE index_type IN (1,2) /* Clustered, NC only*/ + AND is_hypothetical=0 + AND is_disabled=0 + AND is_primary_key = 0) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 2 AS check_id, + ip.index_sanity_id, + 30 AS Priority, + 'Multiple Index Personalities' AS findings_group, + 'Borderline duplicate keys' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/duplicateindex' AS URL, + ip.db_schema_object_indexid AS details, + ip.index_definition, + ip.secret_columns, + ip.index_usage_summary, + ips.index_size_summary + FROM #IndexSanity AS ip + JOIN #IndexSanitySize ips ON ip.index_sanity_id = ips.index_sanity_id + WHERE EXISTS ( + SELECT di.[object_id] + FROM borderline_duplicate_indexes AS di + WHERE di.[object_id] = ip.[object_id] AND + di.database_id = ip.database_id AND + di.first_key_column_name = ip.first_key_column_name AND + di.key_column_names <> ip.key_column_names AND + di.number_dupes > 1 + ) + AND ip.is_primary_key = 0 + ORDER BY ips.total_rows DESC, ip.[schema_name], ip.[object_name], ip.key_column_names, ip.include_column_names + OPTION ( RECOMPILE ); + + ---------------------------------------- + --Aggressive Indexes: Check_id 10-19 + ---------------------------------------- + + RAISERROR(N'check_id 11: Total lock wait time > 5 minutes (row + page)', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 11 AS check_id, + i.index_sanity_id, + 70 AS Priority, + N'Aggressive ' + + CASE COALESCE((SELECT SUM(1) + FROM #IndexSanity iMe + INNER JOIN #IndexSanity iOthers + ON iMe.database_id = iOthers.database_id + AND iMe.object_id = iOthers.object_id + AND iOthers.index_id > 1 + WHERE i.index_sanity_id = iMe.index_sanity_id + AND iOthers.is_hypothetical = 0 + AND iOthers.is_disabled = 0 + ), 0) + WHEN 0 THEN N'Under-Indexing' + WHEN 1 THEN N'Under-Indexing' + WHEN 2 THEN N'Under-Indexing' + WHEN 3 THEN N'Under-Indexing' + WHEN 4 THEN N'Indexes' + WHEN 5 THEN N'Indexes' + WHEN 6 THEN N'Indexes' + WHEN 7 THEN N'Indexes' + WHEN 8 THEN N'Indexes' + WHEN 9 THEN N'Indexes' + ELSE N'Over-Indexing' + END AS findings_group, + N'Total lock wait time > 5 minutes (row + page)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AggressiveIndexes' AS URL, + (i.db_schema_object_indexid + N': ' + + sz.index_lock_wait_summary + N' NC indexes on table: ') COLLATE DATABASE_DEFAULT + + CAST(COALESCE((SELECT SUM(1) + FROM #IndexSanity iMe + INNER JOIN #IndexSanity iOthers + ON iMe.database_id = iOthers.database_id + AND iMe.object_id = iOthers.object_id + AND iOthers.index_id > 1 + WHERE i.index_sanity_id = iMe.index_sanity_id + AND iOthers.is_hypothetical = 0 + AND iOthers.is_disabled = 0 + ), 0) + AS NVARCHAR(30)) AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE (total_row_lock_wait_in_ms + total_page_lock_wait_in_ms) > 300000 + GROUP BY i.index_sanity_id, [database_name], i.db_schema_object_indexid, sz.index_lock_wait_summary, i.index_definition, i.secret_columns, i.index_usage_summary, sz.index_size_summary, sz.index_sanity_id + ORDER BY SUM(total_row_lock_wait_in_ms + total_page_lock_wait_in_ms) DESC, 4, [database_name], 8 + OPTION ( RECOMPILE ); + + + + ---------------------------------------- + --Index Hoarder: Check_id 20-29 + ---------------------------------------- + RAISERROR(N'check_id 20: >= 10 NC indexes on any given table. Yes, 10 is an arbitrary number.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 20 AS check_id, + MAX(i.index_sanity_id) AS index_sanity_id, + 10 AS Priority, + 'Index Hoarder' AS findings_group, + 'Many NC Indexes on a Single Table' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + CAST (COUNT(*) AS NVARCHAR(30)) + ' NC indexes on ' + i.db_schema_object_name AS details, + i.db_schema_object_name + ' (' + CAST (COUNT(*) AS NVARCHAR(30)) + ' indexes)' AS index_definition, + '' AS secret_columns, + REPLACE(CONVERT(NVARCHAR(30),CAST(SUM(total_reads) AS MONEY), 1), N'.00', N'') + N' reads (ALL); ' + + REPLACE(CONVERT(NVARCHAR(30),CAST(SUM(user_updates) AS MONEY), 1), N'.00', N'') + N' writes (ALL); ', + REPLACE(CONVERT(NVARCHAR(30),CAST(MAX(total_rows) AS MONEY), 1), N'.00', N'') + N' rows (MAX)' + + CASE WHEN SUM(total_reserved_MB) > 1024 THEN + N'; ' + CAST(CAST(SUM(total_reserved_MB)/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'GB (ALL)' + WHEN SUM(total_reserved_MB) > 0 THEN + N'; ' + CAST(CAST(SUM(total_reserved_MB) AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'MB (ALL)' + ELSE '' + END AS index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + WHERE index_id NOT IN ( 0, 1 ) + GROUP BY db_schema_object_name, [i].[database_name] + HAVING COUNT(*) >= 10 + ORDER BY i.db_schema_object_name DESC + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 22: NC indexes with 0 reads. (Borderline) and >= 10,000 writes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 22 AS check_id, + i.index_sanity_id, + 10 AS Priority, + N'Index Hoarder' AS findings_group, + N'Unused NC Index with High Writes' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + N'Reads: 0,' + + N' Writes: ' + + REPLACE(CONVERT(NVARCHAR(30), CAST((i.user_updates) AS MONEY), 1), N'.00', N'') + + N' on: ' + + i.db_schema_object_indexid + AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.total_reads=0 + AND i.user_updates >= 10000 + AND i.index_id NOT IN (0,1) /*NCs only*/ + AND i.is_unique = 0 + AND sz.total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0) THEN @ThresholdMB ELSE sz.total_reserved_MB END + AND @Filter <> 1 /* 1 = "ignore unused */ + ORDER BY i.db_schema_object_indexid + OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 34: Filtered index definition columns not in index definition', 0,1) WITH NOWAIT; + + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 34 AS check_id, + i.index_sanity_id, + 80 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Filter Columns Not In Index Definition' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexFeatures' AS URL, + N'The index ' + + QUOTENAME(i.index_name) + + N' on [' + + i.db_schema_object_name + + N'] has a filter on [' + + i.filter_definition + + N'] but is missing [' + + LTRIM(i.filter_columns_not_in_index) + + N'] from the index definition.' + AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.filter_columns_not_in_index IS NOT NULL + ORDER BY i.db_schema_object_indexid + OPTION ( RECOMPILE ); + + ---------------------------------------- + --Self Loathing Indexes : Check_id 40-49 + ---------------------------------------- + + RAISERROR(N'check_id 40: Fillfactor in nonclustered 80 percent or less', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 40 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Low Fill Factor on Nonclustered Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + CAST(fill_factor AS NVARCHAR(10)) + N'% fill factor on ' + db_schema_object_indexid + N'. '+ + CASE WHEN (last_user_update IS NULL OR user_updates < 1) + THEN N'No writes have been made.' + ELSE + N'Last write was ' + CONVERT(NVARCHAR(16),last_user_update,121) + N' and ' + + CAST(user_updates AS NVARCHAR(25)) + N' updates have been made.' + END + AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE index_id > 1 + AND fill_factor BETWEEN 1 AND 80 OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 40: Fillfactor in clustered 80 percent or less', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 40 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Low Fill Factor on Clustered Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + N'Fill factor on ' + db_schema_object_indexid + N' is ' + CAST(fill_factor AS NVARCHAR(10)) + N'%. '+ + CASE WHEN (last_user_update IS NULL OR user_updates < 1) + THEN N'No writes have been made.' + ELSE + N'Last write was ' + CONVERT(NVARCHAR(16),last_user_update,121) + N' and ' + + CAST(user_updates AS NVARCHAR(25)) + N' updates have been made.' + END + AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE index_id = 1 + AND fill_factor BETWEEN 1 AND 80 OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 43: Heaps with forwarded records', 0,1) WITH NOWAIT; + WITH heaps_cte + AS ( SELECT [object_id], + [database_id], + [schema_name], + SUM(forwarded_fetch_count) AS forwarded_fetch_count, + SUM(leaf_delete_count) AS leaf_delete_count + FROM #IndexPartitionSanity + GROUP BY [object_id], + [database_id], + [schema_name] + HAVING SUM(forwarded_fetch_count) > 0) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 43 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Heaps with Forwarded Fetches' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + CASE WHEN h.forwarded_fetch_count >= 922337203685477 THEN '>= 922,337,203,685,477' + WHEN @DaysUptime < 1 THEN CAST(h.forwarded_fetch_count AS NVARCHAR(256)) + N' forwarded fetches against heap: ' + db_schema_object_indexid + ELSE REPLACE(CONVERT(NVARCHAR(256),CAST(CAST( + (h.forwarded_fetch_count /*/@DaysUptime */) + AS BIGINT) AS MONEY), 1), '.00', '') + END + N' forwarded fetches per day against heap: ' + + db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + JOIN heaps_cte h ON i.[object_id] = h.[object_id] + AND i.[database_id] = h.[database_id] + AND i.[schema_name] = h.[schema_name] + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_id = 0 + AND h.forwarded_fetch_count / @DaysUptime > 1000 + AND sz.total_reserved_MB >= CASE WHEN NOT (@GetAllDatabases = 1 OR @Mode = 4) THEN @ThresholdMB ELSE sz.total_reserved_MB END + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 44: Large Heaps with reads or writes.', 0,1) WITH NOWAIT; + WITH heaps_cte + AS ( SELECT [object_id], + [database_id], + [schema_name], + SUM(forwarded_fetch_count) AS forwarded_fetch_count, + SUM(leaf_delete_count) AS leaf_delete_count + FROM #IndexPartitionSanity + GROUP BY [object_id], + [database_id], + [schema_name] + HAVING SUM(forwarded_fetch_count) > 0 + OR SUM(leaf_delete_count) > 0) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 44 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Large Active Heap' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + N'Should this table be a heap? ' + db_schema_object_indexid AS details, + i.index_definition, + 'N/A' AS secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + LEFT JOIN heaps_cte h ON i.[object_id] = h.[object_id] + AND i.[database_id] = h.[database_id] + AND i.[schema_name] = h.[schema_name] + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_id = 0 + AND (i.total_reads > 0 OR i.user_updates > 0) + AND sz.total_rows >= 100000 + AND h.[object_id] IS NULL /*don't duplicate the prior check.*/ + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 45: Medium Heaps with reads or writes.', 0,1) WITH NOWAIT; + WITH heaps_cte + AS ( SELECT [object_id], + [database_id], + [schema_name], + SUM(forwarded_fetch_count) AS forwarded_fetch_count, + SUM(leaf_delete_count) AS leaf_delete_count + FROM #IndexPartitionSanity + GROUP BY [object_id], + [database_id], + [schema_name] + HAVING SUM(forwarded_fetch_count) > 0 + OR SUM(leaf_delete_count) > 0) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 45 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Medium Active heap' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + N'Should this table be a heap? ' + db_schema_object_indexid AS details, + i.index_definition, + 'N/A' AS secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + LEFT JOIN heaps_cte h ON i.[object_id] = h.[object_id] + AND i.[database_id] = h.[database_id] + AND i.[schema_name] = h.[schema_name] + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_id = 0 + AND + (i.total_reads > 0 OR i.user_updates > 0) + AND sz.total_rows >= 10000 AND sz.total_rows < 100000 + AND h.[object_id] IS NULL /*don't duplicate the prior check.*/ + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 46: Small Heaps with reads or writes.', 0,1) WITH NOWAIT; + WITH heaps_cte + AS ( SELECT [object_id], + [database_id], + [schema_name], + SUM(forwarded_fetch_count) AS forwarded_fetch_count, + SUM(leaf_delete_count) AS leaf_delete_count + FROM #IndexPartitionSanity + GROUP BY [object_id], + [database_id], + [schema_name] + HAVING SUM(forwarded_fetch_count) > 0 + OR SUM(leaf_delete_count) > 0) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 46 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Small Active heap' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + N'Should this table be a heap? ' + db_schema_object_indexid AS details, + i.index_definition, + 'N/A' AS secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + LEFT JOIN heaps_cte h ON i.[object_id] = h.[object_id] + AND i.[database_id] = h.[database_id] + AND i.[schema_name] = h.[schema_name] + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_id = 0 + AND + (i.total_reads > 0 OR i.user_updates > 0) + AND sz.total_rows < 10000 + AND h.[object_id] IS NULL /*don't duplicate the prior check.*/ + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 47: Heap with a Nonclustered Primary Key', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 47 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Heap with a Nonclustered Primary Key' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + db_schema_object_indexid + N' is a HEAP with a Nonclustered Primary Key' AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_type = 2 AND i.is_primary_key = 1 + AND EXISTS + ( + SELECT 1/0 + FROM #IndexSanity AS isa + WHERE i.database_id = isa.database_id + AND i.object_id = isa.object_id + AND isa.index_id = 0 + ) + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 48: Nonclustered indexes with a bad read to write ratio', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 48 AS check_id, + i.index_sanity_id, + 100 AS Priority, + N'Index Hoarder' AS findings_group, + N'NC index with High Writes:Reads' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + N'Reads: ' + + REPLACE(CONVERT(NVARCHAR(30), CAST((i.total_reads) AS MONEY), 1), N'.00', N'') + + N' Writes: ' + + REPLACE(CONVERT(NVARCHAR(30), CAST((i.user_updates) AS MONEY), 1), N'.00', N'') + + N' on: ' + + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.total_reads > 0 /*Not totally unused*/ + AND i.user_updates >= 10000 /*Decent write activity*/ + AND i.total_reads < 10000 + AND ((i.total_reads * 10) < i.user_updates) /*10x more writes than reads*/ + AND i.index_id NOT IN (0,1) /*NCs only*/ + AND i.is_unique = 0 + AND sz.total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0) THEN @ThresholdMB ELSE sz.total_reserved_MB END + ORDER BY i.db_schema_object_indexid + OPTION ( RECOMPILE ); + + ---------------------------------------- + --Indexaphobia + --Missing indexes with value >= 5 million: : Check_id 50-59 + ---------------------------------------- + RAISERROR(N'check_id 50: Indexaphobia.', 0,1) WITH NOWAIT; + WITH index_size_cte + AS ( SELECT i.database_id, + i.schema_name, + i.[object_id], + MAX(i.index_sanity_id) AS index_sanity_id, + ISNULL(NULLIF(MAX(DATEDIFF(DAY, i.create_date, SYSDATETIME())), 0), 1) AS create_days, + ISNULL ( + CAST(SUM(CASE WHEN index_id NOT IN (0,1) THEN 1 ELSE 0 END) + AS NVARCHAR(30))+ N' NC indexes exist (' + + CASE WHEN SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) > 1024 + THEN CAST(CAST(SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END )/1024. + + AS NUMERIC(29,1)) AS NVARCHAR(30)) + N'GB); ' + ELSE CAST(SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + AS NVARCHAR(30)) + N'MB); ' + END + + CASE WHEN MAX(sz.[total_rows]) >= 922337203685477 THEN '>= 922,337,203,685,477' + ELSE REPLACE(CONVERT(NVARCHAR(30),CAST(MAX(sz.[total_rows]) AS MONEY), 1), '.00', '') + END + + + N' Estimated Rows;' + ,N'') AS index_size_summary + FROM #IndexSanity AS i + LEFT JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id AND i.database_id = sz.database_id + WHERE i.is_hypothetical = 0 + AND i.is_disabled = 0 + GROUP BY i.database_id, i.schema_name, i.[object_id]) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + index_usage_summary, index_size_summary, create_tsql, more_info, sample_query_plan ) + + SELECT check_id, t.index_sanity_id, t.check_id, t.findings_group, t.finding, t.[Database Name], t.URL, t.details, t.[definition], + index_estimated_impact, t.index_size_summary, create_tsql, more_info, sample_query_plan + FROM + ( + SELECT ROW_NUMBER() OVER (ORDER BY magic_benefit_number DESC) AS rownum, + 50 AS check_id, + sz.index_sanity_id, + 40 AS Priority, + N'Indexaphobia' AS findings_group, + N'High Value Missing Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/Indexaphobia' AS URL, + mi.[statement] + + N' Est. benefit per day: ' + + CASE WHEN magic_benefit_number >= 922337203685477 THEN '>= 922,337,203,685,477' + ELSE REPLACE(CONVERT(NVARCHAR(256),CAST(CAST( + (magic_benefit_number/@DaysUptime) + AS BIGINT) AS MONEY), 1), '.00', '') + END AS details, + missing_index_details AS [definition], + index_estimated_impact, + sz.index_size_summary, + mi.create_tsql, + mi.more_info, + magic_benefit_number, + mi.is_low, + mi.sample_query_plan + FROM #MissingIndexes mi + LEFT JOIN index_size_cte sz ON mi.[object_id] = sz.object_id + AND mi.database_id = sz.database_id + AND mi.schema_name = sz.schema_name + /* Minimum benefit threshold = 100k/day of uptime OR since table creation date, whichever is lower*/ + WHERE @ShowAllMissingIndexRequests=1 + OR ( @Mode = 4 AND (magic_benefit_number / CASE WHEN sz.create_days < @DaysUptime THEN sz.create_days ELSE @DaysUptime END) >= 100000 ) + OR (magic_benefit_number / CASE WHEN sz.create_days < @DaysUptime THEN sz.create_days ELSE @DaysUptime END) >= 100000 + ) AS t + WHERE t.rownum <= CASE WHEN (@Mode <> 4) THEN 20 ELSE t.rownum END + ORDER BY magic_benefit_number DESC + OPTION ( RECOMPILE ); + + + + RAISERROR(N'check_id 68: Identity columns within 30 percent of the end of range', 0,1) WITH NOWAIT; + -- Allowed Ranges: + --int -2,147,483,648 to 2,147,483,647 + --smallint -32,768 to 32,768 + --tinyint 0 to 255 + + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 68 AS check_id, + i.index_sanity_id, + 80 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Identity Column Within ' + + CAST (calc1.percent_remaining AS NVARCHAR(256)) + + N' Percent End of Range' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_name + N'.' + QUOTENAME(ic.column_name) + + N' is an identity with type ' + ic.system_type_name + + N', last value of ' + + ISNULL((CONVERT(NVARCHAR(256),CAST(ic.last_value AS DECIMAL(38,0)), 1)),N'NULL') + + N', seed of ' + + ISNULL((CONVERT(NVARCHAR(256),CAST(ic.seed_value AS DECIMAL(38,0)), 1)),N'NULL') + + N', increment of ' + CAST(ic.increment_value AS NVARCHAR(256)) + + N', and range of ' + + CASE ic.system_type_name WHEN 'int' THEN N'+/- 2,147,483,647' + WHEN 'smallint' THEN N'+/- 32,768' + WHEN 'tinyint' THEN N'0 to 255' + ELSE 'unknown' + END + AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexColumns ic ON + i.object_id=ic.object_id + AND i.database_id = ic.database_id + AND i.schema_name = ic.schema_name + AND i.index_id IN (0,1) /* heaps and cx only */ + AND ic.is_identity=1 + AND ic.system_type_name IN ('tinyint', 'smallint', 'int') + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + CROSS APPLY ( + SELECT CAST(CASE WHEN ic.increment_value >= 0 + THEN + CASE ic.system_type_name + WHEN 'int' THEN (2147483647 - (ISNULL(ic.last_value,ic.seed_value) + ic.increment_value)) / 2147483647.*100 + WHEN 'smallint' THEN (32768 - (ISNULL(ic.last_value,ic.seed_value) + ic.increment_value)) / 32768.*100 + WHEN 'tinyint' THEN ( 255 - (ISNULL(ic.last_value,ic.seed_value) + ic.increment_value)) / 255.*100 + ELSE 999 + END + ELSE --ic.increment_value is negative + CASE ic.system_type_name + WHEN 'int' THEN ABS(-2147483647 - (ISNULL(ic.last_value,ic.seed_value) + ic.increment_value)) / 2147483647.*100 + WHEN 'smallint' THEN ABS(-32768 - (ISNULL(ic.last_value,ic.seed_value) + ic.increment_value)) / 32768.*100 + WHEN 'tinyint' THEN ABS( 0 - (ISNULL(ic.last_value,ic.seed_value) + ic.increment_value)) / 255.*100 + ELSE -1 + END + END AS NUMERIC(5,1)) AS percent_remaining + ) AS calc1 + WHERE i.index_id IN (1,0) + AND calc1.percent_remaining <= 30 + OPTION (RECOMPILE); + + + RAISERROR(N'check_id 72: Columnstore indexes with Trace Flag 834', 0,1) WITH NOWAIT; + IF EXISTS (SELECT * FROM #IndexSanity WHERE index_type IN (5,6)) + AND EXISTS (SELECT * FROM #TraceStatus WHERE TraceFlag = 834 AND status = 1) + BEGIN + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 72 AS check_id, + i.index_sanity_id, + 80 AS Priority, + N'Abnormal Psychology' AS findings_group, + 'Columnstore Indexes with Trace Flag 834' AS finding, + [database_name] AS [Database Name], + N'https://support.microsoft.com/en-us/kb/3210239' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_type IN (5,6) + OPTION ( RECOMPILE ); + END; + + ---------------------------------------- + --Statistics Info: Check_id 90-99 + ---------------------------------------- + + RAISERROR(N'check_id 90: Outdated statistics', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 90 AS check_id, + 90 AS Priority, + 'Functioning Statistaholics' AS findings_group, + 'Statistics Not Updated Recently', + s.database_name, + 'https://www.brentozar.com/go/stats' AS URL, + 'Statistics on this table were last updated ' + + CASE WHEN s.last_statistics_update IS NULL THEN N' NEVER ' + ELSE CONVERT(NVARCHAR(20), s.last_statistics_update) + + ' have had ' + CONVERT(NVARCHAR(100), s.modification_counter) + + ' modifications in that time, which is ' + + CONVERT(NVARCHAR(100), s.percent_modifications) + + '% of the table.' + END AS details, + QUOTENAME(database_name) + '.' + QUOTENAME(s.schema_name) + '.' + QUOTENAME(s.table_name) + '.' + QUOTENAME(s.index_name) + '.' + QUOTENAME(s.statistics_name) + '.' + QUOTENAME(s.column_names) AS index_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #Statistics AS s + WHERE s.last_statistics_update <= CONVERT(DATETIME, GETDATE() - 30) + AND s.percent_modifications >= 10. + AND s.rows >= 10000 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 91: Statistics with a low sample rate', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 91 AS check_id, + 90 AS Priority, + 'Functioning Statistaholics' AS findings_group, + 'Low Sampling Rates', + s.database_name, + 'https://www.brentozar.com/go/stats' AS URL, + 'Only ' + CONVERT(NVARCHAR(100), s.percent_sampled) + '% of the rows were sampled during the last statistics update. This may lead to poor cardinality estimates.' AS details, + QUOTENAME(database_name) + '.' + QUOTENAME(s.schema_name) + '.' + QUOTENAME(s.table_name) + '.' + QUOTENAME(s.index_name) + '.' + QUOTENAME(s.statistics_name) + '.' + QUOTENAME(s.column_names) AS index_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #Statistics AS s + WHERE (s.rows BETWEEN 10000 AND 1000000 AND s.percent_sampled < 10) + OR (s.rows > 1000000 AND s.percent_sampled < 1) + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 92: Statistics with NO RECOMPUTE', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 92 AS check_id, + 90 AS Priority, + 'Functioning Statistaholics' AS findings_group, + 'Statistics With NO RECOMPUTE', + s.database_name, + 'https://www.brentozar.com/go/stats' AS URL, + 'The statistic ' + QUOTENAME(s.statistics_name) + ' is set to not recompute. This can be helpful if data is really skewed, but harmful if you expect automatic statistics updates.' AS details, + QUOTENAME(database_name) + '.' + QUOTENAME(s.schema_name) + '.' + QUOTENAME(s.table_name) + '.' + QUOTENAME(s.index_name) + '.' + QUOTENAME(s.statistics_name) + '.' + QUOTENAME(s.column_names) AS index_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #Statistics AS s + WHERE s.no_recompute = 1 + OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 94: Check Constraints That Reference Functions', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 94 AS check_id, + 100 AS Priority, + 'Serial Forcer' AS findings_group, + 'Check Constraint with Scalar UDF' AS finding, + cc.database_name, + 'https://www.brentozar.com/go/computedscalar' AS URL, + 'The check constraint ' + QUOTENAME(cc.constraint_name) + ' on ' + QUOTENAME(cc.schema_name) + '.' + QUOTENAME(cc.table_name) + ' is based on ' + cc.definition + + '. That indicates it may reference a scalar function, or a CLR function with data access, which can cause all queries and maintenance to run serially.' AS details, + cc.column_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #CheckConstraints AS cc + WHERE cc.is_function = 1 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 99: Computed Columns That Reference Functions', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 99 AS check_id, + 100 AS Priority, + 'Serial Forcer' AS findings_group, + 'Computed Column with Scalar UDF' AS finding, + cc.database_name, + 'https://www.brentozar.com/go/serialudf' AS URL, + 'The computed column ' + QUOTENAME(cc.column_name) + ' on ' + QUOTENAME(cc.schema_name) + '.' + QUOTENAME(cc.table_name) + ' is based on ' + cc.definition + + '. That indicates it may reference a scalar function, or a CLR function with data access, which can cause all queries and maintenance to run serially.' AS details, + cc.column_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #ComputedColumns AS cc + WHERE cc.is_function = 1 + OPTION ( RECOMPILE ); + + + + END /* IF @Mode IN (0, 4) DIAGNOSE priorities 1-100 */ + + + + + + + + + IF @Mode = 4 /* DIAGNOSE*/ + BEGIN; + RAISERROR(N'@Mode=4, running rules for priorities 101+.', 0,1) WITH NOWAIT; + + RAISERROR(N'check_id 21: More Than 5 Percent NC Indexes Are Unused', 0,1) WITH NOWAIT; + DECLARE @percent_NC_indexes_unused NUMERIC(29,1); + DECLARE @NC_indexes_unused_reserved_MB NUMERIC(29,1); + + SELECT @percent_NC_indexes_unused = ( 100.00 * SUM(CASE + WHEN total_reads = 0 + THEN 1 + ELSE 0 + END) ) / COUNT(*), + @NC_indexes_unused_reserved_MB = SUM(CASE + WHEN total_reads = 0 + THEN sz.total_reserved_MB + ELSE 0 + END) + FROM #IndexSanity i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE index_id NOT IN ( 0, 1 ) + AND i.is_unique = 0 + /*Skipping tables created in the last week, or modified in past 2 days*/ + AND i.create_date >= DATEADD(dd,-7,GETDATE()) + AND i.modify_date > DATEADD(dd,-2,GETDATE()) + OPTION ( RECOMPILE ); + IF @percent_NC_indexes_unused >= 5 + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 21 AS check_id, + MAX(i.index_sanity_id) AS index_sanity_id, + 150 AS Priority, + N'Index Hoarder' AS findings_group, + N'More Than 5 Percent NC Indexes Are Unused' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + CAST (@percent_NC_indexes_unused AS NVARCHAR(30)) + N' percent NC indexes (' + CAST(COUNT(*) AS NVARCHAR(10)) + N') unused. ' + + N'These take up ' + CAST (@NC_indexes_unused_reserved_MB AS NVARCHAR(30)) + N'MB of space.' AS details, + i.database_name + ' (' + CAST (COUNT(*) AS NVARCHAR(30)) + N' indexes)' AS index_definition, + '' AS secret_columns, + CAST(SUM(total_reads) AS NVARCHAR(256)) + N' reads (ALL); ' + + CAST(SUM([user_updates]) AS NVARCHAR(256)) + N' writes (ALL)' AS index_usage_summary, + + REPLACE(CONVERT(NVARCHAR(30),CAST(MAX([total_rows]) AS MONEY), 1), '.00', '') + N' rows (MAX)' + + CASE WHEN SUM(total_reserved_MB) > 1024 THEN + N'; ' + CAST(CAST(SUM(total_reserved_MB)/1024. AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'GB (ALL)' + WHEN SUM(total_reserved_MB) > 0 THEN + N'; ' + CAST(CAST(SUM(total_reserved_MB) AS NUMERIC(29,1)) AS NVARCHAR(30)) + 'MB (ALL)' + ELSE '' + END AS index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE index_id NOT IN ( 0, 1 ) + AND i.is_unique = 0 + AND total_reads = 0 + /*Skipping tables created in the last week, or modified in past 2 days*/ + AND i.create_date >= DATEADD(dd,-7,GETDATE()) + AND i.modify_date > DATEADD(dd,-2,GETDATE()) + GROUP BY i.database_name + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 23: Indexes with 7 or more columns. (Borderline)', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 23 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Index Hoarder' AS findings_group, + N'Borderline: Wide Indexes (7 or More Columns)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + CAST(count_key_columns + count_included_columns AS NVARCHAR(10)) + ' columns on ' + + i.db_schema_object_indexid AS details, i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE ( count_key_columns + count_included_columns ) >= 7 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 24: Wide clustered indexes (> 3 columns or > 16 bytes).', 0,1) WITH NOWAIT; + WITH count_columns AS ( + SELECT database_id, [object_id], + SUM(CASE max_length WHEN -1 THEN 0 ELSE max_length END) AS sum_max_length + FROM #IndexColumns ic + WHERE index_id IN (1,0) /*Heap or clustered only*/ + AND key_ordinal > 0 + GROUP BY database_id, object_id + ) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 24 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Index Hoarder' AS findings_group, + N'Wide Clustered Index (> 3 columns OR > 16 bytes)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + CAST (i.count_key_columns AS NVARCHAR(10)) + N' columns with potential size of ' + + CAST(cc.sum_max_length AS NVARCHAR(10)) + + N' bytes in clustered index:' + i.db_schema_object_name + + N'. ' + + (SELECT CAST(COUNT(*) AS NVARCHAR(23)) + FROM #IndexSanity i2 + WHERE i2.[object_id]=i.[object_id] + AND i2.database_id = i.database_id + AND i2.index_id <> 1 + AND i2.is_disabled=0 + AND i2.is_hypothetical=0) + + N' NC indexes on the table.' + AS details, + i.index_definition, + secret_columns, + i.index_usage_summary, + ip.index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + JOIN count_columns AS cc ON i.[object_id]=cc.[object_id] + AND i.database_id = cc.database_id + WHERE index_id = 1 /* clustered only */ + AND + (count_key_columns > 3 /*More than three key columns.*/ + OR cc.sum_max_length > 16 /*More than 16 bytes in key */) + AND i.is_CX_columnstore = 0 + ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 25: Addicted to nullable columns.', 0,1) WITH NOWAIT; + WITH count_columns AS ( + SELECT [object_id], + [database_id], + [schema_name], + SUM(CASE is_nullable WHEN 1 THEN 0 ELSE 1 END) AS non_nullable_columns, + COUNT(*) AS total_columns + FROM #IndexColumns ic + WHERE index_id IN (1,0) /*Heap or clustered only*/ + GROUP BY [object_id], + [database_id], + [schema_name] + ) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 25 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Index Hoarder' AS findings_group, + N'Addicted to Nulls' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + i.db_schema_object_name + + N' allows null in ' + CAST((total_columns-non_nullable_columns) AS NVARCHAR(10)) + + N' of ' + CAST(total_columns AS NVARCHAR(10)) + + N' columns.' AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + JOIN count_columns AS cc ON i.[object_id]=cc.[object_id] + AND cc.database_id = ip.database_id + AND cc.[schema_name] = ip.[schema_name] + WHERE i.index_id IN (1,0) + AND cc.non_nullable_columns < 2 + AND cc.total_columns > 3 + ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 26: Wide tables (35+ cols or > 2000 non-LOB bytes).', 0,1) WITH NOWAIT; + WITH count_columns AS ( + SELECT [object_id], + [database_id], + [schema_name], + SUM(CASE max_length WHEN -1 THEN 1 ELSE 0 END) AS count_lob_columns, + SUM(CASE max_length WHEN -1 THEN 0 ELSE max_length END) AS sum_max_length, + COUNT(*) AS total_columns + FROM #IndexColumns ic + WHERE index_id IN (1,0) /*Heap or clustered only*/ + GROUP BY [object_id], + [database_id], + [schema_name] + ) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 26 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Index Hoarder' AS findings_group, + N'Wide Tables: 35+ cols or > 2000 non-LOB bytes' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + i.db_schema_object_name + + N' has ' + CAST((total_columns) AS NVARCHAR(10)) + + N' total columns with a max possible width of ' + CAST(sum_max_length AS NVARCHAR(10)) + + N' bytes.' + + CASE WHEN count_lob_columns > 0 THEN CAST((count_lob_columns) AS NVARCHAR(10)) + + ' columns are LOB types.' ELSE '' + END + AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + JOIN count_columns AS cc ON i.[object_id]=cc.[object_id] + AND cc.database_id = i.database_id + AND cc.[schema_name] = i.[schema_name] + WHERE i.index_id IN (1,0) + AND + (cc.total_columns >= 35 OR + cc.sum_max_length >= 2000) + ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 27: Addicted to strings.', 0,1) WITH NOWAIT; + WITH count_columns AS ( + SELECT [object_id], + [database_id], + [schema_name], + SUM(CASE WHEN system_type_name IN ('varchar','nvarchar','char') OR max_length=-1 THEN 1 ELSE 0 END) AS string_or_LOB_columns, + COUNT(*) AS total_columns + FROM #IndexColumns ic + WHERE index_id IN (1,0) /*Heap or clustered only*/ + GROUP BY [object_id], + [database_id], + [schema_name] + ) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 27 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Index Hoarder' AS findings_group, + N'Addicted to strings' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + i.db_schema_object_name + + N' uses string or LOB types for ' + CAST((string_or_LOB_columns) AS NVARCHAR(10)) + + N' of ' + CAST(total_columns AS NVARCHAR(10)) + + N' columns. Check if data types are valid.' AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + JOIN count_columns AS cc ON i.[object_id]=cc.[object_id] + AND cc.database_id = i.database_id + AND cc.[schema_name] = i.[schema_name] + CROSS APPLY (SELECT cc.total_columns - string_or_LOB_columns AS non_string_or_lob_columns) AS calc1 + WHERE i.index_id IN (1,0) + AND calc1.non_string_or_lob_columns <= 1 + AND cc.total_columns > 3 + ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 28: Non-unique clustered index.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 28 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Index Hoarder' AS findings_group, + N'Non-Unique Clustered Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + N'Uniquifiers will be required! Clustered index: ' + i.db_schema_object_name + + N' and all NC indexes. ' + + (SELECT CAST(COUNT(*) AS NVARCHAR(23)) + FROM #IndexSanity i2 + WHERE i2.[object_id]=i.[object_id] + AND i2.database_id = i.database_id + AND i2.index_id <> 1 + AND i2.is_disabled=0 + AND i2.is_hypothetical=0) + + N' NC indexes on the table.' + AS details, + i.index_definition, + secret_columns, + i.index_usage_summary, + ip.index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + WHERE index_id = 1 /* clustered only */ + AND is_unique=0 /* not unique */ + AND is_CX_columnstore=0 /* not a clustered columnstore-- no unique option on those */ + ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 29: NC indexes with 0 reads. (Borderline) and < 10,000 writes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 29 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Index Hoarder' AS findings_group, + N'Unused NC index with Low Writes' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexHoarder' AS URL, + N'0 reads: ' + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.total_reads=0 + AND i.user_updates < 10000 + AND i.index_id NOT IN (0,1) /*NCs only*/ + AND i.is_unique = 0 + AND sz.total_reserved_MB >= CASE WHEN (@GetAllDatabases = 1 OR @Mode = 0) THEN @ThresholdMB ELSE sz.total_reserved_MB END + /*Skipping tables created in the last week, or modified in past 2 days*/ + AND i.create_date >= DATEADD(dd,-7,GETDATE()) + AND i.modify_date > DATEADD(dd,-2,GETDATE()) + ORDER BY i.db_schema_object_indexid + OPTION ( RECOMPILE ); + + + ---------------------------------------- + --Feature-Phobic Indexes: Check_id 30-39 + ---------------------------------------- + RAISERROR(N'check_id 30: No indexes with includes', 0,1) WITH NOWAIT; + /* This does not work the way you'd expect with @GetAllDatabases = 1. For details: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/825 + */ + + SELECT database_name, + SUM(CASE WHEN count_included_columns > 0 THEN 1 ELSE 0 END) AS number_indexes_with_includes, + 100.* SUM(CASE WHEN count_included_columns > 0 THEN 1 ELSE 0 END) / ( 1.0 * COUNT(*) ) AS percent_indexes_with_includes + INTO #index_includes + FROM #IndexSanity + WHERE is_hypothetical = 0 + AND is_disabled = 0 + AND NOT (@GetAllDatabases = 1 OR @Mode = 0) + GROUP BY database_name; + + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 30 AS check_id, + NULL AS index_sanity_id, + 250 AS Priority, + N'Feature-Phobic Indexes' AS findings_group, + database_name AS [Database Name], + N'No Indexes Use Includes' AS finding, 'https://www.brentozar.com/go/IndexFeatures' AS URL, + N'No Indexes Use Includes' AS details, + database_name + N' (Entire database)' AS index_definition, + N'' AS secret_columns, + N'N/A' AS index_usage_summary, + N'N/A' AS index_size_summary + FROM #index_includes + WHERE number_indexes_with_includes = 0 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 31: < 3 percent of indexes have includes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 31 AS check_id, + NULL AS index_sanity_id, + 250 AS Priority, + N'Feature-Phobic Indexes' AS findings_group, + N'Few Indexes Use Includes' AS findings, + database_name AS [Database Name], + N'https://www.brentozar.com/go/IndexFeatures' AS URL, + N'Only ' + CAST(percent_indexes_with_includes AS NVARCHAR(20)) + '% of indexes have includes' AS details, + N'Entire database' AS index_definition, + N'' AS secret_columns, + N'N/A' AS index_usage_summary, + N'N/A' AS index_size_summary + FROM #index_includes + WHERE number_indexes_with_includes > 0 AND percent_indexes_with_includes <= 3 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 32: filtered indexes and indexed views', 0,1) WITH NOWAIT; + + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT DISTINCT + 32 AS check_id, + NULL AS index_sanity_id, + 250 AS Priority, + N'Feature-Phobic Indexes' AS findings_group, + N'No Filtered Indexes or Indexed Views' AS finding, + i.database_name AS [Database Name], + N'https://www.brentozar.com/go/IndexFeatures' AS URL, + N'These are NOT always needed-- but do you know when you would use them?' AS details, + i.database_name + N' (Entire database)' AS index_definition, + N'' AS secret_columns, + N'N/A' AS index_usage_summary, + N'N/A' AS index_size_summary + FROM #IndexSanity i + WHERE i.database_name NOT IN ( + SELECT database_name + FROM #IndexSanity + WHERE filter_definition <> '' ) + AND i.database_name NOT IN ( + SELECT database_name + FROM #IndexSanity + WHERE is_indexed_view = 1 ) + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 33: Potential filtered indexes based on column names.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 33 AS check_id, + i.index_sanity_id AS index_sanity_id, + 250 AS Priority, + N'Feature-Phobic Indexes' AS findings_group, + N'Potential Filtered Index (Based on Column Name)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/IndexFeatures' AS URL, + N'A column name in this index suggests it might be a candidate for filtering (is%, %archive%, %active%, %flag%)' AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexColumns ic + JOIN #IndexSanity i ON ic.[object_id]=i.[object_id] + AND ic.database_id =i.database_id + AND ic.schema_name = i.schema_name + AND ic.[index_id]=i.[index_id] + AND i.[index_id] > 1 /* non-clustered index */ + JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + WHERE (column_name LIKE 'is%' + OR column_name LIKE '%archive%' + OR column_name LIKE '%active%' + OR column_name LIKE '%flag%') + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 41: Hypothetical indexes ', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 41 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Hypothetical Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + N'Hypothetical Index: ' + db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + N'' AS index_usage_summary, + N'' AS index_size_summary + FROM #IndexSanity AS i + WHERE is_hypothetical = 1 + OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 42: Disabled indexes', 0,1) WITH NOWAIT; + --Note: disabled NC indexes will have O rows in #IndexSanitySize! + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 42 AS check_id, + index_sanity_id, + 150 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Disabled Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + N'Disabled Index:' + db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + 'DISABLED' AS index_size_summary + FROM #IndexSanity AS i + WHERE is_disabled = 1 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 49: Heaps with deletes', 0,1) WITH NOWAIT; + WITH heaps_cte + AS ( SELECT [object_id], + [database_id], + [schema_name], + SUM(leaf_delete_count) AS leaf_delete_count + FROM #IndexPartitionSanity + GROUP BY [object_id], + [database_id], + [schema_name] + HAVING SUM(forwarded_fetch_count) < 1000 * @DaysUptime /* Only alert about indexes with no forwarded fetches - we already alerted about those in check_id 43 */ + AND SUM(leaf_delete_count) > 0) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 49 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Self Loathing Indexes' AS findings_group, + N'Heaps with Deletes' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/SelfLoathing' AS URL, + CAST(h.leaf_delete_count AS NVARCHAR(256)) + N' deletes against heap:' + + db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + sz.index_size_summary + FROM #IndexSanity i + JOIN heaps_cte h ON i.[object_id] = h.[object_id] + AND i.[database_id] = h.[database_id] + AND i.[schema_name] = h.[schema_name] + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.index_id = 0 + AND sz.total_reserved_MB >= CASE WHEN NOT (@GetAllDatabases = 1 OR @Mode = 4) THEN @ThresholdMB ELSE sz.total_reserved_MB END + OPTION ( RECOMPILE ); + + ---------------------------------------- + --Abnormal Psychology : Check_id 60-79 + ---------------------------------------- + RAISERROR(N'check_id 60: XML indexes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 60 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'XML Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + N'' AS index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.is_XML = 1 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 61: Columnstore indexes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 61 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + CASE WHEN i.is_NC_columnstore=1 + THEN N'NC Columnstore Index' + ELSE N'Clustered Columnstore Index' + END AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.is_NC_columnstore = 1 OR i.is_CX_columnstore=1 + OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 62: Spatial indexes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 62 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Spatial Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.is_spatial = 1 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 63: Compressed indexes', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 63 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Compressed Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid + N'. COMPRESSION: ' + sz.data_compression_desc AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE sz.data_compression_desc LIKE '%PAGE%' OR sz.data_compression_desc LIKE '%ROW%' + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 64: Partitioned', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 64 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Partitioned Index' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.partition_key_column_name IS NOT NULL + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 65: Non-Aligned Partitioned', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 65 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Non-Aligned Index on a Partitioned Table' AS finding, + i.[database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanity AS iParent ON + i.[object_id]=iParent.[object_id] + AND i.database_id = iParent.database_id + AND i.schema_name = iParent.schema_name + AND iParent.index_id IN (0,1) /* could be a partitioned heap or clustered table */ + AND iParent.partition_key_column_name IS NOT NULL /* parent is partitioned*/ + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.partition_key_column_name IS NULL + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 66: Recently created tables/indexes (1 week)', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 66 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Recently Created Tables/Indexes (1 week)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid + N' was created on ' + + CONVERT(NVARCHAR(16),i.create_date,121) + + N'. Tables/indexes which are dropped/created regularly require special methods for index tuning.' + AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.create_date >= DATEADD(dd,-7,GETDATE()) + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 67: Recently modified tables/indexes (2 days)', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 67 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Recently Modified Tables/Indexes (2 days)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid + N' was modified on ' + + CONVERT(NVARCHAR(16),i.modify_date,121) + + N'. A large amount of recently modified indexes may mean a lot of rebuilds are occurring each night.' + AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.modify_date > DATEADD(dd,-2,GETDATE()) + AND /*Exclude recently created tables.*/ + i.create_date < DATEADD(dd,-7,GETDATE()) + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 69: Column collation does not match database collation', 0,1) WITH NOWAIT; + WITH count_columns AS ( + SELECT [object_id], + database_id, + schema_name, + COUNT(*) AS column_count + FROM #IndexColumns ic + WHERE index_id IN (1,0) /*Heap or clustered only*/ + AND collation_name <> @collation + GROUP BY [object_id], + database_id, + schema_name + ) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 69 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Column Collation Does Not Match Database Collation' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_name + + N' has ' + CAST(column_count AS NVARCHAR(20)) + + N' column' + CASE WHEN column_count > 1 THEN 's' ELSE '' END + + N' with a different collation than the db collation of ' + + @collation AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + JOIN count_columns AS cc ON i.[object_id]=cc.[object_id] + AND cc.database_id = i.database_id + AND cc.schema_name = i.schema_name + WHERE i.index_id IN (1,0) + ORDER BY i.db_schema_object_name DESC OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 70: Replicated columns', 0,1) WITH NOWAIT; + WITH count_columns AS ( + SELECT [object_id], + database_id, + schema_name, + COUNT(*) AS column_count, + SUM(CASE is_replicated WHEN 1 THEN 1 ELSE 0 END) AS replicated_column_count + FROM #IndexColumns ic + WHERE index_id IN (1,0) /*Heap or clustered only*/ + GROUP BY object_id, + database_id, + schema_name + ) + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 70 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Replicated Columns' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_name + + N' has ' + CAST(replicated_column_count AS NVARCHAR(20)) + + N' out of ' + CAST(column_count AS NVARCHAR(20)) + + N' column' + CASE WHEN column_count > 1 THEN 's' ELSE '' END + + N' in one or more publications.' + AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + JOIN count_columns AS cc ON i.[object_id]=cc.[object_id] + AND i.database_id = cc.database_id + AND i.schema_name = cc.schema_name + WHERE i.index_id IN (1,0) + AND replicated_column_count > 0 + ORDER BY i.db_schema_object_name DESC + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 71: Cascading updates or cascading deletes.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary, more_info ) + SELECT 71 AS check_id, + NULL AS index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Cascading Updates or Deletes' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + N'Foreign Key ' + QUOTENAME(foreign_key_name) + + N' on ' + QUOTENAME(parent_object_name) + N'(' + LTRIM(parent_fk_columns) + N')' + + N' referencing ' + QUOTENAME(referenced_object_name) + N'(' + LTRIM(referenced_fk_columns) + N')' + + N' has settings:' + + CASE [delete_referential_action_desc] WHEN N'NO_ACTION' THEN N'' ELSE N' ON DELETE ' +[delete_referential_action_desc] END + + CASE [update_referential_action_desc] WHEN N'NO_ACTION' THEN N'' ELSE N' ON UPDATE ' + [update_referential_action_desc] END + AS details, + [fk].[database_name] AS index_definition, + N'N/A' AS secret_columns, + N'N/A' AS index_usage_summary, + N'N/A' AS index_size_summary, + (SELECT TOP 1 more_info FROM #IndexSanity i WHERE i.object_id=fk.parent_object_id AND i.database_id = fk.database_id AND i.schema_name = fk.schema_name) + AS more_info + FROM #ForeignKeys fk + WHERE ([delete_referential_action_desc] <> N'NO_ACTION' + OR [update_referential_action_desc] <> N'NO_ACTION') + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 72: Unindexed foreign keys.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary, more_info ) + SELECT 72 AS check_id, + NULL AS index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Unindexed Foreign Keys' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + N'Foreign Key ' + QUOTENAME(foreign_key_name) + + N' on ' + QUOTENAME(parent_object_name) + N'' + + N' referencing ' + QUOTENAME(referenced_object_name) + N'' + + N' does not appear to have a supporting index.' AS details, + N'N/A' AS index_definition, + N'N/A' AS secret_columns, + N'N/A' AS index_usage_summary, + N'N/A' AS index_size_summary, + (SELECT TOP 1 more_info FROM #IndexSanity i WHERE i.object_id=fk.parent_object_id AND i.database_id = fk.database_id AND i.schema_name = fk.schema_name) + AS more_info + FROM #UnindexedForeignKeys AS fk + OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 73: In-Memory OLTP', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 73 AS check_id, + i.index_sanity_id, + 150 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'In-Memory OLTP' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_indexid AS details, + i.index_definition, + i.secret_columns, + i.index_usage_summary, + ISNULL(sz.index_size_summary,'') AS index_size_summary + FROM #IndexSanity AS i + JOIN #IndexSanitySize sz ON i.index_sanity_id = sz.index_sanity_id + WHERE i.is_in_memory_oltp = 1 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 74: Identity column with unusual seed', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 74 AS check_id, + i.index_sanity_id, + 200 AS Priority, + N'Abnormal Psychology' AS findings_group, + N'Identity Column Using a Negative Seed or Increment Other Than 1' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/AbnormalPsychology' AS URL, + i.db_schema_object_name + N'.' + QUOTENAME(ic.column_name) + + N' is an identity with type ' + ic.system_type_name + + N', last value of ' + + ISNULL((CONVERT(NVARCHAR(256),CAST(ic.last_value AS DECIMAL(38,0)), 1)),N'NULL') + + N', seed of ' + + ISNULL((CONVERT(NVARCHAR(256),CAST(ic.seed_value AS DECIMAL(38,0)), 1)),N'NULL') + + N', increment of ' + CAST(ic.increment_value AS NVARCHAR(256)) + + N', and range of ' + + CASE ic.system_type_name WHEN 'int' THEN N'+/- 2,147,483,647' + WHEN 'smallint' THEN N'+/- 32,768' + WHEN 'tinyint' THEN N'0 to 255' + ELSE 'unknown' + END + AS details, + i.index_definition, + secret_columns, + ISNULL(i.index_usage_summary,''), + ISNULL(ip.index_size_summary,'') + FROM #IndexSanity i + JOIN #IndexColumns ic ON + i.object_id=ic.object_id + AND i.database_id = ic.database_id + AND i.schema_name = ic.schema_name + AND i.index_id IN (0,1) /* heaps and cx only */ + AND ic.is_identity=1 + AND ic.system_type_name IN ('tinyint', 'smallint', 'int') + JOIN #IndexSanitySize ip ON i.index_sanity_id = ip.index_sanity_id + WHERE i.index_id IN (1,0) + AND (ic.seed_value < 0 OR ic.increment_value <> 1) + ORDER BY finding, details DESC + OPTION ( RECOMPILE ); + + ---------------------------------------- + --Workaholics: Check_id 80-89 + ---------------------------------------- + + RAISERROR(N'check_id 80: Most scanned indexes (index_usage_stats)', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + + --Workaholics according to index_usage_stats + --This isn't perfect: it mentions the number of scans present in a plan + --A "scan" isn't necessarily a full scan, but hey, we gotta do the best with what we've got. + --in the case of things like indexed views, the operator might be in the plan but never executed + SELECT TOP 5 + 80 AS check_id, + i.index_sanity_id AS index_sanity_id, + 200 AS Priority, + N'Workaholics' AS findings_group, + N'Scan-a-lots (index-usage-stats)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/Workaholics' AS URL, + REPLACE(CONVERT( NVARCHAR(50),CAST(i.user_scans AS MONEY),1),'.00','') + + N' scans against ' + i.db_schema_object_indexid + + N'. Latest scan: ' + ISNULL(CAST(i.last_user_scan AS NVARCHAR(128)),'?') + N'. ' + + N'ScanFactor=' + CAST(((i.user_scans * iss.total_reserved_MB)/1000000.) AS NVARCHAR(256)) AS details, + ISNULL(i.key_column_names_with_sort_order,'N/A') AS index_definition, + ISNULL(i.secret_columns,'') AS secret_columns, + i.index_usage_summary AS index_usage_summary, + iss.index_size_summary AS index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize iss ON i.index_sanity_id=iss.index_sanity_id + WHERE ISNULL(i.user_scans,0) > 0 + ORDER BY i.user_scans * iss.total_reserved_MB DESC + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 81: Top recent accesses (op stats)', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, index_sanity_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + --Workaholics according to index_operational_stats + --This isn't perfect either: range_scan_count contains full scans, partial scans, even seeks in nested loop ops + --But this can help bubble up some most-accessed tables + SELECT TOP 5 + 81 AS check_id, + i.index_sanity_id AS index_sanity_id, + 200 AS Priority, + N'Workaholics' AS findings_group, + N'Top Recent Accesses (index-op-stats)' AS finding, + [database_name] AS [Database Name], + N'https://www.brentozar.com/go/Workaholics' AS URL, + ISNULL(REPLACE( + CONVERT(NVARCHAR(50),CAST((iss.total_range_scan_count + iss.total_singleton_lookup_count) AS MONEY),1), + N'.00',N'') + + N' uses of ' + i.db_schema_object_indexid + N'. ' + + REPLACE(CONVERT(NVARCHAR(50), CAST(iss.total_range_scan_count AS MONEY),1),N'.00',N'') + N' scans or seeks. ' + + REPLACE(CONVERT(NVARCHAR(50), CAST(iss.total_singleton_lookup_count AS MONEY), 1),N'.00',N'') + N' singleton lookups. ' + + N'OpStatsFactor=' + CAST(((((iss.total_range_scan_count + iss.total_singleton_lookup_count) * iss.total_reserved_MB))/1000000.) AS VARCHAR(256)),'') AS details, + ISNULL(i.key_column_names_with_sort_order,'N/A') AS index_definition, + ISNULL(i.secret_columns,'') AS secret_columns, + i.index_usage_summary AS index_usage_summary, + iss.index_size_summary AS index_size_summary + FROM #IndexSanity i + JOIN #IndexSanitySize iss ON i.index_sanity_id=iss.index_sanity_id + WHERE (ISNULL(iss.total_range_scan_count,0) > 0 OR ISNULL(iss.total_singleton_lookup_count,0) > 0) + ORDER BY ((iss.total_range_scan_count + iss.total_singleton_lookup_count) * iss.total_reserved_MB) DESC + OPTION ( RECOMPILE ); + + + + RAISERROR(N'check_id 93: Statistics with filters', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 93 AS check_id, + 200 AS Priority, + 'Functioning Statistaholics' AS findings_group, + 'Filter Fixation', + s.database_name, + 'https://www.brentozar.com/go/stats' AS URL, + 'The statistic ' + QUOTENAME(s.statistics_name) + ' is filtered on [' + s.filter_definition + ']. It could be part of a filtered index, or just a filtered statistic. This is purely informational.' AS details, + QUOTENAME(database_name) + '.' + QUOTENAME(s.schema_name) + '.' + QUOTENAME(s.table_name) + '.' + QUOTENAME(s.index_name) + '.' + QUOTENAME(s.statistics_name) + '.' + QUOTENAME(s.column_names) AS index_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #Statistics AS s + WHERE s.has_filter = 1 + OPTION ( RECOMPILE ); + + + RAISERROR(N'check_id 100: Computed Columns that are not Persisted.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + SELECT 100 AS check_id, + 200 AS Priority, + 'Cold Calculators' AS findings_group, + 'Definition Defeatists' AS finding, + cc.database_name, + '' AS URL, + 'The computed column ' + QUOTENAME(cc.column_name) + ' on ' + QUOTENAME(cc.schema_name) + '.' + QUOTENAME(cc.table_name) + ' is not persisted, which means it will be calculated when a query runs.' + + 'You can change this with the following command, if the definition is deterministic: ALTER TABLE ' + QUOTENAME(cc.schema_name) + '.' + QUOTENAME(cc.table_name) + ' ALTER COLUMN ' + cc.column_name + + ' ADD PERSISTED' AS details, + cc.column_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #ComputedColumns AS cc + WHERE cc.is_persisted = 0 + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 110: Temporal Tables.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + + SELECT 110 AS check_id, + 200 AS Priority, + 'Abnormal Psychology' AS findings_group, + 'Temporal Tables', + t.database_name, + '' AS URL, + 'The table ' + QUOTENAME(t.schema_name) + '.' + QUOTENAME(t.table_name) + ' is a temporal table, with rows versioned in ' + + QUOTENAME(t.history_schema_name) + '.' + QUOTENAME(t.history_table_name) + ' on History columns ' + QUOTENAME(t.start_column_name) + ' and ' + QUOTENAME(t.end_column_name) + '.' + AS details, + '' AS index_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #TemporalTables AS t + OPTION ( RECOMPILE ); + + RAISERROR(N'check_id 121: Optimized For Sequental Keys.', 0,1) WITH NOWAIT; + INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition, + secret_columns, index_usage_summary, index_size_summary ) + + SELECT 121 AS check_id, + 200 AS Priority, + 'Medicated Indexes' AS findings_group, + 'Optimized For Sequential Keys', + i.database_name, + '' AS URL, + 'The table ' + QUOTENAME(i.schema_name) + '.' + QUOTENAME(i.object_name) + ' is optimized for sequential keys.' AS details, + '' AS index_definition, + 'N/A' AS secret_columns, + 'N/A' AS index_usage_summary, + 'N/A' AS index_size_summary + FROM #IndexSanity AS i + WHERE i.optimize_for_sequential_key = 1 + OPTION ( RECOMPILE ); + + + + END /* IF @Mode = 4 */ + + + + + + + + + RAISERROR(N'Insert a row to help people find help', 0,1) WITH NOWAIT; + IF DATEDIFF(MM, @VersionDate, GETDATE()) > 6 + BEGIN + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( -1, 0 , + 'Outdated sp_BlitzIndex', 'sp_BlitzIndex is Over 6 Months Old', 'http://FirstResponderKit.org/', + 'Fine wine gets better with age, but this ' + @ScriptVersionName + ' is more like bad cheese. Time to get a new one.', + @DaysUptimeInsertValue,N'',N'' + ); + END; + + IF EXISTS(SELECT * FROM #BlitzIndexResults) + BEGIN + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( -1, 0 , + @ScriptVersionName, + CASE WHEN @GetAllDatabases = 1 THEN N'All Databases' ELSE N'Database ' + QUOTENAME(@DatabaseName) + N' as of ' + CONVERT(NVARCHAR(16),GETDATE(),121) END, + N'From Your Community Volunteers' , N'http://FirstResponderKit.org' , + @DaysUptimeInsertValue,N'',N'' + ); + END; + ELSE IF @Mode = 0 OR (@GetAllDatabases = 1 AND @Mode <> 4) + BEGIN + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( -1, 0 , + @ScriptVersionName, + CASE WHEN @GetAllDatabases = 1 THEN N'All Databases' ELSE N'Database ' + QUOTENAME(@DatabaseName) + N' as of ' + CONVERT(NVARCHAR(16),GETDATE(),121) END, + N'From Your Community Volunteers' , N'http://FirstResponderKit.org' , + @DaysUptimeInsertValue, N'',N'' + ); + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( 1, 0 , + N'No Major Problems Found', + N'Nice Work!', + N'http://FirstResponderKit.org', + N'Consider running with @Mode = 4 in individual databases (not all) for more detailed diagnostics.', + N'The default Mode 0 only looks for very serious index issues.', + @DaysUptimeInsertValue, N'' + ); + + END; + ELSE + BEGIN + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( -1, 0 , + @ScriptVersionName, + CASE WHEN @GetAllDatabases = 1 THEN N'All Databases' ELSE N'Database ' + QUOTENAME(@DatabaseName) + N' as of ' + CONVERT(NVARCHAR(16),GETDATE(),121) END, + N'From Your Community Volunteers' , N'http://FirstResponderKit.org' , + @DaysUptimeInsertValue, N'',N'' + ); + INSERT #BlitzIndexResults ( Priority, check_id, findings_group, finding, URL, details, index_definition, + index_usage_summary, index_size_summary ) + VALUES ( 1, 0 , + N'No Problems Found', + N'Nice job! Or more likely, you have a nearly empty database.', + N'http://FirstResponderKit.org', 'Time to go read some blog posts.', + @DaysUptimeInsertValue, N'', N'' + ); + + END; + + RAISERROR(N'Returning results.', 0,1) WITH NOWAIT; + + /*Return results.*/ + IF (@ValidOutputLocation = 1 AND COALESCE(@OutputServerName, @OutputDatabaseName, @OutputSchemaName, @OutputTableName) IS NOT NULL) + BEGIN + IF NOT @SchemaExists = 1 + BEGIN + RAISERROR (N'Invalid schema name, data could not be saved.', 16, 0); + RETURN; + END + + IF @TableExists = 0 + BEGIN + SET @StringToExecute = + N'CREATE TABLE @@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [id] INT IDENTITY(1,1) NOT NULL, + [run_id] UNIQUEIDENTIFIER, + [run_datetime] DATETIME, + [server_name] NVARCHAR(128), + [priority] INT, + [finding] NVARCHAR(4000), + [database_name] NVARCHAR(128), + [details] NVARCHAR(MAX), + [index_definition] NVARCHAR(MAX), + [secret_columns] NVARCHAR(MAX), + [index_usage_summary] NVARCHAR(MAX), + [index_size_summary] NVARCHAR(MAX), + [more_info] NVARCHAR(MAX), + [url] NVARCHAR(MAX), + [create_tsql] NVARCHAR(MAX), + [sample_query_plan] XML, + CONSTRAINT [PK_ID_@@@RunID@@@] PRIMARY KEY CLUSTERED ([id] ASC) + );'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''',''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END; + END; /* @TableExists = 0 */ + + -- Re-check that table now exists (if not we failed creating it) + SET @TableExists = NULL; + EXEC sp_executesql @TableExistsSql, N'@TableExists BIT OUTPUT', @TableExists OUTPUT; + + IF NOT @TableExists = 1 + BEGIN + RAISERROR('Creation of the output table failed.', 16, 0); + RETURN; + END; + + SET @StringToExecute = + N'INSERT @@@OutputServerName@@@.@@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [run_id], + [run_datetime], + [server_name], + [priority], + [finding], + [database_name], + [details], + [index_definition], + [secret_columns], + [index_usage_summary], + [index_size_summary], + [more_info], + [url], + [create_tsql], + [sample_query_plan] + ) + SELECT + ''@@@RunID@@@'', + ''@@@GETDATE@@@'', + ''@@@LocalServerName@@@'', + -- Below should be a copy/paste of the real query + -- Make sure all quotes are escaped + Priority, ISNULL(br.findings_group,N'''') + + CASE WHEN ISNULL(br.finding,N'''') <> N'''' THEN N'': '' ELSE N'''' END + + br.finding AS [Finding], + br.[database_name] AS [Database Name], + br.details AS [Details: schema.table.index(indexid)], + br.index_definition AS [Definition: [Property]] ColumnName {datatype maxbytes}], + ISNULL(br.secret_columns,'''') AS [Secret Columns], + br.index_usage_summary AS [Usage], + br.index_size_summary AS [Size], + COALESCE(br.more_info,sn.more_info,'''') AS [More Info], + br.URL, + COALESCE(br.create_tsql,ts.create_tsql,'''') AS [Create TSQL], + br.sample_query_plan AS [Sample Query Plan] + FROM #BlitzIndexResults br + LEFT JOIN #IndexSanity sn ON + br.index_sanity_id=sn.index_sanity_id + LEFT JOIN #IndexCreateTsql ts ON + br.index_sanity_id=ts.index_sanity_id + ORDER BY br.Priority ASC, br.check_id ASC, br.blitz_result_id ASC, br.findings_group ASC + OPTION (RECOMPILE);'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputServerName@@@', @OutputServerName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@GETDATE@@@', GETDATE()); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@LocalServerName@@@', CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))); + EXEC(@StringToExecute); + + END + ELSE + BEGIN + IF(@OutputType <> 'NONE') + BEGIN + SELECT Priority, ISNULL(br.findings_group,N'') + + CASE WHEN ISNULL(br.finding,N'') <> N'' THEN N': ' ELSE N'' END + + br.finding AS [Finding], + br.[database_name] AS [Database Name], + br.details AS [Details: schema.table.index(indexid)], + br.index_definition AS [Definition: [Property]] ColumnName {datatype maxbytes}], + ISNULL(br.secret_columns,'') AS [Secret Columns], + br.index_usage_summary AS [Usage], + br.index_size_summary AS [Size], + COALESCE(br.more_info,sn.more_info,'') AS [More Info], + br.URL, + COALESCE(br.create_tsql,ts.create_tsql,'') AS [Create TSQL], + br.sample_query_plan AS [Sample Query Plan] + FROM #BlitzIndexResults br + LEFT JOIN #IndexSanity sn ON + br.index_sanity_id=sn.index_sanity_id + LEFT JOIN #IndexCreateTsql ts ON + br.index_sanity_id=ts.index_sanity_id + ORDER BY br.Priority ASC, br.check_id ASC, br.blitz_result_id ASC, br.findings_group ASC + OPTION (RECOMPILE); + END; + + END; + + END /* End @Mode=0 or 4 (diagnose)*/ + + + + + + + + + ELSE IF (@Mode=1) /*Summarize*/ + BEGIN + --This mode is to give some overall stats on the database. + IF (@ValidOutputLocation = 1 AND COALESCE(@OutputServerName, @OutputDatabaseName, @OutputSchemaName, @OutputTableName) IS NOT NULL) + BEGIN + + IF NOT @SchemaExists = 1 + BEGIN + RAISERROR (N'Invalid schema name, data could not be saved.', 16, 0); + RETURN; + END + + IF @TableExists = 0 + BEGIN + SET @StringToExecute = + N'CREATE TABLE @@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [id] INT IDENTITY(1,1) NOT NULL, + [run_id] UNIQUEIDENTIFIER, + [run_datetime] DATETIME, + [server_name] NVARCHAR(128), + [database_name] NVARCHAR(128), + [object_count] INT, + [reserved_gb] NUMERIC(29,1), + [reserved_lob_gb] NUMERIC(29,1), + [reserved_row_overflow_gb] NUMERIC(29,1), + [clustered_table_count] INT, + [clustered_table_gb] NUMERIC(29,1), + [nc_index_count] INT, + [nc_index_gb] NUMERIC(29,1), + [table_nc_index_ratio] NUMERIC(29,1), + [heap_count] INT, + [heap_gb] NUMERIC(29,1), + [partioned_table_count] INT, + [partioned_nc_count] INT, + [partioned_gb] NUMERIC(29,1), + [filtered_index_count] INT, + [indexed_view_count] INT, + [max_table_row_count] INT, + [max_table_gb] NUMERIC(29,1), + [max_nc_index_gb] NUMERIC(29,1), + [table_count_over_1gb] INT, + [table_count_over_10gb] INT, + [table_count_over_100gb] INT, + [nc_index_count_over_1gb] INT, + [nc_index_count_over_10gb] INT, + [nc_index_count_over_100gb] INT, + [min_create_date] DATETIME, + [max_create_date] DATETIME, + [max_modify_date] DATETIME, + [display_order] INT, + CONSTRAINT [PK_ID_@@@RunID@@@] PRIMARY KEY CLUSTERED ([id] ASC) + );'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''',''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END; + END; /* @TableExists = 0 */ + + -- Re-check that table now exists (if not we failed creating it) + SET @TableExists = NULL; + EXEC sp_executesql @TableExistsSql, N'@TableExists BIT OUTPUT', @TableExists OUTPUT; + + IF NOT @TableExists = 1 + BEGIN + RAISERROR('Creation of the output table failed.', 16, 0); + RETURN; + END; + + SET @StringToExecute = + N'INSERT @@@OutputServerName@@@.@@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [run_id], + [run_datetime], + [server_name], + [database_name], + [object_count], + [reserved_gb], + [reserved_lob_gb], + [reserved_row_overflow_gb], + [clustered_table_count], + [clustered_table_gb], + [nc_index_count], + [nc_index_gb], + [table_nc_index_ratio], + [heap_count], + [heap_gb], + [partioned_table_count], + [partioned_nc_count], + [partioned_gb], + [filtered_index_count], + [indexed_view_count], + [max_table_row_count], + [max_table_gb], + [max_nc_index_gb], + [table_count_over_1gb], + [table_count_over_10gb], + [table_count_over_100gb], + [nc_index_count_over_1gb], + [nc_index_count_over_10gb], + [nc_index_count_over_100gb], + [min_create_date], + [max_create_date], + [max_modify_date], + [display_order] + ) + SELECT ''@@@RunID@@@'', + ''@@@GETDATE@@@'', + ''@@@LocalServerName@@@'', + -- Below should be a copy/paste of the real query + -- Make sure all quotes are escaped + -- NOTE! information line is skipped from output and the query below + -- NOTE! initial columns are not casted to nvarchar due to not outputing informational line + DB_NAME(i.database_id) AS [Database Name], + COUNT(*) AS [Number Objects], + CAST(SUM(sz.total_reserved_MB)/ + 1024. AS NUMERIC(29,1)) AS [All GB], + CAST(SUM(sz.total_reserved_LOB_MB)/ + 1024. AS NUMERIC(29,1)) AS [LOB GB], + CAST(SUM(sz.total_reserved_row_overflow_MB)/ + 1024. AS NUMERIC(29,1)) AS [Row Overflow GB], + SUM(CASE WHEN index_id=1 THEN 1 ELSE 0 END) AS [Clustered Tables], + CAST(SUM(CASE WHEN index_id=1 THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Clustered Tables GB], + SUM(CASE WHEN index_id NOT IN (0,1) THEN 1 ELSE 0 END) AS [NC Indexes], + CAST(SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [NC Indexes GB], + CASE WHEN SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) > 0 THEN + CAST(SUM(CASE WHEN index_id IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + / SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) AS NUMERIC(29,1)) + ELSE 0 END AS [ratio table: NC Indexes], + SUM(CASE WHEN index_id=0 THEN 1 ELSE 0 END) AS [Heaps], + CAST(SUM(CASE WHEN index_id=0 THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Heaps GB], + SUM(CASE WHEN index_id IN (0,1) AND partition_key_column_name IS NOT NULL THEN 1 ELSE 0 END) AS [Partitioned Tables], + SUM(CASE WHEN index_id NOT IN (0,1) AND partition_key_column_name IS NOT NULL THEN 1 ELSE 0 END) AS [Partitioned NCs], + CAST(SUM(CASE WHEN partition_key_column_name IS NOT NULL THEN sz.total_reserved_MB ELSE 0 END)/1024. AS NUMERIC(29,1)) AS [Partitioned GB], + SUM(CASE WHEN filter_definition <> '''' THEN 1 ELSE 0 END) AS [Filtered Indexes], + SUM(CASE WHEN is_indexed_view=1 THEN 1 ELSE 0 END) AS [Indexed Views], + MAX(total_rows) AS [Max Row Count], + CAST(MAX(CASE WHEN index_id IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Max Table GB], + CAST(MAX(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Max NC Index GB], + SUM(CASE WHEN index_id IN (0,1) AND sz.total_reserved_MB > 1024 THEN 1 ELSE 0 END) AS [Count Tables > 1GB], + SUM(CASE WHEN index_id IN (0,1) AND sz.total_reserved_MB > 10240 THEN 1 ELSE 0 END) AS [Count Tables > 10GB], + SUM(CASE WHEN index_id IN (0,1) AND sz.total_reserved_MB > 102400 THEN 1 ELSE 0 END) AS [Count Tables > 100GB], + SUM(CASE WHEN index_id NOT IN (0,1) AND sz.total_reserved_MB > 1024 THEN 1 ELSE 0 END) AS [Count NCs > 1GB], + SUM(CASE WHEN index_id NOT IN (0,1) AND sz.total_reserved_MB > 10240 THEN 1 ELSE 0 END) AS [Count NCs > 10GB], + SUM(CASE WHEN index_id NOT IN (0,1) AND sz.total_reserved_MB > 102400 THEN 1 ELSE 0 END) AS [Count NCs > 100GB], + MIN(create_date) AS [Oldest Create Date], + MAX(create_date) AS [Most Recent Create Date], + MAX(modify_date) AS [Most Recent Modify Date], + 1 AS [Display Order] + FROM #IndexSanity AS i + --left join here so we don''t lose disabled nc indexes + LEFT JOIN #IndexSanitySize AS sz + ON i.index_sanity_id=sz.index_sanity_id + GROUP BY DB_NAME(i.database_id) + ORDER BY [Display Order] ASC + OPTION (RECOMPILE);'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputServerName@@@', @OutputServerName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@GETDATE@@@', GETDATE()); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@LocalServerName@@@', CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))); + EXEC(@StringToExecute); + + END; /* @ValidOutputLocation = 1 */ + ELSE + BEGIN + IF(@OutputType <> 'NONE') + BEGIN + + RAISERROR(N'@Mode=1, we are summarizing.', 0,1) WITH NOWAIT; + + SELECT DB_NAME(i.database_id) AS [Database Name], + CAST((COUNT(*)) AS NVARCHAR(256)) AS [Number Objects], + CAST(CAST(SUM(sz.total_reserved_MB)/ + 1024. AS NUMERIC(29,1)) AS NVARCHAR(500)) AS [All GB], + CAST(CAST(SUM(sz.total_reserved_LOB_MB)/ + 1024. AS NUMERIC(29,1)) AS NVARCHAR(500)) AS [LOB GB], + CAST(CAST(SUM(sz.total_reserved_row_overflow_MB)/ + 1024. AS NUMERIC(29,1)) AS NVARCHAR(500)) AS [Row Overflow GB], + CAST(SUM(CASE WHEN index_id=1 THEN 1 ELSE 0 END)AS NVARCHAR(50)) AS [Clustered Tables], + CAST(SUM(CASE WHEN index_id=1 THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Clustered Tables GB], + SUM(CASE WHEN index_id NOT IN (0,1) THEN 1 ELSE 0 END) AS [NC Indexes], + CAST(SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [NC Indexes GB], + CASE WHEN SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) > 0 THEN + CAST(SUM(CASE WHEN index_id IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + / SUM(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) AS NUMERIC(29,1)) + ELSE 0 END AS [ratio table: NC Indexes], + SUM(CASE WHEN index_id=0 THEN 1 ELSE 0 END) AS [Heaps], + CAST(SUM(CASE WHEN index_id=0 THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Heaps GB], + SUM(CASE WHEN index_id IN (0,1) AND partition_key_column_name IS NOT NULL THEN 1 ELSE 0 END) AS [Partitioned Tables], + SUM(CASE WHEN index_id NOT IN (0,1) AND partition_key_column_name IS NOT NULL THEN 1 ELSE 0 END) AS [Partitioned NCs], + CAST(SUM(CASE WHEN partition_key_column_name IS NOT NULL THEN sz.total_reserved_MB ELSE 0 END)/1024. AS NUMERIC(29,1)) AS [Partitioned GB], + SUM(CASE WHEN filter_definition <> '' THEN 1 ELSE 0 END) AS [Filtered Indexes], + SUM(CASE WHEN is_indexed_view=1 THEN 1 ELSE 0 END) AS [Indexed Views], + MAX(total_rows) AS [Max Row Count], + CAST(MAX(CASE WHEN index_id IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Max Table GB], + CAST(MAX(CASE WHEN index_id NOT IN (0,1) THEN sz.total_reserved_MB ELSE 0 END) + /1024. AS NUMERIC(29,1)) AS [Max NC Index GB], + SUM(CASE WHEN index_id IN (0,1) AND sz.total_reserved_MB > 1024 THEN 1 ELSE 0 END) AS [Count Tables > 1GB], + SUM(CASE WHEN index_id IN (0,1) AND sz.total_reserved_MB > 10240 THEN 1 ELSE 0 END) AS [Count Tables > 10GB], + SUM(CASE WHEN index_id IN (0,1) AND sz.total_reserved_MB > 102400 THEN 1 ELSE 0 END) AS [Count Tables > 100GB], + SUM(CASE WHEN index_id NOT IN (0,1) AND sz.total_reserved_MB > 1024 THEN 1 ELSE 0 END) AS [Count NCs > 1GB], + SUM(CASE WHEN index_id NOT IN (0,1) AND sz.total_reserved_MB > 10240 THEN 1 ELSE 0 END) AS [Count NCs > 10GB], + SUM(CASE WHEN index_id NOT IN (0,1) AND sz.total_reserved_MB > 102400 THEN 1 ELSE 0 END) AS [Count NCs > 100GB], + MIN(create_date) AS [Oldest Create Date], + MAX(create_date) AS [Most Recent Create Date], + MAX(modify_date) AS [Most Recent Modify Date], + 1 AS [Display Order] + FROM #IndexSanity AS i + --left join here so we don't lose disabled nc indexes + LEFT JOIN #IndexSanitySize AS sz + ON i.index_sanity_id=sz.index_sanity_id + GROUP BY DB_NAME(i.database_id) + UNION ALL + SELECT CASE WHEN @GetAllDatabases = 1 THEN N'All Databases' ELSE N'Database ' + N' as of ' + CONVERT(NVARCHAR(16),GETDATE(),121) END, + @ScriptVersionName, + N'From Your Community Volunteers' , + N'http://FirstResponderKit.org' , + @DaysUptimeInsertValue, + NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, + NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, + NULL,NULL,0 AS display_order + ORDER BY [Display Order] ASC + OPTION (RECOMPILE); + END; + END; + + END; /* End @Mode=1 (summarize)*/ + + + + + + + + + ELSE IF (@Mode=2) /*Index Detail*/ + BEGIN + --This mode just spits out all the detail without filters. + --This supports slicing AND dicing in Excel + RAISERROR(N'@Mode=2, here''s the details on existing indexes.', 0,1) WITH NOWAIT; + + IF (@ValidOutputLocation = 1 AND COALESCE(@OutputServerName, @OutputDatabaseName, @OutputSchemaName, @OutputTableName) IS NOT NULL) + BEGIN + IF @SchemaExists = 1 + BEGIN + IF @TableExists = 0 + BEGIN + SET @StringToExecute = + N'CREATE TABLE @@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [id] INT IDENTITY(1,1) NOT NULL, + [run_id] UNIQUEIDENTIFIER, + [run_datetime] DATETIME, + [server_name] NVARCHAR(128), + [database_name] NVARCHAR(128), + [schema_name] NVARCHAR(128), + [table_name] NVARCHAR(128), + [index_name] NVARCHAR(128), + [Drop_Tsql] NVARCHAR(MAX), + [Create_Tsql] NVARCHAR(MAX), + [index_id] INT, + [db_schema_object_indexid] NVARCHAR(500), + [object_type] NVARCHAR(15), + [index_definition] NVARCHAR(MAX), + [key_column_names_with_sort_order] NVARCHAR(MAX), + [count_key_columns] INT, + [include_column_names] NVARCHAR(MAX), + [count_included_columns] INT, + [secret_columns] NVARCHAR(MAX), + [count_secret_columns] INT, + [partition_key_column_name] NVARCHAR(MAX), + [filter_definition] NVARCHAR(MAX), + [is_indexed_view] BIT, + [is_primary_key] BIT, + [is_unique_constraint] BIT, + [is_XML] BIT, + [is_spatial] BIT, + [is_NC_columnstore] BIT, + [is_CX_columnstore] BIT, + [is_in_memory_oltp] BIT, + [is_disabled] BIT, + [is_hypothetical] BIT, + [is_padded] BIT, + [fill_factor] INT, + [is_referenced_by_foreign_key] BIT, + [last_user_seek] DATETIME, + [last_user_scan] DATETIME, + [last_user_lookup] DATETIME, + [last_user_update] DATETIME, + [total_reads] BIGINT, + [user_updates] BIGINT, + [reads_per_write] MONEY, + [index_usage_summary] NVARCHAR(200), + [total_singleton_lookup_count] BIGINT, + [total_range_scan_count] BIGINT, + [total_leaf_delete_count] BIGINT, + [total_leaf_update_count] BIGINT, + [index_op_stats] NVARCHAR(200), + [partition_count] INT, + [total_rows] BIGINT, + [total_reserved_MB] NUMERIC(29,2), + [total_reserved_LOB_MB] NUMERIC(29,2), + [total_reserved_row_overflow_MB] NUMERIC(29,2), + [index_size_summary] NVARCHAR(300), + [total_row_lock_count] BIGINT, + [total_row_lock_wait_count] BIGINT, + [total_row_lock_wait_in_ms] BIGINT, + [avg_row_lock_wait_in_ms] BIGINT, + [total_page_lock_count] BIGINT, + [total_page_lock_wait_count] BIGINT, + [total_page_lock_wait_in_ms] BIGINT, + [avg_page_lock_wait_in_ms] BIGINT, + [total_index_lock_promotion_attempt_count] BIGINT, + [total_index_lock_promotion_count] BIGINT, + [total_forwarded_fetch_count] BIGINT, + [data_compression_desc] NVARCHAR(4000), + [page_latch_wait_count] BIGINT, + [page_latch_wait_in_ms] BIGINT, + [page_io_latch_wait_count] BIGINT, + [page_io_latch_wait_in_ms] BIGINT, + [create_date] DATETIME, + [modify_date] DATETIME, + [more_info] NVARCHAR(500), + [display_order] INT, + CONSTRAINT [PK_ID_@@@RunID@@@] PRIMARY KEY CLUSTERED ([id] ASC) + );'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''',''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END; + END; /* @TableExists = 0 */ + + + -- Re-check that table now exists (if not we failed creating it) + SET @TableExists = NULL; + EXEC sp_executesql @TableExistsSql, N'@TableExists BIT OUTPUT', @TableExists OUTPUT; + + IF @TableExists = 1 + BEGIN + SET @StringToExecute = + N'INSERT @@@OutputServerName@@@.@@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [run_id], + [run_datetime], + [server_name], + [database_name], + [schema_name], + [table_name], + [index_name], + [Drop_Tsql], + [Create_Tsql], + [index_id], + [db_schema_object_indexid], + [object_type], + [index_definition], + [key_column_names_with_sort_order], + [count_key_columns], + [include_column_names], + [count_included_columns], + [secret_columns], + [count_secret_columns], + [partition_key_column_name], + [filter_definition], + [is_indexed_view], + [is_primary_key], + [is_unique_constraint], + [is_XML], + [is_spatial], + [is_NC_columnstore], + [is_CX_columnstore], + [is_in_memory_oltp], + [is_disabled], + [is_hypothetical], + [is_padded], + [fill_factor], + [is_referenced_by_foreign_key], + [last_user_seek], + [last_user_scan], + [last_user_lookup], + [last_user_update], + [total_reads], + [user_updates], + [reads_per_write], + [index_usage_summary], + [total_singleton_lookup_count], + [total_range_scan_count], + [total_leaf_delete_count], + [total_leaf_update_count], + [index_op_stats], + [partition_count], + [total_rows], + [total_reserved_MB], + [total_reserved_LOB_MB], + [total_reserved_row_overflow_MB], + [index_size_summary], + [total_row_lock_count], + [total_row_lock_wait_count], + [total_row_lock_wait_in_ms], + [avg_row_lock_wait_in_ms], + [total_page_lock_count], + [total_page_lock_wait_count], + [total_page_lock_wait_in_ms], + [avg_page_lock_wait_in_ms], + [total_index_lock_promotion_attempt_count], + [total_index_lock_promotion_count], + [total_forwarded_fetch_count], + [data_compression_desc], + [page_latch_wait_count], + [page_latch_wait_in_ms], + [page_io_latch_wait_count], + [page_io_latch_wait_in_ms], + [create_date], + [modify_date], + [more_info], + [display_order] + ) + SELECT ''@@@RunID@@@'', + ''@@@GETDATE@@@'', + ''@@@LocalServerName@@@'', + -- Below should be a copy/paste of the real query + -- Make sure all quotes are escaped + i.[database_name] AS [Database Name], + i.[schema_name] AS [Schema Name], + i.[object_name] AS [Object Name], + ISNULL(i.index_name, '''') AS [Index Name], + CASE + WHEN i.is_primary_key = 1 AND i.index_definition <> ''[HEAP]'' + THEN N''--ALTER TABLE '' + QUOTENAME(i.[database_name]) + N''.'' + QUOTENAME(i.[schema_name]) + N''.'' + QUOTENAME(i.[object_name]) + + N'' DROP CONSTRAINT '' + QUOTENAME(i.index_name) + N'';'' + WHEN i.is_primary_key = 0 AND i.is_unique_constraint = 1 AND i.index_definition <> ''[HEAP]'' + THEN N''--ALTER TABLE '' + QUOTENAME(i.[database_name]) + N''.'' + QUOTENAME(i.[schema_name]) + N''.'' + QUOTENAME(i.[object_name]) + + N'' DROP CONSTRAINT '' + QUOTENAME(i.index_name) + N'';'' + WHEN i.is_primary_key = 0 AND i.index_definition <> ''[HEAP]'' + THEN N''--DROP INDEX ''+ QUOTENAME(i.index_name) + N'' ON '' + QUOTENAME(i.[database_name]) + N''.'' + + QUOTENAME(i.[schema_name]) + N''.'' + QUOTENAME(i.[object_name]) + N'';'' + ELSE N'''' + END AS [Drop TSQL], + CASE + WHEN i.index_definition = ''[HEAP]'' THEN N'''' + ELSE N''--'' + ict.create_tsql END AS [Create TSQL], + CAST(i.index_id AS NVARCHAR(10))AS [Index ID], + db_schema_object_indexid AS [Details: schema.table.index(indexid)], + CASE WHEN index_id IN ( 1, 0 ) THEN ''TABLE'' + ELSE ''NonClustered'' + END AS [Object Type], + LEFT(index_definition,4000) AS [Definition: [Property]] ColumnName {datatype maxbytes}], + ISNULL(LTRIM(key_column_names_with_sort_order), '''') AS [Key Column Names With Sort], + ISNULL(count_key_columns, 0) AS [Count Key Columns], + ISNULL(include_column_names, '''') AS [Include Column Names], + ISNULL(count_included_columns,0) AS [Count Included Columns], + ISNULL(secret_columns,'''') AS [Secret Column Names], + ISNULL(count_secret_columns,0) AS [Count Secret Columns], + ISNULL(partition_key_column_name, '''') AS [Partition Key Column Name], + ISNULL(filter_definition, '''') AS [Filter Definition], + is_indexed_view AS [Is Indexed View], + is_primary_key AS [Is Primary Key], + is_unique_constraint AS [Is Unique Constraint], + is_XML AS [Is XML], + is_spatial AS [Is Spatial], + is_NC_columnstore AS [Is NC Columnstore], + is_CX_columnstore AS [Is CX Columnstore], + is_in_memory_oltp AS [Is In-Memory OLTP], + is_disabled AS [Is Disabled], + is_hypothetical AS [Is Hypothetical], + is_padded AS [Is Padded], + fill_factor AS [Fill Factor], + is_referenced_by_foreign_key AS [Is Reference by Foreign Key], + last_user_seek AS [Last User Seek], + last_user_scan AS [Last User Scan], + last_user_lookup AS [Last User Lookup], + last_user_update AS [Last User Update], + total_reads AS [Total Reads], + user_updates AS [User Updates], + reads_per_write AS [Reads Per Write], + index_usage_summary AS [Index Usage], + sz.total_singleton_lookup_count AS [Singleton Lookups], + sz.total_range_scan_count AS [Range Scans], + sz.total_leaf_delete_count AS [Leaf Deletes], + sz.total_leaf_update_count AS [Leaf Updates], + sz.index_op_stats AS [Index Op Stats], + sz.partition_count AS [Partition Count], + sz.total_rows AS [Rows], + sz.total_reserved_MB AS [Reserved MB], + sz.total_reserved_LOB_MB AS [Reserved LOB MB], + sz.total_reserved_row_overflow_MB AS [Reserved Row Overflow MB], + sz.index_size_summary AS [Index Size], + sz.total_row_lock_count AS [Row Lock Count], + sz.total_row_lock_wait_count AS [Row Lock Wait Count], + sz.total_row_lock_wait_in_ms AS [Row Lock Wait ms], + sz.avg_row_lock_wait_in_ms AS [Avg Row Lock Wait ms], + sz.total_page_lock_count AS [Page Lock Count], + sz.total_page_lock_wait_count AS [Page Lock Wait Count], + sz.total_page_lock_wait_in_ms AS [Page Lock Wait ms], + sz.avg_page_lock_wait_in_ms AS [Avg Page Lock Wait ms], + sz.total_index_lock_promotion_attempt_count AS [Lock Escalation Attempts], + sz.total_index_lock_promotion_count AS [Lock Escalations], + sz.total_forwarded_fetch_count AS [Forwarded Fetches], + sz.data_compression_desc AS [Data Compression], + sz.page_latch_wait_count, + sz.page_latch_wait_in_ms, + sz.page_io_latch_wait_count, + sz.page_io_latch_wait_in_ms, + i.create_date AS [Create Date], + i.modify_date AS [Modify Date], + more_info AS [More Info], + 1 AS [Display Order] + FROM #IndexSanity AS i + LEFT JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + LEFT JOIN #IndexCreateTsql AS ict ON i.index_sanity_id = ict.index_sanity_id + ORDER BY [Database Name], [Schema Name], [Object Name], [Index ID] + OPTION (RECOMPILE);'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputServerName@@@', @OutputServerName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@GETDATE@@@', GETDATE()); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@LocalServerName@@@', CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))); + EXEC(@StringToExecute); + END; /* @TableExists = 1 */ + ELSE + RAISERROR('Creation of the output table failed.', 16, 0); + END; /* @TableExists = 0 */ + ELSE + RAISERROR (N'Invalid schema name, data could not be saved.', 16, 0); + END; /* @ValidOutputLocation = 1 */ + ELSE + + IF(@OutputType <> 'NONE') + BEGIN + SELECT i.[database_name] AS [Database Name], + i.[schema_name] AS [Schema Name], + i.[object_name] AS [Object Name], + ISNULL(i.index_name, '') AS [Index Name], + CAST(i.index_id AS NVARCHAR(10))AS [Index ID], + db_schema_object_indexid AS [Details: schema.table.index(indexid)], + CASE WHEN index_id IN ( 1, 0 ) THEN 'TABLE' + ELSE 'NonClustered' + END AS [Object Type], + index_definition AS [Definition: [Property]] ColumnName {datatype maxbytes}], + ISNULL(LTRIM(key_column_names_with_sort_order), '') AS [Key Column Names With Sort], + ISNULL(count_key_columns, 0) AS [Count Key Columns], + ISNULL(include_column_names, '') AS [Include Column Names], + ISNULL(count_included_columns,0) AS [Count Included Columns], + ISNULL(secret_columns,'') AS [Secret Column Names], + ISNULL(count_secret_columns,0) AS [Count Secret Columns], + ISNULL(partition_key_column_name, '') AS [Partition Key Column Name], + ISNULL(filter_definition, '') AS [Filter Definition], + is_indexed_view AS [Is Indexed View], + is_primary_key AS [Is Primary Key], + is_unique_constraint AS [Is Unique Constraint] , + is_XML AS [Is XML], + is_spatial AS [Is Spatial], + is_NC_columnstore AS [Is NC Columnstore], + is_CX_columnstore AS [Is CX Columnstore], + is_in_memory_oltp AS [Is In-Memory OLTP], + is_disabled AS [Is Disabled], + is_hypothetical AS [Is Hypothetical], + is_padded AS [Is Padded], + fill_factor AS [Fill Factor], + is_referenced_by_foreign_key AS [Is Reference by Foreign Key], + last_user_seek AS [Last User Seek], + last_user_scan AS [Last User Scan], + last_user_lookup AS [Last User Lookup], + last_user_update AS [Last User Update], + total_reads AS [Total Reads], + user_updates AS [User Updates], + reads_per_write AS [Reads Per Write], + index_usage_summary AS [Index Usage], + sz.total_singleton_lookup_count AS [Singleton Lookups], + sz.total_range_scan_count AS [Range Scans], + sz.total_leaf_delete_count AS [Leaf Deletes], + sz.total_leaf_update_count AS [Leaf Updates], + sz.index_op_stats AS [Index Op Stats], + sz.partition_count AS [Partition Count], + sz.total_rows AS [Rows], + sz.total_reserved_MB AS [Reserved MB], + sz.total_reserved_LOB_MB AS [Reserved LOB MB], + sz.total_reserved_row_overflow_MB AS [Reserved Row Overflow MB], + sz.index_size_summary AS [Index Size], + sz.total_row_lock_count AS [Row Lock Count], + sz.total_row_lock_wait_count AS [Row Lock Wait Count], + sz.total_row_lock_wait_in_ms AS [Row Lock Wait ms], + sz.avg_row_lock_wait_in_ms AS [Avg Row Lock Wait ms], + sz.total_page_lock_count AS [Page Lock Count], + sz.total_page_lock_wait_count AS [Page Lock Wait Count], + sz.total_page_lock_wait_in_ms AS [Page Lock Wait ms], + sz.avg_page_lock_wait_in_ms AS [Avg Page Lock Wait ms], + sz.total_index_lock_promotion_attempt_count AS [Lock Escalation Attempts], + sz.total_index_lock_promotion_count AS [Lock Escalations], + sz.page_latch_wait_count AS [Page Latch Wait Count], + sz.page_latch_wait_in_ms AS [Page Latch Wait ms], + sz.page_io_latch_wait_count AS [Page IO Latch Wait Count], + sz.page_io_latch_wait_in_ms as [Page IO Latch Wait ms], + sz.total_forwarded_fetch_count AS [Forwarded Fetches], + sz.data_compression_desc AS [Data Compression], + i.create_date AS [Create Date], + i.modify_date AS [Modify Date], + more_info AS [More Info], + CASE + WHEN i.is_primary_key = 1 AND i.index_definition <> '[HEAP]' + THEN N'--ALTER TABLE ' + QUOTENAME(i.[database_name]) + N'.' + QUOTENAME(i.[schema_name]) + N'.' + QUOTENAME(i.[object_name]) + + N' DROP CONSTRAINT ' + QUOTENAME(i.index_name) + N';' + WHEN i.is_primary_key = 0 AND i.is_unique_constraint = 1 AND i.index_definition <> '[HEAP]' + THEN N'--ALTER TABLE ' + QUOTENAME(i.[database_name]) + N'.' + QUOTENAME(i.[schema_name]) + N'.' + QUOTENAME(i.[object_name]) + + N' DROP CONSTRAINT ' + QUOTENAME(i.index_name) + N';' + WHEN i.is_primary_key = 0 AND i.index_definition <> '[HEAP]' + THEN N'--DROP INDEX '+ QUOTENAME(i.index_name) + N' ON ' + QUOTENAME(i.[database_name]) + N'.' + + QUOTENAME(i.[schema_name]) + N'.' + QUOTENAME(i.[object_name]) + N';' + ELSE N'' + END AS [Drop TSQL], + CASE + WHEN i.index_definition = '[HEAP]' THEN N'' + ELSE N'--' + ict.create_tsql END AS [Create TSQL], + 1 AS [Display Order] + FROM #IndexSanity AS i --left join here so we don't lose disabled nc indexes + LEFT JOIN #IndexSanitySize AS sz ON i.index_sanity_id = sz.index_sanity_id + LEFT JOIN #IndexCreateTsql AS ict ON i.index_sanity_id = ict.index_sanity_id + ORDER BY /* Shout out to DHutmacher */ + /*DESC*/ + CASE WHEN @SortOrder = N'rows' AND @SortDirection = N'desc' THEN sz.total_rows ELSE NULL END DESC, + CASE WHEN @SortOrder = N'reserved_mb' AND @SortDirection = N'desc' THEN sz.total_reserved_MB ELSE NULL END DESC, + CASE WHEN @SortOrder = N'size' AND @SortDirection = N'desc' THEN sz.total_reserved_MB ELSE NULL END DESC, + CASE WHEN @SortOrder = N'reserved_lob_mb' AND @SortDirection = N'desc' THEN sz.total_reserved_LOB_MB ELSE NULL END DESC, + CASE WHEN @SortOrder = N'lob' AND @SortDirection = N'desc' THEN sz.total_reserved_LOB_MB ELSE NULL END DESC, + CASE WHEN @SortOrder = N'total_row_lock_wait_in_ms' AND @SortDirection = N'desc' THEN COALESCE(sz.total_row_lock_wait_in_ms,0) ELSE NULL END DESC, + CASE WHEN @SortOrder = N'total_page_lock_wait_in_ms' AND @SortDirection = N'desc' THEN COALESCE(sz.total_page_lock_wait_in_ms,0) ELSE NULL END DESC, + CASE WHEN @SortOrder = N'lock_time' AND @SortDirection = N'desc' THEN (COALESCE(sz.total_row_lock_wait_in_ms,0) + COALESCE(sz.total_page_lock_wait_in_ms,0)) ELSE NULL END DESC, + CASE WHEN @SortOrder = N'total_reads' AND @SortDirection = N'desc' THEN total_reads ELSE NULL END DESC, + CASE WHEN @SortOrder = N'reads' AND @SortDirection = N'desc' THEN total_reads ELSE NULL END DESC, + CASE WHEN @SortOrder = N'user_updates' AND @SortDirection = N'desc' THEN user_updates ELSE NULL END DESC, + CASE WHEN @SortOrder = N'writes' AND @SortDirection = N'desc' THEN user_updates ELSE NULL END DESC, + CASE WHEN @SortOrder = N'reads_per_write' AND @SortDirection = N'desc' THEN reads_per_write ELSE NULL END DESC, + CASE WHEN @SortOrder = N'ratio' AND @SortDirection = N'desc' THEN reads_per_write ELSE NULL END DESC, + CASE WHEN @SortOrder = N'forward_fetches' AND @SortDirection = N'desc' THEN sz.total_forwarded_fetch_count ELSE NULL END DESC, + CASE WHEN @SortOrder = N'fetches' AND @SortDirection = N'desc' THEN sz.total_forwarded_fetch_count ELSE NULL END DESC, + CASE WHEN @SortOrder = N'create_date' AND @SortDirection = N'desc' THEN CONVERT(DATETIME, i.create_date) ELSE NULL END DESC, + CASE WHEN @SortOrder = N'modify_date' AND @SortDirection = N'desc' THEN CONVERT(DATETIME, i.modify_date) ELSE NULL END DESC, + /*ASC*/ + CASE WHEN @SortOrder = N'rows' AND @SortDirection = N'asc' THEN sz.total_rows ELSE NULL END ASC, + CASE WHEN @SortOrder = N'reserved_mb' AND @SortDirection = N'asc' THEN sz.total_reserved_MB ELSE NULL END ASC, + CASE WHEN @SortOrder = N'size' AND @SortDirection = N'asc' THEN sz.total_reserved_MB ELSE NULL END ASC, + CASE WHEN @SortOrder = N'reserved_lob_mb' AND @SortDirection = N'asc' THEN sz.total_reserved_LOB_MB ELSE NULL END ASC, + CASE WHEN @SortOrder = N'lob' AND @SortDirection = N'asc' THEN sz.total_reserved_LOB_MB ELSE NULL END ASC, + CASE WHEN @SortOrder = N'total_row_lock_wait_in_ms' AND @SortDirection = N'asc' THEN COALESCE(sz.total_row_lock_wait_in_ms,0) ELSE NULL END ASC, + CASE WHEN @SortOrder = N'total_page_lock_wait_in_ms' AND @SortDirection = N'asc' THEN COALESCE(sz.total_page_lock_wait_in_ms,0) ELSE NULL END ASC, + CASE WHEN @SortOrder = N'lock_time' AND @SortDirection = N'asc' THEN (COALESCE(sz.total_row_lock_wait_in_ms,0) + COALESCE(sz.total_page_lock_wait_in_ms,0)) ELSE NULL END ASC, + CASE WHEN @SortOrder = N'total_reads' AND @SortDirection = N'asc' THEN total_reads ELSE NULL END ASC, + CASE WHEN @SortOrder = N'reads' AND @SortDirection = N'asc' THEN total_reads ELSE NULL END ASC, + CASE WHEN @SortOrder = N'user_updates' AND @SortDirection = N'asc' THEN user_updates ELSE NULL END ASC, + CASE WHEN @SortOrder = N'writes' AND @SortDirection = N'asc' THEN user_updates ELSE NULL END ASC, + CASE WHEN @SortOrder = N'reads_per_write' AND @SortDirection = N'asc' THEN reads_per_write ELSE NULL END ASC, + CASE WHEN @SortOrder = N'ratio' AND @SortDirection = N'asc' THEN reads_per_write ELSE NULL END ASC, + CASE WHEN @SortOrder = N'forward_fetches' AND @SortDirection = N'asc' THEN sz.total_forwarded_fetch_count ELSE NULL END ASC, + CASE WHEN @SortOrder = N'fetches' AND @SortDirection = N'asc' THEN sz.total_forwarded_fetch_count ELSE NULL END ASC, + CASE WHEN @SortOrder = N'create_date' AND @SortDirection = N'asc' THEN CONVERT(DATETIME, i.create_date) ELSE NULL END ASC, + CASE WHEN @SortOrder = N'modify_date' AND @SortDirection = N'asc' THEN CONVERT(DATETIME, i.modify_date) ELSE NULL END ASC, + i.[database_name], [Schema Name], [Object Name], [Index ID] + OPTION (RECOMPILE); + END; + + END; /* End @Mode=2 (index detail)*/ + + + + + + + + + ELSE IF (@Mode=3) /*Missing index Detail*/ + BEGIN + IF (@ValidOutputLocation = 1 AND COALESCE(@OutputServerName, @OutputDatabaseName, @OutputSchemaName, @OutputTableName) IS NOT NULL) + BEGIN + + IF NOT @SchemaExists = 1 + BEGIN + RAISERROR (N'Invalid schema name, data could not be saved.', 16, 0); + RETURN; + END + + IF @TableExists = 0 + BEGIN + SET @StringToExecute = + N'CREATE TABLE @@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [id] INT IDENTITY(1,1) NOT NULL, + [run_id] UNIQUEIDENTIFIER, + [run_datetime] DATETIME, + [server_name] NVARCHAR(128), + [database_name] NVARCHAR(128), + [schema_name] NVARCHAR(128), + [table_name] NVARCHAR(128), + [magic_benefit_number] BIGINT, + [missing_index_details] NVARCHAR(MAX), + [avg_total_user_cost] NUMERIC(29,4), + [avg_user_impact] NUMERIC(29,1), + [user_seeks] BIGINT, + [user_scans] BIGINT, + [unique_compiles] BIGINT, + [equality_columns_with_data_type] NVARCHAR(MAX), + [inequality_columns_with_data_type] NVARCHAR(MAX), + [included_columns_with_data_type] NVARCHAR(MAX), + [index_estimated_impact] NVARCHAR(256), + [create_tsql] NVARCHAR(MAX), + [more_info] NVARCHAR(600), + [display_order] INT, + [is_low] BIT, + [sample_query_plan] XML, + CONSTRAINT [PK_ID_@@@RunID@@@] PRIMARY KEY CLUSTERED ([id] ASC) + );'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + + IF @ValidOutputServer = 1 + BEGIN + SET @StringToExecute = REPLACE(@StringToExecute,'''',''''''); + EXEC('EXEC('''+@StringToExecute+''') AT ' + @OutputServerName); + END; + ELSE + BEGIN + EXEC(@StringToExecute); + END; + END; /* @TableExists = 0 */ + + -- Re-check that table now exists (if not we failed creating it) + SET @TableExists = NULL; + EXEC sp_executesql @TableExistsSql, N'@TableExists BIT OUTPUT', @TableExists OUTPUT; + + IF NOT @TableExists = 1 + BEGIN + RAISERROR('Creation of the output table failed.', 16, 0); + RETURN; + END; + SET @StringToExecute = + N'WITH create_date AS ( + SELECT i.database_id, + i.schema_name, + i.[object_id], + ISNULL(NULLIF(MAX(DATEDIFF(DAY, i.create_date, SYSDATETIME())), 0), 1) AS create_days + FROM #IndexSanity AS i + GROUP BY i.database_id, i.schema_name, i.object_id + ) + INSERT @@@OutputServerName@@@.@@@OutputDatabaseName@@@.@@@OutputSchemaName@@@.@@@OutputTableName@@@ + ( + [run_id], + [run_datetime], + [server_name], + [database_name], + [schema_name], + [table_name], + [magic_benefit_number], + [missing_index_details], + [avg_total_user_cost], + [avg_user_impact], + [user_seeks], + [user_scans], + [unique_compiles], + [equality_columns_with_data_type], + [inequality_columns_with_data_type], + [included_columns_with_data_type], + [index_estimated_impact], + [create_tsql], + [more_info], + [display_order], + [is_low], + [sample_query_plan] + ) + SELECT ''@@@RunID@@@'', + ''@@@GETDATE@@@'', + ''@@@LocalServerName@@@'', + -- Below should be a copy/paste of the real query + -- Make sure all quotes are escaped + -- NOTE! information line is skipped from output and the query below + -- NOTE! CTE block is above insert in the copied SQL + mi.database_name AS [Database Name], + mi.[schema_name] AS [Schema], + mi.table_name AS [Table], + CAST((mi.magic_benefit_number / CASE WHEN cd.create_days < @DaysUptime THEN cd.create_days ELSE @DaysUptime END) AS BIGINT) + AS [Magic Benefit Number], + mi.missing_index_details AS [Missing Index Details], + mi.avg_total_user_cost AS [Avg Query Cost], + mi.avg_user_impact AS [Est Index Improvement], + mi.user_seeks AS [Seeks], + mi.user_scans AS [Scans], + mi.unique_compiles AS [Compiles], + mi.equality_columns_with_data_type AS [Equality Columns], + mi.inequality_columns_with_data_type AS [Inequality Columns], + mi.included_columns_with_data_type AS [Included Columns], + mi.index_estimated_impact AS [Estimated Impact], + mi.create_tsql AS [Create TSQL], + mi.more_info AS [More Info], + 1 AS [Display Order], + mi.is_low, + mi.sample_query_plan AS [Sample Query Plan] + FROM #MissingIndexes AS mi + LEFT JOIN create_date AS cd + ON mi.[object_id] = cd.object_id + AND mi.database_id = cd.database_id + AND mi.schema_name = cd.schema_name + /* Minimum benefit threshold = 100k/day of uptime OR since table creation date, whichever is lower*/ + WHERE @ShowAllMissingIndexRequests=1 + OR (mi.magic_benefit_number / CASE WHEN cd.create_days < @DaysUptime THEN cd.create_days ELSE @DaysUptime END) >= 100000 + ORDER BY [Display Order] ASC, [Magic Benefit Number] DESC + OPTION (RECOMPILE);'; + + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputServerName@@@', @OutputServerName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputDatabaseName@@@', @OutputDatabaseName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputSchemaName@@@', @OutputSchemaName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@OutputTableName@@@', @OutputTableName); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@RunID@@@', @RunID); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@GETDATE@@@', GETDATE()); + SET @StringToExecute = REPLACE(@StringToExecute, '@@@LocalServerName@@@', CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128))); + EXEC sp_executesql @StringToExecute, N'@DaysUptime NUMERIC(23,2), @ShowAllMissingIndexRequests BIT', @DaysUptime = @DaysUptime, @ShowAllMissingIndexRequests = @ShowAllMissingIndexRequests; + + END; /* @ValidOutputLocation = 1 */ + ELSE + BEGIN + IF(@OutputType <> 'NONE') + BEGIN + WITH create_date AS ( + SELECT i.database_id, + i.schema_name, + i.[object_id], + ISNULL(NULLIF(MAX(DATEDIFF(DAY, i.create_date, SYSDATETIME())), 0), 1) AS create_days + FROM #IndexSanity AS i + GROUP BY i.database_id, i.schema_name, i.object_id + ) + SELECT + mi.database_name AS [Database Name], + mi.[schema_name] AS [Schema], + mi.table_name AS [Table], + CAST((mi.magic_benefit_number / CASE WHEN cd.create_days < @DaysUptime THEN cd.create_days ELSE @DaysUptime END) AS BIGINT) + AS [Magic Benefit Number], + mi.missing_index_details AS [Missing Index Details], + mi.avg_total_user_cost AS [Avg Query Cost], + mi.avg_user_impact AS [Est Index Improvement], + mi.user_seeks AS [Seeks], + mi.user_scans AS [Scans], + mi.unique_compiles AS [Compiles], + mi.equality_columns_with_data_type AS [Equality Columns], + mi.inequality_columns_with_data_type AS [Inequality Columns], + mi.included_columns_with_data_type AS [Included Columns], + mi.index_estimated_impact AS [Estimated Impact], + mi.create_tsql AS [Create TSQL], + mi.more_info AS [More Info], + 1 AS [Display Order], + mi.is_low, + mi.sample_query_plan AS [Sample Query Plan] + FROM #MissingIndexes AS mi + LEFT JOIN create_date AS cd + ON mi.[object_id] = cd.object_id + AND mi.database_id = cd.database_id + AND mi.schema_name = cd.schema_name + /* Minimum benefit threshold = 100k/day of uptime OR since table creation date, whichever is lower*/ + WHERE @ShowAllMissingIndexRequests=1 + OR (mi.magic_benefit_number / CASE WHEN cd.create_days < @DaysUptime THEN cd.create_days ELSE @DaysUptime END) >= 100000 + UNION ALL + SELECT + @ScriptVersionName, + N'From Your Community Volunteers' , + N'http://FirstResponderKit.org' , + 100000000000, + @DaysUptimeInsertValue, + NULL,NULL,NULL,NULL,NULL,NULL,NULL, + NULL, NULL, NULL, NULL, 0 AS [Display Order], NULL AS is_low, NULL + ORDER BY [Display Order] ASC, [Magic Benefit Number] DESC + OPTION (RECOMPILE); + END; + + + IF (@BringThePain = 1 + AND @DatabaseName IS NOT NULL + AND @GetAllDatabases = 0) + + BEGIN + EXEC sp_BlitzCache @SortOrder = 'sp_BlitzIndex', @DatabaseName = @DatabaseName, @BringThePain = 1, @QueryFilter = 'statement', @HideSummary = 1; + END; + + END; + + + + + + END; /* End @Mode=3 (index detail)*/ + +END /* End @TableName IS NULL (mode 0/1/2/3/4) */ +END TRY + +BEGIN CATCH + RAISERROR (N'Failure analyzing temp tables.', 0,1) WITH NOWAIT; + + SELECT @msg = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); + + RAISERROR (@msg, + @ErrorSeverity, + @ErrorState + ); + + WHILE @@trancount > 0 + ROLLBACK; + + RETURN; + END CATCH; +GO +IF OBJECT_ID('dbo.sp_BlitzLock') IS NULL +BEGIN + EXEC ('CREATE PROCEDURE dbo.sp_BlitzLock AS RETURN 0;'); +END; +GO + +ALTER PROCEDURE + dbo.sp_BlitzLock +( + @DatabaseName sysname = NULL, + @StartDate datetime = NULL, + @EndDate datetime = NULL, + @ObjectName nvarchar(1024) = NULL, + @StoredProcName nvarchar(1024) = NULL, + @AppName sysname = NULL, + @HostName sysname = NULL, + @LoginName sysname = NULL, + @EventSessionName sysname = N'system_health', + @TargetSessionType sysname = NULL, + @VictimsOnly bit = 0, + @Debug bit = 0, + @Help bit = 0, + @Version varchar(30) = NULL OUTPUT, + @VersionDate datetime = NULL OUTPUT, + @VersionCheckMode bit = 0, + @OutputDatabaseName sysname = NULL, + @OutputSchemaName sysname = N'dbo', /*ditto as below*/ + @OutputTableName sysname = N'BlitzLock', /*put a standard here no need to check later in the script*/ + @ExportToExcel bit = 0 +) +WITH RECOMPILE +AS +BEGIN + SET STATISTICS XML OFF; + SET NOCOUNT, XACT_ABORT ON; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + SELECT @Version = '8.16', @VersionDate = '20230820'; + + IF @VersionCheckMode = 1 + BEGIN + RETURN; + END; + + IF @Help = 1 + BEGIN + PRINT N' + /* + sp_BlitzLock from http://FirstResponderKit.org + + This script checks for and analyzes deadlocks from the system health session or a custom extended event path + + Variables you can use: + + @DatabaseName: If you want to filter to a specific database + + @StartDate: The date you want to start searching on, defaults to last 7 days + + @EndDate: The date you want to stop searching on, defaults to current date + + @ObjectName: If you want to filter to a specific able. + The object name has to be fully qualified ''Database.Schema.Table'' + + @StoredProcName: If you want to search for a single stored proc + The proc name has to be fully qualified ''Database.Schema.Sproc'' + + @AppName: If you want to filter to a specific application + + @HostName: If you want to filter to a specific host + + @LoginName: If you want to filter to a specific login + + @EventSessionName: If you want to point this at an XE session rather than the system health session. + + @TargetSessionType: Can be ''ring_buffer'' or ''event_file''. Leave NULL to auto-detect. + + @OutputDatabaseName: If you want to output information to a specific database + + @OutputSchemaName: Specify a schema name to output information to a specific Schema + + @OutputTableName: Specify table name to to output information to a specific table + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only SQL Server 2012 and newer is supported + - If your tables have weird characters in them (https://en.wikipedia.org/wiki/List_of_xml_and_HTML_character_entity_references) you may get errors trying to parse the xml. + I took a long look at this one, and: + 1) Trying to account for all the weird places these could crop up is a losing effort. + 2) Replace is slow af on lots of xml. + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */'; + + RETURN; + END; /* @Help = 1 */ + + /*Declare local variables used in the procudure*/ + DECLARE + @DatabaseId int = + DB_ID(@DatabaseName), + @ProductVersion nvarchar(128) = + CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(128)), + @ProductVersionMajor float = + SUBSTRING + ( + CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(128)), + 1, + CHARINDEX('.', CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(128))) + 1 + ), + @ProductVersionMinor int = + PARSENAME + ( + CONVERT + ( + varchar(32), + CAST(SERVERPROPERTY('ProductVersion') AS nvarchar(128)) + ), + 2 + ), + @ObjectFullName nvarchar(MAX) = N'', + @Azure bit = + CASE + WHEN + ( + SELECT + CONVERT + ( + integer, + SERVERPROPERTY('EngineEdition') + ) + ) = 5 + THEN 1 + ELSE 0 + END, + @RDS bit = + CASE + WHEN LEFT(CAST(COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),SERVERPROPERTY('ServerName')) AS varchar(8000)), 8) <> 'EC2AMAZ-' + AND LEFT(CAST(SERVERPROPERTY('MachineName') AS varchar(8000)), 8) <> 'EC2AMAZ-' + AND DB_ID('rdsadmin') IS NULL + THEN 0 + ELSE 1 + END, + @d varchar(40) = '', + @StringToExecute nvarchar(4000) = N'', + @StringToExecuteParams nvarchar(500) = N'', + @r sysname = NULL, + @OutputTableFindings nvarchar(100) = N'[BlitzLockFindings]', + @DeadlockCount int = 0, + @ServerName sysname = @@SERVERNAME, + @OutputDatabaseCheck bit = -1, + @SessionId int = 0, + @TargetSessionId int = 0, + @FileName nvarchar(4000) = N'', + @inputbuf_bom nvarchar(1) = CONVERT(nvarchar(1), 0x0a00, 0), + @deadlock_result nvarchar(MAX) = N'', + @StartDateOriginal datetime = @StartDate, + @EndDateOriginal datetime = @EndDate, + @StartDateUTC datetime, + @EndDateUTC datetime; + + /*Temporary objects used in the procedure*/ + DECLARE + @sysAssObjId AS table + ( + database_id int, + partition_id bigint, + schema_name sysname, + table_name sysname + ); + + CREATE TABLE + #x + ( + x xml NOT NULL + DEFAULT N'x' + ); + + CREATE TABLE + #deadlock_data + ( + deadlock_xml xml NOT NULL + DEFAULT N'x' + ); + + CREATE TABLE + #t + ( + id int NOT NULL + ); + + CREATE TABLE + #deadlock_findings + ( + id int IDENTITY PRIMARY KEY, + check_id int NOT NULL, + database_name nvarchar(256), + object_name nvarchar(1000), + finding_group nvarchar(100), + finding nvarchar(4000), + sort_order bigint + ); + + /*Set these to some sane defaults if NULLs are passed in*/ + /*Normally I'd hate this, but we RECOMPILE everything*/ + + SELECT + @StartDate = + CASE + WHEN @StartDate IS NULL + THEN + DATEADD + ( + MINUTE, + DATEDIFF + ( + MINUTE, + SYSDATETIME(), + GETUTCDATE() + ), + DATEADD + ( + DAY, + -7, + SYSDATETIME() + ) + ) + ELSE + DATEADD + ( + MINUTE, + DATEDIFF + ( + MINUTE, + SYSDATETIME(), + GETUTCDATE() + ), + @StartDate + ) + END, + @EndDate = + CASE + WHEN @EndDate IS NULL + THEN + DATEADD + ( + MINUTE, + DATEDIFF + ( + MINUTE, + SYSDATETIME(), + GETUTCDATE() + ), + SYSDATETIME() + ) + ELSE + DATEADD + ( + MINUTE, + DATEDIFF + ( + MINUTE, + SYSDATETIME(), + GETUTCDATE() + ), + @EndDate + ) + END; + + SELECT + @StartDateUTC = @StartDate, + @EndDateUTC = @EndDate; + + IF @Azure = 0 + BEGIN + IF NOT EXISTS + ( + SELECT + 1/0 + FROM sys.server_event_sessions AS ses + JOIN sys.dm_xe_sessions AS dxs + ON dxs.name = ses.name + WHERE ses.name = @EventSessionName + AND dxs.create_time IS NOT NULL + ) + BEGIN + RAISERROR('A session with the name %s does not exist or is not currently active.', 11, 1, @EventSessionName) WITH NOWAIT; + RETURN; + END; + END; + + IF @Azure = 1 + BEGIN + IF NOT EXISTS + ( + SELECT + 1/0 + FROM sys.database_event_sessions AS ses + JOIN sys.dm_xe_database_sessions AS dxs + ON dxs.name = ses.name + WHERE ses.name = @EventSessionName + AND dxs.create_time IS NOT NULL + ) + BEGIN + RAISERROR('A session with the name %s does not exist or is not currently active.', 11, 1, @EventSessionName) WITH NOWAIT; + RETURN; + END; + END; + + IF @OutputDatabaseName IS NOT NULL + BEGIN /*IF databaseName is set, do some sanity checks and put [] around def.*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('@OutputDatabaseName set to %s, checking validity at %s', 0, 1, @OutputDatabaseName, @d) WITH NOWAIT; + + IF NOT EXISTS + ( + SELECT + 1/0 + FROM sys.databases AS d + WHERE d.name = @OutputDatabaseName + ) /*If database is invalid raiserror and set bitcheck*/ + BEGIN + RAISERROR('Database Name (%s) for output of table is invalid please, Output to Table will not be performed', 0, 1, @OutputDatabaseName) WITH NOWAIT; + SET @OutputDatabaseCheck = -1; /* -1 invalid/false, 0 = good/true */ + END; + ELSE + BEGIN + SET @OutputDatabaseCheck = 0; + + SELECT + @StringToExecute = + N'SELECT @r = o.name FROM ' + + @OutputDatabaseName + + N'.sys.objects AS o WHERE o.type_desc = N''USER_TABLE'' AND o.name = ' + + QUOTENAME + ( + @OutputTableName, + N'''' + ) + + N' AND o.schema_id = SCHEMA_ID(' + + QUOTENAME + ( + @OutputSchemaName, + N'''' + ) + + N');', + @StringToExecuteParams = + N'@r sysname OUTPUT'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute, + @StringToExecuteParams, + @r OUTPUT; + + IF @Debug = 1 + BEGIN + RAISERROR('@r is set to: %s for schema name %s and table name %s', 0, 1, @r, @OutputSchemaName, @OutputTableName) WITH NOWAIT; + END; + + /*protection spells*/ + SELECT + @ObjectFullName = + QUOTENAME(@OutputDatabaseName) + + N'.' + + QUOTENAME(@OutputSchemaName) + + N'.' + + QUOTENAME(@OutputTableName), + @OutputDatabaseName = + QUOTENAME(@OutputDatabaseName), + @OutputTableName = + QUOTENAME(@OutputTableName), + @OutputSchemaName = + QUOTENAME(@OutputSchemaName); + + IF (@r IS NOT NULL) /*if it is not null, there is a table, so check for newly added columns*/ + BEGIN + /* If the table doesn't have the new spid column, add it. See Github #3101. */ + SET @StringToExecute = + N'IF NOT EXISTS (SELECT 1/0 FROM ' + + @OutputDatabaseName + + N'.sys.all_columns AS o WHERE o.object_id = (OBJECT_ID(''' + + @ObjectFullName + + N''')) AND o.name = N''spid'') + /*Add spid column*/ + ALTER TABLE ' + + @ObjectFullName + + N' ADD spid smallint NULL;'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /* If the table doesn't have the new wait_resource column, add it. See Github #3101. */ + SET @StringToExecute = + N'IF NOT EXISTS (SELECT 1/0 FROM ' + + @OutputDatabaseName + + N'.sys.all_columns AS o WHERE o.object_id = (OBJECT_ID(''' + + @ObjectFullName + + N''')) AND o.name = N''wait_resource'') + /*Add wait_resource column*/ + ALTER TABLE ' + + @ObjectFullName + + N' ADD wait_resource nvarchar(MAX) NULL;'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /* If the table doesn't have the new client option column, add it. See Github #3101. */ + SET @StringToExecute = + N'IF NOT EXISTS (SELECT 1/0 FROM ' + + @OutputDatabaseName + + N'.sys.all_columns AS o WHERE o.object_id = (OBJECT_ID(''' + + @ObjectFullName + + N''')) AND o.name = N''client_option_1'') + /*Add wait_resource column*/ + ALTER TABLE ' + + @ObjectFullName + + N' ADD client_option_1 varchar(500) NULL;'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /* If the table doesn't have the new client option column, add it. See Github #3101. */ + SET @StringToExecute = + N'IF NOT EXISTS (SELECT 1/0 FROM ' + + @OutputDatabaseName + + N'.sys.all_columns AS o WHERE o.object_id = (OBJECT_ID(''' + + @ObjectFullName + + N''')) AND o.name = N''client_option_2'') + /*Add wait_resource column*/ + ALTER TABLE ' + + @ObjectFullName + + N' ADD client_option_2 varchar(500) NULL;'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /* If the table doesn't have the new lock mode column, add it. See Github #3101. */ + SET @StringToExecute = + N'IF NOT EXISTS (SELECT 1/0 FROM ' + + @OutputDatabaseName + + N'.sys.all_columns AS o WHERE o.object_id = (OBJECT_ID(''' + + @ObjectFullName + + N''')) AND o.name = N''lock_mode'') + /*Add wait_resource column*/ + ALTER TABLE ' + + @ObjectFullName + + N' ADD lock_mode nvarchar(256) NULL;'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /* If the table doesn't have the new status column, add it. See Github #3101. */ + SET @StringToExecute = + N'IF NOT EXISTS (SELECT 1/0 FROM ' + + @OutputDatabaseName + + N'.sys.all_columns AS o WHERE o.object_id = (OBJECT_ID(''' + + @ObjectFullName + + N''')) AND o.name = N''status'') + /*Add wait_resource column*/ + ALTER TABLE ' + + @ObjectFullName + + N' ADD status nvarchar(256) NULL;'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + END; + ELSE /* end if @r is not null. if it is null there is no table, create it from above execution */ + BEGIN + SELECT + @StringToExecute = + N'USE ' + + @OutputDatabaseName + + N'; + CREATE TABLE ' + + @OutputSchemaName + + N'.' + + @OutputTableName + + N' ( + ServerName nvarchar(256), + deadlock_type nvarchar(256), + event_date datetime, + database_name nvarchar(256), + spid smallint, + deadlock_group nvarchar(256), + query xml, + object_names xml, + isolation_level nvarchar(256), + owner_mode nvarchar(256), + waiter_mode nvarchar(256), + lock_mode nvarchar(256), + transaction_count bigint, + client_option_1 varchar(500), + client_option_2 varchar(500), + login_name nvarchar(256), + host_name nvarchar(256), + client_app nvarchar(1024), + wait_time bigint, + wait_resource nvarchar(max), + priority smallint, + log_used bigint, + last_tran_started datetime, + last_batch_started datetime, + last_batch_completed datetime, + transaction_name nvarchar(256), + status nvarchar(256), + owner_waiter_type nvarchar(256), + owner_activity nvarchar(256), + owner_waiter_activity nvarchar(256), + owner_merging nvarchar(256), + owner_spilling nvarchar(256), + owner_waiting_to_close nvarchar(256), + waiter_waiter_type nvarchar(256), + waiter_owner_activity nvarchar(256), + waiter_waiter_activity nvarchar(256), + waiter_merging nvarchar(256), + waiter_spilling nvarchar(256), + waiter_waiting_to_close nvarchar(256), + deadlock_graph xml + )'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /*table created.*/ + SELECT + @StringToExecute = + N'SELECT @r = o.name FROM ' + + @OutputDatabaseName + + N'.sys.objects AS o + WHERE o.type_desc = N''USER_TABLE'' + AND o.name = N''BlitzLockFindings''', + @StringToExecuteParams = + N'@r sysname OUTPUT'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute, + @StringToExecuteParams, + @r OUTPUT; + + IF (@r IS NULL) /*if table does not exist*/ + BEGIN + SELECT + @OutputTableFindings = + QUOTENAME(N'BlitzLockFindings'), + @StringToExecute = + N'USE ' + + @OutputDatabaseName + + N'; + CREATE TABLE ' + + @OutputSchemaName + + N'.' + + @OutputTableFindings + + N' ( + ServerName nvarchar(256), + check_id INT, + database_name nvarchar(256), + object_name nvarchar(1000), + finding_group nvarchar(100), + finding nvarchar(4000) + );'; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + END; + END; + + /*create synonym for deadlockfindings.*/ + IF EXISTS + ( + SELECT + 1/0 + FROM sys.objects AS o + WHERE o.name = N'DeadlockFindings' + AND o.type_desc = N'SYNONYM' + ) + BEGIN + RAISERROR('Found synonym DeadlockFindings, dropping', 0, 1) WITH NOWAIT; + DROP SYNONYM DeadlockFindings; + END; + + RAISERROR('Creating synonym DeadlockFindings', 0, 1) WITH NOWAIT; + SET @StringToExecute = + N'CREATE SYNONYM DeadlockFindings FOR ' + + @OutputDatabaseName + + N'.' + + @OutputSchemaName + + N'.' + + @OutputTableFindings; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + /*create synonym for deadlock table.*/ + IF EXISTS + ( + SELECT + 1/0 + FROM sys.objects AS o + WHERE o.name = N'DeadLockTbl' + AND o.type_desc = N'SYNONYM' + ) + BEGIN + RAISERROR('Found synonym DeadLockTbl, dropping', 0, 1) WITH NOWAIT; + DROP SYNONYM DeadLockTbl; + END; + + RAISERROR('Creating synonym DeadLockTbl', 0, 1) WITH NOWAIT; + SET @StringToExecute = + N'CREATE SYNONYM DeadLockTbl FOR ' + + @OutputDatabaseName + + N'.' + + @OutputSchemaName + + N'.' + + @OutputTableName; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + END; + END; + + /* WITH ROWCOUNT doesn't work on Amazon RDS - see: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2037 */ + IF @RDS = 0 + BEGIN; + BEGIN TRY; + RAISERROR('@RDS = 0, updating #t with high row and page counts', 0, 1) WITH NOWAIT; + UPDATE STATISTICS + #t + WITH + ROWCOUNT = 9223372036854775807, + PAGECOUNT = 9223372036854775807; + END TRY + BEGIN CATCH; + /* Misleading error returned, if run without permissions to update statistics the error returned is "Cannot find object". + Catching specific error, and returning message with better info. If any other error is returned, then throw as normal */ + IF (ERROR_NUMBER() = 1088) + BEGIN; + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Cannot run UPDATE STATISTICS on a #temp table without db_owner or sysadmin permissions', 0, 1) WITH NOWAIT; + END; + ELSE + BEGIN; + THROW; + END; + END CATCH; + END; + + /*If @TargetSessionType, we need to figure out if it's ring buffer or event file*/ + /*Azure has differently named views, so we need to separate. Thanks, Azure.*/ + + IF + ( + @Azure = 0 + AND @TargetSessionType IS NULL + ) + BEGIN + RAISERROR('@TargetSessionType is NULL, assigning for non-Azure instance', 0, 1) WITH NOWAIT; + + SELECT TOP (1) + @TargetSessionType = t.target_name + FROM sys.dm_xe_sessions AS s + JOIN sys.dm_xe_session_targets AS t + ON s.address = t.event_session_address + WHERE s.name = @EventSessionName + AND t.target_name IN (N'event_file', N'ring_buffer') + ORDER BY t.target_name + OPTION(RECOMPILE); + + RAISERROR('@TargetSessionType assigned as %s for non-Azure', 0, 1, @TargetSessionType) WITH NOWAIT; + END; + + IF + ( + @Azure = 1 + AND @TargetSessionType IS NULL + ) + BEGIN + RAISERROR('@TargetSessionType is NULL, assigning for Azure instance', 0, 1) WITH NOWAIT; + + SELECT TOP (1) + @TargetSessionType = t.target_name + FROM sys.dm_xe_database_sessions AS s + JOIN sys.dm_xe_database_session_targets AS t + ON s.address = t.event_session_address + WHERE s.name = @EventSessionName + AND t.target_name IN (N'event_file', N'ring_buffer') + ORDER BY t.target_name + OPTION(RECOMPILE); + + RAISERROR('@TargetSessionType assigned as %s for Azure', 0, 1, @TargetSessionType) WITH NOWAIT; + END; + + + /*The system health stuff gets handled different from user extended events.*/ + /*These next sections deal with user events, dependent on target.*/ + + /*If ring buffers*/ + IF + ( + @TargetSessionType LIKE N'ring%' + AND @EventSessionName NOT LIKE N'system_health%' + ) + BEGIN + IF @Azure = 0 + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('@TargetSessionType is ring_buffer, inserting XML for non-Azure at %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #x WITH(TABLOCKX) + ( + x + ) + SELECT + x = TRY_CAST(t.target_data AS xml) + FROM sys.dm_xe_session_targets AS t + JOIN sys.dm_xe_sessions AS s + ON s.address = t.event_session_address + WHERE s.name = @EventSessionName + AND t.target_name = N'ring_buffer' + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + IF @Azure = 1 + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('@TargetSessionType is ring_buffer, inserting XML for Azure at %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #x WITH(TABLOCKX) + ( + x + ) + SELECT + x = TRY_CAST(t.target_data AS xml) + FROM sys.dm_xe_database_session_targets AS t + JOIN sys.dm_xe_database_sessions AS s + ON s.address = t.event_session_address + WHERE s.name = @EventSessionName + AND t.target_name = N'ring_buffer' + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + END; + + + /*If event file*/ + IF + ( + @TargetSessionType LIKE N'event%' + AND @EventSessionName NOT LIKE N'system_health%' + ) + BEGIN + IF @Azure = 0 + BEGIN + RAISERROR('@TargetSessionType is event_file, assigning XML for non-Azure', 0, 1) WITH NOWAIT; + + SELECT + @SessionId = t.event_session_id, + @TargetSessionId = t.target_id + FROM sys.server_event_session_targets AS t + JOIN sys.server_event_sessions AS s + ON s.event_session_id = t.event_session_id + WHERE t.name = @TargetSessionType + AND s.name = @EventSessionName + OPTION(RECOMPILE); + + /*We get the file name automatically, here*/ + RAISERROR('Assigning @FileName...', 0, 1) WITH NOWAIT; + SELECT + @FileName = + CASE + WHEN f.file_name LIKE N'%.xel' + THEN REPLACE(f.file_name, N'.xel', N'*.xel') + ELSE f.file_name + N'*.xel' + END + FROM + ( + SELECT + file_name = + CONVERT(nvarchar(4000), f.value) + FROM sys.server_event_session_fields AS f + WHERE f.event_session_id = @SessionId + AND f.object_id = @TargetSessionId + AND f.name = N'filename' + ) AS f + OPTION(RECOMPILE); + END; + + IF @Azure = 1 + BEGIN + RAISERROR('@TargetSessionType is event_file, assigning XML for Azure', 0, 1) WITH NOWAIT; + SELECT + @SessionId = + t.event_session_address, + @TargetSessionId = + t.target_name + FROM sys.dm_xe_database_session_targets t + JOIN sys.dm_xe_database_sessions s + ON s.address = t.event_session_address + WHERE t.target_name = @TargetSessionType + AND s.name = @EventSessionName + OPTION(RECOMPILE); + + /*We get the file name automatically, here*/ + RAISERROR('Assigning @FileName...', 0, 1) WITH NOWAIT; + SELECT + @FileName = + CASE + WHEN f.file_name LIKE N'%.xel' + THEN REPLACE(f.file_name, N'.xel', N'*.xel') + ELSE f.file_name + N'*.xel' + END + FROM + ( + SELECT + file_name = + CONVERT(nvarchar(4000), f.value) + FROM sys.server_event_session_fields AS f + WHERE f.event_session_id = @SessionId + AND f.object_id = @TargetSessionId + AND f.name = N'filename' + ) AS f + OPTION(RECOMPILE); + END; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Reading for event_file %s', 0, 1, @FileName) WITH NOWAIT; + + INSERT + #x WITH(TABLOCKX) + ( + x + ) + SELECT + x = TRY_CAST(f.event_data AS xml) + FROM sys.fn_xe_file_target_read_file(@FileName, NULL, NULL, NULL) AS f + LEFT JOIN #t AS t + ON 1 = 1 + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + /*The XML is parsed differently if it comes from the event file or ring buffer*/ + + /*If ring buffers*/ + IF + ( + @TargetSessionType LIKE N'ring%' + AND @EventSessionName NOT LIKE N'system_health%' + ) + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Inserting to #deadlock_data for ring buffer data', 0, 1) WITH NOWAIT; + + INSERT + #deadlock_data WITH(TABLOCKX) + ( + deadlock_xml + ) + SELECT + deadlock_xml = + e.x.query(N'.') + FROM #x AS x + LEFT JOIN #t AS t + ON 1 = 1 + CROSS APPLY x.x.nodes('/RingBufferTarget/event') AS e(x) + WHERE + ( + e.x.exist('@name[ .= "xml_deadlock_report"]') = 1 + OR e.x.exist('@name[ .= "database_xml_deadlock_report"]') = 1 + OR e.x.exist('@name[ .= "xml_deadlock_report_filtered"]') = 1 + ) + AND e.x.exist('@timestamp[. >= sql:variable("@StartDate") and .< sql:variable("@EndDate")]') = 1 + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + /*If event file*/ + IF + ( + @TargetSessionType LIKE N'event_file%' + AND @EventSessionName NOT LIKE N'system_health%' + ) + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Inserting to #deadlock_data for event file data', 0, 1) WITH NOWAIT; + + IF @Debug = 1 BEGIN SET STATISTICS XML ON; END; + + INSERT + #deadlock_data WITH(TABLOCKX) + ( + deadlock_xml + ) + SELECT + deadlock_xml = + e.x.query('.') + FROM #x AS x + LEFT JOIN #t AS t + ON 1 = 1 + CROSS APPLY x.x.nodes('/event') AS e(x) + WHERE + ( + e.x.exist('@name[ .= "xml_deadlock_report"]') = 1 + OR e.x.exist('@name[ .= "database_xml_deadlock_report"]') = 1 + OR e.x.exist('@name[ .= "xml_deadlock_report_filtered"]') = 1 + ) + AND e.x.exist('@timestamp[. >= sql:variable("@StartDate") and .< sql:variable("@EndDate")]') = 1 + OPTION(RECOMPILE); + + IF @Debug = 1 BEGIN SET STATISTICS XML OFF; END; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + /*This section deals with event file*/ + IF + ( + @TargetSessionType LIKE N'event%' + AND @EventSessionName LIKE N'system_health%' + ) + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Grab the initial set of xml to parse at %s', 0, 1, @d) WITH NOWAIT; + + IF @Debug = 1 BEGIN SET STATISTICS XML ON; END; + + SELECT + xml.deadlock_xml + INTO #xml + FROM + ( + SELECT + deadlock_xml = + TRY_CAST(fx.event_data AS xml) + FROM sys.fn_xe_file_target_read_file(N'system_health*.xel', NULL, NULL, NULL) AS fx + LEFT JOIN #t AS t + ON 1 = 1 + WHERE fx.object_name = N'xml_deadlock_report' + ) AS xml + CROSS APPLY xml.deadlock_xml.nodes('/event') AS e(x) + WHERE 1 = 1 + AND e.x.exist('@timestamp[. >= sql:variable("@StartDate") and .< sql:variable("@EndDate")]') = 1 + OPTION(RECOMPILE); + + INSERT + #deadlock_data WITH(TABLOCKX) + SELECT + deadlock_xml = + xml.deadlock_xml + FROM #xml AS xml + LEFT JOIN #t AS t + ON 1 = 1 + WHERE xml.deadlock_xml IS NOT NULL + OPTION(RECOMPILE); + + IF @Debug = 1 BEGIN SET STATISTICS XML OFF; END; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + /*Parse process and input buffer xml*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Initial Parse process and input buffer xml %s', 0, 1, @d) WITH NOWAIT; + + SELECT + d1.deadlock_xml, + event_date = d1.deadlock_xml.value('(event/@timestamp)[1]', 'datetime2'), + victim_id = d1.deadlock_xml.value('(//deadlock/victim-list/victimProcess/@id)[1]', 'nvarchar(256)'), + is_parallel = d1.deadlock_xml.exist('//deadlock/resource-list/exchangeEvent'), + is_parallel_batch = d1.deadlock_xml.exist('//deadlock/resource-list/SyncPoint'), + deadlock_graph = d1.deadlock_xml.query('/event/data/value/deadlock') + INTO #dd + FROM #deadlock_data AS d1 + LEFT JOIN #t AS t + ON 1 = 1 + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Final Parse process and input buffer xml %s', 0, 1, @d) WITH NOWAIT; + + SELECT + q.event_date, + q.victim_id, + is_parallel = + CONVERT(bit, q.is_parallel), + q.deadlock_graph, + q.id, + q.spid, + q.database_id, + database_name = + ISNULL + ( + DB_NAME(q.database_id), + N'UNKNOWN' + ), + q.current_database_name, + q.priority, + q.log_used, + q.wait_resource, + q.wait_time, + q.transaction_name, + q.last_tran_started, + q.last_batch_started, + q.last_batch_completed, + q.lock_mode, + q.status, + q.transaction_count, + q.client_app, + q.host_name, + q.login_name, + q.isolation_level, + client_option_1 = + SUBSTRING + ( + CASE WHEN q.clientoption1 & 1 = 1 THEN ', DISABLE_DEF_CNST_CHECK' ELSE '' END + + CASE WHEN q.clientoption1 & 2 = 2 THEN ', IMPLICIT_TRANSACTIONS' ELSE '' END + + CASE WHEN q.clientoption1 & 4 = 4 THEN ', CURSOR_CLOSE_ON_COMMIT' ELSE '' END + + CASE WHEN q.clientoption1 & 8 = 8 THEN ', ANSI_WARNINGS' ELSE '' END + + CASE WHEN q.clientoption1 & 16 = 16 THEN ', ANSI_PADDING' ELSE '' END + + CASE WHEN q.clientoption1 & 32 = 32 THEN ', ANSI_NULLS' ELSE '' END + + CASE WHEN q.clientoption1 & 64 = 64 THEN ', ARITHABORT' ELSE '' END + + CASE WHEN q.clientoption1 & 128 = 128 THEN ', ARITHIGNORE' ELSE '' END + + CASE WHEN q.clientoption1 & 256 = 256 THEN ', QUOTED_IDENTIFIER' ELSE '' END + + CASE WHEN q.clientoption1 & 512 = 512 THEN ', NOCOUNT' ELSE '' END + + CASE WHEN q.clientoption1 & 1024 = 1024 THEN ', ANSI_NULL_DFLT_ON' ELSE '' END + + CASE WHEN q.clientoption1 & 2048 = 2048 THEN ', ANSI_NULL_DFLT_OFF' ELSE '' END + + CASE WHEN q.clientoption1 & 4096 = 4096 THEN ', CONCAT_NULL_YIELDS_NULL' ELSE '' END + + CASE WHEN q.clientoption1 & 8192 = 8192 THEN ', NUMERIC_ROUNDABORT' ELSE '' END + + CASE WHEN q.clientoption1 & 16384 = 16384 THEN ', XACT_ABORT' ELSE '' END, + 3, + 500 + ), + client_option_2 = + SUBSTRING + ( + CASE WHEN q.clientoption2 & 1024 = 1024 THEN ', DB CHAINING' ELSE '' END + + CASE WHEN q.clientoption2 & 2048 = 2048 THEN ', NUMERIC ROUNDABORT' ELSE '' END + + CASE WHEN q.clientoption2 & 4096 = 4096 THEN ', ARITHABORT' ELSE '' END + + CASE WHEN q.clientoption2 & 8192 = 8192 THEN ', ANSI PADDING' ELSE '' END + + CASE WHEN q.clientoption2 & 16384 = 16384 THEN ', ANSI NULL DEFAULT' ELSE '' END + + CASE WHEN q.clientoption2 & 65536 = 65536 THEN ', CONCAT NULL YIELDS NULL' ELSE '' END + + CASE WHEN q.clientoption2 & 131072 = 131072 THEN ', RECURSIVE TRIGGERS' ELSE '' END + + CASE WHEN q.clientoption2 & 1048576 = 1048576 THEN ', DEFAULT TO LOCAL CURSOR' ELSE '' END + + CASE WHEN q.clientoption2 & 8388608 = 8388608 THEN ', QUOTED IDENTIFIER' ELSE '' END + + CASE WHEN q.clientoption2 & 16777216 = 16777216 THEN ', AUTO CREATE STATISTICS' ELSE '' END + + CASE WHEN q.clientoption2 & 33554432 = 33554432 THEN ', CURSOR CLOSE ON COMMIT' ELSE '' END + + CASE WHEN q.clientoption2 & 67108864 = 67108864 THEN ', ANSI NULLS' ELSE '' END + + CASE WHEN q.clientoption2 & 268435456 = 268435456 THEN ', ANSI WARNINGS' ELSE '' END + + CASE WHEN q.clientoption2 & 536870912 = 536870912 THEN ', FULL TEXT ENABLED' ELSE '' END + + CASE WHEN q.clientoption2 & 1073741824 = 1073741824 THEN ', AUTO UPDATE STATISTICS' ELSE '' END + + CASE WHEN q.clientoption2 & 1469283328 = 1469283328 THEN ', ALL SETTABLE OPTIONS' ELSE '' END, + 3, + 500 + ), + q.process_xml + INTO #deadlock_process + FROM + ( + SELECT + dd.deadlock_xml, + event_date = + DATEADD + ( + MINUTE, + DATEDIFF + ( + MINUTE, + GETUTCDATE(), + SYSDATETIME() + ), + dd.event_date + ), + dd.victim_id, + is_parallel = + CONVERT(tinyint, dd.is_parallel) + + CONVERT(tinyint, dd.is_parallel_batch), + dd.deadlock_graph, + id = ca.dp.value('@id', 'nvarchar(256)'), + spid = ca.dp.value('@spid', 'smallint'), + database_id = ca.dp.value('@currentdb', 'bigint'), + current_database_name = ca.dp.value('@currentdbname', 'nvarchar(256)'), + priority = ca.dp.value('@priority', 'smallint'), + log_used = ca.dp.value('@logused', 'bigint'), + wait_resource = ca.dp.value('@waitresource', 'nvarchar(256)'), + wait_time = ca.dp.value('@waittime', 'bigint'), + transaction_name = ca.dp.value('@transactionname', 'nvarchar(256)'), + last_tran_started = ca.dp.value('@lasttranstarted', 'datetime'), + last_batch_started = ca.dp.value('@lastbatchstarted', 'datetime'), + last_batch_completed = ca.dp.value('@lastbatchcompleted', 'datetime'), + lock_mode = ca.dp.value('@lockMode', 'nvarchar(256)'), + status = ca.dp.value('@status', 'nvarchar(256)'), + transaction_count = ca.dp.value('@trancount', 'bigint'), + client_app = ca.dp.value('@clientapp', 'nvarchar(1024)'), + host_name = ca.dp.value('@hostname', 'nvarchar(256)'), + login_name = ca.dp.value('@loginname', 'nvarchar(256)'), + isolation_level = ca.dp.value('@isolationlevel', 'nvarchar(256)'), + clientoption1 = ca.dp.value('@clientoption1', 'bigint'), + clientoption2 = ca.dp.value('@clientoption2', 'bigint'), + process_xml = ISNULL(ca.dp.query(N'.'), N'') + FROM #dd AS dd + CROSS APPLY dd.deadlock_xml.nodes('//deadlock/process-list/process') AS ca(dp) + WHERE (ca.dp.exist('@currentdb[. = sql:variable("@DatabaseId")]') = 1 OR @DatabaseName IS NULL) + AND (ca.dp.exist('@clientapp[. = sql:variable("@AppName")]') = 1 OR @AppName IS NULL) + AND (ca.dp.exist('@hostname[. = sql:variable("@HostName")]') = 1 OR @HostName IS NULL) + AND (ca.dp.exist('@loginname[. = sql:variable("@LoginName")]') = 1 OR @LoginName IS NULL) + ) AS q + LEFT JOIN #t AS t + ON 1 = 1 + OPTION(RECOMPILE); + + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse execution stack xml*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse execution stack xml %s', 0, 1, @d) WITH NOWAIT; + + SELECT DISTINCT + dp.id, + dp.event_date, + proc_name = ca.dp.value('@procname', 'nvarchar(1024)'), + sql_handle = ca.dp.value('@sqlhandle', 'nvarchar(131)') + INTO #deadlock_stack + FROM #deadlock_process AS dp + CROSS APPLY dp.process_xml.nodes('//executionStack/frame') AS ca(dp) + WHERE (ca.dp.exist('@procname[. = sql:variable("@StoredProcName")]') = 1 OR @StoredProcName IS NULL) + AND ca.dp.exist('@sqlhandle[ .= "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"]') = 0 + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Grab the full resource list*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + + RAISERROR('Grab the full resource list %s', 0, 1, @d) WITH NOWAIT; + + SELECT + event_date = + DATEADD + ( + MINUTE, + DATEDIFF + ( + MINUTE, + GETUTCDATE(), + SYSDATETIME() + ), + dr.event_date + ), + dr.victim_id, + dr.resource_xml + INTO + #deadlock_resource + FROM + ( + SELECT + event_date = dd.deadlock_xml.value('(event/@timestamp)[1]', 'datetime2'), + victim_id = dd.deadlock_xml.value('(//deadlock/victim-list/victimProcess/@id)[1]', 'nvarchar(256)'), + resource_xml = ISNULL(ca.dp.query(N'.'), N'') + FROM #deadlock_data AS dd + CROSS APPLY dd.deadlock_xml.nodes('//deadlock/resource-list') AS ca(dp) + ) AS dr + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse object locks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse object locks %s', 0, 1, @d) WITH NOWAIT; + + SELECT DISTINCT + ca.event_date, + ca.database_id, + database_name = + ISNULL + ( + DB_NAME(ca.database_id), + N'UNKNOWN' + ), + object_name = + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + ca.object_name COLLATE Latin1_General_BIN2, + NCHAR(31), N'?'), NCHAR(30), N'?'), NCHAR(29), N'?'), NCHAR(28), N'?'), NCHAR(27), N'?'), + NCHAR(26), N'?'), NCHAR(25), N'?'), NCHAR(24), N'?'), NCHAR(23), N'?'), NCHAR(22), N'?'), + NCHAR(21), N'?'), NCHAR(20), N'?'), NCHAR(19), N'?'), NCHAR(18), N'?'), NCHAR(17), N'?'), + NCHAR(16), N'?'), NCHAR(15), N'?'), NCHAR(14), N'?'), NCHAR(12), N'?'), NCHAR(11), N'?'), + NCHAR(8), N'?'), NCHAR(7), N'?'), NCHAR(6), N'?'), NCHAR(5), N'?'), NCHAR(4), N'?'), + NCHAR(3), N'?'), NCHAR(2), N'?'), NCHAR(1), N'?'), NCHAR(0), N'?'), + ca.lock_mode, + ca.index_name, + ca.associatedObjectId, + waiter_id = w.l.value('@id', 'nvarchar(256)'), + waiter_mode = w.l.value('@mode', 'nvarchar(256)'), + owner_id = o.l.value('@id', 'nvarchar(256)'), + owner_mode = o.l.value('@mode', 'nvarchar(256)'), + lock_type = CAST(N'OBJECT' AS nvarchar(100)) + INTO #deadlock_owner_waiter + FROM + ( + SELECT + dr.event_date, + database_id = ca.dr.value('@dbid', 'bigint'), + object_name = ca.dr.value('@objectname', 'nvarchar(1024)'), + lock_mode = ca.dr.value('@mode', 'nvarchar(256)'), + index_name = ca.dr.value('@indexname', 'nvarchar(256)'), + associatedObjectId = ca.dr.value('@associatedObjectId', 'bigint'), + dr = ca.dr.query('.') + FROM #deadlock_resource AS dr + CROSS APPLY dr.resource_xml.nodes('//resource-list/objectlock') AS ca(dr) + WHERE (ca.dr.exist('@objectname[. = sql:variable("@ObjectName")]') = 1 OR @ObjectName IS NULL) + ) AS ca + CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l) + CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse page locks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse page locks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_owner_waiter WITH(TABLOCKX) + SELECT DISTINCT + ca.event_date, + ca.database_id, + database_name = + ISNULL + ( + DB_NAME(ca.database_id), + N'UNKNOWN' + ), + ca.object_name, + ca.lock_mode, + ca.index_name, + ca.associatedObjectId, + waiter_id = w.l.value('@id', 'nvarchar(256)'), + waiter_mode = w.l.value('@mode', 'nvarchar(256)'), + owner_id = o.l.value('@id', 'nvarchar(256)'), + owner_mode = o.l.value('@mode', 'nvarchar(256)'), + lock_type = N'PAGE' + FROM + ( + SELECT + dr.event_date, + database_id = ca.dr.value('@dbid', 'bigint'), + object_name = ca.dr.value('@objectname', 'nvarchar(1024)'), + lock_mode = ca.dr.value('@mode', 'nvarchar(256)'), + index_name = ca.dr.value('@indexname', 'nvarchar(256)'), + associatedObjectId = ca.dr.value('@associatedObjectId', 'bigint'), + dr = ca.dr.query('.') + FROM #deadlock_resource AS dr + CROSS APPLY dr.resource_xml.nodes('//resource-list/pagelock') AS ca(dr) + WHERE (ca.dr.exist('@objectname[. = sql:variable("@ObjectName")]') = 1 OR @ObjectName IS NULL) + ) AS ca + CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l) + CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse key locks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse key locks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_owner_waiter WITH(TABLOCKX) + SELECT DISTINCT + ca.event_date, + ca.database_id, + database_name = + ISNULL + ( + DB_NAME(ca.database_id), + N'UNKNOWN' + ), + ca.object_name, + ca.lock_mode, + ca.index_name, + ca.associatedObjectId, + waiter_id = w.l.value('@id', 'nvarchar(256)'), + waiter_mode = w.l.value('@mode', 'nvarchar(256)'), + owner_id = o.l.value('@id', 'nvarchar(256)'), + owner_mode = o.l.value('@mode', 'nvarchar(256)'), + lock_type = N'KEY' + FROM + ( + SELECT + dr.event_date, + database_id = ca.dr.value('@dbid', 'bigint'), + object_name = ca.dr.value('@objectname', 'nvarchar(1024)'), + lock_mode = ca.dr.value('@mode', 'nvarchar(256)'), + index_name = ca.dr.value('@indexname', 'nvarchar(256)'), + associatedObjectId = ca.dr.value('@associatedObjectId', 'bigint'), + dr = ca.dr.query('.') + FROM #deadlock_resource AS dr + CROSS APPLY dr.resource_xml.nodes('//resource-list/keylock') AS ca(dr) + WHERE (ca.dr.exist('@objectname[. = sql:variable("@ObjectName")]') = 1 OR @ObjectName IS NULL) + ) AS ca + CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l) + CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse RID locks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse RID locks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_owner_waiter WITH(TABLOCKX) + SELECT DISTINCT + ca.event_date, + ca.database_id, + database_name = + ISNULL + ( + DB_NAME(ca.database_id), + N'UNKNOWN' + ), + ca.object_name, + ca.lock_mode, + ca.index_name, + ca.associatedObjectId, + waiter_id = w.l.value('@id', 'nvarchar(256)'), + waiter_mode = w.l.value('@mode', 'nvarchar(256)'), + owner_id = o.l.value('@id', 'nvarchar(256)'), + owner_mode = o.l.value('@mode', 'nvarchar(256)'), + lock_type = N'RID' + FROM + ( + SELECT + dr.event_date, + database_id = ca.dr.value('@dbid', 'bigint'), + object_name = ca.dr.value('@objectname', 'nvarchar(1024)'), + lock_mode = ca.dr.value('@mode', 'nvarchar(256)'), + index_name = ca.dr.value('@indexname', 'nvarchar(256)'), + associatedObjectId = ca.dr.value('@associatedObjectId', 'bigint'), + dr = ca.dr.query('.') + FROM #deadlock_resource AS dr + CROSS APPLY dr.resource_xml.nodes('//resource-list/ridlock') AS ca(dr) + WHERE (ca.dr.exist('@objectname[. = sql:variable("@ObjectName")]') = 1 OR @ObjectName IS NULL) + ) AS ca + CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l) + CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse row group locks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse row group locks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_owner_waiter WITH(TABLOCKX) + SELECT DISTINCT + ca.event_date, + ca.database_id, + database_name = + ISNULL + ( + DB_NAME(ca.database_id), + N'UNKNOWN' + ), + ca.object_name, + ca.lock_mode, + ca.index_name, + ca.associatedObjectId, + waiter_id = w.l.value('@id', 'nvarchar(256)'), + waiter_mode = w.l.value('@mode', 'nvarchar(256)'), + owner_id = o.l.value('@id', 'nvarchar(256)'), + owner_mode = o.l.value('@mode', 'nvarchar(256)'), + lock_type = N'ROWGROUP' + FROM + ( + SELECT + dr.event_date, + database_id = ca.dr.value('@dbid', 'bigint'), + object_name = ca.dr.value('@objectname', 'nvarchar(1024)'), + lock_mode = ca.dr.value('@mode', 'nvarchar(256)'), + index_name = ca.dr.value('@indexname', 'nvarchar(256)'), + associatedObjectId = ca.dr.value('@associatedObjectId', 'bigint'), + dr = ca.dr.query('.') + FROM #deadlock_resource AS dr + CROSS APPLY dr.resource_xml.nodes('//resource-list/rowgrouplock') AS ca(dr) + WHERE (ca.dr.exist('@objectname[. = sql:variable("@ObjectName")]') = 1 OR @ObjectName IS NULL) + ) AS ca + CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l) + CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Fixing heaps in #deadlock_owner_waiter %s', 0, 1, @d) WITH NOWAIT; + + UPDATE + d + SET + d.index_name = + d.object_name + N'.HEAP' + FROM #deadlock_owner_waiter AS d + WHERE d.lock_type IN + ( + N'HEAP', + N'RID' + ) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Parse parallel deadlocks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Parse parallel deadlocks %s', 0, 1, @d) WITH NOWAIT; + + SELECT DISTINCT + ca.id, + ca.event_date, + ca.wait_type, + ca.node_id, + ca.waiter_type, + ca.owner_activity, + ca.waiter_activity, + ca.merging, + ca.spilling, + ca.waiting_to_close, + waiter_id = w.l.value('@id', 'nvarchar(256)'), + owner_id = o.l.value('@id', 'nvarchar(256)') + INTO #deadlock_resource_parallel + FROM + ( + SELECT + dr.event_date, + id = ca.dr.value('@id', 'nvarchar(256)'), + wait_type = ca.dr.value('@WaitType', 'nvarchar(256)'), + node_id = ca.dr.value('@nodeId', 'bigint'), + /* These columns are in 2017 CU5+ ONLY */ + waiter_type = ca.dr.value('@waiterType', 'nvarchar(256)'), + owner_activity = ca.dr.value('@ownerActivity', 'nvarchar(256)'), + waiter_activity = ca.dr.value('@waiterActivity', 'nvarchar(256)'), + merging = ca.dr.value('@merging', 'nvarchar(256)'), + spilling = ca.dr.value('@spilling', 'nvarchar(256)'), + waiting_to_close = ca.dr.value('@waitingToClose', 'nvarchar(256)'), + /* These columns are in 2017 CU5+ ONLY */ + dr = ca.dr.query('.') + FROM #deadlock_resource AS dr + CROSS APPLY dr.resource_xml.nodes('//resource-list/exchangeEvent') AS ca(dr) + ) AS ca + CROSS APPLY ca.dr.nodes('//waiter-list/waiter') AS w(l) + CROSS APPLY ca.dr.nodes('//owner-list/owner') AS o(l) + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Get rid of parallel noise*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Get rid of parallel noise %s', 0, 1, @d) WITH NOWAIT; + + WITH + c AS + ( + SELECT + *, + rn = + ROW_NUMBER() OVER + ( + PARTITION BY + drp.owner_id, + drp.waiter_id + ORDER BY + drp.event_date + ) + FROM #deadlock_resource_parallel AS drp + ) + DELETE + FROM c + WHERE c.rn > 1 + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Get rid of nonsense*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Get rid of nonsense %s', 0, 1, @d) WITH NOWAIT; + + DELETE dow + FROM #deadlock_owner_waiter AS dow + WHERE dow.owner_id = dow.waiter_id + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Add some nonsense*/ + ALTER TABLE + #deadlock_process + ADD + waiter_mode nvarchar(256), + owner_mode nvarchar(256), + is_victim AS + CONVERT + ( + bit, + CASE + WHEN id = victim_id + THEN 1 + ELSE 0 + END + ) PERSISTED; + + /*Update some nonsense*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Update some nonsense part 1 %s', 0, 1, @d) WITH NOWAIT; + + UPDATE + dp + SET + dp.owner_mode = dow.owner_mode + FROM #deadlock_process AS dp + JOIN #deadlock_owner_waiter AS dow + ON dp.id = dow.owner_id + AND dp.event_date = dow.event_date + WHERE dp.is_victim = 0 + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Update some nonsense part 2 %s', 0, 1, @d) WITH NOWAIT; + + UPDATE + dp + SET + dp.waiter_mode = dow.waiter_mode + FROM #deadlock_process AS dp + JOIN #deadlock_owner_waiter AS dow + ON dp.victim_id = dow.waiter_id + AND dp.event_date = dow.event_date + WHERE dp.is_victim = 1 + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Get Agent Job and Step names*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Get Agent Job and Step names %s', 0, 1, @d) WITH NOWAIT; + + SELECT + x.event_date, + x.victim_id, + x.id, + x.database_id, + x.client_app, + x.job_id, + x.step_id, + job_id_guid = + CONVERT + ( + uniqueidentifier, + TRY_CAST + ( + N'' + AS xml + ).value('xs:hexBinary(substring(sql:column("x.job_id"), 0))', 'binary(16)') + ) + INTO #agent_job + FROM + ( + SELECT + dp.event_date, + dp.victim_id, + dp.id, + dp.database_id, + dp.client_app, + job_id = + SUBSTRING + ( + dp.client_app, + CHARINDEX(N'0x', dp.client_app) + LEN(N'0x'), + 32 + ), + step_id = + CASE + WHEN CHARINDEX(N': Step ', dp.client_app) > 0 + AND CHARINDEX(N')', dp.client_app, CHARINDEX(N': Step ', dp.client_app)) > 0 + THEN + SUBSTRING + ( + dp.client_app, + CHARINDEX(N': Step ', dp.client_app) + LEN(N': Step '), + CHARINDEX(N')', dp.client_app, CHARINDEX(N': Step ', dp.client_app)) - + (CHARINDEX(N': Step ', dp.client_app) + LEN(N': Step ')) + ) + ELSE dp.client_app + END + FROM #deadlock_process AS dp + WHERE dp.client_app LIKE N'SQLAgent - %' + AND dp.client_app <> N'SQLAgent - Initial Boot Probe' + ) AS x + OPTION(RECOMPILE); + + ALTER TABLE + #agent_job + ADD + job_name nvarchar(256), + step_name nvarchar(256); + + IF + ( + @Azure = 0 + AND @RDS = 0 + ) + BEGIN + SET @StringToExecute = + N' + UPDATE + aj + SET + aj.job_name = j.name, + aj.step_name = s.step_name + FROM msdb.dbo.sysjobs AS j + JOIN msdb.dbo.sysjobsteps AS s + ON j.job_id = s.job_id + JOIN #agent_job AS aj + ON aj.job_id_guid = j.job_id + AND aj.step_id = s.step_id + OPTION(RECOMPILE); + '; + + IF @Debug = 1 BEGIN PRINT @StringToExecute; END; + EXEC sys.sp_executesql + @StringToExecute; + + END; + + UPDATE + dp + SET + dp.client_app = + CASE + WHEN dp.client_app LIKE N'SQLAgent - %' + THEN N'SQLAgent - Job: ' + + aj.job_name + + N' Step: ' + + aj.step_name + ELSE dp.client_app + END + FROM #deadlock_process AS dp + JOIN #agent_job AS aj + ON dp.event_date = aj.event_date + AND dp.victim_id = aj.victim_id + AND dp.id = aj.id + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Get each and every table of all databases*/ + IF @Azure = 0 + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Inserting to @sysAssObjId %s', 0, 1, @d) WITH NOWAIT; + + INSERT INTO + @sysAssObjId + ( + database_id, + partition_id, + schema_name, + table_name + ) + EXECUTE sys.sp_MSforeachdb + N' + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + USE [?]; + + IF EXISTS + ( + SELECT + 1/0 + FROM #deadlock_process AS dp + WHERE dp.database_id = DB_ID() + ) + BEGIN + SELECT + database_id = + DB_ID(), + p.partition_id, + schema_name = + s.name, + table_name = + t.name + FROM sys.partitions p + JOIN sys.tables t + ON t.object_id = p.object_id + JOIN sys.schemas s + ON s.schema_id = t.schema_id + WHERE s.name IS NOT NULL + AND t.name IS NOT NULL + OPTION(RECOMPILE); + END; + '; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + IF @Azure = 1 + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Inserting to @sysAssObjId at %s', 0, 1, @d) WITH NOWAIT; + + INSERT INTO + @sysAssObjId + ( + database_id, + partition_id, + schema_name, + table_name + ) + SELECT + database_id = + DB_ID(), + p.partition_id, + schema_name = + s.name, + table_name = + t.name + FROM sys.partitions p + JOIN sys.tables t + ON t.object_id = p.object_id + JOIN sys.schemas s + ON s.schema_id = t.schema_id + WHERE s.name IS NOT NULL + AND t.name IS NOT NULL + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + /*Begin checks based on parsed values*/ + + /* + First, revert these back since we already converted the event data to local time, + and searches will break if we use the times converted over to UTC for the event data + */ + SELECT + @StartDate = @StartDateOriginal, + @EndDate = @EndDateOriginal; + + /*Check 1 is deadlocks by database*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 1 database deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 1, + dp.database_name, + object_name = N'-', + finding_group = N'Total Database Deadlocks', + finding = + N'This database had ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + + N' deadlocks.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC) + FROM #deadlock_process AS dp + WHERE 1 = 1 + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY dp.database_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 2 is deadlocks with selects*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 2 select deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 2, + dow.database_name, + object_name = + N'You Might Need RCSI', + finding_group = N'Total Deadlocks Involving Selects', + finding = + N'There have been ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dow.event_date) + ) + + N' deadlock(s) between read queries and modification queries.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC) + FROM #deadlock_owner_waiter AS dow + WHERE 1 = 1 + AND dow.lock_mode IN + ( + N'S', + N'IS' + ) + OR dow.owner_mode IN + ( + N'S', + N'IS' + ) + OR dow.waiter_mode IN + ( + N'S', + N'IS' + ) + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + GROUP BY + dow.database_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 3 is deadlocks by object*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 3 object deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 3, + dow.database_name, + object_name = + ISNULL + ( + dow.object_name, + N'UNKNOWN' + ), + finding_group = N'Total Object Deadlocks', + finding = + N'This object was involved in ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dow.event_date) + ) + + N' deadlock(s).', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC) + FROM #deadlock_owner_waiter AS dow + WHERE 1 = 1 + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + GROUP BY + dow.database_name, + dow.object_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 3 continuation, number of deadlocks per index*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 3 (continued) index deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 3, + dow.database_name, + index_name = dow.index_name, + finding_group = N'Total Index Deadlocks', + finding = + N'This index was involved in ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dow.event_date) + ) + + N' deadlock(s).', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC) + FROM #deadlock_owner_waiter AS dow + WHERE 1 = 1 + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + AND dow.lock_type NOT IN + ( + N'HEAP', + N'RID' + ) + AND dow.index_name IS NOT NULL + GROUP BY + dow.database_name, + dow.index_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 3 continuation, number of deadlocks per heap*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 3 (continued) heap deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 3, + dow.database_name, + index_name = dow.index_name, + finding_group = N'Total Heap Deadlocks', + finding = + N'This heap was involved in ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dow.event_date) + ) + + N' deadlock(s).', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dow.event_date) DESC) + FROM #deadlock_owner_waiter AS dow + WHERE 1 = 1 + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + AND dow.lock_type IN + ( + N'HEAP', + N'RID' + ) + GROUP BY + dow.database_name, + dow.index_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 4 looks for Serializable deadlocks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 4 serializable deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 4, + database_name = + dp.database_name, + object_name = N'-', + finding_group = N'Serializable Deadlocking', + finding = + N'This database has had ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + + N' instances of Serializable deadlocks.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC) + FROM #deadlock_process AS dp + WHERE dp.isolation_level LIKE N'serializable%' + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY dp.database_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 5 looks for Repeatable Read deadlocks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 5 repeatable read deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 5, + dp.database_name, + object_name = N'-', + finding_group = N'Repeatable Read Deadlocking', + finding = + N'This database has had ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + + N' instances of Repeatable Read deadlocks.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC) + FROM #deadlock_process AS dp + WHERE dp.isolation_level LIKE N'repeatable%' + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY dp.database_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 6 breaks down app, host, and login information*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 6 app/host/login deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 6, + database_name = + dp.database_name, + object_name = N'-', + finding_group = N'Login, App, and Host deadlocks', + finding = + N'This database has had ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + + N' instances of deadlocks involving the login ' + + ISNULL + ( + dp.login_name, + N'UNKNOWN' + ) + + N' from the application ' + + ISNULL + ( + dp.client_app, + N'UNKNOWN' + ) + + N' on host ' + + ISNULL + ( + dp.host_name, + N'UNKNOWN' + ) + + N'.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT dp.event_date) DESC) + FROM #deadlock_process AS dp + WHERE 1 = 1 + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY + dp.database_name, + dp.login_name, + dp.client_app, + dp.host_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 7 breaks down the types of deadlocks (object, page, key, etc.)*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 7 types of deadlocks %s', 0, 1, @d) WITH NOWAIT; + + WITH + lock_types AS + ( + SELECT + database_name = + dp.database_name, + dow.object_name, + lock = + CASE + WHEN CHARINDEX(N':', dp.wait_resource) > 0 + THEN SUBSTRING + ( + dp.wait_resource, + 1, + CHARINDEX(N':', dp.wait_resource) - 1 + ) + ELSE dp.wait_resource + END, + lock_count = + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + FROM #deadlock_process AS dp + JOIN #deadlock_owner_waiter AS dow + ON (dp.id = dow.owner_id + OR dp.victim_id = dow.waiter_id) + AND dp.event_date = dow.event_date + WHERE 1 = 1 + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + AND dow.object_name IS NOT NULL + GROUP BY + dp.database_name, + CASE + WHEN CHARINDEX(N':', dp.wait_resource) > 0 + THEN SUBSTRING + ( + dp.wait_resource, + 1, + CHARINDEX(N':', dp.wait_resource) - 1 + ) + ELSE dp.wait_resource + END, + dow.object_name + ) + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 7, + lt.database_name, + lt.object_name, + finding_group = N'Types of locks by object', + finding = + N'This object has had ' + + STUFF + ( + ( + SELECT + N', ' + + lt2.lock_count + + N' ' + + lt2.lock + FROM lock_types AS lt2 + WHERE lt2.database_name = lt.database_name + AND lt2.object_name = lt.object_name + FOR XML + PATH(N''), + TYPE + ).value(N'.[1]', N'nvarchar(MAX)'), + 1, + 1, + N'' + ) + N' locks.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY CONVERT(bigint, lt.lock_count) DESC) + FROM lock_types AS lt + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 8 gives you more info queries for sp_BlitzCache & BlitzQueryStore*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 8 more info part 1 BlitzCache %s', 0, 1, @d) WITH NOWAIT; + + WITH + deadlock_stack AS + ( + SELECT DISTINCT + ds.id, + ds.event_date, + ds.proc_name, + database_name = + PARSENAME(ds.proc_name, 3), + schema_name = + PARSENAME(ds.proc_name, 2), + proc_only_name = + PARSENAME(ds.proc_name, 1), + sql_handle_csv = + N'''' + + STUFF + ( + ( + SELECT DISTINCT + N',' + + ds2.sql_handle + FROM #deadlock_stack AS ds2 + WHERE ds2.id = ds.id + AND ds2.event_date = ds.event_date + AND ds2.sql_handle <> 0x + FOR XML + PATH(N''), + TYPE + ).value(N'.[1]', N'nvarchar(MAX)'), + 1, + 1, + N'' + ) + + N'''' + FROM #deadlock_stack AS ds + WHERE ds.sql_handle <> 0x + GROUP BY + PARSENAME(ds.proc_name, 3), + PARSENAME(ds.proc_name, 2), + PARSENAME(ds.proc_name, 1), + ds.proc_name, + ds.id, + ds.event_date + ) + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT DISTINCT + check_id = 8, + dow.database_name, + object_name = ds.proc_name, + finding_group = N'More Info - Query', + finding = N'EXEC sp_BlitzCache ' + + CASE + WHEN ds.proc_name = N'adhoc' + THEN N'@OnlySqlHandles = ' + ds.sql_handle_csv + ELSE N'@StoredProcName = ' + QUOTENAME(ds.proc_only_name, N'''') + END + N';' + FROM deadlock_stack AS ds + JOIN #deadlock_owner_waiter AS dow + ON dow.owner_id = ds.id + AND dow.event_date = ds.event_date + WHERE 1 = 1 + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @StoredProcName OR @StoredProcName IS NULL) + AND ds.proc_name NOT LIKE 'Unknown%' + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + IF (@ProductVersionMajor >= 13 OR @Azure = 1) + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 8 more info part 2 BlitzQueryStore %s', 0, 1, @d) WITH NOWAIT; + + WITH + deadlock_stack AS + ( + SELECT DISTINCT + ds.id, + ds.sql_handle, + ds.proc_name, + ds.event_date, + database_name = + PARSENAME(ds.proc_name, 3), + schema_name = + PARSENAME(ds.proc_name, 2), + proc_only_name = + PARSENAME(ds.proc_name, 1) + FROM #deadlock_stack AS ds + ) + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT DISTINCT + check_id = 8, + dow.database_name, + object_name = ds.proc_name, + finding_group = N'More Info - Query', + finding = + N'EXEC sp_BlitzQueryStore ' + + N'@DatabaseName = ' + + QUOTENAME(ds.database_name, N'''') + + N', ' + + N'@StoredProcName = ' + + QUOTENAME(ds.proc_only_name, N'''') + + N';' + FROM deadlock_stack AS ds + JOIN #deadlock_owner_waiter AS dow + ON dow.owner_id = ds.id + AND dow.event_date = ds.event_date + WHERE ds.proc_name <> N'adhoc' + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @StoredProcName OR @StoredProcName IS NULL) + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; + + /*Check 9 gives you stored procedure deadlock counts*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 9 stored procedure deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 9, + database_name = + dp.database_name, + object_name = ds.proc_name, + finding_group = N'Stored Procedure Deadlocks', + finding = + N'The stored procedure ' + + PARSENAME(ds.proc_name, 2) + + N'.' + + PARSENAME(ds.proc_name, 1) + + N' has been involved in ' + + CONVERT + ( + nvarchar(10), + COUNT_BIG(DISTINCT ds.id) + ) + + N' deadlocks.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT ds.id) DESC) + FROM #deadlock_stack AS ds + JOIN #deadlock_process AS dp + ON dp.id = ds.id + AND ds.event_date = dp.event_date + WHERE ds.proc_name <> N'adhoc' + AND (ds.proc_name = @StoredProcName OR @StoredProcName IS NULL) + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY + dp.database_name, + ds.proc_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 10 gives you more info queries for sp_BlitzIndex */ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 10 more info, BlitzIndex %s', 0, 1, @d) WITH NOWAIT; + + WITH + bi AS + ( + SELECT DISTINCT + dow.object_name, + dow.database_name, + schema_name = s.schema_name, + table_name = s.table_name + FROM #deadlock_owner_waiter AS dow + JOIN @sysAssObjId AS s + ON s.database_id = dow.database_id + AND s.partition_id = dow.associatedObjectId + WHERE 1 = 1 + AND (dow.database_id = @DatabaseId OR @DatabaseName IS NULL) + AND (dow.event_date >= @StartDate OR @StartDate IS NULL) + AND (dow.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + AND dow.object_name IS NOT NULL + ) + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT DISTINCT + check_id = 10, + bi.database_name, + bi.object_name, + finding_group = N'More Info - Table', + finding = + N'EXEC sp_BlitzIndex ' + + N'@DatabaseName = ' + + QUOTENAME(bi.database_name, N'''') + + N', @SchemaName = ' + + QUOTENAME(bi.schema_name, N'''') + + N', @TableName = ' + + QUOTENAME(bi.table_name, N'''') + + N';' + FROM bi + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 11 gets total deadlock wait time per object*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 11 deadlock wait time per object %s', 0, 1, @d) WITH NOWAIT; + + WITH + chopsuey AS + ( + + SELECT + database_name = + dp.database_name, + dow.object_name, + wait_days = + CONVERT + ( + nvarchar(30), + ( + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + ) / 1000 / 86400 + ) + ), + wait_time_hms = + /*the more wait time you rack up the less accurate this gets, + it's either that or erroring out*/ + CASE + WHEN + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + )/1000 > 2147483647 + THEN + CONVERT + ( + nvarchar(30), + DATEADD + ( + MINUTE, + ( + ( + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + ) + )/ + 60000 + ), + 0 + ), + 14 + ) + WHEN + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + ) BETWEEN 2147483648 AND 2147483647000 + THEN + CONVERT + ( + nvarchar(30), + DATEADD + ( + SECOND, + ( + ( + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + ) + )/ + 1000 + ), + 0 + ), + 14 + ) + ELSE + CONVERT + ( + nvarchar(30), + DATEADD + ( + MILLISECOND, + ( + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + ) + ), + 0 + ), + 14 + ) + END, + total_waits = + SUM(CONVERT(bigint, dp.wait_time)) + FROM #deadlock_owner_waiter AS dow + JOIN #deadlock_process AS dp + ON (dp.id = dow.owner_id + OR dp.victim_id = dow.waiter_id) + AND dp.event_date = dow.event_date + WHERE 1 = 1 + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dow.object_name = @ObjectName OR @ObjectName IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY + dp.database_name, + dow.object_name + ) + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 11, + cs.database_name, + cs.object_name, + finding_group = N'Total object deadlock wait time', + finding = + N'This object has had ' + + CONVERT + ( + nvarchar(30), + cs.wait_days + ) + + N' ' + + CONVERT + ( + nvarchar(30), + cs.wait_time_hms, + 14 + ) + + N' [dd hh:mm:ss:ms] of deadlock wait time.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY cs.total_waits DESC) + FROM chopsuey AS cs + WHERE cs.object_name IS NOT NULL + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 12 gets total deadlock wait time per database*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 12 deadlock wait time per database %s', 0, 1, @d) WITH NOWAIT; + + WITH + wait_time AS + ( + SELECT + database_name = + dp.database_name, + total_wait_time_ms = + SUM + ( + CONVERT + ( + bigint, + dp.wait_time + ) + ) + FROM #deadlock_owner_waiter AS dow + JOIN #deadlock_process AS dp + ON (dp.id = dow.owner_id + OR dp.victim_id = dow.waiter_id) + AND dp.event_date = dow.event_date + WHERE 1 = 1 + AND (dp.database_name = @DatabaseName OR @DatabaseName IS NULL) + AND (dp.event_date >= @StartDate OR @StartDate IS NULL) + AND (dp.event_date < @EndDate OR @EndDate IS NULL) + AND (dp.client_app = @AppName OR @AppName IS NULL) + AND (dp.host_name = @HostName OR @HostName IS NULL) + AND (dp.login_name = @LoginName OR @LoginName IS NULL) + GROUP BY + dp.database_name + ) + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 12, + wt.database_name, + object_name = N'-', + finding_group = N'Total database deadlock wait time', + N'This database has had ' + + CONVERT + ( + nvarchar(30), + ( + SUM + ( + CONVERT + ( + bigint, + wt.total_wait_time_ms + ) + ) / 1000 / 86400 + ) + ) + + N' ' + + /*the more wait time you rack up the less accurate this gets, + it's either that or erroring out*/ + CASE + WHEN + SUM + ( + CONVERT + ( + bigint, + wt.total_wait_time_ms + ) + )/1000 > 2147483647 + THEN + CONVERT + ( + nvarchar(30), + DATEADD + ( + MINUTE, + ( + ( + SUM + ( + CONVERT + ( + bigint, + wt.total_wait_time_ms + ) + ) + )/ + 60000 + ), + 0 + ), + 14 + ) + WHEN + SUM + ( + CONVERT + ( + bigint, + wt.total_wait_time_ms + ) + ) BETWEEN 2147483648 AND 2147483647000 + THEN + CONVERT + ( + nvarchar(30), + DATEADD + ( + SECOND, + ( + ( + SUM + ( + CONVERT + ( + bigint, + wt.total_wait_time_ms + ) + ) + )/ + 1000 + ), + 0 + ), + 14 + ) + ELSE + CONVERT + ( + nvarchar(30), + DATEADD + ( + MILLISECOND, + ( + SUM + ( + CONVERT + ( + bigint, + wt.total_wait_time_ms + ) + ) + ), + 0 + ), + 14 + ) END + + N' [dd hh:mm:ss:ms] of deadlock wait time.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY SUM(CONVERT(bigint, wt.total_wait_time_ms)) DESC) + FROM wait_time AS wt + GROUP BY + wt.database_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 13 gets total deadlock wait time for SQL Agent*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 13 deadlock count for SQL Agent %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding, + sort_order + ) + SELECT + check_id = 13, + database_name = + DB_NAME(aj.database_id), + object_name = + N'SQLAgent - Job: ' + + aj.job_name + + N' Step: ' + + aj.step_name, + finding_group = N'Agent Job Deadlocks', + finding = + N'There have been ' + + RTRIM(COUNT_BIG(DISTINCT aj.event_date)) + + N' deadlocks from this Agent Job and Step.', + sort_order = + ROW_NUMBER() + OVER (ORDER BY COUNT_BIG(DISTINCT aj.event_date) DESC) + FROM #agent_job AS aj + GROUP BY + DB_NAME(aj.database_id), + aj.job_name, + aj.step_name + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 14 is total parallel deadlocks*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 14 parallel deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT + check_id = 14, + database_name = N'-', + object_name = N'-', + finding_group = N'Total parallel deadlocks', + finding = + N'There have been ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT drp.event_date) + ) + + N' parallel deadlocks.' + FROM #deadlock_resource_parallel AS drp + HAVING COUNT_BIG(DISTINCT drp.event_date) > 0 + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 15 is total deadlocks involving sleeping sessions*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 15 sleeping deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT + check_id = 15, + database_name = N'-', + object_name = N'-', + finding_group = N'Total deadlocks involving sleeping sessions', + finding = + N'There have been ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + + N' sleepy deadlocks.' + FROM #deadlock_process AS dp + WHERE dp.status = N'sleeping' + HAVING COUNT_BIG(DISTINCT dp.event_date) > 0 + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Check 16 is total deadlocks involving implicit transactions*/ + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Check 16 implicit transaction deadlocks %s', 0, 1, @d) WITH NOWAIT; + + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT + check_id = 14, + database_name = N'-', + object_name = N'-', + finding_group = N'Total implicit transaction deadlocks', + finding = + N'There have been ' + + CONVERT + ( + nvarchar(20), + COUNT_BIG(DISTINCT dp.event_date) + ) + + N' implicit transaction deadlocks.' + FROM #deadlock_process AS dp + WHERE dp.transaction_name = N'implicit_transaction' + HAVING COUNT_BIG(DISTINCT dp.event_date) > 0 + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*Thank you goodnight*/ + INSERT + #deadlock_findings WITH(TABLOCKX) + ( + check_id, + database_name, + object_name, + finding_group, + finding + ) + VALUES + ( + -1, + N'sp_BlitzLock version ' + CONVERT(nvarchar(10), @Version), + N'Results for ' + CONVERT(nvarchar(10), @StartDate, 23) + N' through ' + CONVERT(nvarchar(10), @EndDate, 23), + N'http://FirstResponderKit.org/', + N'To get help or add your own contributions to the SQL Server First Responder Kit, join us at http://FirstResponderKit.org.' + ); + + RAISERROR('Finished rollup at %s', 0, 1, @d) WITH NOWAIT; + + /*Results*/ + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Results 1 %s', 0, 1, @d) WITH NOWAIT; + + CREATE CLUSTERED INDEX cx_whatever_dp ON #deadlock_process (event_date, id); + CREATE CLUSTERED INDEX cx_whatever_drp ON #deadlock_resource_parallel (event_date, owner_id); + CREATE CLUSTERED INDEX cx_whatever_dow ON #deadlock_owner_waiter (event_date, owner_id, waiter_id); + + IF @Debug = 1 BEGIN SET STATISTICS XML ON; END; + + WITH + deadlocks AS + ( + SELECT + deadlock_type = + N'Regular Deadlock', + dp.event_date, + dp.id, + dp.victim_id, + dp.spid, + dp.database_id, + dp.database_name, + dp.current_database_name, + dp.priority, + dp.log_used, + wait_resource = + dp.wait_resource COLLATE DATABASE_DEFAULT, + object_names = + CONVERT + ( + xml, + STUFF + ( + ( + SELECT DISTINCT + object_name = + NCHAR(10) + + N' ' + + ISNULL(c.object_name, N'') + + N' ' COLLATE DATABASE_DEFAULT + FROM #deadlock_owner_waiter AS c + WHERE (dp.id = c.owner_id + OR dp.victim_id = c.waiter_id) + AND dp.event_date = c.event_date + FOR XML + PATH(N''), + TYPE + ).value(N'.[1]', N'nvarchar(4000)'), + 1, + 1, + N'' + ) + ), + dp.wait_time, + dp.transaction_name, + dp.status, + dp.last_tran_started, + dp.last_batch_started, + dp.last_batch_completed, + dp.lock_mode, + dp.transaction_count, + dp.client_app, + dp.host_name, + dp.login_name, + dp.isolation_level, + dp.client_option_1, + dp.client_option_2, + inputbuf = + dp.process_xml.value('(//process/inputbuf/text())[1]', 'nvarchar(MAX)'), + en = + DENSE_RANK() OVER (ORDER BY dp.event_date), + qn = + ROW_NUMBER() OVER (PARTITION BY dp.event_date ORDER BY dp.event_date) - 1, + dn = + ROW_NUMBER() OVER (PARTITION BY dp.event_date, dp.id ORDER BY dp.event_date), + dp.is_victim, + owner_mode = + ISNULL(dp.owner_mode, N'-'), + owner_waiter_type = NULL, + owner_activity = NULL, + owner_waiter_activity = NULL, + owner_merging = NULL, + owner_spilling = NULL, + owner_waiting_to_close = NULL, + waiter_mode = + ISNULL(dp.waiter_mode, N'-'), + waiter_waiter_type = NULL, + waiter_owner_activity = NULL, + waiter_waiter_activity = NULL, + waiter_merging = NULL, + waiter_spilling = NULL, + waiter_waiting_to_close = NULL, + dp.deadlock_graph + FROM #deadlock_process AS dp + WHERE dp.victim_id IS NOT NULL + AND dp.is_parallel = 0 + + UNION ALL + + SELECT + deadlock_type = + N'Parallel Deadlock', + dp.event_date, + dp.id, + dp.victim_id, + dp.spid, + dp.database_id, + dp.database_name, + dp.current_database_name, + dp.priority, + dp.log_used, + dp.wait_resource COLLATE DATABASE_DEFAULT, + object_names = + CONVERT + ( + xml, + STUFF + ( + ( + SELECT DISTINCT + object_name = + NCHAR(10) + + N' ' + + ISNULL(c.object_name, N'') + + N' ' COLLATE DATABASE_DEFAULT + FROM #deadlock_owner_waiter AS c + WHERE (dp.id = c.owner_id + OR dp.victim_id = c.waiter_id) + AND dp.event_date = c.event_date + FOR XML + PATH(N''), + TYPE + ).value(N'.[1]', N'nvarchar(4000)'), + 1, + 1, + N'' + ) + ), + dp.wait_time, + dp.transaction_name, + dp.status, + dp.last_tran_started, + dp.last_batch_started, + dp.last_batch_completed, + dp.lock_mode, + dp.transaction_count, + dp.client_app, + dp.host_name, + dp.login_name, + dp.isolation_level, + dp.client_option_1, + dp.client_option_2, + inputbuf = + dp.process_xml.value('(//process/inputbuf/text())[1]', 'nvarchar(MAX)'), + en = + DENSE_RANK() OVER (ORDER BY dp.event_date), + qn = + ROW_NUMBER() OVER (PARTITION BY dp.event_date ORDER BY dp.event_date) - 1, + dn = + ROW_NUMBER() OVER (PARTITION BY dp.event_date, dp.id ORDER BY dp.event_date), + is_victim = 1, + owner_mode = cao.wait_type COLLATE DATABASE_DEFAULT, + owner_waiter_type = cao.waiter_type, + owner_activity = cao.owner_activity, + owner_waiter_activity = cao.waiter_activity, + owner_merging = cao.merging, + owner_spilling = cao.spilling, + owner_waiting_to_close = cao.waiting_to_close, + waiter_mode = caw.wait_type COLLATE DATABASE_DEFAULT, + waiter_waiter_type = caw.waiter_type, + waiter_owner_activity = caw.owner_activity, + waiter_waiter_activity = caw.waiter_activity, + waiter_merging = caw.merging, + waiter_spilling = caw.spilling, + waiter_waiting_to_close = caw.waiting_to_close, + dp.deadlock_graph + FROM #deadlock_process AS dp + OUTER APPLY + ( + SELECT TOP (1) + drp.* + FROM #deadlock_resource_parallel AS drp + WHERE drp.owner_id = dp.id + AND drp.wait_type IN + ( + N'e_waitPortOpen', + N'e_waitPipeNewRow' + ) + ORDER BY drp.event_date + ) AS cao + OUTER APPLY + ( + SELECT TOP (1) + drp.* + FROM #deadlock_resource_parallel AS drp + WHERE drp.owner_id = dp.id + AND drp.wait_type IN + ( + N'e_waitPortOpen', + N'e_waitPipeGetRow' + ) + ORDER BY drp.event_date + ) AS caw + WHERE dp.is_parallel = 1 + ) + SELECT + d.deadlock_type, + d.event_date, + d.id, + d.victim_id, + d.spid, + deadlock_group = + N'Deadlock #' + + CONVERT + ( + nvarchar(10), + d.en + ) + + N', Query #' + + CASE + WHEN d.qn = 0 + THEN N'1' + ELSE CONVERT(nvarchar(10), d.qn) + END + CASE + WHEN d.is_victim = 1 + THEN N' - VICTIM' + ELSE N'' + END, + d.database_id, + d.database_name, + d.current_database_name, + d.priority, + d.log_used, + d.wait_resource, + d.object_names, + d.wait_time, + d.transaction_name, + d.status, + d.last_tran_started, + d.last_batch_started, + d.last_batch_completed, + d.lock_mode, + d.transaction_count, + d.client_app, + d.host_name, + d.login_name, + d.isolation_level, + d.client_option_1, + d.client_option_2, + inputbuf = + CASE + WHEN d.inputbuf + LIKE @inputbuf_bom + N'Proc |[Database Id = %' ESCAPE N'|' + THEN + OBJECT_SCHEMA_NAME + ( + SUBSTRING + ( + d.inputbuf, + CHARINDEX(N'Object Id = ', d.inputbuf) + 12, + LEN(d.inputbuf) - (CHARINDEX(N'Object Id = ', d.inputbuf) + 12) + ) + , + SUBSTRING + ( + d.inputbuf, + CHARINDEX(N'Database Id = ', d.inputbuf) + 14, + CHARINDEX(N'Object Id', d.inputbuf) - (CHARINDEX(N'Database Id = ', d.inputbuf) + 14) + ) + ) + + N'.' + + OBJECT_NAME + ( + SUBSTRING + ( + d.inputbuf, + CHARINDEX(N'Object Id = ', d.inputbuf) + 12, + LEN(d.inputbuf) - (CHARINDEX(N'Object Id = ', d.inputbuf) + 12) + ) + , + SUBSTRING + ( + d.inputbuf, + CHARINDEX(N'Database Id = ', d.inputbuf) + 14, + CHARINDEX(N'Object Id', d.inputbuf) - (CHARINDEX(N'Database Id = ', d.inputbuf) + 14) + ) + ) + ELSE d.inputbuf + END COLLATE Latin1_General_BIN2, + d.owner_mode, + d.owner_waiter_type, + d.owner_activity, + d.owner_waiter_activity, + d.owner_merging, + d.owner_spilling, + d.owner_waiting_to_close, + d.waiter_mode, + d.waiter_waiter_type, + d.waiter_owner_activity, + d.waiter_waiter_activity, + d.waiter_merging, + d.waiter_spilling, + d.waiter_waiting_to_close, + d.deadlock_graph, + d.is_victim + INTO #deadlocks + FROM deadlocks AS d + WHERE d.dn = 1 + AND (d.is_victim = @VictimsOnly + OR @VictimsOnly = 0) + AND d.qn < CASE + WHEN d.deadlock_type = N'Parallel Deadlock' + THEN 2 + ELSE 2147483647 + END + AND (DB_NAME(d.database_id) = @DatabaseName OR @DatabaseName IS NULL) + AND (d.event_date >= @StartDate OR @StartDate IS NULL) + AND (d.event_date < @EndDate OR @EndDate IS NULL) + AND (CONVERT(nvarchar(MAX), d.object_names) COLLATE Latin1_General_BIN2 LIKE N'%' + @ObjectName + N'%' OR @ObjectName IS NULL) + AND (d.client_app = @AppName OR @AppName IS NULL) + AND (d.host_name = @HostName OR @HostName IS NULL) + AND (d.login_name = @LoginName OR @LoginName IS NULL) + OPTION (RECOMPILE, LOOP JOIN, HASH JOIN); + + UPDATE d + SET d.inputbuf = + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( + d.inputbuf, + NCHAR(31), N'?'), NCHAR(30), N'?'), NCHAR(29), N'?'), NCHAR(28), N'?'), NCHAR(27), N'?'), + NCHAR(26), N'?'), NCHAR(25), N'?'), NCHAR(24), N'?'), NCHAR(23), N'?'), NCHAR(22), N'?'), + NCHAR(21), N'?'), NCHAR(20), N'?'), NCHAR(19), N'?'), NCHAR(18), N'?'), NCHAR(17), N'?'), + NCHAR(16), N'?'), NCHAR(15), N'?'), NCHAR(14), N'?'), NCHAR(12), N'?'), NCHAR(11), N'?'), + NCHAR(8), N'?'), NCHAR(7), N'?'), NCHAR(6), N'?'), NCHAR(5), N'?'), NCHAR(4), N'?'), + NCHAR(3), N'?'), NCHAR(2), N'?'), NCHAR(1), N'?'), NCHAR(0), N'?') + FROM #deadlocks AS d + OPTION(RECOMPILE); + + SELECT + d.deadlock_type, + d.event_date, + database_name = + DB_NAME(d.database_id), + database_name_x = + d.database_name, + d.current_database_name, + d.spid, + d.deadlock_group, + d.client_option_1, + d.client_option_2, + d.lock_mode, + query_xml = + ( + SELECT + [processing-instruction(query)] = + d.inputbuf + FOR XML + PATH(N''), + TYPE + ), + query_string = + d.inputbuf, + d.object_names, + d.isolation_level, + d.owner_mode, + d.waiter_mode, + d.transaction_count, + d.login_name, + d.host_name, + d.client_app, + d.wait_time, + d.wait_resource, + d.priority, + d.log_used, + d.last_tran_started, + d.last_batch_started, + d.last_batch_completed, + d.transaction_name, + d.status, + /*These columns will be NULL for regular (non-parallel) deadlocks*/ + parallel_deadlock_details = + ( + SELECT + d.owner_waiter_type, + d.owner_activity, + d.owner_waiter_activity, + d.owner_merging, + d.owner_spilling, + d.owner_waiting_to_close, + d.waiter_waiter_type, + d.waiter_owner_activity, + d.waiter_waiter_activity, + d.waiter_merging, + d.waiter_spilling, + d.waiter_waiting_to_close + FOR XML + PATH('parallel_deadlock_details'), + TYPE + ), + d.owner_waiter_type, + d.owner_activity, + d.owner_waiter_activity, + d.owner_merging, + d.owner_spilling, + d.owner_waiting_to_close, + d.waiter_waiter_type, + d.waiter_owner_activity, + d.waiter_waiter_activity, + d.waiter_merging, + d.waiter_spilling, + d.waiter_waiting_to_close, + /*end parallel deadlock columns*/ + d.deadlock_graph, + d.is_victim, + d.id + INTO #deadlock_results + FROM #deadlocks AS d; + + IF @Debug = 1 BEGIN SET STATISTICS XML OFF; END; + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + /*There's too much risk of errors sending the*/ + IF @OutputDatabaseCheck = 0 + BEGIN + SET @ExportToExcel = 0; + END; + + SET @deadlock_result += N' + SELECT + server_name = + @@SERVERNAME, + dr.deadlock_type, + dr.event_date, + database_name = + COALESCE + ( + dr.database_name, + dr.database_name_x, + dr.current_database_name + ), + dr.spid, + dr.deadlock_group, + ' + CASE @ExportToExcel + WHEN 1 + THEN N' + query = dr.query_string, + object_names = + REPLACE( + REPLACE( + CONVERT + ( + nvarchar(MAX), + dr.object_names + ) COLLATE Latin1_General_BIN2, + '''', ''''), + '''', ''''),' + ELSE N'query = dr.query_xml, + dr.object_names,' + END + N' + dr.isolation_level, + dr.owner_mode, + dr.waiter_mode, + dr.lock_mode, + dr.transaction_count, + dr.client_option_1, + dr.client_option_2, + dr.login_name, + dr.host_name, + dr.client_app, + dr.wait_time, + dr.wait_resource, + dr.priority, + dr.log_used, + dr.last_tran_started, + dr.last_batch_started, + dr.last_batch_completed, + dr.transaction_name, + dr.status,' + + CASE + WHEN (@ExportToExcel = 1 + OR @OutputDatabaseCheck = 0) + THEN N' + dr.owner_waiter_type, + dr.owner_activity, + dr.owner_waiter_activity, + dr.owner_merging, + dr.owner_spilling, + dr.owner_waiting_to_close, + dr.waiter_waiter_type, + dr.waiter_owner_activity, + dr.waiter_waiter_activity, + dr.waiter_merging, + dr.waiter_spilling, + dr.waiter_waiting_to_close,' + ELSE N' + dr.parallel_deadlock_details,' + END + + CASE + @ExportToExcel + WHEN 1 + THEN N' + deadlock_graph = + REPLACE(REPLACE( + REPLACE(REPLACE( + CONVERT + ( + nvarchar(MAX), + dr.deadlock_graph + ) COLLATE Latin1_General_BIN2, + ''NCHAR(10)'', ''''), ''NCHAR(13)'', ''''), + ''CHAR(10)'', ''''), ''CHAR(13)'', '''')' + ELSE N' + dr.deadlock_graph' + END + N' + FROM #deadlock_results AS dr + ORDER BY + dr.event_date, + dr.is_victim DESC + OPTION(RECOMPILE, LOOP JOIN, HASH JOIN); + '; + + IF (@OutputDatabaseCheck = 0) + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Results to table %s', 0, 1, @d) WITH NOWAIT; + + IF @Debug = 1 + BEGIN + PRINT @deadlock_result; + SET STATISTICS XML ON; + END; + + INSERT INTO + DeadLockTbl + ( + ServerName, + deadlock_type, + event_date, + database_name, + spid, + deadlock_group, + query, + object_names, + isolation_level, + owner_mode, + waiter_mode, + lock_mode, + transaction_count, + client_option_1, + client_option_2, + login_name, + host_name, + client_app, + wait_time, + wait_resource, + priority, + log_used, + last_tran_started, + last_batch_started, + last_batch_completed, + transaction_name, + status, + owner_waiter_type, + owner_activity, + owner_waiter_activity, + owner_merging, + owner_spilling, + owner_waiting_to_close, + waiter_waiter_type, + waiter_owner_activity, + waiter_waiter_activity, + waiter_merging, + waiter_spilling, + waiter_waiting_to_close, + deadlock_graph + ) + EXEC sys.sp_executesql + @deadlock_result; + + IF @Debug = 1 + BEGIN + SET STATISTICS XML OFF; + END; + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + DROP SYNONYM DeadLockTbl; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Findings to table %s', 0, 1, @d) WITH NOWAIT; + + INSERT INTO + DeadlockFindings + ( + ServerName, + check_id, + database_name, + object_name, + finding_group, + finding + ) + SELECT + @@SERVERNAME, + df.check_id, + df.database_name, + df.object_name, + df.finding_group, + df.finding + FROM #deadlock_findings AS df + ORDER BY df.check_id + OPTION(RECOMPILE); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + DROP SYNONYM DeadlockFindings; /*done with inserting.*/ + END; + ELSE /*Output to database is not set output to client app*/ + BEGIN + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Results to client %s', 0, 1, @d) WITH NOWAIT; + + IF @Debug = 1 BEGIN SET STATISTICS XML ON; END; + + EXEC sys.sp_executesql + @deadlock_result; + + IF @Debug = 1 + BEGIN + SET STATISTICS XML OFF; + PRINT @deadlock_result; + END; + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Getting available execution plans for deadlocks %s', 0, 1, @d) WITH NOWAIT; + + SELECT DISTINCT + available_plans = + 'available_plans', + ds.proc_name, + sql_handle = + CONVERT(varbinary(64), ds.sql_handle, 1), + dow.database_name, + dow.database_id, + dow.object_name, + query_xml = + TRY_CAST(dr.query_xml AS nvarchar(MAX)) + INTO #available_plans + FROM #deadlock_stack AS ds + JOIN #deadlock_owner_waiter AS dow + ON dow.owner_id = ds.id + AND dow.event_date = ds.event_date + JOIN #deadlock_results AS dr + ON dr.id = ds.id + AND dr.event_date = ds.event_date + OPTION(RECOMPILE); + + SELECT + deqs.sql_handle, + deqs.plan_handle, + deqs.statement_start_offset, + deqs.statement_end_offset, + deqs.creation_time, + deqs.last_execution_time, + deqs.execution_count, + total_worker_time_ms = + deqs.total_worker_time / 1000., + avg_worker_time_ms = + CONVERT(decimal(38, 6), deqs.total_worker_time / 1000. / deqs.execution_count), + total_elapsed_time_ms = + deqs.total_elapsed_time / 1000., + avg_elapsed_time = + CONVERT(decimal(38, 6), deqs.total_elapsed_time / 1000. / deqs.execution_count), + executions_per_second = + ISNULL + ( + deqs.execution_count / + NULLIF + ( + DATEDIFF + ( + SECOND, + deqs.creation_time, + deqs.last_execution_time + ), + 0 + ), + 0 + ), + total_physical_reads_mb = + deqs.total_physical_reads * 8. / 1024., + total_logical_writes_mb = + deqs.total_logical_writes * 8. / 1024., + total_logical_reads_mb = + deqs.total_logical_reads * 8. / 1024., + min_grant_mb = + deqs.min_grant_kb * 8. / 1024., + max_grant_mb = + deqs.max_grant_kb * 8. / 1024., + min_used_grant_mb = + deqs.min_used_grant_kb * 8. / 1024., + max_used_grant_mb = + deqs.max_used_grant_kb * 8. / 1024., + deqs.min_reserved_threads, + deqs.max_reserved_threads, + deqs.min_used_threads, + deqs.max_used_threads, + deqs.total_rows + INTO #dm_exec_query_stats + FROM sys.dm_exec_query_stats AS deqs + WHERE EXISTS + ( + SELECT + 1/0 + FROM #available_plans AS ap + WHERE ap.sql_handle = deqs.sql_handle + ) + AND deqs.query_hash IS NOT NULL; + + CREATE CLUSTERED INDEX + deqs + ON #dm_exec_query_stats + ( + sql_handle, + plan_handle + ); + + SELECT + ap.available_plans, + ap.database_name, + query_text = + TRY_CAST(ap.query_xml AS xml), + ap.query_plan, + ap.creation_time, + ap.last_execution_time, + ap.execution_count, + ap.executions_per_second, + ap.total_worker_time_ms, + ap.avg_worker_time_ms, + ap.total_elapsed_time_ms, + ap.avg_elapsed_time, + ap.total_logical_reads_mb, + ap.total_physical_reads_mb, + ap.total_logical_writes_mb, + ap.min_grant_mb, + ap.max_grant_mb, + ap.min_used_grant_mb, + ap.max_used_grant_mb, + ap.min_reserved_threads, + ap.max_reserved_threads, + ap.min_used_threads, + ap.max_used_threads, + ap.total_rows, + ap.sql_handle, + ap.statement_start_offset, + ap.statement_end_offset + FROM + ( + + SELECT + ap.*, + c.statement_start_offset, + c.statement_end_offset, + c.creation_time, + c.last_execution_time, + c.execution_count, + c.total_worker_time_ms, + c.avg_worker_time_ms, + c.total_elapsed_time_ms, + c.avg_elapsed_time, + c.executions_per_second, + c.total_physical_reads_mb, + c.total_logical_writes_mb, + c.total_logical_reads_mb, + c.min_grant_mb, + c.max_grant_mb, + c.min_used_grant_mb, + c.max_used_grant_mb, + c.min_reserved_threads, + c.max_reserved_threads, + c.min_used_threads, + c.max_used_threads, + c.total_rows, + c.query_plan + FROM #available_plans AS ap + OUTER APPLY + ( + SELECT + deqs.*, + query_plan = + TRY_CAST(deps.query_plan AS xml) + FROM #dm_exec_query_stats deqs + OUTER APPLY sys.dm_exec_text_query_plan + ( + deqs.plan_handle, + deqs.statement_start_offset, + deqs.statement_end_offset + ) AS deps + WHERE deqs.sql_handle = ap.sql_handle + AND deps.dbid = ap.database_id + ) AS c + ) AS ap + WHERE ap.query_plan IS NOT NULL + ORDER BY + ap.avg_worker_time_ms DESC + OPTION(RECOMPILE, LOOP JOIN, HASH JOIN); + + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Returning findings %s', 0, 1, @d) WITH NOWAIT; + + SELECT + df.check_id, + df.database_name, + df.object_name, + df.finding_group, + df.finding + FROM #deadlock_findings AS df + ORDER BY + df.check_id, + df.sort_order + OPTION(RECOMPILE); + + SET @d = CONVERT(varchar(40), GETDATE(), 109); + RAISERROR('Finished at %s', 0, 1, @d) WITH NOWAIT; + END; /*done with output to client app.*/ + END; + + IF @Debug = 1 + BEGIN + SELECT + table_name = N'#deadlock_data', + * + FROM #deadlock_data AS dd + OPTION(RECOMPILE); + + SELECT + table_name = N'#dd', + * + FROM #dd AS d + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlock_resource', + * + FROM #deadlock_resource AS dr + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlock_resource_parallel', + * + FROM #deadlock_resource_parallel AS drp + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlock_owner_waiter', + * + FROM #deadlock_owner_waiter AS dow + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlock_process', + * + FROM #deadlock_process AS dp + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlock_stack', + * + FROM #deadlock_stack AS ds + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlocks', + * + FROM #deadlocks AS d + OPTION(RECOMPILE); + + SELECT + table_name = N'#deadlock_results', + * + FROM #deadlock_results AS dr + OPTION(RECOMPILE); + + SELECT + table_name = N'#x', + * + FROM #x AS x + OPTION(RECOMPILE); + + SELECT + table_name = N'@sysAssObjId', + * + FROM @sysAssObjId AS s + OPTION(RECOMPILE); + + SELECT + table_name = N'#available_plans', + * + FROM #available_plans AS ap + OPTION(RECOMPILE); + + SELECT + table_name = N'#dm_exec_query_stats', + * + FROM #dm_exec_query_stats + OPTION(RECOMPILE); + + SELECT + procedure_parameters = + 'procedure_parameters', + DatabaseName = + @DatabaseName, + StartDate = + @StartDate, + EndDate = + @EndDate, + ObjectName = + @ObjectName, + StoredProcName = + @StoredProcName, + AppName = + @AppName, + HostName = + @HostName, + LoginName = + @LoginName, + EventSessionName = + @EventSessionName, + TargetSessionType = + @TargetSessionType, + VictimsOnly = + @VictimsOnly, + Debug = + @Debug, + Help = + @Help, + Version = + @Version, + VersionDate = + @VersionDate, + VersionCheckMode = + @VersionCheckMode, + OutputDatabaseName = + @OutputDatabaseName, + OutputSchemaName = + @OutputSchemaName, + OutputTableName = + @OutputTableName, + ExportToExcel = + @ExportToExcel; + + SELECT + declared_variables = + 'declared_variables', + DatabaseId = + @DatabaseId, + StartDateUTC = + @StartDateUTC, + EndDateUTC = + @EndDateUTC, + ProductVersion = + @ProductVersion, + ProductVersionMajor = + @ProductVersionMajor, + ProductVersionMinor = + @ProductVersionMinor, + ObjectFullName = + @ObjectFullName, + Azure = + @Azure, + RDS = + @RDS, + d = + @d, + StringToExecute = + @StringToExecute, + StringToExecuteParams = + @StringToExecuteParams, + r = + @r, + OutputTableFindings = + @OutputTableFindings, + DeadlockCount = + @DeadlockCount, + ServerName = + @ServerName, + OutputDatabaseCheck = + @OutputDatabaseCheck, + SessionId = + @SessionId, + TargetSessionId = + @TargetSessionId, + FileName = + @FileName, + inputbuf_bom = + @inputbuf_bom, + deadlock_result = + @deadlock_result; + END; /*End debug*/ + END; /*Final End*/ +GO +SET ANSI_NULLS ON; +SET ANSI_PADDING ON; +SET ANSI_WARNINGS ON; +SET ARITHABORT ON; +SET CONCAT_NULL_YIELDS_NULL ON; +SET QUOTED_IDENTIFIER ON; +SET STATISTICS IO OFF; +SET STATISTICS TIME OFF; +GO + +DECLARE @msg NVARCHAR(MAX) = N''; + + -- Must be a compatible, on-prem version of SQL (2016+) +IF ( (SELECT CONVERT(NVARCHAR(128), SERVERPROPERTY ('EDITION'))) <> 'SQL Azure' + AND (SELECT PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 4)) < 13 + ) + -- or Azure Database (not Azure Data Warehouse), running at database compat level 130+ +OR ( (SELECT CONVERT(NVARCHAR(128), SERVERPROPERTY ('EDITION'))) = 'SQL Azure' + AND (SELECT SERVERPROPERTY ('ENGINEEDITION')) NOT IN (5,8) + AND (SELECT [compatibility_level] FROM sys.databases WHERE [name] = DB_NAME()) < 130 + ) +BEGIN + SELECT @msg = N'Sorry, sp_BlitzQueryStore doesn''t work on versions of SQL prior to 2016, or Azure Database compatibility < 130.' + REPLICATE(CHAR(13), 7933); + PRINT @msg; + RETURN; +END; + +IF OBJECT_ID('dbo.sp_BlitzQueryStore') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_BlitzQueryStore AS RETURN 0;'); +GO + +ALTER PROCEDURE dbo.sp_BlitzQueryStore + @Help BIT = 0, + @DatabaseName NVARCHAR(128) = NULL , + @Top INT = 3, + @StartDate DATETIME2 = NULL, + @EndDate DATETIME2 = NULL, + @MinimumExecutionCount INT = NULL, + @DurationFilter DECIMAL(38,4) = NULL , + @StoredProcName NVARCHAR(128) = NULL, + @Failed BIT = 0, + @PlanIdFilter INT = NULL, + @QueryIdFilter INT = NULL, + @ExportToExcel BIT = 0, + @HideSummary BIT = 0 , + @SkipXML BIT = 0, + @Debug BIT = 0, + @ExpertMode BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +WITH RECOMPILE +AS +BEGIN /*First BEGIN*/ + +SET NOCOUNT ON; +SET STATISTICS XML OFF; +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + +SELECT @Version = '8.16', @VersionDate = '20230820'; +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + + +DECLARE /*Variables for the variable Gods*/ + @msg NVARCHAR(MAX) = N'', --Used to format RAISERROR messages in some places + @sql_select NVARCHAR(MAX) = N'', --Used to hold SELECT statements for dynamic SQL + @sql_where NVARCHAR(MAX) = N'', -- Used to hold WHERE clause for dynamic SQL + @duration_filter_ms DECIMAL(38,4) = (@DurationFilter * 1000.), --We accept Duration in seconds, but we filter in milliseconds (this is grandfathered from sp_BlitzCache) + @execution_threshold INT = 1000, --Threshold at which we consider a query to be frequently executed + @ctp_threshold_pct TINYINT = 10, --Percentage of CTFP at which we consider a query to be near parallel + @long_running_query_warning_seconds BIGINT = 300 * 1000 ,--Number of seconds (converted to milliseconds) at which a query is considered long running + @memory_grant_warning_percent INT = 10,--Percent of memory grant used compared to what's granted; used to trigger unused memory grant warning + @ctp INT,--Holds the CTFP value for the server + @min_memory_per_query INT,--Holds the server configuration value for min memory per query + @cr NVARCHAR(1) = NCHAR(13),--Special character + @lf NVARCHAR(1) = NCHAR(10),--Special character + @tab NVARCHAR(1) = NCHAR(9),--Special character + @error_severity INT,--Holds error info for try/catch blocks + @error_state INT,--Holds error info for try/catch blocks + @sp_params NVARCHAR(MAX) = N'@sp_Top INT, @sp_StartDate DATETIME2, @sp_EndDate DATETIME2, @sp_MinimumExecutionCount INT, @sp_MinDuration INT, @sp_StoredProcName NVARCHAR(128), @sp_PlanIdFilter INT, @sp_QueryIdFilter INT',--Holds parameters used in dynamic SQL + @is_azure_db BIT = 0, --Are we using Azure? I'm not. You might be. That's cool. + @compatibility_level TINYINT = 0, --Some functionality (T-SQL) isn't available in lower compat levels. We can use this to weed out those issues as we go. + @log_size_mb DECIMAL(38,2) = 0, + @avg_tempdb_data_file DECIMAL(38,2) = 0; + +/*Grabs CTFP setting*/ +SELECT @ctp = NULLIF(CAST(value AS INT), 0) +FROM sys.configurations +WHERE name = N'cost threshold for parallelism' +OPTION (RECOMPILE); + +/*Grabs min query memory setting*/ +SELECT @min_memory_per_query = CONVERT(INT, c.value) +FROM sys.configurations AS c +WHERE c.name = N'min memory per query (KB)' +OPTION (RECOMPILE); + +/*Check if this is Azure first*/ +IF (SELECT CONVERT(NVARCHAR(128), SERVERPROPERTY ('EDITION'))) <> 'SQL Azure' + BEGIN + /*Grabs log size for datbase*/ + SELECT @log_size_mb = AVG(((mf.size * 8) / 1024.)) + FROM sys.master_files AS mf + WHERE mf.database_id = DB_ID(@DatabaseName) + AND mf.type_desc = 'LOG'; + + /*Grab avg tempdb file size*/ + SELECT @avg_tempdb_data_file = AVG(((mf.size * 8) / 1024.)) + FROM sys.master_files AS mf + WHERE mf.database_id = DB_ID('tempdb') + AND mf.type_desc = 'ROWS'; + END; + +/*Help section*/ + +IF @Help = 1 + BEGIN + + SELECT N'You have requested assistance. It will arrive as soon as humanly possible.' AS [Take four red capsules, help is on the way]; + + PRINT N' + sp_BlitzQueryStore from http://FirstResponderKit.org + + This script displays your most resource-intensive queries from the Query Store, + and points to ways you can tune these queries to make them faster. + + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - This query will not run on SQL Server versions less than 2016. + - This query will not run on Azure Databases with compatibility less than 130. + - This query will not run on Azure Data Warehouse. + + Unknown limitations of this version: + - Could be tickling + + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + '; + RETURN; + +END; + +/*Making sure your version is copasetic*/ +IF ( (SELECT CONVERT(NVARCHAR(128), SERVERPROPERTY ('EDITION'))) = 'SQL Azure' ) + BEGIN + SET @is_azure_db = 1; + + IF ( (SELECT SERVERPROPERTY ('ENGINEEDITION')) NOT IN (5,8) + OR (SELECT [compatibility_level] FROM sys.databases WHERE [name] = DB_NAME()) < 130 + ) + BEGIN + SELECT @msg = N'Sorry, sp_BlitzQueryStore doesn''t work on Azure Data Warehouse, or Azure Databases with DB compatibility < 130.' + REPLICATE(CHAR(13), 7933); + PRINT @msg; + RETURN; + END; + END; +ELSE IF ( (SELECT PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 4) ) < 13 ) + BEGIN + SELECT @msg = N'Sorry, sp_BlitzQueryStore doesn''t work on versions of SQL prior to 2016.' + REPLICATE(CHAR(13), 7933); + PRINT @msg; + RETURN; + END; + +/*Making sure at least one database uses QS*/ +IF ( SELECT COUNT(*) + FROM sys.databases AS d + WHERE d.is_query_store_on = 1 + AND d.user_access_desc='MULTI_USER' + AND d.state_desc = 'ONLINE' + AND d.name NOT IN ('master', 'model', 'msdb', 'tempdb', '32767') + AND d.is_distributor = 0 ) = 0 + BEGIN + SELECT @msg = N'You don''t currently have any databases with Query Store enabled.' + REPLICATE(CHAR(13), 7933); + PRINT @msg; + RETURN; + END; + +/*Making sure your databases are using QDS.*/ +RAISERROR('Checking database validity', 0, 1) WITH NOWAIT; + +IF (@is_azure_db = 1 AND SERVERPROPERTY ('ENGINEEDITION') <> 8) + SET @DatabaseName = DB_NAME(); +ELSE +BEGIN + + /*If we're on Azure SQL DB we don't need to check all this @DatabaseName stuff...*/ + + SET @DatabaseName = LTRIM(RTRIM(@DatabaseName)); + + /*Did you set @DatabaseName?*/ + RAISERROR('Making sure [%s] isn''t NULL', 0, 1, @DatabaseName) WITH NOWAIT; + IF (@DatabaseName IS NULL) + BEGIN + RAISERROR('@DatabaseName cannot be NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + + /*Does the database exist?*/ + RAISERROR('Making sure [%s] exists', 0, 1, @DatabaseName) WITH NOWAIT; + IF ((DB_ID(@DatabaseName)) IS NULL) + BEGIN + RAISERROR('The @DatabaseName you specified ([%s]) does not exist. Please check the name and try again.', 0, 1, @DatabaseName) WITH NOWAIT; + RETURN; + END; + + /*Is it online?*/ + RAISERROR('Making sure [%s] is online', 0, 1, @DatabaseName) WITH NOWAIT; + IF (DATABASEPROPERTYEX(@DatabaseName, 'Collation')) IS NULL + BEGIN + RAISERROR('The @DatabaseName you specified ([%s]) is not readable. Please check the name and try again. Better yet, check your server.', 0, 1, @DatabaseName); + RETURN; + END; +END; + +/*Does it have Query Store enabled?*/ +RAISERROR('Making sure [%s] has Query Store enabled', 0, 1, @DatabaseName) WITH NOWAIT; +IF + + ( SELECT [d].[name] + FROM [sys].[databases] AS d + WHERE [d].[is_query_store_on] = 1 + AND [d].[user_access_desc]='MULTI_USER' + AND [d].[state_desc] = 'ONLINE' + AND [d].[database_id] = (SELECT database_id FROM sys.databases WHERE name = @DatabaseName) + ) IS NULL +BEGIN + RAISERROR('The @DatabaseName you specified ([%s]) does not have the Query Store enabled. Please check the name or settings, and try again.', 0, 1, @DatabaseName) WITH NOWAIT; + RETURN; +END; + +/*Check database compat level*/ + +RAISERROR('Checking database compatibility level', 0, 1) WITH NOWAIT; + +SELECT @compatibility_level = d.compatibility_level +FROM sys.databases AS d +WHERE d.name = @DatabaseName; + +RAISERROR('The @DatabaseName you specified ([%s])is running in compatibility level ([%d]).', 0, 1, @DatabaseName, @compatibility_level) WITH NOWAIT; + + +/*Making sure top is set to something if NULL*/ +IF ( @Top IS NULL ) + BEGIN + SET @Top = 3; + END; + +/* +This section determines if you have the Query Store wait stats DMV +*/ + +RAISERROR('Checking for query_store_wait_stats', 0, 1) WITH NOWAIT; + +DECLARE @ws_out INT, + @waitstats BIT, + @ws_sql NVARCHAR(MAX) = N'SELECT @i_out = COUNT(*) FROM ' + QUOTENAME(@DatabaseName) + N'.sys.all_objects WHERE name = ''query_store_wait_stats'' OPTION (RECOMPILE);', + @ws_params NVARCHAR(MAX) = N'@i_out INT OUTPUT'; + +EXEC sys.sp_executesql @ws_sql, @ws_params, @i_out = @ws_out OUTPUT; + +SELECT @waitstats = CASE @ws_out WHEN 0 THEN 0 ELSE 1 END; + +SET @msg = N'Wait stats DMV ' + CASE @waitstats + WHEN 0 THEN N' does not exist, skipping.' + WHEN 1 THEN N' exists, will analyze.' + END; +RAISERROR(@msg, 0, 1) WITH NOWAIT; + +/* +This section determines if you have some additional columns present in 2017, in case they get back ported. +*/ + +RAISERROR('Checking for new columns in query_store_runtime_stats', 0, 1) WITH NOWAIT; + +DECLARE @nc_out INT, + @new_columns BIT, + @nc_sql NVARCHAR(MAX) = N'SELECT @i_out = COUNT(*) + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.all_columns AS ac + WHERE OBJECT_NAME(object_id) = ''query_store_runtime_stats'' + AND ac.name IN ( + ''avg_num_physical_io_reads'', + ''last_num_physical_io_reads'', + ''min_num_physical_io_reads'', + ''max_num_physical_io_reads'', + ''avg_log_bytes_used'', + ''last_log_bytes_used'', + ''min_log_bytes_used'', + ''max_log_bytes_used'', + ''avg_tempdb_space_used'', + ''last_tempdb_space_used'', + ''min_tempdb_space_used'', + ''max_tempdb_space_used'' + ) OPTION (RECOMPILE);', + @nc_params NVARCHAR(MAX) = N'@i_out INT OUTPUT'; + +EXEC sys.sp_executesql @nc_sql, @ws_params, @i_out = @nc_out OUTPUT; + +SELECT @new_columns = CASE @nc_out WHEN 12 THEN 1 ELSE 0 END; + +SET @msg = N'New query_store_runtime_stats columns ' + CASE @new_columns + WHEN 0 THEN N' do not exist, skipping.' + WHEN 1 THEN N' exist, will analyze.' + END; +RAISERROR(@msg, 0, 1) WITH NOWAIT; + + +/* +These are the temp tables we use +*/ + + +/* +This one holds the grouped data that helps use figure out which periods to examine +*/ + +RAISERROR(N'Creating temp tables', 0, 1) WITH NOWAIT; + +DROP TABLE IF EXISTS #grouped_interval; + +CREATE TABLE #grouped_interval +( + flat_date DATE NULL, + start_range DATETIME NULL, + end_range DATETIME NULL, + total_avg_duration_ms DECIMAL(38, 2) NULL, + total_avg_cpu_time_ms DECIMAL(38, 2) NULL, + total_avg_logical_io_reads_mb DECIMAL(38, 2) NULL, + total_avg_physical_io_reads_mb DECIMAL(38, 2) NULL, + total_avg_logical_io_writes_mb DECIMAL(38, 2) NULL, + total_avg_query_max_used_memory_mb DECIMAL(38, 2) NULL, + total_rowcount DECIMAL(38, 2) NULL, + total_count_executions BIGINT NULL, + total_avg_log_bytes_mb DECIMAL(38, 2) NULL, + total_avg_tempdb_space DECIMAL(38, 2) NULL, + total_max_duration_ms DECIMAL(38, 2) NULL, + total_max_cpu_time_ms DECIMAL(38, 2) NULL, + total_max_logical_io_reads_mb DECIMAL(38, 2) NULL, + total_max_physical_io_reads_mb DECIMAL(38, 2) NULL, + total_max_logical_io_writes_mb DECIMAL(38, 2) NULL, + total_max_query_max_used_memory_mb DECIMAL(38, 2) NULL, + total_max_log_bytes_mb DECIMAL(38, 2) NULL, + total_max_tempdb_space DECIMAL(38, 2) NULL, + INDEX gi_ix_dates CLUSTERED (start_range, end_range) +); + + +/* +These are the plans we focus on based on what we find in the grouped intervals +*/ +DROP TABLE IF EXISTS #working_plans; + +CREATE TABLE #working_plans +( + plan_id BIGINT, + query_id BIGINT, + pattern NVARCHAR(258), + INDEX wp_ix_ids CLUSTERED (plan_id, query_id) +); + + +/* +These are the gathered metrics we get from query store to generate some warnings and help you find your worst offenders +*/ +DROP TABLE IF EXISTS #working_metrics; + +CREATE TABLE #working_metrics +( + database_name NVARCHAR(258), + plan_id BIGINT, + query_id BIGINT, + query_id_all_plan_ids VARCHAR(8000), + /*these columns are from query_store_query*/ + proc_or_function_name NVARCHAR(258), + batch_sql_handle VARBINARY(64), + query_hash BINARY(8), + query_parameterization_type_desc NVARCHAR(258), + parameter_sniffing_symptoms NVARCHAR(4000), + count_compiles BIGINT, + avg_compile_duration DECIMAL(38,2), + last_compile_duration DECIMAL(38,2), + avg_bind_duration DECIMAL(38,2), + last_bind_duration DECIMAL(38,2), + avg_bind_cpu_time DECIMAL(38,2), + last_bind_cpu_time DECIMAL(38,2), + avg_optimize_duration DECIMAL(38,2), + last_optimize_duration DECIMAL(38,2), + avg_optimize_cpu_time DECIMAL(38,2), + last_optimize_cpu_time DECIMAL(38,2), + avg_compile_memory_kb DECIMAL(38,2), + last_compile_memory_kb DECIMAL(38,2), + /*These come from query_store_runtime_stats*/ + execution_type_desc NVARCHAR(128), + first_execution_time DATETIME2, + last_execution_time DATETIME2, + count_executions BIGINT, + avg_duration DECIMAL(38,2) , + last_duration DECIMAL(38,2), + min_duration DECIMAL(38,2), + max_duration DECIMAL(38,2), + avg_cpu_time DECIMAL(38,2), + last_cpu_time DECIMAL(38,2), + min_cpu_time DECIMAL(38,2), + max_cpu_time DECIMAL(38,2), + avg_logical_io_reads DECIMAL(38,2), + last_logical_io_reads DECIMAL(38,2), + min_logical_io_reads DECIMAL(38,2), + max_logical_io_reads DECIMAL(38,2), + avg_logical_io_writes DECIMAL(38,2), + last_logical_io_writes DECIMAL(38,2), + min_logical_io_writes DECIMAL(38,2), + max_logical_io_writes DECIMAL(38,2), + avg_physical_io_reads DECIMAL(38,2), + last_physical_io_reads DECIMAL(38,2), + min_physical_io_reads DECIMAL(38,2), + max_physical_io_reads DECIMAL(38,2), + avg_clr_time DECIMAL(38,2), + last_clr_time DECIMAL(38,2), + min_clr_time DECIMAL(38,2), + max_clr_time DECIMAL(38,2), + avg_dop BIGINT, + last_dop BIGINT, + min_dop BIGINT, + max_dop BIGINT, + avg_query_max_used_memory DECIMAL(38,2), + last_query_max_used_memory DECIMAL(38,2), + min_query_max_used_memory DECIMAL(38,2), + max_query_max_used_memory DECIMAL(38,2), + avg_rowcount DECIMAL(38,2), + last_rowcount DECIMAL(38,2), + min_rowcount DECIMAL(38,2), + max_rowcount DECIMAL(38,2), + /*These are 2017 only, AFAIK*/ + avg_num_physical_io_reads DECIMAL(38,2), + last_num_physical_io_reads DECIMAL(38,2), + min_num_physical_io_reads DECIMAL(38,2), + max_num_physical_io_reads DECIMAL(38,2), + avg_log_bytes_used DECIMAL(38,2), + last_log_bytes_used DECIMAL(38,2), + min_log_bytes_used DECIMAL(38,2), + max_log_bytes_used DECIMAL(38,2), + avg_tempdb_space_used DECIMAL(38,2), + last_tempdb_space_used DECIMAL(38,2), + min_tempdb_space_used DECIMAL(38,2), + max_tempdb_space_used DECIMAL(38,2), + /*These are computed columns to make some stuff easier down the line*/ + total_compile_duration AS avg_compile_duration * count_compiles, + total_bind_duration AS avg_bind_duration * count_compiles, + total_bind_cpu_time AS avg_bind_cpu_time * count_compiles, + total_optimize_duration AS avg_optimize_duration * count_compiles, + total_optimize_cpu_time AS avg_optimize_cpu_time * count_compiles, + total_compile_memory_kb AS avg_compile_memory_kb * count_compiles, + total_duration AS avg_duration * count_executions, + total_cpu_time AS avg_cpu_time * count_executions, + total_logical_io_reads AS avg_logical_io_reads * count_executions, + total_logical_io_writes AS avg_logical_io_writes * count_executions, + total_physical_io_reads AS avg_physical_io_reads * count_executions, + total_clr_time AS avg_clr_time * count_executions, + total_query_max_used_memory AS avg_query_max_used_memory * count_executions, + total_rowcount AS avg_rowcount * count_executions, + total_num_physical_io_reads AS avg_num_physical_io_reads * count_executions, + total_log_bytes_used AS avg_log_bytes_used * count_executions, + total_tempdb_space_used AS avg_tempdb_space_used * count_executions, + xpm AS NULLIF(count_executions, 0) / NULLIF(DATEDIFF(MINUTE, first_execution_time, last_execution_time), 0), + percent_memory_grant_used AS CONVERT(MONEY, ISNULL(NULLIF(( max_query_max_used_memory * 1.00 ), 0) / NULLIF(min_query_max_used_memory, 0), 0) * 100.), + INDEX wm_ix_ids CLUSTERED (plan_id, query_id, query_hash) +); + + +/* +This is where we store some additional metrics, along with the query plan and text +*/ +DROP TABLE IF EXISTS #working_plan_text; + +CREATE TABLE #working_plan_text +( + database_name NVARCHAR(258), + plan_id BIGINT, + query_id BIGINT, + /*These are from query_store_plan*/ + plan_group_id BIGINT, + engine_version NVARCHAR(64), + compatibility_level INT, + query_plan_hash BINARY(8), + query_plan_xml XML, + is_online_index_plan BIT, + is_trivial_plan BIT, + is_parallel_plan BIT, + is_forced_plan BIT, + is_natively_compiled BIT, + force_failure_count BIGINT, + last_force_failure_reason_desc NVARCHAR(258), + count_compiles BIGINT, + initial_compile_start_time DATETIME2, + last_compile_start_time DATETIME2, + last_execution_time DATETIME2, + avg_compile_duration DECIMAL(38,2), + last_compile_duration BIGINT, + /*These are from query_store_query*/ + query_sql_text NVARCHAR(MAX), + statement_sql_handle VARBINARY(64), + is_part_of_encrypted_module BIT, + has_restricted_text BIT, + /*This is from query_context_settings*/ + context_settings NVARCHAR(512), + /*This is from #working_plans*/ + pattern NVARCHAR(512), + top_three_waits NVARCHAR(MAX), + INDEX wpt_ix_ids CLUSTERED (plan_id, query_id, query_plan_hash) +); + + +/* +This is where we store warnings that we generate from the XML and metrics +*/ +DROP TABLE IF EXISTS #working_warnings; + +CREATE TABLE #working_warnings +( + plan_id BIGINT, + query_id BIGINT, + query_hash BINARY(8), + sql_handle VARBINARY(64), + proc_or_function_name NVARCHAR(258), + plan_multiple_plans BIT, + is_forced_plan BIT, + is_forced_parameterized BIT, + is_cursor BIT, + is_optimistic_cursor BIT, + is_forward_only_cursor BIT, + is_fast_forward_cursor BIT, + is_cursor_dynamic BIT, + is_parallel BIT, + is_forced_serial BIT, + is_key_lookup_expensive BIT, + key_lookup_cost FLOAT, + is_remote_query_expensive BIT, + remote_query_cost FLOAT, + frequent_execution BIT, + parameter_sniffing BIT, + unparameterized_query BIT, + near_parallel BIT, + plan_warnings BIT, + long_running BIT, + downlevel_estimator BIT, + implicit_conversions BIT, + tvf_estimate BIT, + compile_timeout BIT, + compile_memory_limit_exceeded BIT, + warning_no_join_predicate BIT, + query_cost FLOAT, + missing_index_count INT, + unmatched_index_count INT, + is_trivial BIT, + trace_flags_session NVARCHAR(1000), + is_unused_grant BIT, + function_count INT, + clr_function_count INT, + is_table_variable BIT, + no_stats_warning BIT, + relop_warnings BIT, + is_table_scan BIT, + backwards_scan BIT, + forced_index BIT, + forced_seek BIT, + forced_scan BIT, + columnstore_row_mode BIT, + is_computed_scalar BIT , + is_sort_expensive BIT, + sort_cost FLOAT, + is_computed_filter BIT, + op_name NVARCHAR(100) NULL, + index_insert_count INT NULL, + index_update_count INT NULL, + index_delete_count INT NULL, + cx_insert_count INT NULL, + cx_update_count INT NULL, + cx_delete_count INT NULL, + table_insert_count INT NULL, + table_update_count INT NULL, + table_delete_count INT NULL, + index_ops AS (index_insert_count + index_update_count + index_delete_count + + cx_insert_count + cx_update_count + cx_delete_count + + table_insert_count + table_update_count + table_delete_count), + is_row_level BIT, + is_spatial BIT, + index_dml BIT, + table_dml BIT, + long_running_low_cpu BIT, + low_cost_high_cpu BIT, + stale_stats BIT, + is_adaptive BIT, + is_slow_plan BIT, + is_compile_more BIT, + index_spool_cost FLOAT, + index_spool_rows FLOAT, + is_spool_expensive BIT, + is_spool_more_rows BIT, + estimated_rows FLOAT, + is_bad_estimate BIT, + is_big_log BIT, + is_big_tempdb BIT, + is_paul_white_electric BIT, + is_row_goal BIT, + is_mstvf BIT, + is_mm_join BIT, + is_nonsargable BIT, + busy_loops BIT, + tvf_join BIT, + implicit_conversion_info XML, + cached_execution_parameters XML, + missing_indexes XML, + warnings NVARCHAR(4000) + INDEX ww_ix_ids CLUSTERED (plan_id, query_id, query_hash, sql_handle) +); + + +DROP TABLE IF EXISTS #working_wait_stats; + +CREATE TABLE #working_wait_stats +( + plan_id BIGINT, + wait_category TINYINT, + wait_category_desc NVARCHAR(258), + total_query_wait_time_ms BIGINT, + avg_query_wait_time_ms DECIMAL(38, 2), + last_query_wait_time_ms BIGINT, + min_query_wait_time_ms BIGINT, + max_query_wait_time_ms BIGINT, + wait_category_mapped AS CASE wait_category + WHEN 0 THEN N'UNKNOWN' + WHEN 1 THEN N'SOS_SCHEDULER_YIELD' + WHEN 2 THEN N'THREADPOOL' + WHEN 3 THEN N'LCK_M_%' + WHEN 4 THEN N'LATCH_%' + WHEN 5 THEN N'PAGELATCH_%' + WHEN 6 THEN N'PAGEIOLATCH_%' + WHEN 7 THEN N'RESOURCE_SEMAPHORE_QUERY_COMPILE' + WHEN 8 THEN N'CLR%, SQLCLR%' + WHEN 9 THEN N'DBMIRROR%' + WHEN 10 THEN N'XACT%, DTC%, TRAN_MARKLATCH_%, MSQL_XACT_%, TRANSACTION_MUTEX' + WHEN 11 THEN N'SLEEP_%, LAZYWRITER_SLEEP, SQLTRACE_BUFFER_FLUSH, SQLTRACE_INCREMENTAL_FLUSH_SLEEP, SQLTRACE_WAIT_ENTRIES, FT_IFTS_SCHEDULER_IDLE_WAIT, XE_DISPATCHER_WAIT, REQUEST_FOR_DEADLOCK_SEARCH, LOGMGR_QUEUE, ONDEMAND_TASK_QUEUE, CHECKPOINT_QUEUE, XE_TIMER_EVENT' + WHEN 12 THEN N'PREEMPTIVE_%' + WHEN 13 THEN N'BROKER_% (but not BROKER_RECEIVE_WAITFOR)' + WHEN 14 THEN N'LOGMGR, LOGBUFFER, LOGMGR_RESERVE_APPEND, LOGMGR_FLUSH, LOGMGR_PMM_LOG, CHKPT, WRITELOG' + WHEN 15 THEN N'ASYNC_NETWORK_IO, NET_WAITFOR_PACKET, PROXY_NETWORK_IO, EXTERNAL_SCRIPT_NETWORK_IOF' + WHEN 16 THEN N'CXPACKET, EXCHANGE, CXCONSUMER' + WHEN 17 THEN N'RESOURCE_SEMAPHORE, CMEMTHREAD, CMEMPARTITIONED, EE_PMOLOCK, MEMORY_ALLOCATION_EXT, RESERVED_MEMORY_ALLOCATION_EXT, MEMORY_GRANT_UPDATE' + WHEN 18 THEN N'WAITFOR, WAIT_FOR_RESULTS, BROKER_RECEIVE_WAITFOR' + WHEN 19 THEN N'TRACEWRITE, SQLTRACE_LOCK, SQLTRACE_FILE_BUFFER, SQLTRACE_FILE_WRITE_IO_COMPLETION, SQLTRACE_FILE_READ_IO_COMPLETION, SQLTRACE_PENDING_BUFFER_WRITERS, SQLTRACE_SHUTDOWN, QUERY_TRACEOUT, TRACE_EVTNOTIFF' + WHEN 20 THEN N'FT_RESTART_CRAWL, FULLTEXT GATHERER, MSSEARCH, FT_METADATA_MUTEX, FT_IFTSHC_MUTEX, FT_IFTSISM_MUTEX, FT_IFTS_RWLOCK, FT_COMPROWSET_RWLOCK, FT_MASTER_MERGE, FT_PROPERTYLIST_CACHE, FT_MASTER_MERGE_COORDINATOR, PWAIT_RESOURCE_SEMAPHORE_FT_PARALLEL_QUERY_SYNC' + WHEN 21 THEN N'ASYNC_IO_COMPLETION, IO_COMPLETION, BACKUPIO, WRITE_COMPLETION, IO_QUEUE_LIMIT, IO_RETRY' + WHEN 22 THEN N'SE_REPL_%, REPL_%, HADR_% (but not HADR_THROTTLE_LOG_RATE_GOVERNOR), PWAIT_HADR_%, REPLICA_WRITES, FCB_REPLICA_WRITE, FCB_REPLICA_READ, PWAIT_HADRSIM' + WHEN 23 THEN N'LOG_RATE_GOVERNOR, POOL_LOG_RATE_GOVERNOR, HADR_THROTTLE_LOG_RATE_GOVERNOR, INSTANCE_LOG_RATE_GOVERNOR' + END, + INDEX wws_ix_ids CLUSTERED ( plan_id) +); + + +/* +The next three tables hold plan XML parsed out to different degrees +*/ +DROP TABLE IF EXISTS #statements; + +CREATE TABLE #statements +( + plan_id BIGINT, + query_id BIGINT, + query_hash BINARY(8), + sql_handle VARBINARY(64), + statement XML, + is_cursor BIT + INDEX s_ix_ids CLUSTERED (plan_id, query_id, query_hash, sql_handle) +); + + +DROP TABLE IF EXISTS #query_plan; + +CREATE TABLE #query_plan +( + plan_id BIGINT, + query_id BIGINT, + query_hash BINARY(8), + sql_handle VARBINARY(64), + query_plan XML, + INDEX qp_ix_ids CLUSTERED (plan_id, query_id, query_hash, sql_handle) +); + + +DROP TABLE IF EXISTS #relop; + +CREATE TABLE #relop +( + plan_id BIGINT, + query_id BIGINT, + query_hash BINARY(8), + sql_handle VARBINARY(64), + relop XML, + INDEX ix_ids CLUSTERED (plan_id, query_id, query_hash, sql_handle) +); + + +DROP TABLE IF EXISTS #plan_cost; + +CREATE TABLE #plan_cost +( + query_plan_cost DECIMAL(38,2), + sql_handle VARBINARY(64), + plan_id INT, + INDEX px_ix_ids CLUSTERED (sql_handle, plan_id) +); + + +DROP TABLE IF EXISTS #est_rows; + +CREATE TABLE #est_rows +( + estimated_rows DECIMAL(38,2), + query_hash BINARY(8), + INDEX px_ix_ids CLUSTERED (query_hash) +); + + +DROP TABLE IF EXISTS #stats_agg; + +CREATE TABLE #stats_agg +( + sql_handle VARBINARY(64), + last_update DATETIME2, + modification_count BIGINT, + sampling_percent DECIMAL(38, 2), + [statistics] NVARCHAR(258), + [table] NVARCHAR(258), + [schema] NVARCHAR(258), + [database] NVARCHAR(258), + INDEX sa_ix_ids CLUSTERED (sql_handle) +); + + +DROP TABLE IF EXISTS #trace_flags; + +CREATE TABLE #trace_flags +( + sql_handle VARBINARY(54), + global_trace_flags NVARCHAR(4000), + session_trace_flags NVARCHAR(4000), + INDEX tf_ix_ids CLUSTERED (sql_handle) +); + + +DROP TABLE IF EXISTS #warning_results; + +CREATE TABLE #warning_results +( + ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, + CheckID INT, + Priority TINYINT, + FindingsGroup NVARCHAR(50), + Finding NVARCHAR(200), + URL NVARCHAR(200), + Details NVARCHAR(4000) +); + +/*These next three tables hold information about implicit conversion and cached parameters */ +DROP TABLE IF EXISTS #stored_proc_info; + +CREATE TABLE #stored_proc_info +( + sql_handle VARBINARY(64), + query_hash BINARY(8), + variable_name NVARCHAR(258), + variable_datatype NVARCHAR(258), + converted_column_name NVARCHAR(258), + compile_time_value NVARCHAR(258), + proc_name NVARCHAR(1000), + column_name NVARCHAR(4000), + converted_to NVARCHAR(258), + set_options NVARCHAR(1000) + INDEX tf_ix_ids CLUSTERED (sql_handle, query_hash) +); + +DROP TABLE IF EXISTS #variable_info; + +CREATE TABLE #variable_info +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + proc_name NVARCHAR(1000), + variable_name NVARCHAR(258), + variable_datatype NVARCHAR(258), + compile_time_value NVARCHAR(258), + INDEX vif_ix_ids CLUSTERED (sql_handle, query_hash) +); + +DROP TABLE IF EXISTS #conversion_info; + +CREATE TABLE #conversion_info +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + proc_name NVARCHAR(128), + expression NVARCHAR(4000), + at_charindex AS CHARINDEX('@', expression), + bracket_charindex AS CHARINDEX(']', expression, CHARINDEX('@', expression)) - CHARINDEX('@', expression), + comma_charindex AS CHARINDEX(',', expression) + 1, + second_comma_charindex AS + CHARINDEX(',', expression, CHARINDEX(',', expression) + 1) - CHARINDEX(',', expression) - 1, + equal_charindex AS CHARINDEX('=', expression) + 1, + paren_charindex AS CHARINDEX('(', expression) + 1, + comma_paren_charindex AS + CHARINDEX(',', expression, CHARINDEX('(', expression) + 1) - CHARINDEX('(', expression) - 1, + convert_implicit_charindex AS CHARINDEX('=CONVERT_IMPLICIT', expression), + INDEX cif_ix_ids CLUSTERED (sql_handle, query_hash) +); + +/* These tables support the Missing Index details clickable*/ + + +DROP TABLE IF EXISTS #missing_index_xml; + +CREATE TABLE #missing_index_xml +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + impact FLOAT, + index_xml XML, + INDEX mix_ix_ids CLUSTERED (sql_handle, query_hash) +); + +DROP TABLE IF EXISTS #missing_index_schema; + +CREATE TABLE #missing_index_schema +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + index_xml XML, + INDEX mis_ix_ids CLUSTERED (sql_handle, query_hash) +); + + +DROP TABLE IF EXISTS #missing_index_usage; + +CREATE TABLE #missing_index_usage +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + usage NVARCHAR(128), + index_xml XML, + INDEX miu_ix_ids CLUSTERED (sql_handle, query_hash) +); + +DROP TABLE IF EXISTS #missing_index_detail; + +CREATE TABLE #missing_index_detail +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + usage NVARCHAR(128), + column_name NVARCHAR(128), + INDEX mid_ix_ids CLUSTERED (sql_handle, query_hash) +); + + +DROP TABLE IF EXISTS #missing_index_pretty; + +CREATE TABLE #missing_index_pretty +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + equality NVARCHAR(MAX), + inequality NVARCHAR(MAX), + [include] NVARCHAR(MAX), + is_spool BIT, + details AS N'/* ' + + CHAR(10) + + CASE is_spool + WHEN 0 + THEN N'The Query Processor estimates that implementing the ' + ELSE N'We estimate that implementing the ' + END + + CONVERT(NVARCHAR(30), impact) + + '%.' + + CHAR(10) + + N'*/' + + CHAR(10) + CHAR(13) + + N'/* ' + + CHAR(10) + + N'USE ' + + database_name + + CHAR(10) + + N'GO' + + CHAR(10) + CHAR(13) + + N'CREATE NONCLUSTERED INDEX ix_' + + ISNULL(REPLACE(REPLACE(REPLACE(equality,'[', ''), ']', ''), ', ', '_'), '') + + ISNULL(REPLACE(REPLACE(REPLACE(inequality,'[', ''), ']', ''), ', ', '_'), '') + + CASE WHEN [include] IS NOT NULL THEN + N'_Includes' ELSE N'' END + + CHAR(10) + + N' ON ' + + schema_name + + N'.' + + table_name + + N' (' + + + CASE WHEN equality IS NOT NULL + THEN equality + + CASE WHEN inequality IS NOT NULL + THEN N', ' + inequality + ELSE N'' + END + ELSE inequality + END + + N')' + + CHAR(10) + + CASE WHEN include IS NOT NULL + THEN N'INCLUDE (' + include + N') WITH (FILLFACTOR=100, ONLINE=?, SORT_IN_TEMPDB=?, DATA_COMPRESSION=?);' + ELSE N' WITH (FILLFACTOR=100, ONLINE=?, SORT_IN_TEMPDB=?, DATA_COMPRESSION=?);' + END + + CHAR(10) + + N'GO' + + CHAR(10) + + N'*/', + INDEX mip_ix_ids CLUSTERED (sql_handle, query_hash) +); + +DROP TABLE IF EXISTS #index_spool_ugly; + +CREATE TABLE #index_spool_ugly +( + query_hash BINARY(8), + sql_handle VARBINARY(64), + impact FLOAT, + database_name NVARCHAR(128), + schema_name NVARCHAR(128), + table_name NVARCHAR(128), + equality NVARCHAR(MAX), + inequality NVARCHAR(MAX), + [include] NVARCHAR(MAX), + INDEX isu_ix_ids CLUSTERED (sql_handle, query_hash) +); + + +/*Sets up WHERE clause that gets used quite a bit*/ + +--Date stuff +--If they're both NULL, we'll just look at the last 7 days +IF (@StartDate IS NULL AND @EndDate IS NULL) + BEGIN + RAISERROR(N'@StartDate and @EndDate are NULL, checking last 7 days', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.last_execution_time >= DATEADD(DAY, -7, DATEDIFF(DAY, 0, SYSDATETIME() )) + '; + END; + +--Hey, that's nice of me +IF @StartDate IS NOT NULL + BEGIN + RAISERROR(N'Setting start date filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.last_execution_time >= @sp_StartDate + '; + END; + +--Alright, sensible +IF @EndDate IS NOT NULL + BEGIN + RAISERROR(N'Setting end date filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.last_execution_time < @sp_EndDate + '; + END; + +--C'mon, why would you do that? +IF (@StartDate IS NULL AND @EndDate IS NOT NULL) + BEGIN + RAISERROR(N'Setting reasonable start date filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.last_execution_time >= DATEADD(DAY, -7, @sp_EndDate) + '; + END; + +--Jeez, abusive +IF (@StartDate IS NOT NULL AND @EndDate IS NULL) + BEGIN + RAISERROR(N'Setting reasonable end date filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.last_execution_time < DATEADD(DAY, 7, @sp_StartDate) + '; + END; + +--I care about minimum execution counts +IF @MinimumExecutionCount IS NOT NULL + BEGIN + RAISERROR(N'Setting execution filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.count_executions >= @sp_MinimumExecutionCount + '; + END; + +--You care about stored proc names +IF @StoredProcName IS NOT NULL + BEGIN + RAISERROR(N'Setting stored proc filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND object_name(qsq.object_id, DB_ID(' + QUOTENAME(@DatabaseName, '''') + N')) = @sp_StoredProcName + '; + END; + +--I will always love you, but hopefully this query will eventually end +IF @DurationFilter IS NOT NULL + BEGIN + RAISERROR(N'Setting duration filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND (qsrs.avg_duration / 1000.) >= @sp_MinDuration + '; + END; + +--I don't know why you'd go looking for failed queries, but hey +IF (@Failed = 0 OR @Failed IS NULL) + BEGIN + RAISERROR(N'Setting failed query filter to 0', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.execution_type = 0 + '; + END; +IF (@Failed = 1) + BEGIN + RAISERROR(N'Setting failed query filter to 3, 4', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsrs.execution_type IN (3, 4) + '; + END; + +/*Filtering for plan_id or query_id*/ +IF (@PlanIdFilter IS NOT NULL) + BEGIN + RAISERROR(N'Setting plan_id filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsp.plan_id = @sp_PlanIdFilter + '; + END; + +IF (@QueryIdFilter IS NOT NULL) + BEGIN + RAISERROR(N'Setting query_id filter', 0, 1) WITH NOWAIT; + SET @sql_where += N' AND qsq.query_id = @sp_QueryIdFilter + '; + END; + +IF @Debug = 1 + RAISERROR(N'Starting WHERE clause:', 0, 1) WITH NOWAIT; + PRINT @sql_where; + +IF @sql_where IS NULL + BEGIN + RAISERROR(N'@sql_where is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +IF (@ExportToExcel = 1 OR @SkipXML = 1) + BEGIN + RAISERROR(N'Exporting to Excel or skipping XML, hiding summary', 0, 1) WITH NOWAIT; + SET @HideSummary = 1; + END; + +IF @StoredProcName IS NOT NULL + BEGIN + + DECLARE @sql NVARCHAR(MAX); + DECLARE @out INT; + DECLARE @proc_params NVARCHAR(MAX) = N'@sp_StartDate DATETIME2, @sp_EndDate DATETIME2, @sp_MinimumExecutionCount INT, @sp_MinDuration INT, @sp_StoredProcName NVARCHAR(128), @sp_PlanIdFilter INT, @sp_QueryIdFilter INT, @i_out INT OUTPUT'; + + + SET @sql = N'SELECT @i_out = COUNT(*) + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp + ON qsp.plan_id = qsrs.plan_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq + ON qsq.query_id = qsp.query_id + WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + + SET @sql += @sql_where; + + EXEC sys.sp_executesql @sql, + @proc_params, + @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter, @i_out = @out OUTPUT; + + IF @out = 0 + BEGIN + + SET @msg = N'We couldn''t find the Stored Procedure ' + QUOTENAME(@StoredProcName) + N' in the Query Store views for ' + QUOTENAME(@DatabaseName) + N' between ' + CONVERT(NVARCHAR(30), ISNULL(@StartDate, DATEADD(DAY, -7, DATEDIFF(DAY, 0, SYSDATETIME() ))) ) + N' and ' + CONVERT(NVARCHAR(30), ISNULL(@EndDate, SYSDATETIME())) + + '. Try removing schema prefixes or adjusting dates. If it was executed from a different database context, try searching there instead.'; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + + SELECT @msg AS [Blue Flowers, Blue Flowers, Blue Flowers]; + + RETURN; + + END; + + END; + + + + +/* +This is our grouped interval query. + +By default, it looks at queries: + In the last 7 days + That aren't system queries + That have a query plan (some won't, if nested level is > 128, along with other reasons) + And haven't failed + This stuff, along with some other options, will be configurable in the stored proc + +*/ + +IF @sql_where IS NOT NULL +BEGIN TRY + BEGIN + + RAISERROR(N'Populating temp tables', 0, 1) WITH NOWAIT; + +RAISERROR(N'Gathering intervals', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +SELECT CONVERT(DATE, qsrs.last_execution_time) AS flat_date, + MIN(DATEADD(HOUR, DATEDIFF(HOUR, 0, qsrs.last_execution_time), 0)) AS start_range, + MAX(DATEADD(HOUR, DATEDIFF(HOUR, 0, qsrs.last_execution_time) + 1, 0)) AS end_range, + SUM(qsrs.avg_duration / 1000.) / SUM(qsrs.count_executions) AS total_avg_duration_ms, + SUM(qsrs.avg_cpu_time / 1000.) / SUM(qsrs.count_executions) AS total_avg_cpu_time_ms, + SUM((qsrs.avg_logical_io_reads * 8 ) / 1024.) / SUM(qsrs.count_executions) AS total_avg_logical_io_reads_mb, + SUM((qsrs.avg_physical_io_reads* 8 ) / 1024.) / SUM(qsrs.count_executions) AS total_avg_physical_io_reads_mb, + SUM((qsrs.avg_logical_io_writes* 8 ) / 1024.) / SUM(qsrs.count_executions) AS total_avg_logical_io_writes_mb, + SUM((qsrs.avg_query_max_used_memory * 8 ) / 1024.) / SUM(qsrs.count_executions) AS total_avg_query_max_used_memory_mb, + SUM(qsrs.avg_rowcount) AS total_rowcount, + SUM(qsrs.count_executions) AS total_count_executions, + SUM(qsrs.max_duration / 1000.) AS total_max_duration_ms, + SUM(qsrs.max_cpu_time / 1000.) AS total_max_cpu_time_ms, + SUM((qsrs.max_logical_io_reads * 8 ) / 1024.) AS total_max_logical_io_reads_mb, + SUM((qsrs.max_physical_io_reads* 8 ) / 1024.) AS total_max_physical_io_reads_mb, + SUM((qsrs.max_logical_io_writes* 8 ) / 1024.) AS total_max_logical_io_writes_mb, + SUM((qsrs.max_query_max_used_memory * 8 ) / 1024.) AS total_max_query_max_used_memory_mb '; + IF @new_columns = 1 + BEGIN + SET @sql_select += N', + SUM((qsrs.avg_log_bytes_used) / 1048576.) / SUM(qsrs.count_executions) AS total_avg_log_bytes_mb, + SUM(qsrs.avg_tempdb_space_used) / SUM(qsrs.count_executions) AS total_avg_tempdb_space, + SUM((qsrs.max_log_bytes_used) / 1048576.) AS total_max_log_bytes_mb, + SUM(qsrs.max_tempdb_space_used) AS total_max_tempdb_space + '; + END; + IF @new_columns = 0 + BEGIN + SET @sql_select += N', + NULL AS total_avg_log_bytes_mb, + NULL AS total_avg_tempdb_space, + NULL AS total_max_log_bytes_mb, + NULL AS total_max_tempdb_space + '; + END; + + +SET @sql_select += N'FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp + ON qsp.plan_id = qsrs.plan_id + JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq + ON qsq.query_id = qsp.query_id + WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + + +SET @sql_select += @sql_where; + +SET @sql_select += + N'GROUP BY CONVERT(DATE, qsrs.last_execution_time) + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +INSERT #grouped_interval WITH (TABLOCK) + ( flat_date, start_range, end_range, total_avg_duration_ms, + total_avg_cpu_time_ms, total_avg_logical_io_reads_mb, total_avg_physical_io_reads_mb, + total_avg_logical_io_writes_mb, total_avg_query_max_used_memory_mb, total_rowcount, + total_count_executions, total_max_duration_ms, total_max_cpu_time_ms, total_max_logical_io_reads_mb, + total_max_physical_io_reads_mb, total_max_logical_io_writes_mb, total_max_query_max_used_memory_mb, + total_avg_log_bytes_mb, total_avg_tempdb_space, total_max_log_bytes_mb, total_max_tempdb_space ) + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/* +The next group of queries looks at plans in the ranges we found in the grouped interval query + +We take the highest value from each metric (duration, cpu, etc) and find the top plans by that metric in the range + +They insert into the #working_plans table +*/ + + + +/*Get longest duration plans*/ + +RAISERROR(N'Gathering longest duration plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH duration_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_duration_ms DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg duration'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN duration_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_duration DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH duration_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_duration_ms DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max duration'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN duration_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_duration DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get longest cpu plans*/ + +RAISERROR(N'Gathering highest cpu plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH cpu_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_cpu_time_ms DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg cpu'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN cpu_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_cpu_time DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH cpu_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_cpu_time_ms DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max cpu'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN cpu_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_cpu_time DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get highest logical read plans*/ + +RAISERROR(N'Gathering highest logical read plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH logical_reads_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_logical_io_reads_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg logical reads'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN logical_reads_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_logical_io_reads DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH logical_reads_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_logical_io_reads_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max logical reads'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN logical_reads_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_logical_io_reads DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get highest physical read plans*/ + +RAISERROR(N'Gathering highest physical read plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH physical_read_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_physical_io_reads_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg physical reads'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN physical_read_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_physical_io_reads DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH physical_read_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_physical_io_reads_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max physical reads'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN physical_read_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_physical_io_reads DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get highest logical write plans*/ + +RAISERROR(N'Gathering highest write plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH logical_writes_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_logical_io_writes_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg writes'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN logical_writes_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_logical_io_writes DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH logical_writes_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_logical_io_writes_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max writes'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN logical_writes_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_logical_io_writes DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get highest memory use plans*/ + +RAISERROR(N'Gathering highest memory use plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH memory_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_query_max_used_memory_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg memory'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN memory_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_query_max_used_memory DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH memory_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_query_max_used_memory_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max memory'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN memory_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_query_max_used_memory DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get highest row count plans*/ + +RAISERROR(N'Gathering highest row count plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH rowcount_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_rowcount DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg rows'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN rowcount_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_rowcount DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +IF @new_columns = 1 +BEGIN + +RAISERROR(N'Gathering new 2017 new column info...', 0, 1) WITH NOWAIT; + +/*Get highest log byte count plans*/ + +RAISERROR(N'Gathering highest log byte use plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH rowcount_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_log_bytes_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg log bytes'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN rowcount_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_log_bytes_used DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + +RAISERROR(N'Gathering highest log byte use plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH rowcount_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_log_bytes_mb DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max log bytes'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN rowcount_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_log_bytes_used DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +/*Get highest tempdb use plans*/ + +RAISERROR(N'Gathering highest tempdb use plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH rowcount_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_avg_tempdb_space DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''avg tempdb space'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN rowcount_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.avg_tempdb_space_used DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +WITH rowcount_max +AS ( SELECT TOP 1 + gi.start_range, + gi.end_range + FROM #grouped_interval AS gi + ORDER BY gi.total_max_tempdb_space DESC ) +INSERT #working_plans WITH (TABLOCK) + ( plan_id, query_id, pattern ) +SELECT TOP ( @sp_Top ) + qsp.plan_id, qsp.query_id, ''max tempdb space'' +FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +JOIN rowcount_max AS dm +ON qsp.last_execution_time >= dm.start_range + AND qsp.last_execution_time < dm.end_range +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = qsp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON qsq.query_id = qsp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'ORDER BY qsrs.max_tempdb_space_used DESC + OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + +END; + + +/* +This rolls up the different patterns we find before deduplicating. + +The point of this is so we know if a query was gathered by one or more of the search queries + +*/ + +RAISERROR(N'Updating patterns', 0, 1) WITH NOWAIT; + +WITH patterns AS ( +SELECT wp.plan_id, wp.query_id, + pattern_path = STUFF((SELECT DISTINCT N', ' + wp2.pattern + FROM #working_plans AS wp2 + WHERE wp.plan_id = wp2.plan_id + AND wp.query_id = wp2.query_id + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') +FROM #working_plans AS wp +) +UPDATE wp +SET wp.pattern = patterns.pattern_path +FROM #working_plans AS wp +JOIN patterns +ON wp.plan_id = patterns.plan_id +AND wp.query_id = patterns.query_id +OPTION (RECOMPILE); + + +/* +This dedupes our results so we hopefully don't double-work the same plan +*/ + +RAISERROR(N'Deduplicating gathered plans', 0, 1) WITH NOWAIT; + +WITH dedupe AS ( +SELECT * , ROW_NUMBER() OVER (PARTITION BY wp.plan_id ORDER BY wp.plan_id) AS dupes +FROM #working_plans AS wp +) +DELETE dedupe +WHERE dedupe.dupes > 1 +OPTION (RECOMPILE); + +SET @msg = N'Removed ' + CONVERT(NVARCHAR(10), @@ROWCOUNT) + N' duplicate plan_ids.'; +RAISERROR(@msg, 0, 1) WITH NOWAIT; + + +/* +This gathers data for the #working_metrics table +*/ + + +RAISERROR(N'Collecting worker metrics', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +SELECT ' + QUOTENAME(@DatabaseName, '''') + N' AS database_name, wp.plan_id, wp.query_id, + QUOTENAME(object_schema_name(qsq.object_id, DB_ID(' + QUOTENAME(@DatabaseName, '''') + N'))) + ''.'' + + QUOTENAME(object_name(qsq.object_id, DB_ID(' + QUOTENAME(@DatabaseName, '''') + N'))) AS proc_or_function_name, + qsq.batch_sql_handle, qsq.query_hash, qsq.query_parameterization_type_desc, qsq.count_compiles, + (qsq.avg_compile_duration / 1000.), + (qsq.last_compile_duration / 1000.), + (qsq.avg_bind_duration / 1000.), + (qsq.last_bind_duration / 1000.), + (qsq.avg_bind_cpu_time / 1000.), + (qsq.last_bind_cpu_time / 1000.), + (qsq.avg_optimize_duration / 1000.), + (qsq.last_optimize_duration / 1000.), + (qsq.avg_optimize_cpu_time / 1000.), + (qsq.last_optimize_cpu_time / 1000.), + (qsq.avg_compile_memory_kb / 1024.), + (qsq.last_compile_memory_kb / 1024.), + qsrs.execution_type_desc, qsrs.first_execution_time, qsrs.last_execution_time, qsrs.count_executions, + (qsrs.avg_duration / 1000.), + (qsrs.last_duration / 1000.), + (qsrs.min_duration / 1000.), + (qsrs.max_duration / 1000.), + (qsrs.avg_cpu_time / 1000.), + (qsrs.last_cpu_time / 1000.), + (qsrs.min_cpu_time / 1000.), + (qsrs.max_cpu_time / 1000.), + ((qsrs.avg_logical_io_reads * 8 ) / 1024.), + ((qsrs.last_logical_io_reads * 8 ) / 1024.), + ((qsrs.min_logical_io_reads * 8 ) / 1024.), + ((qsrs.max_logical_io_reads * 8 ) / 1024.), + ((qsrs.avg_logical_io_writes * 8 ) / 1024.), + ((qsrs.last_logical_io_writes * 8 ) / 1024.), + ((qsrs.min_logical_io_writes * 8 ) / 1024.), + ((qsrs.max_logical_io_writes * 8 ) / 1024.), + ((qsrs.avg_physical_io_reads * 8 ) / 1024.), + ((qsrs.last_physical_io_reads * 8 ) / 1024.), + ((qsrs.min_physical_io_reads * 8 ) / 1024.), + ((qsrs.max_physical_io_reads * 8 ) / 1024.), + (qsrs.avg_clr_time / 1000.), + (qsrs.last_clr_time / 1000.), + (qsrs.min_clr_time / 1000.), + (qsrs.max_clr_time / 1000.), + qsrs.avg_dop, qsrs.last_dop, qsrs.min_dop, qsrs.max_dop, + ((qsrs.avg_query_max_used_memory * 8 ) / 1024.), + ((qsrs.last_query_max_used_memory * 8 ) / 1024.), + ((qsrs.min_query_max_used_memory * 8 ) / 1024.), + ((qsrs.max_query_max_used_memory * 8 ) / 1024.), + qsrs.avg_rowcount, qsrs.last_rowcount, qsrs.min_rowcount, qsrs.max_rowcount,'; + + IF @new_columns = 1 + BEGIN + SET @sql_select += N' + qsrs.avg_num_physical_io_reads, qsrs.last_num_physical_io_reads, qsrs.min_num_physical_io_reads, qsrs.max_num_physical_io_reads, + (qsrs.avg_log_bytes_used / 100000000), + (qsrs.last_log_bytes_used / 100000000), + (qsrs.min_log_bytes_used / 100000000), + (qsrs.max_log_bytes_used / 100000000), + ((qsrs.avg_tempdb_space_used * 8 ) / 1024.), + ((qsrs.last_tempdb_space_used * 8 ) / 1024.), + ((qsrs.min_tempdb_space_used * 8 ) / 1024.), + ((qsrs.max_tempdb_space_used * 8 ) / 1024.) + '; + END; + IF @new_columns = 0 + BEGIN + SET @sql_select += N' + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL + '; + END; +SET @sql_select += +N'FROM #working_plans AS wp +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON wp.query_id = qsq.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = wp.plan_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +ON qsp.plan_id = wp.plan_id +AND qsp.query_id = wp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +INSERT #working_metrics WITH (TABLOCK) + ( database_name, plan_id, query_id, + proc_or_function_name, + batch_sql_handle, query_hash, query_parameterization_type_desc, count_compiles, + avg_compile_duration, last_compile_duration, avg_bind_duration, last_bind_duration, avg_bind_cpu_time, last_bind_cpu_time, avg_optimize_duration, + last_optimize_duration, avg_optimize_cpu_time, last_optimize_cpu_time, avg_compile_memory_kb, last_compile_memory_kb, execution_type_desc, + first_execution_time, last_execution_time, count_executions, avg_duration, last_duration, min_duration, max_duration, avg_cpu_time, last_cpu_time, + min_cpu_time, max_cpu_time, avg_logical_io_reads, last_logical_io_reads, min_logical_io_reads, max_logical_io_reads, avg_logical_io_writes, + last_logical_io_writes, min_logical_io_writes, max_logical_io_writes, avg_physical_io_reads, last_physical_io_reads, min_physical_io_reads, + max_physical_io_reads, avg_clr_time, last_clr_time, min_clr_time, max_clr_time, avg_dop, last_dop, min_dop, max_dop, avg_query_max_used_memory, + last_query_max_used_memory, min_query_max_used_memory, max_query_max_used_memory, avg_rowcount, last_rowcount, min_rowcount, max_rowcount, + /* 2017 only columns */ + avg_num_physical_io_reads, last_num_physical_io_reads, min_num_physical_io_reads, max_num_physical_io_reads, + avg_log_bytes_used, last_log_bytes_used, min_log_bytes_used, max_log_bytes_used, + avg_tempdb_space_used, last_tempdb_space_used, min_tempdb_space_used, max_tempdb_space_used ) + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + + +/*This just helps us classify our queries*/ +UPDATE #working_metrics +SET proc_or_function_name = N'Statement' +WHERE proc_or_function_name IS NULL +OPTION(RECOMPILE); + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' + WITH patterns AS ( + SELECT query_id, planid_path = STUFF((SELECT DISTINCT N'', '' + RTRIM(qsp2.plan_id) + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp2 + WHERE qsp.query_id = qsp2.query_id + FOR XML PATH(N''''), TYPE).value(N''.[1]'', N''NVARCHAR(MAX)''), 1, 2, N'''') + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp + ) + UPDATE wm + SET wm.query_id_all_plan_ids = patterns.planid_path + FROM #working_metrics AS wm + JOIN patterns + ON wm.query_id = patterns.query_id + OPTION (RECOMPILE); +' + +EXEC sys.sp_executesql @stmt = @sql_select; + +/* +This gathers data for the #working_plan_text table +*/ + + +RAISERROR(N'Gathering working plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +SELECT ' + QUOTENAME(@DatabaseName, '''') + N' AS database_name, wp.plan_id, wp.query_id, + qsp.plan_group_id, qsp.engine_version, qsp.compatibility_level, qsp.query_plan_hash, TRY_CAST(qsp.query_plan AS XML), qsp.is_online_index_plan, qsp.is_trivial_plan, + qsp.is_parallel_plan, qsp.is_forced_plan, qsp.is_natively_compiled, qsp.force_failure_count, qsp.last_force_failure_reason_desc, qsp.count_compiles, + qsp.initial_compile_start_time, qsp.last_compile_start_time, qsp.last_execution_time, + (qsp.avg_compile_duration / 1000.), + (qsp.last_compile_duration / 1000.), + qsqt.query_sql_text, qsqt.statement_sql_handle, qsqt.is_part_of_encrypted_module, qsqt.has_restricted_text +FROM #working_plans AS wp +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +ON qsp.plan_id = wp.plan_id + AND qsp.query_id = wp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON wp.query_id = qsq.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = wp.plan_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +INSERT #working_plan_text WITH (TABLOCK) + ( database_name, plan_id, query_id, + plan_group_id, engine_version, compatibility_level, query_plan_hash, query_plan_xml, is_online_index_plan, is_trivial_plan, + is_parallel_plan, is_forced_plan, is_natively_compiled, force_failure_count, last_force_failure_reason_desc, count_compiles, + initial_compile_start_time, last_compile_start_time, last_execution_time, avg_compile_duration, last_compile_duration, + query_sql_text, statement_sql_handle, is_part_of_encrypted_module, has_restricted_text ) + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + + + +/* +This gets us context settings for our queries and adds it to the #working_plan_text table +*/ + +RAISERROR(N'Gathering context settings', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +UPDATE wp +SET wp.context_settings = SUBSTRING( + CASE WHEN (CAST(qcs.set_options AS INT) & 1 = 1) THEN '', ANSI_PADDING'' ELSE '''' END + + CASE WHEN (CAST(qcs.set_options AS INT) & 8 = 8) THEN '', CONCAT_NULL_YIELDS_NULL'' ELSE '''' END + + CASE WHEN (CAST(qcs.set_options AS INT) & 16 = 16) THEN '', ANSI_WARNINGS'' ELSE '''' END + + CASE WHEN (CAST(qcs.set_options AS INT) & 32 = 32) THEN '', ANSI_NULLS'' ELSE '''' END + + CASE WHEN (CAST(qcs.set_options AS INT) & 64 = 64) THEN '', QUOTED_IDENTIFIER'' ELSE '''' END + + CASE WHEN (CAST(qcs.set_options AS INT) & 4096 = 4096) THEN '', ARITH_ABORT'' ELSE '''' END + + CASE WHEN (CAST(qcs.set_options AS INT) & 8192 = 8192) THEN '', NUMERIC_ROUNDABORT'' ELSE '''' END + , 2, 200000) +FROM #working_plan_text wp +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON wp.query_id = qsq.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_context_settings AS qcs +ON qcs.context_settings_id = qsq.context_settings_id +OPTION (RECOMPILE); +'; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select; + + +/*This adds the patterns we found from each interval to the #working_plan_text table*/ + +RAISERROR(N'Add patterns to working plans', 0, 1) WITH NOWAIT; + +UPDATE wpt +SET wpt.pattern = wp.pattern +FROM #working_plans AS wp +JOIN #working_plan_text AS wpt +ON wpt.plan_id = wp.plan_id +AND wpt.query_id = wp.query_id +OPTION (RECOMPILE); + +/*This cleans up query text a bit*/ + +RAISERROR(N'Clean awkward characters from query text', 0, 1) WITH NOWAIT; + +UPDATE b +SET b.query_sql_text = REPLACE(REPLACE(REPLACE(b.query_sql_text, @cr, ' '), @lf, ' '), @tab, ' ') +FROM #working_plan_text AS b +OPTION (RECOMPILE); + + +/*This populates #working_wait_stats when available*/ + +IF @waitstats = 1 + + BEGIN + + RAISERROR(N'Collecting wait stats info', 0, 1) WITH NOWAIT; + + + SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; + SET @sql_select += N' + SELECT qws.plan_id, + qws.wait_category, + qws.wait_category_desc, + SUM(qws.total_query_wait_time_ms) AS total_query_wait_time_ms, + SUM(qws.avg_query_wait_time_ms) AS avg_query_wait_time_ms, + SUM(qws.last_query_wait_time_ms) AS last_query_wait_time_ms, + SUM(qws.min_query_wait_time_ms) AS min_query_wait_time_ms, + SUM(qws.max_query_wait_time_ms) AS max_query_wait_time_ms + FROM ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_wait_stats qws + JOIN #working_plans AS wp + ON qws.plan_id = wp.plan_id + GROUP BY qws.plan_id, qws.wait_category, qws.wait_category_desc + HAVING SUM(qws.min_query_wait_time_ms) >= 5 + OPTION (RECOMPILE); + '; + + IF @Debug = 1 + PRINT @sql_select; + + IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + + INSERT #working_wait_stats WITH (TABLOCK) + ( plan_id, wait_category, wait_category_desc, total_query_wait_time_ms, avg_query_wait_time_ms, last_query_wait_time_ms, min_query_wait_time_ms, max_query_wait_time_ms ) + + EXEC sys.sp_executesql @stmt = @sql_select; + + + /*This updates #working_plan_text with the top three waits from the wait stats DMV*/ + + RAISERROR(N'Update working_plan_text with top three waits', 0, 1) WITH NOWAIT; + + + UPDATE wpt + SET wpt.top_three_waits = x.top_three_waits + FROM #working_plan_text AS wpt + JOIN ( + SELECT wws.plan_id, + top_three_waits = STUFF((SELECT TOP 3 N', ' + wws2.wait_category_desc + N' (' + CONVERT(NVARCHAR(20), SUM(CONVERT(BIGINT, wws2.avg_query_wait_time_ms))) + N' ms) ' + FROM #working_wait_stats AS wws2 + WHERE wws.plan_id = wws2.plan_id + GROUP BY wws2.wait_category_desc + ORDER BY SUM(wws2.avg_query_wait_time_ms) DESC + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') + FROM #working_wait_stats AS wws + GROUP BY wws.plan_id + ) AS x + ON x.plan_id = wpt.plan_id + OPTION (RECOMPILE); + +END; + +/*End wait stats population*/ + +UPDATE #working_plan_text +SET top_three_waits = CASE + WHEN @waitstats = 0 + THEN N'The query store waits stats DMV is not available' + ELSE N'No Significant waits detected!' + END +WHERE top_three_waits IS NULL +OPTION(RECOMPILE); + +END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure populating temp tables.', 0,1) WITH NOWAIT; + + IF @sql_select IS NOT NULL + BEGIN + SET @msg = N'Last @sql_select: ' + @sql_select; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @error_severity = ERROR_SEVERITY(), @error_state = ERROR_STATE(); + RAISERROR (@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + RETURN; +END CATCH; + +IF (@SkipXML = 0) +BEGIN TRY +BEGIN + +/* +This sets up the #working_warnings table with the IDs we're interested in so we can tie warnings back to them +*/ + +RAISERROR(N'Populate working warnings table with gathered plans', 0, 1) WITH NOWAIT; + + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +SELECT DISTINCT wp.plan_id, wp.query_id, qsq.query_hash, qsqt.statement_sql_handle +FROM #working_plans AS wp +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +ON qsp.plan_id = wp.plan_id + AND qsp.query_id = wp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON wp.query_id = qsq.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = wp.plan_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += N'OPTION (RECOMPILE); + '; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +INSERT #working_warnings WITH (TABLOCK) + ( plan_id, query_id, query_hash, sql_handle ) +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + +/* +This looks for queries in the query stores that we picked up from an internal that have multiple plans in cache + +This and several of the following queries all replaced XML parsing to find plan attributes. Sweet. + +Thanks, Query Store +*/ + +RAISERROR(N'Populating object name in #working_warnings', 0, 1) WITH NOWAIT; +UPDATE w +SET w.proc_or_function_name = ISNULL(wm.proc_or_function_name, N'Statement') +FROM #working_warnings AS w +JOIN #working_metrics AS wm +ON w.plan_id = wm.plan_id + AND w.query_id = wm.query_id +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for multiple plans', 0, 1) WITH NOWAIT; + +SET @sql_select = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'; +SET @sql_select += N' +UPDATE ww +SET ww.plan_multiple_plans = 1 +FROM #working_warnings AS ww +JOIN +( +SELECT wp.query_id, COUNT(qsp.plan_id) AS plans +FROM #working_plans AS wp +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_plan AS qsp +ON qsp.plan_id = wp.plan_id + AND qsp.query_id = wp.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query AS qsq +ON wp.query_id = qsq.query_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_query_text AS qsqt +ON qsqt.query_text_id = qsq.query_text_id +JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.query_store_runtime_stats AS qsrs +ON qsrs.plan_id = wp.plan_id +WHERE 1 = 1 + AND qsq.is_internal_query = 0 + AND qsp.query_plan IS NOT NULL + '; + +SET @sql_select += @sql_where; + +SET @sql_select += +N'GROUP BY wp.query_id + HAVING COUNT(qsp.plan_id) > 1 +) AS x + ON ww.query_id = x.query_id +OPTION (RECOMPILE); +'; + +IF @Debug = 1 + PRINT @sql_select; + +IF @sql_select IS NULL + BEGIN + RAISERROR(N'@sql_select is NULL', 0, 1) WITH NOWAIT; + RETURN; + END; + +EXEC sys.sp_executesql @stmt = @sql_select, + @params = @sp_params, + @sp_Top = @Top, @sp_StartDate = @StartDate, @sp_EndDate = @EndDate, @sp_MinimumExecutionCount = @MinimumExecutionCount, @sp_MinDuration = @duration_filter_ms, @sp_StoredProcName = @StoredProcName, @sp_PlanIdFilter = @PlanIdFilter, @sp_QueryIdFilter = @QueryIdFilter; + +/* +This looks for forced plans +*/ + +RAISERROR(N'Checking for forced plans', 0, 1) WITH NOWAIT; + +UPDATE ww +SET ww.is_forced_plan = 1 +FROM #working_warnings AS ww +JOIN #working_plan_text AS wp +ON ww.plan_id = wp.plan_id + AND ww.query_id = wp.query_id + AND wp.is_forced_plan = 1 +OPTION (RECOMPILE); + + +/* +This looks for forced parameterization +*/ + +RAISERROR(N'Checking for forced parameterization', 0, 1) WITH NOWAIT; + +UPDATE ww +SET ww.is_forced_parameterized = 1 +FROM #working_warnings AS ww +JOIN #working_metrics AS wm +ON ww.plan_id = wm.plan_id + AND ww.query_id = wm.query_id + AND wm.query_parameterization_type_desc = 'Forced' +OPTION (RECOMPILE); + + +/* +This looks for unparameterized queries +*/ + +RAISERROR(N'Checking for unparameterized plans', 0, 1) WITH NOWAIT; + +UPDATE ww +SET ww.unparameterized_query = 1 +FROM #working_warnings AS ww +JOIN #working_metrics AS wm +ON ww.plan_id = wm.plan_id + AND ww.query_id = wm.query_id + AND wm.query_parameterization_type_desc = 'None' + AND ww.proc_or_function_name = 'Statement' +OPTION (RECOMPILE); + + +/* +This looks for cursors +*/ + +RAISERROR(N'Checking for cursors', 0, 1) WITH NOWAIT; +UPDATE ww +SET ww.is_cursor = 1 +FROM #working_warnings AS ww +JOIN #working_plan_text AS wp +ON ww.plan_id = wp.plan_id + AND ww.query_id = wp.query_id + AND wp.plan_group_id > 0 +OPTION (RECOMPILE); + + +UPDATE ww +SET ww.is_cursor = 1 +FROM #working_warnings AS ww +JOIN #working_plan_text AS wp +ON ww.plan_id = wp.plan_id + AND ww.query_id = wp.query_id +WHERE ww.query_hash = 0x0000000000000000 +OR wp.query_plan_hash = 0x0000000000000000 +OPTION (RECOMPILE); + +/* +This looks for parallel plans +*/ +UPDATE ww +SET ww.is_parallel = 1 +FROM #working_warnings AS ww +JOIN #working_plan_text AS wp +ON ww.plan_id = wp.plan_id + AND ww.query_id = wp.query_id + AND wp.is_parallel_plan = 1 +OPTION (RECOMPILE); + +/*This looks for old CE*/ + +RAISERROR(N'Checking for legacy CE', 0, 1) WITH NOWAIT; + +UPDATE w +SET w.downlevel_estimator = 1 +FROM #working_warnings AS w +JOIN #working_plan_text AS wpt +ON w.plan_id = wpt.plan_id +AND w.query_id = wpt.query_id +/*PLEASE DON'T TELL ANYONE I DID THIS*/ +WHERE PARSENAME(wpt.engine_version, 4) < PARSENAME(CONVERT(VARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 4) +OPTION (RECOMPILE); +/*NO SERIOUSLY THIS IS A HORRIBLE IDEA*/ + + +/*Plans that compile 2x more than they execute*/ + +RAISERROR(N'Checking for plans that compile 2x more than they execute', 0, 1) WITH NOWAIT; + +UPDATE ww +SET ww.is_compile_more = 1 +FROM #working_warnings AS ww +JOIN #working_metrics AS wm +ON ww.plan_id = wm.plan_id + AND ww.query_id = wm.query_id + AND wm.count_compiles > (wm.count_executions * 2) +OPTION (RECOMPILE); + +/*Plans that compile 2x more than they execute*/ + +RAISERROR(N'Checking for plans that take more than 5 seconds to bind, compile, or optimize', 0, 1) WITH NOWAIT; + +UPDATE ww +SET ww.is_slow_plan = 1 +FROM #working_warnings AS ww +JOIN #working_metrics AS wm +ON ww.plan_id = wm.plan_id + AND ww.query_id = wm.query_id + AND (wm.avg_bind_duration > 5000 + OR + wm.avg_compile_duration > 5000 + OR + wm.avg_optimize_duration > 5000 + OR + wm.avg_optimize_cpu_time > 5000) +OPTION (RECOMPILE); + + + +/* +This parses the XML from our top plans into smaller chunks for easier consumption +*/ + +RAISERROR(N'Begin XML nodes parsing', 0, 1) WITH NOWAIT; + +RAISERROR(N'Inserting #statements', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #statements WITH (TABLOCK) ( plan_id, query_id, query_hash, sql_handle, statement, is_cursor ) + SELECT ww.plan_id, ww.query_id, ww.query_hash, ww.sql_handle, q.n.query('.') AS statement, 0 AS is_cursor + FROM #working_warnings AS ww + JOIN #working_plan_text AS wp + ON ww.plan_id = wp.plan_id + AND ww.query_id = wp.query_id + CROSS APPLY wp.query_plan_xml.nodes('//p:StmtSimple') AS q(n) +OPTION (RECOMPILE); + +RAISERROR(N'Inserting parsed cursor XML to #statements', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #statements WITH (TABLOCK) ( plan_id, query_id, query_hash, sql_handle, statement, is_cursor ) + SELECT ww.plan_id, ww.query_id, ww.query_hash, ww.sql_handle, q.n.query('.') AS statement, 1 AS is_cursor + FROM #working_warnings AS ww + JOIN #working_plan_text AS wp + ON ww.plan_id = wp.plan_id + AND ww.query_id = wp.query_id + CROSS APPLY wp.query_plan_xml.nodes('//p:StmtCursor') AS q(n) +OPTION (RECOMPILE); + +RAISERROR(N'Inserting to #query_plan', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #query_plan WITH (TABLOCK) ( plan_id, query_id, query_hash, sql_handle, query_plan ) +SELECT s.plan_id, s.query_id, s.query_hash, s.sql_handle, q.n.query('.') AS query_plan +FROM #statements AS s + CROSS APPLY s.statement.nodes('//p:QueryPlan') AS q(n) +OPTION (RECOMPILE); + +RAISERROR(N'Inserting to #relop', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #relop WITH (TABLOCK) ( plan_id, query_id, query_hash, sql_handle, relop) +SELECT qp.plan_id, qp.query_id, qp.query_hash, qp.sql_handle, q.n.query('.') AS relop +FROM #query_plan qp + CROSS APPLY qp.query_plan.nodes('//p:RelOp') AS q(n) +OPTION (RECOMPILE); + + +-- statement level checks + +RAISERROR(N'Performing compile timeout checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.compile_timeout = 1 +FROM #statements s +JOIN #working_warnings AS b +ON s.query_hash = b.query_hash +WHERE s.statement.exist('/p:StmtSimple/@StatementOptmEarlyAbortReason[.="TimeOut"]') = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Performing compile memory limit exceeded checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.compile_memory_limit_exceeded = 1 +FROM #statements s +JOIN #working_warnings AS b +ON s.query_hash = b.query_hash +WHERE s.statement.exist('/p:StmtSimple/@StatementOptmEarlyAbortReason[.="MemoryLimitExceeded"]') = 1 +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Performing index DML checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +index_dml AS ( + SELECT s.query_hash, + index_dml = CASE WHEN s.statement.exist('//p:StmtSimple/@StatementType[.="CREATE INDEX"]') = 1 THEN 1 + WHEN s.statement.exist('//p:StmtSimple/@StatementType[.="DROP INDEX"]') = 1 THEN 1 + END + FROM #statements s + ) + UPDATE b + SET b.index_dml = i.index_dml + FROM #working_warnings AS b + JOIN index_dml i + ON i.query_hash = b.query_hash + WHERE i.index_dml = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Performing table DML checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +table_dml AS ( + SELECT s.query_hash, + table_dml = CASE WHEN s.statement.exist('//p:StmtSimple/@StatementType[.="CREATE TABLE"]') = 1 THEN 1 + WHEN s.statement.exist('//p:StmtSimple/@StatementType[.="DROP OBJECT"]') = 1 THEN 1 + END + FROM #statements AS s + ) + UPDATE b + SET b.table_dml = t.table_dml + FROM #working_warnings AS b + JOIN table_dml t + ON t.query_hash = b.query_hash + WHERE t.table_dml = 1 +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Gathering trivial plans', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +UPDATE b +SET b.is_trivial = 1 +FROM #working_warnings AS b +JOIN ( +SELECT s.sql_handle +FROM #statements AS s +JOIN ( SELECT r.sql_handle + FROM #relop AS r + WHERE r.relop.exist('//p:RelOp[contains(@LogicalOp, "Scan")]') = 1 ) AS r + ON r.sql_handle = s.sql_handle +WHERE s.statement.exist('//p:StmtSimple[@StatementOptmLevel[.="TRIVIAL"]]/p:QueryPlan/p:ParameterList') = 1 +) AS s +ON b.sql_handle = s.sql_handle +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Gathering row estimates', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +INSERT #est_rows (query_hash, estimated_rows) +SELECT DISTINCT + CONVERT(BINARY(8), RIGHT('0000000000000000' + SUBSTRING(c.n.value('@QueryHash', 'VARCHAR(18)'), 3, 18), 16), 2) AS query_hash, + c.n.value('(/p:StmtSimple/@StatementEstRows)[1]', 'FLOAT') AS estimated_rows +FROM #statements AS s +CROSS APPLY s.statement.nodes('/p:StmtSimple') AS c(n) +WHERE c.n.exist('/p:StmtSimple[@StatementEstRows > 0]') = 1; + + UPDATE b + SET b.estimated_rows = er.estimated_rows + FROM #working_warnings AS b + JOIN #est_rows er + ON er.query_hash = b.query_hash + OPTION (RECOMPILE); +END; + + +/*Begin plan cost calculations*/ +RAISERROR(N'Gathering statement costs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #plan_cost WITH (TABLOCK) + ( query_plan_cost, sql_handle, plan_id ) +SELECT DISTINCT + s.statement.value('sum(/p:StmtSimple/@StatementSubTreeCost)', 'float') query_plan_cost, + s.sql_handle, + s.plan_id +FROM #statements s +OUTER APPLY s.statement.nodes('/p:StmtSimple') AS q(n) +WHERE s.statement.value('sum(/p:StmtSimple/@StatementSubTreeCost)', 'float') > 0 +OPTION (RECOMPILE); + + +RAISERROR(N'Updating statement costs', 0, 1) WITH NOWAIT; +WITH pc AS ( + SELECT SUM(DISTINCT pc.query_plan_cost) AS queryplancostsum, pc.sql_handle, pc.plan_id + FROM #plan_cost AS pc + GROUP BY pc.sql_handle, pc.plan_id + ) + UPDATE b + SET b.query_cost = ISNULL(pc.queryplancostsum, 0) + FROM #working_warnings AS b + JOIN pc + ON pc.sql_handle = b.sql_handle + AND pc.plan_id = b.plan_id +OPTION (RECOMPILE); + + +/*End plan cost calculations*/ + + +RAISERROR(N'Checking for plan warnings', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.plan_warnings = 1 +FROM #query_plan qp +JOIN #working_warnings b +ON qp.sql_handle = b.sql_handle +AND qp.query_plan.exist('/p:QueryPlan/p:Warnings') = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for implicit conversion', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.implicit_conversions = 1 +FROM #query_plan qp +JOIN #working_warnings b +ON qp.sql_handle = b.sql_handle +AND qp.query_plan.exist('/p:QueryPlan/p:Warnings/p:PlanAffectingConvert/@Expression[contains(., "CONVERT_IMPLICIT")]') = 1 +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Performing busy loops checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE p +SET busy_loops = CASE WHEN (x.estimated_executions / 100.0) > x.estimated_rows THEN 1 END +FROM #working_warnings p + JOIN ( + SELECT qs.sql_handle, + relop.value('sum(/p:RelOp/@EstimateRows)', 'float') AS estimated_rows , + relop.value('sum(/p:RelOp/@EstimateRewinds)', 'float') + relop.value('sum(/p:RelOp/@EstimateRebinds)', 'float') + 1.0 AS estimated_executions + FROM #relop qs + ) AS x ON p.sql_handle = x.sql_handle +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Performing TVF join check', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE p +SET p.tvf_join = CASE WHEN x.tvf_join = 1 THEN 1 END +FROM #working_warnings p + JOIN ( + SELECT r.sql_handle, + 1 AS tvf_join + FROM #relop AS r + WHERE r.relop.exist('//p:RelOp[(@LogicalOp[.="Table-valued function"])]') = 1 + AND r.relop.exist('//p:RelOp[contains(@LogicalOp, "Join")]') = 1 + ) AS x ON p.sql_handle = x.sql_handle +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for operator warnings', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, x AS ( +SELECT r.sql_handle, + c.n.exist('//p:Warnings[(@NoJoinPredicate[.="1"])]') AS warning_no_join_predicate, + c.n.exist('//p:ColumnsWithNoStatistics') AS no_stats_warning , + c.n.exist('//p:Warnings') AS relop_warnings +FROM #relop AS r +CROSS APPLY r.relop.nodes('/p:RelOp/p:Warnings') AS c(n) +) +UPDATE b +SET b.warning_no_join_predicate = x.warning_no_join_predicate, + b.no_stats_warning = x.no_stats_warning, + b.relop_warnings = x.relop_warnings +FROM #working_warnings b +JOIN x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Checking for table variables', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, x AS ( +SELECT r.sql_handle, + c.n.value('substring(@Table, 2, 1)','VARCHAR(100)') AS first_char +FROM #relop r +CROSS APPLY r.relop.nodes('//p:Object') AS c(n) +) +UPDATE b +SET b.is_table_variable = 1 +FROM #working_warnings b +JOIN x ON x.sql_handle = b.sql_handle +JOIN #working_metrics AS wm +ON b.plan_id = wm.plan_id +AND b.query_id = wm.query_id +AND wm.batch_sql_handle IS NOT NULL +WHERE x.first_char = '@' +OPTION (RECOMPILE); + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for functions', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, x AS ( +SELECT r.sql_handle, + n.fn.value('count(distinct-values(//p:UserDefinedFunction[not(@IsClrFunction)]))', 'INT') AS function_count, + n.fn.value('count(distinct-values(//p:UserDefinedFunction[@IsClrFunction = "1"]))', 'INT') AS clr_function_count +FROM #relop r +CROSS APPLY r.relop.nodes('/p:RelOp/p:ComputeScalar/p:DefinedValues/p:DefinedValue/p:ScalarOperator') n(fn) +) +UPDATE b +SET b.function_count = x.function_count, + b.clr_function_count = x.clr_function_count +FROM #working_warnings b +JOIN x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Checking for expensive key lookups', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.key_lookup_cost = x.key_lookup_cost +FROM #working_warnings b +JOIN ( +SELECT + r.sql_handle, + MAX(r.relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS key_lookup_cost +FROM #relop r +WHERE r.relop.exist('/p:RelOp/p:IndexScan[(@Lookup[.="1"])]') = 1 +GROUP BY r.sql_handle +) AS x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for expensive remote queries', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.remote_query_cost = x.remote_query_cost +FROM #working_warnings b +JOIN ( +SELECT + r.sql_handle, + MAX(r.relop.value('sum(/p:RelOp/@EstimatedTotalSubtreeCost)', 'float')) AS remote_query_cost +FROM #relop r +WHERE r.relop.exist('/p:RelOp[(@PhysicalOp[contains(., "Remote")])]') = 1 +GROUP BY r.sql_handle +) AS x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for expensive sorts', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET sort_cost = y.max_sort_cost +FROM #working_warnings b +JOIN ( + SELECT x.sql_handle, MAX((x.sort_io + x.sort_cpu)) AS max_sort_cost + FROM ( + SELECT + qs.sql_handle, + relop.value('sum(/p:RelOp/@EstimateIO)', 'float') AS sort_io, + relop.value('sum(/p:RelOp/@EstimateCPU)', 'float') AS sort_cpu + FROM #relop qs + WHERE [relop].exist('/p:RelOp[(@PhysicalOp[.="Sort"])]') = 1 + ) AS x + GROUP BY x.sql_handle + ) AS y +ON b.sql_handle = y.sql_handle +OPTION (RECOMPILE); + +IF NOT EXISTS(SELECT 1/0 FROM #statements AS s WHERE s.is_cursor = 1) +BEGIN + +RAISERROR(N'No cursor plans found, skipping', 0, 1) WITH NOWAIT; + +END + +IF EXISTS(SELECT 1/0 FROM #statements AS s WHERE s.is_cursor = 1) +BEGIN + +RAISERROR(N'Cursor plans found, investigating', 0, 1) WITH NOWAIT; + +RAISERROR(N'Checking for Optimistic cursors', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_optimistic_cursor = 1 +FROM #working_warnings b +JOIN #statements AS s +ON b.sql_handle = s.sql_handle +CROSS APPLY s.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE n1.fn.exist('//p:CursorPlan/@CursorConcurrency[.="Optimistic"]') = 1 +AND s.is_cursor = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Checking if cursor is Forward Only', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_forward_only_cursor = 1 +FROM #working_warnings b +JOIN #statements AS s +ON b.sql_handle = s.sql_handle +CROSS APPLY s.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE n1.fn.exist('//p:CursorPlan/@ForwardOnly[.="true"]') = 1 +AND s.is_cursor = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Checking if cursor is Fast Forward', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_fast_forward_cursor = 1 +FROM #working_warnings b +JOIN #statements AS s +ON b.sql_handle = s.sql_handle +CROSS APPLY s.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE n1.fn.exist('//p:CursorPlan/@CursorActualType[.="FastForward"]') = 1 +AND s.is_cursor = 1 +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for Dynamic cursors', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_cursor_dynamic = 1 +FROM #working_warnings b +JOIN #statements AS s +ON b.sql_handle = s.sql_handle +CROSS APPLY s.statement.nodes('/p:StmtCursor') AS n1(fn) +WHERE n1.fn.exist('//p:CursorPlan/@CursorActualType[.="Dynamic"]') = 1 +AND s.is_cursor = 1 +OPTION (RECOMPILE); + +END + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for bad scans and plan forcing', 0, 1) WITH NOWAIT; +;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET +b.is_table_scan = x.is_table_scan, +b.backwards_scan = x.backwards_scan, +b.forced_index = x.forced_index, +b.forced_seek = x.forced_seek, +b.forced_scan = x.forced_scan +FROM #working_warnings b +JOIN ( +SELECT + r.sql_handle, + 0 AS is_table_scan, + q.n.exist('@ScanDirection[.="BACKWARD"]') AS backwards_scan, + q.n.value('@ForcedIndex', 'bit') AS forced_index, + q.n.value('@ForceSeek', 'bit') AS forced_seek, + q.n.value('@ForceScan', 'bit') AS forced_scan +FROM #relop r +CROSS APPLY r.relop.nodes('//p:IndexScan') AS q(n) +UNION ALL +SELECT + r.sql_handle, + 1 AS is_table_scan, + q.n.exist('@ScanDirection[.="BACKWARD"]') AS backwards_scan, + q.n.value('@ForcedIndex', 'bit') AS forced_index, + q.n.value('@ForceSeek', 'bit') AS forced_seek, + q.n.value('@ForceScan', 'bit') AS forced_scan +FROM #relop r +CROSS APPLY r.relop.nodes('//p:TableScan') AS q(n) +) AS x ON b.sql_handle = x.sql_handle +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for computed columns that reference scalar UDFs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_computed_scalar = x.computed_column_function +FROM #working_warnings b +JOIN ( +SELECT r.sql_handle, + n.fn.value('count(distinct-values(//p:UserDefinedFunction[not(@IsClrFunction)]))', 'INT') AS computed_column_function +FROM #relop r +CROSS APPLY r.relop.nodes('/p:RelOp/p:ComputeScalar/p:DefinedValues/p:DefinedValue/p:ScalarOperator') n(fn) +WHERE n.fn.exist('/p:RelOp/p:ComputeScalar/p:DefinedValues/p:DefinedValue/p:ColumnReference[(@ComputedColumn[.="1"])]') = 1 +) AS x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); +END; + + +RAISERROR(N'Checking for filters that reference scalar UDFs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_computed_filter = x.filter_function +FROM #working_warnings b +JOIN ( +SELECT +r.sql_handle, +c.n.value('count(distinct-values(//p:UserDefinedFunction[not(@IsClrFunction)]))', 'INT') AS filter_function +FROM #relop AS r +CROSS APPLY r.relop.nodes('/p:RelOp/p:Filter/p:Predicate/p:ScalarOperator/p:Compare/p:ScalarOperator/p:UserDefinedFunction') c(n) +) x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking modification queries that hit lots of indexes', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +IndexOps AS +( + SELECT + r.query_hash, + c.n.value('@PhysicalOp', 'VARCHAR(100)') AS op_name, + c.n.exist('@PhysicalOp[.="Index Insert"]') AS ii, + c.n.exist('@PhysicalOp[.="Index Update"]') AS iu, + c.n.exist('@PhysicalOp[.="Index Delete"]') AS id, + c.n.exist('@PhysicalOp[.="Clustered Index Insert"]') AS cii, + c.n.exist('@PhysicalOp[.="Clustered Index Update"]') AS ciu, + c.n.exist('@PhysicalOp[.="Clustered Index Delete"]') AS cid, + c.n.exist('@PhysicalOp[.="Table Insert"]') AS ti, + c.n.exist('@PhysicalOp[.="Table Update"]') AS tu, + c.n.exist('@PhysicalOp[.="Table Delete"]') AS td + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp') c(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:ScalarInsert/p:Object') q(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:Update/p:Object') o2(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:SimpleUpdate/p:Object') o3(n) +), iops AS +( + SELECT ios.query_hash, + SUM(CONVERT(TINYINT, ios.ii)) AS index_insert_count, + SUM(CONVERT(TINYINT, ios.iu)) AS index_update_count, + SUM(CONVERT(TINYINT, ios.id)) AS index_delete_count, + SUM(CONVERT(TINYINT, ios.cii)) AS cx_insert_count, + SUM(CONVERT(TINYINT, ios.ciu)) AS cx_update_count, + SUM(CONVERT(TINYINT, ios.cid)) AS cx_delete_count, + SUM(CONVERT(TINYINT, ios.ti)) AS table_insert_count, + SUM(CONVERT(TINYINT, ios.tu)) AS table_update_count, + SUM(CONVERT(TINYINT, ios.td)) AS table_delete_count + FROM IndexOps AS ios + WHERE ios.op_name IN ('Index Insert', 'Index Delete', 'Index Update', + 'Clustered Index Insert', 'Clustered Index Delete', 'Clustered Index Update', + 'Table Insert', 'Table Delete', 'Table Update') + GROUP BY ios.query_hash) +UPDATE b +SET b.index_insert_count = iops.index_insert_count, + b.index_update_count = iops.index_update_count, + b.index_delete_count = iops.index_delete_count, + b.cx_insert_count = iops.cx_insert_count, + b.cx_update_count = iops.cx_update_count, + b.cx_delete_count = iops.cx_delete_count, + b.table_insert_count = iops.table_insert_count, + b.table_update_count = iops.table_update_count, + b.table_delete_count = iops.table_delete_count +FROM #working_warnings AS b +JOIN iops ON iops.query_hash = b.query_hash +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for Spatial index use', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_spatial = x.is_spatial +FROM #working_warnings AS b +JOIN ( +SELECT r.sql_handle, + 1 AS is_spatial +FROM #relop r +CROSS APPLY r.relop.nodes('/p:RelOp//p:Object') n(fn) +WHERE n.fn.exist('(@IndexKind[.="Spatial"])') = 1 +) AS x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); +END; + +RAISERROR(N'Checking for forced serialization', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_forced_serial = 1 +FROM #query_plan qp +JOIN #working_warnings AS b +ON qp.sql_handle = b.sql_handle +AND b.is_parallel IS NULL +AND qp.query_plan.exist('/p:QueryPlan/@NonParallelPlanReason') = 1 +OPTION (RECOMPILE); + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for ColumnStore queries operating in Row Mode instead of Batch Mode', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.columnstore_row_mode = x.is_row_mode +FROM #working_warnings AS b +JOIN ( +SELECT + r.sql_handle, + r.relop.exist('/p:RelOp[(@EstimatedExecutionMode[.="Row"])]') AS is_row_mode +FROM #relop r +WHERE r.relop.exist('/p:RelOp/p:IndexScan[(@Storage[.="ColumnStore"])]') = 1 +) AS x ON x.sql_handle = b.sql_handle +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR('Checking for row level security only', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_row_level = 1 +FROM #working_warnings b +JOIN #statements s +ON s.query_hash = b.query_hash +WHERE s.statement.exist('/p:StmtSimple/@SecurityPolicyApplied[.="true"]') = 1 +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR('Checking for wonky Index Spools', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( + 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +, selects +AS ( SELECT s.plan_id, s.query_id + FROM #statements AS s + WHERE s.statement.exist('/p:StmtSimple/@StatementType[.="SELECT"]') = 1 ) +, spools +AS ( SELECT DISTINCT r.plan_id, + r.query_id, + c.n.value('@EstimateRows', 'FLOAT') AS estimated_rows, + c.n.value('@EstimateIO', 'FLOAT') AS estimated_io, + c.n.value('@EstimateCPU', 'FLOAT') AS estimated_cpu, + c.n.value('@EstimateRebinds', 'FLOAT') AS estimated_rebinds +FROM #relop AS r +JOIN selects AS s +ON s.plan_id = r.plan_id + AND s.query_id = r.query_id +CROSS APPLY r.relop.nodes('/p:RelOp') AS c(n) +WHERE r.relop.exist('/p:RelOp[@PhysicalOp="Index Spool" and @LogicalOp="Eager Spool"]') = 1 +) +UPDATE ww + SET ww.index_spool_rows = sp.estimated_rows, + ww.index_spool_cost = ((sp.estimated_io * sp.estimated_cpu) * CASE WHEN sp.estimated_rebinds < 1 THEN 1 ELSE sp.estimated_rebinds END) + +FROM #working_warnings ww +JOIN spools sp +ON ww.plan_id = sp.plan_id +AND ww.query_id = sp.query_id +OPTION (RECOMPILE); +END; + + +IF (PARSENAME(CONVERT(VARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 4)) >= 14 +OR ((PARSENAME(CONVERT(VARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 4)) = 13 + AND PARSENAME(CONVERT(VARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 2) >= 5026) + +BEGIN + +RAISERROR(N'Beginning 2017 and 2016 SP2 specfic checks', 0, 1) WITH NOWAIT; + +IF @ExpertMode > 0 +BEGIN +RAISERROR('Gathering stats information', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +INSERT #stats_agg WITH (TABLOCK) + (sql_handle, last_update, modification_count, sampling_percent, [statistics], [table], [schema], [database]) +SELECT qp.sql_handle, + x.c.value('@LastUpdate', 'DATETIME2(7)') AS LastUpdate, + x.c.value('@ModificationCount', 'BIGINT') AS ModificationCount, + x.c.value('@SamplingPercent', 'FLOAT') AS SamplingPercent, + x.c.value('@Statistics', 'NVARCHAR(258)') AS [Statistics], + x.c.value('@Table', 'NVARCHAR(258)') AS [Table], + x.c.value('@Schema', 'NVARCHAR(258)') AS [Schema], + x.c.value('@Database', 'NVARCHAR(258)') AS [Database] +FROM #query_plan AS qp +CROSS APPLY qp.query_plan.nodes('//p:OptimizerStatsUsage/p:StatisticsInfo') x (c) +OPTION (RECOMPILE); + + +RAISERROR('Checking for stale stats', 0, 1) WITH NOWAIT; +WITH stale_stats AS ( + SELECT sa.sql_handle + FROM #stats_agg AS sa + GROUP BY sa.sql_handle + HAVING MAX(sa.last_update) <= DATEADD(DAY, -7, SYSDATETIME()) + AND AVG(sa.modification_count) >= 100000 +) +UPDATE b +SET b.stale_stats = 1 +FROM #working_warnings AS b +JOIN stale_stats os +ON b.sql_handle = os.sql_handle +OPTION (RECOMPILE); +END; + + +IF (PARSENAME(CONVERT(VARCHAR(128), SERVERPROPERTY ('PRODUCTVERSION')), 4)) >= 14 + AND @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for Adaptive Joins', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +aj AS ( + SELECT r.sql_handle + FROM #relop AS r + CROSS APPLY r.relop.nodes('//p:RelOp') x(c) + WHERE x.c.exist('@IsAdaptive[.=1]') = 1 +) +UPDATE b +SET b.is_adaptive = 1 +FROM #working_warnings AS b +JOIN aj +ON b.sql_handle = aj.sql_handle +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN; +RAISERROR(N'Checking for Row Goals', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +row_goals AS( +SELECT qs.query_hash +FROM #relop qs +WHERE relop.value('sum(/p:RelOp/@EstimateRowsWithoutRowGoal)', 'float') > 0 +) +UPDATE b +SET b.is_row_goal = 1 +FROM #working_warnings b +JOIN row_goals +ON b.query_hash = row_goals.query_hash +OPTION (RECOMPILE); +END; + +END; + + +RAISERROR(N'Performing query level checks', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.missing_index_count = query_plan.value('count(//p:QueryPlan/p:MissingIndexes/p:MissingIndexGroup)', 'int') , + b.unmatched_index_count = CASE WHEN is_trivial <> 1 THEN query_plan.value('count(//p:QueryPlan/p:UnmatchedIndexes/p:Parameterization/p:Object)', 'int') END +FROM #query_plan qp +JOIN #working_warnings AS b +ON b.query_hash = qp.query_hash +OPTION (RECOMPILE); + + +RAISERROR(N'Trace flag checks', 0, 1) WITH NOWAIT; +;WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +, tf_pretty AS ( +SELECT qp.sql_handle, + q.n.value('@Value', 'INT') AS trace_flag, + q.n.value('@Scope', 'VARCHAR(10)') AS scope +FROM #query_plan qp +CROSS APPLY qp.query_plan.nodes('/p:QueryPlan/p:TraceFlags/p:TraceFlag') AS q(n) +) +INSERT #trace_flags WITH (TABLOCK) + (sql_handle, global_trace_flags, session_trace_flags ) +SELECT DISTINCT tf1.sql_handle , + STUFF(( + SELECT DISTINCT N', ' + CONVERT(NVARCHAR(5), tf2.trace_flag) + FROM tf_pretty AS tf2 + WHERE tf1.sql_handle = tf2.sql_handle + AND tf2.scope = 'Global' + FOR XML PATH(N'')), 1, 2, N'' + ) AS global_trace_flags, + STUFF(( + SELECT DISTINCT N', ' + CONVERT(NVARCHAR(5), tf2.trace_flag) + FROM tf_pretty AS tf2 + WHERE tf1.sql_handle = tf2.sql_handle + AND tf2.scope = 'Session' + FOR XML PATH(N'')), 1, 2, N'' + ) AS session_trace_flags +FROM tf_pretty AS tf1 +OPTION (RECOMPILE); + +UPDATE b +SET b.trace_flags_session = tf.session_trace_flags +FROM #working_warnings AS b +JOIN #trace_flags tf +ON tf.sql_handle = b.sql_handle +OPTION (RECOMPILE); + + +RAISERROR(N'Checking for MSTVFs', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_mstvf = 1 +FROM #relop AS r +JOIN #working_warnings AS b +ON b.sql_handle = r.sql_handle +WHERE r.relop.exist('/p:RelOp[(@EstimateRows="100" or @EstimateRows="1") and @LogicalOp="Table-valued function"]') = 1 +OPTION (RECOMPILE); + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Checking for many to many merge joins', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.is_mm_join = 1 +FROM #relop AS r +JOIN #working_warnings AS b +ON b.sql_handle = r.sql_handle +WHERE r.relop.exist('/p:RelOp/p:Merge/@ManyToMany[.="1"]') = 1 +OPTION (RECOMPILE); +END; + + +IF @ExpertMode > 0 +BEGIN +RAISERROR(N'Is Paul White Electric?', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p), +is_paul_white_electric AS ( +SELECT 1 AS [is_paul_white_electric], +r.sql_handle +FROM #relop AS r +CROSS APPLY r.relop.nodes('//p:RelOp') c(n) +WHERE c.n.exist('@PhysicalOp[.="Switch"]') = 1 +) +UPDATE b +SET b.is_paul_white_electric = ipwe.is_paul_white_electric +FROM #working_warnings AS b +JOIN is_paul_white_electric ipwe +ON ipwe.sql_handle = b.sql_handle +OPTION (RECOMPILE); +END; + + + +RAISERROR(N'Checking for non-sargable predicates', 0, 1) WITH NOWAIT; +WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) +, nsarg + AS ( SELECT r.query_hash, 1 AS fn, 0 AS jo, 0 AS lk + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp/p:IndexScan/p:Predicate/p:ScalarOperator/p:Compare/p:ScalarOperator') AS ca(x) + WHERE ( ca.x.exist('//p:ScalarOperator/p:Intrinsic/@FunctionName') = 1 + OR ca.x.exist('//p:ScalarOperator/p:IF') = 1 ) + UNION ALL + SELECT r.query_hash, 0 AS fn, 1 AS jo, 0 AS lk + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp//p:ScalarOperator') AS ca(x) + WHERE r.relop.exist('/p:RelOp[contains(@LogicalOp, "Join")]') = 1 + AND ca.x.exist('//p:ScalarOperator[contains(@ScalarString, "Expr")]') = 1 + UNION ALL + SELECT r.query_hash, 0 AS fn, 0 AS jo, 1 AS lk + FROM #relop AS r + CROSS APPLY r.relop.nodes('/p:RelOp/p:IndexScan/p:Predicate/p:ScalarOperator') AS ca(x) + CROSS APPLY ca.x.nodes('//p:Const') AS co(x) + WHERE ca.x.exist('//p:ScalarOperator/p:Intrinsic/@FunctionName[.="like"]') = 1 + AND ( ( co.x.value('substring(@ConstValue, 1, 1)', 'VARCHAR(100)') <> 'N' + AND co.x.value('substring(@ConstValue, 2, 1)', 'VARCHAR(100)') = '%' ) + OR ( co.x.value('substring(@ConstValue, 1, 1)', 'VARCHAR(100)') = 'N' + AND co.x.value('substring(@ConstValue, 3, 1)', 'VARCHAR(100)') = '%' ))), + d_nsarg + AS ( SELECT DISTINCT + nsarg.query_hash + FROM nsarg + WHERE nsarg.fn = 1 + OR nsarg.jo = 1 + OR nsarg.lk = 1 ) +UPDATE b +SET b.is_nonsargable = 1 +FROM d_nsarg AS d +JOIN #working_warnings AS b + ON b.query_hash = d.query_hash +OPTION ( RECOMPILE ); + + + RAISERROR(N'Getting information about implicit conversions and stored proc parameters', 0, 1) WITH NOWAIT; + + RAISERROR(N'Getting variable info', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #variable_info ( query_hash, sql_handle, proc_name, variable_name, variable_datatype, compile_time_value ) + SELECT DISTINCT + qp.query_hash, + qp.sql_handle, + b.proc_or_function_name AS proc_name, + q.n.value('@Column', 'NVARCHAR(258)') AS variable_name, + q.n.value('@ParameterDataType', 'NVARCHAR(258)') AS variable_datatype, + q.n.value('@ParameterCompiledValue', 'NVARCHAR(258)') AS compile_time_value + FROM #query_plan AS qp + JOIN #working_warnings AS b + ON (b.query_hash = qp.query_hash AND b.proc_or_function_name = 'adhoc') + OR (b.sql_handle = qp.sql_handle AND b.proc_or_function_name <> 'adhoc') + CROSS APPLY qp.query_plan.nodes('//p:QueryPlan/p:ParameterList/p:ColumnReference') AS q(n) + OPTION (RECOMPILE); + + RAISERROR(N'Getting conversion info', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #conversion_info ( query_hash, sql_handle, proc_name, expression ) + SELECT DISTINCT + qp.query_hash, + qp.sql_handle, + b.proc_or_function_name AS proc_name, + qq.c.value('@Expression', 'NVARCHAR(4000)') AS expression + FROM #query_plan AS qp + JOIN #working_warnings AS b + ON (b.query_hash = qp.query_hash AND b.proc_or_function_name = 'adhoc') + OR (b.sql_handle = qp.sql_handle AND b.proc_or_function_name <> 'adhoc') + CROSS APPLY qp.query_plan.nodes('//p:QueryPlan/p:Warnings/p:PlanAffectingConvert') AS qq(c) + WHERE qq.c.exist('@ConvertIssue[.="Seek Plan"]') = 1 + AND b.implicit_conversions = 1 + OPTION (RECOMPILE); + + RAISERROR(N'Parsing conversion info', 0, 1) WITH NOWAIT; + INSERT #stored_proc_info ( sql_handle, query_hash, proc_name, variable_name, variable_datatype, converted_column_name, column_name, converted_to, compile_time_value ) + SELECT ci.sql_handle, + ci.query_hash, + ci.proc_name, + CASE WHEN ci.at_charindex > 0 + AND ci.bracket_charindex > 0 + THEN SUBSTRING(ci.expression, ci.at_charindex, ci.bracket_charindex) + ELSE N'**no_variable**' + END AS variable_name, + N'**no_variable**' AS variable_datatype, + CASE WHEN ci.at_charindex = 0 + AND ci.comma_charindex > 0 + AND ci.second_comma_charindex > 0 + THEN SUBSTRING(ci.expression, ci.comma_charindex, ci.second_comma_charindex) + ELSE N'**no_column**' + END AS converted_column_name, + CASE WHEN ci.at_charindex = 0 + AND ci.equal_charindex > 0 + AND ci.convert_implicit_charindex = 0 + THEN SUBSTRING(ci.expression, ci.equal_charindex, 4000) + WHEN ci.at_charindex = 0 + AND (ci.equal_charindex -1) > 0 + AND ci.convert_implicit_charindex > 0 + THEN SUBSTRING(ci.expression, 0, ci.equal_charindex -1) + WHEN ci.at_charindex > 0 + AND ci.comma_charindex > 0 + AND ci.second_comma_charindex > 0 + THEN SUBSTRING(ci.expression, ci.comma_charindex, ci.second_comma_charindex) + ELSE N'**no_column **' + END AS column_name, + CASE WHEN ci.paren_charindex > 0 + AND ci.comma_paren_charindex > 0 + THEN SUBSTRING(ci.expression, ci.paren_charindex, ci.comma_paren_charindex) + END AS converted_to, + CASE WHEN ci.at_charindex = 0 + AND ci.convert_implicit_charindex = 0 + AND ci.proc_name = 'Statement' + THEN SUBSTRING(ci.expression, ci.equal_charindex, 4000) + ELSE '**idk_man**' + END AS compile_time_value + FROM #conversion_info AS ci + OPTION (RECOMPILE); + + RAISERROR(N'Updating variables inserted procs', 0, 1) WITH NOWAIT; + UPDATE sp + SET sp.variable_datatype = vi.variable_datatype, + sp.compile_time_value = vi.compile_time_value + FROM #stored_proc_info AS sp + JOIN #variable_info AS vi + ON (sp.proc_name = 'adhoc' AND sp.query_hash = vi.query_hash) + OR (sp.proc_name <> 'adhoc' AND sp.sql_handle = vi.sql_handle) + AND sp.variable_name = vi.variable_name + OPTION (RECOMPILE); + + + RAISERROR(N'Inserting variables for other procs', 0, 1) WITH NOWAIT; + INSERT #stored_proc_info + ( sql_handle, query_hash, variable_name, variable_datatype, compile_time_value, proc_name ) + SELECT vi.sql_handle, vi.query_hash, vi.variable_name, vi.variable_datatype, vi.compile_time_value, vi.proc_name + FROM #variable_info AS vi + WHERE NOT EXISTS + ( + SELECT * + FROM #stored_proc_info AS sp + WHERE (sp.proc_name = 'adhoc' AND sp.query_hash = vi.query_hash) + OR (sp.proc_name <> 'adhoc' AND sp.sql_handle = vi.sql_handle) + ) + OPTION (RECOMPILE); + + RAISERROR(N'Updating procs', 0, 1) WITH NOWAIT; + UPDATE s + SET s.variable_datatype = CASE WHEN s.variable_datatype LIKE '%(%)%' + THEN LEFT(s.variable_datatype, CHARINDEX('(', s.variable_datatype) - 1) + ELSE s.variable_datatype + END, + s.converted_to = CASE WHEN s.converted_to LIKE '%(%)%' + THEN LEFT(s.converted_to, CHARINDEX('(', s.converted_to) - 1) + ELSE s.converted_to + END, + s.compile_time_value = CASE WHEN s.compile_time_value LIKE '%(%)%' + THEN SUBSTRING(s.compile_time_value, + CHARINDEX('(', s.compile_time_value) + 1, + CHARINDEX(')', s.compile_time_value) - 1 - CHARINDEX('(', s.compile_time_value) + ) + WHEN variable_datatype NOT IN ('bit', 'tinyint', 'smallint', 'int', 'bigint') + AND s.variable_datatype NOT LIKE '%binary%' + AND s.compile_time_value NOT LIKE 'N''%''' + AND s.compile_time_value NOT LIKE '''%''' + AND s.compile_time_value <> s.column_name + AND s.compile_time_value <> '**idk_man**' + THEN QUOTENAME(compile_time_value, '''') + ELSE s.compile_time_value + END + FROM #stored_proc_info AS s + OPTION (RECOMPILE); + + + RAISERROR(N'Updating SET options', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + UPDATE s + SET set_options = set_options.ansi_set_options + FROM #stored_proc_info AS s + JOIN ( + SELECT x.sql_handle, + N'SET ANSI_NULLS ' + CASE WHEN [ANSI_NULLS] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET ANSI_PADDING ' + CASE WHEN [ANSI_PADDING] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET ANSI_WARNINGS ' + CASE WHEN [ANSI_WARNINGS] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET ARITHABORT ' + CASE WHEN [ARITHABORT] = 'true' THEN N'ON ' ELSE N' OFF ' END + NCHAR(10) + + N'SET CONCAT_NULL_YIELDS_NULL ' + CASE WHEN [CONCAT_NULL_YIELDS_NULL] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET NUMERIC_ROUNDABORT ' + CASE WHEN [NUMERIC_ROUNDABORT] = 'true' THEN N'ON ' ELSE N'OFF ' END + NCHAR(10) + + N'SET QUOTED_IDENTIFIER ' + CASE WHEN [QUOTED_IDENTIFIER] = 'true' THEN N'ON ' ELSE N'OFF ' + NCHAR(10) END AS [ansi_set_options] + FROM ( + SELECT + s.sql_handle, + so.o.value('@ANSI_NULLS', 'NVARCHAR(20)') AS [ANSI_NULLS], + so.o.value('@ANSI_PADDING', 'NVARCHAR(20)') AS [ANSI_PADDING], + so.o.value('@ANSI_WARNINGS', 'NVARCHAR(20)') AS [ANSI_WARNINGS], + so.o.value('@ARITHABORT', 'NVARCHAR(20)') AS [ARITHABORT], + so.o.value('@CONCAT_NULL_YIELDS_NULL', 'NVARCHAR(20)') AS [CONCAT_NULL_YIELDS_NULL], + so.o.value('@NUMERIC_ROUNDABORT', 'NVARCHAR(20)') AS [NUMERIC_ROUNDABORT], + so.o.value('@QUOTED_IDENTIFIER', 'NVARCHAR(20)') AS [QUOTED_IDENTIFIER] + FROM #statements AS s + CROSS APPLY s.statement.nodes('//p:StatementSetOptions') AS so(o) + ) AS x + ) AS set_options ON set_options.sql_handle = s.sql_handle + OPTION(RECOMPILE); + + + RAISERROR(N'Updating conversion XML', 0, 1) WITH NOWAIT; + WITH precheck AS ( + SELECT spi.sql_handle, + spi.proc_name, + (SELECT CASE WHEN spi.proc_name <> 'Statement' + THEN N'The stored procedure ' + spi.proc_name + ELSE N'This ad hoc statement' + END + + N' had the following implicit conversions: ' + + CHAR(10) + + STUFF(( + SELECT DISTINCT + @cr + @lf + + CASE WHEN spi2.variable_name <> N'**no_variable**' + THEN N'The variable ' + WHEN spi2.variable_name = N'**no_variable**' AND (spi2.column_name = spi2.converted_column_name OR spi2.column_name LIKE '%CONVERT_IMPLICIT%') + THEN N'The compiled value ' + WHEN spi2.column_name LIKE '%Expr%' + THEN 'The expression ' + ELSE N'The column ' + END + + CASE WHEN spi2.variable_name <> N'**no_variable**' + THEN spi2.variable_name + WHEN spi2.variable_name = N'**no_variable**' AND (spi2.column_name = spi2.converted_column_name OR spi2.column_name LIKE '%CONVERT_IMPLICIT%') + THEN spi2.compile_time_value + + ELSE spi2.column_name + END + + N' has a data type of ' + + CASE WHEN spi2.variable_datatype = N'**no_variable**' THEN spi2.converted_to + ELSE spi2.variable_datatype + END + + N' which caused implicit conversion on the column ' + + CASE WHEN spi2.column_name LIKE N'%CONVERT_IMPLICIT%' + THEN spi2.converted_column_name + WHEN spi2.column_name = N'**no_column**' + THEN spi2.converted_column_name + WHEN spi2.converted_column_name = N'**no_column**' + THEN spi2.column_name + WHEN spi2.column_name <> spi2.converted_column_name + THEN spi2.converted_column_name + ELSE spi2.column_name + END + + CASE WHEN spi2.variable_name = N'**no_variable**' AND (spi2.column_name = spi2.converted_column_name OR spi2.column_name LIKE '%CONVERT_IMPLICIT%') + THEN N'' + WHEN spi2.column_name LIKE '%Expr%' + THEN N'' + WHEN spi2.compile_time_value NOT IN ('**declared in proc**', '**idk_man**') + AND spi2.compile_time_value <> spi2.column_name + THEN ' with the value ' + RTRIM(spi2.compile_time_value) + ELSE N'' + END + + '.' + FROM #stored_proc_info AS spi2 + WHERE spi.sql_handle = spi2.sql_handle + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + AS [processing-instruction(ClickMe)] FOR XML PATH(''), TYPE ) + AS implicit_conversion_info + FROM #stored_proc_info AS spi + GROUP BY spi.sql_handle, spi.proc_name + ) + UPDATE b + SET b.implicit_conversion_info = pk.implicit_conversion_info + FROM #working_warnings AS b + JOIN precheck AS pk + ON pk.sql_handle = b.sql_handle + OPTION (RECOMPILE); + + RAISERROR(N'Updating cached parameter XML for procs', 0, 1) WITH NOWAIT; + WITH precheck AS ( + SELECT spi.sql_handle, + spi.proc_name, + (SELECT set_options + + @cr + @lf + + @cr + @lf + + N'EXEC ' + + spi.proc_name + + N' ' + + STUFF(( + SELECT DISTINCT N', ' + + CASE WHEN spi2.variable_name <> N'**no_variable**' AND spi2.compile_time_value <> N'**idk_man**' + THEN spi2.variable_name + N' = ' + ELSE @cr + @lf + N' We could not find any cached parameter values for this stored proc. ' + END + + CASE WHEN spi2.variable_name = N'**no_variable**' OR spi2.compile_time_value = N'**idk_man**' + THEN @cr + @lf + N' Possible reasons include declared variables inside the procedure, recompile hints, etc. ' + WHEN spi2.compile_time_value = N'NULL' + THEN spi2.compile_time_value + ELSE RTRIM(spi2.compile_time_value) + END + FROM #stored_proc_info AS spi2 + WHERE spi.sql_handle = spi2.sql_handle + AND spi2.proc_name <> N'Statement' + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + AS [processing-instruction(ClickMe)] FOR XML PATH(''), TYPE ) + AS cached_execution_parameters + FROM #stored_proc_info AS spi + GROUP BY spi.sql_handle, spi.proc_name, set_options + ) + UPDATE b + SET b.cached_execution_parameters = pk.cached_execution_parameters + FROM #working_warnings AS b + JOIN precheck AS pk + ON pk.sql_handle = b.sql_handle + WHERE b.proc_or_function_name <> N'Statement' + OPTION (RECOMPILE); + + + RAISERROR(N'Updating cached parameter XML for statements', 0, 1) WITH NOWAIT; + WITH precheck AS ( + SELECT spi.sql_handle, + spi.proc_name, + (SELECT + set_options + + @cr + @lf + + @cr + @lf + + N' See QueryText column for full query text' + + @cr + @lf + + @cr + @lf + + STUFF(( + SELECT DISTINCT N', ' + + CASE WHEN spi2.variable_name <> N'**no_variable**' AND spi2.compile_time_value <> N'**idk_man**' + THEN spi2.variable_name + N' = ' + ELSE + @cr + @lf + N' We could not find any cached parameter values for this stored proc. ' + END + + CASE WHEN spi2.variable_name = N'**no_variable**' OR spi2.compile_time_value = N'**idk_man**' + THEN + @cr + @lf + N' Possible reasons include declared variables inside the procedure, recompile hints, etc. ' + WHEN spi2.compile_time_value = N'NULL' + THEN spi2.compile_time_value + ELSE RTRIM(spi2.compile_time_value) + END + FROM #stored_proc_info AS spi2 + WHERE spi.sql_handle = spi2.sql_handle + AND spi2.proc_name = N'Statement' + AND spi2.variable_name NOT LIKE N'%msparam%' + FOR XML PATH(N''), TYPE).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 1, N'') + AS [processing-instruction(ClickMe)] FOR XML PATH(''), TYPE ) + AS cached_execution_parameters + FROM #stored_proc_info AS spi + GROUP BY spi.sql_handle, spi.proc_name, spi.set_options + ) + UPDATE b + SET b.cached_execution_parameters = pk.cached_execution_parameters + FROM #working_warnings AS b + JOIN precheck AS pk + ON pk.sql_handle = b.sql_handle + WHERE b.proc_or_function_name = N'Statement' + OPTION (RECOMPILE); + + +RAISERROR(N'Filling in implicit conversion info', 0, 1) WITH NOWAIT; +UPDATE b +SET b.implicit_conversion_info = CASE WHEN b.implicit_conversion_info IS NULL + OR CONVERT(NVARCHAR(MAX), b.implicit_conversion_info) = N'' + THEN N'' + ELSE b.implicit_conversion_info + END, + b.cached_execution_parameters = CASE WHEN b.cached_execution_parameters IS NULL + OR CONVERT(NVARCHAR(MAX), b.cached_execution_parameters) = N'' + THEN N'' + ELSE b.cached_execution_parameters + END +FROM #working_warnings AS b +OPTION (RECOMPILE); + +/*End implicit conversion and parameter info*/ + +/*Begin Missing Index*/ +IF EXISTS ( SELECT 1/0 + FROM #working_warnings AS ww + WHERE ww.missing_index_count > 0 + OR ww.index_spool_cost > 0 + OR ww.index_spool_rows > 0 ) + + BEGIN + + RAISERROR(N'Inserting to #missing_index_xml', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_xml + SELECT qp.query_hash, + qp.sql_handle, + c.mg.value('@Impact', 'FLOAT') AS Impact, + c.mg.query('.') AS cmg + FROM #query_plan AS qp + CROSS APPLY qp.query_plan.nodes('//p:MissingIndexes/p:MissingIndexGroup') AS c(mg) + WHERE qp.query_hash IS NOT NULL + AND c.mg.value('@Impact', 'FLOAT') > 70.0 + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_schema', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_schema + SELECT mix.query_hash, mix.sql_handle, mix.impact, + c.mi.value('@Database', 'NVARCHAR(128)'), + c.mi.value('@Schema', 'NVARCHAR(128)'), + c.mi.value('@Table', 'NVARCHAR(128)'), + c.mi.query('.') + FROM #missing_index_xml AS mix + CROSS APPLY mix.index_xml.nodes('//p:MissingIndex') AS c(mi) + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_usage', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_usage + SELECT ms.query_hash, ms.sql_handle, ms.impact, ms.database_name, ms.schema_name, ms.table_name, + c.cg.value('@Usage', 'NVARCHAR(128)'), + c.cg.query('.') + FROM #missing_index_schema ms + CROSS APPLY ms.index_xml.nodes('//p:ColumnGroup') AS c(cg) + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_detail', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES ( 'http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p ) + INSERT #missing_index_detail + SELECT miu.query_hash, + miu.sql_handle, + miu.impact, + miu.database_name, + miu.schema_name, + miu.table_name, + miu.usage, + c.c.value('@Name', 'NVARCHAR(128)') + FROM #missing_index_usage AS miu + CROSS APPLY miu.index_xml.nodes('//p:Column') AS c(c) + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to #missing_index_pretty', 0, 1) WITH NOWAIT; + INSERT #missing_index_pretty + SELECT DISTINCT m.query_hash, m.sql_handle, m.impact, m.database_name, m.schema_name, m.table_name + , STUFF(( SELECT DISTINCT N', ' + ISNULL(m2.column_name, '') AS column_name + FROM #missing_index_detail AS m2 + WHERE m2.usage = 'EQUALITY' + AND m.query_hash = m2.query_hash + AND m.sql_handle = m2.sql_handle + AND m.impact = m2.impact + AND m.database_name = m2.database_name + AND m.schema_name = m2.schema_name + AND m.table_name = m2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS equality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(m2.column_name, '') AS column_name + FROM #missing_index_detail AS m2 + WHERE m2.usage = 'INEQUALITY' + AND m.query_hash = m2.query_hash + AND m.sql_handle = m2.sql_handle + AND m.impact = m2.impact + AND m.database_name = m2.database_name + AND m.schema_name = m2.schema_name + AND m.table_name = m2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS inequality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(m2.column_name, '') AS column_name + FROM #missing_index_detail AS m2 + WHERE m2.usage = 'INCLUDE' + AND m.query_hash = m2.query_hash + AND m.sql_handle = m2.sql_handle + AND m.impact = m2.impact + AND m.database_name = m2.database_name + AND m.schema_name = m2.schema_name + AND m.table_name = m2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS [include], + 0 AS is_spool + FROM #missing_index_detail AS m + GROUP BY m.query_hash, m.sql_handle, m.impact, m.database_name, m.schema_name, m.table_name + OPTION (RECOMPILE); + + RAISERROR(N'Inserting to #index_spool_ugly', 0, 1) WITH NOWAIT; + WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) + INSERT #index_spool_ugly + (query_hash, sql_handle, impact, database_name, schema_name, table_name, equality, inequality, include) + SELECT r.query_hash, + r.sql_handle, + (c.n.value('@EstimateIO', 'FLOAT') + (c.n.value('@EstimateCPU', 'FLOAT'))) + / ( 1 * NULLIF(ww.query_cost, 0)) * 100 AS impact, + o.n.value('@Database', 'NVARCHAR(128)') AS output_database, + o.n.value('@Schema', 'NVARCHAR(128)') AS output_schema, + o.n.value('@Table', 'NVARCHAR(128)') AS output_table, + k.n.value('@Column', 'NVARCHAR(128)') AS range_column, + e.n.value('@Column', 'NVARCHAR(128)') AS expression_column, + o.n.value('@Column', 'NVARCHAR(128)') AS output_column + FROM #relop AS r + JOIN #working_warnings AS ww + ON ww.query_hash = r.query_hash + CROSS APPLY r.relop.nodes('/p:RelOp') AS c(n) + CROSS APPLY r.relop.nodes('/p:RelOp/p:OutputList/p:ColumnReference') AS o(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:Spool/p:SeekPredicateNew/p:SeekKeys/p:Prefix/p:RangeColumns/p:ColumnReference') AS k(n) + OUTER APPLY r.relop.nodes('/p:RelOp/p:Spool/p:SeekPredicateNew/p:SeekKeys/p:Prefix/p:RangeExpressions/p:ColumnReference') AS e(n) + WHERE r.relop.exist('/p:RelOp[@PhysicalOp="Index Spool" and @LogicalOp="Eager Spool"]') = 1 + + RAISERROR(N'Inserting to spools to #missing_index_pretty', 0, 1) WITH NOWAIT; + INSERT #missing_index_pretty + (query_hash, sql_handle, impact, database_name, schema_name, table_name, equality, inequality, include, is_spool) + SELECT DISTINCT + isu.query_hash, + isu.sql_handle, + isu.impact, + isu.database_name, + isu.schema_name, + isu.table_name + , STUFF(( SELECT DISTINCT N', ' + ISNULL(isu2.equality, '') AS column_name + FROM #index_spool_ugly AS isu2 + WHERE isu2.equality IS NOT NULL + AND isu.query_hash = isu2.query_hash + AND isu.sql_handle = isu2.sql_handle + AND isu.impact = isu2.impact + AND isu.database_name = isu2.database_name + AND isu.schema_name = isu2.schema_name + AND isu.table_name = isu2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS equality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(isu2.inequality, '') AS column_name + FROM #index_spool_ugly AS isu2 + WHERE isu2.inequality IS NOT NULL + AND isu.query_hash = isu2.query_hash + AND isu.sql_handle = isu2.sql_handle + AND isu.impact = isu2.impact + AND isu.database_name = isu2.database_name + AND isu.schema_name = isu2.schema_name + AND isu.table_name = isu2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS inequality + , STUFF(( SELECT DISTINCT N', ' + ISNULL(isu2.include, '') AS column_name + FROM #index_spool_ugly AS isu2 + WHERE isu2.include IS NOT NULL + AND isu.query_hash = isu2.query_hash + AND isu.sql_handle = isu2.sql_handle + AND isu.impact = isu2.impact + AND isu.database_name = isu2.database_name + AND isu.schema_name = isu2.schema_name + AND isu.table_name = isu2.table_name + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'NVARCHAR(MAX)'), 1, 2, N'') AS include, + 1 AS is_spool + FROM #index_spool_ugly AS isu + + + RAISERROR(N'Updating missing index information', 0, 1) WITH NOWAIT; + WITH missing AS ( + SELECT DISTINCT + mip.query_hash, + mip.sql_handle, + N'' + AS full_details + FROM #missing_index_pretty AS mip + GROUP BY mip.query_hash, mip.sql_handle, mip.impact + ) + UPDATE ww + SET ww.missing_indexes = m.full_details + FROM #working_warnings AS ww + JOIN missing AS m + ON m.sql_handle = ww.sql_handle + OPTION (RECOMPILE); + + RAISERROR(N'Filling in missing index blanks', 0, 1) WITH NOWAIT; + UPDATE ww + SET ww.missing_indexes = + CASE WHEN ww.missing_indexes IS NULL + THEN '' + ELSE ww.missing_indexes + END + FROM #working_warnings AS ww + OPTION (RECOMPILE); + +END +/*End Missing Index*/ + +RAISERROR(N'General query dispositions: frequent executions, long running, etc.', 0, 1) WITH NOWAIT; + +WITH XMLNAMESPACES('http://schemas.microsoft.com/sqlserver/2004/07/showplan' AS p) +UPDATE b +SET b.frequent_execution = CASE WHEN wm.xpm > @execution_threshold THEN 1 END , + b.near_parallel = CASE WHEN b.query_cost BETWEEN @ctp * (1 - (@ctp_threshold_pct / 100.0)) AND @ctp THEN 1 END, + b.long_running = CASE WHEN wm.avg_duration > @long_running_query_warning_seconds THEN 1 + WHEN wm.max_duration > @long_running_query_warning_seconds THEN 1 + WHEN wm.avg_cpu_time > @long_running_query_warning_seconds THEN 1 + WHEN wm.max_cpu_time > @long_running_query_warning_seconds THEN 1 END, + b.is_key_lookup_expensive = CASE WHEN b.query_cost >= (@ctp / 2) AND b.key_lookup_cost >= b.query_cost * .5 THEN 1 END, + b.is_sort_expensive = CASE WHEN b.query_cost >= (@ctp / 2) AND b.sort_cost >= b.query_cost * .5 THEN 1 END, + b.is_remote_query_expensive = CASE WHEN b.remote_query_cost >= b.query_cost * .05 THEN 1 END, + b.is_unused_grant = CASE WHEN percent_memory_grant_used <= @memory_grant_warning_percent AND min_query_max_used_memory > @min_memory_per_query THEN 1 END, + b.long_running_low_cpu = CASE WHEN wm.avg_duration > wm.avg_cpu_time * 4 AND avg_cpu_time < 500. THEN 1 END, + b.low_cost_high_cpu = CASE WHEN b.query_cost < 10 AND wm.avg_cpu_time > 5000. THEN 1 END, + b.is_spool_expensive = CASE WHEN b.query_cost > (@ctp / 2) AND b.index_spool_cost >= b.query_cost * .1 THEN 1 END, + b.is_spool_more_rows = CASE WHEN b.index_spool_rows >= wm.min_rowcount THEN 1 END, + b.is_bad_estimate = CASE WHEN wm.avg_rowcount > 0 AND (b.estimated_rows * 1000 < wm.avg_rowcount OR b.estimated_rows > wm.avg_rowcount * 1000) THEN 1 END, + b.is_big_log = CASE WHEN wm.avg_log_bytes_used >= (@log_size_mb / 2.) THEN 1 END, + b.is_big_tempdb = CASE WHEN wm.avg_tempdb_space_used >= (@avg_tempdb_data_file / 2.) THEN 1 END +FROM #working_warnings AS b +JOIN #working_metrics AS wm +ON b.plan_id = wm.plan_id +AND b.query_id = wm.query_id +JOIN #working_plan_text AS wpt +ON b.plan_id = wpt.plan_id +AND b.query_id = wpt.query_id +OPTION (RECOMPILE); + + +RAISERROR('Populating Warnings column', 0, 1) WITH NOWAIT; +/* Populate warnings */ +UPDATE b +SET b.warnings = SUBSTRING( + CASE WHEN b.warning_no_join_predicate = 1 THEN ', No Join Predicate' ELSE '' END + + CASE WHEN b.compile_timeout = 1 THEN ', Compilation Timeout' ELSE '' END + + CASE WHEN b.compile_memory_limit_exceeded = 1 THEN ', Compile Memory Limit Exceeded' ELSE '' END + + CASE WHEN b.is_forced_plan = 1 THEN ', Forced Plan' ELSE '' END + + CASE WHEN b.is_forced_parameterized = 1 THEN ', Forced Parameterization' ELSE '' END + + CASE WHEN b.unparameterized_query = 1 THEN ', Unparameterized Query' ELSE '' END + + CASE WHEN b.missing_index_count > 0 THEN ', Missing Indexes (' + CAST(b.missing_index_count AS NVARCHAR(3)) + ')' ELSE '' END + + CASE WHEN b.unmatched_index_count > 0 THEN ', Unmatched Indexes (' + CAST(b.unmatched_index_count AS NVARCHAR(3)) + ')' ELSE '' END + + CASE WHEN b.is_cursor = 1 THEN ', Cursor' + + CASE WHEN b.is_optimistic_cursor = 1 THEN '; optimistic' ELSE '' END + + CASE WHEN b.is_forward_only_cursor = 0 THEN '; not forward only' ELSE '' END + + CASE WHEN b.is_cursor_dynamic = 1 THEN '; dynamic' ELSE '' END + + CASE WHEN b.is_fast_forward_cursor = 1 THEN '; fast forward' ELSE '' END + ELSE '' END + + CASE WHEN b.is_parallel = 1 THEN ', Parallel' ELSE '' END + + CASE WHEN b.near_parallel = 1 THEN ', Nearly Parallel' ELSE '' END + + CASE WHEN b.frequent_execution = 1 THEN ', Frequent Execution' ELSE '' END + + CASE WHEN b.plan_warnings = 1 THEN ', Plan Warnings' ELSE '' END + + CASE WHEN b.parameter_sniffing = 1 THEN ', Parameter Sniffing' ELSE '' END + + CASE WHEN b.long_running = 1 THEN ', Long Running Query' ELSE '' END + + CASE WHEN b.downlevel_estimator = 1 THEN ', Downlevel CE' ELSE '' END + + CASE WHEN b.implicit_conversions = 1 THEN ', Implicit Conversions' ELSE '' END + + CASE WHEN b.plan_multiple_plans = 1 THEN ', Multiple Plans' ELSE '' END + + CASE WHEN b.is_trivial = 1 THEN ', Trivial Plans' ELSE '' END + + CASE WHEN b.is_forced_serial = 1 THEN ', Forced Serialization' ELSE '' END + + CASE WHEN b.is_key_lookup_expensive = 1 THEN ', Expensive Key Lookup' ELSE '' END + + CASE WHEN b.is_remote_query_expensive = 1 THEN ', Expensive Remote Query' ELSE '' END + + CASE WHEN b.trace_flags_session IS NOT NULL THEN ', Session Level Trace Flag(s) Enabled: ' + b.trace_flags_session ELSE '' END + + CASE WHEN b.is_unused_grant = 1 THEN ', Unused Memory Grant' ELSE '' END + + CASE WHEN b.function_count > 0 THEN ', Calls ' + CONVERT(VARCHAR(10), b.function_count) + ' function(s)' ELSE '' END + + CASE WHEN b.clr_function_count > 0 THEN ', Calls ' + CONVERT(VARCHAR(10), b.clr_function_count) + ' CLR function(s)' ELSE '' END + + CASE WHEN b.is_table_variable = 1 THEN ', Table Variables' ELSE '' END + + CASE WHEN b.no_stats_warning = 1 THEN ', Columns With No Statistics' ELSE '' END + + CASE WHEN b.relop_warnings = 1 THEN ', Operator Warnings' ELSE '' END + + CASE WHEN b.is_table_scan = 1 THEN ', Table Scans' ELSE '' END + + CASE WHEN b.backwards_scan = 1 THEN ', Backwards Scans' ELSE '' END + + CASE WHEN b.forced_index = 1 THEN ', Forced Indexes' ELSE '' END + + CASE WHEN b.forced_seek = 1 THEN ', Forced Seeks' ELSE '' END + + CASE WHEN b.forced_scan = 1 THEN ', Forced Scans' ELSE '' END + + CASE WHEN b.columnstore_row_mode = 1 THEN ', ColumnStore Row Mode ' ELSE '' END + + CASE WHEN b.is_computed_scalar = 1 THEN ', Computed Column UDF ' ELSE '' END + + CASE WHEN b.is_sort_expensive = 1 THEN ', Expensive Sort' ELSE '' END + + CASE WHEN b.is_computed_filter = 1 THEN ', Filter UDF' ELSE '' END + + CASE WHEN b.index_ops >= 5 THEN ', >= 5 Indexes Modified' ELSE '' END + + CASE WHEN b.is_row_level = 1 THEN ', Row Level Security' ELSE '' END + + CASE WHEN b.is_spatial = 1 THEN ', Spatial Index' ELSE '' END + + CASE WHEN b.index_dml = 1 THEN ', Index DML' ELSE '' END + + CASE WHEN b.table_dml = 1 THEN ', Table DML' ELSE '' END + + CASE WHEN b.low_cost_high_cpu = 1 THEN ', Low Cost High CPU' ELSE '' END + + CASE WHEN b.long_running_low_cpu = 1 THEN + ', Long Running With Low CPU' ELSE '' END + + CASE WHEN b.stale_stats = 1 THEN + ', Statistics used have > 100k modifications in the last 7 days' ELSE '' END + + CASE WHEN b.is_adaptive = 1 THEN + ', Adaptive Joins' ELSE '' END + + CASE WHEN b.is_spool_expensive = 1 THEN + ', Expensive Index Spool' ELSE '' END + + CASE WHEN b.is_spool_more_rows = 1 THEN + ', Large Index Row Spool' ELSE '' END + + CASE WHEN b.is_bad_estimate = 1 THEN + ', Row estimate mismatch' ELSE '' END + + CASE WHEN b.is_big_log = 1 THEN + ', High log use' ELSE '' END + + CASE WHEN b.is_big_tempdb = 1 THEN ', High tempdb use' ELSE '' END + + CASE WHEN b.is_paul_white_electric = 1 THEN ', SWITCH!' ELSE '' END + + CASE WHEN b.is_row_goal = 1 THEN ', Row Goals' ELSE '' END + + CASE WHEN b.is_mstvf = 1 THEN ', MSTVFs' ELSE '' END + + CASE WHEN b.is_mm_join = 1 THEN ', Many to Many Merge' ELSE '' END + + CASE WHEN b.is_nonsargable = 1 THEN ', non-SARGables' ELSE '' END + , 2, 200000) +FROM #working_warnings b +OPTION (RECOMPILE); + + +END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure generating warnings.', 0,1) WITH NOWAIT; + + IF @sql_select IS NOT NULL + BEGIN + SET @msg = N'Last @sql_select: ' + @sql_select; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @error_severity = ERROR_SEVERITY(), @error_state = ERROR_STATE(); + RAISERROR (@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + RETURN; +END CATCH; + + +BEGIN TRY +BEGIN + +RAISERROR(N'Checking for parameter sniffing symptoms', 0, 1) WITH NOWAIT; + +UPDATE b +SET b.parameter_sniffing_symptoms = +CASE WHEN b.count_executions < 2 THEN 'Too few executions to compare (< 2).' + ELSE + SUBSTRING( + /*Duration*/ + CASE WHEN (b.min_duration * 100) < (b.avg_duration) THEN ', Fast sometimes' ELSE '' END + + CASE WHEN (b.max_duration) > (b.avg_duration * 100) THEN ', Slow sometimes' ELSE '' END + + CASE WHEN (b.last_duration * 100) < (b.avg_duration) THEN ', Fast last run' ELSE '' END + + CASE WHEN (b.last_duration) > (b.avg_duration * 100) THEN ', Slow last run' ELSE '' END + + /*CPU*/ + CASE WHEN (b.min_cpu_time / b.avg_dop) * 100 < (b.avg_cpu_time / b.avg_dop) THEN ', Low CPU sometimes' ELSE '' END + + CASE WHEN (b.max_cpu_time / b.max_dop) > (b.avg_cpu_time / b.avg_dop) * 100 THEN ', High CPU sometimes' ELSE '' END + + CASE WHEN (b.last_cpu_time / b.last_dop) * 100 < (b.avg_cpu_time / b.avg_dop) THEN ', Low CPU last run' ELSE '' END + + CASE WHEN (b.last_cpu_time / b.last_dop) > (b.avg_cpu_time / b.avg_dop) * 100 THEN ', High CPU last run' ELSE '' END + + /*Logical Reads*/ + CASE WHEN (b.min_logical_io_reads * 100) < (b.avg_logical_io_reads) THEN ', Low reads sometimes' ELSE '' END + + CASE WHEN (b.max_logical_io_reads) > (b.avg_logical_io_reads * 100) THEN ', High reads sometimes' ELSE '' END + + CASE WHEN (b.last_logical_io_reads * 100) < (b.avg_logical_io_reads) THEN ', Low reads last run' ELSE '' END + + CASE WHEN (b.last_logical_io_reads) > (b.avg_logical_io_reads * 100) THEN ', High reads last run' ELSE '' END + + /*Logical Writes*/ + CASE WHEN (b.min_logical_io_writes * 100) < (b.avg_logical_io_writes) THEN ', Low writes sometimes' ELSE '' END + + CASE WHEN (b.max_logical_io_writes) > (b.avg_logical_io_writes * 100) THEN ', High writes sometimes' ELSE '' END + + CASE WHEN (b.last_logical_io_writes * 100) < (b.avg_logical_io_writes) THEN ', Low writes last run' ELSE '' END + + CASE WHEN (b.last_logical_io_writes) > (b.avg_logical_io_writes * 100) THEN ', High writes last run' ELSE '' END + + /*Physical Reads*/ + CASE WHEN (b.min_physical_io_reads * 100) < (b.avg_physical_io_reads) THEN ', Low physical reads sometimes' ELSE '' END + + CASE WHEN (b.max_physical_io_reads) > (b.avg_physical_io_reads * 100) THEN ', High physical reads sometimes' ELSE '' END + + CASE WHEN (b.last_physical_io_reads * 100) < (b.avg_physical_io_reads) THEN ', Low physical reads last run' ELSE '' END + + CASE WHEN (b.last_physical_io_reads) > (b.avg_physical_io_reads * 100) THEN ', High physical reads last run' ELSE '' END + + /*Memory*/ + CASE WHEN (b.min_query_max_used_memory * 100) < (b.avg_query_max_used_memory) THEN ', Low memory sometimes' ELSE '' END + + CASE WHEN (b.max_query_max_used_memory) > (b.avg_query_max_used_memory * 100) THEN ', High memory sometimes' ELSE '' END + + CASE WHEN (b.last_query_max_used_memory * 100) < (b.avg_query_max_used_memory) THEN ', Low memory last run' ELSE '' END + + CASE WHEN (b.last_query_max_used_memory) > (b.avg_query_max_used_memory * 100) THEN ', High memory last run' ELSE '' END + + /*Duration*/ + CASE WHEN b.min_rowcount * 100 < b.avg_rowcount THEN ', Low row count sometimes' ELSE '' END + + CASE WHEN b.max_rowcount > b.avg_rowcount * 100 THEN ', High row count sometimes' ELSE '' END + + CASE WHEN b.last_rowcount * 100 < b.avg_rowcount THEN ', Low row count run' ELSE '' END + + CASE WHEN b.last_rowcount > b.avg_rowcount * 100 THEN ', High row count last run' ELSE '' END + + /*DOP*/ + CASE WHEN b.min_dop <> b.max_dop THEN ', Serial sometimes' ELSE '' END + + CASE WHEN b.min_dop <> b.max_dop AND b.last_dop = 1 THEN ', Serial last run' ELSE '' END + + CASE WHEN b.min_dop <> b.max_dop AND b.last_dop > 1 THEN ', Parallel last run' ELSE '' END + + /*tempdb*/ + CASE WHEN b.min_tempdb_space_used * 100 < b.avg_tempdb_space_used THEN ', Low tempdb sometimes' ELSE '' END + + CASE WHEN b.max_tempdb_space_used > b.avg_tempdb_space_used * 100 THEN ', High tempdb sometimes' ELSE '' END + + CASE WHEN b.last_tempdb_space_used * 100 < b.avg_tempdb_space_used THEN ', Low tempdb run' ELSE '' END + + CASE WHEN b.last_tempdb_space_used > b.avg_tempdb_space_used * 100 THEN ', High tempdb last run' ELSE '' END + + /*tlog*/ + CASE WHEN b.min_log_bytes_used * 100 < b.avg_log_bytes_used THEN ', Low log use sometimes' ELSE '' END + + CASE WHEN b.max_log_bytes_used > b.avg_log_bytes_used * 100 THEN ', High log use sometimes' ELSE '' END + + CASE WHEN b.last_log_bytes_used * 100 < b.avg_log_bytes_used THEN ', Low log use run' ELSE '' END + + CASE WHEN b.last_log_bytes_used > b.avg_log_bytes_used * 100 THEN ', High log use last run' ELSE '' END + , 2, 200000) + END +FROM #working_metrics AS b +OPTION (RECOMPILE); + +END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure analyzing parameter sniffing', 0,1) WITH NOWAIT; + + IF @sql_select IS NOT NULL + BEGIN + SET @msg = N'Last @sql_select: ' + @sql_select; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @error_severity = ERROR_SEVERITY(), @error_state = ERROR_STATE(); + RAISERROR (@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + RETURN; +END CATCH; + +BEGIN TRY + +BEGIN + +IF (@Failed = 0 AND @ExportToExcel = 0 AND @SkipXML = 0) +BEGIN + +RAISERROR(N'Returning regular results', 0, 1) WITH NOWAIT; + +WITH x AS ( +SELECT wpt.database_name, ww.query_cost, wm.plan_id, wm.query_id, wm.query_id_all_plan_ids, wpt.query_sql_text, wm.proc_or_function_name, wpt.query_plan_xml, ww.warnings, wpt.pattern, + wm.parameter_sniffing_symptoms, wpt.top_three_waits, ww.missing_indexes, ww.implicit_conversion_info, ww.cached_execution_parameters, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time, + wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads, + wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount, + wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used, + wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads, + wm.first_execution_time, wm.last_execution_time, wpt.last_force_failure_reason_desc, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn +FROM #working_plan_text AS wpt +JOIN #working_warnings AS ww + ON wpt.plan_id = ww.plan_id + AND wpt.query_id = ww.query_id +JOIN #working_metrics AS wm + ON wpt.plan_id = wm.plan_id + AND wpt.query_id = wm.query_id +) +SELECT * +FROM x +WHERE x.rn = 1 +ORDER BY x.last_execution_time +OPTION (RECOMPILE); + +END; + +IF (@Failed = 1 AND @ExportToExcel = 0 AND @SkipXML = 0) +BEGIN + +RAISERROR(N'Returning results for failed queries', 0, 1) WITH NOWAIT; + +WITH x AS ( +SELECT wpt.database_name, ww.query_cost, wm.plan_id, wm.query_id, wm.query_id_all_plan_ids, wpt.query_sql_text, wm.proc_or_function_name, wpt.query_plan_xml, ww.warnings, wpt.pattern, + wm.parameter_sniffing_symptoms, wpt.last_force_failure_reason_desc, wpt.top_three_waits, ww.missing_indexes, ww.implicit_conversion_info, ww.cached_execution_parameters, + wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time, + wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads, + wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount, + wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used, + wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads, + wm.first_execution_time, wm.last_execution_time, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn +FROM #working_plan_text AS wpt +JOIN #working_warnings AS ww + ON wpt.plan_id = ww.plan_id + AND wpt.query_id = ww.query_id +JOIN #working_metrics AS wm + ON wpt.plan_id = wm.plan_id + AND wpt.query_id = wm.query_id +) +SELECT * +FROM x +WHERE x.rn = 1 +ORDER BY x.last_execution_time +OPTION (RECOMPILE); + +END; + +IF (@ExportToExcel = 1 AND @SkipXML = 0) +BEGIN + +RAISERROR(N'Returning results for Excel export', 0, 1) WITH NOWAIT; + +UPDATE #working_plan_text +SET query_sql_text = SUBSTRING(REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(query_sql_text)),' ','<>'),'><',''),'<>',' '), 1, 31000) +OPTION (RECOMPILE); + +WITH x AS ( +SELECT wpt.database_name, ww.query_cost, wm.plan_id, wm.query_id, wm.query_id_all_plan_ids, wpt.query_sql_text, wm.proc_or_function_name, ww.warnings, wpt.pattern, + wm.parameter_sniffing_symptoms, wpt.last_force_failure_reason_desc, wpt.top_three_waits, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time, + wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads, + wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount, + wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used, + wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads, + wm.first_execution_time, wm.last_execution_time, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn +FROM #working_plan_text AS wpt +JOIN #working_warnings AS ww + ON wpt.plan_id = ww.plan_id + AND wpt.query_id = ww.query_id +JOIN #working_metrics AS wm + ON wpt.plan_id = wm.plan_id + AND wpt.query_id = wm.query_id +) +SELECT * +FROM x +WHERE x.rn = 1 +ORDER BY x.last_execution_time +OPTION (RECOMPILE); + +END; + +IF (@ExportToExcel = 0 AND @SkipXML = 1) +BEGIN + +RAISERROR(N'Returning results for skipped XML', 0, 1) WITH NOWAIT; + +WITH x AS ( +SELECT wpt.database_name, wm.plan_id, wm.query_id, wm.query_id_all_plan_ids, wpt.query_sql_text, wpt.query_plan_xml, wpt.pattern, + wm.parameter_sniffing_symptoms, wpt.top_three_waits, wm.count_executions, wm.count_compiles, wm.total_cpu_time, wm.avg_cpu_time, + wm.total_duration, wm.avg_duration, wm.total_logical_io_reads, wm.avg_logical_io_reads, + wm.total_physical_io_reads, wm.avg_physical_io_reads, wm.total_logical_io_writes, wm.avg_logical_io_writes, wm.total_rowcount, wm.avg_rowcount, + wm.total_query_max_used_memory, wm.avg_query_max_used_memory, wm.total_tempdb_space_used, wm.avg_tempdb_space_used, + wm.total_log_bytes_used, wm.avg_log_bytes_used, wm.total_num_physical_io_reads, wm.avg_num_physical_io_reads, + wm.first_execution_time, wm.last_execution_time, wpt.last_force_failure_reason_desc, wpt.context_settings, ROW_NUMBER() OVER (PARTITION BY wm.plan_id, wm.query_id, wm.last_execution_time ORDER BY wm.plan_id) AS rn +FROM #working_plan_text AS wpt +JOIN #working_metrics AS wm + ON wpt.plan_id = wm.plan_id + AND wpt.query_id = wm.query_id +) +SELECT * +FROM x +WHERE x.rn = 1 +ORDER BY x.last_execution_time +OPTION (RECOMPILE); + +END; + +END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure returning results', 0,1) WITH NOWAIT; + + IF @sql_select IS NOT NULL + BEGIN + SET @msg = N'Last @sql_select: ' + @sql_select; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @error_severity = ERROR_SEVERITY(), @error_state = ERROR_STATE(); + RAISERROR (@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + RETURN; +END CATCH; + +BEGIN TRY +BEGIN + +IF (@ExportToExcel = 0 AND @HideSummary = 0 AND @SkipXML = 0) +BEGIN + RAISERROR('Building query plan summary data.', 0, 1) WITH NOWAIT; + + /* Build summary data */ + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE frequent_execution = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 1, + 100, + 'Execution Pattern', + 'Frequently Executed Queries', + 'https://www.brentozar.com/blitzcache/frequently-executed-queries/', + 'Queries are being executed more than ' + + CAST (@execution_threshold AS VARCHAR(5)) + + ' times per minute. This can put additional load on the server, even when queries are lightweight.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE parameter_sniffing = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 2, + 50, + 'Parameterization', + 'Parameter Sniffing', + 'https://www.brentozar.com/blitzcache/parameter-sniffing/', + 'There are signs of parameter sniffing (wide variance in rows return or time to execute). Investigate query patterns and tune code appropriately.') ; + + /* Forced execution plans */ + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_forced_plan = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 3, + 5, + 'Parameterization', + 'Forced Plans', + 'https://www.brentozar.com/blitzcache/forced-plans/', + 'Execution plans have been compiled with forced plans, either through FORCEPLAN, plan guides, or forced parameterization. This will make general tuning efforts less effective.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_cursor = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 4, + 200, + 'Cursors', + 'Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'There are cursors in the plan cache. This is neither good nor bad, but it is a thing. Cursors are weird in SQL Server.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_cursor = 1 + AND is_optimistic_cursor = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 4, + 200, + 'Cursors', + 'Optimistic Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'There are optimistic cursors in the plan cache, which can harm performance.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_cursor = 1 + AND is_forward_only_cursor = 0 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 4, + 200, + 'Cursors', + 'Non-forward Only Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'There are non-forward only cursors in the plan cache, which can harm performance.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_cursor = 1 + AND is_cursor_dynamic = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (4, + 200, + 'Cursors', + 'Dynamic Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'Dynamic Cursors inhibit parallelism!.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_cursor = 1 + AND is_fast_forward_cursor = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (4, + 200, + 'Cursors', + 'Fast Forward Cursors', + 'https://www.brentozar.com/blitzcache/cursors-found-slow-queries/', + 'Fast forward cursors inhibit parallelism!.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_forced_parameterized = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 5, + 50, + 'Parameterization', + 'Forced Parameterization', + 'https://www.brentozar.com/blitzcache/forced-parameterization/', + 'Execution plans have been compiled with forced parameterization.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_parallel = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 6, + 200, + 'Execution Plans', + 'Parallelism', + 'https://www.brentozar.com/blitzcache/parallel-plans-detected/', + 'Parallel plans detected. These warrant investigation, but are neither good nor bad.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.near_parallel = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 7, + 200, + 'Execution Plans', + 'Nearly Parallel', + 'https://www.brentozar.com/blitzcache/query-cost-near-cost-threshold-parallelism/', + 'Queries near the cost threshold for parallelism. These may go parallel when you least expect it.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.plan_warnings = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 8, + 50, + 'Execution Plans', + 'Query Plan Warnings', + 'https://www.brentozar.com/blitzcache/query-plan-warnings/', + 'Warnings detected in execution plans. SQL Server is telling you that something bad is going on that requires your attention.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.long_running = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 9, + 50, + 'Performance', + 'Long Running Queries', + 'https://www.brentozar.com/blitzcache/long-running-queries/', + 'Long running queries have been found. These are queries with an average duration longer than ' + + CAST(@long_running_query_warning_seconds / 1000 / 1000 AS VARCHAR(5)) + + ' second(s). These queries should be investigated for additional tuning options.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.missing_index_count > 0 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 10, + 50, + 'Performance', + 'Missing Index Request', + 'https://www.brentozar.com/blitzcache/missing-index-request/', + 'Queries found with missing indexes.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.downlevel_estimator = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 13, + 200, + 'Cardinality', + 'Legacy Cardinality Estimator in Use', + 'https://www.brentozar.com/blitzcache/legacy-cardinality-estimator/', + 'A legacy cardinality estimator is being used by one or more queries. Investigate whether you need to be using this cardinality estimator. This may be caused by compatibility levels, global trace flags, or query level trace flags.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.implicit_conversions = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 14, + 50, + 'Performance', + 'Implicit Conversions', + 'https://www.brentozar.com/go/implicit', + 'One or more queries are comparing two fields that are not of the same data type.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE busy_loops = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 16, + 100, + 'Performance', + 'Busy Loops', + 'https://www.brentozar.com/blitzcache/busy-loops/', + 'Operations have been found that are executed 100 times more often than the number of rows returned by each iteration. This is an indicator that something is off in query execution.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE tvf_join = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 17, + 50, + 'Performance', + 'Joining to table valued functions', + 'https://www.brentozar.com/blitzcache/tvf-join/', + 'Execution plans have been found that join to table valued functions (TVFs). TVFs produce inaccurate estimates of the number of rows returned and can lead to any number of query plan problems.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE compile_timeout = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 18, + 50, + 'Execution Plans', + 'Compilation timeout', + 'https://www.brentozar.com/blitzcache/compilation-timeout/', + 'Query compilation timed out for one or more queries. SQL Server did not find a plan that meets acceptable performance criteria in the time allotted so the best guess was returned. There is a very good chance that this plan isn''t even below average - it''s probably terrible.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE compile_memory_limit_exceeded = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 19, + 50, + 'Execution Plans', + 'Compilation memory limit exceeded', + 'https://www.brentozar.com/blitzcache/compile-memory-limit-exceeded/', + 'The optimizer has a limited amount of memory available. One or more queries are complex enough that SQL Server was unable to allocate enough memory to fully optimize the query. A best fit plan was found, and it''s probably terrible.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE warning_no_join_predicate = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 20, + 10, + 'Execution Plans', + 'No join predicate', + 'https://www.brentozar.com/blitzcache/no-join-predicate/', + 'Operators in a query have no join predicate. This means that all rows from one table will be matched with all rows from anther table producing a Cartesian product. That''s a whole lot of rows. This may be your goal, but it''s important to investigate why this is happening.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE plan_multiple_plans = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 21, + 200, + 'Execution Plans', + 'Multiple execution plans', + 'https://www.brentozar.com/blitzcache/multiple-plans/', + 'Queries exist with multiple execution plans (as determined by query_plan_hash). Investigate possible ways to parameterize these queries or otherwise reduce the plan count.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE unmatched_index_count > 0 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 22, + 100, + 'Performance', + 'Unmatched indexes', + 'https://www.brentozar.com/blitzcache/unmatched-indexes', + 'An index could have been used, but SQL Server chose not to use it - likely due to parameterization and filtered indexes.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE unparameterized_query = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 23, + 100, + 'Parameterization', + 'Unparameterized queries', + 'https://www.brentozar.com/blitzcache/unparameterized-queries', + 'Unparameterized queries found. These could be ad hoc queries, data exploration, or queries using "OPTIMIZE FOR UNKNOWN".'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings + WHERE is_trivial = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 24, + 100, + 'Execution Plans', + 'Trivial Plans', + 'https://www.brentozar.com/blitzcache/trivial-plans', + 'Trivial plans get almost no optimization. If you''re finding these in the top worst queries, something may be going wrong.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_forced_serial= 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 25, + 10, + 'Execution Plans', + 'Forced Serialization', + 'https://www.brentozar.com/blitzcache/forced-serialization/', + 'Something in your plan is forcing a serial query. Further investigation is needed if this is not by design.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_key_lookup_expensive= 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 26, + 100, + 'Execution Plans', + 'Expensive Key Lookups', + 'https://www.brentozar.com/blitzcache/expensive-key-lookups/', + 'There''s a key lookup in your plan that costs >=50% of the total plan cost.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_remote_query_expensive= 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 28, + 100, + 'Execution Plans', + 'Expensive Remote Query', + 'https://www.brentozar.com/blitzcache/expensive-remote-query/', + 'There''s a remote query in your plan that costs >=50% of the total plan cost.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.trace_flags_session IS NOT NULL + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 29, + 100, + 'Trace Flags', + 'Session Level Trace Flags Enabled', + 'https://www.brentozar.com/blitz/trace-flags-enabled-globally/', + 'Someone is enabling session level Trace Flags in a query.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_unused_grant IS NOT NULL + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 30, + 100, + 'Unused memory grants', + 'Queries are asking for more memory than they''re using', + 'https://www.brentozar.com/blitzcache/unused-memory-grants/', + 'Queries have large unused memory grants. This can cause concurrency issues, if queries are waiting a long time to get memory to run.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.function_count > 0 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 31, + 100, + 'Compute Scalar That References A Function', + 'This could be trouble if you''re using Scalar Functions or MSTVFs', + 'https://www.brentozar.com/blitzcache/compute-scalar-functions/', + 'Both of these will force queries to run serially, run at least once per row, and may result in poor cardinality estimates.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.clr_function_count > 0 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 32, + 100, + 'Compute Scalar That References A CLR Function', + 'This could be trouble if your CLR functions perform data access', + 'https://www.brentozar.com/blitzcache/compute-scalar-functions/', + 'May force queries to run serially, run at least once per row, and may result in poor cardinlity estimates.') ; + + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_table_variable = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 33, + 100, + 'Table Variables detected', + 'Beware nasty side effects', + 'https://www.brentozar.com/blitzcache/table-variables/', + 'All modifications are single threaded, and selects have really low row estimates.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.no_stats_warning = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 35, + 100, + 'Columns with no statistics', + 'Poor cardinality estimates may ensue', + 'https://www.brentozar.com/blitzcache/columns-no-statistics/', + 'Sometimes this happens with indexed views, other times because auto create stats is turned off.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.relop_warnings = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 36, + 100, + 'Operator Warnings', + 'SQL is throwing operator level plan warnings', + 'https://www.brentozar.com/blitzcache/query-plan-warnings/', + 'Check the plan for more details.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_table_scan = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 37, + 100, + 'Table Scans', + 'Your database has HEAPs', + 'https://www.brentozar.com/archive/2012/05/video-heaps/', + 'This may not be a problem. Run sp_BlitzIndex for more information.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.backwards_scan = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 38, + 100, + 'Backwards Scans', + 'Indexes are being read backwards', + 'https://www.brentozar.com/blitzcache/backwards-scans/', + 'This isn''t always a problem. They can cause serial zones in plans, and may need an index to match sort order.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.forced_index = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 39, + 100, + 'Index forcing', + 'Someone is using hints to force index usage', + 'https://www.brentozar.com/blitzcache/optimizer-forcing/', + 'This can cause inefficient plans, and will prevent missing index requests.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.forced_seek = 1 + OR p.forced_scan = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 40, + 100, + 'Seek/Scan forcing', + 'Someone is using hints to force index seeks/scans', + 'https://www.brentozar.com/blitzcache/optimizer-forcing/', + 'This can cause inefficient plans by taking seek vs scan choice away from the optimizer.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.columnstore_row_mode = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 41, + 100, + 'ColumnStore indexes operating in Row Mode', + 'Batch Mode is optimal for ColumnStore indexes', + 'https://www.brentozar.com/blitzcache/columnstore-indexes-operating-row-mode/', + 'ColumnStore indexes operating in Row Mode indicate really poor query choices.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_computed_scalar = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 42, + 50, + 'Computed Columns Referencing Scalar UDFs', + 'This makes a whole lot of stuff run serially', + 'https://www.brentozar.com/blitzcache/computed-columns-referencing-functions/', + 'This can cause a whole mess of bad serializartion problems.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_sort_expensive = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 43, + 100, + 'Execution Plans', + 'Expensive Sort', + 'https://www.brentozar.com/blitzcache/expensive-sorts/', + 'There''s a sort in your plan that costs >=50% of the total plan cost.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_computed_filter = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 44, + 50, + 'Filters Referencing Scalar UDFs', + 'This forces serialization', + 'https://www.brentozar.com/blitzcache/compute-scalar-functions/', + 'Someone put a Scalar UDF in the WHERE clause!') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.index_ops >= 5 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 45, + 100, + 'Many Indexes Modified', + 'Write Queries Are Hitting >= 5 Indexes', + 'https://www.brentozar.com/blitzcache/many-indexes-modified/', + 'This can cause lots of hidden I/O -- Run sp_BlitzIndex for more information.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_row_level = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 46, + 100, + 'Plan Confusion', + 'Row Level Security is in use', + 'https://www.brentozar.com/blitzcache/row-level-security/', + 'You may see a lot of confusing junk in your query plan.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_spatial = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 47, + 200, + 'Spatial Abuse', + 'You hit a Spatial Index', + 'https://www.brentozar.com/blitzcache/spatial-indexes/', + 'Purely informational.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.index_dml = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 48, + 150, + 'Index DML', + 'Indexes were created or dropped', + 'https://www.brentozar.com/blitzcache/index-dml/', + 'This can cause recompiles and stuff.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.table_dml = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 49, + 150, + 'Table DML', + 'Tables were created or dropped', + 'https://www.brentozar.com/blitzcache/table-dml/', + 'This can cause recompiles and stuff.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.long_running_low_cpu = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 50, + 150, + 'Long Running Low CPU', + 'You have a query that runs for much longer than it uses CPU', + 'https://www.brentozar.com/blitzcache/long-running-low-cpu/', + 'This can be a sign of blocking, linked servers, or poor client application code (ASYNC_NETWORK_IO).') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.low_cost_high_cpu = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 51, + 150, + 'Low Cost Query With High CPU', + 'You have a low cost query that uses a lot of CPU', + 'https://www.brentozar.com/blitzcache/low-cost-high-cpu/', + 'This can be a sign of functions or Dynamic SQL that calls black-box code.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.stale_stats = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 52, + 150, + 'Biblical Statistics', + 'Statistics used in queries are >7 days old with >100k modifications', + 'https://www.brentozar.com/blitzcache/stale-statistics/', + 'Ever heard of updating statistics?') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_adaptive = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 53, + 150, + 'Adaptive joins', + 'This is pretty cool -- you''re living in the future.', + 'https://www.brentozar.com/blitzcache/adaptive-joins/', + 'Joe Sack rules.') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_spool_expensive = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 54, + 150, + 'Expensive Index Spool', + 'You have an index spool, this is usually a sign that there''s an index missing somewhere.', + 'https://www.brentozar.com/blitzcache/eager-index-spools/', + 'Check operator predicates and output for index definition guidance') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_spool_more_rows = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 55, + 150, + 'Index Spools Many Rows', + 'You have an index spool that spools more rows than the query returns', + 'https://www.brentozar.com/blitzcache/eager-index-spools/', + 'Check operator predicates and output for index definition guidance') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_bad_estimate = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 56, + 100, + 'Potentially bad cardinality estimates', + 'Estimated rows are different from average rows by a factor of 10000', + 'https://www.brentozar.com/blitzcache/bad-estimates/', + 'This may indicate a performance problem if mismatches occur regularly') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_big_log = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 57, + 100, + 'High transaction log use', + 'This query on average uses more than half of the transaction log', + 'http://michaeljswart.com/2014/09/take-care-when-scripting-batches/', + 'This is probably a sign that you need to start batching queries') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_big_tempdb = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 58, + 100, + 'High tempdb use', + 'This query uses more than half of a data file on average', + 'No URL yet', + 'You should take a look at tempdb waits to see if you''re having problems') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_row_goal = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (59, + 200, + 'Row Goals', + 'This query had row goals introduced', + 'https://www.brentozar.com/archive/2018/01/sql-server-2017-cu3-adds-optimizer-row-goal-information-query-plans/', + 'This can be good or bad, and should be investigated for high read queries') ; + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_mstvf = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 60, + 100, + 'MSTVFs', + 'These have many of the same problems scalar UDFs have', + 'https://www.brentozar.com/blitzcache/tvf-join/', + 'Execution plans have been found that join to table valued functions (TVFs). TVFs produce inaccurate estimates of the number of rows returned and can lead to any number of query plan problems.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_mstvf = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (61, + 100, + 'Many to Many Merge', + 'These use secret worktables that could be doing lots of reads', + 'https://www.brentozar.com/archive/2018/04/many-mysteries-merge-joins/', + 'Occurs when join inputs aren''t known to be unique. Can be really bad when parallel.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_nonsargable = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (62, + 50, + 'Non-SARGable queries', + 'Queries may be using', + 'https://www.brentozar.com/blitzcache/non-sargable-predicates/', + 'Occurs when join inputs aren''t known to be unique. Can be really bad when parallel.'); + + IF EXISTS (SELECT 1/0 + FROM #working_warnings p + WHERE p.is_paul_white_electric = 1 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 998, + 200, + 'Is Paul White Electric?', + 'This query has a Switch operator in it!', + 'https://www.sql.kiwi/2013/06/hello-operator-my-switch-is-bored.html', + 'You should email this query plan to Paul: SQLkiwi at gmail dot com') ; + + + + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT + 999, + 200, + 'Database Level Statistics', + 'The database ' + sa.[database] + ' last had a stats update on ' + CONVERT(NVARCHAR(10), CONVERT(DATE, MAX(sa.last_update))) + ' and has ' + CONVERT(NVARCHAR(10), AVG(sa.modification_count)) + ' modifications on average.' AS Finding, + 'https://www.brentozar.com/blitzcache/stale-statistics/' AS URL, + 'Consider updating statistics more frequently,' AS Details + FROM #stats_agg AS sa + GROUP BY sa.[database] + HAVING MAX(sa.last_update) <= DATEADD(DAY, -7, SYSDATETIME()) + AND AVG(sa.modification_count) >= 100000; + + + IF EXISTS (SELECT 1/0 + FROM #trace_flags AS tf + WHERE tf.global_trace_flags IS NOT NULL + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 1000, + 255, + 'Global Trace Flags Enabled', + 'You have Global Trace Flags enabled on your server', + 'https://www.brentozar.com/blitz/trace-flags-enabled-globally/', + 'You have the following Global Trace Flags enabled: ' + (SELECT TOP 1 tf.global_trace_flags FROM #trace_flags AS tf WHERE tf.global_trace_flags IS NOT NULL)) ; + + + /* + Return worsts + */ + WITH worsts AS ( + SELECT gi.flat_date, + gi.start_range, + gi.end_range, + gi.total_avg_duration_ms, + gi.total_avg_cpu_time_ms, + gi.total_avg_logical_io_reads_mb, + gi.total_avg_physical_io_reads_mb, + gi.total_avg_logical_io_writes_mb, + gi.total_avg_query_max_used_memory_mb, + gi.total_rowcount, + gi.total_avg_log_bytes_mb, + gi.total_avg_tempdb_space, + gi.total_max_duration_ms, + gi.total_max_cpu_time_ms, + gi.total_max_logical_io_reads_mb, + gi.total_max_physical_io_reads_mb, + gi.total_max_logical_io_writes_mb, + gi.total_max_query_max_used_memory_mb, + gi.total_max_log_bytes_mb, + gi.total_max_tempdb_space, + CONVERT(NVARCHAR(20), gi.flat_date) AS worst_date, + CASE WHEN DATEPART(HOUR, gi.start_range) = 0 THEN ' midnight ' + WHEN DATEPART(HOUR, gi.start_range) <= 12 THEN CONVERT(NVARCHAR(3), DATEPART(HOUR, gi.start_range)) + 'am ' + WHEN DATEPART(HOUR, gi.start_range) > 12 THEN CONVERT(NVARCHAR(3), DATEPART(HOUR, gi.start_range) -12) + 'pm ' + END AS worst_start_time, + CASE WHEN DATEPART(HOUR, gi.end_range) = 0 THEN ' midnight ' + WHEN DATEPART(HOUR, gi.end_range) <= 12 THEN CONVERT(NVARCHAR(3), DATEPART(HOUR, gi.end_range)) + 'am ' + WHEN DATEPART(HOUR, gi.end_range) > 12 THEN CONVERT(NVARCHAR(3), DATEPART(HOUR, gi.end_range) -12) + 'pm ' + END AS worst_end_time + FROM #grouped_interval AS gi + ), /*averages*/ + duration_worst AS ( + SELECT TOP 1 'Your worst avg duration range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_duration_ms DESC + ), + cpu_worst AS ( + SELECT TOP 1 'Your worst avg cpu range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_cpu_time_ms DESC + ), + logical_reads_worst AS ( + SELECT TOP 1 'Your worst avg logical read range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_logical_io_reads_mb DESC + ), + physical_reads_worst AS ( + SELECT TOP 1 'Your worst avg physical read range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_physical_io_reads_mb DESC + ), + logical_writes_worst AS ( + SELECT TOP 1 'Your worst avg logical write range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_logical_io_writes_mb DESC + ), + memory_worst AS ( + SELECT TOP 1 'Your worst avg memory range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_query_max_used_memory_mb DESC + ), + rowcount_worst AS ( + SELECT TOP 1 'Your worst avg row count range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_rowcount DESC + ), + logbytes_worst AS ( + SELECT TOP 1 'Your worst avg log bytes range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_log_bytes_mb DESC + ), + tempdb_worst AS ( + SELECT TOP 1 'Your worst avg tempdb range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_avg_tempdb_space DESC + )/*maxes*/, + max_duration_worst AS ( + SELECT TOP 1 'Your worst max duration range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_duration_ms DESC + ), + max_cpu_worst AS ( + SELECT TOP 1 'Your worst max cpu range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_cpu_time_ms DESC + ), + max_logical_reads_worst AS ( + SELECT TOP 1 'Your worst max logical read range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_logical_io_reads_mb DESC + ), + max_physical_reads_worst AS ( + SELECT TOP 1 'Your worst max physical read range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_physical_io_reads_mb DESC + ), + max_logical_writes_worst AS ( + SELECT TOP 1 'Your worst max logical write range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_logical_io_writes_mb DESC + ), + max_memory_worst AS ( + SELECT TOP 1 'Your worst max memory range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_query_max_used_memory_mb DESC + ), + max_logbytes_worst AS ( + SELECT TOP 1 'Your worst max log bytes range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_log_bytes_mb DESC + ), + max_tempdb_worst AS ( + SELECT TOP 1 'Your worst max tempdb range was on ' + worsts.worst_date + ' between ' + worsts.worst_start_time + ' and ' + worsts.worst_end_time + '.' AS msg + FROM worsts + ORDER BY worsts.total_max_tempdb_space DESC + ) + INSERT #warning_results ( CheckID, Priority, FindingsGroup, Finding, URL, Details ) + /*averages*/ + SELECT 1002, 255, 'Worsts', 'Worst Avg Duration', 'N/A', duration_worst.msg + FROM duration_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg CPU', 'N/A', cpu_worst.msg + FROM cpu_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg Logical Reads', 'N/A', logical_reads_worst.msg + FROM logical_reads_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg Physical Reads', 'N/A', physical_reads_worst.msg + FROM physical_reads_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg Logical Writes', 'N/A', logical_writes_worst.msg + FROM logical_writes_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg Memory', 'N/A', memory_worst.msg + FROM memory_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Row Counts', 'N/A', rowcount_worst.msg + FROM rowcount_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg Log Bytes', 'N/A', logbytes_worst.msg + FROM logbytes_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Avg tempdb', 'N/A', tempdb_worst.msg + FROM tempdb_worst + UNION ALL + /*maxes*/ + SELECT 1002, 255, 'Worsts', 'Worst Max Duration', 'N/A', max_duration_worst.msg + FROM max_duration_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max CPU', 'N/A', max_cpu_worst.msg + FROM max_cpu_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max Logical Reads', 'N/A', max_logical_reads_worst.msg + FROM max_logical_reads_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max Physical Reads', 'N/A', max_physical_reads_worst.msg + FROM max_physical_reads_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max Logical Writes', 'N/A', max_logical_writes_worst.msg + FROM max_logical_writes_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max Memory', 'N/A', max_memory_worst.msg + FROM max_memory_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max Log Bytes', 'N/A', max_logbytes_worst.msg + FROM max_logbytes_worst + UNION ALL + SELECT 1002, 255, 'Worsts', 'Worst Max tempdb', 'N/A', max_tempdb_worst.msg + FROM max_tempdb_worst + OPTION (RECOMPILE); + + + IF NOT EXISTS (SELECT 1/0 + FROM #warning_results AS bcr + WHERE bcr.Priority = 2147483646 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (2147483646, + 255, + 'Need more help?' , + 'Paste your plan on the internet!', + 'http://pastetheplan.com', + 'This makes it easy to share plans and post them to Q&A sites like https://dba.stackexchange.com/!') ; + + + IF NOT EXISTS (SELECT 1/0 + FROM #warning_results AS bcr + WHERE bcr.Priority = 2147483647 + ) + INSERT INTO #warning_results (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES ( + 2147483647, + 255, + 'Thanks for using sp_BlitzQueryStore!' , + 'From Your Community Volunteers', + 'http://FirstResponderKit.org', + 'We hope you found this tool useful. Current version: ' + @Version + ' released on ' + CONVERT(NVARCHAR(30), @VersionDate) + '.') ; + + + + SELECT Priority, + FindingsGroup, + Finding, + URL, + Details, + CheckID + FROM #warning_results + GROUP BY Priority, + FindingsGroup, + Finding, + URL, + Details, + CheckID + ORDER BY Priority ASC, FindingsGroup, Finding, CheckID ASC + OPTION (RECOMPILE); + + + +END; + +END; +END TRY +BEGIN CATCH + RAISERROR (N'Failure returning warnings', 0,1) WITH NOWAIT; + + IF @sql_select IS NOT NULL + BEGIN + SET @msg = N'Last @sql_select: ' + @sql_select; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @error_severity = ERROR_SEVERITY(), @error_state = ERROR_STATE(); + RAISERROR (@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + RETURN; +END CATCH; + +IF @Debug = 1 + +BEGIN TRY + +BEGIN + +RAISERROR(N'Returning debugging data from temp tables', 0, 1) WITH NOWAIT; + +--Table content debugging + +SELECT '#working_metrics' AS table_name, * +FROM #working_metrics AS wm +OPTION (RECOMPILE); + +SELECT '#working_plan_text' AS table_name, * +FROM #working_plan_text AS wpt +OPTION (RECOMPILE); + +SELECT '#working_warnings' AS table_name, * +FROM #working_warnings AS ww +OPTION (RECOMPILE); + +SELECT '#working_wait_stats' AS table_name, * +FROM #working_wait_stats wws +OPTION (RECOMPILE); + +SELECT '#grouped_interval' AS table_name, * +FROM #grouped_interval +OPTION (RECOMPILE); + +SELECT '#working_plans' AS table_name, * +FROM #working_plans +OPTION (RECOMPILE); + +SELECT '#stats_agg' AS table_name, * +FROM #stats_agg +OPTION (RECOMPILE); + +SELECT '#trace_flags' AS table_name, * +FROM #trace_flags +OPTION (RECOMPILE); + +SELECT '#statements' AS table_name, * +FROM #statements AS s +OPTION (RECOMPILE); + +SELECT '#query_plan' AS table_name, * +FROM #query_plan AS qp +OPTION (RECOMPILE); + +SELECT '#relop' AS table_name, * +FROM #relop AS r +OPTION (RECOMPILE); + +SELECT '#plan_cost' AS table_name, * +FROM #plan_cost AS pc +OPTION (RECOMPILE); + +SELECT '#est_rows' AS table_name, * +FROM #est_rows AS er +OPTION (RECOMPILE); + +SELECT '#stored_proc_info' AS table_name, * +FROM #stored_proc_info AS spi +OPTION(RECOMPILE); + +SELECT '#conversion_info' AS table_name, * +FROM #conversion_info AS ci +OPTION ( RECOMPILE ); + +SELECT '#variable_info' AS table_name, * +FROM #variable_info AS vi +OPTION ( RECOMPILE ); + +SELECT '#missing_index_xml' AS table_name, * +FROM #missing_index_xml +OPTION ( RECOMPILE ); + +SELECT '#missing_index_schema' AS table_name, * +FROM #missing_index_schema +OPTION ( RECOMPILE ); + +SELECT '#missing_index_usage' AS table_name, * +FROM #missing_index_usage +OPTION ( RECOMPILE ); + +SELECT '#missing_index_detail' AS table_name, * +FROM #missing_index_detail +OPTION ( RECOMPILE ); + +SELECT '#missing_index_pretty' AS table_name, * +FROM #missing_index_pretty +OPTION ( RECOMPILE ); + +END; + +END TRY +BEGIN CATCH + RAISERROR (N'Failure returning debug temp tables', 0,1) WITH NOWAIT; + + IF @sql_select IS NOT NULL + BEGIN + SET @msg = N'Last @sql_select: ' + @sql_select; + RAISERROR(@msg, 0, 1) WITH NOWAIT; + END; + + SELECT @msg = @DatabaseName + N' database failed to process. ' + ERROR_MESSAGE(), @error_severity = ERROR_SEVERITY(), @error_state = ERROR_STATE(); + RAISERROR (@msg, @error_severity, @error_state) WITH NOWAIT; + + + WHILE @@TRANCOUNT > 0 + ROLLBACK; + + RETURN; +END CATCH; + +/* +Ways to run this thing + +--Debug +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Debug = 1 + +--Get the top 1 +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @Debug = 1 + +--Use a StartDate +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @StartDate = '20170527' + +--Use an EndDate +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @EndDate = '20170527' + +--Use Both +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @StartDate = '20170526', @EndDate = '20170527' + +--Set a minimum execution count +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @MinimumExecutionCount = 10 + +--Set a duration minimum +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @DurationFilter = 5 + +--Look for a stored procedure name (that doesn't exist!) +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @StoredProcName = 'blah' + +--Look for a stored procedure name that does (at least On My Computer®) +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @StoredProcName = 'UserReportExtended' + +--Look for failed queries +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @Top = 1, @Failed = 1 + +--Filter by plan_id +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @PlanIdFilter = 3356 + +--Filter by query_id +EXEC sp_BlitzQueryStore @DatabaseName = 'StackOverflow', @QueryIdFilter = 2958 + +*/ + +END; + +GO +IF OBJECT_ID('dbo.sp_BlitzWho') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_BlitzWho AS RETURN 0;') +GO + +ALTER PROCEDURE dbo.sp_BlitzWho + @Help TINYINT = 0 , + @ShowSleepingSPIDs TINYINT = 0, + @ExpertMode BIT = 0, + @Debug BIT = 0, + @OutputDatabaseName NVARCHAR(256) = NULL , + @OutputSchemaName NVARCHAR(256) = NULL , + @OutputTableName NVARCHAR(256) = NULL , + @OutputTableRetentionDays TINYINT = 3 , + @MinElapsedSeconds INT = 0 , + @MinCPUTime INT = 0 , + @MinLogicalReads INT = 0 , + @MinPhysicalReads INT = 0 , + @MinWrites INT = 0 , + @MinTempdbMB INT = 0 , + @MinRequestedMemoryKB INT = 0 , + @MinBlockingSeconds INT = 0 , + @CheckDateOverride DATETIMEOFFSET = NULL, + @ShowActualParameters BIT = 0, + @GetOuterCommand BIT = 0, + @GetLiveQueryPlan BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0, + @SortOrder NVARCHAR(256) = N'elapsed time' +AS +BEGIN + SET NOCOUNT ON; + SET STATISTICS XML OFF; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + SELECT @Version = '8.16', @VersionDate = '20230820'; + + IF(@VersionCheckMode = 1) + BEGIN + RETURN; + END; + + + + IF @Help = 1 + BEGIN + PRINT ' +sp_BlitzWho from http://FirstResponderKit.org + +This script gives you a snapshot of everything currently executing on your SQL Server. + +To learn more, visit http://FirstResponderKit.org where you can download new +versions for free, watch training videos on how it works, get more info on +the findings, contribute your own code, and more. + +Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. + - Outputting to table is only supported with SQL Server 2012 and higher. + - If @OutputDatabaseName and @OutputSchemaName are populated, the database and + schema must already exist. We will not create them, only the table. + +MIT License + +Copyright (c) Brent Ozar Unlimited + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +'; +RETURN; +END; /* @Help = 1 */ + +/* Get the major and minor build numbers */ +DECLARE @ProductVersion NVARCHAR(128) + ,@ProductVersionMajor DECIMAL(10,2) + ,@ProductVersionMinor DECIMAL(10,2) + ,@Platform NVARCHAR(8) /* Azure or NonAzure are acceptable */ = (SELECT CASE WHEN @@VERSION LIKE '%Azure%' THEN N'Azure' ELSE N'NonAzure' END AS [Platform]) + ,@EnhanceFlag BIT = 0 + ,@BlockingCheck NVARCHAR(MAX) + ,@StringToSelect NVARCHAR(MAX) + ,@StringToExecute NVARCHAR(MAX) + ,@OutputTableCleanupDate DATE + ,@SessionWaits BIT = 0 + ,@SessionWaitsSQL NVARCHAR(MAX) = + N'LEFT JOIN ( SELECT DISTINCT + wait.session_id , + ( SELECT TOP 5 waitwait.wait_type + N'' ('' + + CAST(MAX(waitwait.wait_time_ms) AS NVARCHAR(128)) + + N'' ms), '' + FROM sys.dm_exec_session_wait_stats AS waitwait + WHERE waitwait.session_id = wait.session_id + GROUP BY waitwait.wait_type + HAVING SUM(waitwait.wait_time_ms) > 5 + ORDER BY 1 + FOR + XML PATH('''') ) AS session_wait_info + FROM sys.dm_exec_session_wait_stats AS wait ) AS wt2 + ON s.session_id = wt2.session_id + LEFT JOIN sys.dm_exec_query_stats AS session_stats + ON r.sql_handle = session_stats.sql_handle + AND r.plan_handle = session_stats.plan_handle + AND r.statement_start_offset = session_stats.statement_start_offset + AND r.statement_end_offset = session_stats.statement_end_offset' + ,@ObjectFullName NVARCHAR(2000) + ,@OutputTableNameQueryStats_View NVARCHAR(256) + ,@LineFeed NVARCHAR(MAX) /* Had to set as MAX up from 10 as it was truncating the view creation*/; + +/* Let's get @SortOrder set to lower case here for comparisons later */ +SET @SortOrder = REPLACE(LOWER(@SortOrder), N' ', N'_'); + +SET @ProductVersion = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)); +SELECT @ProductVersionMajor = SUBSTRING(@ProductVersion, 1,CHARINDEX('.', @ProductVersion) + 1 ), + @ProductVersionMinor = PARSENAME(CONVERT(VARCHAR(32), @ProductVersion), 2) + +SELECT + @OutputTableNameQueryStats_View = QUOTENAME(@OutputTableName + '_Deltas'), + @OutputDatabaseName = QUOTENAME(@OutputDatabaseName), + @OutputSchemaName = QUOTENAME(@OutputSchemaName), + @OutputTableName = QUOTENAME(@OutputTableName), + @LineFeed = CHAR(13) + CHAR(10); + +IF @OutputDatabaseName IS NOT NULL AND @OutputSchemaName IS NOT NULL AND @OutputTableName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + SET @ExpertMode = 1; /* Force ExpertMode when we're logging to table */ + + /* Create the table if it doesn't exist */ + SET @StringToExecute = N'USE ' + + @OutputDatabaseName + + N'; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + N'.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + N''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + N'.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + N''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableName + N''') CREATE TABLE ' + + @OutputSchemaName + N'.' + + @OutputTableName + + N'('; + SET @StringToExecute = @StringToExecute + N' + ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128) NOT NULL, + CheckDate DATETIMEOFFSET NOT NULL, + [elapsed_time] [varchar](41) NULL, + [session_id] [smallint] NOT NULL, + [database_name] [nvarchar](128) NULL, + [query_text] [nvarchar](max) NULL, + [outer_command] NVARCHAR(4000) NULL, + [query_plan] [xml] NULL, + [live_query_plan] [xml] NULL, + [cached_parameter_info] [nvarchar](max) NULL, + [live_parameter_info] [nvarchar](max) NULL, + [query_cost] [float] NULL, + [status] [nvarchar](30) NOT NULL, + [wait_info] [nvarchar](max) NULL, + [wait_resource] [nvarchar](max) NULL, + [top_session_waits] [nvarchar](max) NULL, + [blocking_session_id] [smallint] NULL, + [open_transaction_count] [int] NULL, + [is_implicit_transaction] [int] NOT NULL, + [nt_domain] [nvarchar](128) NULL, + [host_name] [nvarchar](128) NULL, + [login_name] [nvarchar](128) NOT NULL, + [nt_user_name] [nvarchar](128) NULL, + [program_name] [nvarchar](128) NULL, + [fix_parameter_sniffing] [nvarchar](150) NULL, + [client_interface_name] [nvarchar](32) NULL, + [login_time] [datetime] NOT NULL, + [start_time] [datetime] NULL, + [request_time] [datetime] NULL, + [request_cpu_time] [int] NULL, + [request_logical_reads] [bigint] NULL, + [request_writes] [bigint] NULL, + [request_physical_reads] [bigint] NULL, + [session_cpu] [int] NOT NULL, + [session_logical_reads] [bigint] NOT NULL, + [session_physical_reads] [bigint] NOT NULL, + [session_writes] [bigint] NOT NULL, + [tempdb_allocations_mb] [decimal](38, 2) NULL, + [memory_usage] [int] NOT NULL, + [estimated_completion_time] [bigint] NULL, + [percent_complete] [real] NULL, + [deadlock_priority] [int] NULL, + [transaction_isolation_level] [varchar](33) NOT NULL, + [degree_of_parallelism] [smallint] NULL, + [last_dop] [bigint] NULL, + [min_dop] [bigint] NULL, + [max_dop] [bigint] NULL, + [last_grant_kb] [bigint] NULL, + [min_grant_kb] [bigint] NULL, + [max_grant_kb] [bigint] NULL, + [last_used_grant_kb] [bigint] NULL, + [min_used_grant_kb] [bigint] NULL, + [max_used_grant_kb] [bigint] NULL, + [last_ideal_grant_kb] [bigint] NULL, + [min_ideal_grant_kb] [bigint] NULL, + [max_ideal_grant_kb] [bigint] NULL, + [last_reserved_threads] [bigint] NULL, + [min_reserved_threads] [bigint] NULL, + [max_reserved_threads] [bigint] NULL, + [last_used_threads] [bigint] NULL, + [min_used_threads] [bigint] NULL, + [max_used_threads] [bigint] NULL, + [grant_time] [varchar](20) NULL, + [requested_memory_kb] [bigint] NULL, + [grant_memory_kb] [bigint] NULL, + [is_request_granted] [varchar](39) NOT NULL, + [required_memory_kb] [bigint] NULL, + [query_memory_grant_used_memory_kb] [bigint] NULL, + [ideal_memory_kb] [bigint] NULL, + [is_small] [bit] NULL, + [timeout_sec] [int] NULL, + [resource_semaphore_id] [smallint] NULL, + [wait_order] [varchar](20) NULL, + [wait_time_ms] [varchar](20) NULL, + [next_candidate_for_memory_grant] [varchar](3) NOT NULL, + [target_memory_kb] [bigint] NULL, + [max_target_memory_kb] [varchar](30) NULL, + [total_memory_kb] [bigint] NULL, + [available_memory_kb] [bigint] NULL, + [granted_memory_kb] [bigint] NULL, + [query_resource_semaphore_used_memory_kb] [bigint] NULL, + [grantee_count] [int] NULL, + [waiter_count] [int] NULL, + [timeout_error_count] [bigint] NULL, + [forced_grant_count] [varchar](30) NULL, + [workload_group_name] [sysname] NULL, + [resource_pool_name] [sysname] NULL, + [context_info] [varchar](128) NULL, + [query_hash] [binary](8) NULL, + [query_plan_hash] [binary](8) NULL, + [sql_handle] [varbinary] (64) NULL, + [plan_handle] [varbinary] (64) NULL, + [statement_start_offset] INT NULL, + [statement_end_offset] INT NULL, + JoinKey AS ServerName + CAST(CheckDate AS NVARCHAR(50)), + PRIMARY KEY CLUSTERED (ID ASC));'; + IF @Debug = 1 + BEGIN + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 0, 8000)) + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 8000, 16000)) + END + EXEC(@StringToExecute); + + /* If the table doesn't have the new JoinKey computed column, add it. See Github #2162. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''JoinKey'') + ALTER TABLE ' + @ObjectFullName + N' ADD JoinKey AS ServerName + CAST(CheckDate AS NVARCHAR(50));'; + EXEC(@StringToExecute); + + /* If the table doesn't have the new cached_parameter_info computed column, add it. See Github #2842. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''cached_parameter_info'') + ALTER TABLE ' + @ObjectFullName + N' ADD cached_parameter_info NVARCHAR(MAX) NULL;'; + EXEC(@StringToExecute); + + /* If the table doesn't have the new live_parameter_info computed column, add it. See Github #2842. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''live_parameter_info'') + ALTER TABLE ' + @ObjectFullName + N' ADD live_parameter_info NVARCHAR(MAX) NULL;'; + EXEC(@StringToExecute); + + /* If the table doesn't have the new outer_command column, add it. See Github #2887. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''outer_command'') + ALTER TABLE ' + @ObjectFullName + N' ADD outer_command NVARCHAR(4000) NULL;'; + EXEC(@StringToExecute); + + /* If the table doesn't have the new wait_resource column, add it. See Github #2970. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''wait_resource'') + ALTER TABLE ' + @ObjectFullName + N' ADD wait_resource NVARCHAR(MAX) NULL;'; + EXEC(@StringToExecute); + + /* Delete history older than @OutputTableRetentionDays */ + SET @OutputTableCleanupDate = CAST( (DATEADD(DAY, -1 * @OutputTableRetentionDays, GETDATE() ) ) AS DATE); + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + N'.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + N''') DELETE ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + N' WHERE ServerName = @SrvName AND CheckDate < @CheckDate;'; + IF @Debug = 1 + BEGIN + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 0, 8000)) + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 8000, 16000)) + END + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate date', + @@SERVERNAME, @OutputTableCleanupDate; + + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableNameQueryStats_View; + + /* Create the view */ + IF OBJECT_ID(@ObjectFullName) IS NULL + BEGIN + SET @StringToExecute = N'USE ' + + @OutputDatabaseName + + N'; EXEC (''CREATE VIEW ' + + @OutputSchemaName + '.' + + @OutputTableNameQueryStats_View + N' AS ' + @LineFeed + + N'WITH MaxQueryDuration AS ' + @LineFeed + + N'( ' + @LineFeed + + N' SELECT ' + @LineFeed + + N' MIN([ID]) AS [MinID], ' + @LineFeed + + N' MAX([ID]) AS [MaxID] ' + @LineFeed + + N' FROM ' + @OutputSchemaName + '.' + @OutputTableName + '' + @LineFeed + + N' GROUP BY [ServerName], ' + @LineFeed + + N' [session_id], ' + @LineFeed + + N' [database_name], ' + @LineFeed + + N' [request_time], ' + @LineFeed + + N' [start_time], ' + @LineFeed + + N' [sql_handle] ' + @LineFeed + + N') ' + @LineFeed + + N'SELECT ' + @LineFeed + + N' [ID], ' + @LineFeed + + N' [ServerName], ' + @LineFeed + + N' [CheckDate], ' + @LineFeed + + N' [elapsed_time], ' + @LineFeed + + N' [session_id], ' + @LineFeed + + N' [database_name], ' + @LineFeed + + N' [query_text_snippet], ' + @LineFeed + + N' [query_plan], ' + @LineFeed + + N' [live_query_plan], ' + @LineFeed + + N' [query_cost], ' + @LineFeed + + N' [status], ' + @LineFeed + + N' [wait_info], ' + @LineFeed + + N' [wait_resource], ' + @LineFeed + + N' [top_session_waits], ' + @LineFeed + + N' [blocking_session_id], ' + @LineFeed + + N' [open_transaction_count], ' + @LineFeed + + N' [is_implicit_transaction], ' + @LineFeed + + N' [nt_domain], ' + @LineFeed + + N' [host_name], ' + @LineFeed + + N' [login_name], ' + @LineFeed + + N' [nt_user_name], ' + @LineFeed + + N' [program_name], ' + @LineFeed + + N' [fix_parameter_sniffing], ' + @LineFeed + + N' [client_interface_name], ' + @LineFeed + + N' [login_time], ' + @LineFeed + + N' [start_time], ' + @LineFeed + + N' [request_time], ' + @LineFeed + + N' [request_cpu_time], ' + @LineFeed + + N' [degree_of_parallelism], ' + @LineFeed + + N' [request_logical_reads], ' + @LineFeed + + N' [Logical_Reads_MB], ' + @LineFeed + + N' [request_writes], ' + @LineFeed + + N' [Logical_Writes_MB], ' + @LineFeed + + N' [request_physical_reads], ' + @LineFeed + + N' [Physical_reads_MB], ' + @LineFeed + + N' [session_cpu], ' + @LineFeed + + N' [session_logical_reads], ' + @LineFeed + + N' [session_logical_reads_MB], ' + @LineFeed + + N' [session_physical_reads], ' + @LineFeed + + N' [session_physical_reads_MB], ' + @LineFeed + + N' [session_writes], ' + @LineFeed + + N' [session_writes_MB], ' + @LineFeed + + N' [tempdb_allocations_mb], ' + @LineFeed + + N' [memory_usage], ' + @LineFeed + + N' [estimated_completion_time], ' + @LineFeed + + N' [percent_complete], ' + @LineFeed + + N' [deadlock_priority], ' + @LineFeed + + N' [transaction_isolation_level], ' + @LineFeed + + N' [last_dop], ' + @LineFeed + + N' [min_dop], ' + @LineFeed + + N' [max_dop], ' + @LineFeed + + N' [last_grant_kb], ' + @LineFeed + + N' [min_grant_kb], ' + @LineFeed + + N' [max_grant_kb], ' + @LineFeed + + N' [last_used_grant_kb], ' + @LineFeed + + N' [min_used_grant_kb], ' + @LineFeed + + N' [max_used_grant_kb], ' + @LineFeed + + N' [last_ideal_grant_kb], ' + @LineFeed + + N' [min_ideal_grant_kb], ' + @LineFeed + + N' [max_ideal_grant_kb], ' + @LineFeed + + N' [last_reserved_threads], ' + @LineFeed + + N' [min_reserved_threads], ' + @LineFeed + + N' [max_reserved_threads], ' + @LineFeed + + N' [last_used_threads], ' + @LineFeed + + N' [min_used_threads], ' + @LineFeed + + N' [max_used_threads], ' + @LineFeed + + N' [grant_time], ' + @LineFeed + + N' [requested_memory_kb], ' + @LineFeed + + N' [grant_memory_kb], ' + @LineFeed + + N' [is_request_granted], ' + @LineFeed + + N' [required_memory_kb], ' + @LineFeed + + N' [query_memory_grant_used_memory_kb], ' + @LineFeed + + N' [ideal_memory_kb], ' + @LineFeed + + N' [is_small], ' + @LineFeed + + N' [timeout_sec], ' + @LineFeed + + N' [resource_semaphore_id], ' + @LineFeed + + N' [wait_order], ' + @LineFeed + + N' [wait_time_ms], ' + @LineFeed + + N' [next_candidate_for_memory_grant], ' + @LineFeed + + N' [target_memory_kb], ' + @LineFeed + + N' [max_target_memory_kb], ' + @LineFeed + + N' [total_memory_kb], ' + @LineFeed + + N' [available_memory_kb], ' + @LineFeed + + N' [granted_memory_kb], ' + @LineFeed + + N' [query_resource_semaphore_used_memory_kb], ' + @LineFeed + + N' [grantee_count], ' + @LineFeed + + N' [waiter_count], ' + @LineFeed + + N' [timeout_error_count], ' + @LineFeed + + N' [forced_grant_count], ' + @LineFeed + + N' [workload_group_name], ' + @LineFeed + + N' [resource_pool_name], ' + @LineFeed + + N' [context_info], ' + @LineFeed + + N' [query_hash], ' + @LineFeed + + N' [query_plan_hash], ' + @LineFeed + + N' [sql_handle], ' + @LineFeed + + N' [plan_handle], ' + @LineFeed + + N' [statement_start_offset], ' + @LineFeed + + N' [statement_end_offset] ' + @LineFeed + + N' FROM ' + @LineFeed + + N' ( ' + @LineFeed + + N' SELECT ' + @LineFeed + + N' [ID], ' + @LineFeed + + N' [ServerName], ' + @LineFeed + + N' [CheckDate], ' + @LineFeed + + N' [elapsed_time], ' + @LineFeed + + N' [session_id], ' + @LineFeed + + N' [database_name], ' + @LineFeed + + N' /* Truncate the query text to aid performance of painting the rows in SSMS */ ' + @LineFeed + + N' CAST([query_text] AS NVARCHAR(1000)) AS [query_text_snippet], ' + @LineFeed + + N' [query_plan], ' + @LineFeed + + N' [live_query_plan], ' + @LineFeed + + N' [query_cost], ' + @LineFeed + + N' [status], ' + @LineFeed + + N' [wait_info], ' + @LineFeed + + N' [wait_resource], ' + @LineFeed + + N' [top_session_waits], ' + @LineFeed + + N' [blocking_session_id], ' + @LineFeed + + N' [open_transaction_count], ' + @LineFeed + + N' [is_implicit_transaction], ' + @LineFeed + + N' [nt_domain], ' + @LineFeed + + N' [host_name], ' + @LineFeed + + N' [login_name], ' + @LineFeed + + N' [nt_user_name], ' + @LineFeed + + N' [program_name], ' + @LineFeed + + N' [fix_parameter_sniffing], ' + @LineFeed + + N' [client_interface_name], ' + @LineFeed + + N' [login_time], ' + @LineFeed + + N' [start_time], ' + @LineFeed + + N' [request_time], ' + @LineFeed + + N' [request_cpu_time], ' + @LineFeed + + N' [degree_of_parallelism], ' + @LineFeed + + N' [request_logical_reads], ' + @LineFeed + + N' ((CAST([request_logical_reads] AS DECIMAL(38,2))* 8)/ 1024) [Logical_Reads_MB], ' + @LineFeed + + N' [request_writes], ' + @LineFeed + + N' ((CAST([request_writes] AS DECIMAL(38,2))* 8)/ 1024) [Logical_Writes_MB], ' + @LineFeed + + N' [request_physical_reads], ' + @LineFeed + + N' ((CAST([request_physical_reads] AS DECIMAL(38,2))* 8)/ 1024) [Physical_reads_MB], ' + @LineFeed + + N' [session_cpu], ' + @LineFeed + + N' [session_logical_reads], ' + @LineFeed + + N' ((CAST([session_logical_reads] AS DECIMAL(38,2))* 8)/ 1024) [session_logical_reads_MB], ' + @LineFeed + + N' [session_physical_reads], ' + @LineFeed + + N' ((CAST([session_physical_reads] AS DECIMAL(38,2))* 8)/ 1024) [session_physical_reads_MB], ' + @LineFeed + + N' [session_writes], ' + @LineFeed + + N' ((CAST([session_writes] AS DECIMAL(38,2))* 8)/ 1024) [session_writes_MB], ' + @LineFeed + + N' [tempdb_allocations_mb], ' + @LineFeed + + N' [memory_usage], ' + @LineFeed + + N' [estimated_completion_time], ' + @LineFeed + + N' [percent_complete], ' + @LineFeed + + N' [deadlock_priority], ' + @LineFeed + + N' [transaction_isolation_level], ' + @LineFeed + + N' [last_dop], ' + @LineFeed + + N' [min_dop], ' + @LineFeed + + N' [max_dop], ' + @LineFeed + + N' [last_grant_kb], ' + @LineFeed + + N' [min_grant_kb], ' + @LineFeed + + N' [max_grant_kb], ' + @LineFeed + + N' [last_used_grant_kb], ' + @LineFeed + + N' [min_used_grant_kb], ' + @LineFeed + + N' [max_used_grant_kb], ' + @LineFeed + + N' [last_ideal_grant_kb], ' + @LineFeed + + N' [min_ideal_grant_kb], ' + @LineFeed + + N' [max_ideal_grant_kb], ' + @LineFeed + + N' [last_reserved_threads], ' + @LineFeed + + N' [min_reserved_threads], ' + @LineFeed + + N' [max_reserved_threads], ' + @LineFeed + + N' [last_used_threads], ' + @LineFeed + + N' [min_used_threads], ' + @LineFeed + + N' [max_used_threads], ' + @LineFeed + + N' [grant_time], ' + @LineFeed + + N' [requested_memory_kb], ' + @LineFeed + + N' [grant_memory_kb], ' + @LineFeed + + N' [is_request_granted], ' + @LineFeed + + N' [required_memory_kb], ' + @LineFeed + + N' [query_memory_grant_used_memory_kb], ' + @LineFeed + + N' [ideal_memory_kb], ' + @LineFeed + + N' [is_small], ' + @LineFeed + + N' [timeout_sec], ' + @LineFeed + + N' [resource_semaphore_id], ' + @LineFeed + + N' [wait_order], ' + @LineFeed + + N' [wait_time_ms], ' + @LineFeed + + N' [next_candidate_for_memory_grant], ' + @LineFeed + + N' [target_memory_kb], ' + @LineFeed + + N' [max_target_memory_kb], ' + @LineFeed + + N' [total_memory_kb], ' + @LineFeed + + N' [available_memory_kb], ' + @LineFeed + + N' [granted_memory_kb], ' + @LineFeed + + N' [query_resource_semaphore_used_memory_kb], ' + @LineFeed + + N' [grantee_count], ' + @LineFeed + + N' [waiter_count], ' + @LineFeed + + N' [timeout_error_count], ' + @LineFeed + + N' [forced_grant_count], ' + @LineFeed + + N' [workload_group_name], ' + @LineFeed + + N' [resource_pool_name], ' + @LineFeed + + N' [context_info], ' + @LineFeed + + N' [query_hash], ' + @LineFeed + + N' [query_plan_hash], ' + @LineFeed + + N' [sql_handle], ' + @LineFeed + + N' [plan_handle], ' + @LineFeed + + N' [statement_start_offset], ' + @LineFeed + + N' [statement_end_offset] ' + @LineFeed + + N' FROM ' + @OutputSchemaName + '.' + @OutputTableName + '' + @LineFeed + + N' ) AS [BlitzWho] ' + @LineFeed + + N'INNER JOIN [MaxQueryDuration] ON [BlitzWho].[ID] = [MaxQueryDuration].[MaxID]; ' + @LineFeed + + N''');' + + IF @Debug = 1 + BEGIN + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 0, 8000)) + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 8000, 16000)) + END + + EXEC(@StringToExecute); + END; + + END + + IF OBJECT_ID('tempdb..#WhoReadableDBs') IS NOT NULL + DROP TABLE #WhoReadableDBs; + +CREATE TABLE #WhoReadableDBs +( +database_id INT +); + +IF EXISTS (SELECT * FROM sys.all_objects o WHERE o.name = 'dm_hadr_database_replica_states') +BEGIN + RAISERROR('Checking for Read intent databases to exclude',0,0) WITH NOWAIT; + + EXEC('INSERT INTO #WhoReadableDBs (database_id) SELECT DBs.database_id FROM sys.databases DBs INNER JOIN sys.availability_replicas Replicas ON DBs.replica_id = Replicas.replica_id WHERE replica_server_name NOT IN (SELECT DISTINCT primary_replica FROM sys.dm_hadr_availability_group_states States) AND Replicas.secondary_role_allow_connections_desc = ''READ_ONLY'' AND replica_server_name = @@SERVERNAME;'); +END + +SELECT @BlockingCheck = N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + + SET LOCK_TIMEOUT 1000; /* To avoid blocking on live query plans. See Github issue #2907. */ + DECLARE @blocked TABLE + ( + dbid SMALLINT NOT NULL, + last_batch DATETIME NOT NULL, + open_tran SMALLINT NOT NULL, + sql_handle BINARY(20) NOT NULL, + session_id SMALLINT NOT NULL, + blocking_session_id SMALLINT NOT NULL, + lastwaittype NCHAR(32) NOT NULL, + waittime BIGINT NOT NULL, + cpu INT NOT NULL, + physical_io BIGINT NOT NULL, + memusage INT NOT NULL + ); + + INSERT @blocked ( dbid, last_batch, open_tran, sql_handle, session_id, blocking_session_id, lastwaittype, waittime, cpu, physical_io, memusage ) + SELECT + sys1.dbid, sys1.last_batch, sys1.open_tran, sys1.sql_handle, + sys2.spid AS session_id, sys2.blocked AS blocking_session_id, sys2.lastwaittype, sys2.waittime, sys2.cpu, sys2.physical_io, sys2.memusage + FROM sys.sysprocesses AS sys1 + JOIN sys.sysprocesses AS sys2 + ON sys1.spid = sys2.blocked; + '+CASE + WHEN (@GetOuterCommand = 1 AND (NOT EXISTS(SELECT 1 FROM sys.all_objects WHERE [name] = N'dm_exec_input_buffer'))) THEN N' + DECLARE @session_id SMALLINT; + DECLARE @Sessions TABLE + ( + session_id INT + ); + + DECLARE @inputbuffer TABLE + ( + ID INT IDENTITY(1,1), + session_id INT, + event_type NVARCHAR(30), + parameters SMALLINT, + event_info NVARCHAR(4000) + ); + + DECLARE inputbuffer_cursor + + CURSOR LOCAL FAST_FORWARD + FOR + SELECT session_id + FROM sys.dm_exec_sessions + WHERE session_id <> @@SPID + AND is_user_process = 1; + + OPEN inputbuffer_cursor; + + FETCH NEXT FROM inputbuffer_cursor INTO @session_id; + + WHILE (@@FETCH_STATUS = 0) + BEGIN; + BEGIN TRY; + + INSERT INTO @inputbuffer ([event_type],[parameters],[event_info]) + EXEC sp_executesql + N''DBCC INPUTBUFFER(@session_id) WITH NO_INFOMSGS;'', + N''@session_id SMALLINT'', + @session_id; + + UPDATE @inputbuffer + SET session_id = @session_id + WHERE ID = SCOPE_IDENTITY(); + + END TRY + BEGIN CATCH + RAISERROR(''DBCC inputbuffer failed for session %d'',0,0,@session_id) WITH NOWAIT; + END CATCH; + + FETCH NEXT FROM inputbuffer_cursor INTO @session_id + + END; + + CLOSE inputbuffer_cursor; + DEALLOCATE inputbuffer_cursor;' + ELSE N'' + END+ + N' + + DECLARE @LiveQueryPlans TABLE + ( + Session_Id INT NOT NULL, + Query_Plan XML NOT NULL + ); + + ' +IF EXISTS (SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('sys.dm_exec_query_statistics_xml') AND name = 'query_plan' AND @GetLiveQueryPlan=1) +BEGIN + SET @BlockingCheck = @BlockingCheck + N' + INSERT INTO @LiveQueryPlans + SELECT s.session_id, query_plan + FROM sys.dm_exec_sessions AS s + CROSS APPLY sys.dm_exec_query_statistics_xml(s.session_id) + WHERE s.session_id <> @@SPID;'; +END + + +IF @ProductVersionMajor > 9 and @ProductVersionMajor < 11 +BEGIN + /* Think of the StringToExecute as starting with this, but we'll set this up later depending on whether we're doing an insert or a select: + SELECT @StringToExecute = N'SELECT GETDATE() AS run_date , + */ + SET @StringToExecute = N' CASE WHEN YEAR(s.last_request_start_time) = 1900 THEN NULL ELSE COALESCE( RIGHT(''00'' + CONVERT(VARCHAR(20), (ABS(r.total_elapsed_time) / 1000) / 86400), 2) + '':'' + CONVERT(VARCHAR(20), (DATEADD(SECOND, (r.total_elapsed_time / 1000), 0) + DATEADD(MILLISECOND, (r.total_elapsed_time % 1000), 0)), 114), RIGHT(''00'' + CONVERT(VARCHAR(20), DATEDIFF(SECOND, s.last_request_start_time, GETDATE()) / 86400), 2) + '':'' + CONVERT(VARCHAR(20), DATEADD(SECOND, DATEDIFF(SECOND, s.last_request_start_time, GETDATE()), 0), 114) ) END AS [elapsed_time] , + s.session_id , + CASE WHEN r.blocking_session_id <> 0 AND blocked.session_id IS NULL + THEN r.blocking_session_id + WHEN r.blocking_session_id <> 0 AND s.session_id <> blocked.blocking_session_id + THEN blocked.blocking_session_id + WHEN r.blocking_session_id = 0 AND s.session_id = blocked.session_id + THEN blocked.blocking_session_id + WHEN r.blocking_session_id <> 0 AND s.session_id = blocked.blocking_session_id + THEN r.blocking_session_id + ELSE NULL + END AS blocking_session_id, + COALESCE(DB_NAME(r.database_id), DB_NAME(blocked.dbid), ''N/A'') AS database_name, + ISNULL(SUBSTRING(dest.text, + ( r.statement_start_offset / 2 ) + 1, + ( ( CASE r.statement_end_offset + WHEN -1 THEN DATALENGTH(dest.text) + ELSE r.statement_end_offset + END - r.statement_start_offset ) + / 2 ) + 1), dest.text) AS query_text , + '+CASE + WHEN @GetOuterCommand = 1 THEN N'CAST(event_info AS NVARCHAR(4000)) AS outer_command,' + ELSE N'' + END+N' + derp.query_plan , + qmg.query_cost , + s.status , + CASE + WHEN s.status <> ''sleeping'' THEN COALESCE(wt.wait_info, RTRIM(blocked.lastwaittype) + '' ('' + CONVERT(VARCHAR(10), blocked.waittime) + '')'' ) + ELSE NULL + END AS wait_info , + r.wait_resource , + COALESCE(r.open_transaction_count, blocked.open_tran) AS open_transaction_count , + CASE WHEN EXISTS ( SELECT 1 + FROM sys.dm_tran_active_transactions AS tat + JOIN sys.dm_tran_session_transactions AS tst + ON tst.transaction_id = tat.transaction_id + WHERE tat.name = ''implicit_transaction'' + AND s.session_id = tst.session_id + ) THEN 1 + ELSE 0 + END AS is_implicit_transaction , + s.nt_domain , + s.host_name , + s.login_name , + s.nt_user_name ,' + IF @Platform = 'NonAzure' + BEGIN + SET @StringToExecute += + N'program_name = COALESCE(( + SELECT REPLACE(program_name,Substring(program_name,30,34),''"''+j.name+''"'') + FROM msdb.dbo.sysjobs j WHERE Substring(program_name,32,32) = CONVERT(char(32),CAST(j.job_id AS binary(16)),2) + ),s.program_name)' + END + ELSE + BEGIN + SET @StringToExecute += N's.program_name' + END + + IF @ExpertMode = 1 + BEGIN + SET @StringToExecute += + N', + ''DBCC FREEPROCCACHE ('' + CONVERT(NVARCHAR(128), r.plan_handle, 1) + '');'' AS fix_parameter_sniffing, + s.client_interface_name , + s.login_time , + r.start_time , + qmg.request_time , + COALESCE(r.cpu_time, s.cpu_time) AS request_cpu_time, + COALESCE(r.logical_reads, s.logical_reads) AS request_logical_reads, + COALESCE(r.writes, s.writes) AS request_writes, + COALESCE(r.reads, s.reads) AS request_physical_reads , + s.cpu_time AS session_cpu, + s.logical_reads AS session_logical_reads, + s.reads AS session_physical_reads , + s.writes AS session_writes, + tempdb_allocations.tempdb_allocations_mb, + s.memory_usage , + r.estimated_completion_time , + r.percent_complete , + r.deadlock_priority , + CASE + WHEN s.transaction_isolation_level = 0 THEN ''Unspecified'' + WHEN s.transaction_isolation_level = 1 THEN ''Read Uncommitted'' + WHEN s.transaction_isolation_level = 2 AND EXISTS (SELECT 1 FROM sys.databases WHERE name = DB_NAME(r.database_id) AND is_read_committed_snapshot_on = 1) THEN ''Read Committed Snapshot Isolation'' + WHEN s.transaction_isolation_level = 2 THEN ''Read Committed'' + WHEN s.transaction_isolation_level = 3 THEN ''Repeatable Read'' + WHEN s.transaction_isolation_level = 4 THEN ''Serializable'' + WHEN s.transaction_isolation_level = 5 THEN ''Snapshot'' + ELSE ''WHAT HAVE YOU DONE?'' + END AS transaction_isolation_level , + qmg.dop AS degree_of_parallelism , + COALESCE(CAST(qmg.grant_time AS VARCHAR(20)), ''N/A'') AS grant_time , + qmg.requested_memory_kb , + qmg.granted_memory_kb AS grant_memory_kb, + CASE WHEN qmg.grant_time IS NULL THEN ''N/A'' + WHEN qmg.requested_memory_kb < qmg.granted_memory_kb + THEN ''Query Granted Less Than Query Requested'' + ELSE ''Memory Request Granted'' + END AS is_request_granted , + qmg.required_memory_kb , + qmg.used_memory_kb AS query_memory_grant_used_memory_kb, + qmg.ideal_memory_kb , + qmg.is_small , + qmg.timeout_sec , + qmg.resource_semaphore_id , + COALESCE(CAST(qmg.wait_order AS VARCHAR(20)), ''N/A'') AS wait_order , + COALESCE(CAST(qmg.wait_time_ms AS VARCHAR(20)), + ''N/A'') AS wait_time_ms , + CASE qmg.is_next_candidate + WHEN 0 THEN ''No'' + WHEN 1 THEN ''Yes'' + ELSE ''N/A'' + END AS next_candidate_for_memory_grant , + qrs.target_memory_kb , + COALESCE(CAST(qrs.max_target_memory_kb AS VARCHAR(20)), + ''Small Query Resource Semaphore'') AS max_target_memory_kb , + qrs.total_memory_kb , + qrs.available_memory_kb , + qrs.granted_memory_kb , + qrs.used_memory_kb AS query_resource_semaphore_used_memory_kb, + qrs.grantee_count , + qrs.waiter_count , + qrs.timeout_error_count , + COALESCE(CAST(qrs.forced_grant_count AS VARCHAR(20)), + ''Small Query Resource Semaphore'') AS forced_grant_count, + wg.name AS workload_group_name , + rp.name AS resource_pool_name, + CONVERT(VARCHAR(128), r.context_info) AS context_info + ' + END /* IF @ExpertMode = 1 */ + + SET @StringToExecute += + N'FROM sys.dm_exec_sessions AS s + '+ + CASE + WHEN @GetOuterCommand = 1 THEN CASE + WHEN EXISTS(SELECT 1 FROM sys.all_objects WHERE [name] = N'dm_exec_input_buffer') THEN N'OUTER APPLY sys.dm_exec_input_buffer (s.session_id, 0) AS ib' + ELSE N'LEFT JOIN @inputbuffer ib ON s.session_id = ib.session_id' + END + ELSE N'' + END+N' + LEFT JOIN sys.dm_exec_requests AS r + ON r.session_id = s.session_id + LEFT JOIN ( SELECT DISTINCT + wait.session_id , + ( SELECT waitwait.wait_type + N'' ('' + + CAST(MAX(waitwait.wait_duration_ms) AS NVARCHAR(128)) + + N'' ms) '' + FROM sys.dm_os_waiting_tasks AS waitwait + WHERE waitwait.session_id = wait.session_id + GROUP BY waitwait.wait_type + ORDER BY SUM(waitwait.wait_duration_ms) DESC + FOR + XML PATH('''') ) AS wait_info + FROM sys.dm_os_waiting_tasks AS wait ) AS wt + ON s.session_id = wt.session_id + LEFT JOIN sys.dm_exec_query_stats AS query_stats + ON r.sql_handle = query_stats.sql_handle + AND r.plan_handle = query_stats.plan_handle + AND r.statement_start_offset = query_stats.statement_start_offset + AND r.statement_end_offset = query_stats.statement_end_offset + LEFT JOIN sys.dm_exec_query_memory_grants qmg + ON r.session_id = qmg.session_id + AND r.request_id = qmg.request_id + LEFT JOIN sys.dm_exec_query_resource_semaphores qrs + ON qmg.resource_semaphore_id = qrs.resource_semaphore_id + AND qmg.pool_id = qrs.pool_id + LEFT JOIN sys.resource_governor_workload_groups wg + ON s.group_id = wg.group_id + LEFT JOIN sys.resource_governor_resource_pools rp + ON wg.pool_id = rp.pool_id + OUTER APPLY ( + SELECT TOP 1 + b.dbid, b.last_batch, b.open_tran, b.sql_handle, + b.session_id, b.blocking_session_id, b.lastwaittype, b.waittime + FROM @blocked b + WHERE (s.session_id = b.session_id + OR s.session_id = b.blocking_session_id) + ) AS blocked + OUTER APPLY sys.dm_exec_sql_text(COALESCE(r.sql_handle, blocked.sql_handle)) AS dest + OUTER APPLY sys.dm_exec_query_plan(r.plan_handle) AS derp + OUTER APPLY ( + SELECT CONVERT(DECIMAL(38,2), SUM( ((((tsu.user_objects_alloc_page_count - user_objects_dealloc_page_count) + (tsu.internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) * 8) / 1024.)) ) AS tempdb_allocations_mb + FROM sys.dm_db_task_space_usage tsu + WHERE tsu.request_id = r.request_id + AND tsu.session_id = r.session_id + AND tsu.session_id = s.session_id + ) as tempdb_allocations + WHERE s.session_id <> @@SPID + AND s.host_name IS NOT NULL + ' + + CASE WHEN @ShowSleepingSPIDs = 0 THEN + N' AND COALESCE(DB_NAME(r.database_id), DB_NAME(blocked.dbid)) IS NOT NULL' + WHEN @ShowSleepingSPIDs = 1 THEN + N' OR COALESCE(r.open_transaction_count, blocked.open_tran) >= 1' + ELSE N'' END; +END /* IF @ProductVersionMajor > 9 and @ProductVersionMajor < 11 */ + +IF @ProductVersionMajor >= 11 + BEGIN + SELECT @EnhanceFlag = + CASE WHEN @ProductVersionMajor = 11 AND @ProductVersionMinor >= 6020 THEN 1 + WHEN @ProductVersionMajor = 12 AND @ProductVersionMinor >= 5000 THEN 1 + WHEN @ProductVersionMajor = 13 AND @ProductVersionMinor >= 1601 THEN 1 + WHEN @ProductVersionMajor > 13 THEN 1 + ELSE 0 + END + + + IF OBJECT_ID('sys.dm_exec_session_wait_stats') IS NOT NULL + BEGIN + SET @SessionWaits = 1 + END + + /* Think of the StringToExecute as starting with this, but we'll set this up later depending on whether we're doing an insert or a select: + SELECT @StringToExecute = N'SELECT GETDATE() AS run_date , + */ + SELECT @StringToExecute = N' CASE WHEN YEAR(s.last_request_start_time) = 1900 THEN NULL ELSE COALESCE( RIGHT(''00'' + CONVERT(VARCHAR(20), (ABS(r.total_elapsed_time) / 1000) / 86400), 2) + '':'' + CONVERT(VARCHAR(20), (DATEADD(SECOND, (r.total_elapsed_time / 1000), 0) + DATEADD(MILLISECOND, (r.total_elapsed_time % 1000), 0)), 114), RIGHT(''00'' + CONVERT(VARCHAR(20), DATEDIFF(SECOND, s.last_request_start_time, GETDATE()) / 86400), 2) + '':'' + CONVERT(VARCHAR(20), DATEADD(SECOND, DATEDIFF(SECOND, s.last_request_start_time, GETDATE()), 0), 114) ) END AS [elapsed_time] , + s.session_id , + CASE WHEN r.blocking_session_id <> 0 AND blocked.session_id IS NULL + THEN r.blocking_session_id + WHEN r.blocking_session_id <> 0 AND s.session_id <> blocked.blocking_session_id + THEN blocked.blocking_session_id + WHEN r.blocking_session_id = 0 AND s.session_id = blocked.session_id + THEN blocked.blocking_session_id + WHEN r.blocking_session_id <> 0 AND s.session_id = blocked.blocking_session_id + THEN r.blocking_session_id + ELSE NULL + END AS blocking_session_id, + COALESCE(DB_NAME(r.database_id), DB_NAME(blocked.dbid), ''N/A'') AS database_name, + ISNULL(SUBSTRING(dest.text, + ( r.statement_start_offset / 2 ) + 1, + ( ( CASE r.statement_end_offset + WHEN -1 THEN DATALENGTH(dest.text) + ELSE r.statement_end_offset + END - r.statement_start_offset ) + / 2 ) + 1), dest.text) AS query_text , + '+CASE + WHEN @GetOuterCommand = 1 THEN N'CAST(event_info AS NVARCHAR(4000)) AS outer_command,' + ELSE N'' + END+N' + derp.query_plan , + CAST(COALESCE(qs_live.Query_Plan, ' + CASE WHEN @GetLiveQueryPlan=1 + THEN '''''' + ELSE '''''' + END + +') AS XML + + + ) AS live_query_plan , + STUFF((SELECT DISTINCT N'', '' + Node.Data.value(''(@Column)[1]'', ''NVARCHAR(4000)'') + N'' {'' + Node.Data.value(''(@ParameterDataType)[1]'', ''NVARCHAR(4000)'') + N''}: '' + Node.Data.value(''(@ParameterCompiledValue)[1]'', ''NVARCHAR(4000)'') + FROM derp.query_plan.nodes(''/*:ShowPlanXML/*:BatchSequence/*:Batch/*:Statements/*:StmtSimple/*:QueryPlan/*:ParameterList/*:ColumnReference'') AS Node(Data) + FOR XML PATH('''')), 1,2,'''') + AS Cached_Parameter_Info, + ' + IF @ShowActualParameters = 1 + BEGIN + SELECT @StringToExecute = @StringToExecute + N'qs_live.Live_Parameter_Info as Live_Parameter_Info,' + END + + SELECT @StringToExecute = @StringToExecute + N' + qmg.query_cost , + s.status , + CASE + WHEN s.status <> ''sleeping'' THEN COALESCE(wt.wait_info, RTRIM(blocked.lastwaittype) + '' ('' + CONVERT(VARCHAR(10), blocked.waittime) + '')'' ) + ELSE NULL + END AS wait_info , + r.wait_resource ,' + + + CASE @SessionWaits + WHEN 1 THEN + N'SUBSTRING(wt2.session_wait_info, 0, LEN(wt2.session_wait_info) ) AS top_session_waits ,' + ELSE N' NULL AS top_session_waits ,' + END + + + N'COALESCE(r.open_transaction_count, blocked.open_tran) AS open_transaction_count , + CASE WHEN EXISTS ( SELECT 1 + FROM sys.dm_tran_active_transactions AS tat + JOIN sys.dm_tran_session_transactions AS tst + ON tst.transaction_id = tat.transaction_id + WHERE tat.name = ''implicit_transaction'' + AND s.session_id = tst.session_id + ) THEN 1 + ELSE 0 + END AS is_implicit_transaction , + s.nt_domain , + s.host_name , + s.login_name , + s.nt_user_name ,' + IF @Platform = 'NonAzure' + BEGIN + SET @StringToExecute += + N'program_name = COALESCE(( + SELECT REPLACE(program_name,Substring(program_name,30,34),''"''+j.name+''"'') + FROM msdb.dbo.sysjobs j WHERE Substring(program_name,32,32) = CONVERT(char(32),CAST(j.job_id AS binary(16)),2) + ),s.program_name)' + END + ELSE + BEGIN + SET @StringToExecute += N's.program_name' + END + + IF @ExpertMode = 1 /* We show more columns in expert mode, so the SELECT gets longer */ + BEGIN + SET @StringToExecute += + N', ''DBCC FREEPROCCACHE ('' + CONVERT(NVARCHAR(128), r.plan_handle, 1) + '');'' AS fix_parameter_sniffing, + s.client_interface_name , + s.login_time , + r.start_time , + qmg.request_time , + COALESCE(r.cpu_time, s.cpu_time) AS request_cpu_time, + COALESCE(r.logical_reads, s.logical_reads) AS request_logical_reads, + COALESCE(r.writes, s.writes) AS request_writes, + COALESCE(r.reads, s.reads) AS request_physical_reads , + s.cpu_time AS session_cpu, + s.logical_reads AS session_logical_reads, + s.reads AS session_physical_reads , + s.writes AS session_writes, + tempdb_allocations.tempdb_allocations_mb, + s.memory_usage , + r.estimated_completion_time , + r.percent_complete , + r.deadlock_priority , + CASE + WHEN s.transaction_isolation_level = 0 THEN ''Unspecified'' + WHEN s.transaction_isolation_level = 1 THEN ''Read Uncommitted'' + WHEN s.transaction_isolation_level = 2 AND EXISTS (SELECT 1 FROM sys.databases WHERE name = DB_NAME(r.database_id) AND is_read_committed_snapshot_on = 1) THEN ''Read Committed Snapshot Isolation'' + WHEN s.transaction_isolation_level = 2 THEN ''Read Committed'' + WHEN s.transaction_isolation_level = 3 THEN ''Repeatable Read'' + WHEN s.transaction_isolation_level = 4 THEN ''Serializable'' + WHEN s.transaction_isolation_level = 5 THEN ''Snapshot'' + ELSE ''WHAT HAVE YOU DONE?'' + END AS transaction_isolation_level , + qmg.dop AS degree_of_parallelism , ' + + + CASE @EnhanceFlag + WHEN 1 THEN N'query_stats.last_dop, + query_stats.min_dop, + query_stats.max_dop, + query_stats.last_grant_kb, + query_stats.min_grant_kb, + query_stats.max_grant_kb, + query_stats.last_used_grant_kb, + query_stats.min_used_grant_kb, + query_stats.max_used_grant_kb, + query_stats.last_ideal_grant_kb, + query_stats.min_ideal_grant_kb, + query_stats.max_ideal_grant_kb, + query_stats.last_reserved_threads, + query_stats.min_reserved_threads, + query_stats.max_reserved_threads, + query_stats.last_used_threads, + query_stats.min_used_threads, + query_stats.max_used_threads,' + ELSE N' NULL AS last_dop, + NULL AS min_dop, + NULL AS max_dop, + NULL AS last_grant_kb, + NULL AS min_grant_kb, + NULL AS max_grant_kb, + NULL AS last_used_grant_kb, + NULL AS min_used_grant_kb, + NULL AS max_used_grant_kb, + NULL AS last_ideal_grant_kb, + NULL AS min_ideal_grant_kb, + NULL AS max_ideal_grant_kb, + NULL AS last_reserved_threads, + NULL AS min_reserved_threads, + NULL AS max_reserved_threads, + NULL AS last_used_threads, + NULL AS min_used_threads, + NULL AS max_used_threads,' + END + + SET @StringToExecute += + N' + COALESCE(CAST(qmg.grant_time AS VARCHAR(20)), ''Memory Not Granted'') AS grant_time , + qmg.requested_memory_kb , + qmg.granted_memory_kb AS grant_memory_kb, + CASE WHEN qmg.grant_time IS NULL THEN ''N/A'' + WHEN qmg.requested_memory_kb < qmg.granted_memory_kb + THEN ''Query Granted Less Than Query Requested'' + ELSE ''Memory Request Granted'' + END AS is_request_granted , + qmg.required_memory_kb , + qmg.used_memory_kb AS query_memory_grant_used_memory_kb, + qmg.ideal_memory_kb , + qmg.is_small , + qmg.timeout_sec , + qmg.resource_semaphore_id , + COALESCE(CAST(qmg.wait_order AS VARCHAR(20)), ''N/A'') AS wait_order , + COALESCE(CAST(qmg.wait_time_ms AS VARCHAR(20)), + ''N/A'') AS wait_time_ms , + CASE qmg.is_next_candidate + WHEN 0 THEN ''No'' + WHEN 1 THEN ''Yes'' + ELSE ''N/A'' + END AS next_candidate_for_memory_grant , + qrs.target_memory_kb , + COALESCE(CAST(qrs.max_target_memory_kb AS VARCHAR(20)), + ''Small Query Resource Semaphore'') AS max_target_memory_kb , + qrs.total_memory_kb , + qrs.available_memory_kb , + qrs.granted_memory_kb , + qrs.used_memory_kb AS query_resource_semaphore_used_memory_kb, + qrs.grantee_count , + qrs.waiter_count , + qrs.timeout_error_count , + COALESCE(CAST(qrs.forced_grant_count AS VARCHAR(20)), + ''Small Query Resource Semaphore'') AS forced_grant_count, + wg.name AS workload_group_name, + rp.name AS resource_pool_name, + CONVERT(VARCHAR(128), r.context_info) AS context_info, + r.query_hash, r.query_plan_hash, r.sql_handle, r.plan_handle, r.statement_start_offset, r.statement_end_offset ' + END /* IF @ExpertMode = 1 */ + + SET @StringToExecute += + N' FROM sys.dm_exec_sessions AS s'+ + CASE + WHEN @GetOuterCommand = 1 THEN CASE + WHEN EXISTS(SELECT 1 FROM sys.all_objects WHERE [name] = N'dm_exec_input_buffer') THEN N' + OUTER APPLY sys.dm_exec_input_buffer (s.session_id, 0) AS ib' + ELSE N' + LEFT JOIN @inputbuffer ib ON s.session_id = ib.session_id' + END + ELSE N'' + END+N' + LEFT JOIN sys.dm_exec_requests AS r + ON r.session_id = s.session_id + LEFT JOIN ( SELECT DISTINCT + wait.session_id , + ( SELECT waitwait.wait_type + N'' ('' + + CAST(MAX(waitwait.wait_duration_ms) AS NVARCHAR(128)) + + N'' ms) '' + FROM sys.dm_os_waiting_tasks AS waitwait + WHERE waitwait.session_id = wait.session_id + GROUP BY waitwait.wait_type + ORDER BY SUM(waitwait.wait_duration_ms) DESC + FOR + XML PATH('''') ) AS wait_info + FROM sys.dm_os_waiting_tasks AS wait ) AS wt + ON s.session_id = wt.session_id + LEFT JOIN sys.dm_exec_query_stats AS query_stats + ON r.sql_handle = query_stats.sql_handle + AND r.plan_handle = query_stats.plan_handle + AND r.statement_start_offset = query_stats.statement_start_offset + AND r.statement_end_offset = query_stats.statement_end_offset + ' + + + CASE @SessionWaits + WHEN 1 THEN @SessionWaitsSQL + ELSE N'' + END + + + N' + LEFT JOIN sys.dm_exec_query_memory_grants qmg + ON r.session_id = qmg.session_id + AND r.request_id = qmg.request_id + LEFT JOIN sys.dm_exec_query_resource_semaphores qrs + ON qmg.resource_semaphore_id = qrs.resource_semaphore_id + AND qmg.pool_id = qrs.pool_id + LEFT JOIN sys.resource_governor_workload_groups wg + ON s.group_id = wg.group_id + LEFT JOIN sys.resource_governor_resource_pools rp + ON wg.pool_id = rp.pool_id + OUTER APPLY ( + SELECT TOP 1 + b.dbid, b.last_batch, b.open_tran, b.sql_handle, + b.session_id, b.blocking_session_id, b.lastwaittype, b.waittime + FROM @blocked b + WHERE (s.session_id = b.session_id + OR s.session_id = b.blocking_session_id) + ) AS blocked + OUTER APPLY sys.dm_exec_sql_text(COALESCE(r.sql_handle, blocked.sql_handle)) AS dest + OUTER APPLY sys.dm_exec_query_plan(r.plan_handle) AS derp + OUTER APPLY ( + SELECT CONVERT(DECIMAL(38,2), SUM( ((((tsu.user_objects_alloc_page_count - user_objects_dealloc_page_count) + (tsu.internal_objects_alloc_page_count - internal_objects_dealloc_page_count)) * 8) / 1024.)) ) AS tempdb_allocations_mb + FROM sys.dm_db_task_space_usage tsu + WHERE tsu.request_id = r.request_id + AND tsu.session_id = r.session_id + AND tsu.session_id = s.session_id + ) as tempdb_allocations + + OUTER APPLY ( + SELECT TOP 1 Query_Plan, + STUFF((SELECT DISTINCT N'', '' + Node.Data.value(''(@Column)[1]'', ''NVARCHAR(4000)'') + N'' {'' + Node.Data.value(''(@ParameterDataType)[1]'', ''NVARCHAR(4000)'') + N''}: '' + Node.Data.value(''(@ParameterCompiledValue)[1]'', ''NVARCHAR(4000)'') + N'' (Actual: '' + Node.Data.value(''(@ParameterRuntimeValue)[1]'', ''NVARCHAR(4000)'') + N'')'' + FROM q.Query_Plan.nodes(''/*:ShowPlanXML/*:BatchSequence/*:Batch/*:Statements/*:StmtSimple/*:QueryPlan/*:ParameterList/*:ColumnReference'') AS Node(Data) + FOR XML PATH('''')), 1,2,'''') + AS Live_Parameter_Info + FROM @LiveQueryPlans q + WHERE (s.session_id = q.Session_Id) + + ) AS qs_live + + WHERE s.session_id <> @@SPID + AND s.host_name IS NOT NULL + AND r.database_id NOT IN (SELECT database_id FROM #WhoReadableDBs) + ' + + CASE WHEN @ShowSleepingSPIDs = 0 THEN + N' AND COALESCE(DB_NAME(r.database_id), DB_NAME(blocked.dbid)) IS NOT NULL' + WHEN @ShowSleepingSPIDs = 1 THEN + N' OR COALESCE(r.open_transaction_count, blocked.open_tran) >= 1' + ELSE N'' END; + + +END /* IF @ProductVersionMajor >= 11 */ + +IF (@MinElapsedSeconds + @MinCPUTime + @MinLogicalReads + @MinPhysicalReads + @MinWrites + @MinTempdbMB + @MinRequestedMemoryKB + @MinBlockingSeconds) > 0 + BEGIN + /* They're filtering for something, so set up a where clause that will let any (not all combined) of the min triggers work: */ + SET @StringToExecute += N' AND (1 = 0 '; + IF @MinElapsedSeconds > 0 + SET @StringToExecute += N' OR ABS(COALESCE(r.total_elapsed_time,0)) / 1000 >= ' + CAST(@MinElapsedSeconds AS NVARCHAR(20)); + IF @MinCPUTime > 0 + SET @StringToExecute += N' OR COALESCE(r.cpu_time, s.cpu_time,0) / 1000 >= ' + CAST(@MinCPUTime AS NVARCHAR(20)); + IF @MinLogicalReads > 0 + SET @StringToExecute += N' OR COALESCE(r.logical_reads, s.logical_reads,0) >= ' + CAST(@MinLogicalReads AS NVARCHAR(20)); + IF @MinPhysicalReads > 0 + SET @StringToExecute += N' OR COALESCE(s.reads,0) >= ' + CAST(@MinPhysicalReads AS NVARCHAR(20)); + IF @MinWrites > 0 + SET @StringToExecute += N' OR COALESCE(r.writes, s.writes,0) >= ' + CAST(@MinWrites AS NVARCHAR(20)); + IF @MinTempdbMB > 0 + SET @StringToExecute += N' OR COALESCE(tempdb_allocations.tempdb_allocations_mb,0) >= ' + CAST(@MinTempdbMB AS NVARCHAR(20)); + IF @MinRequestedMemoryKB > 0 + SET @StringToExecute += N' OR COALESCE(qmg.requested_memory_kb,0) >= ' + CAST(@MinRequestedMemoryKB AS NVARCHAR(20)); + /* Blocking is a little different - we're going to return ALL of the queries if we meet the blocking threshold. */ + IF @MinBlockingSeconds > 0 + SET @StringToExecute += N' OR (SELECT SUM(waittime / 1000) FROM @blocked) >= ' + CAST(@MinBlockingSeconds AS NVARCHAR(20)); + SET @StringToExecute += N' ) '; + END + +SET @StringToExecute += + N' ORDER BY ' + CASE WHEN @SortOrder = 'session_id' THEN '[session_id] DESC' + WHEN @SortOrder = 'query_cost' THEN '[query_cost] DESC' + WHEN @SortOrder = 'database_name' THEN '[database_name] ASC' + WHEN @SortOrder = 'open_transaction_count' THEN '[open_transaction_count] DESC' + WHEN @SortOrder = 'is_implicit_transaction' THEN '[is_implicit_transaction] DESC' + WHEN @SortOrder = 'login_name' THEN '[login_name] ASC' + WHEN @SortOrder = 'program_name' THEN '[program_name] ASC' + WHEN @SortOrder = 'client_interface_name' THEN '[client_interface_name] ASC' + WHEN @SortOrder = 'request_cpu_time' THEN 'COALESCE(r.cpu_time, s.cpu_time) DESC' + WHEN @SortOrder = 'request_logical_reads' THEN 'COALESCE(r.logical_reads, s.logical_reads) DESC' + WHEN @SortOrder = 'request_writes' THEN 'COALESCE(r.writes, s.writes) DESC' + WHEN @SortOrder = 'request_physical_reads' THEN 'COALESCE(r.reads, s.reads) DESC ' + WHEN @SortOrder = 'session_cpu' THEN 's.cpu_time DESC' + WHEN @SortOrder = 'session_logical_reads' THEN 's.logical_reads DESC' + WHEN @SortOrder = 'session_physical_reads' THEN 's.reads DESC' + WHEN @SortOrder = 'session_writes' THEN 's.writes DESC' + WHEN @SortOrder = 'tempdb_allocations_mb' THEN '[tempdb_allocations_mb] DESC' + WHEN @SortOrder = 'memory_usage' THEN '[memory_usage] DESC' + WHEN @SortOrder = 'deadlock_priority' THEN 'r.deadlock_priority DESC' + WHEN @SortOrder = 'transaction_isolation_level' THEN 'r.[transaction_isolation_level] DESC' + WHEN @SortOrder = 'requested_memory_kb' THEN '[requested_memory_kb] DESC' + WHEN @SortOrder = 'grant_memory_kb' THEN 'qmg.granted_memory_kb DESC' + WHEN @SortOrder = 'grant' THEN 'qmg.granted_memory_kb DESC' + WHEN @SortOrder = 'query_memory_grant_used_memory_kb' THEN 'qmg.used_memory_kb DESC' + WHEN @SortOrder = 'ideal_memory_kb' THEN '[ideal_memory_kb] DESC' + WHEN @SortOrder = 'workload_group_name' THEN 'wg.name ASC' + WHEN @SortOrder = 'resource_pool_name' THEN 'rp.name ASC' + ELSE '[elapsed_time] DESC' + END + ' + '; + + +IF @OutputDatabaseName IS NOT NULL AND @OutputSchemaName IS NOT NULL AND @OutputTableName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + SET @StringToExecute = N'USE ' + + @OutputDatabaseName + N'; ' + + @BlockingCheck + + + ' INSERT INTO ' + + @OutputSchemaName + N'.' + + @OutputTableName + + N'(ServerName + ,CheckDate + ,[elapsed_time] + ,[session_id] + ,[blocking_session_id] + ,[database_name] + ,[query_text]' + + CASE WHEN @GetOuterCommand = 1 THEN N',[outer_command]' ELSE N'' END + N' + ,[query_plan]' + + CASE WHEN @ProductVersionMajor >= 11 THEN N',[live_query_plan]' ELSE N'' END + + CASE WHEN @ProductVersionMajor >= 11 THEN N',[cached_parameter_info]' ELSE N'' END + + CASE WHEN @ProductVersionMajor >= 11 AND @ShowActualParameters = 1 THEN N',[Live_Parameter_Info]' ELSE N'' END + N' + ,[query_cost] + ,[status] + ,[wait_info] + ,[wait_resource]' + + CASE WHEN @ProductVersionMajor >= 11 THEN N',[top_session_waits]' ELSE N'' END + N' + ,[open_transaction_count] + ,[is_implicit_transaction] + ,[nt_domain] + ,[host_name] + ,[login_name] + ,[nt_user_name] + ,[program_name] + ,[fix_parameter_sniffing] + ,[client_interface_name] + ,[login_time] + ,[start_time] + ,[request_time] + ,[request_cpu_time] + ,[request_logical_reads] + ,[request_writes] + ,[request_physical_reads] + ,[session_cpu] + ,[session_logical_reads] + ,[session_physical_reads] + ,[session_writes] + ,[tempdb_allocations_mb] + ,[memory_usage] + ,[estimated_completion_time] + ,[percent_complete] + ,[deadlock_priority] + ,[transaction_isolation_level] + ,[degree_of_parallelism]' + + CASE WHEN @ProductVersionMajor >= 11 THEN N' + ,[last_dop] + ,[min_dop] + ,[max_dop] + ,[last_grant_kb] + ,[min_grant_kb] + ,[max_grant_kb] + ,[last_used_grant_kb] + ,[min_used_grant_kb] + ,[max_used_grant_kb] + ,[last_ideal_grant_kb] + ,[min_ideal_grant_kb] + ,[max_ideal_grant_kb] + ,[last_reserved_threads] + ,[min_reserved_threads] + ,[max_reserved_threads] + ,[last_used_threads] + ,[min_used_threads] + ,[max_used_threads]' ELSE N'' END + N' + ,[grant_time] + ,[requested_memory_kb] + ,[grant_memory_kb] + ,[is_request_granted] + ,[required_memory_kb] + ,[query_memory_grant_used_memory_kb] + ,[ideal_memory_kb] + ,[is_small] + ,[timeout_sec] + ,[resource_semaphore_id] + ,[wait_order] + ,[wait_time_ms] + ,[next_candidate_for_memory_grant] + ,[target_memory_kb] + ,[max_target_memory_kb] + ,[total_memory_kb] + ,[available_memory_kb] + ,[granted_memory_kb] + ,[query_resource_semaphore_used_memory_kb] + ,[grantee_count] + ,[waiter_count] + ,[timeout_error_count] + ,[forced_grant_count] + ,[workload_group_name] + ,[resource_pool_name] + ,[context_info]' + + CASE WHEN @ProductVersionMajor >= 11 THEN N' + ,[query_hash] + ,[query_plan_hash] + ,[sql_handle] + ,[plan_handle] + ,[statement_start_offset] + ,[statement_end_offset]' ELSE N'' END + N' +) + SELECT @@SERVERNAME, COALESCE(@CheckDateOverride, SYSDATETIMEOFFSET()) AS CheckDate , ' + + @StringToExecute; + END +ELSE + SET @StringToExecute = @BlockingCheck + N' SELECT GETDATE() AS run_date , ' + @StringToExecute; + +/* If the server has > 50GB of memory, add a max grant hint to avoid getting a giant grant */ +IF (@ProductVersionMajor = 11 AND @ProductVersionMinor >= 6020) + OR (@ProductVersionMajor = 12 AND @ProductVersionMinor >= 5000 ) + OR (@ProductVersionMajor >= 13 ) + AND 50000000 < (SELECT cntr_value + FROM sys.dm_os_performance_counters + WHERE object_name LIKE '%:Memory Manager%' + AND counter_name LIKE 'Target Server Memory (KB)%') + BEGIN + SET @StringToExecute = @StringToExecute + N' OPTION (MAX_GRANT_PERCENT = 1, RECOMPILE) '; + END +ELSE + BEGIN + SET @StringToExecute = @StringToExecute + N' OPTION (RECOMPILE) '; + END + +/* Be good: */ +SET @StringToExecute = @StringToExecute + N' ; '; + + +IF @Debug = 1 + BEGIN + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 0, 8000)) + PRINT CONVERT(VARCHAR(8000), SUBSTRING(@StringToExecute, 8000, 16000)) + END + +EXEC sp_executesql @StringToExecute, + N'@CheckDateOverride DATETIMEOFFSET', + @CheckDateOverride; + +END +GO +IF OBJECT_ID('dbo.sp_DatabaseRestore') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_DatabaseRestore AS RETURN 0;'); +GO +ALTER PROCEDURE [dbo].[sp_DatabaseRestore] + @Database NVARCHAR(128) = NULL, + @RestoreDatabaseName NVARCHAR(128) = NULL, + @BackupPathFull NVARCHAR(260) = NULL, + @BackupPathDiff NVARCHAR(260) = NULL, + @BackupPathLog NVARCHAR(260) = NULL, + @MoveFiles BIT = 1, + @MoveDataDrive NVARCHAR(260) = NULL, + @MoveLogDrive NVARCHAR(260) = NULL, + @MoveFilestreamDrive NVARCHAR(260) = NULL, + @MoveFullTextCatalogDrive NVARCHAR(260) = NULL, + @BufferCount INT = NULL, + @MaxTransferSize INT = NULL, + @BlockSize INT = NULL, + @TestRestore BIT = 0, + @RunCheckDB BIT = 0, + @RestoreDiff BIT = 0, + @ContinueLogs BIT = 0, + @StandbyMode BIT = 0, + @StandbyUndoPath NVARCHAR(MAX) = NULL, + @RunRecovery BIT = 0, + @ForceSimpleRecovery BIT = 0, + @ExistingDBAction tinyint = 0, + @StopAt NVARCHAR(14) = NULL, + @OnlyLogsAfter NVARCHAR(14) = NULL, + @SimpleFolderEnumeration BIT = 0, + @SkipBackupsAlreadyInMsdb BIT = 0, + @DatabaseOwner sysname = NULL, + @SetTrustworthyON BIT = 0, + @FixOrphanUsers BIT = 0, + @KeepCdc BIT = 0, + @Execute CHAR(1) = Y, + @FileExtensionDiff NVARCHAR(128) = NULL, + @Debug INT = 0, + @Help BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +AS +SET NOCOUNT ON; +SET STATISTICS XML OFF; + +/*Versioning details*/ + +SELECT @Version = '8.16', @VersionDate = '20230820'; + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + + +IF @Help = 1 +BEGIN + PRINT ' + /* + sp_DatabaseRestore from http://FirstResponderKit.org + + This script will restore a database from a given file path. + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. + - Tastes awful with marmite. + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */ + '; + + PRINT ' + /* + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''D:\Backup\SQL2016PROD1A\LogShipMe\FULL\'', + @BackupPathLog = ''D:\Backup\SQL2016PROD1A\LogShipMe\LOG\'', + @ContinueLogs = 0, + @RunRecovery = 0; + + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''D:\Backup\SQL2016PROD1A\LogShipMe\FULL\'', + @BackupPathLog = ''D:\Backup\SQL2016PROD1A\LogShipMe\LOG\'', + @ContinueLogs = 1, + @RunRecovery = 0; + + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''D:\Backup\SQL2016PROD1A\LogShipMe\FULL\'', + @BackupPathLog = ''D:\Backup\SQL2016PROD1A\LogShipMe\LOG\'', + @ContinueLogs = 1, + @RunRecovery = 1; + + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''D:\Backup\SQL2016PROD1A\LogShipMe\FULL\'', + @BackupPathLog = ''D:\Backup\SQL2016PROD1A\LogShipMe\LOG\'', + @ContinueLogs = 0, + @RunRecovery = 1; + + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''D:\Backup\SQL2016PROD1A\LogShipMe\FULL\'', + @BackupPathDiff = ''D:\Backup\SQL2016PROD1A\LogShipMe\DIFF\'', + @BackupPathLog = ''D:\Backup\SQL2016PROD1A\LogShipMe\LOG\'', + @RestoreDiff = 1, + @ContinueLogs = 0, + @RunRecovery = 1; + + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''\\StorageServer\LogShipMe\FULL\'', + @BackupPathDiff = ''\\StorageServer\LogShipMe\DIFF\'', + @BackupPathLog = ''\\StorageServer\LogShipMe\LOG\'', + @RestoreDiff = 1, + @ContinueLogs = 0, + @RunRecovery = 1, + @TestRestore = 1, + @RunCheckDB = 1, + @Debug = 0; + + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''\\StorageServer\LogShipMe\FULL\'', + @BackupPathLog = ''\\StorageServer\LogShipMe\LOG\'', + @StandbyMode = 1, + @StandbyUndoPath = ''D:\Data\'', + @ContinueLogs = 1, + @RunRecovery = 0, + @Debug = 0; + + --Restore just through the latest DIFF, ignoring logs, and using a custom ".dif" file extension + EXEC dbo.sp_DatabaseRestore + @Database = ''LogShipMe'', + @BackupPathFull = ''D:\Backup\SQL2016PROD1A\LogShipMe\FULL\'', + @BackupPathDiff = ''D:\Backup\SQL2016PROD1A\LogShipMe\DIFF\'', + @RestoreDiff = 1, + @FileExtensionDiff = ''dif'', + @ContinueLogs = 0, + @RunRecovery = 1; + + -- Restore from stripped backup set when multiple paths are used. This example will restore stripped full backup set along with stripped transactional logs set from multiple backup paths + EXEC dbo.sp_DatabaseRestore + @Database = ''DBA'', + @BackupPathFull = ''D:\Backup1\DBA\FULL,D:\Backup2\DBA\FULL'', + @BackupPathLog = ''D:\Backup1\DBA\LOG,D:\Backup2\DBA\LOG'', + @StandbyMode = 0, + @ContinueLogs = 1, + @RunRecovery = 0, + @Debug = 0; + + --This example will restore the latest differential backup, and stop transaction logs at the specified date time. It will execute and print debug information. + EXEC dbo.sp_DatabaseRestore + @Database = ''DBA'', + @BackupPathFull = ''\\StorageServer\LogShipMe\FULL\'', + @BackupPathDiff = ''\\StorageServer\LogShipMe\DIFF\'', + @BackupPathLog = ''\\StorageServer\LogShipMe\LOG\'', + @RestoreDiff = 1, + @ContinueLogs = 0, + @RunRecovery = 1, + @StopAt = ''20170508201501'', + @Debug = 1; + + --This example will NOT execute the restore. Commands will be printed in a copy/paste ready format only + EXEC dbo.sp_DatabaseRestore + @Database = ''DBA'', + @BackupPathFull = ''\\StorageServer\LogShipMe\FULL\'', + @BackupPathDiff = ''\\StorageServer\LogShipMe\DIFF\'', + @BackupPathLog = ''\\StorageServer\LogShipMe\LOG\'', + @RestoreDiff = 1, + @ContinueLogs = 0, + @RunRecovery = 1, + @TestRestore = 1, + @RunCheckDB = 1, + @Debug = 0, + @Execute = ''N''; + '; + + RETURN; +END; + +-- Get the SQL Server version number because the columns returned by RESTORE commands vary by version +-- Based on: https://www.brentozar.com/archive/2015/05/sql-server-version-detection/ +-- Need to capture BuildVersion because RESTORE HEADERONLY changed with 2014 CU1, not RTM +DECLARE @ProductVersion AS NVARCHAR(20) = CAST(SERVERPROPERTY ('productversion') AS NVARCHAR(20)); +DECLARE @MajorVersion AS SMALLINT = CAST(PARSENAME(@ProductVersion, 4) AS SMALLINT); +DECLARE @MinorVersion AS SMALLINT = CAST(PARSENAME(@ProductVersion, 3) AS SMALLINT); +DECLARE @BuildVersion AS SMALLINT = CAST(PARSENAME(@ProductVersion, 2) AS SMALLINT); + +IF @MajorVersion < 10 +BEGIN + RAISERROR('Sorry, DatabaseRestore doesn''t work on versions of SQL prior to 2008.', 15, 1); + RETURN; +END; + +BEGIN TRY +DECLARE @CurrentDatabaseContext AS VARCHAR(128) = (SELECT DB_NAME()); +DECLARE @CommandExecuteCheck VARCHAR(315) + +SET @CommandExecuteCheck = 'IF NOT EXISTS (SELECT name FROM ' +@CurrentDatabaseContext+'.sys.objects WHERE type = ''P'' AND name = ''CommandExecute'') +BEGIN + RAISERROR (''DatabaseRestore requires the CommandExecute stored procedure from the OLA Hallengren Maintenance solution, are you using the correct database?'', 15, 1); + RETURN; +END;' +EXEC (@CommandExecuteCheck) +END TRY +BEGIN CATCH +THROW; +END CATCH + +DECLARE @cmd NVARCHAR(4000) = N'', --Holds xp_cmdshell command + @sql NVARCHAR(MAX) = N'', --Holds executable SQL commands + @LastFullBackup NVARCHAR(500) = N'', --Last full backup name + @LastDiffBackup NVARCHAR(500) = N'', --Last diff backup name + @LastDiffBackupDateTime NVARCHAR(500) = N'', --Last diff backup date + @BackupFile NVARCHAR(500) = N'', --Name of backup file + @BackupDateTime AS CHAR(15) = N'', --Used for comparisons to generate ordered backup files/create a stopat point + @FullLastLSN NUMERIC(25, 0), --LSN for full + @DiffLastLSN NUMERIC(25, 0), --LSN for diff + @HeadersSQL AS NVARCHAR(4000) = N'', --Dynamic insert into #Headers table (deals with varying results from RESTORE FILELISTONLY across different versions) + @MoveOption AS NVARCHAR(MAX) = N'', --If you need to move restored files to a different directory + @LogRecoveryOption AS NVARCHAR(MAX) = N'', --Holds the option to cause logs to be restored in standby mode or with no recovery + @DatabaseLastLSN NUMERIC(25, 0), --redo_start_lsn of the current database + @i TINYINT = 1, --Maintains loop to continue logs + @LogRestoreRanking INT = 1, --Holds Log iteration # when multiple paths & backup files are being stripped + @LogFirstLSN NUMERIC(25, 0), --Holds first LSN in log backup headers + @LogLastLSN NUMERIC(25, 0), --Holds last LSN in log backup headers + @LogLastNameInMsdbAS NVARCHAR(MAX) = N'', -- Holds last TRN file name already restored + @FileListParamSQL NVARCHAR(4000) = N'', --Holds INSERT list for #FileListParameters + @BackupParameters NVARCHAR(500) = N'', --Used to save BlockSize, MaxTransferSize and BufferCount + @RestoreDatabaseID SMALLINT, --Holds DB_ID of @RestoreDatabaseName + @UnquotedRestoreDatabaseName nvarchar(128); --Holds the unquoted @RestoreDatabaseName + +DECLARE @FileListSimple TABLE ( + BackupFile NVARCHAR(255) NOT NULL, + depth int NOT NULL, + [file] int NOT NULL +); + +DECLARE @FileList TABLE ( + BackupPath NVARCHAR(255) NULL, + BackupFile NVARCHAR(255) NULL +); + +DECLARE @PathItem TABLE ( + PathItem NVARCHAR(512) +); + + +IF OBJECT_ID(N'tempdb..#FileListParameters') IS NOT NULL DROP TABLE #FileListParameters; +CREATE TABLE #FileListParameters +( + LogicalName NVARCHAR(128) NOT NULL, + PhysicalName NVARCHAR(260) NOT NULL, + [Type] CHAR(1) NOT NULL, + FileGroupName NVARCHAR(120) NULL, + Size NUMERIC(20, 0) NOT NULL, + MaxSize NUMERIC(20, 0) NOT NULL, + FileID BIGINT NULL, + CreateLSN NUMERIC(25, 0) NULL, + DropLSN NUMERIC(25, 0) NULL, + UniqueID UNIQUEIDENTIFIER NULL, + ReadOnlyLSN NUMERIC(25, 0) NULL, + ReadWriteLSN NUMERIC(25, 0) NULL, + BackupSizeInBytes BIGINT NULL, + SourceBlockSize INT NULL, + FileGroupID INT NULL, + LogGroupGUID UNIQUEIDENTIFIER NULL, + DifferentialBaseLSN NUMERIC(25, 0) NULL, + DifferentialBaseGUID UNIQUEIDENTIFIER NULL, + IsReadOnly BIT NULL, + IsPresent BIT NULL, + TDEThumbprint VARBINARY(32) NULL, + SnapshotUrl NVARCHAR(360) NULL +); + +IF OBJECT_ID(N'tempdb..#Headers') IS NOT NULL DROP TABLE #Headers; +CREATE TABLE #Headers +( + BackupName NVARCHAR(256), + BackupDescription NVARCHAR(256), + BackupType NVARCHAR(256), + ExpirationDate NVARCHAR(256), + Compressed NVARCHAR(256), + Position NVARCHAR(256), + DeviceType NVARCHAR(256), + UserName NVARCHAR(256), + ServerName NVARCHAR(256), + DatabaseName NVARCHAR(256), + DatabaseVersion NVARCHAR(256), + DatabaseCreationDate NVARCHAR(256), + BackupSize NVARCHAR(256), + FirstLSN NVARCHAR(256), + LastLSN NVARCHAR(256), + CheckpointLSN NVARCHAR(256), + DatabaseBackupLSN NVARCHAR(256), + BackupStartDate NVARCHAR(256), + BackupFinishDate NVARCHAR(256), + SortOrder NVARCHAR(256), + [CodePage] NVARCHAR(256), + UnicodeLocaleId NVARCHAR(256), + UnicodeComparisonStyle NVARCHAR(256), + CompatibilityLevel NVARCHAR(256), + SoftwareVendorId NVARCHAR(256), + SoftwareVersionMajor NVARCHAR(256), + SoftwareVersionMinor NVARCHAR(256), + SoftwareVersionBuild NVARCHAR(256), + MachineName NVARCHAR(256), + Flags NVARCHAR(256), + BindingID NVARCHAR(256), + RecoveryForkID NVARCHAR(256), + Collation NVARCHAR(256), + FamilyGUID NVARCHAR(256), + HasBulkLoggedData NVARCHAR(256), + IsSnapshot NVARCHAR(256), + IsReadOnly NVARCHAR(256), + IsSingleUser NVARCHAR(256), + HasBackupChecksums NVARCHAR(256), + IsDamaged NVARCHAR(256), + BeginsLogChain NVARCHAR(256), + HasIncompleteMetaData NVARCHAR(256), + IsForceOffline NVARCHAR(256), + IsCopyOnly NVARCHAR(256), + FirstRecoveryForkID NVARCHAR(256), + ForkPointLSN NVARCHAR(256), + RecoveryModel NVARCHAR(256), + DifferentialBaseLSN NVARCHAR(256), + DifferentialBaseGUID NVARCHAR(256), + BackupTypeDescription NVARCHAR(256), + BackupSetGUID NVARCHAR(256), + CompressedBackupSize NVARCHAR(256), + Containment NVARCHAR(256), + KeyAlgorithm NVARCHAR(32), + EncryptorThumbprint VARBINARY(20), + EncryptorType NVARCHAR(32), + LastValidRestoreTime DATETIME, + TimeZone NVARCHAR(32), + CompressionAlgorithm NVARCHAR(32), + -- + -- Seq added to retain order by + -- + Seq INT NOT NULL IDENTITY(1, 1) +); + +/* +Correct paths in case people forget a final "\" or "/" +*/ +/*Full*/ +IF (SELECT RIGHT(@BackupPathFull, 1)) <> '/' AND CHARINDEX('/', @BackupPathFull) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @BackupPathFull to add a "/"', 0, 1) WITH NOWAIT; + SET @BackupPathFull += N'/'; +END; +ELSE IF (SELECT RIGHT(@BackupPathFull, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @BackupPathFull to add a "\"', 0, 1) WITH NOWAIT; + SET @BackupPathFull += N'\'; +END; +/*Diff*/ +IF (SELECT RIGHT(@BackupPathDiff, 1)) <> '/' AND CHARINDEX('/', @BackupPathDiff) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @BackupPathDiff to add a "/"', 0, 1) WITH NOWAIT; + SET @BackupPathDiff += N'/'; +END; +ELSE IF (SELECT RIGHT(@BackupPathDiff, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @BackupPathDiff to add a "\"', 0, 1) WITH NOWAIT; + SET @BackupPathDiff += N'\'; +END; +/*Log*/ +IF (SELECT RIGHT(@BackupPathLog, 1)) <> '/' AND CHARINDEX('/', @BackupPathLog) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @BackupPathLog to add a "/"', 0, 1) WITH NOWAIT; + SET @BackupPathLog += N'/'; +END; +IF (SELECT RIGHT(@BackupPathLog, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @BackupPathLog to add a "\"', 0, 1) WITH NOWAIT; + SET @BackupPathLog += N'\'; +END; +/*Move Data File*/ +IF NULLIF(@MoveDataDrive, '') IS NULL +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Getting default data drive for @MoveDataDrive', 0, 1) WITH NOWAIT; + SET @MoveDataDrive = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS nvarchar(260)); +END; +IF (SELECT RIGHT(@MoveDataDrive, 1)) <> '/' AND CHARINDEX('/', @MoveDataDrive) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveDataDrive to add a "/"', 0, 1) WITH NOWAIT; + SET @MoveDataDrive += N'/'; +END; +ELSE IF (SELECT RIGHT(@MoveDataDrive, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveDataDrive to add a "\"', 0, 1) WITH NOWAIT; + SET @MoveDataDrive += N'\'; +END; +/*Move Log File*/ +IF NULLIF(@MoveLogDrive, '') IS NULL +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Getting default log drive for @MoveLogDrive', 0, 1) WITH NOWAIT; + SET @MoveLogDrive = CAST(SERVERPROPERTY('InstanceDefaultLogPath') AS nvarchar(260)); +END; +IF (SELECT RIGHT(@MoveLogDrive, 1)) <> '/' AND CHARINDEX('/', @MoveLogDrive) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing@MoveLogDrive to add a "/"', 0, 1) WITH NOWAIT; + SET @MoveLogDrive += N'/'; +END; +ELSE IF (SELECT RIGHT(@MoveLogDrive, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveLogDrive to add a "\"', 0, 1) WITH NOWAIT; + SET @MoveLogDrive += N'\'; +END; +/*Move Filestream File*/ +IF NULLIF(@MoveFilestreamDrive, '') IS NULL +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Setting default data drive for @MoveFilestreamDrive', 0, 1) WITH NOWAIT; + SET @MoveFilestreamDrive = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS nvarchar(260)); +END; +IF (SELECT RIGHT(@MoveFilestreamDrive, 1)) <> '/' AND CHARINDEX('/', @MoveFilestreamDrive) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveFilestreamDrive to add a "/"', 0, 1) WITH NOWAIT; + SET @MoveFilestreamDrive += N'/'; +END; +ELSE IF (SELECT RIGHT(@MoveFilestreamDrive, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveFilestreamDrive to add a "\"', 0, 1) WITH NOWAIT; + SET @MoveFilestreamDrive += N'\'; +END; +/*Move FullText Catalog File*/ +IF NULLIF(@MoveFullTextCatalogDrive, '') IS NULL +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Setting default data drive for @MoveFullTextCatalogDrive', 0, 1) WITH NOWAIT; + SET @MoveFullTextCatalogDrive = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS nvarchar(260)); +END; +IF (SELECT RIGHT(@MoveFullTextCatalogDrive, 1)) <> '/' AND CHARINDEX('/', @MoveFullTextCatalogDrive) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveFullTextCatalogDrive to add a "/"', 0, 1) WITH NOWAIT; + SET @MoveFullTextCatalogDrive += N'/'; +END; +IF (SELECT RIGHT(@MoveFullTextCatalogDrive, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @MoveFullTextCatalogDrive to add a "\"', 0, 1) WITH NOWAIT; + SET @MoveFullTextCatalogDrive += N'\'; +END; +/*Standby Undo File*/ +IF (SELECT RIGHT(@StandbyUndoPath, 1)) <> '/' AND CHARINDEX('/', @StandbyUndoPath) > 0 --Has to end in a '/' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @StandbyUndoPath to add a "/"', 0, 1) WITH NOWAIT; + SET @StandbyUndoPath += N'/'; +END; +IF (SELECT RIGHT(@StandbyUndoPath, 1)) <> '\' --Has to end in a '\' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Fixing @StandbyUndoPath to add a "\"', 0, 1) WITH NOWAIT; + SET @StandbyUndoPath += N'\'; +END; + +IF @RestoreDatabaseName IS NULL OR @RestoreDatabaseName LIKE N'' /*use LIKE instead of =, otherwise N'' = N' '. See: https://www.brentozar.com/archive/2017/04/surprising-behavior-trailing-spaces/ */ +BEGIN + SET @RestoreDatabaseName = @Database; +END; + +/*check input parameters*/ +IF NOT @MaxTransferSize IS NULL +BEGIN + IF @MaxTransferSize > 4194304 + BEGIN + RAISERROR('@MaxTransferSize can not be greater then 4194304', 0, 1) WITH NOWAIT; + END + + IF @MaxTransferSize % 64 <> 0 + BEGIN + RAISERROR('@MaxTransferSize has to be a multiple of 65536', 0, 1) WITH NOWAIT; + END +END; + +IF NOT @BlockSize IS NULL +BEGIN + IF @BlockSize NOT IN (512, 1024, 2048, 4096, 8192, 16384, 32768, 65536) + BEGIN + RAISERROR('Supported values for @BlockSize are 512, 1024, 2048, 4096, 8192, 16384, 32768, and 65536', 0, 1) WITH NOWAIT; + END +END + +--File Extension cleanup +IF @FileExtensionDiff LIKE '%.%' +BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('Removing "." from @FileExtensionDiff', 0, 1) WITH NOWAIT; + SET @FileExtensionDiff = REPLACE(@FileExtensionDiff,'.',''); +END + +SET @RestoreDatabaseID = DB_ID(@RestoreDatabaseName); +SET @RestoreDatabaseName = QUOTENAME(@RestoreDatabaseName); +SET @UnquotedRestoreDatabaseName = PARSENAME(@RestoreDatabaseName,1); + +--If xp_cmdshell is disabled, force use of xp_dirtree +IF NOT EXISTS (SELECT * FROM sys.configurations WHERE name = 'xp_cmdshell' AND value_in_use = 1) + SET @SimpleFolderEnumeration = 1; + +SET @HeadersSQL = +N'INSERT INTO #Headers WITH (TABLOCK) + (BackupName, BackupDescription, BackupType, ExpirationDate, Compressed, Position, DeviceType, UserName, ServerName + ,DatabaseName, DatabaseVersion, DatabaseCreationDate, BackupSize, FirstLSN, LastLSN, CheckpointLSN, DatabaseBackupLSN + ,BackupStartDate, BackupFinishDate, SortOrder, CodePage, UnicodeLocaleId, UnicodeComparisonStyle, CompatibilityLevel + ,SoftwareVendorId, SoftwareVersionMajor, SoftwareVersionMinor, SoftwareVersionBuild, MachineName, Flags, BindingID + ,RecoveryForkID, Collation, FamilyGUID, HasBulkLoggedData, IsSnapshot, IsReadOnly, IsSingleUser, HasBackupChecksums + ,IsDamaged, BeginsLogChain, HasIncompleteMetaData, IsForceOffline, IsCopyOnly, FirstRecoveryForkID, ForkPointLSN + ,RecoveryModel, DifferentialBaseLSN, DifferentialBaseGUID, BackupTypeDescription, BackupSetGUID, CompressedBackupSize'; + +IF @MajorVersion >= 11 + SET @HeadersSQL += NCHAR(13) + NCHAR(10) + N', Containment'; + +IF @MajorVersion >= 13 OR (@MajorVersion = 12 AND @BuildVersion >= 2342) + SET @HeadersSQL += N', KeyAlgorithm, EncryptorThumbprint, EncryptorType'; + +IF @MajorVersion >= 16 + SET @HeadersSQL += N', LastValidRestoreTime, TimeZone, CompressionAlgorithm'; + +SET @HeadersSQL += N')' + NCHAR(13) + NCHAR(10); +SET @HeadersSQL += N'EXEC (''RESTORE HEADERONLY FROM DISK=''''{Path}'''''')'; + +IF @BackupPathFull IS NOT NULL +BEGIN + DECLARE @CurrentBackupPathFull NVARCHAR(255); + + -- Split CSV string logic has taken from Ola Hallengren's :) + WITH BackupPaths ( + StartPosition, EndPosition, PathItem + ) + AS ( + SELECT 1 AS StartPosition, + ISNULL( NULLIF( CHARINDEX( ',', @BackupPathFull, 1 ), 0 ), LEN( @BackupPathFull ) + 1 ) AS EndPosition, + SUBSTRING( @BackupPathFull, 1, ISNULL( NULLIF( CHARINDEX( ',', @BackupPathFull, 1 ), 0 ), LEN( @BackupPathFull ) + 1 ) - 1 ) AS PathItem + WHERE @BackupPathFull IS NOT NULL + UNION ALL + SELECT CAST( EndPosition AS INT ) + 1 AS StartPosition, + ISNULL( NULLIF( CHARINDEX( ',', @BackupPathFull, EndPosition + 1 ), 0 ), LEN( @BackupPathFull ) + 1 ) AS EndPosition, + SUBSTRING( @BackupPathFull, EndPosition + 1, ISNULL( NULLIF( CHARINDEX( ',', @BackupPathFull, EndPosition + 1 ), 0 ), LEN( @BackupPathFull ) + 1 ) - EndPosition - 1 ) AS PathItem + FROM BackupPaths + WHERE EndPosition < LEN( @BackupPathFull ) + 1 + ) + INSERT INTO @PathItem + SELECT CASE RIGHT( PathItem, 1 ) WHEN '\' THEN PathItem ELSE PathItem + '\' END FROM BackupPaths; + + WHILE 1 = 1 + BEGIN + + SELECT TOP 1 @CurrentBackupPathFull = PathItem FROM @PathItem + WHERE PathItem > COALESCE( @CurrentBackupPathFull, '' ) ORDER BY PathItem; + IF @@rowcount = 0 BREAK; + + IF @SimpleFolderEnumeration = 1 + BEGIN -- Get list of files + INSERT INTO @FileListSimple (BackupFile, depth, [file]) EXEC master.sys.xp_dirtree @CurrentBackupPathFull, 1, 1; + INSERT @FileList (BackupPath,BackupFile) SELECT @CurrentBackupPathFull, BackupFile FROM @FileListSimple; + DELETE FROM @FileListSimple; + END + ELSE + BEGIN + SET @cmd = N'DIR /b "' + @CurrentBackupPathFull + N'"'; + IF @Debug = 1 + BEGIN + IF @cmd IS NULL PRINT '@cmd is NULL for @CurrentBackupPathFull'; + PRINT @cmd; + END; + INSERT INTO @FileList (BackupFile) EXEC master.sys.xp_cmdshell @cmd; + UPDATE @FileList SET BackupPath = @CurrentBackupPathFull + WHERE BackupPath IS NULL; + END; + + IF @Debug = 1 + BEGIN + SELECT BackupPath, BackupFile FROM @FileList; + END; + IF @SimpleFolderEnumeration = 1 + BEGIN + /*Check what we can*/ + IF NOT EXISTS (SELECT * FROM @FileList) + BEGIN + RAISERROR('(FULL) No rows were returned for that database in path %s', 16, 1, @CurrentBackupPathFull) WITH NOWAIT; + RETURN; + END; + END + ELSE + BEGIN + /*Full Sanity check folders*/ + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The system cannot find the path specified.' + OR fl.BackupFile = 'File Not Found' + ) = 1 + BEGIN + RAISERROR('(FULL) No rows or bad value for path %s', 16, 1, @CurrentBackupPathFull) WITH NOWAIT; + RETURN; + END; + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'Access is denied.' + ) = 1 + BEGIN + RAISERROR('(FULL) Access is denied to %s', 16, 1, @CurrentBackupPathFull) WITH NOWAIT; + RETURN; + END; + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + ) = 1 + AND + ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile IS NULL + ) = 1 + BEGIN + RAISERROR('(FULL) Empty directory %s', 16, 1, @CurrentBackupPathFull) WITH NOWAIT; + RETURN; + END + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The user name or password is incorrect.' + ) = 1 + BEGIN + RAISERROR('(FULL) Incorrect user name or password for %s', 16, 1, @CurrentBackupPathFull) WITH NOWAIT; + RETURN; + END; + END; + END + /*End folder sanity check*/ + + IF @StopAt IS NOT NULL + BEGIN + DELETE + FROM @FileList + WHERE BackupFile LIKE N'%[_][0-9].bak' + AND BackupFile LIKE N'%' + @Database + N'%' + AND (REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) > @StopAt); + + DELETE + FROM @FileList + WHERE BackupFile LIKE N'%[_][0-9][0-9].bak' + AND BackupFile LIKE N'%' + @Database + N'%' + AND (REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 18 ), '_', '' ) > @StopAt); + END; + + -- Find latest full backup + SELECT @LastFullBackup = MAX(BackupFile) + FROM @FileList + WHERE BackupFile LIKE N'%.bak' + AND + BackupFile LIKE N'%' + @Database + N'%' + AND + (@StopAt IS NULL OR REPLACE( RIGHT( REPLACE( @LastFullBackup, RIGHT( @LastFullBackup, PATINDEX( '%_[0-9][0-9]%', REVERSE( @LastFullBackup ) ) ), '' ), 16 ), '_', '' ) <= @StopAt); + + /* To get all backups that belong to the same set we can do two things: + 1. RESTORE HEADERONLY of ALL backup files in the folder and look for BackupSetGUID. + Backups that belong to the same split will have the same BackupSetGUID. + 2. Olla Hallengren's solution appends file index at the end of the name: + SQLSERVER1_TEST_DB_FULL_20180703_213211_1.bak + SQLSERVER1_TEST_DB_FULL_20180703_213211_2.bak + SQLSERVER1_TEST_DB_FULL_20180703_213211_N.bak + We can and find all related files with the same timestamp but different index. + This option is simpler and requires less changes to this procedure */ + + IF @LastFullBackup IS NULL + BEGIN + RAISERROR('No backups for "%s" found in "%s"', 16, 1, @Database, @BackupPathFull) WITH NOWAIT; + RETURN; + END; + + SELECT BackupPath, BackupFile INTO #SplitFullBackups + FROM @FileList + WHERE LEFT( BackupFile, LEN( BackupFile ) - PATINDEX( '%[_]%', REVERSE( BackupFile ) ) ) = LEFT( @LastFullBackup, LEN( @LastFullBackup ) - PATINDEX( '%[_]%', REVERSE( @LastFullBackup ) ) ) + AND PATINDEX( '%[_]%', REVERSE( @LastFullBackup ) ) <= 7 -- there is a 1 or 2 digit index at the end of the string which indicates split backups. Ola only supports up to 64 file split. + ORDER BY REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) DESC; + + -- File list can be obtained by running RESTORE FILELISTONLY of any file from the given BackupSet therefore we do not have to cater for split backups when building @FileListParamSQL + + SET @FileListParamSQL = + N'INSERT INTO #FileListParameters WITH (TABLOCK) + (LogicalName, PhysicalName, Type, FileGroupName, Size, MaxSize, FileID, CreateLSN, DropLSN + ,UniqueID, ReadOnlyLSN, ReadWriteLSN, BackupSizeInBytes, SourceBlockSize, FileGroupID, LogGroupGUID + ,DifferentialBaseLSN, DifferentialBaseGUID, IsReadOnly, IsPresent, TDEThumbprint'; + + IF @MajorVersion >= 13 + BEGIN + SET @FileListParamSQL += N', SnapshotUrl'; + END; + + SET @FileListParamSQL += N')' + NCHAR(13) + NCHAR(10); + SET @FileListParamSQL += N'EXEC (''RESTORE FILELISTONLY FROM DISK=''''{Path}'''''')'; + + -- get the TOP record to use in "Restore HeaderOnly/FileListOnly" statement as well as Non-Split Backups Restore Command + SELECT TOP 1 @CurrentBackupPathFull = BackupPath, @LastFullBackup = BackupFile + FROM @FileList + ORDER BY REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) DESC; + + SET @sql = REPLACE(@FileListParamSQL, N'{Path}', @CurrentBackupPathFull + @LastFullBackup); + + IF @Debug = 1 + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for INSERT to #FileListParameters: @BackupPathFull + @LastFullBackup'; + PRINT @sql; + END; + + EXEC (@sql); + IF @Debug = 1 + BEGIN + SELECT '#FileListParameters' AS table_name, * FROM #FileListParameters; + SELECT '@FileList' AS table_name, BackupPath, BackupFile FROM @FileList WHERE BackupFile IS NOT NULL; + END + + --get the backup completed data so we can apply tlogs from that point forwards + SET @sql = REPLACE(@HeadersSQL, N'{Path}', @CurrentBackupPathFull + @LastFullBackup); + + IF @Debug = 1 + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for get backup completed data: @BackupPathFull, @LastFullBackup'; + PRINT @sql; + END; + EXECUTE (@sql); + IF @Debug = 1 + BEGIN + SELECT '#Headers' AS table_name, @LastFullBackup AS FullBackupFile, * FROM #Headers + END; + + --Ensure we are looking at the expected backup, but only if we expect to restore a FULL backups + IF NOT EXISTS (SELECT * FROM #Headers h WHERE h.DatabaseName = @Database) + BEGIN + RAISERROR('Backupfile "%s" does not match @Database parameter "%s"', 16, 1, @LastFullBackup, @Database) WITH NOWAIT; + RETURN; + END; + + IF NOT @BufferCount IS NULL + BEGIN + SET @BackupParameters += N', BufferCount=' + cast(@BufferCount as NVARCHAR(10)) + END + + IF NOT @MaxTransferSize IS NULL + BEGIN + SET @BackupParameters += N', MaxTransferSize=' + cast(@MaxTransferSize as NVARCHAR(7)) + END + + IF NOT @BlockSize IS NULL + BEGIN + SET @BackupParameters += N', BlockSize=' + cast(@BlockSize as NVARCHAR(5)) + END + + IF @MoveFiles = 1 + BEGIN + IF @Execute = 'Y' RAISERROR('@MoveFiles = 1, adjusting paths', 0, 1) WITH NOWAIT; + + WITH Files + AS ( + SELECT + CASE + WHEN Type = 'D' THEN @MoveDataDrive + WHEN Type = 'L' THEN @MoveLogDrive + WHEN Type = 'S' THEN @MoveFilestreamDrive + WHEN Type = 'F' THEN @MoveFullTextCatalogDrive + END + CASE + WHEN @Database = @RestoreDatabaseName THEN REVERSE(LEFT(REVERSE(PhysicalName), CHARINDEX('\', REVERSE(PhysicalName), 1) -1)) + ELSE REPLACE(REVERSE(LEFT(REVERSE(PhysicalName), CHARINDEX('\', REVERSE(PhysicalName), 1) -1)), @Database, SUBSTRING(@RestoreDatabaseName, 2, LEN(@RestoreDatabaseName) -2)) + END AS TargetPhysicalName, + PhysicalName, + LogicalName + FROM #FileListParameters) + SELECT @MoveOption = @MoveOption + N', MOVE ''' + Files.LogicalName + N''' TO ''' + Files.TargetPhysicalName + '''' + FROM Files + WHERE Files.TargetPhysicalName <> Files.PhysicalName; + + IF @Debug = 1 PRINT @MoveOption + END; + + /*Process @ExistingDBAction flag */ + IF @ExistingDBAction BETWEEN 1 AND 4 + BEGIN + IF @RestoreDatabaseID IS NOT NULL + BEGIN + IF @ExistingDBAction = 1 + BEGIN + RAISERROR('Setting single user', 0, 1) WITH NOWAIT; + SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE; ' + NCHAR(13); + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for SINGLE_USER'; + PRINT @sql; + END; + IF @Debug IN (0, 1) AND @Execute = 'Y' + BEGIN + IF DATABASEPROPERTYEX(@UnquotedRestoreDatabaseName,'STATUS') != 'RESTORING' + BEGIN + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'ALTER DATABASE SINGLE_USER', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END + ELSE IF @Debug = 1 + BEGIN + IF DATABASEPROPERTYEX(@UnquotedRestoreDatabaseName,'STATUS') IS NULL PRINT 'Unable to retrieve STATUS from "' + @UnquotedRestoreDatabaseName + '" database. Skipping setting database to SINGLE_USER'; + ELSE IF DATABASEPROPERTYEX(@UnquotedRestoreDatabaseName,'STATUS') = 'RESTORING' PRINT @UnquotedRestoreDatabaseName + ' database STATUS is RESTORING. Skiping setting database to SINGLE_USER'; + END + END + END + IF @ExistingDBAction IN (2, 3) + BEGIN + RAISERROR('Killing connections', 0, 1) WITH NOWAIT; + SET @sql = N'/* Kill connections */' + NCHAR(13); + SELECT + @sql = @sql + N'KILL ' + CAST(spid as nvarchar(5)) + N';' + NCHAR(13) + FROM + --database_ID was only added to sys.dm_exec_sessions in SQL Server 2012 but we need to support older + sys.sysprocesses + WHERE + dbid = @RestoreDatabaseID; + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for Kill connections'; + PRINT @sql; + END; + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'KILL CONNECTIONS', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END + IF @ExistingDBAction = 3 + BEGIN + RAISERROR('Dropping database', 0, 1) WITH NOWAIT; + + SET @sql = N'DROP DATABASE ' + @RestoreDatabaseName + NCHAR(13); + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for DROP DATABASE'; + PRINT @sql; + END; + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'DROP DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END + IF @ExistingDBAction = 4 + BEGIN + RAISERROR ('Offlining database', 0, 1) WITH NOWAIT; + + SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + SPACE( 1 ) + 'SET OFFLINE WITH ROLLBACK IMMEDIATE'; + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for Offline database'; + PRINT @sql; + END; + IF @Debug IN (0, 1) AND @Execute = 'Y' + BEGIN + IF DATABASEPROPERTYEX(@UnquotedRestoreDatabaseName,'STATUS') != 'RESTORING' + BEGIN + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'OFFLINE DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END + ELSE IF @Debug = 1 + BEGIN + IF DATABASEPROPERTYEX(@UnquotedRestoreDatabaseName,'STATUS') IS NULL PRINT 'Unable to retrieve STATUS from "' + @UnquotedRestoreDatabaseName + '" database. Skipping setting database OFFLINE'; + ELSE IF DATABASEPROPERTYEX(@UnquotedRestoreDatabaseName,'STATUS') = 'RESTORING' PRINT @UnquotedRestoreDatabaseName + ' database STATUS is RESTORING. Skiping setting database OFFLINE'; + END + END + END; + END + ELSE + RAISERROR('@ExistingDBAction > 0, but no existing @RestoreDatabaseName', 0, 1) WITH NOWAIT; + END + ELSE + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('@ExistingDBAction %u so do nothing', 0, 1, @ExistingDBAction) WITH NOWAIT; + + IF @ContinueLogs = 0 + BEGIN + IF @Execute = 'Y' RAISERROR('@ContinueLogs set to 0', 0, 1) WITH NOWAIT; + + /* now take split backups into account */ + IF (SELECT COUNT(*) FROM #SplitFullBackups) > 0 + BEGIN + IF @Debug = 1 RAISERROR('Split backups found', 0, 1) WITH NOWAIT; + + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM ' + + STUFF( + (SELECT CHAR( 10 ) + ',DISK=''' + BackupPath + BackupFile + '''' + FROM #SplitFullBackups + ORDER BY BackupFile + FOR XML PATH ('')), + 1, + 2, + '') + N' WITH NORECOVERY, REPLACE' + @BackupParameters + @MoveOption + NCHAR(13) + NCHAR(10); + END; + ELSE + BEGIN + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @CurrentBackupPathFull + @LastFullBackup + N''' WITH NORECOVERY, REPLACE' + @BackupParameters + @MoveOption + NCHAR(13) + NCHAR(10); + END + IF (@StandbyMode = 1) + BEGIN + IF (@StandbyUndoPath IS NULL) + BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('The file path of the undo file for standby mode was not specified. The database will not be restored in standby mode.', 0, 1) WITH NOWAIT; + END + ELSE IF (SELECT COUNT(*) FROM #SplitFullBackups) > 0 + BEGIN + SET @sql = @sql + ', STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf''' + NCHAR(13) + NCHAR(10); + END + ELSE + BEGIN + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @CurrentBackupPathFull + @LastFullBackup + N''' WITH REPLACE' + @BackupParameters + @MoveOption + N' , STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf''' + NCHAR(13) + NCHAR(10); + END + END; + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for RESTORE DATABASE: @BackupPathFull, @LastFullBackup, @MoveOption'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + + -- We already loaded #Headers above + + --setting the @BackupDateTime to a numeric string so that it can be used in comparisons + SET @BackupDateTime = REPLACE( RIGHT( REPLACE( @LastFullBackup, RIGHT( @LastFullBackup, PATINDEX( '%_[0-9][0-9]%', REVERSE( @LastFullBackup ) ) ), '' ), 16 ), '_', '' ); + + SELECT @FullLastLSN = CAST(LastLSN AS NUMERIC(25, 0)) FROM #Headers WHERE BackupType = 1; + IF @Debug = 1 + BEGIN + IF @BackupDateTime IS NULL PRINT '@BackupDateTime is NULL for REPLACE: @LastFullBackup'; + PRINT @BackupDateTime; + END; + + END; + ELSE + BEGIN + + SELECT @DatabaseLastLSN = CAST(f.redo_start_lsn AS NUMERIC(25, 0)) + FROM master.sys.databases d + JOIN master.sys.master_files f ON d.database_id = f.database_id + WHERE d.name = SUBSTRING(@RestoreDatabaseName, 2, LEN(@RestoreDatabaseName) - 2) AND f.file_id = 1; + + END; +END; + +IF @BackupPathFull IS NULL AND @ContinueLogs = 1 +BEGIN + + SELECT @DatabaseLastLSN = CAST(f.redo_start_lsn AS NUMERIC(25, 0)) + FROM master.sys.databases d + JOIN master.sys.master_files f ON d.database_id = f.database_id + WHERE d.name = SUBSTRING(@RestoreDatabaseName, 2, LEN(@RestoreDatabaseName) - 2) AND f.file_id = 1; + +END; + +IF @BackupPathDiff IS NOT NULL +BEGIN + DELETE FROM @FileList; + DELETE FROM @FileListSimple; + DELETE FROM @PathItem; + + DECLARE @CurrentBackupPathDiff NVARCHAR(512); + -- Split CSV string logic has taken from Ola Hallengren's :) + WITH BackupPaths ( + StartPosition, EndPosition, PathItem + ) + AS ( + SELECT 1 AS StartPosition, + ISNULL( NULLIF( CHARINDEX( ',', @BackupPathDiff, 1 ), 0 ), LEN( @BackupPathDiff ) + 1 ) AS EndPosition, + SUBSTRING( @BackupPathDiff, 1, ISNULL( NULLIF( CHARINDEX( ',', @BackupPathDiff, 1 ), 0 ), LEN( @BackupPathDiff ) + 1 ) - 1 ) AS PathItem + WHERE @BackupPathDiff IS NOT NULL + UNION ALL + SELECT CAST( EndPosition AS INT ) + 1 AS StartPosition, + ISNULL( NULLIF( CHARINDEX( ',', @BackupPathDiff, EndPosition + 1 ), 0 ), LEN( @BackupPathDiff ) + 1 ) AS EndPosition, + SUBSTRING( @BackupPathDiff, EndPosition + 1, ISNULL( NULLIF( CHARINDEX( ',', @BackupPathDiff, EndPosition + 1 ), 0 ), LEN( @BackupPathDiff ) + 1 ) - EndPosition - 1 ) AS PathItem + FROM BackupPaths + WHERE EndPosition < LEN( @BackupPathDiff ) + 1 + ) + INSERT INTO @PathItem + SELECT CASE RIGHT( PathItem, 1 ) WHEN '\' THEN PathItem ELSE PathItem + '\' END FROM BackupPaths; + + WHILE 1 = 1 + BEGIN + + SELECT TOP 1 @CurrentBackupPathDiff = PathItem FROM @PathItem + WHERE PathItem > COALESCE( @CurrentBackupPathDiff, '' ) ORDER BY PathItem; + IF @@rowcount = 0 BREAK; + + IF @SimpleFolderEnumeration = 1 + BEGIN -- Get list of files + INSERT INTO @FileListSimple (BackupFile, depth, [file]) EXEC master.sys.xp_dirtree @CurrentBackupPathDiff, 1, 1; + INSERT @FileList (BackupPath,BackupFile) SELECT @CurrentBackupPathDiff, BackupFile FROM @FileListSimple; + DELETE FROM @FileListSimple; + END + ELSE + BEGIN + SET @cmd = N'DIR /b "' + @CurrentBackupPathDiff + N'"'; + IF @Debug = 1 + BEGIN + IF @cmd IS NULL PRINT '@cmd is NULL for @CurrentBackupPathDiff'; + PRINT @cmd; + END; + INSERT INTO @FileList (BackupFile) EXEC master.sys.xp_cmdshell @cmd; + UPDATE @FileList SET BackupPath = @CurrentBackupPathDiff WHERE BackupPath IS NULL; + END; + + IF @Debug = 1 + BEGIN + SELECT BackupPath,BackupFile FROM @FileList WHERE BackupFile IS NOT NULL; + END; + IF @SimpleFolderEnumeration = 0 + BEGIN + /*Full Sanity check folders*/ + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The system cannot find the path specified.' + ) = 1 + BEGIN + RAISERROR('(DIFF) Bad value for path %s', 16, 1, @CurrentBackupPathDiff) WITH NOWAIT; + RETURN; + END; + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'Access is denied.' + ) = 1 + BEGIN + RAISERROR('(DIFF) Access is denied to %s', 16, 1, @CurrentBackupPathDiff) WITH NOWAIT; + RETURN; + END; + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The user name or password is incorrect.' + ) = 1 + BEGIN + RAISERROR('(DIFF) Incorrect user name or password for %s', 16, 1, @CurrentBackupPathDiff) WITH NOWAIT; + RETURN; + END; + END; + END + /*End folder sanity check*/ + -- Find latest diff backup + IF @FileExtensionDiff IS NULL + BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('No @FileExtensionDiff given, assuming "bak".', 0, 1) WITH NOWAIT; + SET @FileExtensionDiff = 'bak'; + END + + SELECT TOP 1 @LastDiffBackup = BackupFile, @CurrentBackupPathDiff = BackupPath + FROM @FileList + WHERE BackupFile LIKE N'%.' + @FileExtensionDiff + AND + BackupFile LIKE N'%' + @Database + '%' + AND + (@StopAt IS NULL OR REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) <= @StopAt) + ORDER BY BackupFile DESC; + + -- Load FileList data into Temp Table sorted by DateTime Stamp desc + SELECT BackupPath, BackupFile INTO #SplitDiffBackups + FROM @FileList + WHERE LEFT( BackupFile, LEN( BackupFile ) - PATINDEX( '%[_]%', REVERSE( BackupFile ) ) ) = LEFT( @LastDiffBackup, LEN( @LastDiffBackup ) - PATINDEX( '%[_]%', REVERSE( @LastDiffBackup ) ) ) + AND PATINDEX( '%[_]%', REVERSE( @LastDiffBackup ) ) <= 7 -- there is a 1 or 2 digit index at the end of the string which indicates split backups. Olla only supports up to 64 file split. + ORDER BY REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) DESC; + + --No file = no backup to restore + SET @LastDiffBackupDateTime = REPLACE( RIGHT( REPLACE( @LastDiffBackup, RIGHT( @LastDiffBackup, PATINDEX( '%_[0-9][0-9]%', REVERSE( @LastDiffBackup ) ) ), '' ), 16 ), '_', '' ); + + IF @RestoreDiff = 1 AND @BackupDateTime < @LastDiffBackupDateTime + BEGIN + + IF (SELECT COUNT(*) FROM #SplitDiffBackups) > 0 + BEGIN + IF @Debug = 1 RAISERROR ('Split backups found', 0, 1) WITH NOWAIT; + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM ' + + STUFF( + (SELECT CHAR( 10 ) + ',DISK=''' + BackupPath + BackupFile + '''' + FROM #SplitDiffBackups + ORDER BY BackupFile + FOR XML PATH ('')), + 1, + 2, + '' ) + N' WITH NORECOVERY, REPLACE' + @BackupParameters + @MoveOption + NCHAR(13) + NCHAR(10); + END; + ELSE + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @CurrentBackupPathDiff + @LastDiffBackup + N''' WITH NORECOVERY' + @BackupParameters + @MoveOption + NCHAR(13) + NCHAR(10); + + IF (@StandbyMode = 1) + BEGIN + IF (@StandbyUndoPath IS NULL) + BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('The file path of the undo file for standby mode was not specified. The database will not be restored in standby mode.', 0, 1) WITH NOWAIT; + END + ELSE IF (SELECT COUNT(*) FROM #SplitDiffBackups) > 0 + SET @sql = @sql + ', STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf''' + NCHAR(13) + NCHAR(10); + ELSE + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' FROM DISK = ''' + @BackupPathDiff + @LastDiffBackup + N''' WITH STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf''' + @BackupParameters + @MoveOption + NCHAR(13) + NCHAR(10); + END; + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for RESTORE DATABASE: @BackupPathDiff, @LastDiffBackup'; + PRINT @sql; + END; + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'RESTORE DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + + --get the backup completed data so we can apply tlogs from that point forwards + SET @sql = REPLACE(@HeadersSQL, N'{Path}', @CurrentBackupPathDiff + @LastDiffBackup); + + IF @Debug = 1 + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for REPLACE: @CurrentBackupPathDiff, @LastDiffBackup'; + PRINT @sql; + END; + + EXECUTE (@sql); + IF @Debug = 1 + BEGIN + SELECT '#Headers' AS table_name, @LastDiffBackup AS DiffbackupFile, * FROM #Headers AS h WHERE h.BackupType = 5; + END + + --set the @BackupDateTime to the date time on the most recent differential + SET @BackupDateTime = ISNULL( @LastDiffBackupDateTime, @BackupDateTime ); + IF @Debug = 1 + BEGIN + IF @BackupDateTime IS NULL PRINT '@BackupDateTime is NULL for REPLACE: @LastDiffBackupDateTime'; + PRINT @BackupDateTime; + END; + SELECT @DiffLastLSN = CAST(LastLSN AS NUMERIC(25, 0)) + FROM #Headers + WHERE BackupType = 5; + END; + + IF @DiffLastLSN IS NULL + BEGIN + SET @DiffLastLSN=@FullLastLSN + END +END + + +IF @BackupPathLog IS NOT NULL +BEGIN + DELETE FROM @FileList; + DELETE FROM @FileListSimple; + DELETE FROM @PathItem; + + DECLARE @CurrentBackupPathLog NVARCHAR(512); + -- Split CSV string logic has taken from Ola Hallengren's :) + WITH BackupPaths ( + StartPosition, EndPosition, PathItem + ) + AS ( + SELECT 1 AS StartPosition, + ISNULL( NULLIF( CHARINDEX( ',', @BackupPathLog, 1 ), 0 ), LEN( @BackupPathLog ) + 1 ) AS EndPosition, + SUBSTRING( @BackupPathLog, 1, ISNULL( NULLIF( CHARINDEX( ',', @BackupPathLog, 1 ), 0 ), LEN( @BackupPathLog ) + 1 ) - 1 ) AS PathItem + WHERE @BackupPathLog IS NOT NULL + UNION ALL + SELECT CAST( EndPosition AS INT ) + 1 AS StartPosition, + ISNULL( NULLIF( CHARINDEX( ',', @BackupPathLog, EndPosition + 1 ), 0 ), LEN( @BackupPathLog ) + 1 ) AS EndPosition, + SUBSTRING( @BackupPathLog, EndPosition + 1, ISNULL( NULLIF( CHARINDEX( ',', @BackupPathLog, EndPosition + 1 ), 0 ), LEN( @BackupPathLog ) + 1 ) - EndPosition - 1 ) AS PathItem + FROM BackupPaths + WHERE EndPosition < LEN( @BackupPathLog ) + 1 + ) + INSERT INTO @PathItem + SELECT CASE RIGHT( PathItem, 1 ) WHEN '\' THEN PathItem ELSE PathItem + '\' END FROM BackupPaths; + + WHILE 1 = 1 + BEGIN + SELECT TOP 1 @CurrentBackupPathLog = PathItem FROM @PathItem + WHERE PathItem > COALESCE( @CurrentBackupPathLog, '' ) ORDER BY PathItem; + IF @@rowcount = 0 BREAK; + + IF @SimpleFolderEnumeration = 1 + BEGIN -- Get list of files + INSERT INTO @FileListSimple (BackupFile, depth, [file]) EXEC master.sys.xp_dirtree @BackupPathLog, 1, 1; + INSERT @FileList (BackupPath, BackupFile) SELECT @CurrentBackupPathLog, BackupFile FROM @FileListSimple; + DELETE FROM @FileListSimple; + END + ELSE + BEGIN + SET @cmd = N'DIR /b "' + @CurrentBackupPathLog + N'"'; + IF @Debug = 1 + BEGIN + IF @cmd IS NULL PRINT '@cmd is NULL for @CurrentBackupPathLog'; + PRINT @cmd; + END; + INSERT INTO @FileList (BackupFile) EXEC master.sys.xp_cmdshell @cmd; + UPDATE @FileList SET BackupPath = @CurrentBackupPathLog + WHERE BackupPath IS NULL; + END; + + IF @SimpleFolderEnumeration = 1 + BEGIN + /*Check what we can*/ + IF NOT EXISTS (SELECT * FROM @FileList) + BEGIN + RAISERROR('(LOG) No rows were returned for that database %s in path %s', 16, 1, @Database, @CurrentBackupPathLog) WITH NOWAIT; + RETURN; + END; + END + ELSE + BEGIN + /*Full Sanity check folders*/ + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The system cannot find the path specified.' + OR fl.BackupFile = 'File Not Found' + ) = 1 + BEGIN + RAISERROR('(LOG) No rows or bad value for path %s', 16, 1, @CurrentBackupPathLog) WITH NOWAIT; + RETURN; + END; + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'Access is denied.' + ) = 1 + BEGIN + RAISERROR('(LOG) Access is denied to %s', 16, 1, @CurrentBackupPathLog) WITH NOWAIT; + RETURN; + END; + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + ) = 1 + AND + ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile IS NULL + ) = 1 + BEGIN + RAISERROR('(LOG) Empty directory %s', 16, 1, @CurrentBackupPathLog) WITH NOWAIT; + RETURN; + END + + IF ( + SELECT COUNT(*) + FROM @FileList AS fl + WHERE fl.BackupFile = 'The user name or password is incorrect.' + ) = 1 + BEGIN + RAISERROR('(LOG) Incorrect user name or password for %s', 16, 1, @CurrentBackupPathLog) WITH NOWAIT; + RETURN; + END; + END; + END + /*End folder sanity check*/ + +IF @Debug = 1 +BEGIN + SELECT * FROM @FileList WHERE BackupFile IS NOT NULL; +END + +IF @SkipBackupsAlreadyInMsdb = 1 +BEGIN + + SELECT TOP 1 @LogLastNameInMsdbAS = bf.physical_device_name + FROM msdb.dbo.backupmediafamily bf + INNER JOIN msdb.dbo.backupset bs ON bs.media_set_id = bf.media_set_id + INNER JOIN msdb.dbo.restorehistory rh ON rh.backup_set_id = bs.backup_set_id + WHERE physical_device_name like @BackupPathLog + '%' + AND rh.destination_database_name = @UnquotedRestoreDatabaseName + ORDER BY physical_device_name DESC + + IF @Debug = 1 + BEGIN + SELECT 'Keeping LOG backups with name > : ' + @LogLastNameInMsdbAS + END + + DELETE fl + FROM @FileList AS fl + WHERE fl.BackupPath + fl.BackupFile <= @LogLastNameInMsdbAS +END + + + +IF (@OnlyLogsAfter IS NOT NULL) +BEGIN + + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('@OnlyLogsAfter is NOT NULL, deleting from @FileList', 0, 1) WITH NOWAIT; + + DELETE fl + FROM @FileList AS fl + WHERE BackupFile LIKE N'%.trn' + AND BackupFile LIKE N'%' + @Database + N'%' + AND REPLACE( RIGHT( REPLACE( fl.BackupFile, RIGHT( fl.BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( fl.BackupFile ) ) ), '' ), 16 ), '_', '' ) < @OnlyLogsAfter; + +END + +-- Check for log backups +IF(@BackupDateTime IS NOT NULL AND @BackupDateTime <> '') + BEGIN + DELETE FROM @FileList + WHERE BackupFile LIKE N'%.trn' + AND BackupFile LIKE N'%' + @Database + N'%' + AND NOT (@ContinueLogs = 1 OR (@ContinueLogs = 0 AND REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' ) >= @BackupDateTime)); + END; + + +IF (@StandbyMode = 1) + BEGIN + IF (@StandbyUndoPath IS NULL) + BEGIN + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('The file path of the undo file for standby mode was not specified. Logs will not be restored in standby mode.', 0, 1) WITH NOWAIT; + END; + ELSE + SET @LogRecoveryOption = N'STANDBY = ''' + @StandbyUndoPath + @Database + 'Undo.ldf'''; + END; + +IF (@LogRecoveryOption = N'') + BEGIN + SET @LogRecoveryOption = N'NORECOVERY'; + END; + +IF (@StopAt IS NOT NULL) +BEGIN + + IF @Execute = 'Y' OR @Debug = 1 RAISERROR('@OnlyLogsAfter is NOT NULL, deleting from @FileList', 0, 1) WITH NOWAIT; + + IF LEN(@StopAt) <> 14 OR PATINDEX('%[^0-9]%', @StopAt) > 0 + BEGIN + RAISERROR('@StopAt parameter is incorrect. It should contain exactly 14 digits in the format yyyyMMddhhmmss.', 16, 1) WITH NOWAIT; + RETURN + END + + IF ISDATE(STUFF(STUFF(STUFF(@StopAt, 13, 0, ':'), 11, 0, ':'), 9, 0, ' ')) = 0 + BEGIN + RAISERROR('@StopAt is not a valid datetime.', 16, 1) WITH NOWAIT; + RETURN + END + + -- Add the STOPAT parameter to the log recovery options but change the value to a valid DATETIME, e.g. '20211118040230' -> '20211118 04:02:30' + SET @LogRecoveryOption += ', STOPAT = ''' + STUFF(STUFF(STUFF(@StopAt, 13, 0, ':'), 11, 0, ':'), 9, 0, ' ') + '''' + + IF @BackupDateTime = @StopAt + BEGIN + IF @Debug = 1 + BEGIN + RAISERROR('@StopAt is the end time of a FULL backup, no log files will be restored.', 0, 1) WITH NOWAIT; + END + END + ELSE + BEGIN + DECLARE @ExtraLogFile NVARCHAR(255) + SELECT TOP 1 @ExtraLogFile = fl.BackupFile + FROM @FileList AS fl + WHERE BackupFile LIKE N'%.trn' + AND BackupFile LIKE N'%' + @Database + N'%' + AND REPLACE( RIGHT( REPLACE( fl.BackupFile, RIGHT( fl.BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( fl.BackupFile ) ) ), '' ), 16 ), '_', '' ) > @StopAt + ORDER BY BackupFile; + END + + IF @ExtraLogFile IS NULL + BEGIN + DELETE fl + FROM @FileList AS fl + WHERE BackupFile LIKE N'%.trn' + AND BackupFile LIKE N'%' + @Database + N'%' + AND REPLACE( RIGHT( REPLACE( fl.BackupFile, RIGHT( fl.BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( fl.BackupFile ) ) ), '' ), 16 ), '_', '' ) > @StopAt; + END + ELSE + BEGIN + -- If this is a split backup, @ExtraLogFile contains only the first split backup file, either _1.trn or _01.trn + -- Change @ExtraLogFile to the max split backup file, then delete all log files greater than this + SET @ExtraLogFile = REPLACE(REPLACE(@ExtraLogFile, '_1.trn', '_9.trn'), '_01.trn', '_64.trn') + + DELETE fl + FROM @FileList AS fl + WHERE BackupFile LIKE N'%.trn' + AND BackupFile LIKE N'%' + @Database + N'%' + AND fl.BackupFile > @ExtraLogFile + END +END + + + -- Group Ordering based on Backup File Name excluding Index {#} to construct coma separated string in "Restore Log" Command +SELECT BackupPath,BackupFile,DENSE_RANK() OVER (ORDER BY REPLACE( RIGHT( REPLACE( BackupFile, RIGHT( BackupFile, PATINDEX( '%_[0-9][0-9]%', REVERSE( BackupFile ) ) ), '' ), 16 ), '_', '' )) AS DenseRank INTO #SplitLogBackups +FROM @FileList +WHERE BackupFile IS NOT NULL; + +-- Loop through all the files for the database + WHILE 1 = 1 + BEGIN + + -- Get the TOP record to use in "Restore HeaderOnly/FileListOnly" statement + SELECT TOP 1 @CurrentBackupPathLog = BackupPath, @BackupFile = BackupFile FROM #SplitLogBackups WHERE DenseRank = @LogRestoreRanking; + IF @@rowcount = 0 BREAK; + + IF @i = 1 + + BEGIN + SET @sql = REPLACE(@HeadersSQL, N'{Path}', @CurrentBackupPathLog + @BackupFile); + + IF @Debug = 1 + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for REPLACE: @HeadersSQL, @CurrentBackupPathLog, @BackupFile'; + PRINT @sql; + END; + + EXECUTE (@sql); + + SELECT TOP 1 @LogFirstLSN = CAST(FirstLSN AS NUMERIC(25, 0)), + @LogLastLSN = CAST(LastLSN AS NUMERIC(25, 0)) + FROM #Headers + WHERE BackupType = 2; + + IF (@ContinueLogs = 0 AND @LogFirstLSN <= @FullLastLSN AND @FullLastLSN <= @LogLastLSN AND @RestoreDiff = 0) OR (@ContinueLogs = 1 AND @LogFirstLSN <= @DatabaseLastLSN AND @DatabaseLastLSN < @LogLastLSN AND @RestoreDiff = 0) + SET @i = 2; + + IF (@ContinueLogs = 0 AND @LogFirstLSN <= @DiffLastLSN AND @DiffLastLSN <= @LogLastLSN AND @RestoreDiff = 1) OR (@ContinueLogs = 1 AND @LogFirstLSN <= @DatabaseLastLSN AND @DatabaseLastLSN < @LogLastLSN AND @RestoreDiff = 1) + SET @i = 2; + + DELETE FROM #Headers WHERE BackupType = 2; + + + END; + + IF @i = 1 + BEGIN + IF @Debug = 1 RAISERROR('No Log to Restore', 0, 1) WITH NOWAIT; + END + + IF @i = 2 + BEGIN + IF @Execute = 'Y' RAISERROR('@i set to 2, restoring logs', 0, 1) WITH NOWAIT; + + IF (SELECT COUNT( * ) FROM #SplitLogBackups WHERE DenseRank = @LogRestoreRanking) > 1 + BEGIN + IF @Debug = 1 RAISERROR ('Split backups found', 0, 1) WITH NOWAIT; + SET @sql = N'RESTORE LOG ' + @RestoreDatabaseName + N' FROM ' + + STUFF( + (SELECT CHAR( 10 ) + ',DISK=''' + BackupPath + BackupFile + '''' + FROM #SplitLogBackups + WHERE DenseRank = @LogRestoreRanking + ORDER BY BackupFile + FOR XML PATH ('')), + 1, + 2, + '' ) + N' WITH ' + @LogRecoveryOption + NCHAR(13) + NCHAR(10); + END; + ELSE + SET @sql = N'RESTORE LOG ' + @RestoreDatabaseName + N' FROM DISK = ''' + @CurrentBackupPathLog + @BackupFile + N''' WITH ' + @LogRecoveryOption + NCHAR(13) + NCHAR(10); + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for RESTORE LOG: @RestoreDatabaseName, @CurrentBackupPathLog, @BackupFile'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'RESTORE LOG', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END; + + SET @LogRestoreRanking += 1; + END; + + + IF @Debug = 1 + BEGIN + SELECT '#SplitLogBackups' AS table_name, BackupPath, BackupFile FROM #SplitLogBackups; + END +END + +-- Put database in a useable state +IF @RunRecovery = 1 + BEGIN + SET @sql = N'RESTORE DATABASE ' + @RestoreDatabaseName + N' WITH RECOVERY'; + + IF @KeepCdc = 1 + SET @sql = @sql + N', KEEP_CDC'; + + SET @sql = @sql + NCHAR(13); + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for RESTORE DATABASE: @RestoreDatabaseName'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'RECOVER DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END; + +-- Ensure simple recovery model +IF @ForceSimpleRecovery = 1 + BEGIN + SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + N' SET RECOVERY SIMPLE' + NCHAR(13); + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for SET RECOVERY SIMPLE: @RestoreDatabaseName'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'SIMPLE LOGGING', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END; + + -- Run checkdb against this database +IF @RunCheckDB = 1 + BEGIN + SET @sql = N'DBCC CHECKDB (' + @RestoreDatabaseName + N') WITH NO_INFOMSGS, ALL_ERRORMSGS, DATA_PURITY;'; + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for Run Integrity Check: @RestoreDatabaseName'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'DBCC CHECKDB', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END; + + +IF @DatabaseOwner IS NOT NULL + BEGIN + IF @RunRecovery = 1 + BEGIN + IF EXISTS (SELECT * FROM master.dbo.syslogins WHERE syslogins.loginname = @DatabaseOwner) + BEGIN + SET @sql = N'ALTER AUTHORIZATION ON DATABASE::' + @RestoreDatabaseName + ' TO [' + @DatabaseOwner + ']'; + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for Set Database Owner'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master',@Command = @sql, @CommandType = 'ALTER AUTHORIZATION', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END + ELSE + BEGIN + PRINT @DatabaseOwner + ' is not a valid Login. Database Owner not set.'; + END + END + ELSE + BEGIN + PRINT @RestoreDatabaseName + ' is still in Recovery, so we are unable to change the database owner to [' + @DatabaseOwner + '].'; + END + END; + + IF @SetTrustworthyON = 1 + BEGIN + IF @RunRecovery = 1 + BEGIN + IF IS_SRVROLEMEMBER('sysadmin') = 1 + BEGIN + SET @sql = N'ALTER DATABASE ' + @RestoreDatabaseName + N' SET TRUSTWORTHY ON;'; + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for SET TRUSTWORTHY ON'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master', @Command = @sql, @CommandType = 'ALTER DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END + ELSE + BEGIN + PRINT 'Current user''s login is NOT a member of the sysadmin role. Database TRUSTWORHY bit has not been enabled.'; + END + END + ELSE + BEGIN + PRINT @RestoreDatabaseName + ' is still in Recovery, so we are unable to enable the TRUSTWORHY bit.'; + END + END; + +-- Link a user entry in the sys.database_principals system catalog view in the restored database to a SQL Server login of the same name +IF @FixOrphanUsers = 1 + BEGIN + SET @sql = N' +-- Fixup Orphan Users by setting database user sid to match login sid +DECLARE @FixOrphansSql NVARCHAR(MAX); +DECLARE @OrphanUsers TABLE (SqlToExecute NVARCHAR(MAX)); +USE ' + @RestoreDatabaseName + '; + +INSERT @OrphanUsers +SELECT ''ALTER USER ['' + d.name + ''] WITH LOGIN = ['' + d.name + '']; '' + FROM sys.database_principals d + INNER JOIN master.sys.server_principals s ON d.name COLLATE DATABASE_DEFAULT = s.name COLLATE DATABASE_DEFAULT + WHERE d.type_desc = ''SQL_USER'' + AND d.name NOT IN (''guest'',''dbo'') + AND d.sid <> s.sid + ORDER BY d.name; + +SELECT @FixOrphansSql = (SELECT SqlToExecute AS [text()] FROM @OrphanUsers FOR XML PATH (''''), TYPE).value(''text()[1]'',''NVARCHAR(MAX)''); + +IF @FixOrphansSql IS NULL + PRINT ''No orphan users require a sid fixup.''; +ELSE +BEGIN + PRINT ''Fix Orphan Users: '' + @FixOrphansSql; + EXECUTE(@FixOrphansSql); +END;' + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for Fix Orphan Users'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE [dbo].[CommandExecute] @DatabaseContext = 'master', @Command = @sql, @CommandType = 'UPDATE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + END; + +-- If test restore then blow the database away (be careful) +IF @TestRestore = 1 + BEGIN + SET @sql = N'DROP DATABASE ' + @RestoreDatabaseName + NCHAR(13); + + IF @Debug = 1 OR @Execute = 'N' + BEGIN + IF @sql IS NULL PRINT '@sql is NULL for DROP DATABASE: @RestoreDatabaseName'; + PRINT @sql; + END; + + IF @Debug IN (0, 1) AND @Execute = 'Y' + EXECUTE @sql = [dbo].[CommandExecute] @DatabaseContext=N'master',@Command = @sql, @CommandType = 'DROP DATABASE', @Mode = 1, @DatabaseName = @UnquotedRestoreDatabaseName, @LogToTable = 'Y', @Execute = 'Y'; + + END; + +-- Clean-Up Tempdb Objects +IF OBJECT_ID( 'tempdb..#SplitFullBackups' ) IS NOT NULL DROP TABLE #SplitFullBackups; +IF OBJECT_ID( 'tempdb..#SplitDiffBackups' ) IS NOT NULL DROP TABLE #SplitDiffBackups; +IF OBJECT_ID( 'tempdb..#SplitLogBackups' ) IS NOT NULL DROP TABLE #SplitLogBackups; +GO +IF OBJECT_ID('dbo.sp_ineachdb') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_ineachdb AS RETURN 0') +GO + +ALTER PROCEDURE [dbo].[sp_ineachdb] + -- mssqltips.com/sqlservertip/5694/execute-a-command-in-the-context-of-each-database-in-sql-server--part-2/ + @command nvarchar(max) = NULL, + @replace_character nchar(1) = N'?', + @print_dbname bit = 0, + @select_dbname bit = 0, + @print_command bit = 0, + @print_command_only bit = 0, + @suppress_quotename bit = 0, -- use with caution + @system_only bit = 0, + @user_only bit = 0, + @name_pattern nvarchar(300) = N'%', + @database_list nvarchar(max) = NULL, + @exclude_pattern nvarchar(300) = NULL, + @exclude_list nvarchar(max) = NULL, + @recovery_model_desc nvarchar(120) = NULL, + @compatibility_level tinyint = NULL, + @state_desc nvarchar(120) = N'ONLINE', + @is_read_only bit = 0, + @is_auto_close_on bit = NULL, + @is_auto_shrink_on bit = NULL, + @is_broker_enabled bit = NULL, + @user_access nvarchar(128) = NULL, + @Help BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 +-- WITH EXECUTE AS OWNER – maybe not a great idea, depending on the security of your system +AS +BEGIN + SET NOCOUNT ON; + SET STATISTICS XML OFF; + + SELECT @Version = '8.16', @VersionDate = '20230820'; + + IF(@VersionCheckMode = 1) + BEGIN + RETURN; + END; + + IF @Help = 1 + + BEGIN + + PRINT ' + /* + sp_ineachdb from http://FirstResponderKit.org + + This script will execute a command against multiple databases. + + To learn more, visit http://FirstResponderKit.org where you can download new + versions for free, watch training videos on how it works, get more info on + the findings, contribute your own code, and more. + + Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. + - Tastes awful with marmite. + + Unknown limitations of this version: + - None. (If we knew them, they would be known. Duh.) + + Changes - for the full list of improvements and fixes in this version, see: + https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + MIT License + + Copyright (c) Brent Ozar Unlimited + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */ + '; + + RETURN -1; + END + + DECLARE @exec nvarchar(150), + @sx nvarchar(18) = N'.sys.sp_executesql', + @db sysname, + @dbq sysname, + @cmd nvarchar(max), + @thisdb sysname, + @cr char(2) = CHAR(13) + CHAR(10), + @SQLVersion AS tinyint = (@@microsoftversion / 0x1000000) & 0xff, -- Stores the SQL Server Version Number(8(2000),9(2005),10(2008 & 2008R2),11(2012),12(2014),13(2016),14(2017),15(2019) + @ServerName AS sysname = CONVERT(sysname, SERVERPROPERTY('ServerName')), -- Stores the SQL Server Instance name. + @NoSpaces nvarchar(20) = N'%[^' + CHAR(9) + CHAR(32) + CHAR(10) + CHAR(13) + N']%'; --Pattern for PATINDEX + + + CREATE TABLE #ineachdb(id int, name nvarchar(512), is_distributor bit); + + +/* + -- first, let's limit to only DBs the caller is interested in + IF @database_list > N'' + -- comma-separated list of potentially valid/invalid/quoted/unquoted names + BEGIN + ;WITH n(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM n WHERE n <= LEN(@database_list)), + names AS + ( + SELECT name = LTRIM(RTRIM(PARSENAME(SUBSTRING(@database_list, n, + CHARINDEX(N',', @database_list + N',', n) - n), 1))) + FROM n + WHERE SUBSTRING(N',' + @database_list, n, 1) = N',' + ) + INSERT #ineachdb(id,name,is_distributor) + SELECT d.database_id, d.name, d.is_distributor + FROM sys.databases AS d + WHERE EXISTS (SELECT 1 FROM names WHERE name = d.name) + OPTION (MAXRECURSION 0); + END + ELSE + BEGIN + INSERT #ineachdb(id,name,is_distributor) SELECT database_id, name, is_distributor FROM sys.databases; + END + + -- now delete any that have been explicitly excluded - exclude trumps include + IF @exclude_list > N'' + -- comma-separated list of potentially valid/invalid/quoted/unquoted names + BEGIN + ;WITH n(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM n WHERE n <= LEN(@exclude_list)), + names AS + ( + SELECT name = LTRIM(RTRIM(PARSENAME(SUBSTRING(@exclude_list, n, + CHARINDEX(N',', @exclude_list + N',', n) - n), 1))) + FROM n + WHERE SUBSTRING(N',' + @exclude_list, n, 1) = N',' + ) + DELETE d + FROM #ineachdb AS d + INNER JOIN names + ON names.name = d.name + OPTION (MAXRECURSION 0); + END +*/ + +/* +@database_list and @exclude_list are are processed at the same time +1)Read the list searching for a comma or [ +2)If we find a comma, save the name +3)If we find a [, we begin to accumulate the result until we reach closing ], (jumping over escaped ]]). +4)Finally, tabs, line breaks and spaces are removed from unquoted names +*/ +WITH C +AS (SELECT V.SrcList + , CAST('' AS nvarchar(MAX)) AS Name + , V.DBList + , 0 AS InBracket + , 0 AS Quoted + FROM (VALUES ('In', @database_list + ','), ('Out', @exclude_list + ',')) AS V (SrcList, DBList) + UNION ALL + SELECT C.SrcList +-- , IIF(V.Found = '[', '', SUBSTRING(C.DBList, 1, V.Place - 1))/*remove initial [*/ + , CASE WHEN V.Found = '[' THEN '' ELSE SUBSTRING(C.DBList, 1, V.Place - 1) END /*remove initial [*/ + , STUFF(C.DBList, 1, V.Place, '') +-- , IIF(V.Found = '[', 1, 0) + ,Case WHEN V.Found = '[' THEN 1 ELSE 0 END + , 0 + FROM C + CROSS APPLY + ( VALUES (PATINDEX('%[,[]%', C.DBList), SUBSTRING(C.DBList, PATINDEX('%[,[]%', C.DBList), 1))) AS V (Place, Found) + WHERE C.DBList > '' + AND C.InBracket = 0 + UNION ALL + SELECT C.SrcList +-- , CONCAT(C.Name, SUBSTRING(C.DBList, 1, V.Place + W.DoubleBracket - 1)) /*Accumulates only one ] if escaped]] or none if end]*/ + , ISNULL(C.Name,'') + ISNULL(SUBSTRING(C.DBList, 1, V.Place + W.DoubleBracket - 1),'') /*Accumulates only one ] if escaped]] or none if end]*/ + , STUFF(C.DBList, 1, V.Place + W.DoubleBracket, '') + , W.DoubleBracket + , 1 + FROM C + CROSS APPLY (VALUES (CHARINDEX(']', C.DBList))) AS V (Place) + -- CROSS APPLY (VALUES (IIF(SUBSTRING(C.DBList, V.Place + 1, 1) = ']', 1, 0))) AS W (DoubleBracket) + CROSS APPLY (VALUES (CASE WHEN SUBSTRING(C.DBList, V.Place + 1, 1) = ']' THEN 1 ELSE 0 END)) AS W (DoubleBracket) + WHERE C.DBList > '' + AND C.InBracket = 1) + , F +AS (SELECT C.SrcList + , CASE WHEN C.Quoted = 0 THEN + SUBSTRING(C.Name, PATINDEX(@NoSpaces, Name), DATALENGTH (Name)/2 - PATINDEX(@NoSpaces, Name) - PATINDEX(@NoSpaces, REVERSE(Name))+2) + ELSE C.Name END + AS name + FROM C + WHERE C.InBracket = 0 + AND C.Name > '') +INSERT #ineachdb(id,name,is_distributor) +SELECT d.database_id + , d.name + , d.is_distributor +FROM sys.databases AS d +WHERE ( EXISTS (SELECT NULL FROM F WHERE F.name = d.name AND F.SrcList = 'In') + OR @database_list IS NULL) + AND NOT EXISTS (SELECT NULL FROM F WHERE F.name = d.name AND F.SrcList = 'Out') +OPTION (MAXRECURSION 0); +; + -- next, let's delete any that *don't* match various criteria passed in + DELETE dbs FROM #ineachdb AS dbs + WHERE (@system_only = 1 AND (id NOT IN (1,2,3,4) AND is_distributor <> 1)) + OR (@user_only = 1 AND (id IN (1,2,3,4) OR is_distributor = 1)) + OR name NOT LIKE @name_pattern + OR name LIKE @exclude_pattern + OR EXISTS + ( + SELECT 1 + FROM sys.databases AS d + WHERE d.database_id = dbs.id + AND NOT + ( + recovery_model_desc = COALESCE(@recovery_model_desc, recovery_model_desc) + AND compatibility_level = COALESCE(@compatibility_level, compatibility_level) + AND is_read_only = COALESCE(@is_read_only, is_read_only) + AND is_auto_close_on = COALESCE(@is_auto_close_on, is_auto_close_on) + AND is_auto_shrink_on = COALESCE(@is_auto_shrink_on, is_auto_shrink_on) + AND is_broker_enabled = COALESCE(@is_broker_enabled, is_broker_enabled) + ) + ); + + -- if a user access is specified, remove any that are NOT in that state + IF @user_access IN (N'SINGLE_USER', N'MULTI_USER', N'RESTRICTED_USER') + BEGIN + DELETE #ineachdb WHERE + CONVERT(nvarchar(128), DATABASEPROPERTYEX(name, 'UserAccess')) <> @user_access; + END + + -- finally, remove any that are not *fully* online or we can't access + DELETE dbs FROM #ineachdb AS dbs + WHERE EXISTS + ( + SELECT 1 FROM sys.databases + WHERE database_id = dbs.id + AND + ( + @state_desc = N'ONLINE' AND + ( + [state] & 992 <> 0 -- inaccessible + OR state_desc <> N'ONLINE' -- not online + OR HAS_DBACCESS(name) = 0 -- don't have access + OR DATABASEPROPERTYEX(name, 'Collation') IS NULL -- not fully online. See "status" here: + -- https://docs.microsoft.com/en-us/sql/t-sql/functions/databasepropertyex-transact-sql + ) + OR (@state_desc <> N'ONLINE' AND state_desc <> @state_desc) + ) + ); + + -- from Andy Mallon / First Responders Kit. Make sure that if we're an + -- AG secondary, we skip any database where allow connections is off + IF @SQLVersion >= 11 AND 3 = (SELECT COUNT(*) FROM sys.all_objects WHERE name IN('availability_replicas','dm_hadr_availability_group_states','dm_hadr_database_replica_states')) + BEGIN + DELETE dbs FROM #ineachdb AS dbs + WHERE EXISTS + ( + SELECT 1 FROM sys.dm_hadr_database_replica_states AS drs + INNER JOIN sys.availability_replicas AS ar + ON ar.replica_id = drs.replica_id + INNER JOIN sys.dm_hadr_availability_group_states ags + ON ags.group_id = ar.group_id + WHERE drs.database_id = dbs.id + AND ar.secondary_role_allow_connections = 0 + AND ags.primary_replica <> @ServerName + ); + END + + -- Well, if we deleted them all... + IF NOT EXISTS (SELECT 1 FROM #ineachdb) + BEGIN + RAISERROR(N'No databases to process.', 1, 0); + RETURN; + END + + -- ok, now, let's go through what we have left + DECLARE dbs CURSOR LOCAL FAST_FORWARD + FOR SELECT DB_NAME(id), QUOTENAME(DB_NAME(id)) + FROM #ineachdb; + + OPEN dbs; + + FETCH NEXT FROM dbs INTO @db, @dbq; + + DECLARE @msg1 nvarchar(512) = N'Could not run against %s : %s.', + @msg2 nvarchar(max); + + WHILE @@FETCH_STATUS <> -1 + BEGIN + SET @thisdb = CASE WHEN @suppress_quotename = 1 THEN @db ELSE @dbq END; + SET @cmd = REPLACE(@command, @replace_character, REPLACE(@thisdb,'''','''''')); + + BEGIN TRY + IF @print_dbname = 1 + BEGIN + PRINT N'/* ' + @thisdb + N' */'; + END + + IF @select_dbname = 1 + BEGIN + SELECT [ineachdb current database] = @thisdb; + END + + IF 1 IN (@print_command, @print_command_only) + BEGIN + PRINT N'/* For ' + @thisdb + ': */' + @cr + @cr + @cmd + @cr + @cr; + END + + IF COALESCE(@print_command_only,0) = 0 + BEGIN + SET @exec = @dbq + @sx; + EXEC @exec @cmd; + END + END TRY + + BEGIN CATCH + SET @msg2 = ERROR_MESSAGE(); + RAISERROR(@msg1, 1, 0, @db, @msg2); + END CATCH + + FETCH NEXT FROM dbs INTO @db, @dbq; + END + + CLOSE dbs; + DEALLOCATE dbs; +END +GO + +IF (OBJECT_ID('dbo.SqlServerVersions') IS NULL) +BEGIN + + CREATE TABLE dbo.SqlServerVersions + ( + MajorVersionNumber tinyint not null, + MinorVersionNumber smallint not null, + Branch varchar(34) not null, + [Url] varchar(99) not null, + ReleaseDate date not null, + MainstreamSupportEndDate date not null, + ExtendedSupportEndDate date not null, + MajorVersionName varchar(19) not null, + MinorVersionName varchar(67) not null, + + CONSTRAINT PK_SqlServerVersions PRIMARY KEY CLUSTERED + ( + MajorVersionNumber ASC, + MinorVersionNumber ASC, + ReleaseDate ASC + ) + ); + + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The major version number.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'MajorVersionNumber' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The minor version number.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'MinorVersionNumber' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The update level of the build. CU indicates a cumulative update. SP indicates a service pack. RTM indicates Release To Manufacturer. GDR indicates a General Distribution Release. QFE indicates Quick Fix Engineering (aka hotfix).' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'Branch' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'A link to the KB article for a version.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'Url' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The date the version was publicly released.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'ReleaseDate' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The date main stream Microsoft support ends for the version.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'MainstreamSupportEndDate' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The date extended Microsoft support ends for the version.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'ExtendedSupportEndDate' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The major version name.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'MajorVersionName' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'The minor version name.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions', @level2type=N'COLUMN',@level2name=N'MinorVersionName' + EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'A reference for SQL Server major and minor versions.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SqlServerVersions' + +END; +GO + +DELETE FROM dbo.SqlServerVersions; + +INSERT INTO dbo.SqlServerVersions + (MajorVersionNumber, MinorVersionNumber, Branch, [Url], ReleaseDate, MainstreamSupportEndDate, ExtendedSupportEndDate, MajorVersionName, MinorVersionName) +VALUES + (16, 4065, 'CU7', 'https://support.microsoft.com/en-us/help/5028743', '2023-08-10', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 7'), + (16, 4055, 'CU6', 'https://support.microsoft.com/en-us/help/5027505', '2023-07-13', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 6'), + (16, 4045, 'CU5', 'https://support.microsoft.com/en-us/help/5026806', '2023-06-15', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 5'), + (16, 4035, 'CU4', 'https://support.microsoft.com/en-us/help/5026717', '2023-05-11', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 4'), + (16, 4025, 'CU3', 'https://support.microsoft.com/en-us/help/5024396', '2023-04-13', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 3'), + (16, 4015, 'CU2', 'https://support.microsoft.com/en-us/help/5023127', '2023-03-15', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 2'), + (16, 4003, 'CU1', 'https://support.microsoft.com/en-us/help/5022375', '2023-02-16', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'Cumulative Update 1'), + (16, 1050, 'RTM GDR', 'https://support.microsoft.com/kb/5021522', '2023-02-14', '2028-01-11', '2033-01-11', 'SQL Server 2022 GDR', 'RTM'), + (16, 1000, 'RTM', '', '2022-11-15', '2028-01-11', '2033-01-11', 'SQL Server 2022', 'RTM'), + (15, 4322, 'CU22', 'https://support.microsoft.com/kb/5027702', '2023-08-14', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 22'), + (15, 4316, 'CU21', 'https://support.microsoft.com/kb/5025808', '2023-06-15', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 21'), + (15, 4312, 'CU20', 'https://support.microsoft.com/kb/5024276', '2023-04-13', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 20'), + (15, 4298, 'CU19', 'https://support.microsoft.com/kb/5023049', '2023-02-16', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 19'), + (15, 4280, 'CU18 GDR', 'https://support.microsoft.com/kb/5021124', '2023-02-14', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 18 GDR'), + (15, 4261, 'CU18', 'https://support.microsoft.com/en-us/help/5017593', '2022-09-28', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 18'), + (15, 4249, 'CU17', 'https://support.microsoft.com/en-us/help/5016394', '2022-08-11', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 17'), + (15, 4236, 'CU16 GDR', 'https://support.microsoft.com/en-us/help/5014353', '2022-06-14', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 16 GDR'), + (15, 4223, 'CU16', 'https://support.microsoft.com/en-us/help/5011644', '2022-04-18', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 16'), + (15, 4198, 'CU15', 'https://support.microsoft.com/en-us/help/5008996', '2022-01-07', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 15'), + (15, 4188, 'CU14', 'https://support.microsoft.com/en-us/help/5007182', '2021-11-22', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 14'), + (15, 4178, 'CU13', 'https://support.microsoft.com/en-us/help/5005679', '2021-10-05', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 13'), + (15, 4153, 'CU12', 'https://support.microsoft.com/en-us/help/5004524', '2021-08-04', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 12'), + (15, 4138, 'CU11', 'https://support.microsoft.com/en-us/help/5003249', '2021-06-10', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 11'), + (15, 4123, 'CU10', 'https://support.microsoft.com/en-us/help/5001090', '2021-04-06', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 10'), + (15, 4102, 'CU9', 'https://support.microsoft.com/en-us/help/5000642', '2021-02-11', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 9 '), + (15, 4073, 'CU8 GDR', 'https://support.microsoft.com/en-us/help/4583459', '2021-01-12', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 8 GDR '), + (15, 4073, 'CU8', 'https://support.microsoft.com/en-us/help/4577194', '2020-10-01', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 8 '), + (15, 4063, 'CU7', 'https://support.microsoft.com/en-us/help/4570012', '2020-09-02', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 7 '), + (15, 4053, 'CU6', 'https://support.microsoft.com/en-us/help/4563110', '2020-08-04', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 6 '), + (15, 4043, 'CU5', 'https://support.microsoft.com/en-us/help/4548597', '2020-06-22', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 5 '), + (15, 4033, 'CU4', 'https://support.microsoft.com/en-us/help/4548597', '2020-03-31', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 4 '), + (15, 4023, 'CU3', 'https://support.microsoft.com/en-us/help/4538853', '2020-03-12', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 3 '), + (15, 4013, 'CU2', 'https://support.microsoft.com/en-us/help/4536075', '2020-02-13', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 2 '), + (15, 4003, 'CU1', 'https://support.microsoft.com/en-us/help/4527376', '2020-01-07', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'Cumulative Update 1 '), + (15, 2070, 'GDR', 'https://support.microsoft.com/en-us/help/4517790', '2019-11-04', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'RTM GDR '), + (15, 2000, 'RTM ', '', '2019-11-04', '2025-01-07', '2030-01-08', 'SQL Server 2019', 'RTM '), + (14, 3460, 'RTM CU31 GDR', 'https://support.microsoft.com/kb/5021126', '2023-02-14', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 31 GDR'), + (14, 3456, 'RTM CU31', 'https://support.microsoft.com/en-us/help/5016884', '2022-09-20', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 31'), + (14, 3451, 'RTM CU30', 'https://support.microsoft.com/en-us/help/5013756', '2022-07-13', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 30'), + (14, 3445, 'RTM CU29 GDR', 'https://support.microsoft.com/en-us/help/5014553', '2022-06-14', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 29 GDR'), + (14, 3436, 'RTM CU29', 'https://support.microsoft.com/en-us/help/5010786', '2022-03-31', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 29'), + (14, 3430, 'RTM CU28', 'https://support.microsoft.com/en-us/help/5006944', '2022-01-13', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 28'), + (14, 3421, 'RTM CU27', 'https://support.microsoft.com/en-us/help/5006944', '2021-10-27', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 27'), + (14, 3411, 'RTM CU26', 'https://support.microsoft.com/en-us/help/5005226', '2021-09-14', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 26'), + (14, 3401, 'RTM CU25', 'https://support.microsoft.com/en-us/help/5003830', '2021-07-12', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 25'), + (14, 3391, 'RTM CU24', 'https://support.microsoft.com/en-us/help/5001228', '2021-05-10', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 24'), + (14, 3381, 'RTM CU23', 'https://support.microsoft.com/en-us/help/5000685', '2021-02-25', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 23'), + (14, 3370, 'RTM CU22 GDR', 'https://support.microsoft.com/en-us/help/4583457', '2021-01-12', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 22 GDR'), + (14, 3356, 'RTM CU22', 'https://support.microsoft.com/en-us/help/4577467', '2020-09-10', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 22'), + (14, 3335, 'RTM CU21', 'https://support.microsoft.com/en-us/help/4557397', '2020-07-01', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 21'), + (14, 3294, 'RTM CU20', 'https://support.microsoft.com/en-us/help/4541283', '2020-04-07', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 20'), + (14, 3257, 'RTM CU19', 'https://support.microsoft.com/en-us/help/4535007', '2020-02-05', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 19'), + (14, 3257, 'RTM CU18', 'https://support.microsoft.com/en-us/help/4527377', '2019-12-09', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 18'), + (14, 3238, 'RTM CU17', 'https://support.microsoft.com/en-us/help/4515579', '2019-10-08', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 17'), + (14, 3223, 'RTM CU16', 'https://support.microsoft.com/en-us/help/4508218', '2019-08-01', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 16'), + (14, 3162, 'RTM CU15', 'https://support.microsoft.com/en-us/help/4498951', '2019-05-24', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 15'), + (14, 3076, 'RTM CU14', 'https://support.microsoft.com/en-us/help/4484710', '2019-03-25', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 14'), + (14, 3048, 'RTM CU13', 'https://support.microsoft.com/en-us/help/4466404', '2018-12-18', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 13'), + (14, 3045, 'RTM CU12', 'https://support.microsoft.com/en-us/help/4464082', '2018-10-24', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 12'), + (14, 3038, 'RTM CU11', 'https://support.microsoft.com/en-us/help/4462262', '2018-09-20', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 11'), + (14, 3037, 'RTM CU10', 'https://support.microsoft.com/en-us/help/4524334', '2018-08-27', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 10'), + (14, 3030, 'RTM CU9', 'https://support.microsoft.com/en-us/help/4515435', '2018-07-18', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 9'), + (14, 3029, 'RTM CU8', 'https://support.microsoft.com/en-us/help/4338363', '2018-06-21', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 8'), + (14, 3026, 'RTM CU7', 'https://support.microsoft.com/en-us/help/4229789', '2018-05-23', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 7'), + (14, 3025, 'RTM CU6', 'https://support.microsoft.com/en-us/help/4101464', '2018-04-17', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 6'), + (14, 3023, 'RTM CU5', 'https://support.microsoft.com/en-us/help/4092643', '2018-03-20', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 5'), + (14, 3022, 'RTM CU4', 'https://support.microsoft.com/en-us/help/4056498', '2018-02-20', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 4'), + (14, 3015, 'RTM CU3', 'https://support.microsoft.com/en-us/help/4052987', '2018-01-04', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 3'), + (14, 3008, 'RTM CU2', 'https://support.microsoft.com/en-us/help/4052574', '2017-11-28', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 2'), + (14, 3006, 'RTM CU1', 'https://support.microsoft.com/en-us/help/4038634', '2017-10-24', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM Cumulative Update 1'), + (14, 1000, 'RTM ', '', '2017-10-02', '2022-10-11', '2027-10-12', 'SQL Server 2017', 'RTM '), + (13, 7016, 'SP3 Azure Feature Pack GDR', 'https://support.microsoft.com/en-us/help/5015371', '2022-06-14', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 3 Azure Feature Pack GDR'), + (13, 7000, 'SP3 Azure Feature Pack', 'https://support.microsoft.com/en-us/help/5014242', '2022-05-19', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 3 Azure Feature Pack'), + (13, 6430, 'SP3 GDR', 'https://support.microsoft.com/kb/5021129', '2023-02-14', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 3 GDR'), + (13, 6419, 'SP3 GDR', 'https://support.microsoft.com/en-us/help/5014355', '2022-06-14', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 3 GDR'), + (13, 6404, 'SP3 GDR', 'https://support.microsoft.com/en-us/help/5006943', '2021-10-27', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 3 GDR'), + (13, 6300, 'SP3 ', 'https://support.microsoft.com/en-us/help/5003279', '2021-09-15', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 3'), + (13, 5888, 'SP2 CU17', 'https://support.microsoft.com/en-us/help/5001092', '2021-03-29', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 17'), + (13, 5882, 'SP2 CU16', 'https://support.microsoft.com/en-us/help/5000645', '2021-02-11', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 16'), + (13, 5865, 'SP2 CU15 GDR', 'https://support.microsoft.com/en-us/help/4583461', '2021-01-12', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 15 GDR'), + (13, 5850, 'SP2 CU15', 'https://support.microsoft.com/en-us/help/4577775', '2020-09-28', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 15'), + (13, 5830, 'SP2 CU14', 'https://support.microsoft.com/en-us/help/4564903', '2020-08-06', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 14'), + (13, 5820, 'SP2 CU13', 'https://support.microsoft.com/en-us/help/4549825', '2020-05-28', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 13'), + (13, 5698, 'SP2 CU12', 'https://support.microsoft.com/en-us/help/4536648', '2020-02-25', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 12'), + (13, 5598, 'SP2 CU11', 'https://support.microsoft.com/en-us/help/4527378', '2019-12-09', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 11'), + (13, 5492, 'SP2 CU10', 'https://support.microsoft.com/en-us/help/4505830', '2019-10-08', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 10'), + (13, 5479, 'SP2 CU9', 'https://support.microsoft.com/en-us/help/4505830', '2019-09-30', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 9'), + (13, 5426, 'SP2 CU8', 'https://support.microsoft.com/en-us/help/4505830', '2019-07-31', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 8'), + (13, 5337, 'SP2 CU7', 'https://support.microsoft.com/en-us/help/4495256', '2019-05-23', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 7'), + (13, 5292, 'SP2 CU6', 'https://support.microsoft.com/en-us/help/4488536', '2019-03-19', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 6'), + (13, 5264, 'SP2 CU5', 'https://support.microsoft.com/en-us/help/4475776', '2019-01-23', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 5'), + (13, 5233, 'SP2 CU4', 'https://support.microsoft.com/en-us/help/4464106', '2018-11-13', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 4'), + (13, 5216, 'SP2 CU3', 'https://support.microsoft.com/en-us/help/4458871', '2018-09-20', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 3'), + (13, 5201, 'SP2 CU2 + Security Update', 'https://support.microsoft.com/en-us/help/4458621', '2018-08-21', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 2 + Security Update'), + (13, 5153, 'SP2 CU2', 'https://support.microsoft.com/en-us/help/4340355', '2018-07-16', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 2'), + (13, 5149, 'SP2 CU1', 'https://support.microsoft.com/en-us/help/4135048', '2018-05-30', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 Cumulative Update 1'), + (13, 5103, 'SP2 GDR', 'https://support.microsoft.com/en-us/help/4583460', '2021-01-12', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 GDR'), + (13, 5026, 'SP2 ', 'https://support.microsoft.com/en-us/help/4052908', '2018-04-24', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 2 '), + (13, 4574, 'SP1 CU15', 'https://support.microsoft.com/en-us/help/4495257', '2019-05-16', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 15'), + (13, 4560, 'SP1 CU14', 'https://support.microsoft.com/en-us/help/4488535', '2019-03-19', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 14'), + (13, 4550, 'SP1 CU13', 'https://support.microsoft.com/en-us/help/4475775', '2019-01-23', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 13'), + (13, 4541, 'SP1 CU12', 'https://support.microsoft.com/en-us/help/4464343', '2018-11-13', '2021-07-13', '2026-07-14', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 12'), + (13, 4528, 'SP1 CU11', 'https://support.microsoft.com/en-us/help/4459676', '2018-09-17', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 11'), + (13, 4514, 'SP1 CU10', 'https://support.microsoft.com/en-us/help/4341569', '2018-07-16', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 10'), + (13, 4502, 'SP1 CU9', 'https://support.microsoft.com/en-us/help/4100997', '2018-05-30', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 9'), + (13, 4474, 'SP1 CU8', 'https://support.microsoft.com/en-us/help/4077064', '2018-03-19', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 8'), + (13, 4466, 'SP1 CU7', 'https://support.microsoft.com/en-us/help/4057119', '2018-01-04', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 7'), + (13, 4457, 'SP1 CU6', 'https://support.microsoft.com/en-us/help/4037354', '2017-11-20', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 6'), + (13, 4451, 'SP1 CU5', 'https://support.microsoft.com/en-us/help/4024305', '2017-09-18', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 5'), + (13, 4446, 'SP1 CU4', 'https://support.microsoft.com/en-us/help/4024305', '2017-08-08', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 4'), + (13, 4435, 'SP1 CU3', 'https://support.microsoft.com/en-us/help/4019916', '2017-05-15', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 3'), + (13, 4422, 'SP1 CU2', 'https://support.microsoft.com/en-us/help/4013106', '2017-03-20', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 2'), + (13, 4411, 'SP1 CU1', 'https://support.microsoft.com/en-us/help/3208177', '2017-01-17', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 1'), + (13, 4224, 'SP1 CU10 + Security Update', 'https://support.microsoft.com/en-us/help/4458842', '2018-08-22', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 Cumulative Update 10 + Security Update'), + (13, 4001, 'SP1 ', 'https://support.microsoft.com/en-us/help/3182545 ', '2016-11-16', '2019-07-09', '2019-07-09', 'SQL Server 2016', 'Service Pack 1 '), + (13, 2216, 'RTM CU9', 'https://support.microsoft.com/en-us/help/4037357', '2017-11-20', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 9'), + (13, 2213, 'RTM CU8', 'https://support.microsoft.com/en-us/help/4024304', '2017-09-18', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 8'), + (13, 2210, 'RTM CU7', 'https://support.microsoft.com/en-us/help/4024304', '2017-08-08', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 7'), + (13, 2204, 'RTM CU6', 'https://support.microsoft.com/en-us/help/4019914', '2017-05-15', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 6'), + (13, 2197, 'RTM CU5', 'https://support.microsoft.com/en-us/help/4013105', '2017-03-20', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 5'), + (13, 2193, 'RTM CU4', 'https://support.microsoft.com/en-us/help/3205052 ', '2017-01-17', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 4'), + (13, 2186, 'RTM CU3', 'https://support.microsoft.com/en-us/help/3205413 ', '2016-11-16', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 3'), + (13, 2164, 'RTM CU2', 'https://support.microsoft.com/en-us/help/3182270 ', '2016-09-22', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 2'), + (13, 2149, 'RTM CU1', 'https://support.microsoft.com/en-us/help/3164674 ', '2016-07-25', '2018-01-09', '2018-01-09', 'SQL Server 2016', 'RTM Cumulative Update 1'), + (13, 1601, 'RTM ', '', '2016-06-01', '2019-01-09', '2019-01-09', 'SQL Server 2016', 'RTM '), + (12, 6444, 'SP3 CU4 GDR', 'https://support.microsoft.com/kb/5021045', '2023-02-14', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 4 GDR'), + (12, 6439, 'SP3 CU4 GDR', 'https://support.microsoft.com/en-us/help/5014164', '2022-06-14', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 4 GDR'), + (12, 6433, 'SP3 CU4 GDR', 'https://support.microsoft.com/en-us/help/4583462', '2021-01-12', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 4 GDR'), + (12, 6372, 'SP3 CU4 GDR', 'https://support.microsoft.com/en-us/help/4535288', '2020-02-11', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 4 GDR'), + (12, 6329, 'SP3 CU4', 'https://support.microsoft.com/en-us/help/4500181', '2019-07-29', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 4'), + (12, 6259, 'SP3 CU3', 'https://support.microsoft.com/en-us/help/4491539', '2019-04-16', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 3'), + (12, 6214, 'SP3 CU2', 'https://support.microsoft.com/en-us/help/4482960', '2019-02-19', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 2'), + (12, 6205, 'SP3 CU1', 'https://support.microsoft.com/en-us/help/4470220', '2018-12-12', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 Cumulative Update 1'), + (12, 6164, 'SP3 GDR', 'https://support.microsoft.com/en-us/help/4583463', '2021-01-12', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 GDR'), + (12, 6024, 'SP3 ', 'https://support.microsoft.com/en-us/help/4022619', '2018-10-30', '2019-07-09', '2024-07-09', 'SQL Server 2014', 'Service Pack 3 '), + (12, 5687, 'SP2 CU18', 'https://support.microsoft.com/en-us/help/4500180', '2019-07-29', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 18'), + (12, 5632, 'SP2 CU17', 'https://support.microsoft.com/en-us/help/4491540', '2019-04-16', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 17'), + (12, 5626, 'SP2 CU16', 'https://support.microsoft.com/en-us/help/4482967', '2019-02-19', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 16'), + (12, 5605, 'SP2 CU15', 'https://support.microsoft.com/en-us/help/4469137', '2018-12-12', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 15'), + (12, 5600, 'SP2 CU14', 'https://support.microsoft.com/en-us/help/4459860', '2018-10-15', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 14'), + (12, 5590, 'SP2 CU13', 'https://support.microsoft.com/en-us/help/4456287', '2018-08-27', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 13'), + (12, 5589, 'SP2 CU12', 'https://support.microsoft.com/en-us/help/4130489', '2018-06-18', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 12'), + (12, 5579, 'SP2 CU11', 'https://support.microsoft.com/en-us/help/4077063', '2018-03-19', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 11'), + (12, 5571, 'SP2 CU10', 'https://support.microsoft.com/en-us/help/4052725', '2018-01-16', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 10'), + (12, 5563, 'SP2 CU9', 'https://support.microsoft.com/en-us/help/4055557', '2017-12-18', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 9'), + (12, 5557, 'SP2 CU8', 'https://support.microsoft.com/en-us/help/4037356', '2017-10-16', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 8'), + (12, 5556, 'SP2 CU7', 'https://support.microsoft.com/en-us/help/4032541', '2017-08-28', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 7'), + (12, 5553, 'SP2 CU6', 'https://support.microsoft.com/en-us/help/4019094', '2017-08-08', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 6'), + (12, 5546, 'SP2 CU5', 'https://support.microsoft.com/en-us/help/4013098', '2017-04-17', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 5'), + (12, 5540, 'SP2 CU4', 'https://support.microsoft.com/en-us/help/4010394', '2017-02-21', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 4'), + (12, 5538, 'SP2 CU3', 'https://support.microsoft.com/en-us/help/3204388 ', '2016-12-19', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 3'), + (12, 5522, 'SP2 CU2', 'https://support.microsoft.com/en-us/help/3188778 ', '2016-10-17', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 2'), + (12, 5511, 'SP2 CU1', 'https://support.microsoft.com/en-us/help/3178925 ', '2016-08-25', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 Cumulative Update 1'), + (12, 5000, 'SP2 ', 'https://support.microsoft.com/en-us/help/3171021 ', '2016-07-11', '2020-01-14', '2020-01-14', 'SQL Server 2014', 'Service Pack 2 '), + (12, 4522, 'SP1 CU13', 'https://support.microsoft.com/en-us/help/4019099', '2017-08-08', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 13'), + (12, 4511, 'SP1 CU12', 'https://support.microsoft.com/en-us/help/4017793', '2017-04-17', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 12'), + (12, 4502, 'SP1 CU11', 'https://support.microsoft.com/en-us/help/4010392', '2017-02-21', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 11'), + (12, 4491, 'SP1 CU10', 'https://support.microsoft.com/en-us/help/3204399 ', '2016-12-19', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 10'), + (12, 4474, 'SP1 CU9', 'https://support.microsoft.com/en-us/help/3186964 ', '2016-10-17', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 9'), + (12, 4468, 'SP1 CU8', 'https://support.microsoft.com/en-us/help/3174038 ', '2016-08-15', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 8'), + (12, 4459, 'SP1 CU7', 'https://support.microsoft.com/en-us/help/3162659 ', '2016-06-20', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 7'), + (12, 4457, 'SP1 CU6', 'https://support.microsoft.com/en-us/help/3167392 ', '2016-05-30', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 6'), + (12, 4449, 'SP1 CU6', 'https://support.microsoft.com/en-us/help/3144524', '2016-04-18', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 6'), + (12, 4438, 'SP1 CU5', 'https://support.microsoft.com/en-us/help/3130926', '2016-02-22', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 5'), + (12, 4436, 'SP1 CU4', 'https://support.microsoft.com/en-us/help/3106660', '2015-12-21', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 4'), + (12, 4427, 'SP1 CU3', 'https://support.microsoft.com/en-us/help/3094221', '2015-10-19', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 3'), + (12, 4422, 'SP1 CU2', 'https://support.microsoft.com/en-us/help/3075950', '2015-08-17', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 2'), + (12, 4416, 'SP1 CU1', 'https://support.microsoft.com/en-us/help/3067839', '2015-06-19', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 Cumulative Update 1'), + (12, 4213, 'SP1 MS15-058: GDR Security Update', 'https://support.microsoft.com/en-us/help/3070446', '2015-07-14', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 MS15-058: GDR Security Update'), + (12, 4100, 'SP1 ', 'https://support.microsoft.com/en-us/help/3058865', '2015-05-04', '2017-10-10', '2017-10-10', 'SQL Server 2014', 'Service Pack 1 '), + (12, 2569, 'RTM CU14', 'https://support.microsoft.com/en-us/help/3158271 ', '2016-06-20', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 14'), + (12, 2568, 'RTM CU13', 'https://support.microsoft.com/en-us/help/3144517', '2016-04-18', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 13'), + (12, 2564, 'RTM CU12', 'https://support.microsoft.com/en-us/help/3130923', '2016-02-22', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 12'), + (12, 2560, 'RTM CU11', 'https://support.microsoft.com/en-us/help/3106659', '2015-12-21', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 11'), + (12, 2556, 'RTM CU10', 'https://support.microsoft.com/en-us/help/3094220', '2015-10-19', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 10'), + (12, 2553, 'RTM CU9', 'https://support.microsoft.com/en-us/help/3075949', '2015-08-17', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 9'), + (12, 2548, 'RTM MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045323', '2015-07-14', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM MS15-058: QFE Security Update'), + (12, 2546, 'RTM CU8', 'https://support.microsoft.com/en-us/help/3067836', '2015-06-19', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 8'), + (12, 2495, 'RTM CU7', 'https://support.microsoft.com/en-us/help/3046038', '2015-04-20', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 7'), + (12, 2480, 'RTM CU6', 'https://support.microsoft.com/en-us/help/3031047', '2015-02-16', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 6'), + (12, 2456, 'RTM CU5', 'https://support.microsoft.com/en-us/help/3011055', '2014-12-17', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 5'), + (12, 2430, 'RTM CU4', 'https://support.microsoft.com/en-us/help/2999197', '2014-10-21', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 4'), + (12, 2402, 'RTM CU3', 'https://support.microsoft.com/en-us/help/2984923', '2014-08-18', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 3'), + (12, 2381, 'RTM MS14-044: QFE Security Update', 'https://support.microsoft.com/en-us/help/2977316', '2014-08-12', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM MS14-044: QFE Security Update'), + (12, 2370, 'RTM CU2', 'https://support.microsoft.com/en-us/help/2967546', '2014-06-27', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 2'), + (12, 2342, 'RTM CU1', 'https://support.microsoft.com/en-us/help/2931693', '2014-04-21', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM Cumulative Update 1'), + (12, 2269, 'RTM MS15-058: GDR Security Update ', 'https://support.microsoft.com/en-us/help/3045324', '2015-07-14', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM MS15-058: GDR Security Update '), + (12, 2254, 'RTM MS14-044: GDR Security Update', 'https://support.microsoft.com/en-us/help/2977315', '2014-08-12', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM MS14-044: GDR Security Update'), + (12, 2000, 'RTM ', '', '2014-04-01', '2016-07-12', '2016-07-12', 'SQL Server 2014', 'RTM '), + (11, 7507, 'SP4 GDR Security Update', 'https://support.microsoft.com/en-us/help/4583465', '2021-01-12', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'Service Pack 4 GDR Security Update for CVE-2021-1636'), + (11, 7493, 'SP4 GDR Security Update', 'https://support.microsoft.com/en-us/help/4532098', '2020-02-11', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'Service Pack 4 GDR Security Update for CVE-2020-0618'), + (11, 7469, 'SP4 On-Demand Hotfix Update', 'https://support.microsoft.com/en-us/help/4091266', '2018-03-28', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'Service Pack 4 SP4 On-Demand Hotfix Update'), + (11, 7462, 'SP4 ADV180002: GDR Security Update', 'https://support.microsoft.com/en-us/help/4057116', '2018-01-12', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'Service Pack 4 ADV180002: GDR Security Update'), + (11, 7001, 'SP4 ', 'https://support.microsoft.com/en-us/help/4018073', '2017-10-02', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'Service Pack 4 '), + (11, 6607, 'SP3 CU10', 'https://support.microsoft.com/en-us/help/4025925', '2017-08-08', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 10'), + (11, 6598, 'SP3 CU9', 'https://support.microsoft.com/en-us/help/4016762', '2017-05-15', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 9'), + (11, 6594, 'SP3 CU8', 'https://support.microsoft.com/en-us/help/3205051 ', '2017-03-20', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 8'), + (11, 6579, 'SP3 CU7', 'https://support.microsoft.com/en-us/help/3205051 ', '2017-01-17', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 7'), + (11, 6567, 'SP3 CU6', 'https://support.microsoft.com/en-us/help/3194992 ', '2016-11-17', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 6'), + (11, 6544, 'SP3 CU5', 'https://support.microsoft.com/en-us/help/3180915 ', '2016-09-19', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 5'), + (11, 6540, 'SP3 CU4', 'https://support.microsoft.com/en-us/help/3165264 ', '2016-07-18', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 4'), + (11, 6537, 'SP3 CU3', 'https://support.microsoft.com/en-us/help/3152635 ', '2016-05-16', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 3'), + (11, 6523, 'SP3 CU2', 'https://support.microsoft.com/en-us/help/3137746', '2016-03-21', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 2'), + (11, 6518, 'SP3 CU1', 'https://support.microsoft.com/en-us/help/3123299', '2016-01-19', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 Cumulative Update 1'), + (11, 6020, 'SP3 ', 'https://support.microsoft.com/en-us/help/3072779', '2015-11-20', '2018-10-09', '2018-10-09', 'SQL Server 2012', 'Service Pack 3 '), + (11, 5678, 'SP2 CU16', 'https://support.microsoft.com/en-us/help/3205416 ', '2016-11-17', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 16'), + (11, 5676, 'SP2 CU15', 'https://support.microsoft.com/en-us/help/3205416 ', '2016-11-17', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 15'), + (11, 5657, 'SP2 CU14', 'https://support.microsoft.com/en-us/help/3180914 ', '2016-09-19', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 14'), + (11, 5655, 'SP2 CU13', 'https://support.microsoft.com/en-us/help/3165266 ', '2016-07-18', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 13'), + (11, 5649, 'SP2 CU12', 'https://support.microsoft.com/en-us/help/3152637 ', '2016-05-16', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 12'), + (11, 5646, 'SP2 CU11', 'https://support.microsoft.com/en-us/help/3137745', '2016-03-21', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 11'), + (11, 5644, 'SP2 CU10', 'https://support.microsoft.com/en-us/help/3120313', '2016-01-19', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 10'), + (11, 5641, 'SP2 CU9', 'https://support.microsoft.com/en-us/help/3098512', '2015-11-16', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 9'), + (11, 5634, 'SP2 CU8', 'https://support.microsoft.com/en-us/help/3082561', '2015-09-21', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 8'), + (11, 5623, 'SP2 CU7', 'https://support.microsoft.com/en-us/help/3072100', '2015-07-20', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 7'), + (11, 5613, 'SP2 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045319', '2015-07-14', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 MS15-058: QFE Security Update'), + (11, 5592, 'SP2 CU6', 'https://support.microsoft.com/en-us/help/3052468', '2015-05-18', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 6'), + (11, 5582, 'SP2 CU5', 'https://support.microsoft.com/en-us/help/3037255', '2015-03-16', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 5'), + (11, 5569, 'SP2 CU4', 'https://support.microsoft.com/en-us/help/3007556', '2015-01-19', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 4'), + (11, 5556, 'SP2 CU3', 'https://support.microsoft.com/en-us/help/3002049', '2014-11-17', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 3'), + (11, 5548, 'SP2 CU2', 'https://support.microsoft.com/en-us/help/2983175', '2014-09-15', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 2'), + (11, 5532, 'SP2 CU1', 'https://support.microsoft.com/en-us/help/2976982', '2014-07-23', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 Cumulative Update 1'), + (11, 5343, 'SP2 MS15-058: GDR Security Update', 'https://support.microsoft.com/en-us/help/3045321', '2015-07-14', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 MS15-058: GDR Security Update'), + (11, 5058, 'SP2 ', 'https://support.microsoft.com/en-us/help/2958429', '2014-06-10', '2017-01-10', '2017-01-10', 'SQL Server 2012', 'Service Pack 2 '), + (11, 3513, 'SP1 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045317', '2015-07-14', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 MS15-058: QFE Security Update'), + (11, 3482, 'SP1 CU13', 'https://support.microsoft.com/en-us/help/3002044', '2014-11-17', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 13'), + (11, 3470, 'SP1 CU12', 'https://support.microsoft.com/en-us/help/2991533', '2014-09-15', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 12'), + (11, 3460, 'SP1 MS14-044: QFE Security Update ', 'https://support.microsoft.com/en-us/help/2977325', '2014-08-12', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 MS14-044: QFE Security Update '), + (11, 3449, 'SP1 CU11', 'https://support.microsoft.com/en-us/help/2975396', '2014-07-21', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 11'), + (11, 3431, 'SP1 CU10', 'https://support.microsoft.com/en-us/help/2954099', '2014-05-19', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 10'), + (11, 3412, 'SP1 CU9', 'https://support.microsoft.com/en-us/help/2931078', '2014-03-17', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 9'), + (11, 3401, 'SP1 CU8', 'https://support.microsoft.com/en-us/help/2917531', '2014-01-20', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 8'), + (11, 3393, 'SP1 CU7', 'https://support.microsoft.com/en-us/help/2894115', '2013-11-18', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 7'), + (11, 3381, 'SP1 CU6', 'https://support.microsoft.com/en-us/help/2874879', '2013-09-16', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 6'), + (11, 3373, 'SP1 CU5', 'https://support.microsoft.com/en-us/help/2861107', '2013-07-15', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 5'), + (11, 3368, 'SP1 CU4', 'https://support.microsoft.com/en-us/help/2833645', '2013-05-30', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 4'), + (11, 3349, 'SP1 CU3', 'https://support.microsoft.com/en-us/help/2812412', '2013-03-18', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 3'), + (11, 3339, 'SP1 CU2', 'https://support.microsoft.com/en-us/help/2790947', '2013-01-21', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 2'), + (11, 3321, 'SP1 CU1', 'https://support.microsoft.com/en-us/help/2765331', '2012-11-20', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 Cumulative Update 1'), + (11, 3156, 'SP1 MS15-058: GDR Security Update', 'https://support.microsoft.com/en-us/help/3045318', '2015-07-14', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 MS15-058: GDR Security Update'), + (11, 3153, 'SP1 MS14-044: GDR Security Update', 'https://support.microsoft.com/en-us/help/2977326', '2014-08-12', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 MS14-044: GDR Security Update'), + (11, 3000, 'SP1 ', 'https://support.microsoft.com/en-us/help/2674319', '2012-11-07', '2015-07-14', '2015-07-14', 'SQL Server 2012', 'Service Pack 1 '), + (11, 2424, 'RTM CU11', 'https://support.microsoft.com/en-us/help/2908007', '2013-12-16', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 11'), + (11, 2420, 'RTM CU10', 'https://support.microsoft.com/en-us/help/2891666', '2013-10-21', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 10'), + (11, 2419, 'RTM CU9', 'https://support.microsoft.com/en-us/help/2867319', '2013-08-20', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 9'), + (11, 2410, 'RTM CU8', 'https://support.microsoft.com/en-us/help/2844205', '2013-06-17', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 8'), + (11, 2405, 'RTM CU7', 'https://support.microsoft.com/en-us/help/2823247', '2013-04-15', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 7'), + (11, 2401, 'RTM CU6', 'https://support.microsoft.com/en-us/help/2728897', '2013-02-18', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 6'), + (11, 2395, 'RTM CU5', 'https://support.microsoft.com/en-us/help/2777772', '2012-12-17', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 5'), + (11, 2383, 'RTM CU4', 'https://support.microsoft.com/en-us/help/2758687', '2012-10-15', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 4'), + (11, 2376, 'RTM MS12-070: QFE Security Update', 'https://support.microsoft.com/en-us/help/2716441', '2012-10-09', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM MS12-070: QFE Security Update'), + (11, 2332, 'RTM CU3', 'https://support.microsoft.com/en-us/help/2723749', '2012-08-31', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 3'), + (11, 2325, 'RTM CU2', 'https://support.microsoft.com/en-us/help/2703275', '2012-06-18', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 2'), + (11, 2316, 'RTM CU1', 'https://support.microsoft.com/en-us/help/2679368', '2012-04-12', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM Cumulative Update 1'), + (11, 2218, 'RTM MS12-070: GDR Security Update', 'https://support.microsoft.com/en-us/help/2716442', '2012-10-09', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM MS12-070: GDR Security Update'), + (11, 2100, 'RTM ', '', '2012-03-06', '2017-07-11', '2022-07-12', 'SQL Server 2012', 'RTM '), + (10, 6529, 'SP3 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045314', '2015-07-14', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'Service Pack 3 MS15-058: QFE Security Update'), + (10, 6220, 'SP3 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045316', '2015-07-14', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'Service Pack 3 MS15-058: QFE Security Update'), + (10, 6000, 'SP3 ', 'https://support.microsoft.com/en-us/help/2979597', '2014-09-26', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'Service Pack 3 '), + (10, 4339, 'SP2 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045312', '2015-07-14', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 MS15-058: QFE Security Update'), + (10, 4321, 'SP2 MS14-044: QFE Security Update', 'https://support.microsoft.com/en-us/help/2977319', '2014-08-14', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 MS14-044: QFE Security Update'), + (10, 4319, 'SP2 CU13', 'https://support.microsoft.com/en-us/help/2967540', '2014-06-30', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 13'), + (10, 4305, 'SP2 CU12', 'https://support.microsoft.com/en-us/help/2938478', '2014-04-21', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 12'), + (10, 4302, 'SP2 CU11', 'https://support.microsoft.com/en-us/help/2926028', '2014-02-18', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 11'), + (10, 4297, 'SP2 CU10', 'https://support.microsoft.com/en-us/help/2908087', '2013-12-17', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 10'), + (10, 4295, 'SP2 CU9', 'https://support.microsoft.com/en-us/help/2887606', '2013-10-28', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 9'), + (10, 4290, 'SP2 CU8', 'https://support.microsoft.com/en-us/help/2871401', '2013-08-22', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 8'), + (10, 4285, 'SP2 CU7', 'https://support.microsoft.com/en-us/help/2844090', '2013-06-17', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 7'), + (10, 4279, 'SP2 CU6', 'https://support.microsoft.com/en-us/help/2830140', '2013-04-15', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 6'), + (10, 4276, 'SP2 CU5', 'https://support.microsoft.com/en-us/help/2797460', '2013-02-18', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 5'), + (10, 4270, 'SP2 CU4', 'https://support.microsoft.com/en-us/help/2777358', '2012-12-17', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 4'), + (10, 4266, 'SP2 CU3', 'https://support.microsoft.com/en-us/help/2754552', '2012-10-15', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 3'), + (10, 4263, 'SP2 CU2', 'https://support.microsoft.com/en-us/help/2740411', '2012-08-31', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 2'), + (10, 4260, 'SP2 CU1', 'https://support.microsoft.com/en-us/help/2720425', '2012-07-24', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 Cumulative Update 1'), + (10, 4042, 'SP2 MS15-058: GDR Security Update', 'https://support.microsoft.com/en-us/help/3045313', '2015-07-14', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 MS15-058: GDR Security Update'), + (10, 4033, 'SP2 MS14-044: GDR Security Update', 'https://support.microsoft.com/en-us/help/2977320', '2014-08-12', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 MS14-044: GDR Security Update'), + (10, 4000, 'SP2 ', 'https://support.microsoft.com/en-us/help/2630458', '2012-07-26', '2015-10-13', '2015-10-13', 'SQL Server 2008 R2', 'Service Pack 2 '), + (10, 2881, 'SP1 CU14', 'https://support.microsoft.com/en-us/help/2868244', '2013-08-08', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 14'), + (10, 2876, 'SP1 CU13', 'https://support.microsoft.com/en-us/help/2855792', '2013-06-17', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 13'), + (10, 2874, 'SP1 CU12', 'https://support.microsoft.com/en-us/help/2828727', '2013-04-15', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 12'), + (10, 2869, 'SP1 CU11', 'https://support.microsoft.com/en-us/help/2812683', '2013-02-18', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 11'), + (10, 2868, 'SP1 CU10', 'https://support.microsoft.com/en-us/help/2783135', '2012-12-17', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 10'), + (10, 2866, 'SP1 CU9', 'https://support.microsoft.com/en-us/help/2756574', '2012-10-15', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 9'), + (10, 2861, 'SP1 MS12-070: QFE Security Update', 'https://support.microsoft.com/en-us/help/2716439', '2012-10-09', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 MS12-070: QFE Security Update'), + (10, 2822, 'SP1 CU8', 'https://support.microsoft.com/en-us/help/2723743', '2012-08-31', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 8'), + (10, 2817, 'SP1 CU7', 'https://support.microsoft.com/en-us/help/2703282', '2012-06-18', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 7'), + (10, 2811, 'SP1 CU6', 'https://support.microsoft.com/en-us/help/2679367', '2012-04-16', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 6'), + (10, 2806, 'SP1 CU5', 'https://support.microsoft.com/en-us/help/2659694', '2012-02-22', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 5'), + (10, 2796, 'SP1 CU4', 'https://support.microsoft.com/en-us/help/2633146', '2011-12-19', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 4'), + (10, 2789, 'SP1 CU3', 'https://support.microsoft.com/en-us/help/2591748', '2011-10-17', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 3'), + (10, 2772, 'SP1 CU2', 'https://support.microsoft.com/en-us/help/2567714', '2011-08-15', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 2'), + (10, 2769, 'SP1 CU1', 'https://support.microsoft.com/en-us/help/2544793', '2011-07-18', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 Cumulative Update 1'), + (10, 2550, 'SP1 MS12-070: GDR Security Update', 'https://support.microsoft.com/en-us/help/2754849', '2012-10-09', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 MS12-070: GDR Security Update'), + (10, 2500, 'SP1 ', 'https://support.microsoft.com/en-us/help/2528583', '2011-07-12', '2013-10-08', '2013-10-08', 'SQL Server 2008 R2', 'Service Pack 1 '), + (10, 1815, 'RTM CU13', 'https://support.microsoft.com/en-us/help/2679366', '2012-04-16', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 13'), + (10, 1810, 'RTM CU12', 'https://support.microsoft.com/en-us/help/2659692', '2012-02-21', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 12'), + (10, 1809, 'RTM CU11', 'https://support.microsoft.com/en-us/help/2633145', '2011-12-19', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 11'), + (10, 1807, 'RTM CU10', 'https://support.microsoft.com/en-us/help/2591746', '2011-10-17', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 10'), + (10, 1804, 'RTM CU9', 'https://support.microsoft.com/en-us/help/2567713', '2011-08-15', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 9'), + (10, 1797, 'RTM CU8', 'https://support.microsoft.com/en-us/help/2534352', '2011-06-20', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 8'), + (10, 1790, 'RTM MS11-049: QFE Security Update', 'https://support.microsoft.com/en-us/help/2494086', '2011-06-14', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM MS11-049: QFE Security Update'), + (10, 1777, 'RTM CU7', 'https://support.microsoft.com/en-us/help/2507770', '2011-04-18', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 7'), + (10, 1765, 'RTM CU6', 'https://support.microsoft.com/en-us/help/2489376', '2011-02-21', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 6'), + (10, 1753, 'RTM CU5', 'https://support.microsoft.com/en-us/help/2438347', '2010-12-20', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 5'), + (10, 1746, 'RTM CU4', 'https://support.microsoft.com/en-us/help/2345451', '2010-10-18', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 4'), + (10, 1734, 'RTM CU3', 'https://support.microsoft.com/en-us/help/2261464', '2010-08-16', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 3'), + (10, 1720, 'RTM CU2', 'https://support.microsoft.com/en-us/help/2072493', '2010-06-21', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 2'), + (10, 1702, 'RTM CU1', 'https://support.microsoft.com/en-us/help/981355', '2010-05-18', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM Cumulative Update 1'), + (10, 1617, 'RTM MS11-049: GDR Security Update', 'https://support.microsoft.com/en-us/help/2494088', '2011-06-14', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM MS11-049: GDR Security Update'), + (10, 1600, 'RTM ', '', '2010-05-10', '2014-07-08', '2019-07-09', 'SQL Server 2008 R2', 'RTM '), + (10, 6535, 'SP3 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045308', '2015-07-14', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS15-058: QFE Security Update'), + (10, 6241, 'SP3 MS15-058: GDR Security Update', 'https://support.microsoft.com/en-us/help/3045311', '2015-07-14', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS15-058: GDR Security Update'), + (10, 5890, 'SP3 MS15-058: QFE Security Update', 'https://support.microsoft.com/en-us/help/3045303', '2015-07-14', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS15-058: QFE Security Update'), + (10, 5869, 'SP3 MS14-044: QFE Security Update', 'https://support.microsoft.com/en-us/help/2984340, https://support.microsoft.com/en-us/help/2977322', '2014-08-12', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS14-044: QFE Security Update'), + (10, 5861, 'SP3 CU17', 'https://support.microsoft.com/en-us/help/2958696', '2014-05-19', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 17'), + (10, 5852, 'SP3 CU16', 'https://support.microsoft.com/en-us/help/2936421', '2014-03-17', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 16'), + (10, 5850, 'SP3 CU15', 'https://support.microsoft.com/en-us/help/2923520', '2014-01-20', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 15'), + (10, 5848, 'SP3 CU14', 'https://support.microsoft.com/en-us/help/2893410', '2013-11-18', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 14'), + (10, 5846, 'SP3 CU13', 'https://support.microsoft.com/en-us/help/2880350', '2013-09-16', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 13'), + (10, 5844, 'SP3 CU12', 'https://support.microsoft.com/en-us/help/2863205', '2013-07-15', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 12'), + (10, 5840, 'SP3 CU11', 'https://support.microsoft.com/en-us/help/2834048', '2013-05-20', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 11'), + (10, 5835, 'SP3 CU10', 'https://support.microsoft.com/en-us/help/2814783', '2013-03-18', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 10'), + (10, 5829, 'SP3 CU9', 'https://support.microsoft.com/en-us/help/2799883', '2013-01-21', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 9'), + (10, 5828, 'SP3 CU8', 'https://support.microsoft.com/en-us/help/2771833', '2012-11-19', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 8'), + (10, 5826, 'SP3 MS12-070: QFE Security Update', 'https://support.microsoft.com/en-us/help/2716435', '2012-10-09', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS12-070: QFE Security Update'), + (10, 5794, 'SP3 CU7', 'https://support.microsoft.com/en-us/help/2738350', '2012-09-17', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 7'), + (10, 5788, 'SP3 CU6', 'https://support.microsoft.com/en-us/help/2715953', '2012-07-16', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 6'), + (10, 5785, 'SP3 CU5', 'https://support.microsoft.com/en-us/help/2696626', '2012-05-21', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 5'), + (10, 5775, 'SP3 CU4', 'https://support.microsoft.com/en-us/help/2673383', '2012-03-19', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 4'), + (10, 5770, 'SP3 CU3', 'https://support.microsoft.com/en-us/help/2648098', '2012-01-16', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 3'), + (10, 5768, 'SP3 CU2', 'https://support.microsoft.com/en-us/help/2633143', '2011-11-21', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 2'), + (10, 5766, 'SP3 CU1', 'https://support.microsoft.com/en-us/help/2617146', '2011-10-17', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 Cumulative Update 1'), + (10, 5538, 'SP3 MS15-058: GDR Security Update', 'https://support.microsoft.com/en-us/help/3045305', '2015-07-14', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS15-058: GDR Security Update'), + (10, 5520, 'SP3 MS14-044: GDR Security Update', 'https://support.microsoft.com/en-us/help/2977321', '2014-08-12', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS14-044: GDR Security Update'), + (10, 5512, 'SP3 MS12-070: GDR Security Update', 'https://support.microsoft.com/en-us/help/2716436', '2012-10-09', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 MS12-070: GDR Security Update'), + (10, 5500, 'SP3 ', 'https://support.microsoft.com/en-us/help/2546951', '2011-10-06', '2015-10-13', '2015-10-13', 'SQL Server 2008', 'Service Pack 3 '), + (10, 4371, 'SP2 MS12-070: QFE Security Update', 'https://support.microsoft.com/en-us/help/2716433', '2012-10-09', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 MS12-070: QFE Security Update'), + (10, 4333, 'SP2 CU11', 'https://support.microsoft.com/en-us/help/2715951', '2012-07-16', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 11'), + (10, 4332, 'SP2 CU10', 'https://support.microsoft.com/en-us/help/2696625', '2012-05-21', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 10'), + (10, 4330, 'SP2 CU9', 'https://support.microsoft.com/en-us/help/2673382', '2012-03-19', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 9'), + (10, 4326, 'SP2 CU8', 'https://support.microsoft.com/en-us/help/2648096', '2012-01-16', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 8'), + (10, 4323, 'SP2 CU7', 'https://support.microsoft.com/en-us/help/2617148', '2011-11-21', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 7'), + (10, 4321, 'SP2 CU6', 'https://support.microsoft.com/en-us/help/2582285', '2011-09-19', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 6'), + (10, 4316, 'SP2 CU5', 'https://support.microsoft.com/en-us/help/2555408', '2011-07-18', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 5'), + (10, 4311, 'SP2 MS11-049: QFE Security Update', 'https://support.microsoft.com/en-us/help/2494094', '2011-06-14', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 MS11-049: QFE Security Update'), + (10, 4285, 'SP2 CU4', 'https://support.microsoft.com/en-us/help/2527180', '2011-05-16', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 4'), + (10, 4279, 'SP2 CU3', 'https://support.microsoft.com/en-us/help/2498535', '2011-03-17', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 3'), + (10, 4272, 'SP2 CU2', 'https://support.microsoft.com/en-us/help/2467239', '2011-01-17', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 2'), + (10, 4266, 'SP2 CU1', 'https://support.microsoft.com/en-us/help/2289254', '2010-11-15', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 Cumulative Update 1'), + (10, 4067, 'SP2 MS12-070: GDR Security Update', 'https://support.microsoft.com/en-us/help/2716434', '2012-10-09', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 MS12-070: GDR Security Update'), + (10, 4064, 'SP2 MS11-049: GDR Security Update', 'https://support.microsoft.com/en-us/help/2494089', '2011-06-14', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 MS11-049: GDR Security Update'), + (10, 4000, 'SP2 ', 'https://support.microsoft.com/en-us/help/2285068', '2010-09-29', '2012-10-09', '2012-10-09', 'SQL Server 2008', 'Service Pack 2 '), + (10, 2850, 'SP1 CU16', 'https://support.microsoft.com/en-us/help/2582282', '2011-09-19', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 16'), + (10, 2847, 'SP1 CU15', 'https://support.microsoft.com/en-us/help/2555406', '2011-07-18', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 15'), + (10, 2841, 'SP1 MS11-049: QFE Security Update', 'https://support.microsoft.com/en-us/help/2494100', '2011-06-14', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 MS11-049: QFE Security Update'), + (10, 2821, 'SP1 CU14', 'https://support.microsoft.com/en-us/help/2527187', '2011-05-16', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 14'), + (10, 2816, 'SP1 CU13', 'https://support.microsoft.com/en-us/help/2497673', '2011-03-17', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 13'), + (10, 2808, 'SP1 CU12', 'https://support.microsoft.com/en-us/help/2467236', '2011-01-17', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 12'), + (10, 2804, 'SP1 CU11', 'https://support.microsoft.com/en-us/help/2413738', '2010-11-15', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 11'), + (10, 2799, 'SP1 CU10', 'https://support.microsoft.com/en-us/help/2279604', '2010-09-20', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 10'), + (10, 2789, 'SP1 CU9', 'https://support.microsoft.com/en-us/help/2083921', '2010-07-19', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 9'), + (10, 2775, 'SP1 CU8', 'https://support.microsoft.com/en-us/help/981702', '2010-05-17', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 8'), + (10, 2766, 'SP1 CU7', 'https://support.microsoft.com/en-us/help/979065', '2010-03-26', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 7'), + (10, 2757, 'SP1 CU6', 'https://support.microsoft.com/en-us/help/977443', '2010-01-18', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 6'), + (10, 2746, 'SP1 CU5', 'https://support.microsoft.com/en-us/help/975977', '2009-11-16', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 5'), + (10, 2734, 'SP1 CU4', 'https://support.microsoft.com/en-us/help/973602', '2009-09-21', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 4'), + (10, 2723, 'SP1 CU3', 'https://support.microsoft.com/en-us/help/971491', '2009-07-20', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 3'), + (10, 2714, 'SP1 CU2', 'https://support.microsoft.com/en-us/help/970315', '2009-05-18', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 2'), + (10, 2710, 'SP1 CU1', 'https://support.microsoft.com/en-us/help/969099', '2009-04-16', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 Cumulative Update 1'), + (10, 2573, 'SP1 MS11-049: GDR Security update', 'https://support.microsoft.com/en-us/help/2494096', '2011-06-14', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 MS11-049: GDR Security update'), + (10, 2531, 'SP1 ', '', '2009-04-01', '2011-10-11', '2011-10-11', 'SQL Server 2008', 'Service Pack 1 '), + (10, 1835, 'RTM CU10', 'https://support.microsoft.com/en-us/help/979064', '2010-03-15', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 10'), + (10, 1828, 'RTM CU9', 'https://support.microsoft.com/en-us/help/977444', '2010-01-18', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 9'), + (10, 1823, 'RTM CU8', 'https://support.microsoft.com/en-us/help/975976', '2009-11-16', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 8'), + (10, 1818, 'RTM CU7', 'https://support.microsoft.com/en-us/help/973601', '2009-09-21', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 7'), + (10, 1812, 'RTM CU6', 'https://support.microsoft.com/en-us/help/971490', '2009-07-20', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 6'), + (10, 1806, 'RTM CU5', 'https://support.microsoft.com/en-us/help/969531', '2009-05-18', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 5'), + (10, 1798, 'RTM CU4', 'https://support.microsoft.com/en-us/help/963036', '2009-03-16', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 4'), + (10, 1787, 'RTM CU3', 'https://support.microsoft.com/en-us/help/960484', '2009-01-19', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 3'), + (10, 1779, 'RTM CU2', 'https://support.microsoft.com/en-us/help/958186', '2008-11-19', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 2'), + (10, 1763, 'RTM CU1', 'https://support.microsoft.com/en-us/help/956717', '2008-09-22', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM Cumulative Update 1'), + (10, 1600, 'RTM ', '', '2008-08-06', '2014-07-08', '2019-07-09', 'SQL Server 2008', 'RTM ') +; +GO +IF OBJECT_ID('dbo.sp_BlitzFirst') IS NULL + EXEC ('CREATE PROCEDURE dbo.sp_BlitzFirst AS RETURN 0;'); +GO + + +ALTER PROCEDURE [dbo].[sp_BlitzFirst] + @LogMessage NVARCHAR(4000) = NULL , + @Help TINYINT = 0 , + @AsOf DATETIMEOFFSET = NULL , + @ExpertMode TINYINT = 0 , + @Seconds INT = 5 , + @OutputType VARCHAR(20) = 'TABLE' , + @OutputServerName NVARCHAR(256) = NULL , + @OutputDatabaseName NVARCHAR(256) = NULL , + @OutputSchemaName NVARCHAR(256) = NULL , + @OutputTableName NVARCHAR(256) = NULL , + @OutputTableNameFileStats NVARCHAR(256) = NULL , + @OutputTableNamePerfmonStats NVARCHAR(256) = NULL , + @OutputTableNameWaitStats NVARCHAR(256) = NULL , + @OutputTableNameBlitzCache NVARCHAR(256) = NULL , + @OutputTableNameBlitzWho NVARCHAR(256) = NULL , + @OutputResultSets NVARCHAR(500) = N'BlitzWho_Start|Findings|FileStats|PerfmonStats|WaitStats|BlitzCache|BlitzWho_End' , + @OutputTableRetentionDays TINYINT = 7 , + @OutputXMLasNVARCHAR TINYINT = 0 , + @FilterPlansByDatabase VARCHAR(MAX) = NULL , + @CheckProcedureCache TINYINT = 0 , + @CheckServerInfo TINYINT = 1 , + @FileLatencyThresholdMS INT = 100 , + @SinceStartup TINYINT = 0 , + @ShowSleepingSPIDs TINYINT = 0 , + @BlitzCacheSkipAnalysis BIT = 1 , + @MemoryGrantThresholdPct DECIMAL(5,2) = 15.00, + @LogMessageCheckID INT = 38, + @LogMessagePriority TINYINT = 1, + @LogMessageFindingsGroup VARCHAR(50) = 'Logged Message', + @LogMessageFinding VARCHAR(200) = 'Logged from sp_BlitzFirst', + @LogMessageURL VARCHAR(200) = '', + @LogMessageCheckDate DATETIMEOFFSET = NULL, + @Debug BIT = 0, + @Version VARCHAR(30) = NULL OUTPUT, + @VersionDate DATETIME = NULL OUTPUT, + @VersionCheckMode BIT = 0 + WITH EXECUTE AS CALLER, RECOMPILE +AS +BEGIN +SET NOCOUNT ON; +SET STATISTICS XML OFF; +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + +SELECT @Version = '8.16', @VersionDate = '20230820'; + +IF(@VersionCheckMode = 1) +BEGIN + RETURN; +END; + +IF @Help = 1 +BEGIN +PRINT ' +sp_BlitzFirst from http://FirstResponderKit.org + +This script gives you a prioritized list of why your SQL Server is slow right now. + +This is not an overall health check - for that, check out sp_Blitz. + +To learn more, visit http://FirstResponderKit.org where you can download new +versions for free, watch training videos on how it works, get more info on +the findings, contribute your own code, and more. + +Known limitations of this version: + - Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000. It + may work just fine on 2005, and if it does, hug your parents. Just don''t + file support issues if it breaks. + - If a temp table called #CustomPerfmonCounters exists for any other session, + but not our session, this stored proc will fail with an error saying the + temp table #CustomPerfmonCounters does not exist. + - @OutputServerName is not functional yet. + - If @OutputDatabaseName, SchemaName, TableName, etc are quoted with brackets, + the write to table may silently fail. Look, I never said I was good at this. + +Unknown limitations of this version: + - None. Like Zombo.com, the only limit is yourself. + +Changes - for the full list of improvements and fixes in this version, see: +https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/ + + +MIT License + +Copyright (c) Brent Ozar Unlimited + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +'; +RETURN; +END; /* @Help = 1 */ + +RAISERROR('Setting up configuration variables',10,1) WITH NOWAIT; +DECLARE @StringToExecute NVARCHAR(MAX), + @ParmDefinitions NVARCHAR(4000), + @Parm1 NVARCHAR(4000), + @OurSessionID INT, + @LineFeed NVARCHAR(10), + @StockWarningHeader NVARCHAR(MAX) = N'', + @StockWarningFooter NVARCHAR(MAX) = N'', + @StockDetailsHeader NVARCHAR(MAX) = N'', + @StockDetailsFooter NVARCHAR(MAX) = N'', + @StartSampleTime DATETIMEOFFSET, + @FinishSampleTime DATETIMEOFFSET, + @FinishSampleTimeWaitFor DATETIME, + @AsOf1 DATETIMEOFFSET, + @AsOf2 DATETIMEOFFSET, + @ServiceName sysname, + @OutputTableNameFileStats_View NVARCHAR(256), + @OutputTableNamePerfmonStats_View NVARCHAR(256), + @OutputTableNamePerfmonStatsActuals_View NVARCHAR(256), + @OutputTableNameWaitStats_View NVARCHAR(256), + @OutputTableNameWaitStats_Categories NVARCHAR(256), + @OutputTableCleanupDate DATE, + @ObjectFullName NVARCHAR(2000), + @BlitzWho NVARCHAR(MAX) = N'EXEC dbo.sp_BlitzWho @ShowSleepingSPIDs = ' + CONVERT(NVARCHAR(1), @ShowSleepingSPIDs) + N';', + @BlitzCacheMinutesBack INT, + @UnquotedOutputServerName NVARCHAR(256) = @OutputServerName , + @UnquotedOutputDatabaseName NVARCHAR(256) = @OutputDatabaseName , + @UnquotedOutputSchemaName NVARCHAR(256) = @OutputSchemaName , + @LocalServerName NVARCHAR(128) = CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)), + @dm_exec_query_statistics_xml BIT = 0, + @total_cpu_usage BIT = 0, + @get_thread_time_ms NVARCHAR(MAX) = N'', + @thread_time_ms FLOAT = 0; + +/* Sanitize our inputs */ +SELECT + @OutputTableNameFileStats_View = QUOTENAME(@OutputTableNameFileStats + '_Deltas'), + @OutputTableNamePerfmonStats_View = QUOTENAME(@OutputTableNamePerfmonStats + '_Deltas'), + @OutputTableNamePerfmonStatsActuals_View = QUOTENAME(@OutputTableNamePerfmonStats + '_Actuals'), + @OutputTableNameWaitStats_View = QUOTENAME(@OutputTableNameWaitStats + '_Deltas'), + @OutputTableNameWaitStats_Categories = QUOTENAME(@OutputTableNameWaitStats + '_Categories'); + +SELECT + @OutputDatabaseName = QUOTENAME(@OutputDatabaseName), + @OutputSchemaName = QUOTENAME(@OutputSchemaName), + @OutputTableName = QUOTENAME(@OutputTableName), + @OutputTableNameFileStats = QUOTENAME(@OutputTableNameFileStats), + @OutputTableNamePerfmonStats = QUOTENAME(@OutputTableNamePerfmonStats), + @OutputTableNameWaitStats = QUOTENAME(@OutputTableNameWaitStats), + @OutputTableCleanupDate = CAST( (DATEADD(DAY, -1 * @OutputTableRetentionDays, GETDATE() ) ) AS DATE), + /* @OutputTableNameBlitzCache = QUOTENAME(@OutputTableNameBlitzCache), We purposely don't sanitize this because sp_BlitzCache will */ + /* @OutputTableNameBlitzWho = QUOTENAME(@OutputTableNameBlitzWho), We purposely don't sanitize this because sp_BlitzWho will */ + @LineFeed = CHAR(13) + CHAR(10), + @OurSessionID = @@SPID, + @OutputType = UPPER(@OutputType); + +IF(@OutputType = 'NONE' AND (@OutputTableName IS NULL OR @OutputSchemaName IS NULL OR @OutputDatabaseName IS NULL)) +BEGIN + RAISERROR('This procedure should be called with a value for all @Output* parameters, as @OutputType is set to NONE',12,1); + RETURN; +END; + +IF UPPER(@OutputType) LIKE 'TOP 10%' SET @OutputType = 'Top10'; +IF @OutputType = 'Top10' SET @SinceStartup = 1; + +/* Logged Message - CheckID 38 */ +IF @LogMessage IS NOT NULL + BEGIN + + RAISERROR('Saving LogMessage to table',10,1) WITH NOWAIT; + + /* Try to set the output table parameters if they don't exist */ + IF @OutputSchemaName IS NULL AND @OutputTableName IS NULL AND @OutputDatabaseName IS NULL + BEGIN + SET @OutputSchemaName = N'[dbo]'; + SET @OutputTableName = N'[BlitzFirst]'; + + /* Look for the table in the current database */ + SELECT TOP 1 @OutputDatabaseName = QUOTENAME(TABLE_CATALOG) + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'BlitzFirst'; + + IF @OutputDatabaseName IS NULL AND EXISTS (SELECT * FROM sys.databases WHERE name = 'DBAtools') + SET @OutputDatabaseName = '[DBAtools]'; + + END; + + IF @OutputDatabaseName IS NULL OR @OutputSchemaName IS NULL OR @OutputTableName IS NULL + OR NOT EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + RAISERROR('We have a hard time logging a message without a valid @OutputDatabaseName, @OutputSchemaName, and @OutputTableName to log it to.', 0, 1) WITH NOWAIT; + RETURN; + END; + IF @LogMessageCheckDate IS NULL + SET @LogMessageCheckDate = SYSDATETIMEOFFSET(); + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, Priority, FindingsGroup, Finding, Details, URL) VALUES( ' + + ' @SrvName, @LogMessageCheckDate, @LogMessageCheckID, @LogMessagePriority, @LogMessageFindingsGroup, @LogMessageFinding, @LogMessage, @LogMessageURL)'; + + EXECUTE sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @LogMessageCheckID INT, @LogMessagePriority TINYINT, @LogMessageFindingsGroup VARCHAR(50), @LogMessageFinding VARCHAR(200), @LogMessage NVARCHAR(4000), @LogMessageCheckDate DATETIMEOFFSET, @LogMessageURL VARCHAR(200)', + @LocalServerName, @LogMessageCheckID, @LogMessagePriority, @LogMessageFindingsGroup, @LogMessageFinding, @LogMessage, @LogMessageCheckDate, @LogMessageURL; + + RAISERROR('LogMessage saved to table. We have made a note of your activity. Keep up the good work.',10,1) WITH NOWAIT; + + RETURN; + END; + +IF @SinceStartup = 1 + SELECT @Seconds = 0, @ExpertMode = 1; + + +IF @OutputType = 'SCHEMA' +BEGIN + SELECT FieldList = '[Priority] TINYINT, [FindingsGroup] VARCHAR(50), [Finding] VARCHAR(200), [URL] VARCHAR(200), [Details] NVARCHAR(4000), [HowToStopIt] NVARCHAR(MAX), [QueryPlan] XML, [QueryText] NVARCHAR(MAX)'; + +END; +ELSE IF @AsOf IS NOT NULL AND @OutputDatabaseName IS NOT NULL AND @OutputSchemaName IS NOT NULL AND @OutputTableName IS NOT NULL +BEGIN + /* They want to look into the past. */ + SET @AsOf1= DATEADD(mi, -15, @AsOf); + SET @AsOf2= DATEADD(mi, +15, @AsOf); + + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') SELECT CheckDate, [Priority], [FindingsGroup], [Finding], [URL], CAST([Details] AS [XML]) AS Details,' + + '[HowToStopIt], [CheckID], [StartTime], [LoginName], [NTUserName], [OriginalLoginName], [ProgramName], [HostName], [DatabaseID],' + + '[DatabaseName], [OpenTransactionCount], [QueryPlan], [QueryText] FROM ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' WHERE CheckDate >= @AsOf1' + + ' AND CheckDate <= @AsOf2' + + ' /*ORDER BY CheckDate, Priority , FindingsGroup , Finding , Details*/;'; + EXEC sp_executesql @StringToExecute, + N'@AsOf1 DATETIMEOFFSET, @AsOf2 DATETIMEOFFSET', + @AsOf1, @AsOf2 + + +END; /* IF @AsOf IS NOT NULL AND @OutputDatabaseName IS NOT NULL AND @OutputSchemaName IS NOT NULL AND @OutputTableName IS NOT NULL */ +ELSE IF @LogMessage IS NULL /* IF @OutputType = 'SCHEMA' */ +BEGIN + /* What's running right now? This is the first and last result set. */ + IF @SinceStartup = 0 AND @Seconds > 0 AND @ExpertMode = 1 AND @OutputType <> 'NONE' AND @OutputResultSets LIKE N'%BlitzWho_Start%' + BEGIN + IF OBJECT_ID('master.dbo.sp_BlitzWho') IS NULL AND OBJECT_ID('dbo.sp_BlitzWho') IS NULL + BEGIN + PRINT N'sp_BlitzWho is not installed in the current database_files. You can get a copy from http://FirstResponderKit.org'; + END; + ELSE + BEGIN + EXEC (@BlitzWho); + END; + END; /* IF @SinceStartup = 0 AND @Seconds > 0 AND @ExpertMode = 1 AND @OutputType <> 'NONE' - What's running right now? This is the first and last result set. */ + + /* Set start/finish times AFTER sp_BlitzWho runs. For more info: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2244 */ + IF @Seconds = 0 AND SERVERPROPERTY('EngineEdition') = 5 /*SERVERPROPERTY('Edition') = 'SQL Azure'*/ + WITH WaitTimes AS ( + SELECT wait_type, wait_time_ms, + NTILE(3) OVER(ORDER BY wait_time_ms) AS grouper + FROM sys.dm_os_wait_stats w + WHERE wait_type IN ('DIRTY_PAGE_POLL','HADR_FILESTREAM_IOMGR_IOCOMPLETION','LAZYWRITER_SLEEP', + 'LOGMGR_QUEUE','REQUEST_FOR_DEADLOCK_SEARCH','XE_TIMER_EVENT') + ) + SELECT @StartSampleTime = DATEADD(mi, AVG(-wait_time_ms / 1000 / 60), SYSDATETIMEOFFSET()), @FinishSampleTime = SYSDATETIMEOFFSET() + FROM WaitTimes + WHERE grouper = 2; + ELSE IF @Seconds = 0 AND SERVERPROPERTY('EngineEdition') <> 5 /*SERVERPROPERTY('Edition') <> 'SQL Azure'*/ + SELECT @StartSampleTime = DATEADD(MINUTE,DATEDIFF(MINUTE, GETDATE(), GETUTCDATE()),create_date) , @FinishSampleTime = SYSDATETIMEOFFSET() + FROM sys.databases + WHERE database_id = 2; + ELSE + SELECT @StartSampleTime = SYSDATETIMEOFFSET(), + @FinishSampleTime = DATEADD(ss, @Seconds, SYSDATETIMEOFFSET()), + @FinishSampleTimeWaitFor = DATEADD(ss, @Seconds, GETDATE()); + + + IF EXISTS + ( + + SELECT + 1/0 + FROM sys.all_columns AS ac + WHERE ac.object_id = OBJECT_ID('sys.dm_os_schedulers') + AND ac.name = 'total_cpu_usage_ms' + + ) + BEGIN + + SELECT + @total_cpu_usage = 1, + @get_thread_time_ms += + N' + SELECT + @thread_time_ms = + CONVERT + ( + FLOAT, + SUM(s.total_cpu_usage_ms) + ) + FROM sys.dm_os_schedulers AS s + WHERE s.status = ''VISIBLE ONLINE'' + AND s.is_online = 1 + OPTION(RECOMPILE); + '; + + END + ELSE + BEGIN + SELECT + @total_cpu_usage = 0, + @get_thread_time_ms += + N' + SELECT + @thread_time_ms = + CONVERT + ( + FLOAT, + SUM(s.total_worker_time / 1000.) + ) + FROM sys.dm_exec_query_stats AS s + OPTION(RECOMPILE); + '; + END + + RAISERROR('Now starting diagnostic analysis',10,1) WITH NOWAIT; + + /* + We start by creating #BlitzFirstResults. It's a temp table that will store + the results from our checks. Throughout the rest of this stored procedure, + we're running a series of checks looking for dangerous things inside the SQL + Server. When we find a problem, we insert rows into the temp table. At the + end, we return these results to the end user. + + #BlitzFirstResults has a CheckID field, but there's no Check table. As we do + checks, we insert data into this table, and we manually put in the CheckID. + We (Brent Ozar Unlimited) maintain a list of the checks by ID#. You can + download that from http://FirstResponderKit.org if you want to build + a tool that relies on the output of sp_BlitzFirst. + */ + + + IF OBJECT_ID('tempdb..#BlitzFirstResults') IS NOT NULL + DROP TABLE #BlitzFirstResults; + CREATE TABLE #BlitzFirstResults + ( + ID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + CheckID INT NOT NULL, + Priority TINYINT NOT NULL, + FindingsGroup VARCHAR(50) NOT NULL, + Finding VARCHAR(200) NOT NULL, + URL VARCHAR(200) NULL, + Details NVARCHAR(MAX) NULL, + HowToStopIt NVARCHAR(MAX) NULL, + QueryPlan [XML] NULL, + QueryText NVARCHAR(MAX) NULL, + StartTime DATETIMEOFFSET NULL, + LoginName NVARCHAR(128) NULL, + NTUserName NVARCHAR(128) NULL, + OriginalLoginName NVARCHAR(128) NULL, + ProgramName NVARCHAR(128) NULL, + HostName NVARCHAR(128) NULL, + DatabaseID INT NULL, + DatabaseName NVARCHAR(128) NULL, + OpenTransactionCount INT NULL, + QueryStatsNowID INT NULL, + QueryStatsFirstID INT NULL, + PlanHandle VARBINARY(64) NULL, + DetailsInt INT NULL, + QueryHash BINARY(8) + ); + + IF OBJECT_ID('tempdb..#WaitStats') IS NOT NULL + DROP TABLE #WaitStats; + CREATE TABLE #WaitStats ( + Pass TINYINT NOT NULL, + wait_type NVARCHAR(60), + wait_time_ms BIGINT, + thread_time_ms FLOAT, + signal_wait_time_ms BIGINT, + waiting_tasks_count BIGINT, + SampleTime datetimeoffset + ); + + IF OBJECT_ID('tempdb..#FileStats') IS NOT NULL + DROP TABLE #FileStats; + CREATE TABLE #FileStats ( + ID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + Pass TINYINT NOT NULL, + SampleTime DATETIMEOFFSET NOT NULL, + DatabaseID INT NOT NULL, + FileID INT NOT NULL, + DatabaseName NVARCHAR(256) , + FileLogicalName NVARCHAR(256) , + TypeDesc NVARCHAR(60) , + SizeOnDiskMB BIGINT , + io_stall_read_ms BIGINT , + num_of_reads BIGINT , + bytes_read BIGINT , + io_stall_write_ms BIGINT , + num_of_writes BIGINT , + bytes_written BIGINT, + PhysicalName NVARCHAR(520) , + avg_stall_read_ms INT , + avg_stall_write_ms INT + ); + + IF OBJECT_ID('tempdb..#QueryStats') IS NOT NULL + DROP TABLE #QueryStats; + CREATE TABLE #QueryStats ( + ID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + Pass INT NOT NULL, + SampleTime DATETIMEOFFSET NOT NULL, + [sql_handle] VARBINARY(64), + statement_start_offset INT, + statement_end_offset INT, + plan_generation_num BIGINT, + plan_handle VARBINARY(64), + execution_count BIGINT, + total_worker_time BIGINT, + total_physical_reads BIGINT, + total_logical_writes BIGINT, + total_logical_reads BIGINT, + total_clr_time BIGINT, + total_elapsed_time BIGINT, + creation_time DATETIMEOFFSET, + query_hash BINARY(8), + query_plan_hash BINARY(8), + Points TINYINT + ); + + IF OBJECT_ID('tempdb..#PerfmonStats') IS NOT NULL + DROP TABLE #PerfmonStats; + CREATE TABLE #PerfmonStats ( + ID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + Pass TINYINT NOT NULL, + SampleTime DATETIMEOFFSET NOT NULL, + [object_name] NVARCHAR(128) NOT NULL, + [counter_name] NVARCHAR(128) NOT NULL, + [instance_name] NVARCHAR(128) NULL, + [cntr_value] BIGINT NULL, + [cntr_type] INT NOT NULL, + [value_delta] BIGINT NULL, + [value_per_second] DECIMAL(18,2) NULL + ); + + IF OBJECT_ID('tempdb..#PerfmonCounters') IS NOT NULL + DROP TABLE #PerfmonCounters; + CREATE TABLE #PerfmonCounters ( + ID INT IDENTITY(1, 1) PRIMARY KEY CLUSTERED, + [object_name] NVARCHAR(128) NOT NULL, + [counter_name] NVARCHAR(128) NOT NULL, + [instance_name] NVARCHAR(128) NULL + ); + + IF OBJECT_ID('tempdb..#FilterPlansByDatabase') IS NOT NULL + DROP TABLE #FilterPlansByDatabase; + CREATE TABLE #FilterPlansByDatabase (DatabaseID INT PRIMARY KEY CLUSTERED); + + IF OBJECT_ID ('tempdb..#checkversion') IS NOT NULL + DROP TABLE #checkversion; + CREATE TABLE #checkversion ( + version NVARCHAR(128), + common_version AS SUBSTRING(version, 1, CHARINDEX('.', version) + 1 ), + major AS PARSENAME(CONVERT(VARCHAR(32), version), 4), + minor AS PARSENAME(CONVERT(VARCHAR(32), version), 3), + build AS PARSENAME(CONVERT(VARCHAR(32), version), 2), + revision AS PARSENAME(CONVERT(VARCHAR(32), version), 1) + ); + + IF OBJECT_ID('tempdb..##WaitCategories') IS NULL + BEGIN + /* We reuse this one by default rather than recreate it every time. */ + CREATE TABLE ##WaitCategories + ( + WaitType NVARCHAR(60) PRIMARY KEY CLUSTERED, + WaitCategory NVARCHAR(128) NOT NULL, + Ignorable BIT DEFAULT 0 + ); + END; /* IF OBJECT_ID('tempdb..##WaitCategories') IS NULL */ + + IF 527 > (SELECT COALESCE(SUM(1),0) FROM ##WaitCategories) + BEGIN + TRUNCATE TABLE ##WaitCategories; + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('ASYNC_IO_COMPLETION','Other Disk IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('ASYNC_NETWORK_IO','Network IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BACKUPIO','Other Disk IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_CONNECTION_RECEIVE_TASK','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_DISPATCHER','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_ENDPOINT_STATE_MUTEX','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_EVENTHANDLER','Service Broker',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_FORWARDER','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_INIT','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_MASTERSTART','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_RECEIVE_WAITFOR','User Wait',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_REGISTERALLENDPOINTS','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_SERVICE','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_SHUTDOWN','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_START','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TASK_SHUTDOWN','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TASK_STOP','Service Broker',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TASK_SUBMIT','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TO_FLUSH','Service Broker',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TRANSMISSION_OBJECT','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TRANSMISSION_TABLE','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TRANSMISSION_WORK','Service Broker',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('BROKER_TRANSMITTER','Service Broker',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CHECKPOINT_QUEUE','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CHKPT','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_AUTO_EVENT','SQL CLR',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_CRST','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_JOIN','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_MANUAL_EVENT','SQL CLR',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_MEMORY_SPY','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_MONITOR','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_RWLOCK_READER','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_RWLOCK_WRITER','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_SEMAPHORE','SQL CLR',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLR_TASK_START','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CLRHOST_STATE_ACCESS','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CMEMPARTITIONED','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CMEMTHREAD','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CXPACKET','Parallelism',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('CXCONSUMER','Parallelism',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DBMIRROR_DBM_EVENT','Mirroring',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DBMIRROR_DBM_MUTEX','Mirroring',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DBMIRROR_EVENTS_QUEUE','Mirroring',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DBMIRROR_SEND','Mirroring',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DBMIRROR_WORKER_QUEUE','Mirroring',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DBMIRRORING_CMD','Mirroring',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DIRTY_PAGE_POLL','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DIRTY_PAGE_TABLE_LOCK','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DISPATCHER_QUEUE_SEMAPHORE','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DPT_ENTRY_LOCK','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTC','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTC_ABORT_REQUEST','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTC_RESOLVE','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTC_STATE','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTC_TMDOWN_REQUEST','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTC_WAITFOR_OUTCOME','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTCNEW_ENLIST','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTCNEW_PREPARE','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTCNEW_RECOVERY','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTCNEW_TM','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTCNEW_TRANSACTION_ENLISTMENT','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('DTCPNTSYNC','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('EE_PMOLOCK','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('EXCHANGE','Parallelism',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('EXTERNAL_SCRIPT_NETWORK_IOF','Network IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FCB_REPLICA_READ','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FCB_REPLICA_WRITE','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_COMPROWSET_RWLOCK','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_IFTS_RWLOCK','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_IFTS_SCHEDULER_IDLE_WAIT','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_IFTSHC_MUTEX','Full Text Search',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_IFTSISM_MUTEX','Full Text Search',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_MASTER_MERGE','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_MASTER_MERGE_COORDINATOR','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_METADATA_MUTEX','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_PROPERTYLIST_CACHE','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FT_RESTART_CRAWL','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('FULLTEXT GATHERER','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_AG_MUTEX','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_AR_CRITICAL_SECTION_ENTRY','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_AR_MANAGER_MUTEX','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_AR_UNLOAD_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_ARCONTROLLER_NOTIFICATIONS_SUBSCRIBER_LIST','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_BACKUP_BULK_LOCK','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_BACKUP_QUEUE','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_CLUSAPI_CALL','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_COMPRESSED_CACHE_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_CONNECTIVITY_INFO','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DATABASE_FLOW_CONTROL','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DATABASE_VERSIONING_STATE','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DATABASE_WAIT_FOR_RECOVERY','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DATABASE_WAIT_FOR_RESTART','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DATABASE_WAIT_FOR_TRANSITION_TO_VERSIONING','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DB_COMMAND','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DB_OP_COMPLETION_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DB_OP_START_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DBR_SUBSCRIBER','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DBR_SUBSCRIBER_FILTER_LIST','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DBSEEDING','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DBSEEDING_LIST','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_DBSTATECHANGE_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FABRIC_CALLBACK','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_BLOCK_FLUSH','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_FILE_CLOSE','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_FILE_REQUEST','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_IOMGR','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_IOMGR_IOCOMPLETION','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_MANAGER','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_FILESTREAM_PREPROC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_GROUP_COMMIT','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_LOGCAPTURE_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_LOGCAPTURE_WAIT','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_LOGPROGRESS_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_NOTIFICATION_DEQUEUE','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_NOTIFICATION_WORKER_EXCLUSIVE_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_NOTIFICATION_WORKER_STARTUP_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_NOTIFICATION_WORKER_TERMINATION_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_PARTNER_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_READ_ALL_NETWORKS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_RECOVERY_WAIT_FOR_CONNECTION','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_RECOVERY_WAIT_FOR_UNDO','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_REPLICAINFO_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SEEDING_CANCELLATION','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SEEDING_FILE_LIST','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SEEDING_LIMIT_BACKUPS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SEEDING_SYNC_COMPLETION','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SEEDING_TIMEOUT_TASK','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SEEDING_WAIT_FOR_COMPLETION','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SYNC_COMMIT','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_SYNCHRONIZING_THROTTLE','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_TDS_LISTENER_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_TDS_LISTENER_SYNC_PROCESSING','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_THROTTLE_LOG_RATE_GOVERNOR','Log Rate Governor',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_TIMER_TASK','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_TRANSPORT_DBRLIST','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_TRANSPORT_FLOW_CONTROL','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_TRANSPORT_SESSION','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_WORK_POOL','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_WORK_QUEUE','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('HADR_XRF_STACK_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('INSTANCE_LOG_RATE_GOVERNOR','Log Rate Governor',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('IO_COMPLETION','Other Disk IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('IO_QUEUE_LIMIT','Other Disk IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('IO_RETRY','Other Disk IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LATCH_DT','Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LATCH_EX','Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LATCH_KP','Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LATCH_NL','Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LATCH_SH','Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LATCH_UP','Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LAZYWRITER_SLEEP','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_BU','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_BU_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_BU_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IS_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IS_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IU','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IU_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IU_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IX','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IX_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_IX_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_NL','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_NL_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_NL_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_S','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_S_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_S_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_U','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_U_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_U_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_X','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_X_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RIn_X_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RS_S','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RS_S_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RS_S_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RS_U','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RS_U_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RS_U_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_S','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_S_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_S_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_U','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_U_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_U_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_X','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_X_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_RX_X_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_S','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_S_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_S_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SCH_M','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SCH_M_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SCH_M_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SCH_S','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SCH_S_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SCH_S_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SIU','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SIU_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SIU_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SIX','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SIX_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_SIX_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_U','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_U_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_U_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_UIX','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_UIX_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_UIX_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_X','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_X_ABORT_BLOCKERS','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LCK_M_X_LOW_PRIORITY','Lock',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOG_RATE_GOVERNOR','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOGBUFFER','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOGMGR','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOGMGR_FLUSH','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOGMGR_PMM_LOG','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOGMGR_QUEUE','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('LOGMGR_RESERVE_APPEND','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('MEMORY_ALLOCATION_EXT','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('MEMORY_GRANT_UPDATE','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('MSQL_XACT_MGR_MUTEX','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('MSQL_XACT_MUTEX','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('MSSEARCH','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('NET_WAITFOR_PACKET','Network IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('ONDEMAND_TASK_QUEUE','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGEIOLATCH_DT','Buffer IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGEIOLATCH_EX','Buffer IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGEIOLATCH_KP','Buffer IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGEIOLATCH_NL','Buffer IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGEIOLATCH_SH','Buffer IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGEIOLATCH_UP','Buffer IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGELATCH_DT','Buffer Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGELATCH_EX','Buffer Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGELATCH_KP','Buffer Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGELATCH_NL','Buffer Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGELATCH_SH','Buffer Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PAGELATCH_UP','Buffer Latch',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_DRAIN_WORKER','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_FLOW_CONTROL','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_LOG_CACHE','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_TRAN_LIST','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_TRAN_TURN','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_WORKER_SYNC','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PARALLEL_REDO_WORKER_WAIT_WORK','Replication',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('POOL_LOG_RATE_GOVERNOR','Log Rate Governor',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('POPULATE_LOCK_ORDINALS','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_ABR','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_CLOSEBACKUPMEDIA','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_CLOSEBACKUPTAPE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_CLOSEBACKUPVDIDEVICE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_CLUSAPI_CLUSTERRESOURCECONTROL','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_COCREATEINSTANCE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_COGETCLASSOBJECT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_CREATEACCESSOR','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_DELETEROWS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_GETCOMMANDTEXT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_GETDATA','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_GETNEXTROWS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_GETRESULT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_GETROWSBYBOOKMARK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBFLUSH','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBLOCKREGION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBREADAT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBSETSIZE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBSTAT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBUNLOCKREGION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_LBWRITEAT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_QUERYINTERFACE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_RELEASE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_RELEASEACCESSOR','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_RELEASEROWS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_RELEASESESSION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_RESTARTPOSITION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_SEQSTRMREAD','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_SEQSTRMREADANDWRITE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_SETDATAFAILURE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_SETPARAMETERINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_SETPARAMETERPROPERTIES','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_STRMLOCKREGION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_STRMSEEKANDREAD','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_STRMSEEKANDWRITE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_STRMSETSIZE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_STRMSTAT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_COM_STRMUNLOCKREGION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_CONSOLEWRITE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_CREATEPARAM','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DEBUG','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSADDLINK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSLINKEXISTCHECK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSLINKHEALTHCHECK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSREMOVELINK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSREMOVEROOT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSROOTFOLDERCHECK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSROOTINIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DFSROOTSHARECHECK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DTC_ABORT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DTC_ABORTREQUESTDONE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DTC_BEGINTRANSACTION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DTC_COMMITREQUESTDONE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DTC_ENLIST','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_DTC_PREPAREREQUESTDONE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_FILESIZEGET','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_FSAOLEDB_ABORTTRANSACTION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_FSAOLEDB_COMMITTRANSACTION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_FSAOLEDB_STARTTRANSACTION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_FSRECOVER_UNCONDITIONALUNDO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_GETRMINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_HADR_LEASE_MECHANISM','Preemptive',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_HTTP_EVENT_WAIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_HTTP_REQUEST','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_LOCKMONITOR','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_MSS_RELEASE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_ODBCOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLE_UNINIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_ABORTORCOMMITTRAN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_ABORTTRAN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_GETDATASOURCE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_GETLITERALINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_GETPROPERTIES','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_GETPROPERTYINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_GETSCHEMALOCK','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_JOINTRANSACTION','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_RELEASE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDB_SETPROPERTIES','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OLEDBOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_ACCEPTSECURITYCONTEXT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_ACQUIRECREDENTIALSHANDLE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_AUTHENTICATIONOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_AUTHORIZATIONOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_AUTHZGETINFORMATIONFROMCONTEXT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_AUTHZINITIALIZECONTEXTFROMSID','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_AUTHZINITIALIZERESOURCEMANAGER','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_BACKUPREAD','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CLOSEHANDLE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CLUSTEROPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_COMOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_COMPLETEAUTHTOKEN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_COPYFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CREATEDIRECTORY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CREATEFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CRYPTACQUIRECONTEXT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CRYPTIMPORTKEY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_CRYPTOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DECRYPTMESSAGE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DELETEFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DELETESECURITYCONTEXT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DEVICEIOCONTROL','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DEVICEOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DIRSVC_NETWORKOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DISCONNECTNAMEDPIPE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DOMAINSERVICESOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DSGETDCNAME','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_DTCOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_ENCRYPTMESSAGE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_FILEOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_FINDFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_FLUSHFILEBUFFERS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_FORMATMESSAGE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_FREECREDENTIALSHANDLE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_FREELIBRARY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GENERICOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETADDRINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETCOMPRESSEDFILESIZE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETDISKFREESPACE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETFILEATTRIBUTES','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETFILESIZE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETFINALFILEPATHBYHANDLE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETLONGPATHNAME','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETPROCADDRESS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETVOLUMENAMEFORVOLUMEMOUNTPOINT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_GETVOLUMEPATHNAME','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_INITIALIZESECURITYCONTEXT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_LIBRARYOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_LOADLIBRARY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_LOGONUSER','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_LOOKUPACCOUNTSID','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_MESSAGEQUEUEOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_MOVEFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETGROUPGETUSERS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETLOCALGROUPGETMEMBERS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETUSERGETGROUPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETUSERGETLOCALGROUPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETUSERMODALSGET','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETVALIDATEPASSWORDPOLICY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_NETVALIDATEPASSWORDPOLICYFREE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_OPENDIRECTORY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_PDH_WMI_INIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_PIPEOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_PROCESSOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_QUERYCONTEXTATTRIBUTES','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_QUERYREGISTRY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_QUERYSECURITYCONTEXTTOKEN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_REMOVEDIRECTORY','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_REPORTEVENT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_REVERTTOSELF','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_RSFXDEVICEOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SECURITYOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SERVICEOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SETENDOFFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SETFILEPOINTER','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SETFILEVALIDDATA','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SETNAMEDSECURITYINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SQLCLROPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_SQMLAUNCH','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_VERIFYSIGNATURE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_VERIFYTRUST','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_VSSOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_WAITFORSINGLEOBJECT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_WINSOCKOPS','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_WRITEFILE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_WRITEFILEGATHER','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_OS_WSASETLASTERROR','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_REENLIST','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_RESIZELOG','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_ROLLFORWARDREDO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_ROLLFORWARDUNDO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SB_STOPENDPOINT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SERVER_STARTUP','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SETRMINFO','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SHAREDMEM_GETDATA','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SNIOPEN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SOSHOST','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SOSTESTING','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_SP_SERVER_DIAGNOSTICS','Preemptive',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_STARTRM','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_STREAMFCB_CHECKPOINT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_STREAMFCB_RECOVER','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_STRESSDRIVER','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_TESTING','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_TRANSIMPORT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_UNMARSHALPROPAGATIONTOKEN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_VSS_CREATESNAPSHOT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_VSS_CREATEVOLUMESNAPSHOT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_CALLBACKEXECUTE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_CX_FILE_OPEN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_CX_HTTP_CALL','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_DISPATCHER','Preemptive',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_ENGINEINIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_GETTARGETSTATE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_SESSIONCOMMIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_TARGETFINALIZE','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_TARGETINIT','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XE_TIMERRUN','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PREEMPTIVE_XETESTING','Preemptive',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_ACTION_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_CHANGE_NOTIFIER_TERMINATION_SYNC','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_CLUSTER_INTEGRATION','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_FAILOVER_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_JOIN','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_OFFLINE_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_ONLINE_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_POST_ONLINE_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_SERVER_READY_CONNECTIONS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADR_WORKITEM_COMPLETED','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_HADRSIM','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('PWAIT_RESOURCE_SEMAPHORE_FT_PARALLEL_QUERY_SYNC','Full Text Search',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('QDS_ASYNC_QUEUE','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('QDS_PERSIST_TASK_MAIN_LOOP_SLEEP','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('QDS_SHUTDOWN_QUEUE','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('QUERY_TRACEOUT','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REDO_THREAD_PENDING_WORK','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPL_CACHE_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPL_HISTORYCACHE_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPL_SCHEMA_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPL_TRANFSINFO_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPL_TRANHASHTABLE_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPL_TRANTEXTINFO_ACCESS','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REPLICA_WRITES','Replication',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('REQUEST_FOR_DEADLOCK_SEARCH','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('RESERVED_MEMORY_ALLOCATION_EXT','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('RESOURCE_SEMAPHORE','Memory',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('RESOURCE_SEMAPHORE_QUERY_COMPILE','Compilation',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_BPOOL_FLUSH','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_BUFFERPOOL_HELPLW','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_DBSTARTUP','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_DCOMSTARTUP','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_MASTERDBREADY','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_MASTERMDREADY','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_MASTERUPGRADED','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_MEMORYPOOL_ALLOCATEPAGES','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_MSDBSTARTUP','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_RETRY_VIRTUALALLOC','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_SYSTEMTASK','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_TASK','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_TEMPDBSTARTUP','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SLEEP_WORKSPACE_ALLOCATEPAGE','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SOS_SCHEDULER_YIELD','CPU',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SOS_WORK_DISPATCHER','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SP_SERVER_DIAGNOSTICS_SLEEP','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLCLR_APPDOMAIN','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLCLR_ASSEMBLY','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLCLR_DEADLOCK_DETECTION','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLCLR_QUANTUM_PUNISHMENT','SQL CLR',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_BUFFER_FLUSH','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_FILE_BUFFER','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_FILE_READ_IO_COMPLETION','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_FILE_WRITE_IO_COMPLETION','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_INCREMENTAL_FLUSH_SLEEP','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_PENDING_BUFFER_WRITERS','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_SHUTDOWN','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('SQLTRACE_WAIT_ENTRIES','Idle',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('THREADPOOL','Worker Thread',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRACE_EVTNOTIF','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRACEWRITE','Tracing',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRAN_MARKLATCH_DT','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRAN_MARKLATCH_EX','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRAN_MARKLATCH_KP','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRAN_MARKLATCH_NL','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRAN_MARKLATCH_SH','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRAN_MARKLATCH_UP','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('TRANSACTION_MUTEX','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('UCS_SESSION_REGISTRATION','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('WAIT_FOR_RESULTS','User Wait',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('WAIT_XTP_OFFLINE_CKPT_NEW_LOG','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('WAITFOR','User Wait',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('WRITE_COMPLETION','Other Disk IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('WRITELOG','Tran Log IO',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XACT_OWN_TRANSACTION','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XACT_RECLAIM_SESSION','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XACTLOCKINFO','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XACTWORKSPACE_MUTEX','Transaction',0); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XE_DISPATCHER_WAIT','Idle',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XE_LIVE_TARGET_TVF','Other',1); + INSERT INTO ##WaitCategories(WaitType, WaitCategory, Ignorable) VALUES ('XE_TIMER_EVENT','Idle',1); + END; /* IF SELECT SUM(1) FROM ##WaitCategories <> 527 */ + + + + IF OBJECT_ID('tempdb..#MasterFiles') IS NOT NULL + DROP TABLE #MasterFiles; + CREATE TABLE #MasterFiles (database_id INT, file_id INT, type_desc NVARCHAR(50), name NVARCHAR(255), physical_name NVARCHAR(255), size BIGINT); + /* Azure SQL Database doesn't have sys.master_files, so we have to build our own. */ + IF (SERVERPROPERTY('EngineEdition') = 5 /*(SERVERPROPERTY('Edition')) = 'SQL Azure' */ + AND (OBJECT_ID('sys.master_files') IS NULL)) + SET @StringToExecute = 'INSERT INTO #MasterFiles (database_id, file_id, type_desc, name, physical_name, size) SELECT DB_ID(), file_id, type_desc, name, physical_name, size FROM sys.database_files;'; + ELSE + SET @StringToExecute = 'INSERT INTO #MasterFiles (database_id, file_id, type_desc, name, physical_name, size) SELECT database_id, file_id, type_desc, name, physical_name, size FROM sys.master_files;'; + EXEC(@StringToExecute); + + IF @FilterPlansByDatabase IS NOT NULL + BEGIN + IF UPPER(LEFT(@FilterPlansByDatabase,4)) = 'USER' + BEGIN + INSERT INTO #FilterPlansByDatabase (DatabaseID) + SELECT database_id + FROM sys.databases + WHERE [name] NOT IN ('master', 'model', 'msdb', 'tempdb'); + END; + ELSE + BEGIN + SET @FilterPlansByDatabase = @FilterPlansByDatabase + ',' + ;WITH a AS + ( + SELECT CAST(1 AS BIGINT) f, CHARINDEX(',', @FilterPlansByDatabase) t, 1 SEQ + UNION ALL + SELECT t + 1, CHARINDEX(',', @FilterPlansByDatabase, t + 1), SEQ + 1 + FROM a + WHERE CHARINDEX(',', @FilterPlansByDatabase, t + 1) > 0 + ) + INSERT #FilterPlansByDatabase (DatabaseID) + SELECT DISTINCT db.database_id + FROM a + INNER JOIN sys.databases db ON LTRIM(RTRIM(SUBSTRING(@FilterPlansByDatabase, a.f, a.t - a.f))) = db.name + WHERE SUBSTRING(@FilterPlansByDatabase, f, t - f) IS NOT NULL + OPTION (MAXRECURSION 0); + END; + END; + + IF OBJECT_ID('tempdb..#ReadableDBs') IS NOT NULL + DROP TABLE #ReadableDBs; + CREATE TABLE #ReadableDBs ( + database_id INT + ); + + IF EXISTS (SELECT * FROM sys.all_objects o WHERE o.name = 'dm_hadr_database_replica_states') + BEGIN + RAISERROR('Checking for Read intent databases to exclude',0,0) WITH NOWAIT; + + SET @StringToExecute = 'INSERT INTO #ReadableDBs (database_id) SELECT DBs.database_id FROM sys.databases DBs INNER JOIN sys.availability_replicas Replicas ON DBs.replica_id = Replicas.replica_id WHERE replica_server_name NOT IN (SELECT DISTINCT primary_replica FROM sys.dm_hadr_availability_group_states States) AND Replicas.secondary_role_allow_connections_desc = ''READ_ONLY'' AND replica_server_name = @@SERVERNAME;'; + EXEC(@StringToExecute); + + END + + DECLARE @v DECIMAL(6,2), + @build INT, + @memGrantSortSupported BIT = 1; + + RAISERROR (N'Determining SQL Server version.',0,1) WITH NOWAIT; + + INSERT INTO #checkversion (version) + SELECT CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)) + OPTION (RECOMPILE); + + + SELECT @v = common_version , + @build = build + FROM #checkversion + OPTION (RECOMPILE); + + IF (@v < 11) + OR (@v = 11 AND @build < 6020) + OR (@v = 12 AND @build < 5000) + OR (@v = 13 AND @build < 1601) + SET @memGrantSortSupported = 0; + + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'dm_exec_query_statistics_xml') + AND ((@v = 13 AND @build >= 5337) /* This DMF causes assertion errors: https://support.microsoft.com/en-us/help/4490136/fix-assertion-error-occurs-when-you-use-sys-dm-exec-query-statistics-x */ + OR (@v = 14 AND @build >= 3162) + OR (@v >= 15) + OR (@v <= 12)) /* Azure */ + SET @dm_exec_query_statistics_xml = 1; + + + SET @StockWarningHeader = '', + @StockDetailsHeader = @StockDetailsHeader + ''; + + /* Get the instance name to use as a Perfmon counter prefix. */ + IF SERVERPROPERTY('EngineEdition') IN (5, 8) /*CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) = 'SQL Azure'*/ + SELECT TOP 1 @ServiceName = LEFT(object_name, (CHARINDEX(':', object_name) - 1)) + FROM sys.dm_os_performance_counters; + ELSE + BEGIN + SET @StringToExecute = 'INSERT INTO #PerfmonStats(object_name, Pass, SampleTime, counter_name, cntr_type) SELECT CASE WHEN @@SERVICENAME = ''MSSQLSERVER'' THEN ''SQLServer'' ELSE ''MSSQL$'' + @@SERVICENAME END, 0, SYSDATETIMEOFFSET(), ''stuffing'', 0 ;'; + EXEC(@StringToExecute); + SELECT @ServiceName = object_name FROM #PerfmonStats; + DELETE #PerfmonStats; + END; + + /* Build a list of queries that were run in the last 10 seconds. + We're looking for the death-by-a-thousand-small-cuts scenario + where a query is constantly running, and it doesn't have that + big of an impact individually, but it has a ton of impact + overall. We're going to build this list, and then after we + finish our @Seconds sample, we'll compare our plan cache to + this list to see what ran the most. */ + + /* Populate #QueryStats. SQL 2005 doesn't have query hash or query plan hash. */ + IF @CheckProcedureCache = 1 + BEGIN + RAISERROR('@CheckProcedureCache = 1, capturing first pass of plan cache',10,1) WITH NOWAIT; + IF @@VERSION LIKE 'Microsoft SQL Server 2005%' + BEGIN + IF @FilterPlansByDatabase IS NULL + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 1 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, NULL AS query_hash, NULL AS query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + WHERE qs.last_execution_time >= (DATEADD(ss, -10, SYSDATETIMEOFFSET()));'; + END; + ELSE + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 1 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, NULL AS query_hash, NULL AS query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS attr + INNER JOIN #FilterPlansByDatabase dbs ON CAST(attr.value AS INT) = dbs.DatabaseID + WHERE qs.last_execution_time >= (DATEADD(ss, -10, SYSDATETIMEOFFSET())) + AND attr.attribute = ''dbid'';'; + END; + END; + ELSE + BEGIN + IF @FilterPlansByDatabase IS NULL + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 1 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + WHERE qs.last_execution_time >= (DATEADD(ss, -10, SYSDATETIMEOFFSET()));'; + END; + ELSE + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 1 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS attr + INNER JOIN #FilterPlansByDatabase dbs ON CAST(attr.value AS INT) = dbs.DatabaseID + WHERE qs.last_execution_time >= (DATEADD(ss, -10, SYSDATETIMEOFFSET())) + AND attr.attribute = ''dbid'';'; + END; + END; + EXEC(@StringToExecute); + + /* Get the totals for the entire plan cache */ + INSERT INTO #QueryStats (Pass, SampleTime, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time) + SELECT -1 AS Pass, SYSDATETIMEOFFSET(), SUM(execution_count), SUM(total_worker_time), SUM(total_physical_reads), SUM(total_logical_writes), SUM(total_logical_reads), SUM(total_clr_time), SUM(total_elapsed_time), MIN(creation_time) + FROM sys.dm_exec_query_stats qs; + END; /*IF @CheckProcedureCache = 1 */ + + + IF EXISTS (SELECT * + FROM tempdb.sys.all_objects obj + INNER JOIN tempdb.sys.all_columns col1 ON obj.object_id = col1.object_id AND col1.name = 'object_name' + INNER JOIN tempdb.sys.all_columns col2 ON obj.object_id = col2.object_id AND col2.name = 'counter_name' + INNER JOIN tempdb.sys.all_columns col3 ON obj.object_id = col3.object_id AND col3.name = 'instance_name' + WHERE obj.name LIKE '%CustomPerfmonCounters%') + BEGIN + SET @StringToExecute = 'INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) SELECT [object_name],[counter_name],[instance_name] FROM #CustomPerfmonCounters'; + EXEC(@StringToExecute); + END; + ELSE + BEGIN + /* Add our default Perfmon counters */ + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Forwarded Records/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Page compression attempts/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Page Splits/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Skipped Ghosted Records/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Table Lock Escalations/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Worktables Created/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Group','Active Hadr Threads','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Bytes Received from Replica/sec','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Bytes Sent to Replica/sec','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Bytes Sent to Transport/sec','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Flow Control Time (ms/sec)','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Flow Control/sec','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Resent Messages/sec','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Availability Replica','Sends to Replica/sec','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Page life expectancy', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Page reads/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Page writes/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Readahead pages/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Target pages', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Total pages', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Active Transactions','_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Database Flow Control Delay', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Database Flow Controls/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Group Commit Time', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Group Commits/Sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Log Apply Pending Queue', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Log Apply Ready Queue', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Log Compression Cache misses/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Log remaining for undo', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Log Send Queue', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Recovery Queue', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Redo blocked/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Redo Bytes Remaining', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Database Replica','Redone Bytes/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','Log Bytes Flushed/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','Log Growths', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','Log Pool LogWriter Pushes/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','Log Shrinks', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','Transactions/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','Write Transactions/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Databases','XTP Memory Used (KB)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Exec Statistics','Distributed Query', 'Execs in progress'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Exec Statistics','DTC calls', 'Execs in progress'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Exec Statistics','Extended Procedures', 'Execs in progress'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Exec Statistics','OLEDB calls', 'Execs in progress'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':General Statistics','Active Temp Tables', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':General Statistics','Logins/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':General Statistics','Logouts/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':General Statistics','Mars Deadlocks', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':General Statistics','Processes blocked', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Number of Deadlocks/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Memory Manager','Memory Grants Pending', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Errors','Errors/sec', '_Total'); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Batch Requests/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Forced Parameterizations/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Guided plan executions/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','SQL Attention rate', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','SQL Compilations/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','SQL Re-Compilations/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Workload Group Stats','Query optimizations/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Workload Group Stats','Suboptimal plans/sec',NULL); + /* Below counters added by Jefferson Elias */ + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Worktables From Cache Base',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Worktables From Cache Ratio',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Database pages',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Free pages',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Stolen pages',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Memory Manager','Granted Workspace Memory (KB)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Memory Manager','Maximum Workspace Memory (KB)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Memory Manager','Target Server Memory (KB)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Memory Manager','Total Server Memory (KB)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Buffer cache hit ratio',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Buffer cache hit ratio base',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Checkpoint pages/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Free list stalls/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Lazy writes/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Auto-Param Attempts/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Failed Auto-Params/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Safe Auto-Params/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','Unsafe Auto-Params/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Workfiles Created/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':General Statistics','User Connections',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Latches','Average Latch Wait Time (ms)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Latches','Average Latch Wait Time Base',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Latches','Latch Waits/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Latches','Total Latch Wait Time (ms)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Average Wait Time (ms)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Average Wait Time Base',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Lock Requests/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Lock Timeouts/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Lock Wait Time (ms)',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Locks','Lock Waits/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Transactions','Longest Transaction Running Time',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Full Scans/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Access Methods','Index Searches/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Buffer Manager','Page lookups/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':Cursor Manager by Type','Active cursors',NULL); + /* Below counters are for In-Memory OLTP (Hekaton), which have a different naming convention. + And yes, they actually hard-coded the version numbers into the counters, and SQL 2019 still says 2017, oddly. + For why, see: https://connect.microsoft.com/SQLServer/feedback/details/817216/xtp-perfmon-counters-should-appear-under-sql-server-perfmon-counter-group + */ + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Cursors','Expired rows removed/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Cursors','Expired rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Garbage Collection','Rows processed/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP IO Governor','Io Issued/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Phantom Processor','Phantom expired rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Phantom Processor','Phantom rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Transaction Log','Log bytes written/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Transaction Log','Log records written/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Transactions','Transactions aborted by user/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Transactions','Transactions aborted/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2014 XTP Transactions','Transactions created/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Cursors','Expired rows removed/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Cursors','Expired rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Garbage Collection','Rows processed/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP IO Governor','Io Issued/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Phantom Processor','Phantom expired rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Phantom Processor','Phantom rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Transaction Log','Log bytes written/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Transaction Log','Log records written/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Transactions','Transactions aborted by user/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Transactions','Transactions aborted/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2016 XTP Transactions','Transactions created/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Cursors','Expired rows removed/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Cursors','Expired rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Garbage Collection','Rows processed/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP IO Governor','Io Issued/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Phantom Processor','Phantom expired rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Phantom Processor','Phantom rows touched/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Transaction Log','Log bytes written/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Transaction Log','Log records written/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Transactions','Transactions aborted by user/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Transactions','Transactions aborted/sec',NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES ('SQL Server 2017 XTP Transactions','Transactions created/sec',NULL); + END; + + + IF @total_cpu_usage IN (0, 1) + BEGIN + EXEC sys.sp_executesql + @get_thread_time_ms, + N'@thread_time_ms FLOAT OUTPUT', + @thread_time_ms OUTPUT; + END + + /* Populate #FileStats, #PerfmonStats, #WaitStats with DMV data. + After we finish doing our checks, we'll take another sample and compare them. */ + RAISERROR('Capturing first pass of wait stats, perfmon counters, file stats',10,1) WITH NOWAIT; + SET @StringToExecute = N' + INSERT #WaitStats(Pass, SampleTime, wait_type, wait_time_ms, thread_time_ms, signal_wait_time_ms, waiting_tasks_count) + SELECT + x.Pass, + x.SampleTime, + x.wait_type, + SUM(x.sum_wait_time_ms) AS sum_wait_time_ms, + CASE @Seconds + WHEN 0 + THEN 0 + ELSE @thread_time_ms + END AS thread_time_ms, + SUM(x.sum_signal_wait_time_ms) AS sum_signal_wait_time_ms, + SUM(x.sum_waiting_tasks) AS sum_waiting_tasks + FROM ( + SELECT + 1 AS Pass, + CASE @Seconds WHEN 0 THEN @StartSampleTime ELSE SYSDATETIMEOFFSET() END AS SampleTime, + owt.wait_type, + CASE @Seconds WHEN 0 THEN 0 ELSE SUM(owt.wait_duration_ms) OVER (PARTITION BY owt.wait_type, owt.session_id) + - CASE WHEN @Seconds = 0 THEN 0 ELSE (@Seconds * 1000) END END AS sum_wait_time_ms, + 0 AS sum_signal_wait_time_ms, + 0 AS sum_waiting_tasks + FROM sys.dm_os_waiting_tasks owt + WHERE owt.session_id > 50 + AND owt.wait_duration_ms >= CASE @Seconds WHEN 0 THEN 0 ELSE @Seconds * 1000 END + UNION ALL + SELECT + 1 AS Pass, + CASE @Seconds WHEN 0 THEN @StartSampleTime ELSE SYSDATETIMEOFFSET() END AS SampleTime, + os.wait_type, + CASE @Seconds WHEN 0 THEN 0 ELSE SUM(os.wait_time_ms) OVER (PARTITION BY os.wait_type) END AS sum_wait_time_ms, + CASE @Seconds WHEN 0 THEN 0 ELSE SUM(os.signal_wait_time_ms) OVER (PARTITION BY os.wait_type ) END AS sum_signal_wait_time_ms, + CASE @Seconds WHEN 0 THEN 0 ELSE SUM(os.waiting_tasks_count) OVER (PARTITION BY os.wait_type) END AS sum_waiting_tasks '; + + IF SERVERPROPERTY('EngineEdition') = 5 /*SERVERPROPERTY('Edition') = 'SQL Azure'*/ + SET @StringToExecute = @StringToExecute + N' FROM sys.dm_db_wait_stats os '; + ELSE + SET @StringToExecute = @StringToExecute + N' FROM sys.dm_os_wait_stats os '; + + SET @StringToExecute = @StringToExecute + N' + ) x + WHERE NOT EXISTS + ( + SELECT * + FROM ##WaitCategories AS wc + WHERE wc.WaitType = x.wait_type + AND wc.Ignorable = 1 + ) + GROUP BY x.Pass, x.SampleTime, x.wait_type + ORDER BY sum_wait_time_ms DESC;' + + EXEC sys.sp_executesql + @StringToExecute, + N'@StartSampleTime DATETIMEOFFSET, + @Seconds INT, + @thread_time_ms FLOAT', + @StartSampleTime, + @Seconds, + @thread_time_ms; + + WITH w AS + ( + SELECT + total_waits = + CONVERT + ( + FLOAT, + SUM(ws.wait_time_ms) + ) + FROM #WaitStats AS ws + WHERE Pass = 1 + ) + UPDATE ws + SET ws.thread_time_ms += w.total_waits + FROM #WaitStats AS ws + CROSS JOIN w + WHERE ws.Pass = 1 + OPTION(RECOMPILE); + + INSERT INTO #FileStats (Pass, SampleTime, DatabaseID, FileID, DatabaseName, FileLogicalName, SizeOnDiskMB, io_stall_read_ms , + num_of_reads, [bytes_read] , io_stall_write_ms,num_of_writes, [bytes_written], PhysicalName, TypeDesc) + SELECT + 1 AS Pass, + CASE @Seconds WHEN 0 THEN @StartSampleTime ELSE SYSDATETIMEOFFSET() END AS SampleTime, + mf.[database_id], + mf.[file_id], + DB_NAME(vfs.database_id) AS [db_name], + mf.name + N' [' + mf.type_desc COLLATE SQL_Latin1_General_CP1_CI_AS + N']' AS file_logical_name , + CAST(( ( vfs.size_on_disk_bytes / 1024.0 ) / 1024.0 ) AS INT) AS size_on_disk_mb , + CASE @Seconds WHEN 0 THEN 0 ELSE vfs.io_stall_read_ms END , + CASE @Seconds WHEN 0 THEN 0 ELSE vfs.num_of_reads END , + CASE @Seconds WHEN 0 THEN 0 ELSE vfs.[num_of_bytes_read] END , + CASE @Seconds WHEN 0 THEN 0 ELSE vfs.io_stall_write_ms END , + CASE @Seconds WHEN 0 THEN 0 ELSE vfs.num_of_writes END , + CASE @Seconds WHEN 0 THEN 0 ELSE vfs.[num_of_bytes_written] END , + mf.physical_name, + mf.type_desc + FROM sys.dm_io_virtual_file_stats (NULL, NULL) AS vfs + INNER JOIN #MasterFiles AS mf ON vfs.file_id = mf.file_id + AND vfs.database_id = mf.database_id + WHERE vfs.num_of_reads > 0 + OR vfs.num_of_writes > 0; + + INSERT INTO #PerfmonStats (Pass, SampleTime, [object_name],[counter_name],[instance_name],[cntr_value],[cntr_type]) + SELECT 1 AS Pass, + CASE @Seconds WHEN 0 THEN @StartSampleTime ELSE SYSDATETIMEOFFSET() END AS SampleTime, RTRIM(dmv.object_name), RTRIM(dmv.counter_name), RTRIM(dmv.instance_name), CASE @Seconds WHEN 0 THEN 0 ELSE dmv.cntr_value END, dmv.cntr_type + FROM #PerfmonCounters counters + INNER JOIN sys.dm_os_performance_counters dmv ON counters.counter_name COLLATE SQL_Latin1_General_CP1_CI_AS = RTRIM(dmv.counter_name) COLLATE SQL_Latin1_General_CP1_CI_AS + AND counters.[object_name] COLLATE SQL_Latin1_General_CP1_CI_AS = RTRIM(dmv.[object_name]) COLLATE SQL_Latin1_General_CP1_CI_AS + AND (counters.[instance_name] IS NULL OR counters.[instance_name] COLLATE SQL_Latin1_General_CP1_CI_AS = RTRIM(dmv.[instance_name]) COLLATE SQL_Latin1_General_CP1_CI_AS); + + /* For Github #2743: */ + CREATE TABLE #TempdbOperationalStats (object_id BIGINT PRIMARY KEY CLUSTERED, + forwarded_fetch_count BIGINT); + INSERT INTO #TempdbOperationalStats (object_id, forwarded_fetch_count) + SELECT object_id, forwarded_fetch_count + FROM tempdb.sys.dm_db_index_operational_stats(DB_ID('tempdb'), NULL, NULL, NULL) os + WHERE os.database_id = DB_ID('tempdb') + AND os.forwarded_fetch_count > 100; + + + /* If they want to run sp_BlitzWho and export to table, go for it. */ + IF @OutputTableNameBlitzWho IS NOT NULL + AND @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + RAISERROR('Logging sp_BlitzWho to table',10,1) WITH NOWAIT; + EXEC sp_BlitzWho @OutputDatabaseName = @UnquotedOutputDatabaseName, @OutputSchemaName = @UnquotedOutputSchemaName, @OutputTableName = @OutputTableNameBlitzWho, @CheckDateOverride = @StartSampleTime, @OutputTableRetentionDays = @OutputTableRetentionDays; + END + + RAISERROR('Beginning investigatory queries',10,1) WITH NOWAIT; + + + /* Maintenance Tasks Running - Backup Running - CheckID 1 */ + IF @Seconds > 0 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 1',10,1) WITH NOWAIT; + END + + IF EXISTS(SELECT * FROM sys.dm_exec_requests WHERE total_elapsed_time > 300000 AND command LIKE 'BACKUP%') + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, QueryHash) + SELECT 1 AS CheckID, + 1 AS Priority, + 'Maintenance Tasks Running' AS FindingGroup, + 'Backup Running' AS Finding, + 'https://www.brentozar.com/askbrent/backups/' AS URL, + 'Backup of ' + DB_NAME(db.resource_database_id) + ' database (' + (SELECT CAST(CAST(SUM(size * 8.0 / 1024 / 1024) AS BIGINT) AS NVARCHAR) FROM #MasterFiles WHERE database_id = db.resource_database_id) + 'GB) ' + @LineFeed + + CAST(r.percent_complete AS NVARCHAR(100)) + '% complete, has been running since ' + CAST(r.start_time AS NVARCHAR(100)) + '. ' + @LineFeed + + CASE WHEN COALESCE(s.nt_username, s.loginame) IS NOT NULL THEN (' Login: ' + COALESCE(s.nt_username, s.loginame) + ' ') ELSE '' END AS Details, + 'KILL ' + CAST(r.session_id AS NVARCHAR(100)) + ';' AS HowToStopIt, + pl.query_plan AS QueryPlan, + r.start_time AS StartTime, + s.loginame AS LoginName, + s.nt_username AS NTUserName, + s.[program_name] AS ProgramName, + s.[hostname] AS HostName, + db.[resource_database_id] AS DatabaseID, + DB_NAME(db.resource_database_id) AS DatabaseName, + 0 AS OpenTransactionCount, + r.query_hash + FROM sys.dm_exec_requests r + INNER JOIN sys.dm_exec_connections c ON r.session_id = c.session_id + INNER JOIN sys.sysprocesses AS s ON r.session_id = s.spid AND s.ecid = 0 + INNER JOIN + ( + SELECT DISTINCT + t.request_session_id, + t.resource_database_id + FROM sys.dm_tran_locks AS t + WHERE t.resource_type = N'DATABASE' + AND t.request_mode = N'S' + AND t.request_status = N'GRANT' + AND t.request_owner_type = N'SHARED_TRANSACTION_WORKSPACE' + ) AS db ON s.spid = db.request_session_id AND s.dbid = db.resource_database_id + CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) pl + WHERE r.command LIKE 'BACKUP%' + AND r.start_time <= DATEADD(minute, -5, GETDATE()) + AND r.database_id NOT IN (SELECT database_id FROM #ReadableDBs); + END + + /* If there's a backup running, add details explaining how long full backup has been taking in the last month. */ + IF @Seconds > 0 AND SERVERPROPERTY('EngineEdition') <> 5 /*CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) <> 'SQL Azure'*/ + BEGIN + SET @StringToExecute = 'UPDATE #BlitzFirstResults SET Details = Details + '' Over the last 60 days, the full backup usually takes '' + CAST((SELECT AVG(DATEDIFF(mi, bs.backup_start_date, bs.backup_finish_date)) FROM msdb.dbo.backupset bs WHERE abr.DatabaseName = bs.database_name AND bs.type = ''D'' AND bs.backup_start_date > DATEADD(dd, -60, SYSDATETIMEOFFSET()) AND bs.backup_finish_date IS NOT NULL) AS NVARCHAR(100)) + '' minutes.'' FROM #BlitzFirstResults abr WHERE abr.CheckID = 1 AND EXISTS (SELECT * FROM msdb.dbo.backupset bs WHERE bs.type = ''D'' AND bs.backup_start_date > DATEADD(dd, -60, SYSDATETIMEOFFSET()) AND bs.backup_finish_date IS NOT NULL AND abr.DatabaseName = bs.database_name AND DATEDIFF(mi, bs.backup_start_date, bs.backup_finish_date) > 1)'; + EXEC(@StringToExecute); + END; + + + /* Maintenance Tasks Running - DBCC CHECK* Running - CheckID 2 */ + IF @Seconds > 0 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 2',10,1) WITH NOWAIT; + END + + IF EXISTS (SELECT * FROM sys.dm_exec_requests WHERE command LIKE 'DBCC%' AND total_elapsed_time > 5000) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, QueryHash) + SELECT 2 AS CheckID, + 1 AS Priority, + 'Maintenance Tasks Running' AS FindingGroup, + 'DBCC CHECK* Running' AS Finding, + 'https://www.brentozar.com/askbrent/dbcc/' AS URL, + 'Corruption check of ' + DB_NAME(db.resource_database_id) + ' database (' + (SELECT CAST(CAST(SUM(size * 8.0 / 1024 / 1024) AS BIGINT) AS NVARCHAR) FROM #MasterFiles WHERE database_id = db.resource_database_id) + 'GB) has been running since ' + CAST(r.start_time AS NVARCHAR(100)) + '. ' AS Details, + 'KILL ' + CAST(r.session_id AS NVARCHAR(100)) + ';' AS HowToStopIt, + pl.query_plan AS QueryPlan, + r.start_time AS StartTime, + s.login_name AS LoginName, + s.nt_user_name AS NTUserName, + s.[program_name] AS ProgramName, + s.[host_name] AS HostName, + db.[resource_database_id] AS DatabaseID, + DB_NAME(db.resource_database_id) AS DatabaseName, + 0 AS OpenTransactionCount, + r.query_hash + FROM sys.dm_exec_requests r + INNER JOIN sys.dm_exec_connections c ON r.session_id = c.session_id + INNER JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id + INNER JOIN (SELECT DISTINCT l.request_session_id, l.resource_database_id + FROM sys.dm_tran_locks l + INNER JOIN sys.databases d ON l.resource_database_id = d.database_id + WHERE l.resource_type = N'DATABASE' + AND l.request_mode = N'S' + AND l.request_status = N'GRANT' + AND l.request_owner_type = N'SHARED_TRANSACTION_WORKSPACE') AS db ON s.session_id = db.request_session_id + OUTER APPLY sys.dm_exec_query_plan(r.plan_handle) pl + OUTER APPLY sys.dm_exec_sql_text(r.sql_handle) AS t + WHERE r.command LIKE 'DBCC%' + AND CAST(t.text AS NVARCHAR(4000)) NOT LIKE '%dm_db_index_physical_stats%' + AND CAST(t.text AS NVARCHAR(4000)) NOT LIKE '%ALTER INDEX%' + AND CAST(t.text AS NVARCHAR(4000)) NOT LIKE '%fileproperty%' + AND r.database_id NOT IN (SELECT database_id FROM #ReadableDBs); + END + + /* Maintenance Tasks Running - Restore Running - CheckID 3 */ + IF @Seconds > 0 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 3',10,1) WITH NOWAIT; + END + + IF EXISTS (SELECT * FROM sys.dm_exec_requests WHERE command LIKE 'RESTORE%' AND total_elapsed_time > 5000) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, QueryHash) + SELECT 3 AS CheckID, + 1 AS Priority, + 'Maintenance Tasks Running' AS FindingGroup, + 'Restore Running' AS Finding, + 'https://www.brentozar.com/askbrent/backups/' AS URL, + 'Restore of ' + DB_NAME(db.resource_database_id) + ' database (' + (SELECT CAST(CAST(SUM(size * 8.0 / 1024 / 1024) AS BIGINT) AS NVARCHAR) FROM #MasterFiles WHERE database_id = db.resource_database_id) + 'GB) is ' + CAST(r.percent_complete AS NVARCHAR(100)) + '% complete, has been running since ' + CAST(r.start_time AS NVARCHAR(100)) + '. ' AS Details, + 'KILL ' + CAST(r.session_id AS NVARCHAR(100)) + ';' AS HowToStopIt, + pl.query_plan AS QueryPlan, + r.start_time AS StartTime, + s.login_name AS LoginName, + s.nt_user_name AS NTUserName, + s.[program_name] AS ProgramName, + s.[host_name] AS HostName, + db.[resource_database_id] AS DatabaseID, + DB_NAME(db.resource_database_id) AS DatabaseName, + 0 AS OpenTransactionCount, + r.query_hash + FROM sys.dm_exec_requests r + INNER JOIN sys.dm_exec_connections c ON r.session_id = c.session_id + INNER JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id + INNER JOIN ( + SELECT DISTINCT request_session_id, resource_database_id + FROM sys.dm_tran_locks + WHERE resource_type = N'DATABASE' + AND request_mode = N'S' + AND request_status = N'GRANT') AS db ON s.session_id = db.request_session_id + CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) pl + WHERE r.command LIKE 'RESTORE%' + AND s.program_name <> 'SQL Server Log Shipping' + AND r.database_id NOT IN (SELECT database_id FROM #ReadableDBs); + END + + /* SQL Server Internal Maintenance - Database File Growing - CheckID 4 */ + IF @Seconds > 0 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 4',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount) + SELECT 4 AS CheckID, + 1 AS Priority, + 'SQL Server Internal Maintenance' AS FindingGroup, + 'Database File Growing' AS Finding, + 'https://www.brentozar.com/go/instant' AS URL, + 'SQL Server is waiting for Windows to provide storage space for a database restore, a data file growth, or a log file growth. This task has been running since ' + CAST(r.start_time AS NVARCHAR(100)) + '.' + @LineFeed + 'Check the query plan (expert mode) to identify the database involved.' AS Details, + 'Unfortunately, you can''t stop this, but you can prevent it next time. Check out https://www.brentozar.com/go/instant for details.' AS HowToStopIt, + pl.query_plan AS QueryPlan, + r.start_time AS StartTime, + s.login_name AS LoginName, + s.nt_user_name AS NTUserName, + s.[program_name] AS ProgramName, + s.[host_name] AS HostName, + NULL AS DatabaseID, + NULL AS DatabaseName, + 0 AS OpenTransactionCount + FROM sys.dm_os_waiting_tasks t + INNER JOIN sys.dm_exec_connections c ON t.session_id = c.session_id + INNER JOIN sys.dm_exec_requests r ON t.session_id = r.session_id + INNER JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id + CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) pl + WHERE t.wait_type = 'PREEMPTIVE_OS_WRITEFILEGATHER' + AND r.database_id NOT IN (SELECT database_id FROM #ReadableDBs); + END + + /* Query Problems - Long-Running Query Blocking Others - CheckID 5 */ + IF SERVERPROPERTY('EngineEdition') <> 5 /*SERVERPROPERTY('Edition') <> 'SQL Azure'*/ AND @Seconds > 0 AND EXISTS(SELECT * FROM sys.dm_os_waiting_tasks WHERE wait_type LIKE 'LCK%' AND wait_duration_ms > 30000) + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 5',10,1) WITH NOWAIT; + END + + SET @StringToExecute = N'INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, QueryText, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, QueryHash) + SELECT 5 AS CheckID, + 1 AS Priority, + ''Query Problems'' AS FindingGroup, + ''Long-Running Query Blocking Others'' AS Finding, + ''https://www.brentozar.com/go/blocking'' AS URL, + ''Query in '' + COALESCE(DB_NAME(COALESCE((SELECT TOP 1 dbid FROM sys.dm_exec_sql_text(r.sql_handle)), + (SELECT TOP 1 t.dbid FROM master..sysprocesses spBlocker CROSS APPLY sys.dm_exec_sql_text(spBlocker.sql_handle) t WHERE spBlocker.spid = tBlocked.blocking_session_id))), ''(Unknown)'') + '' has a last request start time of '' + CAST(s.last_request_start_time AS NVARCHAR(100)) + ''. Query follows: ' + + @LineFeed + @LineFeed + + '''+ CAST(COALESCE((SELECT TOP 1 [text] FROM sys.dm_exec_sql_text(r.sql_handle)), + (SELECT TOP 1 [text] FROM master..sysprocesses spBlocker CROSS APPLY sys.dm_exec_sql_text(spBlocker.sql_handle) WHERE spBlocker.spid = tBlocked.blocking_session_id), '''') AS NVARCHAR(2000)) AS Details, + ''KILL '' + CAST(tBlocked.blocking_session_id AS NVARCHAR(100)) + '';'' AS HowToStopIt, + (SELECT TOP 1 query_plan FROM sys.dm_exec_query_plan(r.plan_handle)) AS QueryPlan, + COALESCE((SELECT TOP 1 [text] FROM sys.dm_exec_sql_text(r.sql_handle)), + (SELECT TOP 1 [text] FROM master..sysprocesses spBlocker CROSS APPLY sys.dm_exec_sql_text(spBlocker.sql_handle) WHERE spBlocker.spid = tBlocked.blocking_session_id)) AS QueryText, + r.start_time AS StartTime, + s.login_name AS LoginName, + s.nt_user_name AS NTUserName, + s.[program_name] AS ProgramName, + s.[host_name] AS HostName, + r.[database_id] AS DatabaseID, + DB_NAME(r.database_id) AS DatabaseName, + 0 AS OpenTransactionCount, + r.query_hash + FROM sys.dm_os_waiting_tasks tBlocked + INNER JOIN sys.dm_exec_sessions s ON tBlocked.blocking_session_id = s.session_id + LEFT OUTER JOIN sys.dm_exec_requests r ON s.session_id = r.session_id + INNER JOIN sys.dm_exec_connections c ON s.session_id = c.session_id + WHERE tBlocked.wait_type LIKE ''LCK%'' AND tBlocked.wait_duration_ms > 30000 + /* And the blocking session ID is not blocked by anyone else: */ + AND NOT EXISTS(SELECT * FROM sys.dm_os_waiting_tasks tBlocking WHERE s.session_id = tBlocking.session_id AND tBlocking.session_id <> tBlocking.blocking_session_id AND tBlocking.blocking_session_id IS NOT NULL) + AND r.database_id NOT IN (SELECT database_id FROM #ReadableDBs);'; + EXECUTE sp_executesql @StringToExecute; + END; + + /* Query Problems - Plan Cache Erased Recently - CheckID 7 */ + IF DATEADD(mi, -15, SYSDATETIME()) < (SELECT TOP 1 creation_time FROM sys.dm_exec_query_stats ORDER BY creation_time) + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 7',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT TOP 1 7 AS CheckID, + 50 AS Priority, + 'Query Problems' AS FindingGroup, + 'Plan Cache Erased Recently' AS Finding, + 'https://www.brentozar.com/askbrent/plan-cache-erased-recently/' AS URL, + 'The oldest query in the plan cache was created at ' + CAST(creation_time AS NVARCHAR(50)) + '. ' + @LineFeed + @LineFeed + + 'This indicates that someone ran DBCC FREEPROCCACHE at that time,' + @LineFeed + + 'Giving SQL Server temporary amnesia. Now, as queries come in,' + @LineFeed + + 'SQL Server has to use a lot of CPU power in order to build execution' + @LineFeed + + 'plans and put them in cache again. This causes high CPU loads.' AS Details, + 'Find who did that, and stop them from doing it again.' AS HowToStopIt + FROM sys.dm_exec_query_stats + ORDER BY creation_time; + END; + + + /* Query Problems - Sleeping Query with Open Transactions - CheckID 8 */ + IF @Seconds > 0 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 8',10,1) WITH NOWAIT; + END + + IF EXISTS (SELECT * FROM sys.dm_exec_requests WHERE total_elapsed_time > 5000 AND request_id > 0) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, QueryText, OpenTransactionCount) + SELECT 8 AS CheckID, + 50 AS Priority, + 'Query Problems' AS FindingGroup, + 'Sleeping Query with Open Transactions' AS Finding, + 'https://www.brentozar.com/askbrent/sleeping-query-with-open-transactions/' AS URL, + 'Database: ' + DB_NAME(db.resource_database_id) + @LineFeed + 'Host: ' + s.hostname + @LineFeed + 'Program: ' + s.[program_name] + @LineFeed + 'Asleep with open transactions and locks since ' + CAST(s.last_batch AS NVARCHAR(100)) + '. ' AS Details, + 'KILL ' + CAST(s.spid AS NVARCHAR(100)) + ';' AS HowToStopIt, + s.last_batch AS StartTime, + s.loginame AS LoginName, + s.nt_username AS NTUserName, + s.[program_name] AS ProgramName, + s.hostname AS HostName, + db.[resource_database_id] AS DatabaseID, + DB_NAME(db.resource_database_id) AS DatabaseName, + (SELECT TOP 1 [text] FROM sys.dm_exec_sql_text(c.most_recent_sql_handle)) AS QueryText, + s.open_tran AS OpenTransactionCount + FROM sys.sysprocesses s + INNER JOIN sys.dm_exec_connections c ON s.spid = c.session_id + INNER JOIN ( + SELECT DISTINCT request_session_id, resource_database_id + FROM sys.dm_tran_locks + WHERE resource_type = N'DATABASE' + AND request_mode = N'S' + AND request_status = N'GRANT' + AND request_owner_type = N'SHARED_TRANSACTION_WORKSPACE') AS db ON s.spid = db.request_session_id + WHERE s.status = 'sleeping' + AND s.open_tran > 0 + AND s.last_batch < DATEADD(ss, -10, SYSDATETIME()) + AND EXISTS(SELECT * FROM sys.dm_tran_locks WHERE request_session_id = s.spid + AND NOT (resource_type = N'DATABASE' AND request_mode = N'S' AND request_status = N'GRANT' AND request_owner_type = N'SHARED_TRANSACTION_WORKSPACE')); + END + + /*Query Problems - Clients using implicit transactions - CheckID 37 */ + IF @Seconds > 0 + AND ( @@VERSION NOT LIKE 'Microsoft SQL Server 2005%' + AND @@VERSION NOT LIKE 'Microsoft SQL Server 2008%' + AND @@VERSION NOT LIKE 'Microsoft SQL Server 2008 R2%' ) + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 37',10,1) WITH NOWAIT; + END + + SET @StringToExecute = N'INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, QueryText, OpenTransactionCount) + SELECT 37 AS CheckId, + 50 AS Priority, + ''Query Problems'' AS FindingsGroup, + ''Implicit Transactions'', + ''https://www.brentozar.com/go/ImplicitTransactions/'' AS URL, + ''Database: '' + DB_NAME(s.database_id) + '' '' + CHAR(13) + CHAR(10) + + ''Host: '' + s.[host_name] + '' '' + CHAR(13) + CHAR(10) + + ''Program: '' + s.[program_name] + '' '' + CHAR(13) + CHAR(10) + + CONVERT(NVARCHAR(10), s.open_transaction_count) + + '' open transactions since: '' + + CONVERT(NVARCHAR(30), tat.transaction_begin_time) + ''. '' + AS Details, + ''Run sp_BlitzWho and check the is_implicit_transaction column to spot the culprits. +If one of them is a lead blocker, consider killing that query.'' AS HowToStopit, + tat.transaction_begin_time, + s.login_name, + s.nt_user_name, + s.program_name, + s.host_name, + s.database_id, + DB_NAME(s.database_id) AS DatabaseName, + NULL AS Querytext, + s.open_transaction_count AS OpenTransactionCount + FROM sys.dm_tran_active_transactions AS tat + LEFT JOIN sys.dm_tran_session_transactions AS tst + ON tst.transaction_id = tat.transaction_id + LEFT JOIN sys.dm_exec_sessions AS s + ON s.session_id = tst.session_id + WHERE tat.name = ''implicit_transaction''; + ' + EXECUTE sp_executesql @StringToExecute; + END; + + /* Query Problems - Query Rolling Back - CheckID 9 */ + IF @Seconds > 0 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 9',10,1) WITH NOWAIT; + END + + IF EXISTS (SELECT * FROM sys.dm_exec_requests WHERE total_elapsed_time > 5000 AND request_id > 0) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, QueryText, QueryHash) + SELECT 9 AS CheckID, + 1 AS Priority, + 'Query Problems' AS FindingGroup, + 'Query Rolling Back' AS Finding, + 'https://www.brentozar.com/askbrent/rollback/' AS URL, + 'Rollback started at ' + CAST(r.start_time AS NVARCHAR(100)) + ', is ' + CAST(r.percent_complete AS NVARCHAR(100)) + '% complete.' AS Details, + 'Unfortunately, you can''t stop this. Whatever you do, don''t restart the server in an attempt to fix it - SQL Server will keep rolling back.' AS HowToStopIt, + r.start_time AS StartTime, + s.login_name AS LoginName, + s.nt_user_name AS NTUserName, + s.[program_name] AS ProgramName, + s.[host_name] AS HostName, + db.[resource_database_id] AS DatabaseID, + DB_NAME(db.resource_database_id) AS DatabaseName, + (SELECT TOP 1 [text] FROM sys.dm_exec_sql_text(c.most_recent_sql_handle)) AS QueryText, + r.query_hash + FROM sys.dm_exec_sessions s + INNER JOIN sys.dm_exec_connections c ON s.session_id = c.session_id + INNER JOIN sys.dm_exec_requests r ON s.session_id = r.session_id + LEFT OUTER JOIN ( + SELECT DISTINCT request_session_id, resource_database_id + FROM sys.dm_tran_locks + WHERE resource_type = N'DATABASE' + AND request_mode = N'S' + AND request_status = N'GRANT' + AND request_owner_type = N'SHARED_TRANSACTION_WORKSPACE') AS db ON s.session_id = db.request_session_id + WHERE r.status = 'rollback'; + END + + IF @Seconds > 0 + BEGIN + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT + 47 AS CheckId, + 50 AS Priority, + 'Query Problems' AS FindingsGroup, + 'High Percentage Of Runnable Queries' AS Finding, + 'https://erikdarlingdata.com/go/RunnableQueue/' AS URL, + 'On the ' + + CASE WHEN y.pass = 1 + THEN '1st' + ELSE '2nd' + END + + ' pass, ' + + RTRIM(y.runnable_pct) + + '% of your queries were waiting to get on a CPU to run. ' + + ' This can indicate CPU pressure.' + FROM + ( + SELECT + 1 AS pass, + x.total, + x.runnable, + CONVERT(decimal(5,2), + ( + x.runnable / + (1. * NULLIF(x.total, 0)) + ) + ) * 100. AS runnable_pct + FROM + ( + SELECT + COUNT_BIG(*) AS total, + SUM(CASE WHEN status = 'runnable' + THEN 1 + ELSE 0 + END) AS runnable + FROM sys.dm_exec_requests + WHERE session_id > 50 + ) AS x + ) AS y + WHERE y.runnable_pct > 20.; + END + + /* Server Performance - Too Much Free Memory - CheckID 34 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 34',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 34 AS CheckID, + 50 AS Priority, + 'Server Performance' AS FindingGroup, + 'Too Much Free Memory' AS Finding, + 'https://www.brentozar.com/go/freememory' AS URL, + CAST((CAST(cFree.cntr_value AS BIGINT) / 1024 / 1024 ) AS NVARCHAR(100)) + N'GB of free memory inside SQL Server''s buffer pool,' + @LineFeed + ' which is ' + CAST((CAST(cTotal.cntr_value AS BIGINT) / 1024 / 1024) AS NVARCHAR(100)) + N'GB. You would think lots of free memory would be good, but check out the URL for more information.' AS Details, + 'Run sp_BlitzCache @SortOrder = ''memory grant'' to find queries with huge memory grants and tune them.' AS HowToStopIt + FROM sys.dm_os_performance_counters cFree + INNER JOIN sys.dm_os_performance_counters cTotal ON cTotal.object_name LIKE N'%Memory Manager%' + AND cTotal.counter_name = N'Total Server Memory (KB) ' + WHERE cFree.object_name LIKE N'%Memory Manager%' + AND cFree.counter_name = N'Free Memory (KB) ' + AND CAST(cFree.cntr_value AS BIGINT) > 20480000000 + AND CAST(cTotal.cntr_value AS BIGINT) * .3 <= CAST(cFree.cntr_value AS BIGINT) + AND CAST(SERVERPROPERTY('edition') AS VARCHAR(100)) NOT LIKE '%Standard%'; + + /* Server Performance - Target Memory Lower Than Max - CheckID 35 */ + IF SERVERPROPERTY('EngineEdition') <> 5 /*SERVERPROPERTY('Edition') <> 'SQL Azure'*/ + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 35',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 35 AS CheckID, + 10 AS Priority, + 'Server Performance' AS FindingGroup, + 'Target Memory Lower Than Max' AS Finding, + 'https://www.brentozar.com/go/target' AS URL, + N'Max server memory is ' + CAST(cMax.value_in_use AS NVARCHAR(50)) + N' MB but target server memory is only ' + CAST((CAST(cTarget.cntr_value AS BIGINT) / 1024) AS NVARCHAR(50)) + N' MB,' + @LineFeed + + N'indicating that SQL Server may be under external memory pressure or max server memory may be set too high.' AS Details, + 'Investigate what OS processes are using memory, and double-check the max server memory setting.' AS HowToStopIt + FROM sys.configurations cMax + INNER JOIN sys.dm_os_performance_counters cTarget ON cTarget.object_name LIKE N'%Memory Manager%' + AND cTarget.counter_name = N'Target Server Memory (KB) ' + WHERE cMax.name = 'max server memory (MB)' + AND CAST(cMax.value_in_use AS BIGINT) >= 1.5 * (CAST(cTarget.cntr_value AS BIGINT) / 1024) + AND CAST(cMax.value_in_use AS BIGINT) < 2147483647 /* Not set to default of unlimited */ + AND CAST(cTarget.cntr_value AS BIGINT) < .8 * (SELECT available_physical_memory_kb FROM sys.dm_os_sys_memory); /* Target memory less than 80% of physical memory (in case they set max too high) */ + END + + /* Server Info - Database Size, Total GB - CheckID 21 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 21',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 21 AS CheckID, + 251 AS Priority, + 'Server Info' AS FindingGroup, + 'Database Size, Total GB' AS Finding, + CAST(SUM (CAST(size AS BIGINT)*8./1024./1024.) AS VARCHAR(100)) AS Details, + SUM (CAST(size AS BIGINT))*8./1024./1024. AS DetailsInt, + 'https://www.brentozar.com/askbrent/' AS URL + FROM #MasterFiles + WHERE database_id > 4; + + /* Server Info - Database Count - CheckID 22 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 22',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 22 AS CheckID, + 251 AS Priority, + 'Server Info' AS FindingGroup, + 'Database Count' AS Finding, + CAST(SUM(1) AS VARCHAR(100)) AS Details, + SUM (1) AS DetailsInt, + 'https://www.brentozar.com/askbrent/' AS URL + FROM sys.databases + WHERE database_id > 4; + + /* Server Info - Memory Grants pending - CheckID 39 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 39',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 39 AS CheckID, + 50 AS Priority, + 'Server Performance' AS FindingGroup, + 'Memory Grants Pending' AS Finding, + CAST(PendingGrants.Details AS NVARCHAR(50)) AS Details, + PendingGrants.DetailsInt, + 'https://www.brentozar.com/blitz/memory-grants/' AS URL + FROM + ( + SELECT + COUNT(1) AS Details, + COUNT(1) AS DetailsInt + FROM sys.dm_exec_query_memory_grants AS Grants + WHERE queue_id IS NOT NULL + ) AS PendingGrants + WHERE PendingGrants.Details > 0; + + /* Server Info - Memory Grant/Workspace info - CheckID 40 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 40',10,1) WITH NOWAIT; + END + + DECLARE @MaxWorkspace BIGINT + SET @MaxWorkspace = (SELECT CAST(cntr_value AS BIGINT)/1024 FROM #PerfmonStats WHERE counter_name = N'Maximum Workspace Memory (KB)') + + IF (@MaxWorkspace IS NULL + OR @MaxWorkspace = 0) + BEGIN + SET @MaxWorkspace = 1 + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 40 AS CheckID, + 251 AS Priority, + 'Server Info' AS FindingGroup, + 'Memory Grant/Workspace info' AS Finding, + + 'Grants Outstanding: ' + CAST((SELECT COUNT(*) FROM sys.dm_exec_query_memory_grants WHERE queue_id IS NULL) AS NVARCHAR(50)) + @LineFeed + + 'Total Granted(MB): ' + CAST(ISNULL(SUM(Grants.granted_memory_kb) / 1024, 0) AS NVARCHAR(50)) + @LineFeed + + 'Total WorkSpace(MB): ' + CAST(ISNULL(@MaxWorkspace, 0) AS NVARCHAR(50)) + @LineFeed + + 'Granted workspace: ' + CAST(ISNULL((CAST(SUM(Grants.granted_memory_kb) / 1024 AS MONEY) + / CAST(@MaxWorkspace AS MONEY)) * 100, 0) AS NVARCHAR(50)) + '%' + @LineFeed + + 'Oldest Grant in seconds: ' + CAST(ISNULL(DATEDIFF(SECOND, MIN(Grants.request_time), GETDATE()), 0) AS NVARCHAR(50)) AS Details, + (SELECT COUNT(*) FROM sys.dm_exec_query_memory_grants WHERE queue_id IS NULL) AS DetailsInt, + 'https://www.brentozar.com/askbrent/' AS URL + FROM sys.dm_exec_query_memory_grants AS Grants; + + /* Query Problems - Queries with high memory grants - CheckID 46 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 46',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, URL, QueryText, QueryPlan) + SELECT 46 AS CheckID, + 100 AS Priority, + 'Query Problems' AS FindingGroup, + 'Query with a memory grant exceeding ' + +CAST(@MemoryGrantThresholdPct AS NVARCHAR(15)) + +'%' AS Finding, + 'Granted size: '+ CAST(CAST(Grants.granted_memory_kb / 1024 AS INT) AS NVARCHAR(50)) + +N'MB ' + + @LineFeed + +N'Granted pct of max workspace: ' + + CAST(ISNULL((CAST(Grants.granted_memory_kb / 1024 AS MONEY) + / CAST(@MaxWorkspace AS MONEY)) * 100, 0) AS NVARCHAR(50)) + '%' + + @LineFeed + +N'SQLHandle: ' + +CONVERT(NVARCHAR(128),Grants.[sql_handle],1), + 'https://www.brentozar.com/memory-grants-sql-servers-public-toilet/' AS URL, + SQLText.[text], + QueryPlan.query_plan + FROM sys.dm_exec_query_memory_grants AS Grants + OUTER APPLY sys.dm_exec_sql_text(Grants.[sql_handle]) AS SQLText + OUTER APPLY sys.dm_exec_query_plan(Grants.[plan_handle]) AS QueryPlan + WHERE Grants.granted_memory_kb > ((@MemoryGrantThresholdPct/100.00)*(@MaxWorkspace*1024)); + + /* Query Problems - Memory Leak in USERSTORE_TOKENPERM Cache - CheckID 45 */ + IF EXISTS (SELECT * FROM sys.all_columns WHERE object_id = OBJECT_ID('sys.dm_os_memory_clerks') AND name = 'pages_kb') + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 45',10,1) WITH NOWAIT; + END + + /* SQL 2012+ version */ + SET @StringToExecute = N' + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, URL) + SELECT 45 AS CheckID, + 50 AS Priority, + ''Query Problems'' AS FindingsGroup, + ''Memory Leak in USERSTORE_TOKENPERM Cache'' AS Finding, + N''UserStore_TokenPerm clerk is using '' + CAST(CAST(SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN pages_kb * 1.0 ELSE 0.0 END) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + + N''GB RAM, total buffer pool is '' + CAST(CAST(SUM(pages_kb) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + N''GB.'' + AS details, + ''https://www.BrentOzar.com/go/userstore'' AS URL + FROM sys.dm_os_memory_clerks + HAVING SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN pages_kb * 1.0 ELSE 0.0 END) / SUM(pages_kb) >= 0.1 + AND SUM(pages_kb) / 1024.0 / 1024.0 >= 1; /* At least 1GB RAM overall */'; + EXEC sp_executesql @StringToExecute; + END + ELSE + BEGIN + /* Antiques Roadshow SQL 2008R2 - version */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 45 (Legacy version)',10,1) WITH NOWAIT; + END + + SET @StringToExecute = N' + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, URL) + SELECT 45 AS CheckID, + 50 AS Priority, + ''Performance'' AS FindingsGroup, + ''Memory Leak in USERSTORE_TOKENPERM Cache'' AS Finding, + N''UserStore_TokenPerm clerk is using '' + CAST(CAST(SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN single_pages_kb + multi_pages_kb * 1.0 ELSE 0.0 END) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + + N''GB RAM, total buffer pool is '' + CAST(CAST(SUM(single_pages_kb + multi_pages_kb) / 1024.0 / 1024.0 AS INT) AS NVARCHAR(100)) + N''GB.'' + AS details, + ''https://www.BrentOzar.com/go/userstore'' AS URL + FROM sys.dm_os_memory_clerks + HAVING SUM(CASE WHEN type = ''USERSTORE_TOKENPERM'' AND name = ''TokenAndPermUserStore'' THEN single_pages_kb + multi_pages_kb * 1.0 ELSE 0.0 END) / SUM(single_pages_kb + multi_pages_kb) >= 0.1 + AND SUM(single_pages_kb + multi_pages_kb) / 1024.0 / 1024.0 >= 1; /* At least 1GB RAM overall */'; + EXEC sp_executesql @StringToExecute; + END + + + + IF @Seconds > 0 + BEGIN + + IF EXISTS ( SELECT 1/0 + FROM sys.all_objects AS ao + WHERE ao.name = 'dm_exec_query_profiles' ) + BEGIN + + IF EXISTS( SELECT 1/0 + FROM sys.dm_exec_requests AS r + JOIN sys.dm_exec_sessions AS s + ON r.session_id = s.session_id + WHERE s.host_name IS NOT NULL + AND r.total_elapsed_time > 5000 + AND r.request_id > 0 ) + BEGIN + + SET @StringToExecute = N' + DECLARE @bad_estimate TABLE + ( + session_id INT, + request_id INT, + estimate_inaccuracy BIT + ); + + INSERT @bad_estimate ( session_id, request_id, estimate_inaccuracy ) + SELECT x.session_id, + x.request_id, + x.estimate_inaccuracy + FROM ( + SELECT deqp.session_id, + deqp.request_id, + CASE WHEN (deqp.row_count/10000) > deqp.estimate_row_count + THEN 1 + ELSE 0 + END AS estimate_inaccuracy + FROM sys.dm_exec_query_profiles AS deqp + INNER JOIN sys.dm_exec_requests r ON deqp.session_id = r.session_id AND deqp.request_id = r.request_id + WHERE deqp.session_id <> @@SPID + AND r.total_elapsed_time > 5000 + ) AS x + WHERE x.estimate_inaccuracy = 1 + GROUP BY x.session_id, + x.request_id, + x.estimate_inaccuracy; + + DECLARE @parallelism_skew TABLE + ( + session_id INT, + request_id INT, + parallelism_skew BIT + ); + + INSERT @parallelism_skew ( session_id, request_id, parallelism_skew ) + SELECT y.session_id, + y.request_id, + y.parallelism_skew + FROM ( + SELECT x.session_id, + x.request_id, + x.node_id, + x.thread_id, + x.row_count, + x.sum_node_rows, + x.node_dop, + x.sum_node_rows / x.node_dop AS even_distribution, + x.row_count / (1. * ISNULL(NULLIF(x.sum_node_rows / x.node_dop, 0), 1)) AS skew_percent, + CASE + WHEN x.row_count > 10000 + AND x.row_count / (1. * ISNULL(NULLIF(x.sum_node_rows / x.node_dop, 0), 1)) > 2. + THEN 1 + WHEN x.row_count > 10000 + AND x.row_count / (1. * ISNULL(NULLIF(x.sum_node_rows / x.node_dop, 0), 1)) < 0.5 + THEN 1 + ELSE 0 + END AS parallelism_skew + FROM ( + SELECT deqp.session_id, + deqp.request_id, + deqp.node_id, + deqp.thread_id, + deqp.row_count, + SUM(deqp.row_count) + OVER ( PARTITION BY deqp.session_id, + deqp.request_id, + deqp.node_id + ORDER BY deqp.row_count + ROWS BETWEEN UNBOUNDED PRECEDING + AND UNBOUNDED FOLLOWING ) + AS sum_node_rows, + COUNT(*) + OVER ( PARTITION BY deqp.session_id, + deqp.request_id, + deqp.node_id + ORDER BY deqp.row_count + ROWS BETWEEN UNBOUNDED PRECEDING + AND UNBOUNDED FOLLOWING ) + AS node_dop + FROM sys.dm_exec_query_profiles AS deqp + WHERE deqp.thread_id > 0 + AND deqp.session_id <> @@SPID + AND EXISTS + ( + SELECT 1/0 + FROM sys.dm_exec_query_profiles AS deqp2 + WHERE deqp.session_id = deqp2.session_id + AND deqp.node_id = deqp2.node_id + AND deqp2.thread_id > 0 + GROUP BY deqp2.session_id, deqp2.node_id + HAVING COUNT(deqp2.node_id) > 1 + ) + ) AS x + ) AS y + WHERE y.parallelism_skew = 1 + GROUP BY y.session_id, + y.request_id, + y.parallelism_skew; + + /* Queries in dm_exec_query_profiles showing signs of poor cardinality estimates - CheckID 42 */ + IF (@Debug = 1) + BEGIN + RAISERROR(''Running CheckID 42'',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults + (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, QueryText, OpenTransactionCount, QueryHash, QueryPlan) + SELECT 42 AS CheckID, + 100 AS Priority, + ''Query Performance'' AS FindingsGroup, + ''Queries with 10000x cardinality misestimations'' AS Findings, + ''https://www.brentozar.com/go/skewedup'' AS URL, + ''The query on SPID '' + + RTRIM(b.session_id) + + '' has been running for '' + + RTRIM(r.total_elapsed_time / 1000) + + '' seconds, with a large cardinality misestimate'' AS Details, + ''No quick fix here: time to dig into the actual execution plan. '' AS HowToStopIt, + r.start_time, + s.login_name, + s.nt_user_name, + s.program_name, + s.host_name, + r.database_id, + DB_NAME(r.database_id), + dest.text, + s.open_transaction_count, + r.query_hash, '; + + IF @dm_exec_query_statistics_xml = 1 + SET @StringToExecute = @StringToExecute + N' COALESCE(qs_live.query_plan, qp.query_plan) AS query_plan '; + ELSE + SET @StringToExecute = @StringToExecute + N' qp.query_plan '; + + SET @StringToExecute = @StringToExecute + N' + FROM @bad_estimate AS b + JOIN sys.dm_exec_requests AS r + ON r.session_id = b.session_id + AND r.request_id = b.request_id + JOIN sys.dm_exec_sessions AS s + ON s.session_id = b.session_id + CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS dest + CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp '; + + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'dm_exec_query_statistics_xml') + /* GitHub #3210 */ + SET @StringToExecute = N' + SET LOCK_TIMEOUT 1000 ' + @StringToExecute + N' OUTER APPLY sys.dm_exec_query_statistics_xml(s.session_id) qs_live '; + + SET @StringToExecute = @StringToExecute + N'; + + /* Queries in dm_exec_query_profiles showing signs of unbalanced parallelism - CheckID 43 */ + IF (@Debug = 1) + BEGIN + RAISERROR(''Running CheckID 43'',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults + (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, StartTime, LoginName, NTUserName, ProgramName, HostName, DatabaseID, DatabaseName, QueryText, OpenTransactionCount, QueryHash, QueryPlan) + SELECT 43 AS CheckID, + 100 AS Priority, + ''Query Performance'' AS FindingsGroup, + ''Queries with 10000x skewed parallelism'' AS Findings, + ''https://www.brentozar.com/go/skewedup'' AS URL, + ''The query on SPID '' + + RTRIM(p.session_id) + + '' has been running for '' + + RTRIM(r.total_elapsed_time / 1000) + + '' seconds, with a parallel threads doing uneven work.'' AS Details, + ''No quick fix here: time to dig into the actual execution plan. '' AS HowToStopIt, + r.start_time, + s.login_name, + s.nt_user_name, + s.program_name, + s.host_name, + r.database_id, + DB_NAME(r.database_id), + dest.text, + s.open_transaction_count, + r.query_hash, '; + + IF @dm_exec_query_statistics_xml = 1 + SET @StringToExecute = @StringToExecute + N' COALESCE(qs_live.query_plan, qp.query_plan) AS query_plan '; + ELSE + SET @StringToExecute = @StringToExecute + N' qp.query_plan '; + + SET @StringToExecute = @StringToExecute + N' + FROM @parallelism_skew AS p + JOIN sys.dm_exec_requests AS r + ON r.session_id = p.session_id + AND r.request_id = p.request_id + JOIN sys.dm_exec_sessions AS s + ON s.session_id = p.session_id + CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS dest + CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp '; + + IF EXISTS (SELECT * FROM sys.all_objects WHERE name = 'dm_exec_query_statistics_xml') + SET @StringToExecute = @StringToExecute + N' OUTER APPLY sys.dm_exec_query_statistics_xml(s.session_id) qs_live '; + + + SET @StringToExecute = @StringToExecute + N';'; + + EXECUTE sp_executesql @StringToExecute, N'@Debug BIT',@Debug = @Debug; + END + + END + END + + /* Server Performance - High CPU Utilization - CheckID 24 */ + IF @Seconds < 30 + BEGIN + /* If we're waiting less than 30 seconds, run this check now rather than wait til the end. + We get this data from the ring buffers, and it's only updated once per minute, so might + as well get it now - whereas if we're checking 30+ seconds, it might get updated by the + end of our sp_BlitzFirst session. */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 24',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 24, 50, 'Server Performance', 'High CPU Utilization', CAST(100 - SystemIdle AS NVARCHAR(20)) + N'%.', 100 - SystemIdle, 'https://www.brentozar.com/go/cpu' + FROM ( + SELECT record, + record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle + FROM ( + SELECT TOP 1 CONVERT(XML, record) AS record + FROM sys.dm_os_ring_buffers + WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' + AND record LIKE '%%' + ORDER BY timestamp DESC) AS rb + ) AS y + WHERE 100 - SystemIdle >= 50; + + /* CPU Utilization - CheckID 23 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 23',10,1) WITH NOWAIT; + END + + IF SERVERPROPERTY('EngineEdition') <> 5 /*SERVERPROPERTY('Edition') <> 'SQL Azure'*/ + WITH y + AS + ( + SELECT CONVERT(VARCHAR(5), 100 - ca.c.value('.', 'INT')) AS system_idle, + CONVERT(VARCHAR(30), rb.event_date) AS event_date, + CONVERT(VARCHAR(8000), rb.record) AS record, + event_date as event_date_raw + FROM + ( SELECT CONVERT(XML, dorb.record) AS record, + DATEADD(ms, -( ts.ms_ticks - dorb.timestamp ), GETDATE()) AS event_date + FROM sys.dm_os_ring_buffers AS dorb + CROSS JOIN + ( SELECT dosi.ms_ticks FROM sys.dm_os_sys_info AS dosi ) AS ts + WHERE dorb.ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' + AND record LIKE '%%' ) AS rb + CROSS APPLY rb.record.nodes('/Record/SchedulerMonitorEvent/SystemHealth/SystemIdle') AS ca(c) + ) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL, HowToStopIt) + SELECT TOP 1 + 23, + 250, + 'Server Info', + 'CPU Utilization', + y.system_idle + N'%. Ring buffer details: ' + CAST(y.record AS NVARCHAR(4000)), + y.system_idle , + 'https://www.brentozar.com/go/cpu', + STUFF(( SELECT TOP 2147483647 + CHAR(10) + CHAR(13) + + y2.system_idle + + '% ON ' + + y2.event_date + + ' Ring buffer details: ' + + y2.record + FROM y AS y2 + ORDER BY y2.event_date_raw DESC + FOR XML PATH(N''), TYPE ).value(N'.[1]', N'VARCHAR(MAX)'), 1, 1, N'') AS query + FROM y + ORDER BY y.event_date_raw DESC; + + + /* Highlight if non SQL processes are using >25% CPU - CheckID 28 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 28',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 28, 50, 'Server Performance', 'High CPU Utilization - Not SQL', CONVERT(NVARCHAR(100),100 - (y.SQLUsage + y.SystemIdle)) + N'% - Other Processes (not SQL Server) are using this much CPU. This may impact on the performance of your SQL Server instance', 100 - (y.SQLUsage + y.SystemIdle), 'https://www.brentozar.com/go/cpu' + FROM ( + SELECT record, + record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle + ,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS SQLUsage + FROM ( + SELECT TOP 1 CONVERT(XML, record) AS record + FROM sys.dm_os_ring_buffers + WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' + AND record LIKE '%%' + ORDER BY timestamp DESC) AS rb + ) AS y + WHERE 100 - (y.SQLUsage + y.SystemIdle) >= 25; + + END; /* IF @Seconds < 30 */ + + /* Query Problems - Statistics Updated Recently - CheckID 44 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 44',10,1) WITH NOWAIT; + END + + IF 20 >= (SELECT COUNT(*) FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb')) + AND @Seconds > 0 + BEGIN + CREATE TABLE #UpdatedStats (HowToStopIt NVARCHAR(4000), RowsForSorting BIGINT); + IF EXISTS(SELECT * FROM sys.all_objects WHERE name = 'dm_db_stats_properties') + BEGIN + /* We don't want to hang around to obtain locks */ + SET LOCK_TIMEOUT 0; + + IF SERVERPROPERTY('EngineEdition') <> 5 /*SERVERPROPERTY('Edition') <> 'SQL Azure'*/ + SET @StringToExecute = N'USE [?];' + @LineFeed; + ELSE + SET @StringToExecute = N''; + + SET @StringToExecute = @StringToExecute + 'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET LOCK_TIMEOUT 1000;' + @LineFeed + + 'BEGIN TRY' + @LineFeed + + ' INSERT INTO #UpdatedStats(HowToStopIt, RowsForSorting)' + @LineFeed + + ' SELECT HowToStopIt = ' + @LineFeed + + ' QUOTENAME(DB_NAME()) + N''.'' +' + @LineFeed + + ' QUOTENAME(SCHEMA_NAME(obj.schema_id)) + N''.'' +' + @LineFeed + + ' QUOTENAME(obj.name) +' + @LineFeed + + ' N'' statistic '' + QUOTENAME(stat.name) + ' + @LineFeed + + ' N'' was updated on '' + CONVERT(NVARCHAR(50), sp.last_updated, 121) + N'','' + ' + @LineFeed + + ' N'' had '' + CAST(sp.rows AS NVARCHAR(50)) + N'' rows, with '' +' + @LineFeed + + ' CAST(sp.rows_sampled AS NVARCHAR(50)) + N'' rows sampled,'' + ' + @LineFeed + + ' N'' producing '' + CAST(sp.steps AS NVARCHAR(50)) + N'' steps in the histogram.'',' + @LineFeed + + ' sp.rows' + @LineFeed + + ' FROM sys.objects AS obj WITH (NOLOCK)' + @LineFeed + + ' INNER JOIN sys.stats AS stat WITH (NOLOCK) ON stat.object_id = obj.object_id ' + @LineFeed + + ' CROSS APPLY sys.dm_db_stats_properties(stat.object_id, stat.stats_id) AS sp ' + @LineFeed + + ' WHERE sp.last_updated > DATEADD(MI, -15, GETDATE())' + @LineFeed + + ' AND obj.is_ms_shipped = 0' + @LineFeed + + ' AND ''[?]'' <> ''[tempdb]'';' + @LineFeed + + 'END TRY' + @LineFeed + + 'BEGIN CATCH' + @LineFeed + + ' IF (ERROR_NUMBER() = 1222)' + @LineFeed + + ' BEGIN ' + @LineFeed + + ' INSERT INTO #UpdatedStats(HowToStopIt, RowsForSorting)' + @LineFeed + + ' SELECT HowToStopIt = ' + @LineFeed + + ' QUOTENAME(DB_NAME()) +' + @LineFeed + + ' N'' No information could be retrieved as the lock timeout was exceeded,''+' + @LineFeed + + ' N'' this is likely due to an Index operation in Progress'',' + @LineFeed + + ' -1' + @LineFeed + + ' END' + @LineFeed + + ' ELSE' + @LineFeed + + ' BEGIN' + @LineFeed + + ' INSERT INTO #UpdatedStats(HowToStopIt, RowsForSorting)' + @LineFeed + + ' SELECT HowToStopIt = ' + @LineFeed + + ' QUOTENAME(DB_NAME()) +' + @LineFeed + + ' N'' No information could be retrieved as a result of error: ''+' + @LineFeed + + ' CAST(ERROR_NUMBER() AS NVARCHAR(10)) +' + @LineFeed + + ' N'' with message: ''+' + @LineFeed + + ' CAST(ERROR_MESSAGE() AS NVARCHAR(128)),' + @LineFeed + + ' -1' + @LineFeed + + ' END' + @LineFeed + + 'END CATCH' + ; + + IF SERVERPROPERTY('EngineEdition') <> 5 /*SERVERPROPERTY('Edition') <> 'SQL Azure'*/ + BEGIN + BEGIN TRY + EXEC sp_MSforeachdb @StringToExecute; + END TRY + BEGIN CATCH + IF (ERROR_NUMBER() = 1222) + BEGIN + INSERT INTO #UpdatedStats(HowToStopIt, RowsForSorting) + SELECT HowToStopIt = N'No information could be retrieved as the lock timeout was exceeded while iterating databases,' + + N' this is likely due to an Index operation in Progress', -1; + END + ELSE + BEGIN + THROW; + END + END CATCH + END + ELSE + EXEC(@StringToExecute); + + /* Set timeout back to a default value of -1 */ + SET LOCK_TIMEOUT -1; + END; + + /* We mark timeout exceeded with a -1 so only show these IF there is statistics info that succeeded */ + IF EXISTS (SELECT * FROM #UpdatedStats WHERE RowsForSorting > -1) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 44 AS CheckId, + 50 AS Priority, + 'Query Problems' AS FindingGroup, + 'Statistics Updated Recently' AS Finding, + 'https://www.brentozar.com/go/stats' AS URL, + 'In the last 15 minutes, statistics were updated. To see which ones, click the HowToStopIt column.' + @LineFeed + @LineFeed + + 'This effectively clears the plan cache for queries that involve these tables,' + @LineFeed + + 'which thereby causes parameter sniffing: those queries are now getting brand new' + @LineFeed + + 'query plans based on whatever parameters happen to call them next.' + @LineFeed + @LineFeed + + 'Be on the lookout for sudden parameter sniffing issues after this time range.', + HowToStopIt = (SELECT (SELECT HowToStopIt + NCHAR(10)) + FROM #UpdatedStats + ORDER BY RowsForSorting DESC + FOR XML PATH('')); + + END + + RAISERROR('Finished running investigatory queries',10,1) WITH NOWAIT; + + + /* End of checks. If we haven't waited @Seconds seconds, wait. */ + IF DATEADD(SECOND,1,SYSDATETIMEOFFSET()) < @FinishSampleTime + BEGIN + RAISERROR('Waiting to match @Seconds parameter',10,1) WITH NOWAIT; + WAITFOR TIME @FinishSampleTimeWaitFor; + END; + + IF @total_cpu_usage IN (0, 1) + BEGIN + EXEC sys.sp_executesql + @get_thread_time_ms, + N'@thread_time_ms FLOAT OUTPUT', + @thread_time_ms OUTPUT; + END + + RAISERROR('Capturing second pass of wait stats, perfmon counters, file stats',10,1) WITH NOWAIT; + /* Populate #FileStats, #PerfmonStats, #WaitStats with DMV data. In a second, we'll compare these. */ + SET @StringToExecute = N' + INSERT #WaitStats(Pass, SampleTime, wait_type, wait_time_ms, thread_time_ms, signal_wait_time_ms, waiting_tasks_count) + SELECT + x.Pass, + x.SampleTime, + x.wait_type, + SUM(x.sum_wait_time_ms) AS sum_wait_time_ms, + @thread_time_ms AS thread_time_ms, + SUM(x.sum_signal_wait_time_ms) AS sum_signal_wait_time_ms, + SUM(x.sum_waiting_tasks) AS sum_waiting_tasks + FROM ( + SELECT + 2 AS Pass, + SYSDATETIMEOFFSET() AS SampleTime, + owt.wait_type, + SUM(owt.wait_duration_ms) OVER (PARTITION BY owt.wait_type, owt.session_id) + - CASE WHEN @Seconds = 0 THEN 0 ELSE (@Seconds * 1000) END AS sum_wait_time_ms, + 0 AS sum_signal_wait_time_ms, + CASE @Seconds WHEN 0 THEN 0 ELSE 1 END AS sum_waiting_tasks + FROM sys.dm_os_waiting_tasks owt + WHERE owt.session_id > 50 + AND owt.wait_duration_ms >= CASE @Seconds WHEN 0 THEN 0 ELSE @Seconds * 1000 END + UNION ALL + SELECT + 2 AS Pass, + SYSDATETIMEOFFSET() AS SampleTime, + os.wait_type, + SUM(os.wait_time_ms) OVER (PARTITION BY os.wait_type) AS sum_wait_time_ms, + SUM(os.signal_wait_time_ms) OVER (PARTITION BY os.wait_type ) AS sum_signal_wait_time_ms, + SUM(os.waiting_tasks_count) OVER (PARTITION BY os.wait_type) AS sum_waiting_tasks '; + + IF SERVERPROPERTY('EngineEdition') = 5 /*SERVERPROPERTY('Edition') = 'SQL Azure'*/ + SET @StringToExecute = @StringToExecute + N' FROM sys.dm_db_wait_stats os '; + ELSE + SET @StringToExecute = @StringToExecute + N' FROM sys.dm_os_wait_stats os '; + + SET @StringToExecute = @StringToExecute + N' + ) x + WHERE NOT EXISTS + ( + SELECT * + FROM ##WaitCategories AS wc + WHERE wc.WaitType = x.wait_type + AND wc.Ignorable = 1 + ) + GROUP BY x.Pass, x.SampleTime, x.wait_type + ORDER BY sum_wait_time_ms DESC;'; + + EXEC sys.sp_executesql + @StringToExecute, + N'@StartSampleTime DATETIMEOFFSET, + @Seconds INT, + @thread_time_ms FLOAT', + @StartSampleTime, + @Seconds, + @thread_time_ms; + + WITH w AS + ( + SELECT + total_waits = + CONVERT + ( + FLOAT, + SUM(ws.wait_time_ms) + ) + FROM #WaitStats AS ws + WHERE Pass = 2 + ) + UPDATE ws + SET ws.thread_time_ms += w.total_waits + FROM #WaitStats AS ws + CROSS JOIN w + WHERE ws.Pass = 2 + OPTION(RECOMPILE); + + + INSERT INTO #FileStats (Pass, SampleTime, DatabaseID, FileID, DatabaseName, FileLogicalName, SizeOnDiskMB, io_stall_read_ms , + num_of_reads, [bytes_read] , io_stall_write_ms,num_of_writes, [bytes_written], PhysicalName, TypeDesc, avg_stall_read_ms, avg_stall_write_ms) + SELECT 2 AS Pass, + SYSDATETIMEOFFSET() AS SampleTime, + mf.[database_id], + mf.[file_id], + DB_NAME(vfs.database_id) AS [db_name], + mf.name + N' [' + mf.type_desc COLLATE SQL_Latin1_General_CP1_CI_AS + N']' AS file_logical_name , + CAST(( ( vfs.size_on_disk_bytes / 1024.0 ) / 1024.0 ) AS INT) AS size_on_disk_mb , + vfs.io_stall_read_ms , + vfs.num_of_reads , + vfs.[num_of_bytes_read], + vfs.io_stall_write_ms , + vfs.num_of_writes , + vfs.[num_of_bytes_written], + mf.physical_name, + mf.type_desc, + 0, + 0 + FROM sys.dm_io_virtual_file_stats (NULL, NULL) AS vfs + INNER JOIN #MasterFiles AS mf ON vfs.file_id = mf.file_id + AND vfs.database_id = mf.database_id + WHERE vfs.num_of_reads > 0 + OR vfs.num_of_writes > 0; + + INSERT INTO #PerfmonStats (Pass, SampleTime, [object_name],[counter_name],[instance_name],[cntr_value],[cntr_type]) + SELECT 2 AS Pass, + SYSDATETIMEOFFSET() AS SampleTime, + RTRIM(dmv.object_name), RTRIM(dmv.counter_name), RTRIM(dmv.instance_name), dmv.cntr_value, dmv.cntr_type + FROM #PerfmonCounters counters + INNER JOIN sys.dm_os_performance_counters dmv ON counters.counter_name COLLATE SQL_Latin1_General_CP1_CI_AS = RTRIM(dmv.counter_name) COLLATE SQL_Latin1_General_CP1_CI_AS + AND counters.[object_name] COLLATE SQL_Latin1_General_CP1_CI_AS = RTRIM(dmv.[object_name]) COLLATE SQL_Latin1_General_CP1_CI_AS + AND (counters.[instance_name] IS NULL OR counters.[instance_name] COLLATE SQL_Latin1_General_CP1_CI_AS = RTRIM(dmv.[instance_name]) COLLATE SQL_Latin1_General_CP1_CI_AS); + + /* Set the latencies and averages. We could do this with a CTE, but we're not ambitious today. */ + UPDATE fNow + SET avg_stall_read_ms = ((fNow.io_stall_read_ms - fBase.io_stall_read_ms) / (fNow.num_of_reads - fBase.num_of_reads)) + FROM #FileStats fNow + INNER JOIN #FileStats fBase ON fNow.DatabaseID = fBase.DatabaseID AND fNow.FileID = fBase.FileID AND fNow.SampleTime > fBase.SampleTime AND fNow.num_of_reads > fBase.num_of_reads AND fNow.io_stall_read_ms > fBase.io_stall_read_ms + WHERE (fNow.num_of_reads - fBase.num_of_reads) > 0; + + UPDATE fNow + SET avg_stall_write_ms = ((fNow.io_stall_write_ms - fBase.io_stall_write_ms) / (fNow.num_of_writes - fBase.num_of_writes)) + FROM #FileStats fNow + INNER JOIN #FileStats fBase ON fNow.DatabaseID = fBase.DatabaseID AND fNow.FileID = fBase.FileID AND fNow.SampleTime > fBase.SampleTime AND fNow.num_of_writes > fBase.num_of_writes AND fNow.io_stall_write_ms > fBase.io_stall_write_ms + WHERE (fNow.num_of_writes - fBase.num_of_writes) > 0; + + UPDATE pNow + SET [value_delta] = pNow.cntr_value - pFirst.cntr_value, + [value_per_second] = ((1.0 * pNow.cntr_value - pFirst.cntr_value) / DATEDIFF(ss, pFirst.SampleTime, pNow.SampleTime)) + FROM #PerfmonStats pNow + INNER JOIN #PerfmonStats pFirst ON pFirst.[object_name] = pNow.[object_name] AND pFirst.counter_name = pNow.counter_name AND (pFirst.instance_name = pNow.instance_name OR (pFirst.instance_name IS NULL AND pNow.instance_name IS NULL)) + AND pNow.ID > pFirst.ID + WHERE DATEDIFF(ss, pFirst.SampleTime, pNow.SampleTime) > 0; + + + /* Query Stats - If we're within 10 seconds of our projected finish time, do the plan cache analysis. - CheckID 18 */ + IF DATEDIFF(ss, @FinishSampleTime, SYSDATETIMEOFFSET()) > 10 AND @CheckProcedureCache = 1 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 18',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + VALUES (18, 210, 'Query Stats', 'Plan Cache Analysis Skipped', 'https://www.brentozar.com/go/topqueries', + 'Due to excessive load, the plan cache analysis was skipped. To override this, use @ExpertMode = 1.'); + + END; + ELSE IF @CheckProcedureCache = 1 + BEGIN + + + RAISERROR('@CheckProcedureCache = 1, capturing second pass of plan cache',10,1) WITH NOWAIT; + + /* Populate #QueryStats. SQL 2005 doesn't have query hash or query plan hash. */ + IF @@VERSION LIKE 'Microsoft SQL Server 2005%' + BEGIN + IF @FilterPlansByDatabase IS NULL + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 2 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, NULL AS query_hash, NULL AS query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + WHERE qs.last_execution_time >= @StartSampleTimeText;'; + END; + ELSE + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 2 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, NULL AS query_hash, NULL AS query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS attr + INNER JOIN #FilterPlansByDatabase dbs ON CAST(attr.value AS INT) = dbs.DatabaseID + WHERE qs.last_execution_time >= @StartSampleTimeText + AND attr.attribute = ''dbid'';'; + END; + END; + ELSE + BEGIN + IF @FilterPlansByDatabase IS NULL + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 2 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + WHERE qs.last_execution_time >= @StartSampleTimeText'; + END; + ELSE + BEGIN + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 2 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS attr + INNER JOIN #FilterPlansByDatabase dbs ON CAST(attr.value AS INT) = dbs.DatabaseID + WHERE qs.last_execution_time >= @StartSampleTimeText + AND attr.attribute = ''dbid'';'; + END; + END; + /* Old version pre-2016/06/13: + IF @@VERSION LIKE 'Microsoft SQL Server 2005%' + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 2 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, NULL AS query_hash, NULL AS query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + WHERE qs.last_execution_time >= @StartSampleTimeText;'; + ELSE + SET @StringToExecute = N'INSERT INTO #QueryStats ([sql_handle], Pass, SampleTime, statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, Points) + SELECT [sql_handle], 2 AS Pass, SYSDATETIMEOFFSET(), statement_start_offset, statement_end_offset, plan_generation_num, plan_handle, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time, query_hash, query_plan_hash, 0 + FROM sys.dm_exec_query_stats qs + WHERE qs.last_execution_time >= @StartSampleTimeText;'; + */ + SET @ParmDefinitions = N'@StartSampleTimeText NVARCHAR(100)'; + SET @Parm1 = CONVERT(NVARCHAR(100), CAST(@StartSampleTime AS DATETIME), 127); + + EXECUTE sp_executesql @StringToExecute, @ParmDefinitions, @StartSampleTimeText = @Parm1; + + RAISERROR('@CheckProcedureCache = 1, totaling up plan cache metrics',10,1) WITH NOWAIT; + + /* Get the totals for the entire plan cache */ + INSERT INTO #QueryStats (Pass, SampleTime, execution_count, total_worker_time, total_physical_reads, total_logical_writes, total_logical_reads, total_clr_time, total_elapsed_time, creation_time) + SELECT 0 AS Pass, SYSDATETIMEOFFSET(), SUM(execution_count), SUM(total_worker_time), SUM(total_physical_reads), SUM(total_logical_writes), SUM(total_logical_reads), SUM(total_clr_time), SUM(total_elapsed_time), MIN(creation_time) + FROM sys.dm_exec_query_stats qs; + + + RAISERROR('@CheckProcedureCache = 1, so analyzing execution plans',10,1) WITH NOWAIT; + /* + Pick the most resource-intensive queries to review. Update the Points field + in #QueryStats - if a query is in the top 10 for logical reads, CPU time, + duration, or execution, add 1 to its points. + */ + WITH qsTop AS ( + SELECT TOP 10 qsNow.ID + FROM #QueryStats qsNow + INNER JOIN #QueryStats qsFirst ON qsNow.[sql_handle] = qsFirst.[sql_handle] AND qsNow.statement_start_offset = qsFirst.statement_start_offset AND qsNow.statement_end_offset = qsFirst.statement_end_offset AND qsNow.plan_generation_num = qsFirst.plan_generation_num AND qsNow.plan_handle = qsFirst.plan_handle AND qsFirst.Pass = 1 + WHERE qsNow.total_elapsed_time > qsFirst.total_elapsed_time + AND qsNow.Pass = 2 + AND qsNow.total_elapsed_time - qsFirst.total_elapsed_time > 1000000 /* Only queries with over 1 second of runtime */ + ORDER BY (qsNow.total_elapsed_time - COALESCE(qsFirst.total_elapsed_time, 0)) DESC) + UPDATE #QueryStats + SET Points = Points + 1 + FROM #QueryStats qs + INNER JOIN qsTop ON qs.ID = qsTop.ID; + + WITH qsTop AS ( + SELECT TOP 10 qsNow.ID + FROM #QueryStats qsNow + INNER JOIN #QueryStats qsFirst ON qsNow.[sql_handle] = qsFirst.[sql_handle] AND qsNow.statement_start_offset = qsFirst.statement_start_offset AND qsNow.statement_end_offset = qsFirst.statement_end_offset AND qsNow.plan_generation_num = qsFirst.plan_generation_num AND qsNow.plan_handle = qsFirst.plan_handle AND qsFirst.Pass = 1 + WHERE qsNow.total_logical_reads > qsFirst.total_logical_reads + AND qsNow.Pass = 2 + AND qsNow.total_logical_reads - qsFirst.total_logical_reads > 1000 /* Only queries with over 1000 reads */ + ORDER BY (qsNow.total_logical_reads - COALESCE(qsFirst.total_logical_reads, 0)) DESC) + UPDATE #QueryStats + SET Points = Points + 1 + FROM #QueryStats qs + INNER JOIN qsTop ON qs.ID = qsTop.ID; + + WITH qsTop AS ( + SELECT TOP 10 qsNow.ID + FROM #QueryStats qsNow + INNER JOIN #QueryStats qsFirst ON qsNow.[sql_handle] = qsFirst.[sql_handle] AND qsNow.statement_start_offset = qsFirst.statement_start_offset AND qsNow.statement_end_offset = qsFirst.statement_end_offset AND qsNow.plan_generation_num = qsFirst.plan_generation_num AND qsNow.plan_handle = qsFirst.plan_handle AND qsFirst.Pass = 1 + WHERE qsNow.total_worker_time > qsFirst.total_worker_time + AND qsNow.Pass = 2 + AND qsNow.total_worker_time - qsFirst.total_worker_time > 1000000 /* Only queries with over 1 second of worker time */ + ORDER BY (qsNow.total_worker_time - COALESCE(qsFirst.total_worker_time, 0)) DESC) + UPDATE #QueryStats + SET Points = Points + 1 + FROM #QueryStats qs + INNER JOIN qsTop ON qs.ID = qsTop.ID; + + WITH qsTop AS ( + SELECT TOP 10 qsNow.ID + FROM #QueryStats qsNow + INNER JOIN #QueryStats qsFirst ON qsNow.[sql_handle] = qsFirst.[sql_handle] AND qsNow.statement_start_offset = qsFirst.statement_start_offset AND qsNow.statement_end_offset = qsFirst.statement_end_offset AND qsNow.plan_generation_num = qsFirst.plan_generation_num AND qsNow.plan_handle = qsFirst.plan_handle AND qsFirst.Pass = 1 + WHERE qsNow.execution_count > qsFirst.execution_count + AND qsNow.Pass = 2 + AND (qsNow.total_elapsed_time - qsFirst.total_elapsed_time > 1000000 /* Only queries with over 1 second of runtime */ + OR qsNow.total_logical_reads - qsFirst.total_logical_reads > 1000 /* Only queries with over 1000 reads */ + OR qsNow.total_worker_time - qsFirst.total_worker_time > 1000000 /* Only queries with over 1 second of worker time */) + ORDER BY (qsNow.execution_count - COALESCE(qsFirst.execution_count, 0)) DESC) + UPDATE #QueryStats + SET Points = Points + 1 + FROM #QueryStats qs + INNER JOIN qsTop ON qs.ID = qsTop.ID; + + /* Query Stats - Most Resource-Intensive Queries - CheckID 17 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 17',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, QueryText, QueryStatsNowID, QueryStatsFirstID, PlanHandle, QueryHash) + SELECT 17, 210, 'Query Stats', 'Most Resource-Intensive Queries', 'https://www.brentozar.com/go/topqueries', + 'Query stats during the sample:' + @LineFeed + + 'Executions: ' + CAST(qsNow.execution_count - (COALESCE(qsFirst.execution_count, 0)) AS NVARCHAR(100)) + @LineFeed + + 'Elapsed Time: ' + CAST(qsNow.total_elapsed_time - (COALESCE(qsFirst.total_elapsed_time, 0)) AS NVARCHAR(100)) + @LineFeed + + 'CPU Time: ' + CAST(qsNow.total_worker_time - (COALESCE(qsFirst.total_worker_time, 0)) AS NVARCHAR(100)) + @LineFeed + + 'Logical Reads: ' + CAST(qsNow.total_logical_reads - (COALESCE(qsFirst.total_logical_reads, 0)) AS NVARCHAR(100)) + @LineFeed + + 'Logical Writes: ' + CAST(qsNow.total_logical_writes - (COALESCE(qsFirst.total_logical_writes, 0)) AS NVARCHAR(100)) + @LineFeed + + 'CLR Time: ' + CAST(qsNow.total_clr_time - (COALESCE(qsFirst.total_clr_time, 0)) AS NVARCHAR(100)) + @LineFeed + + @LineFeed + @LineFeed + 'Query stats since ' + CONVERT(NVARCHAR(100), qsNow.creation_time ,121) + @LineFeed + + 'Executions: ' + CAST(qsNow.execution_count AS NVARCHAR(100)) + + CASE qsTotal.execution_count WHEN 0 THEN '' ELSE (' - Percent of Server Total: ' + CAST(CAST(100.0 * qsNow.execution_count / qsTotal.execution_count AS DECIMAL(6,2)) AS NVARCHAR(100)) + '%') END + @LineFeed + + 'Elapsed Time: ' + CAST(qsNow.total_elapsed_time AS NVARCHAR(100)) + + CASE qsTotal.total_elapsed_time WHEN 0 THEN '' ELSE (' - Percent of Server Total: ' + CAST(CAST(100.0 * qsNow.total_elapsed_time / qsTotal.total_elapsed_time AS DECIMAL(6,2)) AS NVARCHAR(100)) + '%') END + @LineFeed + + 'CPU Time: ' + CAST(qsNow.total_worker_time AS NVARCHAR(100)) + + CASE qsTotal.total_worker_time WHEN 0 THEN '' ELSE (' - Percent of Server Total: ' + CAST(CAST(100.0 * qsNow.total_worker_time / qsTotal.total_worker_time AS DECIMAL(6,2)) AS NVARCHAR(100)) + '%') END + @LineFeed + + 'Logical Reads: ' + CAST(qsNow.total_logical_reads AS NVARCHAR(100)) + + CASE qsTotal.total_logical_reads WHEN 0 THEN '' ELSE (' - Percent of Server Total: ' + CAST(CAST(100.0 * qsNow.total_logical_reads / qsTotal.total_logical_reads AS DECIMAL(6,2)) AS NVARCHAR(100)) + '%') END + @LineFeed + + 'Logical Writes: ' + CAST(qsNow.total_logical_writes AS NVARCHAR(100)) + + CASE qsTotal.total_logical_writes WHEN 0 THEN '' ELSE (' - Percent of Server Total: ' + CAST(CAST(100.0 * qsNow.total_logical_writes / qsTotal.total_logical_writes AS DECIMAL(6,2)) AS NVARCHAR(100)) + '%') END + @LineFeed + + 'CLR Time: ' + CAST(qsNow.total_clr_time AS NVARCHAR(100)) + + CASE qsTotal.total_clr_time WHEN 0 THEN '' ELSE (' - Percent of Server Total: ' + CAST(CAST(100.0 * qsNow.total_clr_time / qsTotal.total_clr_time AS DECIMAL(6,2)) AS NVARCHAR(100)) + '%') END + @LineFeed + + --@LineFeed + @LineFeed + 'Query hash: ' + CAST(qsNow.query_hash AS NVARCHAR(100)) + @LineFeed + + --@LineFeed + @LineFeed + 'Query plan hash: ' + CAST(qsNow.query_plan_hash AS NVARCHAR(100)) + + @LineFeed AS Details, + 'See the URL for tuning tips on why this query may be consuming resources.' AS HowToStopIt, + qp.query_plan, + QueryText = SUBSTRING(st.text, + (qsNow.statement_start_offset / 2) + 1, + ((CASE qsNow.statement_end_offset + WHEN -1 THEN DATALENGTH(st.text) + ELSE qsNow.statement_end_offset + END - qsNow.statement_start_offset) / 2) + 1), + qsNow.ID AS QueryStatsNowID, + qsFirst.ID AS QueryStatsFirstID, + qsNow.plan_handle AS PlanHandle, + qsNow.query_hash + FROM #QueryStats qsNow + INNER JOIN #QueryStats qsTotal ON qsTotal.Pass = 0 + LEFT OUTER JOIN #QueryStats qsFirst ON qsNow.[sql_handle] = qsFirst.[sql_handle] AND qsNow.statement_start_offset = qsFirst.statement_start_offset AND qsNow.statement_end_offset = qsFirst.statement_end_offset AND qsNow.plan_generation_num = qsFirst.plan_generation_num AND qsNow.plan_handle = qsFirst.plan_handle AND qsFirst.Pass = 1 + CROSS APPLY sys.dm_exec_sql_text(qsNow.sql_handle) AS st + CROSS APPLY sys.dm_exec_query_plan(qsNow.plan_handle) AS qp + WHERE qsNow.Points > 0 AND st.text IS NOT NULL AND qp.query_plan IS NOT NULL; + + UPDATE #BlitzFirstResults + SET DatabaseID = CAST(attr.value AS INT), + DatabaseName = DB_NAME(CAST(attr.value AS INT)) + FROM #BlitzFirstResults + CROSS APPLY sys.dm_exec_plan_attributes(#BlitzFirstResults.PlanHandle) AS attr + WHERE attr.attribute = 'dbid'; + + + END; /* IF DATEDIFF(ss, @FinishSampleTime, SYSDATETIMEOFFSET()) > 10 AND @CheckProcedureCache = 1 */ + + + RAISERROR('Analyzing changes between first and second passes of DMVs',10,1) WITH NOWAIT; + + /* Wait Stats - CheckID 6 */ + /* Compare the current wait stats to the sample we took at the start, and insert the top 10 waits. */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 6',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, DetailsInt) + SELECT TOP 10 6 AS CheckID, + 200 AS Priority, + 'Wait Stats' AS FindingGroup, + wNow.wait_type AS Finding, /* IF YOU CHANGE THIS, STUFF WILL BREAK. Other checks look for wait type names in the Finding field. See checks 11, 12 as example. */ + N'https://www.sqlskills.com/help/waits/' + LOWER(wNow.wait_type) + '/' AS URL, + 'For ' + CAST(((wNow.wait_time_ms - COALESCE(wBase.wait_time_ms,0)) / 1000) AS NVARCHAR(100)) + ' seconds over the last ' + CASE @Seconds WHEN 0 THEN (CAST(DATEDIFF(dd,@StartSampleTime,@FinishSampleTime) AS NVARCHAR(10)) + ' days') ELSE (CAST(@Seconds AS NVARCHAR(10)) + ' seconds') END + ', SQL Server was waiting on this particular bottleneck.' + @LineFeed + @LineFeed AS Details, + 'See the URL for more details on how to mitigate this wait type.' AS HowToStopIt, + ((wNow.wait_time_ms - COALESCE(wBase.wait_time_ms,0)) / 1000) AS DetailsInt + FROM #WaitStats wNow + LEFT OUTER JOIN #WaitStats wBase ON wNow.wait_type = wBase.wait_type AND wNow.SampleTime > wBase.SampleTime + WHERE wNow.wait_time_ms > (wBase.wait_time_ms + (.5 * (DATEDIFF(ss,@StartSampleTime,@FinishSampleTime)) * 1000)) /* Only look for things we've actually waited on for half of the time or more */ + ORDER BY (wNow.wait_time_ms - COALESCE(wBase.wait_time_ms,0)) DESC; + + /* Server Performance - Poison Wait Detected - CheckID 30 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 30',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, DetailsInt) + SELECT 30 AS CheckID, + 10 AS Priority, + 'Server Performance' AS FindingGroup, + 'Poison Wait Detected: ' + wNow.wait_type AS Finding, + N'https://www.brentozar.com/go/poison/#' + wNow.wait_type AS URL, + 'For ' + CAST(((wNow.wait_time_ms - COALESCE(wBase.wait_time_ms,0)) / 1000) AS NVARCHAR(100)) + ' seconds over the last ' + CASE @Seconds WHEN 0 THEN (CAST(DATEDIFF(dd,@StartSampleTime,@FinishSampleTime) AS NVARCHAR(10)) + ' days') ELSE (CAST(@Seconds AS NVARCHAR(10)) + ' seconds') END + ', SQL Server was waiting on this particular bottleneck.' + @LineFeed + @LineFeed AS Details, + 'See the URL for more details on how to mitigate this wait type.' AS HowToStopIt, + ((wNow.wait_time_ms - COALESCE(wBase.wait_time_ms,0)) / 1000) AS DetailsInt + FROM #WaitStats wNow + LEFT OUTER JOIN #WaitStats wBase ON wNow.wait_type = wBase.wait_type AND wNow.SampleTime > wBase.SampleTime + WHERE wNow.wait_type IN ('IO_QUEUE_LIMIT', 'IO_RETRY', 'LOG_RATE_GOVERNOR', 'POOL_LOG_RATE_GOVERNOR', 'PREEMPTIVE_DEBUG', 'RESMGR_THROTTLED', 'RESOURCE_SEMAPHORE', 'RESOURCE_SEMAPHORE_QUERY_COMPILE','SE_REPL_CATCHUP_THROTTLE','SE_REPL_COMMIT_ACK','SE_REPL_COMMIT_TURN','SE_REPL_ROLLBACK_ACK','SE_REPL_SLOW_SECONDARY_THROTTLE','THREADPOOL') + AND wNow.wait_time_ms > (wBase.wait_time_ms + 1000); + + + /* Server Performance - Slow Data File Reads - CheckID 11 */ + IF EXISTS (SELECT * FROM #BlitzFirstResults WHERE Finding LIKE 'PAGEIOLATCH%') + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 11',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, DatabaseID, DatabaseName) + SELECT TOP 10 11 AS CheckID, + 50 AS Priority, + 'Server Performance' AS FindingGroup, + 'Slow Data File Reads' AS Finding, + 'https://www.brentozar.com/go/slow/' AS URL, + 'Your server is experiencing PAGEIOLATCH% waits due to slow data file reads. This file is one of the reasons why.' + @LineFeed + + 'File: ' + fNow.PhysicalName + @LineFeed + + 'Number of reads during the sample: ' + CAST((fNow.num_of_reads - fBase.num_of_reads) AS NVARCHAR(20)) + @LineFeed + + 'Seconds spent waiting on storage for these reads: ' + CAST(((fNow.io_stall_read_ms - fBase.io_stall_read_ms) / 1000.0) AS NVARCHAR(20)) + @LineFeed + + 'Average read latency during the sample: ' + CAST(((fNow.io_stall_read_ms - fBase.io_stall_read_ms) / (fNow.num_of_reads - fBase.num_of_reads) ) AS NVARCHAR(20)) + ' milliseconds' + @LineFeed + + 'Microsoft guidance for data file read speed: 20ms or less.' + @LineFeed + @LineFeed AS Details, + 'See the URL for more details on how to mitigate this wait type.' AS HowToStopIt, + fNow.DatabaseID, + fNow.DatabaseName + FROM #FileStats fNow + INNER JOIN #FileStats fBase ON fNow.DatabaseID = fBase.DatabaseID AND fNow.FileID = fBase.FileID AND fNow.SampleTime > fBase.SampleTime AND fNow.num_of_reads > fBase.num_of_reads AND fNow.io_stall_read_ms > (fBase.io_stall_read_ms + 1000) + WHERE (fNow.io_stall_read_ms - fBase.io_stall_read_ms) / (fNow.num_of_reads - fBase.num_of_reads) >= @FileLatencyThresholdMS + AND fNow.TypeDesc = 'ROWS' + ORDER BY (fNow.io_stall_read_ms - fBase.io_stall_read_ms) / (fNow.num_of_reads - fBase.num_of_reads) DESC; + END; + + /* Server Performance - Slow Log File Writes - CheckID 12 */ + IF EXISTS (SELECT * FROM #BlitzFirstResults WHERE Finding LIKE 'WRITELOG%') + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 12',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, DatabaseID, DatabaseName) + SELECT TOP 10 12 AS CheckID, + 50 AS Priority, + 'Server Performance' AS FindingGroup, + 'Slow Log File Writes' AS Finding, + 'https://www.brentozar.com/go/slow/' AS URL, + 'Your server is experiencing WRITELOG waits due to slow log file writes. This file is one of the reasons why.' + @LineFeed + + 'File: ' + fNow.PhysicalName + @LineFeed + + 'Number of writes during the sample: ' + CAST((fNow.num_of_writes - fBase.num_of_writes) AS NVARCHAR(20)) + @LineFeed + + 'Seconds spent waiting on storage for these writes: ' + CAST(((fNow.io_stall_write_ms - fBase.io_stall_write_ms) / 1000.0) AS NVARCHAR(20)) + @LineFeed + + 'Average write latency during the sample: ' + CAST(((fNow.io_stall_write_ms - fBase.io_stall_write_ms) / (fNow.num_of_writes - fBase.num_of_writes) ) AS NVARCHAR(20)) + ' milliseconds' + @LineFeed + + 'Microsoft guidance for log file write speed: 3ms or less.' + @LineFeed + @LineFeed AS Details, + 'See the URL for more details on how to mitigate this wait type.' AS HowToStopIt, + fNow.DatabaseID, + fNow.DatabaseName + FROM #FileStats fNow + INNER JOIN #FileStats fBase ON fNow.DatabaseID = fBase.DatabaseID AND fNow.FileID = fBase.FileID AND fNow.SampleTime > fBase.SampleTime AND fNow.num_of_writes > fBase.num_of_writes AND fNow.io_stall_write_ms > (fBase.io_stall_write_ms + 1000) + WHERE (fNow.io_stall_write_ms - fBase.io_stall_write_ms) / (fNow.num_of_writes - fBase.num_of_writes) >= @FileLatencyThresholdMS + AND fNow.TypeDesc = 'LOG' + ORDER BY (fNow.io_stall_write_ms - fBase.io_stall_write_ms) / (fNow.num_of_writes - fBase.num_of_writes) DESC; + END; + + + /* SQL Server Internal Maintenance - Log File Growing - CheckID 13 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 13',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 13 AS CheckID, + 1 AS Priority, + 'SQL Server Internal Maintenance' AS FindingGroup, + 'Log File Growing' AS Finding, + 'https://www.brentozar.com/askbrent/file-growing/' AS URL, + 'Number of growths during the sample: ' + CAST(ps.value_delta AS NVARCHAR(20)) + @LineFeed + + 'Determined by sampling Perfmon counter ' + ps.object_name + ' - ' + ps.counter_name + @LineFeed AS Details, + 'Pre-grow data and log files during maintenance windows so that they do not grow during production loads. See the URL for more details.' AS HowToStopIt + FROM #PerfmonStats ps + WHERE ps.Pass = 2 + AND object_name = @ServiceName + ':Databases' + AND counter_name = 'Log Growths' + AND value_delta > 0; + + + /* SQL Server Internal Maintenance - Log File Shrinking - CheckID 14 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 14',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 14 AS CheckID, + 1 AS Priority, + 'SQL Server Internal Maintenance' AS FindingGroup, + 'Log File Shrinking' AS Finding, + 'https://www.brentozar.com/askbrent/file-shrinking/' AS URL, + 'Number of shrinks during the sample: ' + CAST(ps.value_delta AS NVARCHAR(20)) + @LineFeed + + 'Determined by sampling Perfmon counter ' + ps.object_name + ' - ' + ps.counter_name + @LineFeed AS Details, + 'Pre-grow data and log files during maintenance windows so that they do not grow during production loads. See the URL for more details.' AS HowToStopIt + FROM #PerfmonStats ps + WHERE ps.Pass = 2 + AND object_name = @ServiceName + ':Databases' + AND counter_name = 'Log Shrinks' + AND value_delta > 0; + + /* Query Problems - Compilations/Sec High - CheckID 15 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 15',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 15 AS CheckID, + 50 AS Priority, + 'Query Problems' AS FindingGroup, + 'Compilations/Sec High' AS Finding, + 'https://www.brentozar.com/askbrent/compilations/' AS URL, + 'Number of batch requests during the sample: ' + CAST(ps.value_delta AS NVARCHAR(20)) + @LineFeed + + 'Number of compilations during the sample: ' + CAST(psComp.value_delta AS NVARCHAR(20)) + @LineFeed + + 'For OLTP environments, Microsoft recommends that 90% of batch requests should hit the plan cache, and not be compiled from scratch. We are exceeding that threshold.' + @LineFeed AS Details, + 'To find the queries that are compiling, start with:' + @LineFeed + + 'sp_BlitzCache @SortOrder = ''recent compilations''' + @LineFeed + + 'If dynamic SQL or non-parameterized strings are involved, consider enabling Forced Parameterization. See the URL for more details.' AS HowToStopIt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats psComp ON psComp.Pass = 2 AND psComp.object_name = @ServiceName + ':SQL Statistics' AND psComp.counter_name = 'SQL Compilations/sec' AND psComp.value_delta > 0 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':SQL Statistics' + AND ps.counter_name = 'Batch Requests/sec' + AND psComp.value_delta > 75 /* Because sp_BlitzFirst does around 50 compilations and re-compilations */ + AND (psComp.value_delta > (10 * @Seconds) OR psComp.value_delta > ps.value_delta) /* Either doing 10 compilations per second, or more compilations than queries */ + AND (psComp.value_delta * 10) > ps.value_delta; /* Compilations are more than 10% of batch requests per second */ + + /* Query Problems - Re-Compilations/Sec High - CheckID 16 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 16',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 16 AS CheckID, + 50 AS Priority, + 'Query Problems' AS FindingGroup, + 'Re-Compilations/Sec High' AS Finding, + 'https://www.brentozar.com/askbrent/recompilations/' AS URL, + 'Number of batch requests during the sample: ' + CAST(ps.value_delta AS NVARCHAR(20)) + @LineFeed + + 'Number of recompilations during the sample: ' + CAST(psComp.value_delta AS NVARCHAR(20)) + @LineFeed + + 'More than 10% of our queries are being recompiled. This is typically due to statistics changing on objects.' + @LineFeed AS Details, + 'To find the queries that are being forced to recompile, start with:' + @LineFeed + + 'sp_BlitzCache @SortOrder = ''recent compilations''' + @LineFeed + + 'Examine those plans to find out which objects are changing so quickly that they hit the stats update threshold. See the URL for more details.' AS HowToStopIt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats psComp ON psComp.Pass = 2 AND psComp.object_name = @ServiceName + ':SQL Statistics' AND psComp.counter_name = 'SQL Re-Compilations/sec' AND psComp.value_delta > 0 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':SQL Statistics' + AND ps.counter_name = 'Batch Requests/sec' + AND psComp.value_delta > 75 /* Because sp_BlitzFirst does around 50 compilations and re-compilations */ + AND (psComp.value_delta > (10 * @Seconds) OR psComp.value_delta > ps.value_delta) /* Either doing 10 recompilations per second, or more recompilations than queries */ + AND (psComp.value_delta * 10) > ps.value_delta; /* Recompilations are more than 10% of batch requests per second */ + + /* Table Problems - Forwarded Fetches/Sec High - CheckID 29 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 29',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 29 AS CheckID, + 40 AS Priority, + 'Table Problems' AS FindingGroup, + 'Forwarded Fetches/Sec High' AS Finding, + 'https://www.brentozar.com/go/fetch/' AS URL, + CAST(ps.value_delta AS NVARCHAR(20)) + ' forwarded fetches (from SQLServer:Access Methods counter)' + @LineFeed + + 'Check your heaps: they need to be rebuilt, or they need a clustered index applied.' + @LineFeed AS Details, + 'Rebuild your heaps. If you use Ola Hallengren maintenance scripts, those do not rebuild heaps by default: https://www.brentozar.com/archive/2016/07/fix-forwarded-records/' AS HowToStopIt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats psComp ON psComp.Pass = 2 AND psComp.object_name = @ServiceName + ':Access Methods' AND psComp.counter_name = 'Forwarded Records/sec' AND psComp.value_delta > 100 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':Access Methods' + AND ps.counter_name = 'Forwarded Records/sec' + AND ps.value_delta > (100 * @Seconds); /* Ignore servers sitting idle */ + + /* Check for temp objects with high forwarded fetches. + This has to be done as dynamic SQL because we have to execute OBJECT_NAME inside TempDB. */ + IF EXISTS (SELECT * FROM #BlitzFirstResults WHERE CheckID = 29) + BEGIN + SET @StringToExecute = N' + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT TOP 10 29 AS CheckID, + 40 AS Priority, + ''Table Problems'' AS FindingGroup, + ''Forwarded Fetches/Sec High: TempDB Object'' AS Finding, + ''https://www.brentozar.com/go/fetch/'' AS URL, + CAST(COALESCE(os.forwarded_fetch_count,0) - COALESCE(os_prior.forwarded_fetch_count,0) AS NVARCHAR(20)) + '' forwarded fetches on '' + + CASE WHEN OBJECT_NAME(os.object_id) IS NULL THEN ''an unknown table '' + WHEN LEN(OBJECT_NAME(os.object_id)) < 50 THEN ''a table variable, internal identifier '' + OBJECT_NAME(os.object_id) + ELSE ''a temp table '' + OBJECT_NAME(os.object_id) + END AS Details, + ''Look through your source code to find the object creating these objects, and tune the creation and population to reduce fetches. See the URL for details.'' AS HowToStopIt + FROM tempdb.sys.dm_db_index_operational_stats(DB_ID(''tempdb''), NULL, NULL, NULL) os + LEFT OUTER JOIN #TempdbOperationalStats os_prior ON os.object_id = os_prior.object_id + AND os.forwarded_fetch_count > os_prior.forwarded_fetch_count + WHERE os.database_id = DB_ID(''tempdb'') + AND os.forwarded_fetch_count - COALESCE(os_prior.forwarded_fetch_count,0) > 100 + ORDER BY os.forwarded_fetch_count DESC;' + + EXECUTE sp_executesql @StringToExecute; + END + + /* In-Memory OLTP - Garbage Collection in Progress - CheckID 31 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 31',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 31 AS CheckID, + 50 AS Priority, + 'In-Memory OLTP' AS FindingGroup, + 'Garbage Collection in Progress' AS Finding, + 'https://www.brentozar.com/go/garbage/' AS URL, + CAST(ps.value_delta AS NVARCHAR(50)) + ' rows processed (from SQL Server YYYY XTP Garbage Collection:Rows processed/sec counter)' + @LineFeed + + 'This can happen due to memory pressure (causing In-Memory OLTP to shrink its footprint) or' + @LineFeed + + 'due to transactional workloads that constantly insert/delete data.' AS Details, + 'Sadly, you cannot choose when garbage collection occurs. This is one of the many gotchas of Hekaton. Learn more: http://nedotter.com/archive/2016/04/row-version-lifecycle-for-in-memory-oltp/' AS HowToStopIt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats psComp ON psComp.Pass = 2 AND psComp.object_name LIKE '%XTP Garbage Collection' AND psComp.counter_name = 'Rows processed/sec' AND psComp.value_delta > 100 + WHERE ps.Pass = 2 + AND ps.object_name LIKE '%XTP Garbage Collection' + AND ps.counter_name = 'Rows processed/sec' + AND ps.value_delta > (100 * @Seconds); /* Ignore servers sitting idle */ + + /* In-Memory OLTP - Transactions Aborted - CheckID 32 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 32',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 32 AS CheckID, + 100 AS Priority, + 'In-Memory OLTP' AS FindingGroup, + 'Transactions Aborted' AS Finding, + 'https://www.brentozar.com/go/aborted/' AS URL, + CAST(ps.value_delta AS NVARCHAR(50)) + ' transactions aborted (from SQL Server YYYY XTP Transactions:Transactions aborted/sec counter)' + @LineFeed + + 'This may indicate that data is changing, or causing folks to retry their transactions, thereby increasing load.' AS Details, + 'Dig into your In-Memory OLTP transactions to figure out which ones are failing and being retried.' AS HowToStopIt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats psComp ON psComp.Pass = 2 AND psComp.object_name LIKE '%XTP Transactions' AND psComp.counter_name = 'Transactions aborted/sec' AND psComp.value_delta > 100 + WHERE ps.Pass = 2 + AND ps.object_name LIKE '%XTP Transactions' + AND ps.counter_name = 'Transactions aborted/sec' + AND ps.value_delta > (10 * @Seconds); /* Ignore servers sitting idle */ + + /* Query Problems - Suboptimal Plans/Sec High - CheckID 33 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 33',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 32 AS CheckID, + 100 AS Priority, + 'Query Problems' AS FindingGroup, + 'Suboptimal Plans/Sec High' AS Finding, + 'https://www.brentozar.com/go/suboptimal/' AS URL, + CAST(ps.value_delta AS NVARCHAR(50)) + ' plans reported in the ' + CAST(ps.instance_name AS NVARCHAR(100)) + ' workload group (from Workload GroupStats:Suboptimal plans/sec counter)' + @LineFeed + + 'Even if you are not using Resource Governor, it still tracks information about user queries, memory grants, etc.' AS Details, + 'Check out sp_BlitzCache to get more information about recent queries, or try sp_BlitzWho to see currently running queries.' AS HowToStopIt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats psComp ON psComp.Pass = 2 AND psComp.object_name = @ServiceName + ':Workload GroupStats' AND psComp.counter_name = 'Suboptimal plans/sec' AND psComp.value_delta > 100 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':Workload GroupStats' + AND ps.counter_name = 'Suboptimal plans/sec' + AND ps.value_delta > (10 * @Seconds); /* Ignore servers sitting idle */ + + /* Azure Performance - Database is Maxed Out - CheckID 41 */ + IF SERVERPROPERTY('EngineEdition') = 5 /*SERVERPROPERTY('Edition') = 'SQL Azure'*/ + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 41',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt) + SELECT 41 AS CheckID, + 10 AS Priority, + 'Azure Performance' AS FindingGroup, + 'Database is Maxed Out' AS Finding, + 'https://www.brentozar.com/go/maxedout' AS URL, + N'At ' + CONVERT(NVARCHAR(100), s.end_time ,121) + N', your database approached (or hit) your DTU limits:' + @LineFeed + + N'Average CPU percent: ' + CAST(avg_cpu_percent AS NVARCHAR(50)) + @LineFeed + + N'Average data IO percent: ' + CAST(avg_data_io_percent AS NVARCHAR(50)) + @LineFeed + + N'Average log write percent: ' + CAST(avg_log_write_percent AS NVARCHAR(50)) + @LineFeed + + N'Max worker percent: ' + CAST(max_worker_percent AS NVARCHAR(50)) + @LineFeed + + N'Max session percent: ' + CAST(max_session_percent AS NVARCHAR(50)) AS Details, + 'Tune your queries or indexes with sp_BlitzCache or sp_BlitzIndex, or consider upgrading to a higher DTU level.' AS HowToStopIt + FROM sys.dm_db_resource_stats s + WHERE s.end_time >= DATEADD(MI, -5, GETDATE()) + AND (avg_cpu_percent > 90 + OR avg_data_io_percent >= 90 + OR avg_log_write_percent >=90 + OR max_worker_percent >= 90 + OR max_session_percent >= 90); + END + + /* Server Info - Batch Requests per Sec - CheckID 19 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 19',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, DetailsInt) + SELECT 19 AS CheckID, + 250 AS Priority, + 'Server Info' AS FindingGroup, + 'Batch Requests per Sec' AS Finding, + 'https://www.brentozar.com/go/measure' AS URL, + CAST(CAST(ps.value_delta AS MONEY) / (DATEDIFF(ss, ps1.SampleTime, ps.SampleTime)) AS NVARCHAR(20)) AS Details, + ps.value_delta / (DATEDIFF(ss, ps1.SampleTime, ps.SampleTime)) AS DetailsInt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats ps1 ON ps.object_name = ps1.object_name AND ps.counter_name = ps1.counter_name AND ps1.Pass = 1 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':SQL Statistics' + AND ps.counter_name = 'Batch Requests/sec'; + + + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','SQL Compilations/sec', NULL); + INSERT INTO #PerfmonCounters ([object_name],[counter_name],[instance_name]) VALUES (@ServiceName + ':SQL Statistics','SQL Re-Compilations/sec', NULL); + + /* Server Info - SQL Compilations/sec - CheckID 25 */ + IF @ExpertMode = 1 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 25',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, DetailsInt) + SELECT 25 AS CheckID, + 250 AS Priority, + 'Server Info' AS FindingGroup, + 'SQL Compilations per Sec' AS Finding, + 'https://www.brentozar.com/go/measure' AS URL, + CAST(ps.value_delta / (DATEDIFF(ss, ps1.SampleTime, ps.SampleTime)) AS NVARCHAR(20)) AS Details, + ps.value_delta / (DATEDIFF(ss, ps1.SampleTime, ps.SampleTime)) AS DetailsInt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats ps1 ON ps.object_name = ps1.object_name AND ps.counter_name = ps1.counter_name AND ps1.Pass = 1 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':SQL Statistics' + AND ps.counter_name = 'SQL Compilations/sec'; + END + + /* Server Info - SQL Re-Compilations/sec - CheckID 26 */ + IF @ExpertMode = 1 + BEGIN + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 26',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, DetailsInt) + SELECT 26 AS CheckID, + 250 AS Priority, + 'Server Info' AS FindingGroup, + 'SQL Re-Compilations per Sec' AS Finding, + 'https://www.brentozar.com/go/measure' AS URL, + CAST(ps.value_delta / (DATEDIFF(ss, ps1.SampleTime, ps.SampleTime)) AS NVARCHAR(20)) AS Details, + ps.value_delta / (DATEDIFF(ss, ps1.SampleTime, ps.SampleTime)) AS DetailsInt + FROM #PerfmonStats ps + INNER JOIN #PerfmonStats ps1 ON ps.object_name = ps1.object_name AND ps.counter_name = ps1.counter_name AND ps1.Pass = 1 + WHERE ps.Pass = 2 + AND ps.object_name = @ServiceName + ':SQL Statistics' + AND ps.counter_name = 'SQL Re-Compilations/sec'; + END + + /* Server Info - Wait Time per Core per Sec - CheckID 20 */ + IF @Seconds > 0 + BEGIN; + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 20',10,1) WITH NOWAIT; + END; + + WITH waits1(SampleTime, waits_ms) AS (SELECT SampleTime, SUM(ws1.wait_time_ms) FROM #WaitStats ws1 WHERE ws1.Pass = 1 GROUP BY SampleTime), + waits2(SampleTime, waits_ms) AS (SELECT SampleTime, SUM(ws2.wait_time_ms) FROM #WaitStats ws2 WHERE ws2.Pass = 2 GROUP BY SampleTime), + cores(cpu_count) AS (SELECT SUM(1) FROM sys.dm_os_schedulers WHERE status = 'VISIBLE ONLINE' AND is_online = 1) + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details, DetailsInt) + SELECT 20 AS CheckID, + 250 AS Priority, + 'Server Info' AS FindingGroup, + 'Wait Time per Core per Sec' AS Finding, + 'https://www.brentozar.com/go/measure' AS URL, + CAST((CAST(waits2.waits_ms - waits1.waits_ms AS MONEY)) / 1000 / i.cpu_count / DATEDIFF(ss, waits1.SampleTime, waits2.SampleTime) AS NVARCHAR(20)) AS Details, + (waits2.waits_ms - waits1.waits_ms) / 1000 / i.cpu_count / DATEDIFF(ss, waits1.SampleTime, waits2.SampleTime) AS DetailsInt + FROM cores i + CROSS JOIN waits1 + CROSS JOIN waits2; + END; + + IF @Seconds > 0 + BEGIN + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, URL, Details) + SELECT + 47 AS CheckId, + 50 AS Priority, + 'Query Problems' AS FindingsGroup, + 'High Percentage Of Runnable Queries' AS Finding, + 'https://erikdarlingdata.com/go/RunnableQueue/' AS URL, + 'On the ' + + CASE WHEN y.pass = 1 + THEN '1st' + ELSE '2nd' + END + + ' pass, ' + + RTRIM(y.runnable_pct) + + '% of your queries were waiting to get on a CPU to run. ' + + ' This can indicate CPU pressure.' + FROM + ( + SELECT + 2 AS pass, + x.total, + x.runnable, + CONVERT(decimal(5,2), + ( + x.runnable / + (1. * NULLIF(x.total, 0)) + ) + ) * 100. AS runnable_pct + FROM + ( + SELECT + COUNT_BIG(*) AS total, + SUM(CASE WHEN status = 'runnable' + THEN 1 + ELSE 0 + END) AS runnable + FROM sys.dm_exec_requests + WHERE session_id > 50 + ) AS x + ) AS y + WHERE y.runnable_pct > 20.; + END + + /* If we're waiting 30+ seconds, run these checks at the end. + We get this data from the ring buffers, and it's only updated once per minute, so might + as well get it now - whereas if we're checking 30+ seconds, it might get updated by the + end of our sp_BlitzFirst session. */ + IF @Seconds >= 30 + BEGIN + /* Server Performance - High CPU Utilization CheckID 24 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 24',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 24, 50, 'Server Performance', 'High CPU Utilization', CAST(100 - SystemIdle AS NVARCHAR(20)) + N'%. Ring buffer details: ' + CAST(record AS NVARCHAR(4000)), 100 - SystemIdle, 'https://www.brentozar.com/go/cpu' + FROM ( + SELECT record, + record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle + FROM ( + SELECT TOP 1 CONVERT(XML, record) AS record + FROM sys.dm_os_ring_buffers + WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' + AND record LIKE '%%' + ORDER BY timestamp DESC) AS rb + ) AS y + WHERE 100 - SystemIdle >= 50; + + /* Server Performance - CPU Utilization CheckID 23 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 23',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults (CheckID, Priority, FindingsGroup, Finding, Details, DetailsInt, URL) + SELECT 23, 250, 'Server Info', 'CPU Utilization', CAST(100 - SystemIdle AS NVARCHAR(20)) + N'%. Ring buffer details: ' + CAST(record AS NVARCHAR(4000)), 100 - SystemIdle, 'https://www.brentozar.com/go/cpu' + FROM ( + SELECT record, + record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle + FROM ( + SELECT TOP 1 CONVERT(XML, record) AS record + FROM sys.dm_os_ring_buffers + WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' + AND record LIKE '%%' + ORDER BY timestamp DESC) AS rb + ) AS y; + + END; /* IF @Seconds >= 30 */ + + IF /* Let people on <2016 know about the thread time column */ + ( + @Seconds > 0 + AND @total_cpu_usage = 0 + ) + BEGIN + INSERT INTO + #BlitzFirstResults + ( + CheckID, + Priority, + FindingsGroup, + Finding, + Details, + URL + ) + SELECT + 48, + 254, + N'Informational', + N'Thread Time comes from the plan cache in versions earlier than 2016, and is not as reliable', + N'The oldest plan in your cache is from ' + + CONVERT(nvarchar(30), MIN(s.creation_time)) + + N' and your server was last restarted on ' + + CONVERT(nvarchar(30), MAX(o.sqlserver_start_time)), + N'https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-os-schedulers-transact-sql' + FROM sys.dm_exec_query_stats AS s + CROSS JOIN sys.dm_os_sys_info AS o + OPTION(RECOMPILE); + END /* Let people on <2016 know about the thread time column */ + + /* If we didn't find anything, apologize. */ + IF NOT EXISTS (SELECT * FROM #BlitzFirstResults WHERE Priority < 250) + BEGIN + + INSERT INTO #BlitzFirstResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES ( -1 , + 1 , + 'No Problems Found' , + 'From Your Community Volunteers' , + 'http://FirstResponderKit.org/' , + 'Try running our more in-depth checks with sp_Blitz, or there may not be an unusual SQL Server performance problem. ' + ); + + END; /*IF NOT EXISTS (SELECT * FROM #BlitzFirstResults) */ + + /* Add credits for the nice folks who put so much time into building and maintaining this for free: */ + INSERT INTO #BlitzFirstResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + VALUES ( -1 , + 255 , + 'Thanks!' , + 'From Your Community Volunteers' , + 'http://FirstResponderKit.org/' , + 'To get help or add your own contributions, join us at http://FirstResponderKit.org.' + ); + + INSERT INTO #BlitzFirstResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + + ) + VALUES ( -1 , + 0 , + 'sp_BlitzFirst ' + CAST(CONVERT(DATETIMEOFFSET, @VersionDate, 102) AS VARCHAR(100)), + 'From Your Community Volunteers' , + 'http://FirstResponderKit.org/' , + 'We hope you found this tool useful.' + ); + + /* Outdated sp_BlitzFirst - sp_BlitzFirst is Over 6 Months Old - CheckID 27 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 27',10,1) WITH NOWAIT; + END + + IF DATEDIFF(MM, @VersionDate, SYSDATETIMEOFFSET()) > 6 + BEGIN + INSERT INTO #BlitzFirstResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 27 AS CheckID , + 0 AS Priority , + 'Outdated sp_BlitzFirst' AS FindingsGroup , + 'sp_BlitzFirst is Over 6 Months Old' AS Finding , + 'http://FirstResponderKit.org/' AS URL , + 'Some things get better with age, like fine wine and your T-SQL. However, sp_BlitzFirst is not one of those things - time to go download the current one.' AS Details; + END; + + IF @CheckServerInfo = 0 /* Github #1680 */ + BEGIN + DELETE #BlitzFirstResults + WHERE FindingsGroup = 'Server Info'; + END + + RAISERROR('Analysis finished, outputting results',10,1) WITH NOWAIT; + + + /* If they want to run sp_BlitzCache and export to table, go for it. */ + IF @OutputTableNameBlitzCache IS NOT NULL + AND @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + + + RAISERROR('Calling sp_BlitzCache',10,1) WITH NOWAIT; + + + /* If they have an newer version of sp_BlitzCache that supports @MinutesBack and @CheckDateOverride */ + IF EXISTS (SELECT * FROM sys.objects o + INNER JOIN sys.parameters pMB ON o.object_id = pMB.object_id AND pMB.name = '@MinutesBack' + INNER JOIN sys.parameters pCDO ON o.object_id = pCDO.object_id AND pCDO.name = '@CheckDateOverride' + WHERE o.name = 'sp_BlitzCache') + BEGIN + /* Get the most recent sp_BlitzCache execution before this one - don't use sp_BlitzFirst because user logs are added in there at any time */ + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + ''' AND QUOTENAME(TABLE_NAME) = ''' + + QUOTENAME(@OutputTableNameBlitzCache) + ''') SELECT TOP 1 @BlitzCacheMinutesBack = DATEDIFF(MI,CheckDate,SYSDATETIMEOFFSET()) FROM ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + QUOTENAME(@OutputTableNameBlitzCache) + + ' WHERE ServerName = ''' + CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)) + ''' ORDER BY CheckDate DESC;'; + EXEC sp_executesql @StringToExecute, N'@BlitzCacheMinutesBack INT OUTPUT', @BlitzCacheMinutesBack OUTPUT; + + /* If there's no data, let's just analyze the last 15 minutes of the plan cache */ + IF @BlitzCacheMinutesBack IS NULL OR @BlitzCacheMinutesBack < 1 OR @BlitzCacheMinutesBack > 60 + SET @BlitzCacheMinutesBack = 15; + + IF(@OutputType = 'NONE') + BEGIN + EXEC sp_BlitzCache + @OutputDatabaseName = @UnquotedOutputDatabaseName, + @OutputSchemaName = @UnquotedOutputSchemaName, + @OutputTableName = @OutputTableNameBlitzCache, + @CheckDateOverride = @StartSampleTime, + @SortOrder = 'all', + @SkipAnalysis = @BlitzCacheSkipAnalysis, + @MinutesBack = @BlitzCacheMinutesBack, + @Debug = @Debug, + @OutputType = @OutputType + ; + END; + ELSE + BEGIN + + EXEC sp_BlitzCache + @OutputDatabaseName = @UnquotedOutputDatabaseName, + @OutputSchemaName = @UnquotedOutputSchemaName, + @OutputTableName = @OutputTableNameBlitzCache, + @CheckDateOverride = @StartSampleTime, + @SortOrder = 'all', + @SkipAnalysis = @BlitzCacheSkipAnalysis, + @MinutesBack = @BlitzCacheMinutesBack, + @Debug = @Debug + ; + END; + + /* Delete history older than @OutputTableRetentionDays */ + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') DELETE ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + QUOTENAME(@OutputTableNameBlitzCache) + + ' WHERE ServerName = @SrvName AND CheckDate < @CheckDate;'; + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate date', + @LocalServerName, @OutputTableCleanupDate; + + + END; + + ELSE + BEGIN + /* No sp_BlitzCache found, or it's outdated - CheckID 36 */ + IF (@Debug = 1) + BEGIN + RAISERROR('Running CheckID 36',10,1) WITH NOWAIT; + END + + INSERT INTO #BlitzFirstResults + ( CheckID , + Priority , + FindingsGroup , + Finding , + URL , + Details + ) + SELECT 36 AS CheckID , + 0 AS Priority , + 'Outdated or Missing sp_BlitzCache' AS FindingsGroup , + 'Update Your sp_BlitzCache' AS Finding , + 'http://FirstResponderKit.org/' AS URL , + 'You passed in @OutputTableNameBlitzCache, but we need a newer version of sp_BlitzCache in master or the current database.' AS Details; + END; + + RAISERROR('sp_BlitzCache Finished',10,1) WITH NOWAIT; + + END; /* End running sp_BlitzCache */ + + /* @OutputTableName lets us export the results to a permanent table */ + IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableName IS NOT NULL + AND @OutputTableName NOT LIKE '#%' + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + ''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + ''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableName + ''') CREATE TABLE ' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + CheckID INT NOT NULL, + Priority TINYINT NOT NULL, + FindingsGroup VARCHAR(50) NOT NULL, + Finding VARCHAR(200) NOT NULL, + URL VARCHAR(200) NOT NULL, + Details NVARCHAR(4000) NULL, + HowToStopIt [XML] NULL, + QueryPlan [XML] NULL, + QueryText NVARCHAR(MAX) NULL, + StartTime DATETIMEOFFSET NULL, + LoginName NVARCHAR(128) NULL, + NTUserName NVARCHAR(128) NULL, + OriginalLoginName NVARCHAR(128) NULL, + ProgramName NVARCHAR(128) NULL, + HostName NVARCHAR(128) NULL, + DatabaseID INT NULL, + DatabaseName NVARCHAR(128) NULL, + OpenTransactionCount INT NULL, + DetailsInt INT NULL, + QueryHash BINARY(8) NULL, + JoinKey AS ServerName + CAST(CheckDate AS NVARCHAR(50)), + PRIMARY KEY CLUSTERED (ID ASC));'; + + EXEC(@StringToExecute); + + /* If the table doesn't have the new QueryHash column, add it. See Github #2162. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''QueryHash'') + ALTER TABLE ' + @ObjectFullName + N' ADD QueryHash BINARY(8) NULL;'; + EXEC(@StringToExecute); + + /* If the table doesn't have the new JoinKey computed column, add it. See Github #2164. */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableName; + SET @StringToExecute = N'IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''JoinKey'') + ALTER TABLE ' + @ObjectFullName + N' ADD JoinKey AS ServerName + CAST(CheckDate AS NVARCHAR(50));'; + EXEC(@StringToExecute); + + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, QueryText, StartTime, LoginName, NTUserName, OriginalLoginName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, DetailsInt, QueryHash) SELECT ' + + ' @SrvName, @CheckDate, CheckID, Priority, FindingsGroup, Finding, URL, LEFT(Details,4000), HowToStopIt, QueryPlan, QueryText, StartTime, LoginName, NTUserName, OriginalLoginName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, DetailsInt, QueryHash FROM #BlitzFirstResults ORDER BY Priority , FindingsGroup , Finding , Details'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + + /* Delete history older than @OutputTableRetentionDays */ + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') DELETE ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableName + + ' WHERE ServerName = @SrvName AND CheckDate < @CheckDate ;'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate date', + @LocalServerName, @OutputTableCleanupDate; + + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 2) = '##') + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''tempdb..' + + @OutputTableName + + ''') IS NULL) CREATE TABLE ' + + @OutputTableName + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + CheckID INT NOT NULL, + Priority TINYINT NOT NULL, + FindingsGroup VARCHAR(50) NOT NULL, + Finding VARCHAR(200) NOT NULL, + URL VARCHAR(200) NOT NULL, + Details NVARCHAR(4000) NULL, + HowToStopIt [XML] NULL, + QueryPlan [XML] NULL, + QueryText NVARCHAR(MAX) NULL, + StartTime DATETIMEOFFSET NULL, + LoginName NVARCHAR(128) NULL, + NTUserName NVARCHAR(128) NULL, + OriginalLoginName NVARCHAR(128) NULL, + ProgramName NVARCHAR(128) NULL, + HostName NVARCHAR(128) NULL, + DatabaseID INT NULL, + DatabaseName NVARCHAR(128) NULL, + OpenTransactionCount INT NULL, + DetailsInt INT NULL, + QueryHash BINARY(8) NULL, + JoinKey AS ServerName + CAST(CheckDate AS NVARCHAR(50)), + PRIMARY KEY CLUSTERED (ID ASC));' + + ' INSERT ' + + @OutputTableName + + ' (ServerName, CheckDate, CheckID, Priority, FindingsGroup, Finding, URL, Details, HowToStopIt, QueryPlan, QueryText, StartTime, LoginName, NTUserName, OriginalLoginName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, DetailsInt) SELECT ' + + ' @SrvName, @CheckDate, CheckID, Priority, FindingsGroup, Finding, URL, LEFT(Details,4000), HowToStopIt, QueryPlan, QueryText, StartTime, LoginName, NTUserName, OriginalLoginName, ProgramName, HostName, DatabaseID, DatabaseName, OpenTransactionCount, DetailsInt FROM #BlitzFirstResults ORDER BY Priority , FindingsGroup , Finding , Details'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + END; + ELSE IF (SUBSTRING(@OutputTableName, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); + END; + + /* @OutputTableNameFileStats lets us export the results to a permanent table */ + IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableNameFileStats IS NOT NULL + AND @OutputTableNameFileStats NOT LIKE '#%' + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + /* Create the table */ + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + ''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + ''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableNameFileStats + ''') CREATE TABLE ' + + @OutputSchemaName + '.' + + @OutputTableNameFileStats + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + DatabaseID INT NOT NULL, + FileID INT NOT NULL, + DatabaseName NVARCHAR(256) , + FileLogicalName NVARCHAR(256) , + TypeDesc NVARCHAR(60) , + SizeOnDiskMB BIGINT , + io_stall_read_ms BIGINT , + num_of_reads BIGINT , + bytes_read BIGINT , + io_stall_write_ms BIGINT , + num_of_writes BIGINT , + bytes_written BIGINT, + PhysicalName NVARCHAR(520) , + PRIMARY KEY CLUSTERED (ID ASC));'; + + EXEC(@StringToExecute); + + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableNameFileStats_View; + + /* If the view exists without the most recently added columns, drop it. See Github #2162. */ + IF OBJECT_ID(@ObjectFullName) IS NOT NULL + BEGIN + SET @StringToExecute = N'USE ' + @OutputDatabaseName + N'; IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''JoinKey'') + DROP VIEW ' + @OutputSchemaName + N'.' + @OutputTableNameFileStats_View + N';'; + + EXEC(@StringToExecute); + END + + /* Create the view */ + IF OBJECT_ID(@ObjectFullName) IS NULL + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; EXEC (''CREATE VIEW ' + + @OutputSchemaName + '.' + + @OutputTableNameFileStats_View + ' AS ' + @LineFeed + + 'WITH RowDates as' + @LineFeed + + '(' + @LineFeed + + ' SELECT ' + @LineFeed + + ' ROW_NUMBER() OVER (ORDER BY [ServerName], [CheckDate]) ID,' + @LineFeed + + ' [CheckDate]' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNameFileStats + '' + @LineFeed + + ' GROUP BY [ServerName], [CheckDate]' + @LineFeed + + '),' + @LineFeed + + 'CheckDates as' + @LineFeed + + '(' + @LineFeed + + ' SELECT ThisDate.CheckDate,' + @LineFeed + + ' LastDate.CheckDate as PreviousCheckDate' + @LineFeed + + ' FROM RowDates ThisDate' + @LineFeed + + ' JOIN RowDates LastDate' + @LineFeed + + ' ON ThisDate.ID = LastDate.ID + 1' + @LineFeed + + ')' + @LineFeed + + ' SELECT f.ServerName,' + @LineFeed + + ' f.CheckDate,' + @LineFeed + + ' f.DatabaseID,' + @LineFeed + + ' f.DatabaseName,' + @LineFeed + + ' f.FileID,' + @LineFeed + + ' f.FileLogicalName,' + @LineFeed + + ' f.TypeDesc,' + @LineFeed + + ' f.PhysicalName,' + @LineFeed + + ' f.SizeOnDiskMB,' + @LineFeed + + ' DATEDIFF(ss, fPrior.CheckDate, f.CheckDate) AS ElapsedSeconds,' + @LineFeed + + ' (f.SizeOnDiskMB - fPrior.SizeOnDiskMB) AS SizeOnDiskMBgrowth,' + @LineFeed + + ' (f.io_stall_read_ms - fPrior.io_stall_read_ms) AS io_stall_read_ms,' + @LineFeed + + ' io_stall_read_ms_average = CASE' + @LineFeed + + ' WHEN(f.num_of_reads - fPrior.num_of_reads) = 0' + @LineFeed + + ' THEN 0' + @LineFeed + + ' ELSE(f.io_stall_read_ms - fPrior.io_stall_read_ms) / (f.num_of_reads - fPrior.num_of_reads)' + @LineFeed + + ' END,' + @LineFeed + + ' (f.num_of_reads - fPrior.num_of_reads) AS num_of_reads,' + @LineFeed + + ' (f.bytes_read - fPrior.bytes_read) / 1024.0 / 1024.0 AS megabytes_read,' + @LineFeed + + ' (f.io_stall_write_ms - fPrior.io_stall_write_ms) AS io_stall_write_ms,' + @LineFeed + + ' io_stall_write_ms_average = CASE' + @LineFeed + + ' WHEN(f.num_of_writes - fPrior.num_of_writes) = 0' + @LineFeed + + ' THEN 0' + @LineFeed + + ' ELSE(f.io_stall_write_ms - fPrior.io_stall_write_ms) / (f.num_of_writes - fPrior.num_of_writes)' + @LineFeed + + ' END,' + @LineFeed + + ' (f.num_of_writes - fPrior.num_of_writes) AS num_of_writes,' + @LineFeed + + ' (f.bytes_written - fPrior.bytes_written) / 1024.0 / 1024.0 AS megabytes_written, ' + @LineFeed + + ' f.ServerName + CAST(f.CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNameFileStats + ' f' + @LineFeed + + ' INNER HASH JOIN CheckDates DATES ON f.CheckDate = DATES.CheckDate' + @LineFeed + + ' INNER JOIN ' + @OutputSchemaName + '.' + @OutputTableNameFileStats + ' fPrior ON f.ServerName = fPrior.ServerName' + @LineFeed + + ' AND f.DatabaseID = fPrior.DatabaseID' + @LineFeed + + ' AND f.FileID = fPrior.FileID' + @LineFeed + + ' AND fPrior.CheckDate = DATES.PreviousCheckDate' + @LineFeed + + '' + @LineFeed + + ' WHERE f.num_of_reads >= fPrior.num_of_reads' + @LineFeed + + ' AND f.num_of_writes >= fPrior.num_of_writes' + @LineFeed + + ' AND DATEDIFF(MI, fPrior.CheckDate, f.CheckDate) BETWEEN 1 AND 60;'')' + + EXEC(@StringToExecute); + END; + + + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableNameFileStats + + ' (ServerName, CheckDate, DatabaseID, FileID, DatabaseName, FileLogicalName, TypeDesc, SizeOnDiskMB, io_stall_read_ms, num_of_reads, bytes_read, io_stall_write_ms, num_of_writes, bytes_written, PhysicalName) SELECT ' + + ' @SrvName, @CheckDate, DatabaseID, FileID, DatabaseName, FileLogicalName, TypeDesc, SizeOnDiskMB, io_stall_read_ms, num_of_reads, bytes_read, io_stall_write_ms, num_of_writes, bytes_written, PhysicalName FROM #FileStats WHERE Pass = 2'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + + /* Delete history older than @OutputTableRetentionDays */ + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') DELETE ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableNameFileStats + + ' WHERE ServerName = @SrvName AND CheckDate < @CheckDate ;'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate date', + @LocalServerName, @OutputTableCleanupDate; + + END; + ELSE IF (SUBSTRING(@OutputTableNameFileStats, 2, 2) = '##') + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''tempdb..' + + @OutputTableNameFileStats + + ''') IS NULL) CREATE TABLE ' + + @OutputTableNameFileStats + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + DatabaseID INT NOT NULL, + FileID INT NOT NULL, + DatabaseName NVARCHAR(256) , + FileLogicalName NVARCHAR(256) , + TypeDesc NVARCHAR(60) , + SizeOnDiskMB BIGINT , + io_stall_read_ms BIGINT , + num_of_reads BIGINT , + bytes_read BIGINT , + io_stall_write_ms BIGINT , + num_of_writes BIGINT , + bytes_written BIGINT, + PhysicalName NVARCHAR(520) , + DetailsInt INT NULL, + PRIMARY KEY CLUSTERED (ID ASC));' + + ' INSERT ' + + @OutputTableNameFileStats + + ' (ServerName, CheckDate, DatabaseID, FileID, DatabaseName, FileLogicalName, TypeDesc, SizeOnDiskMB, io_stall_read_ms, num_of_reads, bytes_read, io_stall_write_ms, num_of_writes, bytes_written, PhysicalName) SELECT ' + + ' @SrvName, @CheckDate, DatabaseID, FileID, DatabaseName, FileLogicalName, TypeDesc, SizeOnDiskMB, io_stall_read_ms, num_of_reads, bytes_read, io_stall_write_ms, num_of_writes, bytes_written, PhysicalName FROM #FileStats WHERE Pass = 2'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + END; + ELSE IF (SUBSTRING(@OutputTableNameFileStats, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); + END; + + + /* @OutputTableNamePerfmonStats lets us export the results to a permanent table */ + IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableNamePerfmonStats IS NOT NULL + AND @OutputTableNamePerfmonStats NOT LIKE '#%' + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + /* Create the table */ + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + ''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + ''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableNamePerfmonStats + ''') CREATE TABLE ' + + @OutputSchemaName + '.' + + @OutputTableNamePerfmonStats + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + [object_name] NVARCHAR(128) NOT NULL, + [counter_name] NVARCHAR(128) NOT NULL, + [instance_name] NVARCHAR(128) NULL, + [cntr_value] BIGINT NULL, + [cntr_type] INT NOT NULL, + [value_delta] BIGINT NULL, + [value_per_second] DECIMAL(18,2) NULL, + PRIMARY KEY CLUSTERED (ID ASC));'; + + EXEC(@StringToExecute); + + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableNamePerfmonStats_View; + + /* If the view exists without the most recently added columns, drop it. See Github #2162. */ + IF OBJECT_ID(@ObjectFullName) IS NOT NULL + BEGIN + SET @StringToExecute = N'USE ' + @OutputDatabaseName + N'; IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''JoinKey'') + DROP VIEW ' + @OutputSchemaName + N'.' + @OutputTableNamePerfmonStats_View + N';'; + + EXEC(@StringToExecute); + END + + /* Create the view */ + IF OBJECT_ID(@ObjectFullName) IS NULL + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; EXEC (''CREATE VIEW ' + + @OutputSchemaName + '.' + + @OutputTableNamePerfmonStats_View + ' AS ' + @LineFeed + + 'WITH RowDates as' + @LineFeed + + '(' + @LineFeed + + ' SELECT ' + @LineFeed + + ' ROW_NUMBER() OVER (ORDER BY [ServerName], [CheckDate]) ID,' + @LineFeed + + ' [CheckDate]' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' +@OutputTableNamePerfmonStats + '' + @LineFeed + + ' GROUP BY [ServerName], [CheckDate]' + @LineFeed + + '),' + @LineFeed + + 'CheckDates as' + @LineFeed + + '(' + @LineFeed + + ' SELECT ThisDate.CheckDate,' + @LineFeed + + ' LastDate.CheckDate as PreviousCheckDate' + @LineFeed + + ' FROM RowDates ThisDate' + @LineFeed + + ' JOIN RowDates LastDate' + @LineFeed + + ' ON ThisDate.ID = LastDate.ID + 1' + @LineFeed + + ')' + @LineFeed + + 'SELECT' + @LineFeed + + ' pMon.[ServerName]' + @LineFeed + + ' ,pMon.[CheckDate]' + @LineFeed + + ' ,pMon.[object_name]' + @LineFeed + + ' ,pMon.[counter_name]' + @LineFeed + + ' ,pMon.[instance_name]' + @LineFeed + + ' ,DATEDIFF(SECOND,pMonPrior.[CheckDate],pMon.[CheckDate]) AS ElapsedSeconds' + @LineFeed + + ' ,pMon.[cntr_value]' + @LineFeed + + ' ,pMon.[cntr_type]' + @LineFeed + + ' ,(pMon.[cntr_value] - pMonPrior.[cntr_value]) AS cntr_delta' + @LineFeed + + ' ,(pMon.cntr_value - pMonPrior.cntr_value) * 1.0 / DATEDIFF(ss, pMonPrior.CheckDate, pMon.CheckDate) AS cntr_delta_per_second' + @LineFeed + + ' ,pMon.ServerName + CAST(pMon.CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' +@OutputTableNamePerfmonStats + ' pMon' + @LineFeed + + ' INNER HASH JOIN CheckDates Dates' + @LineFeed + + ' ON Dates.CheckDate = pMon.CheckDate' + @LineFeed + + ' JOIN ' + @OutputSchemaName + '.' +@OutputTableNamePerfmonStats + ' pMonPrior' + @LineFeed + + ' ON Dates.PreviousCheckDate = pMonPrior.CheckDate' + @LineFeed + + ' AND pMon.[ServerName] = pMonPrior.[ServerName] ' + @LineFeed + + ' AND pMon.[object_name] = pMonPrior.[object_name] ' + @LineFeed + + ' AND pMon.[counter_name] = pMonPrior.[counter_name] ' + @LineFeed + + ' AND pMon.[instance_name] = pMonPrior.[instance_name]' + @LineFeed + + ' WHERE DATEDIFF(MI, pMonPrior.CheckDate, pMon.CheckDate) BETWEEN 1 AND 60;'')' + + EXEC(@StringToExecute); + END + + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableNamePerfmonStatsActuals_View; + + /* If the view exists without the most recently added columns, drop it. See Github #2162. */ + IF OBJECT_ID(@ObjectFullName) IS NOT NULL + BEGIN + SET @StringToExecute = N'USE ' + @OutputDatabaseName + N'; IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''JoinKey'') + DROP VIEW ' + @OutputSchemaName + N'.' + @OutputTableNamePerfmonStatsActuals_View + N';'; + + EXEC(@StringToExecute); + END + + /* Create the second view */ + IF OBJECT_ID(@ObjectFullName) IS NULL + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; EXEC (''CREATE VIEW ' + + @OutputSchemaName + '.' + + @OutputTableNamePerfmonStatsActuals_View + ' AS ' + @LineFeed + + 'WITH PERF_AVERAGE_BULK AS' + @LineFeed + + '(' + @LineFeed + + ' SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' counter_name,' + @LineFeed + + ' CASE WHEN CHARINDEX(''''('''', counter_name) = 0 THEN counter_name ELSE LEFT (counter_name, CHARINDEX(''''('''',counter_name)-1) END AS counter_join,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_delta' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNamePerfmonStats_View + @LineFeed + + ' WHERE cntr_type IN(1073874176)' + @LineFeed + + ' AND cntr_delta <> 0' + @LineFeed + + '),' + @LineFeed + + 'PERF_LARGE_RAW_BASE AS' + @LineFeed + + '(' + @LineFeed + + ' SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' LEFT(counter_name, CHARINDEX(''''BASE'''', UPPER(counter_name))-1) AS counter_join,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_delta' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNamePerfmonStats_View + '' + @LineFeed + + ' WHERE cntr_type IN(1073939712)' + @LineFeed + + ' AND cntr_delta <> 0' + @LineFeed + + '),' + @LineFeed + + 'PERF_AVERAGE_FRACTION AS' + @LineFeed + + '(' + @LineFeed + + ' SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' counter_name,' + @LineFeed + + ' counter_name AS counter_join,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_delta' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNamePerfmonStats_View + '' + @LineFeed + + ' WHERE cntr_type IN(537003264)' + @LineFeed + + ' AND cntr_delta <> 0' + @LineFeed + + '),' + @LineFeed + + 'PERF_COUNTER_BULK_COUNT AS' + @LineFeed + + '(' + @LineFeed + + ' SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' counter_name,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_delta / ElapsedSeconds AS cntr_value' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNamePerfmonStats_View + '' + @LineFeed + + ' WHERE cntr_type IN(272696576, 272696320)' + @LineFeed + + ' AND cntr_delta <> 0' + @LineFeed + + '),' + @LineFeed + + 'PERF_COUNTER_RAWCOUNT AS' + @LineFeed + + '(' + @LineFeed + + ' SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' counter_name,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_value' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNamePerfmonStats_View + '' + @LineFeed + + ' WHERE cntr_type IN(65792, 65536)' + @LineFeed + + ')' + @LineFeed + + '' + @LineFeed + + 'SELECT NUM.ServerName,' + @LineFeed + + ' NUM.object_name,' + @LineFeed + + ' NUM.counter_name,' + @LineFeed + + ' NUM.instance_name,' + @LineFeed + + ' NUM.CheckDate,' + @LineFeed + + ' NUM.cntr_delta / DEN.cntr_delta AS cntr_value,' + @LineFeed + + ' NUM.ServerName + CAST(NUM.CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + ' ' + @LineFeed + + 'FROM PERF_AVERAGE_BULK AS NUM' + @LineFeed + + ' JOIN PERF_LARGE_RAW_BASE AS DEN ON NUM.counter_join = DEN.counter_join' + @LineFeed + + ' AND NUM.CheckDate = DEN.CheckDate' + @LineFeed + + ' AND NUM.ServerName = DEN.ServerName' + @LineFeed + + ' AND NUM.object_name = DEN.object_name' + @LineFeed + + ' AND NUM.instance_name = DEN.instance_name' + @LineFeed + + ' AND DEN.cntr_delta <> 0' + @LineFeed + + '' + @LineFeed + + 'UNION ALL' + @LineFeed + + '' + @LineFeed + + 'SELECT NUM.ServerName,' + @LineFeed + + ' NUM.object_name,' + @LineFeed + + ' NUM.counter_name,' + @LineFeed + + ' NUM.instance_name,' + @LineFeed + + ' NUM.CheckDate,' + @LineFeed + + ' CAST((CAST(NUM.cntr_delta as DECIMAL(19)) / DEN.cntr_delta) as decimal(23,3)) AS cntr_value,' + @LineFeed + + ' NUM.ServerName + CAST(NUM.CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + 'FROM PERF_AVERAGE_FRACTION AS NUM' + @LineFeed + + ' JOIN PERF_LARGE_RAW_BASE AS DEN ON NUM.counter_join = DEN.counter_join' + @LineFeed + + ' AND NUM.CheckDate = DEN.CheckDate' + @LineFeed + + ' AND NUM.ServerName = DEN.ServerName' + @LineFeed + + ' AND NUM.object_name = DEN.object_name' + @LineFeed + + ' AND NUM.instance_name = DEN.instance_name' + @LineFeed + + ' AND DEN.cntr_delta <> 0' + @LineFeed + + 'UNION ALL' + @LineFeed + + '' + @LineFeed + + 'SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' counter_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_value,' + @LineFeed + + ' ServerName + CAST(CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + 'FROM PERF_COUNTER_BULK_COUNT' + @LineFeed + + '' + @LineFeed + + 'UNION ALL' + @LineFeed + + '' + @LineFeed + + 'SELECT ServerName,' + @LineFeed + + ' object_name,' + @LineFeed + + ' counter_name,' + @LineFeed + + ' instance_name,' + @LineFeed + + ' CheckDate,' + @LineFeed + + ' cntr_value,' + @LineFeed + + ' ServerName + CAST(CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + 'FROM PERF_COUNTER_RAWCOUNT;'')'; + + EXEC(@StringToExecute); + END; + + + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableNamePerfmonStats + + ' (ServerName, CheckDate, object_name, counter_name, instance_name, cntr_value, cntr_type, value_delta, value_per_second) SELECT ' + + ' @SrvName, @CheckDate, object_name, counter_name, instance_name, cntr_value, cntr_type, value_delta, value_per_second FROM #PerfmonStats WHERE Pass = 2'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + + /* Delete history older than @OutputTableRetentionDays */ + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') DELETE ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableNamePerfmonStats + + ' WHERE ServerName = @SrvName AND CheckDate < @CheckDate ;'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate date', + @LocalServerName, @OutputTableCleanupDate; + + + + END; + ELSE IF (SUBSTRING(@OutputTableNamePerfmonStats, 2, 2) = '##') + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''tempdb..' + + @OutputTableNamePerfmonStats + + ''') IS NULL) CREATE TABLE ' + + @OutputTableNamePerfmonStats + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + [object_name] NVARCHAR(128) NOT NULL, + [counter_name] NVARCHAR(128) NOT NULL, + [instance_name] NVARCHAR(128) NULL, + [cntr_value] BIGINT NULL, + [cntr_type] INT NOT NULL, + [value_delta] BIGINT NULL, + [value_per_second] DECIMAL(18,2) NULL, + PRIMARY KEY CLUSTERED (ID ASC));' + + ' INSERT ' + + @OutputTableNamePerfmonStats + + ' (ServerName, CheckDate, object_name, counter_name, instance_name, cntr_value, cntr_type, value_delta, value_per_second) SELECT ' + + CAST(SERVERPROPERTY('ServerName') AS NVARCHAR(128)) + + ' @SrvName, @CheckDate, object_name, counter_name, instance_name, cntr_value, cntr_type, value_delta, value_per_second FROM #PerfmonStats WHERE Pass = 2'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + END; + ELSE IF (SUBSTRING(@OutputTableNamePerfmonStats, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); + END; + + + /* @OutputTableNameWaitStats lets us export the results to a permanent table */ + IF @OutputDatabaseName IS NOT NULL + AND @OutputSchemaName IS NOT NULL + AND @OutputTableNameWaitStats IS NOT NULL + AND @OutputTableNameWaitStats NOT LIKE '#%' + AND EXISTS ( SELECT * + FROM sys.databases + WHERE QUOTENAME([name]) = @OutputDatabaseName) + BEGIN + /* Create the table */ + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + + ''') AND NOT EXISTS (SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.TABLES WHERE QUOTENAME(TABLE_SCHEMA) = ''' + + @OutputSchemaName + ''' AND QUOTENAME(TABLE_NAME) = ''' + + @OutputTableNameWaitStats + ''') ' + @LineFeed + + 'BEGIN' + @LineFeed + + 'CREATE TABLE ' + + @OutputSchemaName + '.' + + @OutputTableNameWaitStats + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + wait_type NVARCHAR(60), + wait_time_ms BIGINT, + signal_wait_time_ms BIGINT, + waiting_tasks_count BIGINT , + PRIMARY KEY CLUSTERED (ID));' + @LineFeed + + 'CREATE NONCLUSTERED INDEX IX_ServerName_wait_type_CheckDate_Includes ON ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats + @LineFeed + + '(ServerName, wait_type, CheckDate) INCLUDE (wait_time_ms, signal_wait_time_ms, waiting_tasks_count);' + @LineFeed + + 'END'; + + EXEC(@StringToExecute); + + /* Create the wait stats category table */ + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableNameWaitStats_Categories; + IF OBJECT_ID(@ObjectFullName) IS NULL + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; EXEC (''CREATE TABLE ' + + @OutputSchemaName + '.' + + @OutputTableNameWaitStats_Categories + ' (WaitType NVARCHAR(60) PRIMARY KEY CLUSTERED, WaitCategory NVARCHAR(128) NOT NULL, Ignorable BIT DEFAULT 0);'')'; + + EXEC(@StringToExecute); + END; + + /* Make sure the wait stats category table has the current number of rows */ + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; EXEC (''IF (SELECT COALESCE(SUM(1),0) FROM ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats_Categories + ') <> (SELECT COALESCE(SUM(1),0) FROM ##WaitCategories)' + @LineFeed + + 'BEGIN ' + @LineFeed + + 'TRUNCATE TABLE ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats_Categories + @LineFeed + + 'INSERT INTO ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats_Categories + ' (WaitType, WaitCategory, Ignorable) SELECT WaitType, WaitCategory, Ignorable FROM ##WaitCategories;' + @LineFeed + + 'END'')'; + + EXEC(@StringToExecute); + + + SET @ObjectFullName = @OutputDatabaseName + N'.' + @OutputSchemaName + N'.' + @OutputTableNameWaitStats_View; + + /* If the view exists without the most recently added columns, drop it. See Github #2162. */ + IF OBJECT_ID(@ObjectFullName) IS NOT NULL + BEGIN + SET @StringToExecute = N'USE ' + @OutputDatabaseName + N'; IF NOT EXISTS (SELECT * FROM ' + @OutputDatabaseName + N'.sys.all_columns + WHERE object_id = (OBJECT_ID(''' + @ObjectFullName + N''')) AND name = ''JoinKey'') + DROP VIEW ' + @OutputSchemaName + N'.' + @OutputTableNameWaitStats_View + N';'; + + EXEC(@StringToExecute); + END + + + /* Create the wait stats view */ + IF OBJECT_ID(@ObjectFullName) IS NULL + BEGIN + SET @StringToExecute = 'USE ' + + @OutputDatabaseName + + '; EXEC (''CREATE VIEW ' + + @OutputSchemaName + '.' + + @OutputTableNameWaitStats_View + ' AS ' + @LineFeed + + 'WITH RowDates as' + @LineFeed + + '(' + @LineFeed + + ' SELECT ' + @LineFeed + + ' ROW_NUMBER() OVER (ORDER BY [ServerName], [CheckDate]) ID,' + @LineFeed + + ' [CheckDate]' + @LineFeed + + ' FROM ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats + @LineFeed + + ' GROUP BY [ServerName], [CheckDate]' + @LineFeed + + '),' + @LineFeed + + 'CheckDates as' + @LineFeed + + '(' + @LineFeed + + ' SELECT ThisDate.CheckDate,' + @LineFeed + + ' LastDate.CheckDate as PreviousCheckDate' + @LineFeed + + ' FROM RowDates ThisDate' + @LineFeed + + ' JOIN RowDates LastDate' + @LineFeed + + ' ON ThisDate.ID = LastDate.ID + 1' + @LineFeed + + ')' + @LineFeed + + 'SELECT w.ServerName, w.CheckDate, w.wait_type, COALESCE(wc.WaitCategory, ''''Other'''') AS WaitCategory, COALESCE(wc.Ignorable,0) AS Ignorable' + @LineFeed + + ', DATEDIFF(ss, wPrior.CheckDate, w.CheckDate) AS ElapsedSeconds' + @LineFeed + + ', (w.wait_time_ms - wPrior.wait_time_ms) AS wait_time_ms_delta' + @LineFeed + + ', (w.wait_time_ms - wPrior.wait_time_ms) / 60000.0 AS wait_time_minutes_delta' + @LineFeed + + ', (w.wait_time_ms - wPrior.wait_time_ms) / 1000.0 / DATEDIFF(ss, wPrior.CheckDate, w.CheckDate) AS wait_time_minutes_per_minute' + @LineFeed + + ', (w.signal_wait_time_ms - wPrior.signal_wait_time_ms) AS signal_wait_time_ms_delta' + @LineFeed + + ', (w.waiting_tasks_count - wPrior.waiting_tasks_count) AS waiting_tasks_count_delta' + @LineFeed + + ', w.ServerName + CAST(w.CheckDate AS NVARCHAR(50)) AS JoinKey' + @LineFeed + + 'FROM ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats + ' w' + @LineFeed + + 'INNER HASH JOIN CheckDates Dates' + @LineFeed + + 'ON Dates.CheckDate = w.CheckDate' + @LineFeed + + 'INNER JOIN ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats + ' wPrior ON w.ServerName = wPrior.ServerName AND w.wait_type = wPrior.wait_type AND Dates.PreviousCheckDate = wPrior.CheckDate' + @LineFeed + + 'LEFT OUTER JOIN ' + @OutputSchemaName + '.' + @OutputTableNameWaitStats_Categories + ' wc ON w.wait_type = wc.WaitType' + @LineFeed + + 'WHERE DATEDIFF(MI, wPrior.CheckDate, w.CheckDate) BETWEEN 1 AND 60' + @LineFeed + + 'AND [w].[wait_time_ms] >= [wPrior].[wait_time_ms];'')' + + EXEC(@StringToExecute); + END; + + + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') INSERT ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableNameWaitStats + + ' (ServerName, CheckDate, wait_type, wait_time_ms, signal_wait_time_ms, waiting_tasks_count) SELECT ' + + ' @SrvName, @CheckDate, wait_type, wait_time_ms, signal_wait_time_ms, waiting_tasks_count FROM #WaitStats WHERE Pass = 2 AND wait_time_ms > 0 AND waiting_tasks_count > 0'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + + /* Delete history older than @OutputTableRetentionDays */ + SET @StringToExecute = N' IF EXISTS(SELECT * FROM ' + + @OutputDatabaseName + + '.INFORMATION_SCHEMA.SCHEMATA WHERE QUOTENAME(SCHEMA_NAME) = ''' + + @OutputSchemaName + ''') DELETE ' + + @OutputDatabaseName + '.' + + @OutputSchemaName + '.' + + @OutputTableNameWaitStats + + ' WHERE ServerName = @SrvName AND CheckDate < @CheckDate ;'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate date', + @LocalServerName, @OutputTableCleanupDate; + + END; + ELSE IF (SUBSTRING(@OutputTableNameWaitStats, 2, 2) = '##') + BEGIN + SET @StringToExecute = N' IF (OBJECT_ID(''tempdb..' + + @OutputTableNameWaitStats + + ''') IS NULL) CREATE TABLE ' + + @OutputTableNameWaitStats + + ' (ID INT IDENTITY(1,1) NOT NULL, + ServerName NVARCHAR(128), + CheckDate DATETIMEOFFSET, + wait_type NVARCHAR(60), + wait_time_ms BIGINT, + signal_wait_time_ms BIGINT, + waiting_tasks_count BIGINT , + PRIMARY KEY CLUSTERED (ID ASC));' + + ' INSERT ' + + @OutputTableNameWaitStats + + ' (ServerName, CheckDate, wait_type, wait_time_ms, signal_wait_time_ms, waiting_tasks_count) SELECT ' + + ' @SrvName, @CheckDate, wait_type, wait_time_ms, signal_wait_time_ms, waiting_tasks_count FROM #WaitStats WHERE Pass = 2 AND wait_time_ms > 0 AND waiting_tasks_count > 0'; + + EXEC sp_executesql @StringToExecute, + N'@SrvName NVARCHAR(128), @CheckDate datetimeoffset', + @LocalServerName, @StartSampleTime; + END; + ELSE IF (SUBSTRING(@OutputTableNameWaitStats, 2, 1) = '#') + BEGIN + RAISERROR('Due to the nature of Dymamic SQL, only global (i.e. double pound (##)) temp tables are supported for @OutputTableName', 16, 0); + END; + + + + + DECLARE @separator AS VARCHAR(1); + IF @OutputType = 'RSV' + SET @separator = CHAR(31); + ELSE + SET @separator = ','; + + IF @OutputType = 'COUNT' AND @SinceStartup = 0 + BEGIN + SELECT COUNT(*) AS Warnings + FROM #BlitzFirstResults; + END; + ELSE + IF @OutputType = 'Opserver1' AND @SinceStartup = 0 AND @OutputResultSets LIKE N'%Findings%' + BEGIN + + SELECT r.[Priority] , + r.[FindingsGroup] , + r.[Finding] , + r.[URL] , + r.[Details], + r.[HowToStopIt] , + r.[CheckID] , + r.[StartTime], + r.[LoginName], + r.[NTUserName], + r.[OriginalLoginName], + r.[ProgramName], + r.[HostName], + r.[DatabaseID], + r.[DatabaseName], + r.[OpenTransactionCount], + r.[QueryPlan], + r.[QueryText], + qsNow.plan_handle AS PlanHandle, + qsNow.sql_handle AS SqlHandle, + qsNow.statement_start_offset AS StatementStartOffset, + qsNow.statement_end_offset AS StatementEndOffset, + [Executions] = qsNow.execution_count - (COALESCE(qsFirst.execution_count, 0)), + [ExecutionsPercent] = CAST(100.0 * (qsNow.execution_count - (COALESCE(qsFirst.execution_count, 0))) / (qsTotal.execution_count - qsTotalFirst.execution_count) AS DECIMAL(6,2)), + [Duration] = qsNow.total_elapsed_time - (COALESCE(qsFirst.total_elapsed_time, 0)), + [DurationPercent] = CAST(100.0 * (qsNow.total_elapsed_time - (COALESCE(qsFirst.total_elapsed_time, 0))) / (qsTotal.total_elapsed_time - qsTotalFirst.total_elapsed_time) AS DECIMAL(6,2)), + [CPU] = qsNow.total_worker_time - (COALESCE(qsFirst.total_worker_time, 0)), + [CPUPercent] = CAST(100.0 * (qsNow.total_worker_time - (COALESCE(qsFirst.total_worker_time, 0))) / (qsTotal.total_worker_time - qsTotalFirst.total_worker_time) AS DECIMAL(6,2)), + [Reads] = qsNow.total_logical_reads - (COALESCE(qsFirst.total_logical_reads, 0)), + [ReadsPercent] = CAST(100.0 * (qsNow.total_logical_reads - (COALESCE(qsFirst.total_logical_reads, 0))) / (qsTotal.total_logical_reads - qsTotalFirst.total_logical_reads) AS DECIMAL(6,2)), + [PlanCreationTime] = CONVERT(NVARCHAR(100), qsNow.creation_time ,121), + [TotalExecutions] = qsNow.execution_count, + [TotalExecutionsPercent] = CAST(100.0 * qsNow.execution_count / qsTotal.execution_count AS DECIMAL(6,2)), + [TotalDuration] = qsNow.total_elapsed_time, + [TotalDurationPercent] = CAST(100.0 * qsNow.total_elapsed_time / qsTotal.total_elapsed_time AS DECIMAL(6,2)), + [TotalCPU] = qsNow.total_worker_time, + [TotalCPUPercent] = CAST(100.0 * qsNow.total_worker_time / qsTotal.total_worker_time AS DECIMAL(6,2)), + [TotalReads] = qsNow.total_logical_reads, + [TotalReadsPercent] = CAST(100.0 * qsNow.total_logical_reads / qsTotal.total_logical_reads AS DECIMAL(6,2)), + r.[DetailsInt] + FROM #BlitzFirstResults r + LEFT OUTER JOIN #QueryStats qsTotal ON qsTotal.Pass = 0 + LEFT OUTER JOIN #QueryStats qsTotalFirst ON qsTotalFirst.Pass = -1 + LEFT OUTER JOIN #QueryStats qsNow ON r.QueryStatsNowID = qsNow.ID + LEFT OUTER JOIN #QueryStats qsFirst ON r.QueryStatsFirstID = qsFirst.ID + ORDER BY r.Priority , + r.FindingsGroup , + CASE + WHEN r.CheckID = 6 THEN DetailsInt + ELSE 0 + END DESC, + r.Finding, + r.ID; + END; + ELSE IF @OutputType IN ( 'CSV', 'RSV' ) AND @SinceStartup = 0 AND @OutputResultSets LIKE N'%Findings%' + BEGIN + + SELECT Result = CAST([Priority] AS NVARCHAR(100)) + + @separator + CAST(CheckID AS NVARCHAR(100)) + + @separator + COALESCE([FindingsGroup], + '(N/A)') + @separator + + COALESCE([Finding], '(N/A)') + @separator + + COALESCE(DatabaseName, '(N/A)') + @separator + + COALESCE([URL], '(N/A)') + @separator + + COALESCE([Details], '(N/A)') + FROM #BlitzFirstResults + ORDER BY Priority , + FindingsGroup , + CASE + WHEN CheckID = 6 THEN DetailsInt + ELSE 0 + END DESC, + Finding, + Details; + END; + ELSE IF @OutputType = 'Top10' AND @OutputResultSets LIKE N'%WaitStats%' + BEGIN + /* Measure waits in hours */ + ;WITH max_batch AS ( + SELECT MAX(SampleTime) AS SampleTime + FROM #WaitStats + ) + SELECT TOP 10 + CAST(DATEDIFF(mi,wd1.SampleTime, wd2.SampleTime) / 60.0 AS DECIMAL(18,1)) AS [Hours Sample], + CAST(c.[Total Thread Time (Seconds)] / 60. / 60. AS DECIMAL(18,1)) AS [Thread Time (Hours)], + wd1.wait_type, + COALESCE(wcat.WaitCategory, 'Other') AS wait_category, + CAST(c.[Wait Time (Seconds)] / 60. / 60. AS DECIMAL(18,1)) AS [Wait Time (Hours)], + CAST((wd2.wait_time_ms - wd1.wait_time_ms) / 1000.0 / cores.cpu_count / DATEDIFF(ss, wd1.SampleTime, wd2.SampleTime) AS DECIMAL(18,1)) AS [Per Core Per Hour], + (wd2.waiting_tasks_count - wd1.waiting_tasks_count) AS [Number of Waits], + CASE WHEN (wd2.waiting_tasks_count - wd1.waiting_tasks_count) > 0 + THEN + CAST((wd2.wait_time_ms-wd1.wait_time_ms)/ + (1.0*(wd2.waiting_tasks_count - wd1.waiting_tasks_count)) AS NUMERIC(12,1)) + ELSE 0 END AS [Avg ms Per Wait] + FROM max_batch b + JOIN #WaitStats wd2 ON + wd2.SampleTime =b.SampleTime + JOIN #WaitStats wd1 ON + wd1.wait_type=wd2.wait_type AND + wd2.SampleTime > wd1.SampleTime + CROSS APPLY (SELECT SUM(1) AS cpu_count FROM sys.dm_os_schedulers WHERE status = 'VISIBLE ONLINE' AND is_online = 1) AS cores + CROSS APPLY (SELECT + CAST((wd2.wait_time_ms-wd1.wait_time_ms)/1000. AS DECIMAL(18,1)) AS [Wait Time (Seconds)], + CAST((wd2.signal_wait_time_ms - wd1.signal_wait_time_ms)/1000. AS DECIMAL(18,1)) AS [Signal Wait Time (Seconds)], + CAST((wd2.thread_time_ms)/1000. AS DECIMAL(18,1)) AS [Total Thread Time (Seconds)] + ) AS c + LEFT OUTER JOIN ##WaitCategories wcat ON wd1.wait_type = wcat.WaitType + WHERE (wd2.waiting_tasks_count - wd1.waiting_tasks_count) > 0 + AND wd2.wait_time_ms-wd1.wait_time_ms > 0 + ORDER BY [Wait Time (Seconds)] DESC; + END; + ELSE IF @ExpertMode = 0 AND @OutputType <> 'NONE' AND @OutputXMLasNVARCHAR = 0 AND @SinceStartup = 0 AND @OutputResultSets LIKE N'%Findings%' + BEGIN + SELECT [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + CAST(@StockDetailsHeader + [Details] + @StockDetailsFooter AS XML) AS Details, + CAST(@StockWarningHeader + HowToStopIt + @StockWarningFooter AS XML) AS HowToStopIt, + [QueryText], + [QueryPlan] + FROM #BlitzFirstResults + WHERE (@Seconds > 0 OR (Priority IN (0, 250, 251, 255))) /* For @Seconds = 0, filter out broken checks for now */ + ORDER BY Priority , + FindingsGroup , + CASE + WHEN CheckID = 6 THEN DetailsInt + ELSE 0 + END DESC, + Finding, + ID, + CAST(Details AS NVARCHAR(4000)); + END; + ELSE IF @OutputType <> 'NONE' AND @OutputXMLasNVARCHAR = 1 AND @SinceStartup = 0 AND @OutputResultSets LIKE N'%Findings%' + BEGIN + SELECT [Priority] , + [FindingsGroup] , + [Finding] , + [URL] , + CAST(LEFT(@StockDetailsHeader + [Details] + @StockDetailsFooter,32000) AS TEXT) AS Details, + CAST(LEFT([HowToStopIt],32000) AS TEXT) AS HowToStopIt, + CAST([QueryText] AS NVARCHAR(MAX)) AS QueryText, + CAST([QueryPlan] AS NVARCHAR(MAX)) AS QueryPlan + FROM #BlitzFirstResults + WHERE (@Seconds > 0 OR (Priority IN (0, 250, 251, 255))) /* For @Seconds = 0, filter out broken checks for now */ + ORDER BY Priority , + FindingsGroup , + CASE + WHEN CheckID = 6 THEN DetailsInt + ELSE 0 + END DESC, + Finding, + ID, + CAST(Details AS NVARCHAR(4000)); + END; + ELSE IF @ExpertMode = 1 AND @OutputType <> 'NONE' AND @OutputResultSets LIKE N'%Findings%' + BEGIN + IF @SinceStartup = 0 + SELECT r.[Priority] , + r.[FindingsGroup] , + r.[Finding] , + r.[URL] , + CAST(@StockDetailsHeader + r.[Details] + @StockDetailsFooter AS XML) AS Details, + CAST(@StockWarningHeader + r.HowToStopIt + @StockWarningFooter AS XML) AS HowToStopIt, + r.[CheckID] , + r.[StartTime], + r.[LoginName], + r.[NTUserName], + r.[OriginalLoginName], + r.[ProgramName], + r.[HostName], + r.[DatabaseID], + r.[DatabaseName], + r.[OpenTransactionCount], + r.[QueryPlan], + r.[QueryText], + qsNow.plan_handle AS PlanHandle, + qsNow.sql_handle AS SqlHandle, + qsNow.statement_start_offset AS StatementStartOffset, + qsNow.statement_end_offset AS StatementEndOffset, + [Executions] = qsNow.execution_count - (COALESCE(qsFirst.execution_count, 0)), + [ExecutionsPercent] = CAST(100.0 * (qsNow.execution_count - (COALESCE(qsFirst.execution_count, 0))) / (qsTotal.execution_count - qsTotalFirst.execution_count) AS DECIMAL(6,2)), + [Duration] = qsNow.total_elapsed_time - (COALESCE(qsFirst.total_elapsed_time, 0)), + [DurationPercent] = CAST(100.0 * (qsNow.total_elapsed_time - (COALESCE(qsFirst.total_elapsed_time, 0))) / (qsTotal.total_elapsed_time - qsTotalFirst.total_elapsed_time) AS DECIMAL(6,2)), + [CPU] = qsNow.total_worker_time - (COALESCE(qsFirst.total_worker_time, 0)), + [CPUPercent] = CAST(100.0 * (qsNow.total_worker_time - (COALESCE(qsFirst.total_worker_time, 0))) / (qsTotal.total_worker_time - qsTotalFirst.total_worker_time) AS DECIMAL(6,2)), + [Reads] = qsNow.total_logical_reads - (COALESCE(qsFirst.total_logical_reads, 0)), + [ReadsPercent] = CAST(100.0 * (qsNow.total_logical_reads - (COALESCE(qsFirst.total_logical_reads, 0))) / (qsTotal.total_logical_reads - qsTotalFirst.total_logical_reads) AS DECIMAL(6,2)), + [PlanCreationTime] = CONVERT(NVARCHAR(100), qsNow.creation_time ,121), + [TotalExecutions] = qsNow.execution_count, + [TotalExecutionsPercent] = CAST(100.0 * qsNow.execution_count / qsTotal.execution_count AS DECIMAL(6,2)), + [TotalDuration] = qsNow.total_elapsed_time, + [TotalDurationPercent] = CAST(100.0 * qsNow.total_elapsed_time / qsTotal.total_elapsed_time AS DECIMAL(6,2)), + [TotalCPU] = qsNow.total_worker_time, + [TotalCPUPercent] = CAST(100.0 * qsNow.total_worker_time / qsTotal.total_worker_time AS DECIMAL(6,2)), + [TotalReads] = qsNow.total_logical_reads, + [TotalReadsPercent] = CAST(100.0 * qsNow.total_logical_reads / qsTotal.total_logical_reads AS DECIMAL(6,2)), + r.[DetailsInt] + FROM #BlitzFirstResults r + LEFT OUTER JOIN #QueryStats qsTotal ON qsTotal.Pass = 0 + LEFT OUTER JOIN #QueryStats qsTotalFirst ON qsTotalFirst.Pass = -1 + LEFT OUTER JOIN #QueryStats qsNow ON r.QueryStatsNowID = qsNow.ID + LEFT OUTER JOIN #QueryStats qsFirst ON r.QueryStatsFirstID = qsFirst.ID + WHERE (@Seconds > 0 OR (Priority IN (0, 250, 251, 255))) /* For @Seconds = 0, filter out broken checks for now */ + ORDER BY r.Priority , + r.FindingsGroup , + CASE + WHEN r.CheckID = 6 THEN DetailsInt + ELSE 0 + END DESC, + r.Finding, + r.ID, + CAST(r.Details AS NVARCHAR(4000)); + + ------------------------- + --What happened: #WaitStats + ------------------------- + IF @Seconds = 0 AND @OutputResultSets LIKE N'%WaitStats%' + BEGIN + /* Measure waits in hours */ + ;WITH max_batch AS ( + SELECT MAX(SampleTime) AS SampleTime + FROM #WaitStats + ) + SELECT + 'WAIT STATS' AS Pattern, + b.SampleTime AS [Sample Ended], + CAST(DATEDIFF(mi,wd1.SampleTime, wd2.SampleTime) / 60. AS DECIMAL(18,1)) AS [Hours Sample], + CAST(c.[Total Thread Time (Seconds)] / 60. / 60. AS DECIMAL(18,1)) AS [Thread Time (Hours)], + wd1.wait_type, + COALESCE(wcat.WaitCategory, 'Other') AS wait_category, + CAST(c.[Wait Time (Seconds)] / 60. / 60. AS DECIMAL(18,1)) AS [Wait Time (Hours)], + CAST((wd2.wait_time_ms - wd1.wait_time_ms) / 1000.0 / cores.cpu_count / DATEDIFF(ss, wd1.SampleTime, wd2.SampleTime) AS DECIMAL(18,1)) AS [Per Core Per Hour], + CAST(c.[Signal Wait Time (Seconds)] / 60.0 / 60 AS DECIMAL(18,1)) AS [Signal Wait Time (Hours)], + CASE WHEN c.[Wait Time (Seconds)] > 0 + THEN CAST(100.*(c.[Signal Wait Time (Seconds)]/c.[Wait Time (Seconds)]) AS NUMERIC(4,1)) + ELSE 0 END AS [Percent Signal Waits], + (wd2.waiting_tasks_count - wd1.waiting_tasks_count) AS [Number of Waits], + CASE WHEN (wd2.waiting_tasks_count - wd1.waiting_tasks_count) > 0 + THEN + CAST((wd2.wait_time_ms-wd1.wait_time_ms)/ + (1.0*(wd2.waiting_tasks_count - wd1.waiting_tasks_count)) AS NUMERIC(12,1)) + ELSE 0 END AS [Avg ms Per Wait], + N'https://www.sqlskills.com/help/waits/' + LOWER(wd1.wait_type) + '/' AS URL + FROM max_batch b + JOIN #WaitStats wd2 ON + wd2.SampleTime =b.SampleTime + JOIN #WaitStats wd1 ON + wd1.wait_type=wd2.wait_type AND + wd2.SampleTime > wd1.SampleTime + CROSS APPLY (SELECT SUM(1) AS cpu_count FROM sys.dm_os_schedulers WHERE status = 'VISIBLE ONLINE' AND is_online = 1) AS cores + CROSS APPLY (SELECT + CAST((wd2.wait_time_ms-wd1.wait_time_ms)/1000. AS DECIMAL(18,1)) AS [Wait Time (Seconds)], + CAST((wd2.signal_wait_time_ms - wd1.signal_wait_time_ms)/1000. AS DECIMAL(18,1)) AS [Signal Wait Time (Seconds)], + CAST((wd2.thread_time_ms)/1000. AS DECIMAL(18,1)) AS [Total Thread Time (Seconds)] + ) AS c + LEFT OUTER JOIN ##WaitCategories wcat ON wd1.wait_type = wcat.WaitType + WHERE (wd2.waiting_tasks_count - wd1.waiting_tasks_count) > 0 + AND wd2.wait_time_ms-wd1.wait_time_ms > 0 + ORDER BY [Wait Time (Seconds)] DESC; + END; + ELSE IF @OutputResultSets LIKE N'%WaitStats%' + BEGIN + /* Measure waits in seconds */ + ;WITH max_batch AS ( + SELECT MAX(SampleTime) AS SampleTime + FROM #WaitStats + ) + SELECT + 'WAIT STATS' AS Pattern, + b.SampleTime AS [Sample Ended], + DATEDIFF(ss,wd1.SampleTime, wd2.SampleTime) AS [Seconds Sample], + c.[Total Thread Time (Seconds)], + wd1.wait_type, + COALESCE(wcat.WaitCategory, 'Other') AS wait_category, + c.[Wait Time (Seconds)], + CAST((CAST(wd2.wait_time_ms - wd1.wait_time_ms AS MONEY)) / 1000.0 / cores.cpu_count / DATEDIFF(ss, wd1.SampleTime, wd2.SampleTime) AS DECIMAL(18,1)) AS [Per Core Per Second], + c.[Signal Wait Time (Seconds)], + CASE WHEN c.[Wait Time (Seconds)] > 0 + THEN CAST(100.*(c.[Signal Wait Time (Seconds)]/c.[Wait Time (Seconds)]) AS NUMERIC(4,1)) + ELSE 0 END AS [Percent Signal Waits], + (wd2.waiting_tasks_count - wd1.waiting_tasks_count) AS [Number of Waits], + CASE WHEN (wd2.waiting_tasks_count - wd1.waiting_tasks_count) > 0 + THEN + CAST((wd2.wait_time_ms-wd1.wait_time_ms)/ + (1.0*(wd2.waiting_tasks_count - wd1.waiting_tasks_count)) AS NUMERIC(12,1)) + ELSE 0 END AS [Avg ms Per Wait], + N'https://www.sqlskills.com/help/waits/' + LOWER(wd1.wait_type) + '/' AS URL + FROM max_batch b + JOIN #WaitStats wd2 ON + wd2.SampleTime =b.SampleTime + JOIN #WaitStats wd1 ON + wd1.wait_type=wd2.wait_type AND + wd2.SampleTime > wd1.SampleTime + CROSS APPLY (SELECT SUM(1) AS cpu_count FROM sys.dm_os_schedulers WHERE status = 'VISIBLE ONLINE' AND is_online = 1) AS cores + CROSS APPLY (SELECT + CAST((wd2.wait_time_ms-wd1.wait_time_ms)/1000. AS DECIMAL(18,1)) AS [Wait Time (Seconds)], + CAST((wd2.signal_wait_time_ms - wd1.signal_wait_time_ms)/1000. AS DECIMAL(18,1)) AS [Signal Wait Time (Seconds)], + CAST((wd2.thread_time_ms - wd1.thread_time_ms)/1000. AS DECIMAL(18,1)) AS [Total Thread Time (Seconds)] + ) AS c + LEFT OUTER JOIN ##WaitCategories wcat ON wd1.wait_type = wcat.WaitType + WHERE (wd2.waiting_tasks_count - wd1.waiting_tasks_count) > 0 + AND wd2.wait_time_ms-wd1.wait_time_ms > 0 + ORDER BY [Wait Time (Seconds)] DESC; + END; + + ------------------------- + --What happened: #FileStats + ------------------------- + IF @OutputResultSets LIKE N'%FileStats%' + WITH readstats AS ( + SELECT 'PHYSICAL READS' AS Pattern, + ROW_NUMBER() OVER (ORDER BY wd2.avg_stall_read_ms DESC) AS StallRank, + wd2.SampleTime AS [Sample Time], + DATEDIFF(ss,wd1.SampleTime, wd2.SampleTime) AS [Sample (seconds)], + wd1.DatabaseName , + wd1.FileLogicalName AS [File Name], + UPPER(SUBSTRING(wd1.PhysicalName, 1, 2)) AS [Drive] , + wd1.SizeOnDiskMB , + ( wd2.num_of_reads - wd1.num_of_reads ) AS [# Reads/Writes], + CASE WHEN wd2.num_of_reads - wd1.num_of_reads > 0 + THEN CAST(( wd2.bytes_read - wd1.bytes_read)/1024./1024. AS NUMERIC(21,1)) + ELSE 0 + END AS [MB Read/Written], + wd2.avg_stall_read_ms AS [Avg Stall (ms)], + wd1.PhysicalName AS [file physical name] + FROM #FileStats wd2 + JOIN #FileStats wd1 ON wd2.SampleTime > wd1.SampleTime + AND wd1.DatabaseID = wd2.DatabaseID + AND wd1.FileID = wd2.FileID + ), + writestats AS ( + SELECT + 'PHYSICAL WRITES' AS Pattern, + ROW_NUMBER() OVER (ORDER BY wd2.avg_stall_write_ms DESC) AS StallRank, + wd2.SampleTime AS [Sample Time], + DATEDIFF(ss,wd1.SampleTime, wd2.SampleTime) AS [Sample (seconds)], + wd1.DatabaseName , + wd1.FileLogicalName AS [File Name], + UPPER(SUBSTRING(wd1.PhysicalName, 1, 2)) AS [Drive] , + wd1.SizeOnDiskMB , + ( wd2.num_of_writes - wd1.num_of_writes ) AS [# Reads/Writes], + CASE WHEN wd2.num_of_writes - wd1.num_of_writes > 0 + THEN CAST(( wd2.bytes_written - wd1.bytes_written)/1024./1024. AS NUMERIC(21,1)) + ELSE 0 + END AS [MB Read/Written], + wd2.avg_stall_write_ms AS [Avg Stall (ms)], + wd1.PhysicalName AS [file physical name] + FROM #FileStats wd2 + JOIN #FileStats wd1 ON wd2.SampleTime > wd1.SampleTime + AND wd1.DatabaseID = wd2.DatabaseID + AND wd1.FileID = wd2.FileID + ) + SELECT + Pattern, [Sample Time], [Sample (seconds)], [File Name], [Drive], [# Reads/Writes],[MB Read/Written],[Avg Stall (ms)], [file physical name], [DatabaseName], [StallRank] + FROM readstats + WHERE StallRank <=20 AND [MB Read/Written] > 0 + UNION ALL + SELECT Pattern, [Sample Time], [Sample (seconds)], [File Name], [Drive], [# Reads/Writes],[MB Read/Written],[Avg Stall (ms)], [file physical name], [DatabaseName], [StallRank] + FROM writestats + WHERE StallRank <=20 AND [MB Read/Written] > 0 + ORDER BY Pattern, StallRank; + + + ------------------------- + --What happened: #PerfmonStats + ------------------------- + + IF @OutputResultSets LIKE N'%PerfmonStats%' + SELECT 'PERFMON' AS Pattern, pLast.[object_name], pLast.counter_name, pLast.instance_name, + pFirst.SampleTime AS FirstSampleTime, pFirst.cntr_value AS FirstSampleValue, + pLast.SampleTime AS LastSampleTime, pLast.cntr_value AS LastSampleValue, + pLast.cntr_value - pFirst.cntr_value AS ValueDelta, + ((1.0 * pLast.cntr_value - pFirst.cntr_value) / DATEDIFF(ss, pFirst.SampleTime, pLast.SampleTime)) AS ValuePerSecond + FROM #PerfmonStats pLast + INNER JOIN #PerfmonStats pFirst ON pFirst.[object_name] = pLast.[object_name] AND pFirst.counter_name = pLast.counter_name AND (pFirst.instance_name = pLast.instance_name OR (pFirst.instance_name IS NULL AND pLast.instance_name IS NULL)) + AND pLast.ID > pFirst.ID + WHERE pLast.cntr_value <> pFirst.cntr_value + ORDER BY Pattern, pLast.[object_name], pLast.counter_name, pLast.instance_name; + + + ------------------------- + --What happened: #QueryStats + ------------------------- + IF @CheckProcedureCache = 1 AND @OutputResultSets LIKE N'%BlitzCache%' + BEGIN + + SELECT qsNow.*, qsFirst.* + FROM #QueryStats qsNow + INNER JOIN #QueryStats qsFirst ON qsNow.[sql_handle] = qsFirst.[sql_handle] AND qsNow.statement_start_offset = qsFirst.statement_start_offset AND qsNow.statement_end_offset = qsFirst.statement_end_offset AND qsNow.plan_generation_num = qsFirst.plan_generation_num AND qsNow.plan_handle = qsFirst.plan_handle AND qsFirst.Pass = 1 + WHERE qsNow.Pass = 2; + END; + ELSE + BEGIN + SELECT 'Plan Cache' AS [Pattern], 'Plan cache not analyzed' AS [Finding], 'Use @CheckProcedureCache = 1 or run sp_BlitzCache for more analysis' AS [More Info], CONVERT(XML, @StockDetailsHeader + 'firstresponderkit.org' + @StockDetailsFooter) AS [Details]; + END; + END; + + DROP TABLE #BlitzFirstResults; + + /* What's running right now? This is the first and last result set. */ + IF @SinceStartup = 0 AND @Seconds > 0 AND @ExpertMode = 1 AND @OutputType <> 'NONE' AND @OutputResultSets LIKE N'%BlitzWho_End%' + BEGIN + IF OBJECT_ID('master.dbo.sp_BlitzWho') IS NULL AND OBJECT_ID('dbo.sp_BlitzWho') IS NULL + BEGIN + PRINT N'sp_BlitzWho is not installed in the current database_files. You can get a copy from http://FirstResponderKit.org'; + END; + ELSE + BEGIN + EXEC (@BlitzWho); + END; + END; /* IF @SinceStartup = 0 AND @Seconds > 0 AND @ExpertMode = 1 AND @OutputType <> 'NONE' - What's running right now? This is the first and last result set. */ + +END; /* IF @LogMessage IS NULL */ +END; /* ELSE IF @OutputType = 'SCHEMA' */ + +SET NOCOUNT OFF; +GO + + + +/* How to run it: +EXEC dbo.sp_BlitzFirst + +With extra diagnostic info: +EXEC dbo.sp_BlitzFirst @ExpertMode = 1; + +Saving output to tables: +EXEC sp_BlitzFirst + @OutputDatabaseName = 'DBAtools' +, @OutputSchemaName = 'dbo' +, @OutputTableName = 'BlitzFirst' +, @OutputTableNameFileStats = 'BlitzFirst_FileStats' +, @OutputTableNamePerfmonStats = 'BlitzFirst_PerfmonStats' +, @OutputTableNameWaitStats = 'BlitzFirst_WaitStats' +, @OutputTableNameBlitzCache = 'BlitzCache' +, @OutputTableNameBlitzWho = 'BlitzWho' +, @OutputType = 'none' +*/ \ No newline at end of file diff --git a/DDLs/QRY-SELECT-[Find-Missing-Tables-For-Purging].sql b/DDLs/QRY-SELECT-[Find-Missing-Tables-For-Purging].sql new file mode 100644 index 0000000..74bb989 --- /dev/null +++ b/DDLs/QRY-SELECT-[Find-Missing-Tables-For-Purging].sql @@ -0,0 +1,81 @@ +-- !------------------------------------------------------------------------------------------------! +-- !~~~~ IMPORTANT - USE BELOW CODE TO FIND TABLES NOT PRESENT IN dbo.purge table for Purging ~~~~~~! +-- !------------------------------------------------------------------------------------------------! +declare @_table_name varchar(500); +declare @_sql nvarchar(max); + +if OBJECT_ID('tempdb..#tables') is not null drop table #tables; +create table #tables (table_name varchar(500) not null, [rows] bigint not null); +if OBJECT_ID('tempdb..#tables_to_skip') is not null drop table #tables_to_skip; +create table #tables_to_skip (table_name varchar(500) not null); + +insert #tables_to_skip +select table_name +from ( values ('dbo.BlitzFirst_WaitStats_Categories'), + ('dbo.sql_agent_job_stats'), + ('dbo.sql_agent_jobs_all_servers'), + ('dbo.sql_agent_job_thresholds'), + ('dbo.alert_categories'), + ('dbo.purge_table'), + ('dbo.log_space_consumers_all_servers'), + ('dbo.tempdb_space_usage_all_servers'), + ('dbo.backups_all_servers'), + ('dbo.disk_space_all_servers'), + ('dbo.instance_details'), + ('dbo.instance_hosts'), + ('dbo.credential_manager'), + ('dbo.credential_manager_history'), + ('dbo.services_all_servers'), + ('dbo.all_server_volatile_info'), + ('dbo.sent_alert_history_all_servers'), + ('dbo.login_email_mapping'), + ('dbo.all_server_login_expiry_info_dashboard'), + ('dbo.all_server_collection_latency_info'), + ('dbo.server_login_expiry_collection_computed'), + ('dbo.ag_health_state_all_servers'), + ('dbo.alert_history_all_servers_last_actioned'), + ('dbo.all_server_stable_info') + ) tables_2_skip (table_name); + +declare cur_tables cursor local forward_only for + with cte_user_tables as ( + select table_name = s.name+'.'+t.name from sys.tables t join sys.schemas s on s.schema_id = t.schema_id + where t.is_ms_shipped = 0 + ) + select ut.table_name + from cte_user_tables ut + left join dbo.purge_table pt + on pt.table_name = ut.table_name + where 1=1 + and not ( ut.table_name like 'dbo.sma_%' + or ut.table_name like '%__staging' + ) + and ut.table_name not in (select s.table_name from #tables_to_skip s) + and pt.date_key is null; + +open cur_tables; +fetch next from cur_tables into @_table_name; + +while @@FETCH_STATUS = 0 +begin + print 'Working on '+quotename(@_table_name)+'..'; + set @_sql = null; + + set @_sql = N' + select table_name = '''+@_table_name+''', rows = count(*) + from '+@_table_name+' t + right join (select [table_name] = '''+@_table_name+''') d + on 1=1; + ' + --print @_sql; + insert #tables (table_name, [rows]) + exec (@_sql); + + fetch next from cur_tables into @_table_name; +end + +close cur_tables; +deallocate cur_tables; + +select * from #tables order by rows desc; +go diff --git a/DDLs/Update-SQLAgentJobsThreshold.sql b/DDLs/QRY-UPDATE-[SQLAgentJobsThreshold].sql similarity index 100% rename from DDLs/Update-SQLAgentJobsThreshold.sql rename to DDLs/QRY-UPDATE-[SQLAgentJobsThreshold].sql diff --git a/DDLs/SCH-Create-All-Objects.sql b/DDLs/SCH-Create-All-Objects.sql index 21edf9f..d7aca4d 100644 --- a/DDLs/SCH-Create-All-Objects.sql +++ b/DDLs/SCH-Create-All-Objects.sql @@ -1,5 +1,6 @@ /* - Version -> 2025-01-30 + Version -> 2026-01-31 + 2026-01-31 - #3 - Infra to Track Server and Database Configuration Changes 2025-01-30 - #24 - Add support for Managed Instances (PAAS) 2024-08-10 - #35 - Add Debugging parameter 2024-04-26 - #38 - Add Infra to Track AG State Change @@ -32,31 +33,29 @@ 7) Create table [dbo].[performance_counters] using Partition scheme 8) Create View [dbo].[vw_performance_counters] for Multi SqlCluster on same nodes Architecture 9) Create dbo.perfmon_files table using Partition scheme - 10) Create table [dbo].[os_task_list] using Partition scheme - 11) Create View [dbo].[vw_os_task_list] for Multi SqlCluster on same nodes Architecture - 12) Create table [dbo].[wait_stats] using Partition scheme - 13) Create table [dbo].[BlitzFirst_WaitStats_Categories] - 14) Create view [dbo].[vw_wait_stats] - 15) Create table [dbo].[file_io_stats] - 16) Create required schemas - 17) Create procedure dbo.usp_extended_results - 18) Create table [dbo].[xevent_metrics] - 19) Create table [dbo].[xevent_metrics_queries] - 20) Create view [dbo].[vw_xevent_metrics] - 21) Create Trigger [tgr_insert_xevent_metrics] - 22) Create table [dbo].[xevent_metrics_Processed_XEL_Files] - 23) Create table [dbo].[disk_space] using Partition scheme - 24) Create View [dbo].[vw_disk_space] for Multi SqlCluster on same nodes Architecture - 25) Create view [dbo].[vw_file_io_stats_deltas] - 26) Create table [dbo].[memory_clerks] - 27) Create table [dbo].[server_privileged_info] - 28) Create table [dbo].[ag_health_state] using Partition scheme - 29) Create table [dbo].[alert_categories] - 30) Create table [dbo].[alert_history] - 31) Add boundaries to partition. 1 boundary per hour - 32) Remove boundaries with retention of 3 months - 33) Populate [dbo].[BlitzFirst_WaitStats_Categories] - 34) Create procedure dbo.usp_print + 10) Create table [dbo].[wait_stats] using Partition scheme + 11) Create table [dbo].[BlitzFirst_WaitStats_Categories] + 12) Create view [dbo].[vw_wait_stats] + 13) Create table [dbo].[file_io_stats] + 14) Create required schemas + 15) Create procedure dbo.usp_extended_results + 16) Create table [dbo].[xevent_metrics] + 17) Create table [dbo].[xevent_metrics_queries] + 18) Create view [dbo].[vw_xevent_metrics] + 19) Create Trigger [tgr_insert_xevent_metrics] + 20) Create table [dbo].[xevent_metrics_Processed_XEL_Files] + 21) Create table [dbo].[disk_space] using Partition scheme + 22) Create View [dbo].[vw_disk_space] for Multi SqlCluster on same nodes Architecture + 23) Create view [dbo].[vw_file_io_stats_deltas] + 24) Create table [dbo].[memory_clerks] + 25) Create table [dbo].[server_privileged_info] + 26) Create table [dbo].[ag_health_state] using Partition scheme + 27) Create table [dbo].[alert_categories] + 28) Create table [dbo].[alert_history] + 29) Add boundaries to partition. 1 boundary per hour + 30) Remove boundaries with retention of 3 months + 31) Populate [dbo].[BlitzFirst_WaitStats_Categories] + 32) Create procedure dbo.usp_print */ @@ -281,6 +280,77 @@ begin end go +if not exists (select 1 from dbo.purge_table where table_name = 'dbo.log_space_consumers') +begin + insert dbo.purge_table + (table_name, date_key, retention_days, purge_row_size, reference) + select table_name = 'dbo.log_space_consumers', + date_key = 'collection_time', + retention_days = 15, + purge_row_size = 100000, + reference = 'SQLMonitor Data Collection' +end +go + +if not exists (select 1 from dbo.purge_table where table_name = 'dbo.tempdb_space_usage') +begin + insert dbo.purge_table + (table_name, date_key, retention_days, purge_row_size, reference) + select table_name = 'dbo.tempdb_space_usage', + date_key = 'collection_time', + retention_days = 45, + purge_row_size = 100000, + reference = 'SQLMonitor Data Collection' +end +go + +if not exists (select 1 from dbo.purge_table where table_name = 'dbo.tempdb_space_consumers') +begin + insert dbo.purge_table + (table_name, date_key, retention_days, purge_row_size, reference) + select table_name = 'dbo.tempdb_space_consumers', + date_key = 'collection_time', + retention_days = 15, + purge_row_size = 100000, + reference = 'SQLMonitor Data Collection' +end +go + +if not exists (select 1 from dbo.purge_table where table_name = 'dbo.WhoIsActive') +begin + insert dbo.purge_table + (table_name, date_key, retention_days, purge_row_size, reference) + select table_name = 'dbo.WhoIsActive', + date_key = 'collection_time', + retention_days = 15, + purge_row_size = 10000, + reference = 'SQLMonitor Data Collection' +end +go + +if not exists (select 1 from dbo.purge_table where table_name = 'dbo.login_permissions') +begin + insert dbo.purge_table + (table_name, date_key, retention_days, purge_row_size, reference) + select table_name = 'dbo.login_permissions', + date_key = 'collection_time', + retention_days = 45, + purge_row_size = 10000, + reference = 'SQLMonitor Data Collection' +end +go + +if not exists (select 1 from dbo.purge_table where table_name = 'dbo.tde_implementation_details') +begin + insert dbo.purge_table + (table_name, date_key, retention_days, purge_row_size, reference) + select table_name = 'dbo.tde_implementation_details', + date_key = 'collection_time', + retention_days = 700, + purge_row_size = 100000, + reference = 'SQLMonitor Data Collection' +end +go /* ***** 5) Create table dbo.instance_hosts ***************************** */ @@ -327,7 +397,7 @@ begin [data_destination_sql_instance] varchar(255) null default convert(varchar,serverproperty('MachineName')), [dba_group_mail_id] varchar(2000) not null default 'dba_team@gmail.com', [sqlmonitor_script_path] varchar(2000) not null default 'C:\SQLMonitor', - [sqlmonitor_version] varchar(20) not null default '1.1.0', + [sqlmonitor_version] varchar(18) not null default '1.1.0', constraint pk_instance_details primary key clustered ([sql_instance], [host_name]), constraint fk_host_name foreign key ([host_name]) references dbo.instance_hosts ([host_name]) @@ -470,82 +540,9 @@ end go -/* ***** 10) Create table [dbo].[os_task_list] using Partition scheme ***************** */ -if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '10) Create table [dbo].[os_task_list] using Partition scheme'; - --- drop table [dbo].[os_task_list] -if OBJECT_ID('[dbo].[os_task_list]') is null -begin - CREATE TABLE [dbo].[os_task_list] - ( - [collection_time_utc] [datetime2](7) NOT NULL, - [host_name] [varchar](255) NOT NULL, - [task_name] [nvarchar](100) not null, - [pid] bigint not null, - [session_name] [varchar](20) null, - [memory_kb] bigint NULL, - [status] [varchar](30) NULL, - [user_name] [varchar](200) NOT NULL, - [cpu_time] [char](14) NOT NULL, - [cpu_time_seconds] bigint NOT NULL, - [window_title] [nvarchar](2000) NULL - ) on ps_dba_datetime2_daily ([collection_time_utc]) -end -go - -if not exists (select * from sys.indexes where [object_id] = OBJECT_ID('[dbo].[os_task_list]') and name = 'ci_os_task_list') -begin - create clustered index ci_os_task_list on [dbo].[os_task_list] ([collection_time_utc], [host_name], [task_name]) on ps_dba_datetime2_daily ([collection_time_utc]) -end -go - -if not exists (select 1 from dbo.purge_table where table_name = 'dbo.os_task_list') -begin - insert dbo.purge_table - (table_name, date_key, retention_days, purge_row_size, reference) - select table_name = 'dbo.os_task_list', - date_key = 'collection_time_utc', - retention_days = 15, - purge_row_size = 100000, - reference = 'SQLMonitor Data Collection' -end -go - - -/* ***** 11) Create View [dbo].[vw_os_task_list] for Multi SqlCluster on same nodes Architecture */ -if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '11) Create View [dbo].[vw_os_task_list] for Multi SqlCluster on same nodes Architecture'; - --- drop view dbo.vw_os_task_list -if OBJECT_ID('dbo.vw_os_task_list') is null - exec ('create view dbo.vw_os_task_list as select 1 as dummy;') -go -declare @recreate_multi_server_views bit = 1; -declare @sql nvarchar(max); -if @recreate_multi_server_views = 1 -begin - set quoted_identifier off; - set @sql = "alter view dbo.vw_os_task_list ---with schemabinding -as -with cte_os_tasks_local as (select [collection_time_utc], [host_name], [task_name], [pid], [session_name], [memory_kb], [status], [user_name], [cpu_time], [cpu_time_seconds], [window_title] from dbo.os_task_list) ---,cte_os_tasks_datasource as (select [collection_time_utc], [host_name], [task_name], [pid], [session_name], [memory_kb], [status], [user_name], [cpu_time], [cpu_time_seconds], [window_title] from [SQL2019].DBA.dbo.os_task_list) - -select [collection_time_utc], [host_name], [task_name], [pid], [session_name], [memory_kb], [status], [user_name], [cpu_time], [cpu_time_seconds], [window_title] from cte_os_tasks_local ---union all ---select [collection_time_utc], [host_name], [task_name], [pid], [session_name], [memory_kb], [status], [user_name], [cpu_time], [cpu_time_seconds], [window_title] from cte_os_tasks_datasource" - set quoted_identifier on; - - exec (@sql); -end -go - - - -/* ***** 12) Create table [dbo].[wait_stats] using Partition scheme ***************** */ +/* ***** 10) Create table [dbo].[wait_stats] using Partition scheme ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '12) Create table [dbo].[wait_stats] using Partition scheme'; + print '10) Create table [dbo].[wait_stats] using Partition scheme'; -- drop table [dbo].[wait_stats] if OBJECT_ID('[dbo].[wait_stats]') is null @@ -583,9 +580,9 @@ go -/* ***** 13) Create table [dbo].[BlitzFirst_WaitStats_Categories] ***************** */ +/* ***** 11) Create table [dbo].[BlitzFirst_WaitStats_Categories] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '13) Create table [dbo].[BlitzFirst_WaitStats_Categories]'; + print '11) Create table [dbo].[BlitzFirst_WaitStats_Categories]'; -- drop table [dbo].[BlitzFirst_WaitStats_Categories] if OBJECT_ID('[dbo].[BlitzFirst_WaitStats_Categories]') is null @@ -593,7 +590,7 @@ begin CREATE TABLE [dbo].[BlitzFirst_WaitStats_Categories] ( [WaitType] [nvarchar](60) NOT NULL, - [WaitCategory] [nvarchar](128) NOT NULL, + [WaitCategory] [nvarchar](126) NOT NULL, [Ignorable] [bit] NULL default 0, -- Default settings of First-Responder-Kit [IgnorableOnPerCoreMetric] [bit] NULL default 0, -- Custom settings for PerCorePerMinutes on Central Dashboard [IgnorableOnDashboard] [bit] NULL default 0, -- Custom settings for Wait Stats Dashboard @@ -620,9 +617,9 @@ end go -/* ***** 14) Create view [dbo].[vw_wait_stats_deltas] ***************** */ +/* ***** 12) Create view [dbo].[vw_wait_stats_deltas] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '14) Create view [dbo].[vw_wait_stats_deltas]'; + print '12) Create view [dbo].[vw_wait_stats_deltas]'; -- DROP VIEW [dbo].[vw_wait_stats_deltas]; if OBJECT_ID('[dbo].[vw_wait_stats_deltas]') is null @@ -666,9 +663,9 @@ WHERE [w].[wait_time_ms] >= [wPrior].[wait_time_ms] GO -/* ***** 15) Create table [dbo].[file_io_stats] using Partition scheme ***************** */ +/* ***** 13) Create table [dbo].[file_io_stats] using Partition scheme ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '15) Create table [dbo].[file_io_stats] using Partition scheme'; + print '13) Create table [dbo].[file_io_stats] using Partition scheme'; -- drop table [dbo].[file_io_stats] if OBJECT_ID('[dbo].[file_io_stats]') is null @@ -720,9 +717,9 @@ end go -/* ***** 16) Create required schemas ***************** */ +/* ***** 14) Create required schemas ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '16) Create required schemas'; + print '14) Create required schemas'; if not exists (select * from sys.schemas where name = 'bkp') exec ('CREATE SCHEMA [bkp]') @@ -738,9 +735,9 @@ if not exists (select * from sys.schemas where name = 'tst') GO -/* ***** 17) Create procedure dbo.usp_extended_results ***************** */ +/* ***** 15) Create procedure dbo.usp_extended_results ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '17) Create procedure dbo.usp_extended_results'; + print '15) Create procedure dbo.usp_extended_results'; -- drop procedure usp_extended_results if OBJECT_ID('dbo.usp_extended_results') is null @@ -765,9 +762,9 @@ end go -/* ***** 18) Create table [dbo].[xevent_metrics] ***************** */ +/* ***** 16) Create table [dbo].[xevent_metrics] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '18) Create table [dbo].[xevent_metrics]'; + print '16) Create table [dbo].[xevent_metrics]'; -- DROP TABLE [dbo].[xevent_metrics] IF OBJECT_ID('[dbo].[xevent_metrics]') IS NULL @@ -822,9 +819,9 @@ end go -/* ***** 19) Create table [dbo].[xevent_metrics_queries] ***************** */ +/* ***** 17) Create table [dbo].[xevent_metrics_queries] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '19) Create table [dbo].[xevent_metrics_queries]'; + print '17) Create table [dbo].[xevent_metrics_queries]'; -- DROP TABLE [dbo].[xevent_metrics_queries] IF OBJECT_ID('[dbo].[xevent_metrics_queries]') IS NULL @@ -853,9 +850,9 @@ end go -/* ***** 20) Create view [dbo].[vw_xevent_metrics] ***************** */ +/* ***** 18) Create view [dbo].[vw_xevent_metrics] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '20) Create view [dbo].[vw_xevent_metrics]'; + print '18) Create view [dbo].[vw_xevent_metrics]'; -- DROP VIEW [dbo].[vw_xevent_metrics]; if OBJECT_ID('[dbo].[vw_xevent_metrics]') is null @@ -873,9 +870,9 @@ LEFT JOIN [dbo].[xevent_metrics_queries] txt GO -/* ***** 21) Create Trigger [tgr_insert_xevent_metrics] on View [dbo].[vw_xevent_metrics] ***************** */ +/* ***** 19) Create Trigger [tgr_insert_xevent_metrics] on View [dbo].[vw_xevent_metrics] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '21) Create Trigger [tgr_insert_xevent_metrics] on View [dbo].[vw_xevent_metrics]'; + print '19) Create Trigger [tgr_insert_xevent_metrics] on View [dbo].[vw_xevent_metrics]'; if exists (select * from sys.objects where [name] = N'tgr_insert_xevent_metrics' and [type] = 'TR') drop trigger [dbo].tgr_insert_xevent_metrics @@ -903,9 +900,9 @@ go -/* ***** 22) Create table [dbo].[xevent_metrics_Processed_XEL_Files] ***************** */ +/* ***** 20) Create table [dbo].[xevent_metrics_Processed_XEL_Files] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '22) Create table [dbo].[xevent_metrics_Processed_XEL_Files]'; + print '20) Create table [dbo].[xevent_metrics_Processed_XEL_Files]'; -- drop table dbo.xevent_metrics_Processed_XEL_Files if OBJECT_ID('dbo.xevent_metrics_Processed_XEL_Files') is null @@ -934,22 +931,22 @@ end go -/* ***** 23) Create table [dbo].[disk_space] using Partition scheme *********** */ +/* ***** 21) Create table [dbo].[disk_space] using Partition scheme *********** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '23) Create table [dbo].[disk_space] using Partition scheme'; + print '21) Create table [dbo].[disk_space] using Partition scheme'; if OBJECT_ID('[dbo].[disk_space]') is null begin CREATE TABLE [dbo].[disk_space] ( [collection_time_utc] [datetime2](7) NOT NULL, - [host_name] [varchar](125) NOT NULL, + [host_name] [varchar](123) NOT NULL, [disk_volume] [varchar](255) NOT NULL, - [label] [varchar](125) NULL, + [label] [varchar](123) NULL, [capacity_mb] [decimal](20,2) NOT NULL, [free_mb] [decimal](20,2) NOT NULL, [block_size] [int] NULL, - [filesystem] [varchar](125) NULL, + [filesystem] [varchar](123) NULL, constraint pk_disk_space primary key ([collection_time_utc],[host_name],[disk_volume]) on ps_dba_datetime2_daily ([collection_time_utc]) ) on ps_dba_datetime2_daily ([collection_time_utc]); @@ -968,9 +965,9 @@ begin end go -/* ***** 24) Create View [dbo].[vw_disk_space] for Multi SqlCluster on same nodes Architecture */ +/* ***** 22) Create View [dbo].[vw_disk_space] for Multi SqlCluster on same nodes Architecture */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '24) Create View [dbo].[vw_disk_space] for Multi SqlCluster on same nodes Architecture'; + print '22) Create View [dbo].[vw_disk_space] for Multi SqlCluster on same nodes Architecture'; -- drop view dbo.vw_disk_space if OBJECT_ID('dbo.vw_disk_space') is null @@ -997,9 +994,9 @@ end go -/* ***** 25) Create view [dbo].[vw_file_io_stats_deltas] ***************** */ +/* ***** 23) Create view [dbo].[vw_file_io_stats_deltas] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '25) Create view [dbo].[vw_file_io_stats_deltas]'; + print '23) Create view [dbo].[vw_file_io_stats_deltas]'; -- DROP VIEW [dbo].[vw_file_io_stats_deltas]; if OBJECT_ID('[dbo].[vw_file_io_stats_deltas]') is null @@ -1054,9 +1051,9 @@ WHERE [s].[io_stall] >= [sPrior].[io_stall] GO -/* ***** 26) Create table [dbo].[memory_clerks] *************** */ +/* ***** 24) Create table [dbo].[memory_clerks] *************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '26) Create table [dbo].[memory_clerks]'; + print '24) Create table [dbo].[memory_clerks]'; -- drop table [dbo].[memory_clerks] if OBJECT_ID('[dbo].[memory_clerks]') is null @@ -1091,14 +1088,14 @@ end go -/* **** 27) Create table [dbo].[server_privileged_info] ************* */ +/* **** 25) Create table [dbo].[server_privileged_info] ************* */ -- drop table [dbo].[server_privileged_info] if OBJECT_ID('[dbo].[server_privileged_info]') is null begin CREATE TABLE [dbo].[server_privileged_info] ( [collection_time_utc] datetime2 not null default sysutcdatetime(), - [host_name] varchar(125) not null, + [host_name] varchar(123) not null, [host_distribution] varchar(200) null, [processor_name] varchar(200) null, [fqdn] varchar(255) null, @@ -1127,9 +1124,9 @@ end go -/* ***** 28) Create table [dbo].[ag_health_state] using Partition scheme ***************** */ +/* ***** 26) Create table [dbo].[ag_health_state] using Partition scheme ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '28) Create table [dbo].[ag_health_state] using Partition scheme'; + print '26) Create table [dbo].[ag_health_state] using Partition scheme'; -- drop table [dbo].[ag_health_state] if OBJECT_ID('[dbo].[ag_health_state]') is null @@ -1141,7 +1138,7 @@ begin [is_primary_replica] [bit] NULL, [database_name] [sysname] NULL, [ag_name] [sysname] NULL, - [ag_listener] [nvarchar](114) NULL, + [ag_listener] [nvarchar](112) NULL, [is_local] [bit] NULL, [is_distributed] [bit] NULL, [synchronization_state_desc] [nvarchar](60) NULL, @@ -1179,9 +1176,9 @@ end go -/* ***** 29) Create table [dbo].[alert_categories] **************************** */ +/* ***** 27) Create table [dbo].[alert_categories] **************************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '29) Create table [dbo].[alert_categories]'; + print '27) Create table [dbo].[alert_categories]'; -- drop table [dbo].[alert_categories] if OBJECT_ID('[dbo].[alert_categories]') is null @@ -1190,13 +1187,13 @@ begin ( [error_number] [int] NOT NULL, [error_severity] [int] NULL, - [category] [varchar](128) NOT NULL, - [sub_category] [varchar](128) NULL, + [category] [varchar](126) NOT NULL, + [sub_category] [varchar](126) NULL, [alert_name] [varchar](255) NOT NULL, [remarks] [nvarchar](500) NULL, [created_time] [datetime2](7) NOT NULL default sysdatetime(), - [created_by] [nvarchar](128) NOT NULL default suser_name() + [created_by] [nvarchar](126) NOT NULL default suser_name() ); end go @@ -1230,6 +1227,8 @@ begin , ('41142', 0, 'Availability Group', NULL, '(dba) Replica Cannot Become Primary', NULL) , ('41406', 0, 'Availability Group', NULL, '(dba) AG Not Ready for Auto Failover', NULL) , ('41414', 0, 'Availability Group', NULL, '(dba) Secondary Not Connected', NULL) + , ('0', 17, 'Fatal Error - Sev17', NULL, '(dba) Fatal Error - Sev17', NULL) + , ('0', 18, 'Fatal Error - Sev18', NULL, '(dba) Fatal Error - Sev18', NULL) , ('0', 19, 'Fatal Error - Sev19', NULL, '(dba) Fatal Error - Sev19', NULL) , ('0', 20, 'Fatal Error - Sev20', NULL, '(dba) Fatal Error - Sev20', NULL) , ('0', 21, 'Fatal Error - Sev21', NULL, '(dba) Fatal Error - Sev21', NULL) @@ -1237,10 +1236,11 @@ begin , ('0', 23, 'Fatal Error - Sev23', NULL, '(dba) Fatal Error - Sev23', NULL) , ('0', 24, 'Fatal Error - Sev24', NULL, '(dba) Fatal Error - Sev24', NULL) , ('0', 25, 'Fatal Error - Sev25', NULL, '(dba) Fatal Error - Sev25', NULL) - , ('701', 0, 'Insufficient System Memory', NULL, '(dba) Insufficient System memory', NULL) , ('823', 0, 'Corruption', NULL, '(dba) Corruption - Error 823', NULL) , ('824', 0, 'Corruption', NULL, '(dba) Corruption - Error 824', NULL) , ('825', 0, 'Corruption', NULL, '(dba) Corruption - Error 825', NULL) + , ('15457', 0, 'Config Change - Server', NULL, '(dba) Config Change - Server', 'Tracks sp_configure executions') + , ('5084', 0, 'Config Change - Database', NULL, '(dba) Config Change - Database', 'Tracks ALTER DATABASE..SET.. executions') ) en ([error_number], [error_severity], [category], [sub_category], [alert_name], [remarks]) left join dbo.alert_categories ac on exists (select ac.error_number, ac.[error_severity] intersect select en.error_number, en.[error_severity]) @@ -1249,9 +1249,9 @@ end go -/* ***** 30) Create table [dbo].[alert_history] **************************** */ +/* ***** 28) Create table [dbo].[alert_history] **************************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '30) Create table [dbo].[alert_history]'; + print '28) Create table [dbo].[alert_history]'; -- drop table [dbo].[alert_history] if OBJECT_ID('[dbo].[alert_history]') is null @@ -1259,12 +1259,12 @@ begin create table [dbo].[alert_history] ( [collection_time_utc] [datetime2](7) NOT NULL default sysutcdatetime(), - [server_name] [nvarchar](128) NULL, + [server_name] [nvarchar](126) NULL, [database_name] [sysname] NULL, [error_number] [int] NULL, [error_severity] [tinyint] NULL, [error_message] [nvarchar](510) NULL, - [host_instance] [nvarchar](128) NULL, + [host_instance] [nvarchar](126) NULL, [collection_time] [datetime2](7) NOT NULL default sysdatetime() ) on ps_dba_datetime2_daily ([collection_time_utc]); end @@ -1289,9 +1289,9 @@ end go -/* ***** 31) Add boundaries to partition. 1 boundary per hour ***************** */ +/* ***** 29) Add boundaries to partition. 1 boundary per hour ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '31) Add boundaries to partition. 1 boundary per hour'; + print '29) Add boundaries to partition. 1 boundary per hour'; set nocount on; declare @is_partitioned bit = 1; @@ -1332,9 +1332,9 @@ end go -/* ***** 32) Remove boundaries with retention of 3 months ***************** */ +/* ***** 30) Remove boundaries with retention of 3 months ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '32) Remove boundaries with retention of 3 months'; + print '30) Remove boundaries with retention of 3 months'; set nocount on; declare @is_partitioned bit = 1; @@ -1369,9 +1369,9 @@ end go -/* ***** 33) Populate [dbo].[BlitzFirst_WaitStats_Categories] ***************** */ +/* ***** 31) Populate [dbo].[BlitzFirst_WaitStats_Categories] ***************** */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '33) Populate [dbo].[BlitzFirst_WaitStats_Categories]'; + print '31) Populate [dbo].[BlitzFirst_WaitStats_Categories]'; IF OBJECT_ID('[dbo].[BlitzFirst_WaitStats_Categories]') IS NOT NULL BEGIN @@ -1928,30 +1928,30 @@ BEGIN END GO -/* ****** 34) Create procedure dbo.usp_print ******* */ +/* ****** 32) Create procedure dbo.usp_print ******* */ if (PROGRAM_NAME() <> 'Microsoft SQL Server Management Studio - Query') - print '34) Create procedure dbo.usp_print'; + print '32) Create procedure dbo.usp_print'; go IF OBJECT_ID('dbo.usp_print') IS NULL EXEC('CREATE PROCEDURE dbo.usp_print AS select 1 as dummy;'); GO ALTER PROCEDURE [dbo].[usp_print] ( - @string nvarchar(max), - @verbose tinyint = 0 /* 1 => messages, 2 => messages + table results */ + @String nvarchar(max), + @Verbose tinyint = 0 /* 1 => messages, 2 => messages + table results */ ) AS BEGIN /* Purpose: Modifications: 2025-Feb-27 - Initial Draft - exec usp_print @string; + exec usp_print @String; */ SET NOCOUNT ON DECLARE @CurrentEnd BIGINT; /* track the length of the next substring */ DECLARE @offset tinyint; /*tracks the amount of offset needed */ - set @string = replace( replace(@string, char(13) + char(10), char(10)) , char(13), char(10)) + set @String = replace( replace(@String, char(13) + char(10), char(10)) , char(13), char(10)) WHILE LEN(@String) > 1 BEGIN @@ -1966,7 +1966,7 @@ BEGIN set @offset = 1 END PRINT SUBSTRING(@String, 1, @CurrentEnd) - set @string = SUBSTRING(@String, @CurrentEnd+@offset, LEN(@String)) + set @String = SUBSTRING(@String, @CurrentEnd+@offset, LEN(@String)) END /*End While loop*/ END GO diff --git a/DDLs/SCH-Create-Inventory-Specific-Objects.sql b/DDLs/SCH-Create-Inventory-Specific-Objects.sql index 12a1849..c650cf4 100644 --- a/DDLs/SCH-Create-Inventory-Specific-Objects.sql +++ b/DDLs/SCH-Create-Inventory-Specific-Objects.sql @@ -426,7 +426,6 @@ CREATE TABLE [dbo].[all_server_collection_latency_info] [performance_counters__latency_minutes] int null, [xevent_metrics__latency_minutes] int null, [WhoIsActive__latency_minutes] int null, - [os_task_list__latency_minutes] int null, [disk_space__latency_minutes] int null, [file_io_stats__latency_minutes] int null, [sql_agent_job_stats__latency_minutes] int null, @@ -1551,6 +1550,8 @@ begin ('send_sqlmonitor_job_failure_mail','1','When enabled, then job failure mail is send to DBA team'), ('all_server_volatile_info-parallelize','no','When enabled, then volatile info is collected in parallel threads'), ('all_server_volatile_info-parallel-threads',convert(varchar,(select case when cpu_count > 4 then 4 else cpu_count end from sys.dm_os_sys_info as osi)),'parallel threads/jobs for Volatile Info collection'), + ('alert_history_all_servers-parallelize','no','When enabled, then volatile info is collected in parallel threads'), + ('alert_history_all_servers-parallel-threads',convert(varchar,(select case when cpu_count > 4 then 4 else cpu_count end from sys.dm_os_sys_info as osi)),'parallel threads/jobs for Alert History collection'), ('usp_wrapper_GetAllServerInfo-enable-LOCK_TIMEOUT','0','Enable/Disable LOCK_TIMEOUT in procedure dbo.usp_wrapper_GetAllServerInfo'), ('usp_GetAllServerInfo-enable-LOCK_TIMEOUT','0','Enable/Disable LOCK_TIMEOUT in procedure dbo.usp_GetAllServerInfo') ) my_keys (param_key, param_value, remarks) diff --git a/DDLs/SCH-Job-[(dba) Check-SQLAgentJobs].sql b/DDLs/SCH-Job-[(dba) Check-SQLAgentJobs].sql index 1a141c6..328b5b3 100644 --- a/DDLs/SCH-Job-[(dba) Check-SQLAgentJobs].sql +++ b/DDLs/SCH-Job-[(dba) Check-SQLAgentJobs].sql @@ -42,7 +42,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'usp_chec @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Check-SQLAgentJobs" -d DBA -Q "EXEC dbo.usp_check_sql_agent_jobs @default_mail_recipient = ''dba_team@gmail.com'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Check-SQLAgentJobs" -d DBA -Q "EXEC dbo.usp_check_sql_agent_jobs @default_mail_recipient = ''dba_team@gmail.com'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect Login Expiration Info].sql b/DDLs/SCH-Job-[(dba) Collect Login Expiration Info].sql index 0856666..a9f1502 100644 --- a/DDLs/SCH-Job-[(dba) Collect Login Expiration Info].sql +++ b/DDLs/SCH-Job-[(dba) Collect Login Expiration Info].sql @@ -40,7 +40,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect Login Expiration Info" -d DBA -Q "EXEC dbo.usp_collect_all_server_login_expiration_info @execute = 1, @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect Login Expiration Info" -d DBA -Q "EXEC dbo.usp_collect_all_server_login_expiration_info @execute = 1, @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-AgHealthState].sql b/DDLs/SCH-Job-[(dba) Collect-AgHealthState].sql index 3d52894..7c3a804 100644 --- a/DDLs/SCH-Job-[(dba) Collect-AgHealthState].sql +++ b/DDLs/SCH-Job-[(dba) Collect-AgHealthState].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-AgHealthState" -d DBA -Q "EXEC dbo.usp_collect_ag_health_state @recipients = ''dba_team@gmail.com'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-AgHealthState" -d DBA -Q "EXEC dbo.usp_collect_ag_health_state @recipients = ''dba_team@gmail.com'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-DiskSpace-ManagedInstance].sql b/DDLs/SCH-Job-[(dba) Collect-DiskSpace-ManagedInstance].sql index 65e8475..9de1394 100644 --- a/DDLs/SCH-Job-[(dba) Collect-DiskSpace-ManagedInstance].sql +++ b/DDLs/SCH-Job-[(dba) Collect-DiskSpace-ManagedInstance].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-DiskSpace" -d DBA -Q "EXEC dbo.usp_collect_disk_space"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-DiskSpace" -d DBA -Q "EXEC dbo.usp_collect_disk_space"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-FileIOStats].sql b/DDLs/SCH-Job-[(dba) Collect-FileIOStats].sql index 64762e6..8bf1421 100644 --- a/DDLs/SCH-Job-[(dba) Collect-FileIOStats].sql +++ b/DDLs/SCH-Job-[(dba) Collect-FileIOStats].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-FileIOStats" -d DBA -Q "EXEC dbo.usp_collect_file_io_stats @recipients = ''dba_team@gmail.com'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-FileIOStats" -d DBA -Q "EXEC dbo.usp_collect_file_io_stats @recipients = ''dba_team@gmail.com'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-MemoryClerks].sql b/DDLs/SCH-Job-[(dba) Collect-MemoryClerks].sql index 9741080..9ab4cfa 100644 --- a/DDLs/SCH-Job-[(dba) Collect-MemoryClerks].sql +++ b/DDLs/SCH-Job-[(dba) Collect-MemoryClerks].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-MemoryClerks" -d DBA -Q "EXEC dbo.usp_collect_memory_clerks @recipients = ''dba_team@gmail.com'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-MemoryClerks" -d DBA -Q "EXEC dbo.usp_collect_memory_clerks @recipients = ''dba_team@gmail.com'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-PerfmonData-ManagedInstance].sql b/DDLs/SCH-Job-[(dba) Collect-PerfmonData-ManagedInstance].sql index a1cb7d7..620198f 100644 --- a/DDLs/SCH-Job-[(dba) Collect-PerfmonData-ManagedInstance].sql +++ b/DDLs/SCH-Job-[(dba) Collect-PerfmonData-ManagedInstance].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-PerfmonData" -d DBA -Q "EXEC dbo.usp_collect_performance_metrics"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-PerfmonData" -d DBA -Q "EXEC dbo.usp_collect_performance_metrics"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-PrivilegedInfo].sql b/DDLs/SCH-Job-[(dba) Collect-PrivilegedInfo].sql index 57473a3..7b6b3a0 100644 --- a/DDLs/SCH-Job-[(dba) Collect-PrivilegedInfo].sql +++ b/DDLs/SCH-Job-[(dba) Collect-PrivilegedInfo].sql @@ -42,7 +42,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.serv @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-PrivilegedInfo" -d DBA -Q "EXEC dbo.usp_wrapper_CollectPrivilegedInfo @recipients = ''dba_team@gmail.com'', @step_name = ''dbo.server_privileged_info'', @truncate_table = 1, @has_staging_table = 0, @schedule_minutes = 30, @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-PrivilegedInfo" -d DBA -Q "EXEC dbo.usp_wrapper_CollectPrivilegedInfo @recipients = ''dba_team@gmail.com'', @step_name = ''dbo.server_privileged_info'', @truncate_table = 1, @has_staging_table = 0, @schedule_minutes = 30, @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-WaitStats].sql b/DDLs/SCH-Job-[(dba) Collect-WaitStats].sql index 6727b02..3fb4827 100644 --- a/DDLs/SCH-Job-[(dba) Collect-WaitStats].sql +++ b/DDLs/SCH-Job-[(dba) Collect-WaitStats].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-WaitStats" -d DBA -Q "EXEC dbo.usp_collect_wait_stats @recipients = ''dba_team@gmail.com'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-WaitStats" -d DBA -Q "EXEC dbo.usp_collect_wait_stats @recipients = ''dba_team@gmail.com'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Collect-XEvents].sql b/DDLs/SCH-Job-[(dba) Collect-XEvents].sql index 5b024e1..9eae2be 100644 --- a/DDLs/SCH-Job-[(dba) Collect-XEvents].sql +++ b/DDLs/SCH-Job-[(dba) Collect-XEvents].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Collect-XEvents" -d DBA -Q "EXEC dbo.usp_collect_xevent_metrics;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Collect-XEvents" -d DBA -Q "EXEC dbo.usp_collect_xevent_metrics;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Compute-AllServerVolatileInfoHistoryHourly].sql b/DDLs/SCH-Job-[(dba) Compute-AllServerVolatileInfoHistoryHourly].sql index 129c974..260f2fd 100644 --- a/DDLs/SCH-Job-[(dba) Compute-AllServerVolatileInfoHistoryHourly].sql +++ b/DDLs/SCH-Job-[(dba) Compute-AllServerVolatileInfoHistoryHourly].sql @@ -40,7 +40,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'usp_comp @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Compute-AllServerVolatileInfoHistoryHourly" -d DBA -Q "exec dbo.usp_compute_all_server_volatile_info_history_hourly @days_per_processing_batch = 2, @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Compute-AllServerVolatileInfoHistoryHourly" -d DBA -Q "exec dbo.usp_compute_all_server_volatile_info_history_hourly @days_per_processing_batch = 2, @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerAgHealthState].sql b/DDLs/SCH-Job-[(dba) Get-AllServerAgHealthState].sql index 7d71be9..4a23b5f 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerAgHealthState].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerAgHealthState].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.ag_h @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerAgHealthState" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.ag_health_state_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerAgHealthState" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.ag_health_state_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerAlertHistory].sql b/DDLs/SCH-Job-[(dba) Get-AllServerAlertHistory].sql new file mode 100644 index 0000000..93ce463 --- /dev/null +++ b/DDLs/SCH-Job-[(dba) Get-AllServerAlertHistory].sql @@ -0,0 +1,74 @@ +USE [msdb] +GO + +if exists (select * from msdb.dbo.sysjobs_view where name = N'(dba) Get-AllServerAlertHistory') + EXEC msdb.dbo.sp_delete_job @job_name=N'(dba) Get-AllServerAlertHistory', @delete_unused_schedule=1 +GO + +BEGIN TRANSACTION +DECLARE @ReturnCode INT +SELECT @ReturnCode = 0 + +IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'(dba) SQLMonitor' AND category_class=1) +BEGIN +EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'(dba) SQLMonitor' +IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback + +END + +DECLARE @jobId BINARY(16) +EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'(dba) Get-AllServerAlertHistory', + @enabled=1, + @notify_level_eventlog=0, + @notify_level_email=0, + @notify_level_netsend=0, + @notify_level_page=0, + @delete_level=0, + @description=N'This job execute procedure usp_GetAllServerCollectedData and populates in table dbo.alert_history_all_servers + +https://ajaydwivedi.com/github/sqlmonitor', + @category_name=N'(dba) SQLMonitor', + @job_id = @jobId OUTPUT +IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback + +EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.alert_history_all_servers', + @step_id=1, + @cmdexec_success_code=0, + @on_success_action=1, + @on_success_step_id=0, + @on_fail_action=2, + @on_fail_step_id=0, + @retry_attempts=0, + @retry_interval=0, + @os_run_priority=0, @subsystem=N'CmdExec', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerAlertHistory" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.alert_history_all_servers'', @has_staging_table = 0, @truncate_table = 0, @verbose = 0;"', + @flags=40 +IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback +EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 +IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback +EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'(dba) Get-AllServerAlertHistory', + @enabled=1, + @freq_type=4, + @freq_interval=1, + @freq_subday_type=2, + @freq_subday_interval=30, + @freq_relative_interval=0, + @freq_recurrence_factor=0, + @active_start_date=20220715, + @active_end_date=99991231, + @active_start_time=0, + @active_end_time=235959 + --,@schedule_uid=N'2392ff25-eab3-4714-96dd-6ff9bfac3838' +IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback +EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)' +IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback +COMMIT TRANSACTION +GOTO EndSave +QuitWithRollback: + IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION +EndSave: +GO + + +EXEC msdb.dbo.sp_start_job @job_name=N'(dba) Get-AllServerAlertHistory' +go diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerBackups].sql b/DDLs/SCH-Job-[(dba) Get-AllServerBackups].sql index 3286971..1ae95f8 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerBackups].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerBackups].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.back @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerBackups" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.backups_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerBackups" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.backups_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerCollectionLatencyInfo].sql b/DDLs/SCH-Job-[(dba) Get-AllServerCollectionLatencyInfo].sql index 5a1fc9a..ec1732c 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerCollectionLatencyInfo].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerCollectionLatencyInfo].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.all_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerCollectionLatencyInfo" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.all_server_collection_latency_info'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerCollectionLatencyInfo" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.all_server_collection_latency_info'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerDashboardMail].sql b/DDLs/SCH-Job-[(dba) Get-AllServerDashboardMail].sql index 695ac0d..16ab555 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerDashboardMail].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerDashboardMail].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerDashboardMail" -d DBA -Q "EXEC dbo.usp_GetAllServerDashboardMail @recipients = ''dba_team@gmail.com'', @only_threshold_validated = 1, @send_mail = 1, @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerDashboardMail" -d DBA -Q "EXEC dbo.usp_GetAllServerDashboardMail @recipients = ''dba_team@gmail.com'', @only_threshold_validated = 1, @hide_row_if_no_data = 0, @send_mail = 1, @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerDiskSpace].sql b/DDLs/SCH-Job-[(dba) Get-AllServerDiskSpace].sql index 7ae01e2..06ef1a5 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerDiskSpace].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerDiskSpace].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.disk @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerDiskSpace" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.disk_space_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerDiskSpace" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.disk_space_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerLogSpaceConsumers].sql b/DDLs/SCH-Job-[(dba) Get-AllServerLogSpaceConsumers].sql index f711a70..07df007 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerLogSpaceConsumers].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerLogSpaceConsumers].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.log_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerLogSpaceConsumers" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.log_space_consumers_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerLogSpaceConsumers" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.log_space_consumers_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerServices].sql b/DDLs/SCH-Job-[(dba) Get-AllServerServices].sql index fb07df3..fbf1780 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerServices].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerServices].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.serv @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerServices" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.services_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerServices" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.services_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerSqlAgentJobs].sql b/DDLs/SCH-Job-[(dba) Get-AllServerSqlAgentJobs].sql index 960cd05..e1cbfa4 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerSqlAgentJobs].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerSqlAgentJobs].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.sql_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerSqlAgentJobs" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.sql_agent_jobs_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerSqlAgentJobs" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.sql_agent_jobs_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerStableInfo].sql b/DDLs/SCH-Job-[(dba) Get-AllServerStableInfo].sql index 6fec861..da89e82 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerStableInfo].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerStableInfo].sql @@ -44,7 +44,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.all_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerStableInfo - all_server_stable_info" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.all_server_stable_info'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerStableInfo - all_server_stable_info" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.all_server_stable_info'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback /****** Object: Step [dbo.usp_populate__all_server_stable_info_history] Script Date: Sat, 19 Oct 10:51:24 ******/ @@ -58,7 +58,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerStableInfo - usp_populate__all_server_stable_info_history" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.usp_populate__all_server_stable_info_history'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerStableInfo - usp_populate__all_server_stable_info_history" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.usp_populate__all_server_stable_info_history'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerTempdbSpaceUsage].sql b/DDLs/SCH-Job-[(dba) Get-AllServerTempdbSpaceUsage].sql index 498a5ea..3306587 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerTempdbSpaceUsage].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerTempdbSpaceUsage].sql @@ -41,7 +41,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.temp @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerTempdbSpaceUsage" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.tempdb_space_usage_all_servers'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerTempdbSpaceUsage" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerCollectedData @step_name = ''dbo.tempdb_space_usage_all_servers'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Get-AllServerVolatileInfo].sql b/DDLs/SCH-Job-[(dba) Get-AllServerVolatileInfo].sql index 07b8492..17251af 100644 --- a/DDLs/SCH-Job-[(dba) Get-AllServerVolatileInfo].sql +++ b/DDLs/SCH-Job-[(dba) Get-AllServerVolatileInfo].sql @@ -44,7 +44,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.all_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerVolatileInfo - all_server_volatile_info" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.all_server_volatile_info'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerVolatileInfo - all_server_volatile_info" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.all_server_volatile_info'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback /****** Object: Step [dbo.usp_populate__all_server_volatile_info_history] Script Date: Sat, 19 Oct 10:51:24 ******/ @@ -58,7 +58,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Get-AllServerVolatileInfo - usp_populate__all_server_volatile_info_history" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.usp_populate__all_server_volatile_info_history'', @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Get-AllServerVolatileInfo - usp_populate__all_server_volatile_info_history" -d DBA -Q "EXEC dbo.usp_wrapper_GetAllServerInfo @step_name = ''dbo.usp_populate__all_server_volatile_info_history'', @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Partitions-Maintenance].sql b/DDLs/SCH-Job-[(dba) Partitions-Maintenance].sql index d1f8298..aa861c6 100644 --- a/DDLs/SCH-Job-[(dba) Partitions-Maintenance].sql +++ b/DDLs/SCH-Job-[(dba) Partitions-Maintenance].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=2, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Partitions-Maintenance" -d DBA -Q "exec dbo.usp_partition_maintenance;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Partitions-Maintenance" -d DBA -Q "exec dbo.usp_partition_maintenance;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Populate Inventory Tables].sql b/DDLs/SCH-Job-[(dba) Populate Inventory Tables].sql index c52219d..0ba71f8 100644 --- a/DDLs/SCH-Job-[(dba) Populate Inventory Tables].sql +++ b/DDLs/SCH-Job-[(dba) Populate Inventory Tables].sql @@ -57,7 +57,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Populate Inventory Tables" -d "DBA" -Q "exec dbo.usp_wrapper_populate_sma_sql_instance @send_mail = 1, @verbose = 0, @truncate_log_table = 1;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Populate Inventory Tables" -d "DBA" -Q "exec dbo.usp_wrapper_populate_sma_sql_instance @send_mail = 1, @verbose = 0, @truncate_log_table = 1;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Purge-Tables].sql b/DDLs/SCH-Job-[(dba) Purge-Tables].sql index b3d4296..34942c2 100644 --- a/DDLs/SCH-Job-[(dba) Purge-Tables].sql +++ b/DDLs/SCH-Job-[(dba) Purge-Tables].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Purge-Tables" -d DBA -Q "EXEC dbo.usp_purge_tables;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Purge-Tables" -d DBA -Q "EXEC dbo.usp_purge_tables;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Run-BlitzIndex - Weekly].sql b/DDLs/SCH-Job-[(dba) Run-BlitzIndex - Weekly].sql index 014c112..e077c59 100644 --- a/DDLs/SCH-Job-[(dba) Run-BlitzIndex - Weekly].sql +++ b/DDLs/SCH-Job-[(dba) Run-BlitzIndex - Weekly].sql @@ -40,7 +40,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'sp_Blitz @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S "localhost" -H "(dba) Run-BlitzIndex - Weekly - @Mode = 0" -d "DBA" -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 0, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex_Mode0'';"', + @command=N'sqlcmd -C -E -b -S "localhost" -H "(dba) Run-BlitzIndex - Weekly - @Mode = 0" -d "DBA" -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 0, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex_Mode0'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback @@ -54,7 +54,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'sp_Blitz @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S "localhost" -H "(dba) Run-BlitzIndex - Weekly - @Mode = 1" -d "DBA" -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 1, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex_Mode1'';"', + @command=N'sqlcmd -C -E -b -S "localhost" -H "(dba) Run-BlitzIndex - Weekly - @Mode = 1" -d "DBA" -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 1, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex_Mode1'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback @@ -68,7 +68,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'sp_Blitz @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S "localhost" -H "(dba) Run-BlitzIndex - Weekly - @Mode = 4" -d "DBA" -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 4, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex_Mode4'';"', + @command=N'sqlcmd -C -E -b -S "localhost" -H "(dba) Run-BlitzIndex - Weekly - @Mode = 4" -d "DBA" -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 4, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex_Mode4'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback diff --git a/DDLs/SCH-Job-[(dba) Run-BlitzIndex].sql b/DDLs/SCH-Job-[(dba) Run-BlitzIndex].sql index 80df4e5..0436626 100644 --- a/DDLs/SCH-Job-[(dba) Run-BlitzIndex].sql +++ b/DDLs/SCH-Job-[(dba) Run-BlitzIndex].sql @@ -40,7 +40,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'sp_Blitz @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Run-BlitzIndex" -d DBA -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 2, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Run-BlitzIndex" -d DBA -Q "EXEC master.dbo.sp_BlitzIndex @GetAllDatabases = 1, @Mode = 2, @BringThePain = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''BlitzIndex'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Run-Blitz].sql b/DDLs/SCH-Job-[(dba) Run-Blitz].sql index ecb16d0..5c2e56a 100644 --- a/DDLs/SCH-Job-[(dba) Run-Blitz].sql +++ b/DDLs/SCH-Job-[(dba) Run-Blitz].sql @@ -40,7 +40,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'sp_Blitz @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Run-Blitz" -d DBA -Q "EXEC master.dbo.sp_Blitz @CheckUserDatabaseObjects = 1, @BringThePain = 1, @CheckServerInfo = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''Blitz'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Run-Blitz" -d DBA -Q "EXEC master.dbo.sp_Blitz @CheckUserDatabaseObjects = 1, @BringThePain = 1, @CheckServerInfo = 1, @OutputDatabaseName = ''DBA'', @OutputSchemaName = ''dbo'', @OutputTableName = ''Blitz'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Run-LogSaver].sql b/DDLs/SCH-Job-[(dba) Run-LogSaver].sql index 1e747a9..0f0f947 100644 --- a/DDLs/SCH-Job-[(dba) Run-LogSaver].sql +++ b/DDLs/SCH-Job-[(dba) Run-LogSaver].sql @@ -42,7 +42,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'usp_LogS @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Run-LogSaver" -d DBA -Q "EXEC dbo.usp_LogSaver @email_recipients = ''dba_team@gmail.com'', @log_used_pct_threshold = 80, @log_used_gb_threshold = 100, @threshold_condition = ''or'', @purge_table = 1, @kill_spids = 0, @send_email = 0, @drop_create_table = 0, @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Run-LogSaver" -d DBA -Q "EXEC dbo.usp_LogSaver @email_recipients = ''dba_team@gmail.com'', @log_used_pct_threshold = 80, @log_used_gb_threshold = 100, @threshold_condition = ''or'', @purge_table = 1, @kill_spids = 0, @send_email = 0, @drop_create_table = 0, @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Run-TempDbSaver].sql b/DDLs/SCH-Job-[(dba) Run-TempDbSaver].sql index 38a30de..50b29fb 100644 --- a/DDLs/SCH-Job-[(dba) Run-TempDbSaver].sql +++ b/DDLs/SCH-Job-[(dba) Run-TempDbSaver].sql @@ -42,7 +42,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'usp_Temp @retry_attempts=1, @retry_interval=1, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Run-TempDbSaver" -d DBA -Q "EXEC dbo.usp_TempDbSaver @email_recipients = ''dba_team@gmail.com'', @data_used_pct_threshold = 80, @data_used_gb_threshold = 500, @threshold_condition = ''or'', @purge_table = 1, @kill_spids = 0, @send_email = 0, @drop_create_table = 0, @verbose = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Run-TempDbSaver" -d DBA -Q "EXEC dbo.usp_TempDbSaver @email_recipients = ''dba_team@gmail.com'', @data_used_pct_threshold = 80, @data_used_gb_threshold = 500, @threshold_condition = ''or'', @purge_table = 1, @kill_spids = 0, @send_email = 0, @drop_create_table = 0, @verbose = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Run-WhoIsActive].sql b/DDLs/SCH-Job-[(dba) Run-WhoIsActive].sql index 8f90616..6d4c4a2 100644 --- a/DDLs/SCH-Job-[(dba) Run-WhoIsActive].sql +++ b/DDLs/SCH-Job-[(dba) Run-WhoIsActive].sql @@ -43,7 +43,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_attempts=1, @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Run-WhoIsActive" -d DBA -Q "EXEC dbo.usp_run_WhoIsActive @retention_day = 7, @recipients = ''dba_team@gmail.com'';"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Run-WhoIsActive" -d DBA -Q "EXEC dbo.usp_run_WhoIsActive @retention_day = 7, @recipients = ''dba_team@gmail.com'';"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Job-[(dba) Send Login Expiry EMails].sql b/DDLs/SCH-Job-[(dba) Send Login Expiry EMails].sql index 4596ffe..eb3e875 100644 --- a/DDLs/SCH-Job-[(dba) Send Login Expiry EMails].sql +++ b/DDLs/SCH-Job-[(dba) Send Login Expiry EMails].sql @@ -42,7 +42,7 @@ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'dbo.usp_ @retry_interval=0, @os_run_priority=0, @subsystem=N'CmdExec', - @command=N'sqlcmd -E -b -S localhost -H "(dba) Send Login Expiry EMails" -d DBA -Q "exec dbo.usp_send_login_expiry_emails @warning_threshold_days = 20, @critical_threshold_days = 10, @mail_subject = ''*** IMPORTANT - Database Password Expiration Notification'', @job_name = ''(dba) Send Login Expiry EMails'', @sre_vp_threshold_days = 7, @cto_threshold_days = 3, @copy_dba_team_for_all_mails = 0, @send_mail = 1, @enable_dba_mail_while_testing = 0;"', + @command=N'sqlcmd -C -E -b -S localhost -H "(dba) Send Login Expiry EMails" -d DBA -Q "exec dbo.usp_send_login_expiry_emails @warning_threshold_days = 20, @critical_threshold_days = 10, @mail_subject = ''*** IMPORTANT - Database Password Expiration Notification'', @job_name = ''(dba) Send Login Expiry EMails'', @sre_vp_threshold_days = 7, @cto_threshold_days = 3, @copy_dba_team_for_all_mails = 0, @send_mail = 1, @enable_dba_mail_while_testing = 0;"', @flags=40 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 diff --git a/DDLs/SCH-Linked-Servers-Sample.sql b/DDLs/SCH-Linked-Servers-Sample.sql index c370d55..a141c21 100644 --- a/DDLs/SCH-Linked-Servers-Sample.sql +++ b/DDLs/SCH-Linked-Servers-Sample.sql @@ -6,6 +6,11 @@ EXEC master.dbo.sp_addlinkedserver @server = N'YourSqlInstanceNameHere', @srvpro EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'YourSqlInstanceNameHere',@useself=N'False',@locallogin=NULL,@rmtuser=N'grafana',@rmtpassword='grafana' GO +-- Major Version is sql2014 or lower, then skip below option +IF (select left(cast(serverproperty('productversion') as varchar), charindex('.', cast(serverproperty('productversion') as varchar)) - 1)) > 12 + EXEC master.dbo.sp_serveroption @server=N'YourSqlInstanceNameHere', @optname = 'provider string', @optvalue = 'Encrypt=yes;TrustServerCertificate=yes;User ID=grafana'; +GO + EXEC master.dbo.sp_serveroption @server=N'YourSqlInstanceNameHere', @optname=N'collation compatible', @optvalue=N'false' GO diff --git a/DDLs/SCH-usp_GetAllServerCollectedData.sql b/DDLs/SCH-usp_GetAllServerCollectedData.sql index 20f244e..e09fb78 100644 --- a/DDLs/SCH-usp_GetAllServerCollectedData.sql +++ b/DDLs/SCH-usp_GetAllServerCollectedData.sql @@ -23,6 +23,9 @@ go ALTER PROCEDURE dbo.usp_GetAllServerCollectedData ( @servers varchar(max) = null, /* comma separated list of servers to query */ @result_to_table nvarchar(125), /* table that need to be populated */ + @paginate bit = 0, /* when true, means this proc is running in multiple sessions. So table should not be truncated */ + @page_count int = 1, /* Divide the server count in these pages */ + @page_no int = 1, /* Compulate info for servers of this page */ @verbose tinyint = 0, /* display debugging messages. 0 = No messages. 1 = Only print messages. 2 = Print & Table Results */ @truncate_table bit = 1, /* when enabled, table would be truncated */ @has_staging_table bit = 1 /* when enabled, assume there is no staging table */ @@ -32,8 +35,9 @@ AS BEGIN /* - Version: 2024-08-20 - Date: 2024-08-20 - #10 Add error log entry + Version: 2026-01-31 + Date: 2026-01-31 - #3 Infra to Track Server and Database Configuration Changes + 2024-08-20 - #10 Add error log entry 2024-02-10 - #26 Track Status of SQLAgent Service 2024-01-08 - Backup History on Dashboard 2023-10-17 - Add Latency Dashboard for AG @@ -54,7 +58,7 @@ BEGIN IF @result_to_table NOT IN ('dbo.sql_agent_jobs_all_servers','dbo.disk_space_all_servers','dbo.log_space_consumers_all_servers', 'dbo.tempdb_space_usage_all_servers','dbo.ag_health_state_all_servers','dbo.backups_all_servers', - 'dbo.services_all_servers') + 'dbo.services_all_servers','dbo.alert_history_all_servers') THROW 50001, '''result_to_table'' Parameter value is invalid.', 1; declare @_start_time datetime2 = sysdatetime(); @@ -108,14 +112,16 @@ BEGIN IF @verbose >= 2 BEGIN SELECT @_int_variable = COUNT(1) FROM @_tbl_servers; - PRINT 'No of servers to process => '+CONVERT(varchar,@_int_variable)+''; + PRINT 'No of servers to process => '+CONVERT(varchar(125),@_int_variable)+''; SELECT [RunningQuery] = 'select * from @_tbl_servers', * FROM @_tbl_servers; END - IF @verbose >= 2 - BEGIN - select distinct [RunningQuery] = 'Cursor-Servers', [srvname] = sql_instance + -- Populate table to get list of Servers to process + if object_id('tempdb..#instance_details') is not null + drop table #instance_details + ;with cte_instance_details as ( + select distinct [srvname] = sql_instance from dbo.instance_details where is_available = 1 and is_enabled = 1 and ( ( @servers is null @@ -126,7 +132,24 @@ BEGIN --or source_sql_instance in (select srv_name from @_tbl_servers) ) ) + ) + ) + ,cte_instance_details_paged as ( + select srvname, page_no = NTILE(@page_count) over (order by srvname) + from cte_instance_details + ) + select srvname + into #instance_details + from cte_instance_details_paged + where 1=1 + and ( @paginate = 0 + or ( @paginate = 1 and page_no = @page_no ) ); + + IF @verbose >= 2 + BEGIN + select [RunningQuery] = 'Cursor-Servers', srvname + from #instance_details END IF @truncate_table = 1 @@ -138,18 +161,8 @@ BEGIN END DECLARE cur_servers CURSOR LOCAL FORWARD_ONLY FOR - select distinct [srvname] = sql_instance - from dbo.instance_details - where is_available = 1 and is_enabled = 1 - and ( ( @servers is null - and is_alias = 0 - ) - or ( @servers is not null - and ( sql_instance in (select srv_name from @_tbl_servers) - --or source_sql_instance in (select srv_name from @_tbl_servers) - ) - ) - ); + select srvname + from #instance_details; OPEN cur_servers; FETCH NEXT FROM cur_servers INTO @_srv_name; @@ -172,8 +185,6 @@ BEGIN begin set @_isLocalHost = 0 begin try - --set @_sql = "SELECT @@servername as srv_name;"; - --set @_sql = 'select * from openquery(' + QUOTENAME(@_srv_name) + ', "'+ @_sql + '")'; exec sys.sp_testlinkedserver @_srv_name; end try begin catch @@ -197,8 +208,6 @@ BEGIN [remark] = null, [executed_by] = SUSER_NAME(), [executor_program_name] = @_caller_program; set @_linked_server_failed = 1; - --fetch next from cur_servers into @_srv_name; - --continue; end catch; end @@ -721,6 +730,69 @@ and (dm.servicename like 'SQL Server (%)' or dm.servicename like 'SQL Server Age end catch end + + -- dbo.alert_history_all_servers + if @_linked_server_failed = 0 and @result_to_table = 'dbo.alert_history_all_servers' + begin + declare @_alert_history_collection_time_utc datetime2 = DATEADD(hour,-2,sysutcdatetime()); + + select @_alert_history_collection_time_utc = coalesce(max(collection_time_utc), @_alert_history_collection_time_utc) + from dbo.alert_history_all_servers ahas + where ahas.collection_time_utc > DATEADD(hour,-2,sysutcdatetime()) + and ahas.sql_instance = @_srv_name; + + set @_sql = " +SET QUOTED_IDENTIFIER ON; +SET NOCOUNT ON; +SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; +SET LOCK_TIMEOUT 60000; -- 60 seconds + +declare @_alert_history_collection_time_utc datetime2 = '"+convert(varchar,@_alert_history_collection_time_utc,121)+"'; + +select collection_time_utc = convert(varchar,ah.collection_time_utc,121), + [sql_instance] = '"+@_srv_name+"', + ah.server_name, ah.database_name, ah.error_number, + ah.error_severity, ah.error_message, ah.host_instance, + [updated_time_utc] = sysutcdatetime() +from dbo.alert_history ah +where 1=1 +and ah.collection_time_utc > @_alert_history_collection_time_utc +" + -- Decorate for remote query if LinkedServer + if @_isLocalHost = 0 + set @_sql = 'select * from openquery(' + QUOTENAME(@_srv_name) + ', "'+ @_sql + '")'; + if @verbose >= 2 or (@verbose >= 1 and @_counter = 1) + print @_crlf+@_sql+@_crlf; + + begin try + insert [dbo].[alert_history_all_servers] + ( [collection_time_utc], [sql_instance], [server_name], [database_name], + [error_number], [error_severity], [error_message], [host_instance], [updated_time_utc]) + exec (@_sql); + end try + begin catch + select @_errorNumber = Error_Number() + ,@_errorSeverity = Error_Severity() + ,@_errorState = Error_State() + ,@_errorLine = Error_Line() + ,@_errorMessage = Error_Message(); + + insert [dbo].[sma_errorlog] + ([collection_time], [function_name], [function_call_arguments], [server], [error], [remark], [executed_by], [executor_program_name]) + select [collection_time] = @_start_time, [function_name] = 'usp_GetAllServerCollectedData', + [function_call_arguments] = 'dbo.services_all_servers', [server] = @_srv_name, [error] = @_errorMessage, + [remark] = null, [executed_by] = SUSER_NAME(), [executor_program_name] = @_caller_program; + + set @_errorMessage = 'Error Details => Severity: '+convert(varchar,isnull(@_errorSeverity,''))+ + '. State: '+convert(varchar,isnull(@_errorState,'')) + + '. Error Line: '+convert(varchar,isnull(@_errorLine,'')) + + '. Error Message::: '+ @_errorMessage; + + print @_crlf+@_long_star_line+@_crlf+'Error occurred while executing below query on ['+@_srv_name+'].'+@_crlf+@_errorMessage+@_crlf+' '+@_sql+@_long_star_line+@_crlf; + end catch + end + + -- All the logic should be within the Cursor Loop block FETCH NEXT FROM cur_servers INTO @_srv_name; END diff --git a/DDLs/SCH-usp_GetAllServerDashboardMail.sql b/DDLs/SCH-usp_GetAllServerDashboardMail.sql index 68a5298..f12d652 100644 --- a/DDLs/SCH-usp_GetAllServerDashboardMail.sql +++ b/DDLs/SCH-usp_GetAllServerDashboardMail.sql @@ -45,6 +45,7 @@ ALTER PROCEDURE dbo.usp_GetAllServerDashboardMail @full_threshold_days int = 8, @diff_threshold_hours int = 26, @tlog_threshold_minutes int = 240, + @alert_history_hours int = 4, @collect_core_health_metrics bit = 1, @collect_tempdb_health bit = 1, @collect_log_space bit = 1, @@ -53,18 +54,23 @@ ALTER PROCEDURE dbo.usp_GetAllServerDashboardMail @collect_offline_servers bit = 1, @collect_sqlmonitor_jobs bit = 1, @collect_backup_history bit = 1, + @collect_alert_history bit = 1, + @hide_row_if_no_data bit = 0, /* Hide all details in Mailer for all categories that have no alert issue */ @verbose tinyint = 0 /* 0 - no messages, 1 - debug messages, 2 = debug messages + table results */ ) AS BEGIN /* - Version: 2025-Jan-28 - Update: 2025-Jan-28 - Added Disk Latency in Core Health Metrics Table + Version: 2026-Feb-27 + Update: 2026-Feb-27 - #56 - Add alert_history into mailer + 2026-Jan-31 - #56 - Improve Presentation of Results + 2025-Jan-28 - Added Disk Latency in Core Health Metrics Table 2024-01-11 - #7 - Adding Backup Issues in mailer 2023-12-31 - #24 - Daily Mailer containing similar content of 'Monitoring - Live - All Servers' dashboard EXEC dbo.usp_GetAllServerDashboardMail @recipients = 'sqlagentservice@gmail.com', - @only_threshold_validated = 1, @send_mail = 1, @verbose = 2; + @only_threshold_validated = 1, @hide_row_if_no_data = 0, + @send_mail = 1, @verbose = 2; */ SET NOCOUNT ON; SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; @@ -90,13 +96,23 @@ BEGIN declare @_title nvarchar(2000); declare @_style_css nvarchar(max); declare @_html_core_health nvarchar(MAX); -- 'Core Health Metrics' + declare @_collection_time_core_health datetime = convert(date,'2000-01-01'); declare @_html_tempdb_health nvarchar(MAX); -- 'Tempdb Health' + declare @_collection_time_tempdb_health datetime = convert(date,'2000-01-01'); declare @_html_log_space_health nvarchar(MAX); -- 'Log Space' + declare @_collection_time_log_space_health datetime = convert(date,'2000-01-01'); declare @_html_ag_health nvarchar(MAX); -- 'Ag Latency' + declare @_collection_time_ag_health datetime = convert(date,'2000-01-01'); declare @_html_disk_health nvarchar(MAX); -- 'Disk Space' + declare @_collection_time_disk_health datetime = convert(date,'2000-01-01'); declare @_html_offline_servers nvarchar(MAX); -- 'Offline Servers' + declare @_collection_time_offline_servers datetime = convert(date,'2000-01-01'); declare @_html_sqlmonitor_jobs nvarchar(max); -- 'SQLMonitor Jobs' + declare @_collection_time_sqlmonitor_jobs datetime = convert(date,'2000-01-01'); declare @_html_backup_history nvarchar(max); -- 'Backup History' + declare @_collection_time_backup_history datetime = convert(date,'2000-01-01'); + declare @_html_alert_history nvarchar(max); -- 'Backup History' + declare @_collection_time_alert_history datetime = convert(date,'2000-01-01'); declare @_table_headline nvarchar(500); declare @_table_header nvarchar(max); declare @_table_data nvarchar(max); @@ -110,6 +126,7 @@ BEGIN declare @_url_offline_servers_panel varchar(4000) = @_url_all_servers_dashboard+'?viewPanel=844'; declare @_url_sqlmonitor_jobs_panel varchar(4000) = @_url_all_servers_dashboard+'?viewPanel=864'; declare @_url_backup_history_panel varchar(4000) = @_url_all_servers_dashboard+'?viewPanel=869'; + declare @_url_alert_history_panel varchar(4000) = @_url_all_servers_dashboard+'?viewPanel=890'; declare @_line nvarchar(500); declare @_tab nchar(2) = nchar(9); @@ -225,62 +242,9 @@ BEGIN if not exists (select * from dbo.vw_all_server_info) raiserror ('Data does not exist in dbo.vw_all_server_info', 17, -1) with log; - if @verbose > 1 - begin - ;with asi as ( - select srv_name, os_cpu, sql_cpu, blocked_counts, blocked_duration_max_seconds, avg_disk_latency_ms, - available_physical_memory_kb, system_high_memory_signal_state, physical_memory_in_use_kb, - memory_grants_pending, connection_count, waits_per_core_per_minute - ,issue_rank = (case when os_cpu >= 90 then 3 when os_cpu >= @os_cpu_threshold then 1 else 0 end) + - (case when os_cpu >= 80 then 3 when sql_cpu >= @sql_cpu_threshold then 1 else 0 end) + - (case when blocked_counts >= 20 then 5 - when blocked_counts >= 10 then 4 - when blocked_counts >= 5 then 2 - when blocked_counts >= @blocked_counts_threshold then 1 - else 0 - end) + - (case when blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold then 1 else 0 end) + - (case when ( available_physical_memory_kb < (@available_physical_memory_mb_threshold*1024) and system_high_memory_signal_state = @system_high_memory_signal_state_threshold ) then 1 else 0 end) + - (case when memory_grants_pending > 10 then 5 - when memory_grants_pending > 5 then 4 - when memory_grants_pending > @memory_grants_pending_threshold then 2 - else 0 - end) + - (case when connection_count >= @connection_count_threshold then 1 else 0 end) + - (case when avg_disk_latency_ms >= 100 then 5 - when avg_disk_latency_ms >= 80 then 4 - when avg_disk_latency_ms >= 50 then 3 - when avg_disk_latency_ms >= 35 then 2 - when avg_disk_latency_ms >= @avg_disk_latency_ms then 1 - else 0 - end) + - (case when waits_per_core_per_minute > @waits_per_core_per_minute_threshold then 1 else 0 end) - from dbo.vw_all_server_info - ) - ,asi_filtered as ( - select * - from asi - where 1=1 - and ( os_cpu >= @os_cpu_threshold - or sql_cpu >= @sql_cpu_threshold - or blocked_counts >= @blocked_counts_threshold - or blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold - or ( available_physical_memory_kb < (@available_physical_memory_mb_threshold*1024) - and system_high_memory_signal_state = @system_high_memory_signal_state_threshold - ) - or memory_grants_pending > @memory_grants_pending_threshold - --or connection_count >= @connection_count_threshold - or waits_per_core_per_minute > @waits_per_core_per_minute_threshold - or avg_disk_latency_ms >= @avg_disk_latency_ms - ) - ) - select [RunningQuery], cte.* - from asi_filtered cte - full outer join (select [RunningQuery] = 'Core Health Metrics') rq - on 1=1 - order by issue_rank desc, avg_disk_latency_ms desc, srv_name; - end - + -- Get temp table with alert data + if object_id('tempdb..#asi_filtered') is not null + drop table #asi_filtered; ;with asi as ( select srv_name, os_cpu, sql_cpu, blocked_counts, blocked_duration_max_seconds, avg_disk_latency_ms, available_physical_memory_kb, system_high_memory_signal_state, physical_memory_in_use_kb, @@ -311,97 +275,121 @@ BEGIN (case when waits_per_core_per_minute > @waits_per_core_per_minute_threshold then 1 else 0 end) from dbo.vw_all_server_info ) - ,asi_filtered as ( - select * - from asi - where 1=1 - and ( os_cpu >= @os_cpu_threshold - or sql_cpu >= @sql_cpu_threshold - or blocked_counts >= @blocked_counts_threshold - or blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold - or ( available_physical_memory_kb < (@available_physical_memory_mb_threshold*1024) - and system_high_memory_signal_state = @system_high_memory_signal_state_threshold - ) - or memory_grants_pending > @memory_grants_pending_threshold - --or connection_count >= @connection_count_threshold - or waits_per_core_per_minute > @waits_per_core_per_minute_threshold - or avg_disk_latency_ms >= @avg_disk_latency_ms + select * + into #asi_filtered + from asi + where 1=1 + and ( os_cpu >= @os_cpu_threshold + or sql_cpu >= @sql_cpu_threshold + or blocked_counts >= @blocked_counts_threshold + or blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold + or ( available_physical_memory_kb < (@available_physical_memory_mb_threshold*1024) + and system_high_memory_signal_state = @system_high_memory_signal_state_threshold ) - ) - ,t_cte as ( - select '' - +''+srv_name+'' - +''+convert(varchar,os_cpu)+'' - +''+convert(varchar,sql_cpu)+'' - +''+convert(varchar,isnull(blocked_counts,0))+'' - +''+isnull((case when blocked_duration_max_seconds < 60 then convert(varchar,floor(blocked_duration_max_seconds))+' sec' - when blocked_duration_max_seconds < 3600 then convert(varchar,floor(blocked_duration_max_seconds/60))+' min' - when blocked_duration_max_seconds < 86400 then convert(varchar,floor(blocked_duration_max_seconds/3600))+' hrs' - when blocked_duration_max_seconds >= 86400 then convert(varchar,floor(blocked_duration_max_seconds/86400))+' days' - else 'xx' end),0)+'' - +''+convert(varchar,isnull(avg_disk_latency_ms,0))+' ms' - +''+(case when available_physical_memory_kb < 1024 then convert(varchar,available_physical_memory_kb)+' kb' - when available_physical_memory_kb < 1024*1024 then convert(varchar,floor(available_physical_memory_kb/1024))+' mb' - when available_physical_memory_kb < 1024*1024*1024 then convert(varchar,floor(available_physical_memory_kb/(1024*1024)))+' gb' - when available_physical_memory_kb >= 1024*1024*1024 then convert(varchar,floor(available_physical_memory_kb/(1024*1024*1024)))+' tb' - else 'xx' end)+'' - --+''+system_high_memory_signal_state+'' - +''+(case when physical_memory_in_use_kb < 1024 then convert(varchar,physical_memory_in_use_kb)+' kb' - when physical_memory_in_use_kb < 1024*1024 then convert(varchar,floor(physical_memory_in_use_kb/1024))+' mb' - when physical_memory_in_use_kb < 1024*1024*1024 then convert(varchar,floor(physical_memory_in_use_kb/(1024*1024)))+' gb' - when physical_memory_in_use_kb >= 1024*1024*1024 then convert(varchar,floor(physical_memory_in_use_kb/(1024*1024*1024)))+' tb' - else 'xx' end)+'' - +''+convert(varchar,isnull(memory_grants_pending,0))+'' - /* - +''+convert(varchar,connection_count)+'' - */ - +''+isnull((case when waits_per_core_per_minute < 60 then convert(varchar,floor(waits_per_core_per_minute))+' sec' - when waits_per_core_per_minute < 3600 then convert(varchar,floor(waits_per_core_per_minute/60))+' min' - when waits_per_core_per_minute < 86400 then convert(varchar,floor(waits_per_core_per_minute/3600))+' hrs' - when waits_per_core_per_minute >= 86400 then convert(varchar,floor(waits_per_core_per_minute/86400))+' days' - else 'xx' end),'-1')+'' - +'' as [table_row] - from asi_filtered cte - where 1=1 - ) - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + or memory_grants_pending > @memory_grants_pending_threshold + --or connection_count >= @connection_count_threshold + or waits_per_core_per_minute > @waits_per_core_per_minute_threshold + or avg_disk_latency_ms >= @avg_disk_latency_ms + ); + + if @verbose > 1 + begin + select [RunningQuery], cte.* + from #asi_filtered cte + full outer join (select [RunningQuery] = 'Core Health Metrics') rq + on 1=1 + order by issue_rank desc, avg_disk_latency_ms desc, srv_name; + end + + -- Decide if alert data is present, or add empty info row + if exists (select * from #asi_filtered cte) + begin + ;with t_cte as ( + select '' + +''+srv_name+'' + +''+convert(varchar,os_cpu)+'' + +''+convert(varchar,sql_cpu)+'' + +''+convert(varchar,isnull(blocked_counts,0))+'' + +''+isnull((case when blocked_duration_max_seconds < 60 then convert(varchar,floor(blocked_duration_max_seconds))+' sec' + when blocked_duration_max_seconds < 3600 then convert(varchar,floor(blocked_duration_max_seconds/60))+' min' + when blocked_duration_max_seconds < 86400 then convert(varchar,floor(blocked_duration_max_seconds/3600))+' hrs' + when blocked_duration_max_seconds >= 86400 then convert(varchar,floor(blocked_duration_max_seconds/86400))+' days' + else 'xx' end),0)+'' + +''+convert(varchar,isnull(avg_disk_latency_ms,0))+' ms' + +''+(case when available_physical_memory_kb < 1024 then convert(varchar,available_physical_memory_kb)+' kb' + when available_physical_memory_kb < 1024*1024 then convert(varchar,floor(available_physical_memory_kb/1024))+' mb' + when available_physical_memory_kb < 1024*1024*1024 then convert(varchar,floor(available_physical_memory_kb/(1024*1024)))+' gb' + when available_physical_memory_kb >= 1024*1024*1024 then convert(varchar,floor(available_physical_memory_kb/(1024*1024*1024)))+' tb' + else 'xx' end)+'' + --+''+system_high_memory_signal_state+'' + +''+(case when physical_memory_in_use_kb < 1024 then convert(varchar,physical_memory_in_use_kb)+' kb' + when physical_memory_in_use_kb < 1024*1024 then convert(varchar,floor(physical_memory_in_use_kb/1024))+' mb' + when physical_memory_in_use_kb < 1024*1024*1024 then convert(varchar,floor(physical_memory_in_use_kb/(1024*1024)))+' gb' + when physical_memory_in_use_kb >= 1024*1024*1024 then convert(varchar,floor(physical_memory_in_use_kb/(1024*1024*1024)))+' tb' + else 'xx' end)+'' + +''+convert(varchar,isnull(memory_grants_pending,0))+'' + /* + +''+convert(varchar,connection_count)+'' + */ + +''+isnull((case when waits_per_core_per_minute < 60 then convert(varchar,floor(waits_per_core_per_minute))+' sec' + when waits_per_core_per_minute < 3600 then convert(varchar,floor(waits_per_core_per_minute/60))+' min' + when waits_per_core_per_minute < 86400 then convert(varchar,floor(waits_per_core_per_minute/3600))+' hrs' + when waits_per_core_per_minute >= 86400 then convert(varchar,floor(waits_per_core_per_minute/86400))+' days' + else 'xx' end),'-1')+'' + +'' as [table_row] + from #asi_filtered cte + where 1=1 + ) + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_core_health_metrics = 0; + + select top 1 @_collection_time_core_health = collection_time + from dbo.all_server_volatile_info + order by collection_time desc; + + set @_table_data = 'No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_core_health,120)+''; + end set @_html_core_health = @_table_headline+'
' +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #tsu tsu + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; end + else + begin + if @hide_row_if_no_data = 1 + set @collect_tempdb_health = 0; - ;with tsu as ( - select [collection_time_utc] = [updated_date_utc], - [sql_instance], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], - [log_used_pct], [version_store_mb], [version_store_pct] + select top 1 @_collection_time_tempdb_health = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_date_utc) from dbo.tempdb_space_usage_all_servers su - where (su.data_used_pct > @data_used_pct - or su.data_used_mb > (@data_used_gb*1024) -- 200 gb - ) - and (su.updated_date_utc >= dateadd(minute,-60,getutcdate()) - and su.collection_time_utc >= dateadd(minute,-20,getutcdate()) - ) - ) - ,t_cte as ( - select '' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' as [table_row] - from tsu - where 1=1 - ) - --select * from t_cte; - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + order by updated_date_utc desc; + + set @_table_data = ''; + end set @_html_tempdb_health = '

'+@_table_headline+'
@os_cpu_threshold:'+convert(varchar,@os_cpu_threshold) @@ -446,90 +434,95 @@ BEGIN if not exists (select * from dbo.tempdb_space_usage_all_servers) raiserror ('Data does not exist in dbo.tempdb_space_usage_all_servers', 17, -1) with log; + -- Get temp table with alert data + if object_id('tempdb..#tsu') is not null + drop table #tsu; + select [collection_time_utc] = [updated_date_utc], + [sql_instance], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], + [log_used_pct], [version_store_mb], [version_store_pct] + into #tsu + from dbo.tempdb_space_usage_all_servers su + where (su.data_used_pct > @data_used_pct + or su.data_used_mb > (@data_used_gb*1024) -- 200 gb + ) + and (su.updated_date_utc >= dateadd(minute,-60,getutcdate()) + and su.collection_time_utc >= dateadd(minute,-20,getutcdate()) + ); + if @verbose > 1 begin - ;with t_cte as ( - select [collection_time_utc] = [updated_date_utc], - [sql_instance], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], - [log_used_pct], [version_store_mb], [version_store_pct] - from dbo.tempdb_space_usage_all_servers su - where 1=1 - and (su.data_used_pct > @data_used_pct - or su.data_used_mb > (@data_used_gb*1024) -- 200 gb - ) - and (su.updated_date_utc >= dateadd(minute,-60,getutcdate()) - and su.collection_time_utc >= dateadd(minute,-20,getutcdate()) - ) - ) select [RunningQuery], t_cte.* - from t_cte + from #tsu t_cte full outer join (select [RunningQuery] = 'Tempdb Health') rq on 1=1 + end + + -- Decide if alert data is present, or add empty info row + if exists (select * from #tsu) + begin + ;with t_cte as ( + select '
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), collection_time_utc),120)+''+sql_instance+''+(case when data_size_mb < 1024 then convert(varchar,data_size_mb)+' mb' + when data_size_mb < 1024*1024 then convert(varchar,floor(data_size_mb/1024))+' gb' + when data_size_mb >= 1024*1024 then convert(varchar,floor(data_size_mb/(1024*1024)))+' tb' + else 'xx' end)+''+(case when data_used_mb < 1024 then convert(varchar,data_used_mb)+' mb' + when data_used_mb < 1024*1024 then convert(varchar,floor(data_used_mb/1024))+' gb' + when data_used_mb >= 1024*1024 then convert(varchar,floor(data_used_mb/(1024*1024)))+' tb' + else 'xx' end)+''+convert(varchar,data_used_pct)+''+(case when log_size_mb < 1024 then convert(varchar,log_size_mb)+' mb' + when log_size_mb < 1024*1024 then convert(varchar,floor(log_size_mb/1024))+' gb' + when log_size_mb >= 1024*1024 then convert(varchar,floor(log_size_mb/(1024*1024)))+' tb' + else 'xx' end)+''+(case when log_used_mb < 1024 then convert(varchar,log_used_mb)+' mb' + when log_used_mb < 1024*1024 then convert(varchar,floor(log_used_mb/1024))+' gb' + when log_used_mb >= 1024*1024 then convert(varchar,floor(log_used_mb/(1024*1024)))+' tb' + else 'xx' end)+''+convert(varchar,log_used_pct)+''+(case when version_store_mb < 1024 then convert(varchar,version_store_mb)+' mb' + when version_store_mb < 1024*1024 then convert(varchar,floor(version_store_mb/1024))+' gb' + when version_store_mb >= 1024*1024 then convert(varchar,floor(version_store_mb/(1024*1024)))+' tb' + else 'xx' end)+''+convert(varchar,version_store_pct)+'
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), collection_time_utc),120)+''+sql_instance+''+(case when data_size_mb < 1024 then convert(varchar,data_size_mb)+' mb' - when data_size_mb < 1024*1024 then convert(varchar,floor(data_size_mb/1024))+' gb' - when data_size_mb >= 1024*1024 then convert(varchar,floor(data_size_mb/(1024*1024)))+' tb' - else 'xx' end)+''+(case when data_used_mb < 1024 then convert(varchar,data_used_mb)+' mb' - when data_used_mb < 1024*1024 then convert(varchar,floor(data_used_mb/1024))+' gb' - when data_used_mb >= 1024*1024 then convert(varchar,floor(data_used_mb/(1024*1024)))+' tb' - else 'xx' end)+''+convert(varchar,data_used_pct)+''+(case when log_size_mb < 1024 then convert(varchar,log_size_mb)+' mb' - when log_size_mb < 1024*1024 then convert(varchar,floor(log_size_mb/1024))+' gb' - when log_size_mb >= 1024*1024 then convert(varchar,floor(log_size_mb/(1024*1024)))+' tb' - else 'xx' end)+''+(case when log_used_mb < 1024 then convert(varchar,log_used_mb)+' mb' - when log_used_mb < 1024*1024 then convert(varchar,floor(log_used_mb/1024))+' gb' - when log_used_mb >= 1024*1024 then convert(varchar,floor(log_used_mb/(1024*1024)))+' tb' - else 'xx' end)+''+convert(varchar,log_used_pct)+''+(case when version_store_mb < 1024 then convert(varchar,version_store_mb)+' mb' - when version_store_mb < 1024*1024 then convert(varchar,floor(version_store_mb/1024))+' gb' - when version_store_mb >= 1024*1024 then convert(varchar,floor(version_store_mb/(1024*1024)))+' tb' - else 'xx' end)+''+convert(varchar,version_store_pct)+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_tempdb_health,120)+'
' +'' @@ -565,84 +558,101 @@ BEGIN if not exists (select * from dbo.log_space_consumers_all_servers) raiserror ('Data does not exist in dbo.log_space_consumers_all_servers', 17, -1) with log; + -- Get temp table with alert data + if object_id('tempdb..#lsc') is not null + drop table #lsc; + create table #lsc ( + [collection_time_utc] datetime2 not null, + [sql_instance] varchar(255) not null, + [database_name] varchar(256) not null, + [recovery_model] varchar(20) not null, + [log_reuse_wait_desc] varchar(125) null, + [log_size_mb] decimal(20,2) not null, + [exists_valid_autogrowing_file] bit default 0 not null, + [log_used_mb] decimal(20,2) not null, + [log_used_pct] decimal(20,2) not null, + [login_name] varchar(256) null, + [program_name] varchar(256) null + ); + + set @_params = '@only_threshold_validated bit, @log_used_pct float, @log_used_gb float'; + set @_sql = ' + select [collection_time_utc] = [updated_date_utc], + [sql_instance], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [exists_valid_autogrowing_file], + [log_used_mb], [log_used_pct], [login_name], [program_name] + --,[log_used_pct_threshold], [log_used_gb_threshold], [spid] + --,[transaction_start_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [transaction_start_time]) + --,[host_name], [host_process_id], [command], [additional_info] + --,[action_taken], [sql_text] + from dbo.log_space_consumers_all_servers ls + where 1=1 + '+(case when @only_threshold_validated = 1 then '' else '--' end)+'and ls.thresholds_validated = @only_threshold_validated + '+(case when @only_threshold_validated = 1 then '--' else '' end)+'and ( (ls.log_used_pct > @log_used_pct) or (ls.log_used_mb > (@log_used_gb*1024)) ) + and (ls.updated_date_utc >= dateadd(minute,-60,getutcdate()) + and ls.collection_time_utc >= dateadd(minute,-20,getutcdate()) + ) + '; + + insert #lsc + ( [collection_time_utc], [sql_instance], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], + [exists_valid_autogrowing_file], [log_used_mb], [log_used_pct], [login_name], [program_name] + ) + exec sp_executesql @_sql, @_params, @only_threshold_validated, @log_used_pct, @log_used_gb; + if @verbose > 1 begin - set @_params = '@only_threshold_validated bit, @log_used_pct float, @log_used_gb float'; - set @_sql = ' - ;with t_cte as ( - select [collection_time_utc] = [updated_date_utc], - [sql_instance], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [exists_valid_autogrowing_file], - [log_used_mb], [log_used_pct], [login_name], [program_name] - --,[log_used_pct_threshold], [log_used_gb_threshold], [spid] - --,[transaction_start_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [transaction_start_time]) - --,[host_name], [host_process_id], [command], [additional_info] - --,[action_taken], [sql_text] - from dbo.log_space_consumers_all_servers ls - where 1=1 - '+(case when @only_threshold_validated = 1 then '' else '--' end)+'and ls.thresholds_validated = @only_threshold_validated - '+(case when @only_threshold_validated = 1 then '--' else '' end)+'and ( (ls.log_used_pct > @log_used_pct) or (ls.log_used_mb > (@log_used_gb*1024)) ) - and (ls.updated_date_utc >= dateadd(minute,-60,getutcdate()) - and ls.collection_time_utc >= dateadd(minute,-20,getutcdate()) - ) - ) select [RunningQuery], t_cte.* - from t_cte - full outer join (select [RunningQuery] = ''Log Space'') rq - on 1=1 - '; + from #lsc t_cte + full outer join (select [RunningQuery] = 'Log Space') rq + on 1=1; + end - exec sp_executesql @_sql, @_params, @only_threshold_validated, @log_used_pct, @log_used_gb; + -- Decide if alert data is present, or add empty info row + if exists (select * from #lsc) + begin + ;with t_cte as ( + select '' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #lsc + ) + --select * from t_cte + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; end + else + begin + if @hide_row_if_no_data = 1 + set @collect_log_space = 0; - set @_params = '@only_threshold_validated bit, @log_used_pct float, @log_used_gb float, @table_data nvarchar(max) output'; - set @_sql = ' - ;with lsc as - ( - select [collection_time_utc] = [updated_date_utc], - [sql_instance], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [exists_valid_autogrowing_file], - [log_used_mb], [log_used_pct], [login_name], [program_name] + select top 1 @_collection_time_log_space_health = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_date_utc) from dbo.log_space_consumers_all_servers ls - where 1=1 - '+(case when @only_threshold_validated = 1 then '' else '--' end)+'and ls.thresholds_validated = @only_threshold_validated - '+(case when @only_threshold_validated = 1 then '--' else '' end)+'and ( (ls.log_used_pct > @log_used_pct) or (ls.log_used_mb > (@log_used_gb*1024)) ) - and (ls.updated_date_utc >= dateadd(minute,-60,getutcdate()) - and ls.collection_time_utc >= dateadd(minute,-20,getutcdate()) - ) - ) - ,t_cte as ( - select '''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' - +'''' as [table_row] - from lsc - ) - --select * from t_cte - select @table_data = coalesce(@table_data+'' ''+[table_row],[table_row]) - from t_cte; - '; + order by updated_date_utc desc; - exec sp_executesql @_sql, @_params, @only_threshold_validated, @log_used_pct, @log_used_gb, @table_data = @_table_data output; + set @_table_data = ''; + end set @_html_log_space_health = '

'+@_table_headline+'
@data_used_pct:'+convert(varchar,@data_used_pct)+' || @data_used_gb:'+convert(varchar,@data_used_gb)+'
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), collection_time_utc),120)+''+sql_instance+''+[database_name]+''+recovery_model+''+log_reuse_wait_desc+''+(case when log_size_mb < 1024 then convert(varchar,log_size_mb)+' mb' + when log_size_mb < 1024*1024 then convert(varchar,floor(log_size_mb/1024))+' gb' + when log_size_mb >= 1024*1024 then convert(varchar,floor(log_size_mb/(1024*1024)))+' tb' + else 'xx' end)+''+convert(varchar,exists_valid_autogrowing_file)+''+(case when log_used_mb < 1024 then convert(varchar,log_used_mb)+' mb' + when log_used_mb < 1024*1024 then convert(varchar,floor(log_used_mb/1024))+' gb' + when log_used_mb >= 1024*1024 then convert(varchar,floor(log_used_mb/(1024*1024)))+' tb' + else 'xx' end)+''+convert(varchar,log_used_pct)+''+coalesce([login_name],'')+''+coalesce([program_name],'')+'
''+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), collection_time_utc),120)+''''+sql_instance+''''+[database_name]+''''+recovery_model+''''+log_reuse_wait_desc+''''+(case when log_size_mb < 1024 then convert(varchar,log_size_mb)+'' mb'' - when log_size_mb < 1024*1024 then convert(varchar,floor(log_size_mb/1024))+'' gb'' - when log_size_mb >= 1024*1024 then convert(varchar,floor(log_size_mb/(1024*1024)))+'' tb'' - else ''xx'' end)+''''+convert(varchar,exists_valid_autogrowing_file)+''''+(case when log_used_mb < 1024 then convert(varchar,log_used_mb)+'' mb'' - when log_used_mb < 1024*1024 then convert(varchar,floor(log_used_mb/1024))+'' gb'' - when log_used_mb >= 1024*1024 then convert(varchar,floor(log_used_mb/(1024*1024)))+'' tb'' - else ''xx'' end)+''''+convert(varchar,log_used_pct)+''''+coalesce([login_name],'''')+''''+coalesce([program_name],'''')+''
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_log_space_health,120)+'
' +'' @@ -678,100 +688,105 @@ BEGIN if not exists (select * from dbo.ag_health_state_all_servers) print 'Data does not exist in dbo.ag_health_state_all_servers'; + -- Get temp table with alert data + if object_id('tempdb..#aghs') is not null + drop table #aghs; + select sql_instance, [replica_database] = replica_server_name+' || '+database_name, + is_primary_replica, ag_listener, is_local, synchronization_state_desc, synchronization_health_desc, + latency_seconds, log_send_queue_size, redo_queue_size, is_suspended + into #aghs + from dbo.ag_health_state_all_servers ahs + where 1=1 + and ( ahs.synchronization_health_desc <> 'HEALTHY' + or ahs.synchronization_state_desc not in ('SYNCHRONIZED','SYNCHRONIZING') + or (ahs.latency_seconds is not null and ahs.latency_seconds >= @ag_latency_minutes*60) + or (ahs.log_send_queue_size is not null and ahs.log_send_queue_size >= @log_send_queue_size_gb*1024*1024) + or (ahs.redo_queue_size is not null and ahs.redo_queue_size >= @ag_redo_queue_size_gb*1024*1024) + ); + if @verbose > 1 begin - ;with t_cte as ( - select sql_instance, [replica_database] = replica_server_name+' || '+database_name, - is_primary_replica, ag_listener, is_local, synchronization_state_desc, synchronization_health_desc, - latency_seconds, log_send_queue_size, redo_queue_size, is_suspended - from dbo.ag_health_state_all_servers ahs - where 1=1 - and ( ahs.synchronization_health_desc <> 'HEALTHY' - or ahs.synchronization_state_desc not in ('SYNCHRONIZED','SYNCHRONIZING') - or (ahs.latency_seconds is not null and ahs.latency_seconds >= @ag_latency_minutes*60) - or (ahs.log_send_queue_size is not null and ahs.log_send_queue_size >= @log_send_queue_size_gb*1024*1024) - or (ahs.redo_queue_size is not null and ahs.redo_queue_size >= @ag_redo_queue_size_gb*1024*1024) - ) - ) select [RunningQuery], t_cte.* - from t_cte + from #aghs t_cte full outer join (select [RunningQuery] = 'Ag Latency') rq on 1=1; end - - ;with tsu as ( - select sql_instance, [replica_database] = replica_server_name+' || '+database_name, - is_primary_replica, ag_listener, is_local, synchronization_state_desc, synchronization_health_desc, - latency_seconds, log_send_queue_size, redo_queue_size, is_suspended - from dbo.ag_health_state_all_servers ahs - where 1=1 - and ( ahs.synchronization_health_desc <> 'HEALTHY' - or ahs.synchronization_state_desc not in ('SYNCHRONIZED','SYNCHRONIZING') - or (ahs.latency_seconds is not null and ahs.latency_seconds >= @ag_latency_minutes*60) - or (ahs.log_send_queue_size is not null and ahs.log_send_queue_size >= @log_send_queue_size_gb*1024*1024) - or (ahs.redo_queue_size is not null and ahs.redo_queue_size >= @ag_redo_queue_size_gb*1024*1024) - ) - ) - ,t_cte as ( - select '' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' - +'' - +'' as [table_row] - from tsu - where 1=1 - ) - --select * from t_cte; - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + +'' + +'' + +'' + +'' as [table_row] + from #aghs + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_ag_latency = 0; + + select top 1 @_collection_time_ag_health = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_date_utc) + from dbo.ag_health_state_all_servers aghs + order by updated_date_utc desc; + + set @_table_data = ''; + end set @_html_ag_health = '

'+@_table_headline+'
@only_threshold_validated:'+convert(varchar,@only_threshold_validated)+' || @log_used_pct:'+convert(varchar,@log_used_pct)+' || @log_used_gb:'+convert(varchar,@log_used_gb)+'
'+sql_instance+''+replica_database+''+convert(varchar,is_primary_replica)+''+isnull(ag_listener,'')+''+convert(varchar,is_local)+''+isnull(synchronization_state_desc,'')+''+isnull(synchronization_health_desc,'')+''+isnull((case when latency_seconds < 60 then convert(varchar,floor(latency_seconds))+' sec' - when latency_seconds < 3600 then convert(varchar,floor(latency_seconds/60))+' min' - when latency_seconds < 86400 then convert(varchar,floor(latency_seconds/3600))+' hrs' - when latency_seconds >= 86400 then convert(varchar,floor(latency_seconds/86400))+' days' - else '' end),' ')+''+ - isnull((case when log_send_queue_size < 1024 then convert(varchar,log_send_queue_size)+' kb' - when log_send_queue_size < 1024*1024 then convert(varchar,log_send_queue_size/1024)+' mb' - when log_send_queue_size < 1024*1024*1024 then convert(varchar,floor(log_send_queue_size/(1024*1024)))+' gb' - when log_send_queue_size >= 1024*1024*1024 then convert(varchar,floor(log_send_queue_size/(1024*1024*1024)))+' tb' - else '' end),' ')+'' - +isnull((case when redo_queue_size < 1024 then convert(varchar,redo_queue_size)+' kb' - when redo_queue_size < 1024*1024 then convert(varchar,redo_queue_size/1024)+' mb' - when redo_queue_size < 1024*1024*1024 then convert(varchar,floor(redo_queue_size/(1024*1024)))+' gb' - when redo_queue_size >= 1024*1024*1024 then convert(varchar,floor(redo_queue_size/(1024*1024*1024)))+' tb' + + -- Decide if alert data is present, or add empty info row + if exists (select * from #aghs) + begin + ;with t_cte as ( + select '
'+sql_instance+''+replica_database+''+convert(varchar,is_primary_replica)+''+isnull(ag_listener,'')+''+convert(varchar,is_local)+''+isnull(synchronization_state_desc,'')+''+isnull(synchronization_health_desc,'')+''+isnull((case when latency_seconds < 60 then convert(varchar,floor(latency_seconds))+' sec' + when latency_seconds < 3600 then convert(varchar,floor(latency_seconds/60))+' min' + when latency_seconds < 86400 then convert(varchar,floor(latency_seconds/3600))+' hrs' + when latency_seconds >= 86400 then convert(varchar,floor(latency_seconds/86400))+' days' else '' end),' ')+''+convert(varchar,is_suspended)+'
'+ + isnull((case when log_send_queue_size < 1024 then convert(varchar,log_send_queue_size)+' kb' + when log_send_queue_size < 1024*1024 then convert(varchar,log_send_queue_size/1024)+' mb' + when log_send_queue_size < 1024*1024*1024 then convert(varchar,floor(log_send_queue_size/(1024*1024)))+' gb' + when log_send_queue_size >= 1024*1024*1024 then convert(varchar,floor(log_send_queue_size/(1024*1024*1024)))+' tb' + else '' end),' ')+'' + +isnull((case when redo_queue_size < 1024 then convert(varchar,redo_queue_size)+' kb' + when redo_queue_size < 1024*1024 then convert(varchar,redo_queue_size/1024)+' mb' + when redo_queue_size < 1024*1024*1024 then convert(varchar,floor(redo_queue_size/(1024*1024)))+' gb' + when redo_queue_size >= 1024*1024*1024 then convert(varchar,floor(redo_queue_size/(1024*1024*1024)))+' tb' + else '' end),' ')+''+convert(varchar,is_suspended)+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_ag_health,120)+'
' +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #disk_space tsu + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_disk_space = 0; + + select top 1 @_collection_time_disk_health = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_date_utc) from dbo.disk_space_all_servers ds - where ds.updated_date_utc >= dateadd(minute,-60,getutcdate()) - and ( ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct) - and ds.free_mb < (@disk_threshold_gb)*1024 - ) - or ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free % - ) - order by [used_pct] desc - ) - ,t_cte as ( - select '' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' as [table_row] - from tsu - where 1=1 - ) - --select * from t_cte; - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + order by updated_date_utc desc; + + set @_table_data = ''; + end set @_html_disk_health = '

'+@_table_headline+'
@ag_latency_minutes:'+convert(varchar,@ag_latency_minutes) @@ -808,74 +823,79 @@ BEGIN if not exists (select * from dbo.disk_space_all_servers) raiserror ('Data does not exist in dbo.disk_space_all_servers', 17, -1) with log; + -- Get temp table with alert data + if object_id('tempdb..#disk_space') is not null + drop table #disk_space; + select top 100000 + ds.sql_instance, ds.[host_name], ds.disk_volume, ds.capacity_mb, + ds.free_mb, + [state] = case when (ds.free_mb*100.0/ds.capacity_mb) < (100.0-@disk_critical_pct) then 'Critical' else 'Warning' end, + [used_pct] = 100.0-convert(numeric(20,2),ds.free_mb*100.0/ds.capacity_mb) + into #disk_space + from dbo.disk_space_all_servers ds + where ds.updated_date_utc >= dateadd(minute,-60,getutcdate()) + and ( ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct) + and ds.free_mb < (@disk_threshold_gb)*1024 + ) + or ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free % + ) + order by [used_pct] desc; + if @verbose > 1 begin - ;with t_cte as ( - select ds.sql_instance, ds.host_name, ds.disk_volume, ds.capacity_mb, - ds.free_mb, - [state] = case when (ds.free_mb*100.0/ds.capacity_mb) < (100.0-@disk_critical_pct) then 'Critical' else 'Warning' end, - [used_pct] = 100.0-convert(numeric(20,2),ds.free_mb*100.0/ds.capacity_mb) - from dbo.disk_space_all_servers ds - where ds.updated_date_utc >= dateadd(minute,-60,getutcdate()) - and ( ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct) - and ds.free_mb < (@disk_threshold_gb)*1024 - ) - or ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free % - ) - ) select [RunningQuery], t_cte.* - from t_cte + from #disk_space t_cte full outer join (select [RunningQuery] = 'Disk Space') rq on 1=1 - order by [used_pct] desc + order by [used_pct] desc; end - ;with tsu as ( - select top 100000 - ds.sql_instance, ds.[host_name], ds.disk_volume, ds.capacity_mb, - ds.free_mb, - [state] = case when (ds.free_mb*100.0/ds.capacity_mb) < (100.0-@disk_critical_pct) then 'Critical' else 'Warning' end, - [used_pct] = 100.0-convert(numeric(20,2),ds.free_mb*100.0/ds.capacity_mb) + -- Decide if alert data is present, or add empty info row + if exists (select * from #disk_space) + begin + ;with t_cte as ( + select '
'+sql_instance+''+[host_name]+''+disk_volume+''+(case when capacity_mb < 1024 then convert(varchar,capacity_mb)+' mb' + when capacity_mb < 1024*1024 then convert(varchar,floor(capacity_mb/1024))+' gb' + when capacity_mb >= 1024*1024 then convert(varchar,floor(capacity_mb/(1024*1024)))+' tb' + else 'xx' end)+''+(case when free_mb < 1024 then convert(varchar,free_mb)+' mb' + when free_mb < 1024*1024 then convert(varchar,floor(free_mb/1024))+' gb' + when free_mb >= 1024*1024 then convert(varchar,floor(free_mb/(1024*1024)))+' tb' + else 'xx' end)+''+[state]+''+convert(varchar,used_pct)+'
'+sql_instance+''+[host_name]+''+disk_volume+''+(case when capacity_mb < 1024 then convert(varchar,capacity_mb)+' mb' - when capacity_mb < 1024*1024 then convert(varchar,floor(capacity_mb/1024))+' gb' - when capacity_mb >= 1024*1024 then convert(varchar,floor(capacity_mb/(1024*1024)))+' tb' - else 'xx' end)+''+(case when free_mb < 1024 then convert(varchar,free_mb)+' mb' - when free_mb < 1024*1024 then convert(varchar,floor(free_mb/1024))+' gb' - when free_mb >= 1024*1024 then convert(varchar,floor(free_mb/(1024*1024)))+' tb' - else 'xx' end)+''+[state]+''+convert(varchar,used_pct)+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_disk_health,120)+'
' +'' set @_table_data = NULL; + -- Get temp table with alert data + if object_id('tempdb..#offline_servers') is not null + drop table #offline_servers; + select sql_instance, [host_name], + is_available, + is_linked_server_working = case when is_available = 0 then null else is_linked_server_working end, + [tsql jobs server] = collector_tsql_jobs_server, + [powershell jobs server] = collector_powershell_jobs_server, + [perfmon data server] = data_destination_sql_instance, + last_unavailability_time_utc + into #offline_servers + from dbo.instance_details id + where is_enabled = 1 + and (is_available = 0 or is_linked_server_working = 0); + if @verbose > 1 begin - ;with t_cte as ( - select sql_instance, [host_name], - is_available, - is_linked_server_working = case when is_available = 0 then null else is_linked_server_working end, - [tsql jobs server] = collector_tsql_jobs_server, - [powershell jobs server] = collector_powershell_jobs_server, - [perfmon data server] = data_destination_sql_instance, - last_unavailability_time_utc - from dbo.instance_details id - where is_enabled = 1 - and (is_available = 0 or is_linked_server_working = 0) - ) select [RunningQuery], t_cte.* - from t_cte + from #offline_servers t_cte full outer join (select [RunningQuery] = 'Offline Servers') rq on 1=1; end - ;with tsu as ( - select sql_instance, [host_name], - is_available, - is_linked_server_working = case when is_available = 0 then null else is_linked_server_working end, - [tsql jobs server] = collector_tsql_jobs_server, - [powershell jobs server] = collector_powershell_jobs_server, - [perfmon data server] = data_destination_sql_instance, - last_unavailability_time_utc - from dbo.instance_details id - where is_enabled = 1 - and (is_available = 0 or is_linked_server_working = 0) - ) - ,t_cte as ( - select '' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' as [table_row] - from tsu - where 1=1 - ) - --select * from t_cte; - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + -- Decide if alert data is present, or add empty info row + if exists (select * from #offline_servers) + begin + ;with t_cte as ( + select '' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #offline_servers + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_offline_servers = 0; + + set @_collection_time_offline_servers = DATEADD(mi, -2, GETDATE()); + + set @_table_data = ''; + end set @_html_offline_servers = '

'+@_table_headline+'
@disk_warning_pct:'+convert(varchar,@disk_warning_pct) @@ -912,59 +932,63 @@ BEGIN +N'
Report Time
'+sql_instance+''+[host_name]+''+convert(varchar,is_available)+''+isnull(convert(varchar,is_linked_server_working),'')+''+[tsql jobs server]+''+[powershell jobs server]+''+[perfmon data server]+''+isnull(convert(varchar,last_unavailability_time_utc,120),'')+'
'+sql_instance+''+[host_name]+''+convert(varchar,is_available)+''+isnull(convert(varchar,is_linked_server_working),'')+''+[tsql jobs server]+''+[powershell jobs server]+''+[perfmon data server]+''+isnull(convert(varchar,last_unavailability_time_utc,120),'')+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_offline_servers,120)+'
' +'' @@ -1000,90 +1024,89 @@ BEGIN if not exists (select * from dbo.sql_agent_jobs_all_servers) print 'Data does not exist in dbo.sql_agent_jobs_all_servers'; + -- Get temp table with alert data + if object_id('tempdb..#saj') is not null + drop table #saj; + select top 1000000 + [CollectionTimeUTC] = [UpdatedDateUTC], + [sql_instance], [JobName], + [Job-Delay-Minutes] = case when sj.Last_Successful_ExecutionTime is null then 10080 else datediff(minute, sj.Last_Successful_ExecutionTime, dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@buffer_time_minutes),getutcdate())) end, + [Last_RunTime], [Last_Run_Duration_Seconds], [Last_Run_Outcome], + [Successfull_Execution_ClockTime_Threshold_Minutes], + [Last_Successful_ExecutionTime] + into #saj + from dbo.sql_agent_jobs_all_servers sj + where 1=1 + and exists (select 1/0 from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1) + and sj.JobCategory = '(dba) SQLMonitor' + and sj.JobName like '(dba) %' + and sj.IsDisabled = 0 + and ( dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@buffer_time_minutes),getutcdate()) > sj.Last_Successful_ExecutionTime + or sj.Last_Successful_ExecutionTime is null + ) + order by [Last_Successful_ExecutionTime]; + if @verbose > 1 begin - ;with t_cte as ( - select top 10000000 - [CollectionTimeUTC] = [UpdatedDateUTC], - [sql_instance], [JobName], - [Job-Delay-Minutes] = case when sj.Last_Successful_ExecutionTime is null then 10080 else datediff(minute, sj.Last_Successful_ExecutionTime, dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@buffer_time_minutes),getutcdate())) end, - [Last_RunTime], [Last_Run_Duration_Seconds], [Last_Run_Outcome], - [Successfull_Execution_ClockTime_Threshold_Minutes], - [Last_Successful_ExecutionTime] - from dbo.sql_agent_jobs_all_servers sj - where 1=1 - and exists (select 1/0 from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1) - and sj.JobCategory = '(dba) SQLMonitor' - and sj.JobName like '(dba) %' - and sj.IsDisabled = 0 - and ( dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@buffer_time_minutes),getutcdate()) > sj.Last_Successful_ExecutionTime - or sj.Last_Successful_ExecutionTime is null - ) - order by [Last_Successful_ExecutionTime] - ) select [RunningQuery], t_cte.* - from t_cte + from #saj t_cte full outer join (select [RunningQuery] = 'SQLMonitor Jobs') rq on 1=1; end - ;with tsu as ( - select top 1000000 - [CollectionTimeUTC] = [UpdatedDateUTC], - [sql_instance], [JobName], - [Job-Delay-Minutes] = case when sj.Last_Successful_ExecutionTime is null then 10080 else datediff(minute, sj.Last_Successful_ExecutionTime, dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@buffer_time_minutes),getutcdate())) end, - [Last_RunTime], [Last_Run_Duration_Seconds], [Last_Run_Outcome], - [Successfull_Execution_ClockTime_Threshold_Minutes], - [Last_Successful_ExecutionTime] - from dbo.sql_agent_jobs_all_servers sj - where 1=1 - and exists (select 1/0 from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1) - and sj.JobCategory = '(dba) SQLMonitor' - and sj.JobName like '(dba) %' - and sj.IsDisabled = 0 - and ( dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@buffer_time_minutes),getutcdate()) > sj.Last_Successful_ExecutionTime - or sj.Last_Successful_ExecutionTime is null - ) - order by [Last_Successful_ExecutionTime] - ) - ,t_cte as ( - select '' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' as [table_row] - from tsu - where 1=1 - ) - --select * from t_cte; - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + -- Decide if alert data is present, or add empty info row + if exists (select * from #saj) + begin + ;with t_cte as ( + select '' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #saj + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_sqlmonitor_jobs = 0; + + select top 1 @_collection_time_sqlmonitor_jobs = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), UpdatedDateUTC) + from dbo.sql_agent_jobs_all_servers saj + order by UpdatedDateUTC desc; + + set @_table_data = ''; + end set @_html_sqlmonitor_jobs = '

'+@_table_headline+'
select * from dbo.instance_details where is_enabled = 1 and is_available = 0
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), CollectionTimeUTC),120)+''+sql_instance+''+JobName+'' - +isnull((case when [Job-Delay-Minutes] < 60 then convert(varchar,floor([Job-Delay-Minutes]))+' min' - when [Job-Delay-Minutes] < 60*24 then convert(varchar,floor([Job-Delay-Minutes]/60))+' hrs' - when [Job-Delay-Minutes] >= 60*24 then convert(varchar,floor([Job-Delay-Minutes]/(60*24)))+' days' - else convert(varchar,[Job-Delay-Minutes]) end),'')+''+convert(varchar,Last_RunTime,120)+''+isnull((case when Last_Run_Duration_Seconds < 60 then convert(varchar,floor(Last_Run_Duration_Seconds))+' sec' - when Last_Run_Duration_Seconds < 3600 then convert(varchar,floor(Last_Run_Duration_Seconds/60))+' min' - when Last_Run_Duration_Seconds < 86400 then convert(varchar,floor(Last_Run_Duration_Seconds/3600))+' hrs' - when Last_Run_Duration_Seconds >= 86400 then convert(varchar,floor(Last_Run_Duration_Seconds/86400))+' days' - else convert(varchar,Last_Run_Duration_Seconds) end),'')+''+Last_Run_Outcome+''+isnull((case when Successfull_Execution_ClockTime_Threshold_Minutes < 60 then convert(varchar,floor(Successfull_Execution_ClockTime_Threshold_Minutes))+' min' - when Successfull_Execution_ClockTime_Threshold_Minutes < 1440 then convert(varchar,floor(Successfull_Execution_ClockTime_Threshold_Minutes/60))+' hrs' - when Successfull_Execution_ClockTime_Threshold_Minutes >= 86400 then convert(varchar,floor(Successfull_Execution_ClockTime_Threshold_Minutes/1440))+' days' - else '' end),0)+''+convert(varchar,Last_Successful_ExecutionTime,120)+'
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), CollectionTimeUTC),120)+''+sql_instance+''+JobName+'' + +isnull((case when [Job-Delay-Minutes] < 60 then convert(varchar,floor([Job-Delay-Minutes]))+' min' + when [Job-Delay-Minutes] < 60*24 then convert(varchar,floor([Job-Delay-Minutes]/60))+' hrs' + when [Job-Delay-Minutes] >= 60*24 then convert(varchar,floor([Job-Delay-Minutes]/(60*24)))+' days' + else convert(varchar,[Job-Delay-Minutes]) end),'')+''+convert(varchar,Last_RunTime,120)+''+isnull((case when Last_Run_Duration_Seconds < 60 then convert(varchar,floor(Last_Run_Duration_Seconds))+' sec' + when Last_Run_Duration_Seconds < 3600 then convert(varchar,floor(Last_Run_Duration_Seconds/60))+' min' + when Last_Run_Duration_Seconds < 86400 then convert(varchar,floor(Last_Run_Duration_Seconds/3600))+' hrs' + when Last_Run_Duration_Seconds >= 86400 then convert(varchar,floor(Last_Run_Duration_Seconds/86400))+' days' + else convert(varchar,Last_Run_Duration_Seconds) end),'')+''+Last_Run_Outcome+''+isnull((case when Successfull_Execution_ClockTime_Threshold_Minutes < 60 then convert(varchar,floor(Successfull_Execution_ClockTime_Threshold_Minutes))+' min' + when Successfull_Execution_ClockTime_Threshold_Minutes < 1440 then convert(varchar,floor(Successfull_Execution_ClockTime_Threshold_Minutes/60))+' hrs' + when Successfull_Execution_ClockTime_Threshold_Minutes >= 86400 then convert(varchar,floor(Successfull_Execution_ClockTime_Threshold_Minutes/1440))+' days' + else '' end),0)+''+convert(varchar,Last_Successful_ExecutionTime,120)+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_sqlmonitor_jobs,120)+'
' +'' - --+'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' - +'' as [table_row] - from t_issues bi - where 1=1 - ) - --select * from t_cte; - select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) - from t_cte; + select [sql_instance], [database_name], [recovery_model], + [full_latency_days], [diff_latency_hours], [tlog_latency_minutes], + [full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], + [full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], + [tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], + [full_backup_file], [diff_backup_file], [tlog_backup_file] + into #backups + from t_latency as l + where 1=1 + AND ( (full_latency_days is null or full_latency_days >= @full_threshold_days) + OR (diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours) + OR (tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes) + ); + + if @verbose > 1 + begin + select [RunningQuery], t_cte.* + from #backups t_cte + full outer join (select [RunningQuery] = 'Backup History') rq + on 1=1; + end + + -- Decide if alert data is present, or add empty info row + if exists (select * from #backups) + begin + ;with t_cte as ( + select '' + --+'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #backups bi + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_backup_history = 0; + + select top 1 @_collection_time_backup_history = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), collection_time_utc) + from dbo.backups_all_servers bas + order by collection_time_utc desc; + + set @_table_data = ''; + end set @_html_backup_history = '

'+@_table_headline+'
dbo.sql_agent_jobs_all_servers || @buffer_time_minutes:'+convert(varchar,@buffer_time_minutes) @@ -1120,83 +1143,9 @@ BEGIN if not exists (select * from dbo.backups_all_servers) raiserror ('Data does not exist in dbo.backups_all_servers', 17, -1) with log; - if @verbose > 1 - begin - ;with t_backups as ( - select [collection_time_utc], [sql_instance], [database_name], [backup_type], [log_backups_count], [backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description] - from dbo.backups_all_servers bas - ) - ,t_pivot as ( - select [sql_instance], [database_name] - ,[recovery_model] = max([recovery_model]) - ,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end) - ,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end) - ,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end) - ,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end) - ,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end) - ,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end) - ,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end) - ,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end) - ,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end) - ,[log_backups_count] = max([log_backups_count]) - ,[database_creation_date_utc] = max([database_creation_date_utc]) - ,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end) - ,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end) - ,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end) - from t_backups bkp - where 1=1 - group by [sql_instance], [database_name] - ) - ,t_latency as ( - select [sql_instance], [database_name], [recovery_model], - [full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10 - else datediff(day,[full_backup_time_utc],getutcdate()) - end, - [diff_latency_hours] = case when [diff_backup_time_utc] is null - then case when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7) - then @full_threshold_days * 24 - when (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours - then ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 ) - else null - end - else datediff(hour,[diff_backup_time_utc],getutcdate()) - end, - [tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null - when recovery_model <> 'SIMPLE' - then case when [tlog_backup_time_utc] is null then @full_threshold_days * 1440 - when [tlog_backup_time_utc] is not null - then datediff(minute,[tlog_backup_time_utc],getutcdate()) - else null - end - else null - end, - [full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], - [full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb], - [tlog_compressed_size_mb], [log_backups_count], - [database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file] - from t_pivot as bkp - where 1=1 - ) - ,t_cte as ( - select [sql_instance], [database_name], [recovery_model], - [full_latency_days], [diff_latency_hours], [tlog_latency_minutes], - [full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], - [full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], - [tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], - [full_backup_file], [diff_backup_file], [tlog_backup_file] - from t_latency as l - where 1=1 - AND ( (full_latency_days is null or full_latency_days >= @full_threshold_days) - OR (diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours) - OR (tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes) - ) - ) - select [RunningQuery], t_cte.* - from t_cte - full outer join (select [RunningQuery] = 'Backup History') rq - on 1=1; - end - + -- Get temp table with alert data + if object_id('tempdb..#backups') is not null + drop table #backups; ;with t_backups as ( select [collection_time_utc], [sql_instance], [database_name], [backup_type], [log_backups_count], [backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description] from dbo.backups_all_servers bas @@ -1225,26 +1174,26 @@ BEGIN ,t_latency as ( select [sql_instance], [database_name], [recovery_model], [full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10 - else datediff(day,[full_backup_time_utc],getutcdate()) - end, + else datediff(day,[full_backup_time_utc],getutcdate()) + end, [diff_latency_hours] = case when [diff_backup_time_utc] is null - then case when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7) - then @full_threshold_days * 24 - when (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours - then ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 ) - else null - end - else datediff(hour,[diff_backup_time_utc],getutcdate()) - end, + then case when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7) + then @full_threshold_days * 24 + when (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours + then ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 ) + else null + end + else datediff(hour,[diff_backup_time_utc],getutcdate()) + end, [tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null - when recovery_model <> 'SIMPLE' - then case when [tlog_backup_time_utc] is null then @full_threshold_days * 1440 - when [tlog_backup_time_utc] is not null - then datediff(minute,[tlog_backup_time_utc],getutcdate()) - else null - end - else null - end, + when recovery_model <> 'SIMPLE' + then case when [tlog_backup_time_utc] is null then @full_threshold_days * 1440 + when [tlog_backup_time_utc] is not null + then datediff(minute,[tlog_backup_time_utc],getutcdate()) + else null + end + else null + end, [full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], [full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], @@ -1252,59 +1201,82 @@ BEGIN from t_pivot as bkp where 1=1 ) - ,t_issues as ( - select [sql_instance], [database_name], [recovery_model], - [full_latency_days], [diff_latency_hours], [tlog_latency_minutes], - [full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], - [full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], - [tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], - [full_backup_file], [diff_backup_file], [tlog_backup_file] - from t_latency as l - where 1=1 - AND ( (full_latency_days is null or full_latency_days >= @full_threshold_days) - OR (diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours) - OR (tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes) - ) - ) - ,t_cte as ( - select '
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), CollectionTimeUTC),120)+''+sql_instance+''+[database_name]+''+isnull(recovery_model,'')+''+isnull(convert(varchar,full_latency_days),'')+' days'+'' - +isnull((case when diff_latency_hours < 24 then convert(varchar,floor(diff_latency_hours))+' hrs' - when diff_latency_hours >= 24 then convert(varchar,convert(numeric(20,2),diff_latency_hours/24))+' days' - else convert(varchar,diff_latency_hours) end),'')+'' - +isnull((case when tlog_latency_minutes < 60 then convert(varchar,floor(tlog_latency_minutes))+' min' - when tlog_latency_minutes < 60*24 then convert(varchar,convert(numeric(20,2),tlog_latency_minutes/60))+' hrs' - when tlog_latency_minutes >= 60*24 then convert(varchar,convert(numeric(20,2),tlog_latency_minutes/(60*24)))+' days' - else convert(varchar,tlog_latency_minutes) end),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), full_backup_time_utc),120),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), diff_backup_time_utc),120),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), tlog_backup_time_utc),120),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), database_creation_date_utc),120),'')+'
'+convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), CollectionTimeUTC),120)+''+sql_instance+''+[database_name]+''+isnull(recovery_model,'')+''+isnull(convert(varchar,full_latency_days),'')+' days'+'' + +isnull((case when diff_latency_hours < 24 then convert(varchar,floor(diff_latency_hours))+' hrs' + when diff_latency_hours >= 24 then convert(varchar,convert(numeric(20,2),diff_latency_hours/24))+' days' + else convert(varchar,diff_latency_hours) end),'')+'' + +isnull((case when tlog_latency_minutes < 60 then convert(varchar,floor(tlog_latency_minutes))+' min' + when tlog_latency_minutes < 60*24 then convert(varchar,convert(numeric(20,2),tlog_latency_minutes/60))+' hrs' + when tlog_latency_minutes >= 60*24 then convert(varchar,convert(numeric(20,2),tlog_latency_minutes/(60*24)))+' days' + else convert(varchar,tlog_latency_minutes) end),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), full_backup_time_utc),120),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), diff_backup_time_utc),120),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), tlog_backup_time_utc),120),'')+''+isnull(convert(varchar,DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), database_creation_date_utc),120),'')+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_backup_history,120)+'
' +''; + set @_table_data = NULL; + + if not exists (select * from dbo.alert_history_all_servers) + raiserror ('Data does not exist in dbo.alert_history_all_servers', 17, -1) with log; + + -- Get temp table with alert data + if object_id('tempdb..#alert_history') is not null + drop table #alert_history; + ;with t_aggregated_messages as + ( + select a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), last_occurred = max(a.collection_time_utc), error_message = coalesce(max(m.text), max(a.error_message)) + from dbo.alert_history_all_servers a + left join sys.messages m + on m.language_id = 1033 and m.message_id = a.error_number + where 1 = 1 + and a.collection_time_utc >= dateadd(hour,-@alert_history_hours,GETUTCDATE()) + and (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%' + then 0 + when a.error_number = 1204 then 0 + when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. ' + then 0 + when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%' + then 0 + when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%' + then 0 + when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. ' + then 0 + when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%' + then 0 + when a.error_number = 5084 and error_severity = 10 and a.error_message like 'Setting database option MULTI_USER to ON%' + then 0 + when a.error_number = 5084 and error_severity = 10 and a.error_message like 'Setting database option SINGLE_USER to ON%' + then 0 + when a.error_number = 14151 and error_severity = 18 and a.error_message like '%Named Pipes Provider: Could not open a connection to SQL Server%' + then 0 + when a.error_number = 17828 and error_severity = 20 + then 0 + when a.error_number in (17832, 17836) + then 0 + else 1 + end + ) = 1 + and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance) + group by a.sql_instance, a.error_number, a.error_severity + ) + ,t_unique_errors as ( + select error_number, error_severity, error_message + from t_aggregated_messages + group by error_number, error_severity, error_message + ) + select error_number, error_severity, error_message, + [sql_instance | occurrences | last_occurred] = + stuff(( SELECT ', ' + cc.[sql_instance | occurrences | last_occurred] + FROM t_aggregated_messages am + outer apply (select [last_occurred_localtime] = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), last_occurred) ) lt + outer apply (select [sql_instance | occurrences | last_occurred] = '('+sql_instance+' | '+convert(varchar(10),occurrences)+' | '+convert(varchar,lt.last_occurred_localtime,120)+')') cc + where am.error_number = ue.error_number and am.error_severity = ue.error_severity + order by occurrences desc + FOR XML PATH('') + ), 1, 2, '') + into #alert_history + from t_unique_errors ue; + + if @verbose > 1 + begin + select [RunningQuery], t_cte.* + from #alert_history t_cte + full outer join (select [RunningQuery] = 'Alert History') rq + on 1=1 + where 1=1; + end + + -- Decide if alert data is present, or add empty info row + if exists (select * from #alert_history) + begin + ;with t_cte as ( + select '' + +'' + +'' + +'' + +'' + +'' as [table_row] + from #alert_history ah + where 1=1 + ) + --select * from t_cte; + select @_table_data = coalesce(@_table_data+' '+[table_row],[table_row]) + from t_cte; + end + else + begin + if @hide_row_if_no_data = 1 + set @collect_alert_history = 0; + + select top 1 @_collection_time_alert_history = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), collection_time_utc) + from dbo.alert_history_all_servers ds + order by collection_time_utc desc; + + set @_table_data = ''; + end + + set @_html_alert_history = '

'+@_table_headline+'
dbo.backups_all_servers || @full_threshold_days:'+convert(varchar,@full_threshold_days) @@ -1323,6 +1295,134 @@ BEGIN end end -- 'Backup History' + + if(@collect_alert_history = 1) -- 'Alert History' + begin + if @verbose > 0 + begin + print @_line; + print @_line; + print 'Set @_html_alert_history variable..'; + print @_tab+@_line; + end + --error_number, error_severity, error_message, [sql_instance | occurrences | last_occurred] + set @_table_headline = N'

All Servers - Alert History - Require ATTENTION

'; + set @_table_header = N'
Error Number Severity Error Message (Generic) [sql_instance | occurrences | last_occurred]
'+convert(varchar(20),ah.[error_number])+''+convert(varchar(20),ah.[error_severity])+''+ah.[error_message]+''+ah.[sql_instance | occurrences | last_occurred]+'
No alert qualifying data found. Latest collection @ '+convert(varchar,@_collection_time_alert_history,120)+'
' + +'' + +''+@_table_header+''+isnull(@_table_data,'')+'
@alert_history_hours:'+convert(varchar,@alert_history_hours)+'
'; + + if @verbose > 0 + begin + print @_tab+'@_table_header => '+@_crlf+@_table_header; + print @_tab+@_line; + print @_tab+'@_table_data => '+@_crlf+ISNULL(@_table_data,''); + print @_tab+@_line; + print @_tab+'@_html_alert_history => '+@_crlf+ISNULL(@_html_alert_history,''); + end + end -- 'Alert History' + set @mail_subject = @mail_subject+' - '+convert(varchar,@_collection_time,120); set @_mail_body_html = '' @@ -1337,6 +1437,7 @@ BEGIN +(case when @collect_log_space = 1 then N'

'+@_html_log_space_health+'

' else '' end) +(case when @collect_ag_latency = 1 then N'

'+@_html_ag_health+'

' else '' end) +(case when @collect_disk_space = 1 then N'

'+@_html_disk_health+'

' else '' end) + +(case when @collect_alert_history = 1 then N'

'+@_html_alert_history+'

' else '' end) +(case when @collect_offline_servers = 1 then N'

'+@_html_offline_servers+'

' else '' end) +(case when @collect_sqlmonitor_jobs = 1 then N'

'+@_html_sqlmonitor_jobs+'

' else '' end) +(case when @collect_backup_history = 1 then N'

'+@_html_backup_history+'

' else '' end) diff --git a/DDLs/SCH-usp_GetAllServerInfo.sql b/DDLs/SCH-usp_GetAllServerInfo.sql index 67a7dce..ce53193 100644 --- a/DDLs/SCH-usp_GetAllServerInfo.sql +++ b/DDLs/SCH-usp_GetAllServerInfo.sql @@ -79,23 +79,22 @@ BEGIN DECLARE @_sql NVARCHAR(max); DECLARE @_isLocalHost bit = 0; create table #server_details ( - srv_name varchar(125), at_server_name varchar(125), machine_name varchar(125), server_name varchar(125), - ip varchar(30), domain varchar(125), host_name varchar(125), fqdn varchar(255), host_distribution varchar(200), - processor_name varchar(200), product_version varchar(30), edition varchar(50), sqlserver_start_time_utc datetime2, - os_cpu decimal(20,2), sql_cpu decimal(20,2), pcnt_kernel_mode decimal(20,2), page_faults_kb decimal(20,2), - blocked_counts int, blocked_duration_max_seconds bigint, total_physical_memory_kb bigint, - available_physical_memory_kb bigint, system_high_memory_signal_state varchar(20), - physical_memory_in_use_kb decimal(20,2), memory_grants_pending int, connection_count int, + srv_name varchar(125), at_server_name varchar(125), machine_name varchar(125), server_name varchar(125), + ip varchar(30), domain varchar(125), host_name varchar(125), fqdn varchar(255), host_distribution varchar(200), + processor_name varchar(200), product_version varchar(30), edition varchar(50), sqlserver_start_time_utc datetime2, + os_cpu decimal(20,2), sql_cpu decimal(20,2), pcnt_kernel_mode decimal(20,2), page_faults_kb decimal(20,2), + blocked_counts int, blocked_duration_max_seconds bigint, total_physical_memory_kb bigint, + available_physical_memory_kb bigint, system_high_memory_signal_state varchar(20), + physical_memory_in_use_kb decimal(20,2), memory_grants_pending int, connection_count int, active_requests_count int, waits_per_core_per_minute decimal(20,2), avg_disk_wait_ms decimal(20,2), [avg_disk_latency_ms] int, - os_start_time_utc datetime2, cpu_count smallint, scheduler_count smallint, major_version_number smallint, - minor_version_number smallint, max_server_memory_mb int, page_life_expectancy int, memory_consumers int, + os_start_time_utc datetime2, cpu_count smallint, scheduler_count smallint, major_version_number smallint, + minor_version_number smallint, max_server_memory_mb int, page_life_expectancy int, memory_consumers int, target_server_memory_kb bigint, total_server_memory_kb bigint, performance_counters__latency_minutes int, xevent_metrics__latency_minutes int, WhoIsActive__latency_minutes int, - os_task_list__latency_minutes int, disk_space__latency_minutes int, file_io_stats__latency_minutes int, - sql_agent_job_stats__latency_minutes int, memory_clerks__latency_minutes int, wait_stats__latency_minutes int, - BlitzIndex__latency_days int, BlitzIndex_Mode0__latency_days int, BlitzIndex_Mode1__latency_days int, - BlitzIndex_Mode4__latency_days int + disk_space__latency_minutes int, file_io_stats__latency_minutes int, sql_agent_job_stats__latency_minutes int, + memory_clerks__latency_minutes int, wait_stats__latency_minutes int, BlitzIndex__latency_days int, + BlitzIndex_Mode0__latency_days int, BlitzIndex_Mode1__latency_days int, BlitzIndex_Mode4__latency_days int ); declare @_srv_name nvarchar (125); @@ -146,7 +145,6 @@ BEGIN declare @_file_io_stats__latency_minutes int; declare @_sql_agent_job_stats__latency_minutes int; declare @_memory_clerks__latency_minutes int; - declare @_os_task_list__latency_minutes int; declare @_performance_counters__latency_minutes int; declare @_xevent_metrics__latency_minutes int; declare @_xevent_metrics_queries__latency_minutes int; @@ -321,7 +319,6 @@ BEGIN set @_file_io_stats__latency_minutes = NULL; set @_sql_agent_job_stats__latency_minutes = NULL; set @_memory_clerks__latency_minutes = NULL; - set @_os_task_list__latency_minutes = NULL; set @_performance_counters__latency_minutes = NULL; set @_xevent_metrics__latency_minutes = NULL; set @_xevent_metrics_queries__latency_minutes = NULL; @@ -2344,57 +2341,6 @@ on 1=1"; end - -- [os_task_list__latency_minutes] => Create SQL Statement to Execute - if @_linked_server_failed = 0 and ( @output is null or exists (select * from @_tbl_output_columns where column_name = 'os_task_list__latency_minutes') ) - begin - delete from @_result; - set @_sql = " -"+(case when @enable_lock_timeout = 1 then '' else '--' end)+"SET LOCK_TIMEOUT 60000; -SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; -select [latency_minutes] = coalesce(latency_minutes,dummy_latency_minutes) -from -( select top 1 [latency_minutes] = datediff(minute,collection_time_utc,getutcdate()) from dbo.vw_os_task_list - where 1=1 - and collection_time_utc >= dateadd(minute,-120,getutcdate()) - --and [host_name] = CONVERT(varchar(125),COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),SERVERPROPERTY('ServerName'))) - order by collection_time_utc desc -) od -full outer join (select [dummy_latency_minutes] = 10080) dmy -- 7 days -on 1=1"; - -- Decorate for remote query if LinkedServer - if @_isLocalHost = 0 - set @_sql = 'select * from openquery(' + QUOTENAME(@_srv_name) + ', "'+ @_sql + '")'; - - begin try - insert @_result (col_int) - exec (@_sql); - - -- set @_ip - select @_os_task_list__latency_minutes = col_int from @_result; - end try - begin catch - select @_errorNumber = Error_Number() - ,@_errorSeverity = Error_Severity() - ,@_errorState = Error_State() - ,@_errorLine = Error_Line() - ,@_errorMessage = Error_Message(); - - insert [dbo].[sma_errorlog] - ([collection_time], [function_name], [function_call_arguments], [server], [error], [remark], [executed_by], [executor_program_name]) - select [collection_time] = @_start_time, [function_name] = 'usp_GetAllServerInfo', - [function_call_arguments] = 'os_task_list__latency_minutes', [server] = @_srv_name, [error] = @_errorMessage, - [remark] = null, [executed_by] = SUSER_NAME(), [executor_program_name] = @_caller_program; - - set @_errorMessage = 'Error Details => Severity: '+CONVERT(varchar(125),isnull(@_errorSeverity,''))+ - '. State: '+CONVERT(varchar(125),isnull(@_errorState,'')) + - '. Error Line: '+CONVERT(varchar(125),isnull(@_errorLine,'')) + - '. Error Message::: '+ @_errorMessage; - - print @_crlf+@_long_star_line+@_crlf+'Error occurred while executing below query on ['+@_srv_name+'].'+@_crlf+@_errorMessage+@_crlf+' '+@_sql+@_long_star_line+@_crlf; - end catch - end - - -- [disk_space__latency_minutes] => Create SQL Statement to Execute if @_linked_server_failed = 0 and ( @output is null or exists (select * from @_tbl_output_columns where column_name = 'disk_space__latency_minutes') ) begin @@ -2842,10 +2788,10 @@ on 1=1"; [connection_count], [active_requests_count], [waits_per_core_per_minute], [avg_disk_wait_ms], [avg_disk_latency_ms], [os_start_time_utc], [cpu_count], [scheduler_count], [major_version_number], [minor_version_number], [max_server_memory_mb], [page_life_expectancy], [memory_consumers], [target_server_memory_kb], [total_server_memory_kb], [performance_counters__latency_minutes], - [xevent_metrics__latency_minutes], [WhoIsActive__latency_minutes], [os_task_list__latency_minutes], - [disk_space__latency_minutes], [file_io_stats__latency_minutes], [sql_agent_job_stats__latency_minutes], - [memory_clerks__latency_minutes], [wait_stats__latency_minutes], [BlitzIndex__latency_days], - [BlitzIndex_Mode0__latency_days], [BlitzIndex_Mode1__latency_days], [BlitzIndex_Mode4__latency_days] + [xevent_metrics__latency_minutes], [WhoIsActive__latency_minutes], [disk_space__latency_minutes], + [file_io_stats__latency_minutes], [sql_agent_job_stats__latency_minutes], [memory_clerks__latency_minutes], + [wait_stats__latency_minutes], [BlitzIndex__latency_days], [BlitzIndex_Mode0__latency_days], + [BlitzIndex_Mode1__latency_days], [BlitzIndex_Mode4__latency_days] ) select [srv_name] = @_srv_name ,[@@servername] = @_at_server_name @@ -2889,7 +2835,6 @@ on 1=1"; ,[performance_counters__latency_minutes] = @_performance_counters__latency_minutes ,[xevent_metrics__latency_minutes] = @_xevent_metrics__latency_minutes ,[WhoIsActive__latency_minutes] = @_WhoIsActive__latency_minutes - ,[os_task_list__latency_minutes] = @_os_task_list__latency_minutes ,[disk_space__latency_minutes] = @_disk_space__latency_minutes ,[file_io_stats__latency_minutes] = @_file_io_stats__latency_minutes ,[sql_agent_job_stats__latency_minutes] = @_sql_agent_job_stats__latency_minutes diff --git a/DDLs/SCH-usp_collect_performance_metrics.sql b/DDLs/SCH-usp_collect_performance_metrics.sql index 8256272..7ffec29 100644 --- a/DDLs/SCH-usp_collect_performance_metrics.sql +++ b/DDLs/SCH-usp_collect_performance_metrics.sql @@ -21,8 +21,9 @@ alter procedure [dbo].[usp_collect_performance_metrics] as begin /* Created By: Ajay Dwivedi (https://ajaydwivedi.com/go/sqlmonitor) - Version: 1.0 - Modification: 2025-Jan-30 - Integrate in SQLMonitor + Version: 2026-Jan-31 + Modification: 2026-Jan-31 - Cleanup RingBuffer Code + 2025-Jan-30 - Integrate in SQLMonitor exec dbo.[usp_collect_performance_metrics] @verbose = 2; exec dbo.[usp_collect_performance_metrics] @metrics = 'dm_os_sys_memory'; @@ -33,6 +34,8 @@ begin exec dbo.[usp_collect_performance_metrics] @metrics = 'dm_os_memory_clerks'; exec dbo.[usp_collect_performance_metrics] @metrics = 'dm_os_performance_counters_deprecated_features'; + MS Docs -> https://learn.microsoft.com/en-us/sql/relational-databases/performance-monitor/use-sql-server-objects?view=sql-server-ver17#SQLServerPOs + PERF_COUNTER_RAWCOUNT | Decimal | 65536 -> Raw counter value that does not require calculations, and represents one sample. -> Could not find any records for this type in sys.dm_os_performance_counters @@ -206,14 +209,11 @@ begin from ( SELECT top 1 [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), - [system_cpu_utilization] = CASE WHEN system_cpu_utilization_post_sp2 IS NOT NULL THEN system_cpu_utilization_post_sp2 ELSE system_cpu_utilization_pre_sp2 END, - [sql_cpu_utilization] = CASE WHEN sql_cpu_utilization_post_sp2 IS NOT NULL THEN sql_cpu_utilization_post_sp2 ELSE sql_cpu_utilization_pre_sp2 END + [system_cpu_utilization], [sql_cpu_utilization] FROM ( SELECT record.value('(Record/@id)[1]', 'int') AS record_id, DATEADD (ms, -1 * (ts_now - [timestamp]), SYSDATETIME()) AS collection_time, - 100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu_utilization_post_sp2, - record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu_utilization_post_sp2, - 100-record.value('(Record/SchedluerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu_utilization_pre_sp2, - record.value('(Record/SchedluerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu_utilization_pre_sp2 + 100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu_utilization, + record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu_utilization FROM ( SELECT timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now FROM sys.dm_os_ring_buffers cross apply sys.dm_os_sys_info WHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR' diff --git a/DDLs/SCH-usp_create_agent_alerts.sql b/DDLs/SCH-usp_create_agent_alerts.sql index 5b69b61..5d70cd7 100644 --- a/DDLs/SCH-usp_create_agent_alerts.sql +++ b/DDLs/SCH-usp_create_agent_alerts.sql @@ -15,7 +15,8 @@ IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'u GO ALTER PROCEDURE dbo.usp_create_agent_alerts -( @verbose tinyint = 0, /* 0 - no messages, 1 - debug messages, 2 = debug messages + table results */ +( @drop_create_alert bit = 0, /* When enabled, drop the alert, and recreate */ + @verbose tinyint = 0, /* 0 - no messages, 1 - debug messages, 2 = debug messages + table results */ @alert_operator_name varchar(255) = null ) AS @@ -23,9 +24,11 @@ BEGIN /* https://learn.microsoft.com/en-us/sql/ssms/agent/use-tokens-in-job-steps?view=sql-server-ver16 - Version: 1.0.0 Pre-requisites: dbo.alert_categories, dbo.alert_history, dbo.usp_capture_alert_messages, job [(dba) Capture-AlertMessages] - Date: 2024-05-23 - Updated to include Sev 19-25 + + Version -> 2026-01-31 + 2026-01-31 - #3 - Infra to Track Server and Database Configuration Changes + 2024-05-23 - Updated to include Sev 19-25 EXEC dbo.usp_create_agent_alerts */ @@ -72,7 +75,15 @@ BEGIN WHILE @@fetch_status = 0 BEGIN - IF NOT EXISTS ( SELECT 1/0 FROM msdb.dbo.sysalerts WHERE name = @c_alert_name ) + IF ( @drop_create_alert = 1 + OR EXISTS ( SELECT 1/0 FROM msdb.dbo.sysalerts WHERE name = @c_alert_name + AND job_id = '00000000-0000-0000-0000-000000000000' ) + ) + BEGIN + EXECUTE msdb.dbo.sp_delete_alert @name = @c_alert_name; + END + + IF NOT EXISTS ( SELECT 1/0 FROM msdb.dbo.sysalerts WHERE name = @c_alert_name ) BEGIN EXECUTE msdb.dbo.sp_add_alert @name = @c_alert_name, @message_id = @c_alert_error_number, @severity = @c_error_severity, @enabled = 1, @delay_between_responses = 0, @include_event_description_in = 1, @job_name = N'(dba) Capture-AlertMessages'; diff --git a/DDLs/SCH-usp_enable_page_compression.sql b/DDLs/SCH-usp_enable_page_compression.sql index 7a9f19d..1039720 100644 --- a/DDLs/SCH-usp_enable_page_compression.sql +++ b/DDLs/SCH-usp_enable_page_compression.sql @@ -45,11 +45,6 @@ BEGIN select table_name, index_name from (values ('dbo.performance_counters',NULL), ('dbo.performance_counters','nci_counter_collection_time_utc'), - ('dbo.os_task_list',NULL), - ('dbo.os_task_list','nci_cpu_time_seconds'), - ('dbo.os_task_list','nci_memory_kb'), - ('dbo.os_task_list','nci_user_name'), - ('dbo.os_task_list','nci_window_title'), ('dbo.wait_stats',NULL), ('dbo.xevent_metrics', NULL), ('dbo.xevent_metrics','uq_xevent_metrics'), diff --git a/DDLs/SCH-usp_wrapper_GetAllServerCollectedData.sql b/DDLs/SCH-usp_wrapper_GetAllServerCollectedData.sql index d67d914..9df30e3 100644 --- a/DDLs/SCH-usp_wrapper_GetAllServerCollectedData.sql +++ b/DDLs/SCH-usp_wrapper_GetAllServerCollectedData.sql @@ -29,8 +29,9 @@ AS BEGIN /* - Version: 2024-02-10 - Date: 2024-02-10 - #26 Track Status of SQLAgent Service + Version: 2026-01-31 + Date: 2026-01-31 - #3 Infra to Track Server and Database Configuration Changes + 2024-02-10 - #26 Track Status of SQLAgent Service 2024-01-08 - Backup History 2023-10-17 - Add Latency Dashboard for AG 2023-08-30 - Adding @schedule_minutes parameter @@ -69,6 +70,17 @@ BEGIN DECLARE @recipients varchar(500); /* Folks who receive the failure mail */ DECLARE @send_error_mail bit; /* Send mail on failure */ + DECLARE @_parallelize_collection bit = 0; + DECLARE @_parallel_threads int = 1; + DECLARE @_thread_counter int = 1; + DECLARE @_parallel_job_command nvarchar(max); + DECLARE @_parallel_job_name nvarchar(255); + DECLARE @_parallel_job_id BINARY(16); + DECLARE @_parallel_job_step_name nvarchar(125); + DECLARE @_parallel_job_database nvarchar(125) = DB_NAME(); + DECLARE @_tbl_parallel_jobs table (job_name nvarchar(125)); + DECLARE @_is_job_running bit = 0; + set @_caller_program = case when HOST_NAME() like '(dba) Get-AllServerCollectedData%' then HOST_NAME() else PROGRAM_NAME() @@ -84,7 +96,7 @@ BEGIN IF @step_name NOT IN ('dbo.sql_agent_jobs_all_servers','dbo.disk_space_all_servers','dbo.log_space_consumers_all_servers', 'dbo.tempdb_space_usage_all_servers','dbo.ag_health_state_all_servers','dbo.backups_all_servers', - 'dbo.services_all_servers') + 'dbo.services_all_servers','dbo.alert_history_all_servers') THROW 50001, '''step_name'' Parameter value is invalid.', 1; -- Variables for Try/Catch Block @@ -247,6 +259,200 @@ else EXEC sp_executesql @_sql, @_params, @verbose, @truncate_table, @has_staging_table, @schedule_minutes; END + IF @step_name = 'dbo.alert_history_all_servers' + BEGIN + SET @_parallelize_collection = 0; + SET @_parallel_threads = 1; + SET @_thread_counter = 1; + SET @_parallel_job_command = NULL; + SET @_parallel_job_name = NULL; + SET @_parallel_job_id = NULL; + SET @_parallel_job_step_name = NULL; + SET @_parallel_job_database = DB_NAME(); + DELETE FROM @_tbl_parallel_jobs; + SET @_is_job_running = 0; + + -- Fetch params related to parallelization of jobs + select @_parallelize_collection = convert(bit, case when param_value = 'yes' then 1 else 0 end) + from dbo.sma_params p where p.param_key = 'alert_history_all_servers-parallelize'; + if @_parallelize_collection = 1 + select @_parallel_threads = convert(int, param_value) from dbo.sma_params p where p.param_key = 'alert_history_all_servers-parallel-threads'; + + IF @_parallelize_collection = 0 + BEGIN + IF @verbose > 0 + PRINT 'Populate dbo.alert_history_all_servers in serial manner.'; + SET @_sql = N'-- Collect Latest Alert details from All Servers Every 2 Minutes + if ( (select isnull(max(collection_time_utc),''2023-01-01 00:00'') from dbo.alert_history_all_servers) < dateadd(minute, -@schedule_minutes, getutcdate()) ) + begin + exec dbo.usp_GetAllServerCollectedData + @result_to_table = ''dbo.alert_history_all_servers'', + @verbose = @verbose, + @truncate_table = @truncate_table, + @has_staging_table = @has_staging_table + end + else + print ''Did not meet schedule requirement.''+char(13);'; + IF @verbose > 0 + PRINT @_sql; + EXEC sp_executesql @_sql, @_params, @verbose, @truncate_table, @has_staging_table, @schedule_minutes; + END + ELSE + BEGIN -- Parallelize + IF @verbose > 0 + PRINT 'Populate dbo.alert_history_all_servers in Parallel threads'; + + WHILE @_thread_counter <= @_parallel_threads + BEGIN + IF @verbose > 0 + PRINT 'Loop '+convert(varchar,@_thread_counter)+' of '+convert(varchar,@_parallel_threads)+'..'; + + SET @_parallel_job_id = NULL; + SET @_parallel_job_name = '(dba) Get-AllServerAlertHistory - '+convert(varchar,@_parallel_threads)+'-Threaded-Job-'+convert(varchar,@_thread_counter); + SET @_parallel_job_step_name = 'Get-AllServerAlertHistory - '+convert(varchar,@_parallel_threads)+'-Threaded-Job-'+convert(varchar,@_thread_counter); + + BEGIN TRY + -- Create temporary parallel jobs if required + IF NOT EXISTS (SELECT * FROM msdb.dbo.sysjobs_view WHERE name = @_parallel_job_name) + BEGIN + IF @verbose > 0 + PRINT 'Creating temp job '+QUOTENAME(@_parallel_job_name)+'..' + SET @_parallel_job_command = N'EXEC dbo.usp_GetAllServerCollectedData @result_to_table = ''dbo.alert_history_all_servers'', @has_staging_table = 0, @truncate_table = 0, @verbose = 0, + @paginate = 1, @page_count = '+convert(varchar,@_parallel_threads)+', @page_no = '+convert(varchar,@_thread_counter)+';'; + + EXEC msdb.dbo.sp_add_job @job_name=@_parallel_job_name, @category_name=N'(dba) SQLMonitor', @enabled=1, @job_id = @_parallel_job_id OUTPUT, @notify_level_eventlog=0, + @description=N'Job created as part of [(dba) Get-AllServerAlertHistory] to collect VolatileInfo in parallel threads. https://ajaydwivedi.com/github/sqlmonitor'; + + IF @verbose > 0 + PRINT 'adding step to job '+QUOTENAME(@_parallel_job_name)+'..' + EXEC msdb.dbo.sp_add_jobstep @job_id=@_parallel_job_id, @step_name=@_parallel_job_step_name, @step_id=1, @subsystem=N'TSQL', @command=@_parallel_job_command, @database_name=@_parallel_job_database, @flags=12; + + IF @verbose > 0 + PRINT 'set starting step for job '+QUOTENAME(@_parallel_job_name)+'..' + EXEC msdb.dbo.sp_update_job @job_id = @_parallel_job_id, @start_step_id = 1; + IF @verbose > 0 + PRINT 'set jobserver for job '+QUOTENAME(@_parallel_job_name)+'..' + EXEC msdb.dbo.sp_add_jobserver @job_id = @_parallel_job_id, @server_name = N'(local)'; + END + + -- Start job if not running + set @_is_job_running = dbo.fn_IsJobRunning(@_parallel_job_name); + insert @_tbl_parallel_jobs select @_parallel_job_name; + if @_is_job_running = 0 + exec msdb.dbo.sp_start_job @job_name = @_parallel_job_name; + END TRY + BEGIN CATCH + IF @verbose > 0 + PRINT 'Start Catch Block of dbo.alert_history_all_servers in Parallel threads.' + + SELECT @_errorNumber = Error_Number() + ,@_errorSeverity = Error_Severity() + ,@_errorState = Error_State() + ,@_errorLine = Error_Line() + ,@_errorMessage = Error_Message(); + + IF @verbose >= 1 + BEGIN + PRINT CHAR(13); + PRINT '@_errorNumber => '+convert(varchar,@_errorNumber); + PRINT '@_errorState => '+convert(varchar,@_errorState); + PRINT '@_errorMessage => '+@_errorMessage; + PRINT CHAR(13); + END + + set @_errorMessage = 'Error Details => Severity: '+convert(varchar,isnull(@_errorSeverity,''))+ + '. State: '+convert(varchar,isnull(@_errorState,'')) + + '. Error Line: '+convert(varchar,isnull(@_errorLine,'')) + + '. Error Message::: '+ @_errorMessage; + insert [dbo].[sma_errorlog] + ([collection_time], [function_name], [function_call_arguments], [server], [error], [remark], [executed_by], [executor_program_name]) + select [collection_time] = @_collection_time, [function_name] = 'usp_wrapper_GetAllServerCollectedData', + [function_call_arguments] = @step_name+'-Parallel', [server] = null, [error] = @_errorMessage, + [remark] = null, [executed_by] = SUSER_NAME(), [executor_program_name] = @_caller_program; + END CATCH + + SET @_thread_counter += 1; + END + + -- Once parallel jobs are started, then wait for them to finish + if @verbose >= 2 + select [RunningQuery] = '@_tbl_parallel_jobs', job_name from @_tbl_parallel_jobs; + print 'Wait for 5 seconds before checking status'; + + WAITFOR DELAY '00:00:10'; + + SELECT j.name AS job_name, + ja.start_execution_date AS StartTime, + COALESCE(CONVERT(VARCHAR(5),ABS(DATEDIFF(DAY,(GETDATE()-ja.start_execution_date),'1900-01-01'))) + ' ' + +CONVERT(VARCHAR(10),(GETDATE()-ja.start_execution_date),108),'00 00:00:00') AS [Duration] + FROM msdb.dbo.sysjobactivity ja + LEFT JOIN msdb.dbo.sysjobhistory jh ON ja.job_history_id = jh.instance_id + JOIN msdb.dbo.sysjobs j ON ja.job_id = j.job_id + WHERE ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY session_id DESC) + AND start_execution_date is not null + AND stop_execution_date is null; + + while 1=1 + begin + -- Get one job & check its running status + set @_parallel_job_name = null; + set @_is_job_running = 0; + select top 1 @_parallel_job_name = job_name from @_tbl_parallel_jobs; + + -- If no jobs to process, then exit loop + if @_parallel_job_name is null + begin + if @verbose > 0 + print '@_parallel_job_name is null is null. So exit loop' + break; + end + else + begin -- if job to process is found, then check its running status + if @verbose > 0 + print ' Checking running status for job '+quotename(@_parallel_job_name); + --exec dbo.usp_get_job_running_status @job_name = @_parallel_job_name, @is_running_OUTPUT = @_is_job_running; + set @_is_job_running = dbo.fn_IsJobRunning(@_parallel_job_name); + if @_is_job_running = 0 -- if job is not running, then delete its entry, and proceed for next job + begin + if @verbose > 0 + print ' Job '+quotename(@_parallel_job_name)+' is not running. So delete its entry from @_tbl_parallel_jobs'; + delete from @_tbl_parallel_jobs where job_name = @_parallel_job_name; + continue; + end + else + begin + if @verbose > 0 + print ' Job '+quotename(@_parallel_job_name)+' is running. So wait for another 2 seconds'; + waitfor delay '00:00:02'; + end + end + end + + /* + BEGIN TRAN + if exists (select * from sys.tables where name = 'all_server_volatile_info' and is_memory_optimized = 0) + exec ('truncate table dbo.all_server_volatile_info'); + else + delete from dbo.all_server_volatile_info; + + set @_sql = N' + ;with cte_volatile_info as ( + select '+@_output_columns+', row_id = row_number()over(partition by srv_name order by srv_name) + from dbo.all_server_volatile_info__staging + ) + insert dbo.all_server_volatile_info + ('+@_output_columns+') + select '+@_output_columns+' from cte_volatile_info where row_id = 1; + '; + if @verbose > 0 + print @_sql; + exec (@_sql); + COMMIT TRAN + */ + END + END + + END TRY -- Perform main logic inside Try/Catch BEGIN CATCH IF @verbose > 0 diff --git a/DDLs/SCH-usp_wrapper_GetAllServerInfo.sql b/DDLs/SCH-usp_wrapper_GetAllServerInfo.sql index 081a58c..531f4d6 100644 --- a/DDLs/SCH-usp_wrapper_GetAllServerInfo.sql +++ b/DDLs/SCH-usp_wrapper_GetAllServerInfo.sql @@ -302,7 +302,7 @@ else if @schedule_minutes = 0 or not exists (select 1/0 from dbo.all_server_collection_latency_info where collection_time >= dateadd(minute,-@schedule_minutes,getdate())) begin exec dbo.usp_GetAllServerInfo @result_to_table = ''dbo.all_server_collection_latency_info'', @verbose = @verbose, - @output = ''srv_name, host_name, performance_counters__latency_minutes, xevent_metrics__latency_minutes, WhoIsActive__latency_minutes, os_task_list__latency_minutes, disk_space__latency_minutes, file_io_stats__latency_minutes, sql_agent_job_stats__latency_minutes, memory_clerks__latency_minutes, wait_stats__latency_minutes, BlitzIndex__latency_days, BlitzIndex_Mode0__latency_days, BlitzIndex_Mode1__latency_days, BlitzIndex_Mode4__latency_days''; + @output = ''srv_name, host_name, performance_counters__latency_minutes, xevent_metrics__latency_minutes, WhoIsActive__latency_minutes, disk_space__latency_minutes, file_io_stats__latency_minutes, sql_agent_job_stats__latency_minutes, memory_clerks__latency_minutes, wait_stats__latency_minutes, BlitzIndex__latency_days, BlitzIndex_Mode0__latency_days, BlitzIndex_Mode1__latency_days, BlitzIndex_Mode4__latency_days''; end else print ''Did not meet schedule requirement.''+char(13);'; diff --git a/Grafana-Dashboards/Alert-Engine/Alert-Rules-SQLMonitor.yaml b/Grafana-Dashboards/Alert-Engine/Alert-Rules-SQLMonitor.yaml deleted file mode 100644 index 5d82562..0000000 --- a/Grafana-Dashboards/Alert-Engine/Alert-Rules-SQLMonitor.yaml +++ /dev/null @@ -1,46 +0,0 @@ -apiVersion: 1 -groups: - - orgId: 1 - name: SQLMonitor - folder: SQLServer - interval: 2m - rules: - - uid: 6gZECDEVz - title: Blocking - condition: Blockings - data: - - refId: Blockings - relativeTimeRange: - from: 300 - to: 0 - datasourceUid: ygPVA4snk - model: - datasource: - type: mssql - uid: ygPVA4snk - editorMode: code - format: table - hide: false - intervalMs: 1000 - maxDataPoints: 43200 - rawQuery: true - rawSql: "declare @blocked_counts_threshold int = 2;\r\ndeclare @blocked_duration_max_seconds_threshold bigint = 300;\r\ndeclare @connection_count_threshold int = 300;\r\ndeclare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\nset @params = N'@blocked_counts_threshold int, @blocked_duration_max_seconds_threshold bigint, @connection_count_threshold int';\r\nset quoted_identifier off;\r\nset @sql = \"\r\n;with t_cte as (\r\n\tselect\tsrv_name, blocked_counts, blocked_duration_max_seconds, connection_count\r\n\tfrom dbo.all_server_volatile_info_history h\r\n\twhere h.collection_time = (select max(i.collection_time) from dbo.all_server_volatile_info_history i)\r\n)\r\nselect srv_name, blocked_counts, \r\n [details] = quotename(srv_name) +' has '+ convert(varchar,blocked_counts)+' blocking(s) for over '+ convert(varchar,blocked_duration_max_seconds) + ' seconds'\r\nfrom t_cte cte\r\nwhere 1=1\r\nand ( -- Condition 01 -> Blocked over 4 hours\r\n\t\t(\tblocked_counts > 0\r\n\t\t\tand blocked_duration_max_seconds >= (4*60*60) -- 4 hours\r\n\t\t)\r\n\t\t-- Condition 02 -> More than @blocked_counts_threshold blockings for over @blocked_duration_max_seconds_threshold\r\n\tor\t(\tblocked_counts >= @blocked_counts_threshold\r\n\t\t\tand blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold\r\n\t\t)\r\n\t\t-- Condition 03 -> 50% blockings of @blocked_duration_max_seconds_threshold for over @blocked_duration_max_seconds_threshold when connections are over @connection_count_threshold \r\n or (\tconnection_count >= @connection_count_threshold \r\n\t\t\tand blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold/2\r\n\t\t\tand blocked_counts >= @blocked_counts_threshold\r\n\t\t)\r\n)\r\n\";\r\nset quoted_identifier off;\r\n--print @sql\r\nexec dbo.sp_executesql @sql, @params, @blocked_counts_threshold, @blocked_duration_max_seconds_threshold, @connection_count_threshold;" - refId: Blockings - sql: - columns: - - parameters: [] - type: function - groupBy: - - property: - type: string - type: groupBy - limit: 50 - noDataState: OK - execErrState: Error - for: 0s - annotations: - summary: '{{ $labels.details }}' - labels: - name: Blocking-SQLMonitor - target: sqlmonitor-slack - isPaused: false diff --git a/Grafana-Dashboards/Alert-Engine/Contact-Points-SQLMonitor.yaml b/Grafana-Dashboards/Alert-Engine/Contact-Points-SQLMonitor.yaml deleted file mode 100644 index d54d3a9..0000000 --- a/Grafana-Dashboards/Alert-Engine/Contact-Points-SQLMonitor.yaml +++ /dev/null @@ -1,20 +0,0 @@ -apiVersion: 1 -contactPoints: - - orgId: 1 - name: sqlmonitor-email - receivers: - - uid: xYXjjvEVz - type: email - settings: - addresses: sqlagentservice@gmail.com - singleEmail: false - disableResolveMessage: false - - orgId: 1 - name: sqlmonitor-slack - receivers: - - uid: udz8I1PVk - type: slack - settings: - recipient: '#sqlmonitor-alerts' - url: https://hooks.slack.com/services/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 - disableResolveMessage: false diff --git a/Grafana-Dashboards/Alert-Engine/Notification-Policies-SQLMonitor.yaml b/Grafana-Dashboards/Alert-Engine/Notification-Policies-SQLMonitor.yaml deleted file mode 100644 index 275f4b8..0000000 --- a/Grafana-Dashboards/Alert-Engine/Notification-Policies-SQLMonitor.yaml +++ /dev/null @@ -1,23 +0,0 @@ -apiVersion: 1 -policies: - - orgId: 1 - receiver: sqlmonitor-email - group_by: - - alertname - routes: - - receiver: sqlmonitor-slack - group_by: - - srv_name - - alertname - object_matchers: - - - target - - = - - sqlmonitor-slack - - receiver: sqlmonitor-email - group_by: - - srv_name - - alertname - object_matchers: - - - target - - = - - sqlmonitor-email diff --git a/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - All Servers - INTERNAL.json b/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - All Servers - INTERNAL.json index cabca20..ee75fa8 100644 --- a/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - All Servers - INTERNAL.json +++ b/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - All Servers - INTERNAL.json @@ -673,18 +673,6 @@ } ] }, - { - "matcher": { - "id": "byName", - "options": "os_task_list__latency_minutes" - }, - "properties": [ - { - "id": "displayName", - "value": "os_task_list_latency" - } - ] - }, { "matcher": { "id": "byName", @@ -806,7 +794,7 @@ "h": 10, "w": 24, "x": 0, - "y": 20 + "y": 2 }, "id": 850, "options": { @@ -823,7 +811,7 @@ "showHeader": true, "sortBy": [] }, - "pluginVersion": "11.2.0", + "pluginVersion": "11.3.1", "targets": [ { "datasource": { @@ -834,7 +822,7 @@ "format": "table", "hide": false, "rawQuery": true, - "rawSql": "declare @_latency_minutes_threshold int = 20;\r\ndeclare @_latency_days_threshold int = 3;\r\ndeclare @t_servers_exception table (srv_name varchar(125));\r\n\r\ninsert @t_servers_exception -- list of servers that have permanent issues\r\nvalues ('192.168.1.21'),('192.168.1.22');\r\n\r\nselect\tcli.srv_name, id.jobs_server,\r\n\t\tcli.performance_counters__latency_minutes,\r\n\t\txevent_metrics__latency_minutes = case when (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11) then cli.xevent_metrics__latency_minutes \r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\r\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.xevent_metrics__latency_minutes end,\r\n\t\tWhoIsActive__latency_minutes = case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then cli.WhoIsActive__latency_minutes \r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\r\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.WhoIsActive__latency_minutes end,\r\n\t\tcli.os_task_list__latency_minutes,\r\n\t\tcli.disk_space__latency_minutes, \r\n\t\tcli.file_io_stats__latency_minutes,\r\n\t\tcli.sql_agent_job_stats__latency_minutes,\r\n\t\tcli.memory_clerks__latency_minutes,\r\n\t\tcli.wait_stats__latency_minutes,\r\n\t\tcli.BlitzIndex__latency_days,\r\n\t\tcli.BlitzIndex_Mode0__latency_days,\r\n\t\t--cli.BlitzIndex_Mode1__latency_days,\r\n\t\t--cli.BlitzIndex_Mode4__latency_days,\r\n\t\t--collection_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), cli.collection_time)\r\n\t\t[Issues] = (case when cli.performance_counters__latency_minutes > @_latency_minutes_threshold then 'performance_counters,' else '' end)+\r\n\t\t\t\t\t(case when cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11 then 'xevent_metrics' else '' end)+\r\n\t\t\t\t\t(case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then 'WhoIsActive,' else '' end)+\r\n\t\t\t\t\t(case when cli.os_task_list__latency_minutes > @_latency_minutes_threshold then 'os_task_list,' else '' end)+\r\n\t\t\t\t\t(case when cli.disk_space__latency_minutes > @_latency_minutes_threshold*3 then 'disk_space,' else '' end)+\r\n\t\t\t\t\t(case when cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3 then 'file_io_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2 then 'wait_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5 then 'sql_agent_job_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.memory_clerks__latency_minutes > @_latency_minutes_threshold then 'memory_clerks,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex__latency_days > @_latency_days_threshold then 'BlitzIndex,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode0,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode1,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode4,' else '' end)\r\n\t\t--,cli.host_name ,si.major_version_number ,si.product_version ,si.edition, vi.os_cpu, vi.sql_cpu\r\nfrom dbo.all_server_collection_latency_info cli\r\njoin dbo.all_server_stable_info si on si.srv_name = cli.srv_name\r\njoin dbo.all_server_volatile_info vi on vi.srv_name = cli.srv_name\r\nouter apply (select STUFF((select ', '+id.collector_powershell_jobs_server from (select distinct id.collector_powershell_jobs_server from dbo.instance_details id where id.sql_instance = cli.srv_name and id.host_name = cli.host_name) id for xml path('')), 1, 1, '') as jobs_server ) id\r\nwhere 1=1\r\nand (\tcli.performance_counters__latency_minutes > @_latency_minutes_threshold\r\n\t\tor (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11)\r\n\t\tor (cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20)\r\n\t\tor cli.os_task_list__latency_minutes > @_latency_minutes_threshold\r\n\t\tor cli.disk_space__latency_minutes > @_latency_minutes_threshold*3\r\n\t\tor cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3\r\n\t\tor cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2\r\n\t\tor cli.BlitzIndex__latency_days > @_latency_days_threshold\r\n\t\tor cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4\r\n\t\t--or cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4\r\n\t\t--or cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4\r\n\t\tor cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5\r\n\t\tor cli.memory_clerks__latency_minutes > @_latency_minutes_threshold\r\n\t)\r\nand cli.srv_name not in (select e.srv_name from @t_servers_exception e)\r\n\r\n", + "rawSql": "declare @_latency_minutes_threshold int = 20;\r\ndeclare @_latency_days_threshold int = 3;\r\ndeclare @t_servers_exception table (srv_name varchar(125));\r\n\r\ninsert @t_servers_exception -- list of servers that have permanent issues\r\nvalues ('192.168.1.21'),('192.168.1.22');\r\n\r\nselect\tcli.srv_name, id.jobs_server,\r\n\t\tcli.performance_counters__latency_minutes,\r\n\t\txevent_metrics__latency_minutes = case when (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11) then cli.xevent_metrics__latency_minutes \r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\r\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.xevent_metrics__latency_minutes end,\r\n\t\tWhoIsActive__latency_minutes = case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then cli.WhoIsActive__latency_minutes \r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\r\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.WhoIsActive__latency_minutes end,\r\n\t\tcli.disk_space__latency_minutes, \r\n\t\tcli.file_io_stats__latency_minutes,\r\n\t\tcli.sql_agent_job_stats__latency_minutes,\r\n\t\tcli.memory_clerks__latency_minutes,\r\n\t\tcli.wait_stats__latency_minutes,\r\n\t\tcli.BlitzIndex__latency_days,\r\n\t\tcli.BlitzIndex_Mode0__latency_days,\r\n\t\t--cli.BlitzIndex_Mode1__latency_days,\r\n\t\t--cli.BlitzIndex_Mode4__latency_days,\r\n\t\t--collection_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), cli.collection_time)\r\n\t\t[Issues] = (case when cli.performance_counters__latency_minutes > @_latency_minutes_threshold then 'performance_counters,' else '' end)+\r\n\t\t\t\t\t(case when cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11 then 'xevent_metrics' else '' end)+\r\n\t\t\t\t\t(case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then 'WhoIsActive,' else '' end)+\r\n\t\t\t\t\t(case when cli.disk_space__latency_minutes > @_latency_minutes_threshold*3 then 'disk_space,' else '' end)+\r\n\t\t\t\t\t(case when cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3 then 'file_io_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2 then 'wait_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5 then 'sql_agent_job_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.memory_clerks__latency_minutes > @_latency_minutes_threshold then 'memory_clerks,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex__latency_days > @_latency_days_threshold then 'BlitzIndex,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode0,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode1,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode4,' else '' end)\r\n\t\t--,cli.host_name ,si.major_version_number ,si.product_version ,si.edition, vi.os_cpu, vi.sql_cpu\r\nfrom dbo.all_server_collection_latency_info cli\r\njoin dbo.all_server_stable_info si on si.srv_name = cli.srv_name\r\njoin dbo.all_server_volatile_info vi on vi.srv_name = cli.srv_name\r\nouter apply (select STUFF((select ', '+id.collector_powershell_jobs_server from (select distinct id.collector_powershell_jobs_server from dbo.instance_details id where id.sql_instance = cli.srv_name and id.host_name = cli.host_name) id for xml path('')), 1, 1, '') as jobs_server ) id\r\nwhere 1=1\r\nand (\tcli.performance_counters__latency_minutes > @_latency_minutes_threshold\r\n\t\tor (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11)\r\n\t\tor (cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20)\r\n\t\tor cli.disk_space__latency_minutes > @_latency_minutes_threshold*3\r\n\t\tor cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3\r\n\t\tor cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2\r\n\t\tor cli.BlitzIndex__latency_days > @_latency_days_threshold\r\n\t\tor cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4\r\n\t\t--or cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4\r\n\t\t--or cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4\r\n\t\tor cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5\r\n\t\tor cli.memory_clerks__latency_minutes > @_latency_minutes_threshold\r\n\t)\r\nand cli.srv_name not in (select e.srv_name from @t_servers_exception e)\r\n\r\n", "refId": "A", "sql": { "columns": [ @@ -1091,7 +1079,7 @@ "h": 5, "w": 24, "x": 0, - "y": 21 + "y": 3 }, "id": 844, "options": { @@ -1108,7 +1096,7 @@ "showHeader": true, "sortBy": [] }, - "pluginVersion": "11.2.0", + "pluginVersion": "11.3.1", "targets": [ { "alias": "", @@ -1397,7 +1385,7 @@ "h": 7, "w": 24, "x": 0, - "y": 26 + "y": 8 }, "id": 887, "options": { @@ -1414,7 +1402,7 @@ "showHeader": true, "sortBy": [] }, - "pluginVersion": "11.2.0", + "pluginVersion": "11.3.1", "targets": [ { "alias": "", @@ -1640,7 +1628,7 @@ "h": 6, "w": 24, "x": 0, - "y": 30 + "y": 408 }, "id": 871, "options": { @@ -2136,7 +2124,7 @@ "h": 8, "w": 24, "x": 0, - "y": 23 + "y": 401 }, "id": 874, "options": { @@ -2618,7 +2606,7 @@ "h": 10, "w": 24, "x": 0, - "y": 31 + "y": 409 }, "id": 888, "options": { @@ -3053,7 +3041,7 @@ "h": 8, "w": 24, "x": 0, - "y": 24 + "y": 402 }, "id": 864, "options": { @@ -3448,7 +3436,7 @@ "h": 12, "w": 24, "x": 0, - "y": 33 + "y": 411 }, "id": 852, "options": { @@ -4306,7 +4294,7 @@ "h": 7, "w": 24, "x": 0, - "y": 34 + "y": 412 }, "id": 867, "options": { @@ -4903,7 +4891,7 @@ "h": 8, "w": 24, "x": 0, - "y": 35 + "y": 413 }, "id": 856, "options": { @@ -5395,7 +5383,7 @@ "h": 8, "w": 24, "x": 0, - "y": 36 + "y": 316 }, "id": 860, "options": { @@ -5411,7 +5399,7 @@ }, "showHeader": true }, - "pluginVersion": "11.1.4", + "pluginVersion": "11.3.1", "targets": [ { "datasource": { @@ -5472,7 +5460,8 @@ "custom": { "align": "auto", "cellOptions": { - "type": "auto" + "type": "auto", + "wrapText": true }, "filterable": true, "inspect": true @@ -5482,22 +5471,503 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "error_number" + }, + "properties": [ + { + "id": "custom.width", + "value": 120 }, { - "color": "red", - "value": 80 + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Error KB", + "url": "https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-${__data.fields.error_number}-database-engine-error" + } + ] + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "red", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_severity" + }, + "properties": [ + { + "id": "custom.width", + "value": 128 + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "orange", + "value": null + }, + { + "color": "red", + "value": 20 + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_message" + }, + "properties": [ + { + "id": "custom.align", + "value": "left" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance | occurrences | last_occurred" + }, + "properties": [ + { + "id": "custom.align", + "value": "left" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instances" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true } ] } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 11 + }, + "id": 890, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false }, - "overrides": [] + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "dataset": "AdventureWorks2019", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "editorMode": "code", + "expr": "", + "format": "table", + "legendFormat": "__auto", + "range": true, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(max);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\r\n\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\n\r\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor - All Server Alert History */\r\nset nocount on;\r\n;with t_aggregated_messages as (\r\nselect a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), last_occurred = max(a.collection_time_utc), error_message = coalesce(max(m.text), max(a.error_message))\r\n --a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \r\n --a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\r\nfrom dbo.alert_history_all_servers a\r\nleft join sys.messages m \r\n on m.language_id = 1033 and m.message_id = a.error_number\r\nwhere 1 = 1\r\nand a.collection_time_utc between @start_time_utc and @end_time_utc\r\nand (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\r\n then 0\r\n when a.error_number = 1204 then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\r\n then 0\r\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\r\n then 0\r\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\r\n then 0\r\n when a.error_number = 5084 and error_severity = 10 and a.error_message like 'Setting database option MULTI_USER to ON%'\r\n then 0\r\n when a.error_number = 5084 and error_severity = 10 and a.error_message like 'Setting database option SINGLE_USER to ON%'\r\n then 0\r\n when a.error_number = 14151 and error_severity = 18 and a.error_message like '%Named Pipes Provider: Could not open a connection to SQL Server%'\r\n then 0\r\n when a.error_number = 17828 and error_severity = 20\r\n then 0\r\n when a.error_number in (17832, 17836)\r\n then 0\r\n else 1\r\n end\r\n ) = 1\r\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\r\ngroup by a.sql_instance, a.error_number, a.error_severity\r\n--order by error_severity desc, occurrences desc, sql_instance\r\n)\r\n,t_unique_errors as (\r\nselect error_number, error_severity, error_message\r\n --,sql_instance, occurrences, last_occurred\r\nfrom t_aggregated_messages\r\ngroup by error_number, error_severity, error_message\r\n)\r\nselect error_number, error_severity, error_message,\r\n [sql_instance | occurrences | last_occurred] = \r\n stuff(( SELECT ', ' + cc.[sql_instance | occurrences | last_occurred]\r\n FROM t_aggregated_messages am\r\n outer apply (select [last_occurred_localtime] = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), last_occurred) ) lt\r\n outer apply (select [sql_instance | occurrences | last_occurred] = '('+sql_instance+' | '+convert(varchar(10),occurrences)+' | '+convert(varchar,lt.last_occurred_localtime,120)+')') cc\r\n where am.error_number = ue.error_number and am.error_severity = ue.error_severity\r\n order by occurrences desc\r\n FOR XML PATH('')\r\n ), 1, 2, ''),\r\n [sql_instances] = \r\n stuff(( SELECT ', ' + cc.sql_instance_quoted\r\n FROM t_aggregated_messages am\r\n outer apply (select sql_instance_quoted = quotename(sql_instance,'''')) cc\r\n where am.error_number = ue.error_number and am.error_severity = ue.error_severity\r\n FOR XML PATH('')\r\n ), 1, 2, '')\r\nfrom t_unique_errors ue\r\n\"\r\nset quoted_identifier on;\r\n\r\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Alerts Aggregated by Type - \"${alert_history_aggregated_row_count}\" Total - Between ____${__from:date:YYYY-MM-DD HH.mm}___to___${__to:date:YYYY-MM-DD HH.mm}____", + "type": "table" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto", + "wrapText": false + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "collection_time" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": true + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "dark-blue", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": true + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue", + "value": null + } + ] + } + }, + { + "id": "custom.width", + "value": 218 + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Server Health Dashboard", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.sql_instance}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "server_name" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_name" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": true + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue", + "value": null + } + ] + } + }, + { + "id": "custom.width", + "value": 189 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_number" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "red", + "value": null + } + ] + } + }, + { + "id": "custom.width", + "value": 124 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_severity" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "yellow", + "value": null + }, + { + "color": "red", + "value": 20 + } + ] + } + }, + { + "id": "custom.width", + "value": 126 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_message" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": false + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-yellow", + "value": null + } + ] + } + }, + { + "id": "custom.minWidth", + "value": 400 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "host_instance" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "updated_time_utc" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "collection_time" + }, + "properties": [ + { + "id": "custom.width", + "value": 152 + } + ] + } + ] }, "gridPos": { - "h": 8, + "h": 12, "w": 24, "x": 0, - "y": 29 + "y": 21 }, "id": 873, "options": { @@ -5510,20 +5980,21 @@ ], "show": false }, - "showHeader": true + "showHeader": true, + "sortBy": [] }, - "pluginVersion": "11.2.0", + "pluginVersion": "11.3.1", "targets": [ { "dataset": "AdventureWorks2019", "datasource": { "type": "mssql", - "uid": "ygPVA4snk" + "uid": "${sqlmonitor_datasource}" }, "editorMode": "code", "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(max);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\r\n\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\n\r\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor - All Server Alert History */\r\nset nocount on;\r\nselect a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \r\n a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\r\nfrom dbo.alert_history_all_servers a\r\nwhere 1 = 1\r\nand a.collection_time_utc between @start_time_utc and @end_time_utc\r\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\r\n\"\r\nset quoted_identifier on;\r\n\r\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(max);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\r\n\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\n\r\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor - All Server Alert History */\r\nset nocount on;\r\nselect a.collection_time_utc, a.sql_instance, a.database_name, a.error_number, \r\n a.error_severity, a.error_message \r\n --, a.host_instance, a.updated_time_utc, a.server_name\r\nfrom dbo.alert_history_all_servers a\r\nwhere 1 = 1\r\nand a.collection_time_utc between @start_time_utc and @end_time_utc\r\nand (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\r\n then 0\r\n when a.error_number = 1204 then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\r\n then 0\r\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\r\n then 0\r\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\r\n then 0\r\n else 1\r\n end\r\n ) = 1\r\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\r\norder by 1 desc, 2\r\n\"\r\nset quoted_identifier on;\r\n\r\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -5548,7 +6019,7 @@ "type": "table" } ], - "title": "All Servers - Alert History - \"${alert_history_row_count}\" Total - Between ____${__from:date:YYYY-MM-DD HH.mm}___to___${__to:date:YYYY-MM-DD HH.mm}____", + "title": "All Servers - Alert History - \"${alert_history_aggregated_row_count}/${alert_history_row_count}\" Total - Between ____${__from:date:YYYY-MM-DD HH.mm}___to___${__to:date:YYYY-MM-DD HH.mm}____", "type": "row" }, { @@ -5578,7 +6049,8 @@ "custom": { "align": "center", "cellOptions": { - "type": "auto" + "type": "auto", + "wrapText": true }, "filterable": true, "inspect": false @@ -6329,7 +6801,7 @@ "h": 7, "w": 24, "x": 0, - "y": 84 + "y": 462 }, "id": 119, "options": { @@ -6426,8 +6898,8 @@ }, { "current": { - "text": "1763105370478", - "value": "1763105370478" + "text": "1770179641409", + "value": "1770179641409" }, "datasource": { "type": "mssql", @@ -7167,8 +7639,8 @@ }, { "current": { - "text": "0", - "value": "0" + "text": "5", + "value": "5" }, "datasource": { "type": "mssql", @@ -7187,8 +7659,8 @@ }, { "current": { - "text": "10", - "value": "10" + "text": "11", + "value": "11" }, "datasource": { "type": "mssql", @@ -7247,8 +7719,8 @@ }, { "current": { - "text": "0", - "value": "0" + "text": "1", + "value": "1" }, "datasource": { "type": "mssql", @@ -7267,8 +7739,8 @@ }, { "current": { - "text": "1", - "value": "1" + "text": "0", + "value": "0" }, "datasource": { "type": "mssql", @@ -7367,8 +7839,8 @@ }, { "current": { - "text": "0", - "value": "0" + "text": "348", + "value": "348" }, "datasource": { "type": "mssql", @@ -7388,8 +7860,8 @@ }, { "current": { - "text": "1763105402382", - "value": "1763105402382" + "text": "1770179408610", + "value": "1770179408610" }, "datasource": { "type": "mssql", @@ -7408,8 +7880,8 @@ }, { "current": { - "text": "1763105408692", - "value": "1763105408692" + "text": "1770179641409", + "value": "1770179641409" }, "datasource": { "type": "mssql", @@ -7428,8 +7900,8 @@ }, { "current": { - "text": "1763105407949", - "value": "1763105407949" + "text": "1770179409128", + "value": "1770179409128" }, "datasource": { "type": "mssql", @@ -7448,8 +7920,8 @@ }, { "current": { - "text": "1763105402336", - "value": "1763105402336" + "text": "1770179403606", + "value": "1770179403606" }, "datasource": { "type": "mssql", @@ -7468,8 +7940,8 @@ }, { "current": { - "text": "1763105403443", - "value": "1763105403443" + "text": "1770179404163", + "value": "1770179404163" }, "datasource": { "type": "mssql", @@ -7488,8 +7960,8 @@ }, { "current": { - "text": "1763105406053", - "value": "1763105406053" + "text": "1770179408173", + "value": "1770179408173" }, "datasource": { "type": "mssql", @@ -7508,8 +7980,8 @@ }, { "current": { - "text": "1763104501666", - "value": "1763104501666" + "text": "1769063700910", + "value": "1769063700910" }, "datasource": { "type": "mssql", @@ -7545,8 +8017,8 @@ }, { "current": { - "text": "1763105341333", - "value": "1763105341333" + "text": "1770179641070", + "value": "1770179641070" }, "datasource": { "type": "mssql", @@ -7565,8 +8037,8 @@ }, { "current": { - "text": "1763105341303", - "value": "1763105341303" + "text": "1770179641096", + "value": "1770179641096" }, "datasource": { "type": "mssql", @@ -7602,11 +8074,31 @@ "refresh": 1, "regex": "", "type": "query" + }, + { + "current": { + "text": "1", + "value": "1" + }, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "-- alert_history_aggregated_row_count\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(max);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - All Server Alert History */\nset nocount on;\n;with cte_aggregated as (\n select a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), error_message = max(m.text)\n --a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \n --a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\n from dbo.alert_history_all_servers a\n left join sys.messages m \n on m.language_id = 1033 and m.message_id = a.error_number\n where 1 = 1\n and a.collection_time_utc between @start_time_utc and @end_time_utc\n and (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\n then 0\n when a.error_number = 1204 then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\n then 0\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\n then 0\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\n then 0\n else 1\n end\n ) = 1\n \"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\n group by a.sql_instance, a.error_number, a.error_severity\n)\nselect count(*) from cte_aggregated\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "description": "Alert History Aggregated Row Count", + "hide": 2, + "label": "Alert History Aggregated Row Count", + "name": "alert_history_aggregated_row_count", + "options": [], + "query": "-- alert_history_aggregated_row_count\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(max);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - All Server Alert History */\nset nocount on;\n;with cte_aggregated as (\n select a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), error_message = max(m.text)\n --a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \n --a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\n from dbo.alert_history_all_servers a\n left join sys.messages m \n on m.language_id = 1033 and m.message_id = a.error_number\n where 1 = 1\n and a.collection_time_utc between @start_time_utc and @end_time_utc\n and (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\n then 0\n when a.error_number = 1204 then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\n then 0\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\n then 0\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\n then 0\n else 1\n end\n ) = 1\n \"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\n group by a.sql_instance, a.error_number, a.error_severity\n)\nselect count(*) from cte_aggregated\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "refresh": 2, + "regex": "", + "type": "query" } ] }, "time": { - "from": "now-2h", + "from": "now-4h", "to": "now" }, "timepicker": { @@ -7621,6 +8113,6 @@ "timezone": "browser", "title": "Monitoring - Live - All Servers", "uid": "distributed_live_dashboard_all_servers", - "version": 8, + "version": 28, "weekStart": "" } \ No newline at end of file diff --git a/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - Distributed - INTERNAL.json b/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - Distributed - INTERNAL.json index ed0f0ac..5c30b6b 100644 --- a/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - Distributed - INTERNAL.json +++ b/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Live - Distributed - INTERNAL.json @@ -373,22 +373,34 @@ "mode": "absolute", "steps": [ { - "color": "dark-blue", + "color": "green", "value": null + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 } ] }, - "unit": "s" + "unit": "percent" }, "overrides": [] }, "gridPos": { - "h": 2, - "w": 5, + "h": 3, + "w": 3, "x": 11, "y": 1 }, - "id": 83, + "id": 158, "options": { "colorMode": "value", "graphMode": "none", @@ -396,17 +408,11 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [ - "last" - ], + "calcs": [], "fields": "/.*/", - "values": true + "values": false }, "showPercentChange": false, - "text": { - "titleSize": 8, - "valueSize": 18 - }, "textMode": "auto", "wideLayout": true }, @@ -418,31 +424,72 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "editorMode": "code", "format": "table", - "rawQuery": true, - "rawSql": "select [processor_info] = '$processor_name'", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ + "rawSql": "SELECT $ring_buffer_os_cpu;", + "refId": "A" + } + ], + "title": "OS CPU @ ${ring_buffer_event_time:date:YYYY-MM-DD}", + "type": "stat" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "", + "fieldConfig": { + "defaults": { + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ { - "property": { - "type": "string" - }, - "type": "groupBy" + "color": "green", + "value": null } - ], - "limit": 50 - } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 3, + "w": 3, + "x": 14, + "y": 1 + }, + "id": 161, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [], + "fields": "/.*/", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "format": "table", + "rawSql": "SELECT $ring_buffer_idle_cpu;", + "refId": "A" } ], - "title": "Processor Info", + "title": "Idle CPU @ ${ring_buffer_event_time:date:HH.mm}", "type": "stat" }, { @@ -453,14 +500,29 @@ "description": "", "fieldConfig": { "defaults": { - "decimals": 2, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "dark-blue", + "color": "dark-red", "value": null + }, + { + "color": "yellow", + "value": 300 + }, + { + "color": "orange", + "value": 600 + }, + { + "color": "light-green", + "value": 601 + }, + { + "color": "dark-green", + "value": 1200 } ] }, @@ -469,12 +531,12 @@ "overrides": [] }, "gridPos": { - "h": 2, - "w": 3, - "x": 16, + "h": 3, + "w": 2, + "x": 17, "y": 1 }, - "id": 69, + "id": 15, "options": { "colorMode": "value", "graphMode": "none", @@ -482,17 +544,11 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [ - "last" - ], + "calcs": [], "fields": "/.*/", - "values": true + "values": false }, "showPercentChange": false, - "text": { - "titleSize": 8, - "valueSize": 18 - }, "textMode": "auto", "wideLayout": true }, @@ -504,12 +560,31 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "editorMode": "code", "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect virtual_machine_type_desc\nfrom sys.dm_os_sys_info as osi\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "refId": "A" + "rawQuery": true, + "rawSql": "select $ple;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "Machine Type", + "title": "PLE", "type": "stat" }, { @@ -870,12 +945,31 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "editorMode": "code", "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect count(*) as counts\nfrom $os_task_list_table_name otl with (nolock)\nwhere collection_time_utc = (select top 1 i.collection_time_utc from $os_task_list_table_name i with (nolock)\n where i.host_name = @perfmon_host_name order by i.collection_time_utc desc)\nand otl.host_name = @perfmon_host_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "refId": "A" + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nselect proc_counts = count(*)\nfrom $perfmon_table_name pc\nwhere 1=1\n--and collection_time_utc between @start_time_utc and @end_time_utc\nand collection_time_utc = (\n select max(i.collection_time_utc) from $perfmon_table_name i \n where i.collection_time_utc >= dateadd(minute, -15, getutcdate())\n and i.host_name = @perfmon_host_name and object = 'process' and counter = '% processor time'\n )\nand pc.host_name = @perfmon_host_name\nand object = 'process' and counter = '% processor time'\nand pc.instance not in ('_total','idle')\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "OS Processes @ ${os_task_list_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "title": "OS Processes @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "stat" }, { @@ -956,34 +1050,22 @@ "mode": "absolute", "steps": [ { - "color": "green", + "color": "dark-blue", "value": null - }, - { - "color": "yellow", - "value": 70 - }, - { - "color": "orange", - "value": 80 - }, - { - "color": "red", - "value": 90 } ] }, - "unit": "percent" + "unit": "s" }, "overrides": [] }, "gridPos": { - "h": 3, - "w": 3, + "h": 2, + "w": 5, "x": 11, - "y": 3 + "y": 4 }, - "id": 158, + "id": 83, "options": { "colorMode": "value", "graphMode": "none", @@ -991,11 +1073,17 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [], + "calcs": [ + "last" + ], "fields": "/.*/", - "values": false + "values": true }, "showPercentChange": false, + "text": { + "titleSize": 8, + "valueSize": 18 + }, "textMode": "auto", "wideLayout": true }, @@ -1007,12 +1095,31 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "editorMode": "code", "format": "table", - "rawSql": "SELECT $ring_buffer_os_cpu;", - "refId": "A" + "rawQuery": true, + "rawSql": "select [processor_info] = '$processor_name'", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "OS CPU @ ${ring_buffer_event_time:date:YYYY-MM-DD}", + "title": "Processor Info", "type": "stat" }, { @@ -1023,27 +1130,28 @@ "description": "", "fieldConfig": { "defaults": { + "decimals": 2, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green", + "color": "dark-blue", "value": null } ] }, - "unit": "percent" + "unit": "s" }, "overrides": [] }, "gridPos": { - "h": 3, + "h": 2, "w": 3, - "x": 14, - "y": 3 + "x": 16, + "y": 4 }, - "id": 161, + "id": 69, "options": { "colorMode": "value", "graphMode": "none", @@ -1051,87 +1159,17 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [], + "calcs": [ + "last" + ], "fields": "/.*/", - "values": false + "values": true }, "showPercentChange": false, - "textMode": "auto", - "wideLayout": true - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "format": "table", - "rawSql": "SELECT $ring_buffer_idle_cpu;", - "refId": "A" - } - ], - "title": "Idle CPU @ ${ring_buffer_event_time:date:HH.mm}", - "type": "stat" - }, - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "", - "fieldConfig": { - "defaults": { - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "dark-red", - "value": null - }, - { - "color": "yellow", - "value": 300 - }, - { - "color": "orange", - "value": 600 - }, - { - "color": "light-green", - "value": 601 - }, - { - "color": "dark-green", - "value": 1200 - } - ] - }, - "unit": "s" - }, - "overrides": [] - }, - "gridPos": { - "h": 3, - "w": 2, - "x": 17, - "y": 3 - }, - "id": 15, - "options": { - "colorMode": "value", - "graphMode": "none", - "justifyMode": "center", - "orientation": "auto", - "percentChangeColorMode": "standard", - "reduceOptions": { - "calcs": [], - "fields": "/.*/", - "values": false + "text": { + "titleSize": 8, + "valueSize": 18 }, - "showPercentChange": false, "textMode": "auto", "wideLayout": true }, @@ -1143,31 +1181,12 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "editorMode": "code", "format": "table", - "rawQuery": true, - "rawSql": "select $ple;", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect virtual_machine_type_desc\nfrom sys.dm_os_sys_info as osi\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", + "refId": "A" } ], - "title": "PLE", + "title": "Machine Type", "type": "stat" }, { @@ -1642,7 +1661,7 @@ { "matcher": { "id": "byName", - "options": "IsClustered" + "options": "IsHadrEnabled" }, "properties": [ { @@ -1662,7 +1681,7 @@ "x": 14, "y": 7 }, - "id": 74, + "id": 168, "options": { "colorMode": "value", "graphMode": "none", @@ -1690,11 +1709,11 @@ "uid": "${sqlmonitor_datasource}" }, "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsClustered')) as IsClustered --, CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "refId": "A" } ], - "title": "IsClustered", + "title": "IsHadrEnabled", "type": "stat" }, { @@ -1721,7 +1740,7 @@ { "matcher": { "id": "byName", - "options": "IsHadrEnabled" + "options": "IsClustered" }, "properties": [ { @@ -1741,7 +1760,7 @@ "x": 16, "y": 7 }, - "id": 168, + "id": 74, "options": { "colorMode": "value", "graphMode": "none", @@ -1769,11 +1788,11 @@ "uid": "${sqlmonitor_datasource}" }, "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsClustered')) as IsClustered --, CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "refId": "A" } ], - "title": "IsHadrEnabled", + "title": "IsClustered", "type": "stat" }, { @@ -3243,7 +3262,7 @@ "h": 9, "w": 24, "x": 0, - "y": 18 + "y": 632 }, "id": 181, "options": { @@ -3358,7 +3377,8 @@ "mode": "absolute", "steps": [ { - "color": "transparent" + "color": "transparent", + "value": null } ] }, @@ -3370,14 +3390,14 @@ "h": 8, "w": 24, "x": 0, - "y": 28 + "y": 62 }, "id": 16, "options": { "legend": { "calcs": [ "lastNotNull", - "p99" + "p98" ], "displayMode": "table", "placement": "right", @@ -3424,10 +3444,129 @@ ], "title": "CPU - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "timeseries" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "% CPU", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "line+area" + } + }, + "mappings": [], + "max": 100, + "min": 0, + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "transparent", + "value": null + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 240 + }, + "id": 193, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "p98" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "98th %", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\n;with cte_process_cpu as (\n\tselect pc.collection_time_utc, pc.host_name, pc.object, pc.counter,\n\t\t\tpc.value, \n\t\t\tpc.instance,\n\t\t\tprocess = case when CHARINDEX('#',s1.instance) > 0 then left(s1.instance, CHARINDEX('#',s1.instance)-1) else s1.instance end\n\tfrom $perfmon_table_name pc\n\touter apply (select instance = case when CHARINDEX('$',pc.instance) > 0 then left(pc.instance, CHARINDEX('$',pc.instance)-1) else pc.instance end ) s1\n\twhere collection_time_utc between @start_time_utc and @end_time_utc\n\tand pc.host_name = @perfmon_host_name\n\t--and collection_time_utc = (select max(i.collection_time_utc) from dbo.performance_counters i)\n\tand object = 'process' and counter = '% processor time'\n\tand pc.instance not in ('_total','idle')\n)\nselect\t[time] = collection_time_utc, --p.host_name, p.object, p.counter, \n\t\t[metric] = p.process,\n\t\t[value] = ceiling(sum(p.value)*1.0/si.cpu_count)\nfrom cte_process_cpu p\nouter apply (select si.cpu_count from sys.dm_os_sys_info si) si\ngroup by collection_time_utc, p.process, si.cpu_count\nhaving ceiling(sum(p.value)*1.0/si.cpu_count) > 0\norder by [time]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "OS Processes CPU Utilization - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "timeseries" } ], "repeat": "datasource", - "title": "Trend - CPU Utilization", + "title": "Trend - CPU Utilization @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -3438,180 +3577,91 @@ "x": 0, "y": 19 }, - "id": 113, + "id": 195, "panels": [ { "datasource": { + "default": false, "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "Blocking Details____In Selected Time Duration____Since____${__from:date:YYYY-MM-DD HH.mm}___till___${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}____", + "description": "select physical_memory_in_use_kb from sys.dm_os_process_memory", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "filterable": true, - "inspect": false + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" - } - ] - } - }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "BLOCKING_TREE" - }, - "properties": [ - { - "id": "custom.width", - "value": 308 - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "CPU" - }, - "properties": [ - { - "id": "unit", - "value": "ms" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "writes" - }, - "properties": [ - { - "id": "unit", - "value": "short" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "used_memory_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "used_memory" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "reads_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "reads" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "tempdb_allocations_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "tempdb_allocations" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "tempdb_current_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "tempdb_current" + "color": "green", + "value": null } ] }, - { - "matcher": { - "id": "byName", - "options": "granted_memory_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "granted_memory" - } - ] - } - ] + "unit": "kbytes" + }, + "overrides": [] }, "gridPos": { - "h": 8, + "h": 10, "w": 24, "x": 0, - "y": 251 + "y": 305 }, - "id": 111, + "id": 194, "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" + "legend": { + "calcs": [ + "last", + "max", + "mean" ], - "show": false + "displayMode": "table", + "placement": "bottom", + "showLegend": true, + "sortBy": "Mean", + "sortDesc": true }, - "showHeader": true, - "sortBy": [ - { - "desc": false, - "displayName": "collection_time" - } - ] + "tooltip": { + "mode": "multi", + "sort": "desc" + } }, "pluginVersion": "11.3.1", "targets": [ @@ -3622,9 +3672,9 @@ "uid": "${sqlmonitor_datasource}" }, "editorMode": "code", - "format": "table", + "format": "time_series", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\n\ndeclare @collection_time datetime;\nselect @collection_time = max(l.collection_time) from dbo.WhoIsActive l;\n\n-- Get Blocking Tree\nif exists (select 1/0 from dbo.WhoIsActive w with (nolock) where w.collection_time = @collection_time and (blocking_session_id is not null or blocked_session_count > 0))\nbegin\n\t;WITH T_BLOCKERS AS (\n\t\t-- Find block Leaders\n\t\tSELECT\t[duration_s] = datediff(SECOND, start_time, r.collection_time), [collection_time], [session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE([sql_command],[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), [login_name], wait_info, [blocking_session_id], [blocked_session_count] = replace(blocked_session_count,',',''),\n\t\t\t\t[status], open_tran_count, [host_name], [database_name], [program_name], tasks, granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], additional_info,\n\t\t\t\t[LEVEL] = CAST (REPLICATE ('0', 4-LEN (CAST (r.session_id AS VARCHAR))) + CAST (r.session_id AS VARCHAR) AS VARCHAR (1000))\n\t\t\t\t,[head_blocker] = session_id ,query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock) WHERE r.collection_time = @collection_time and (r.blocking_session_id IS NULL AND blocked_session_count > 0)\n\t\t--\t\n\t\tUNION ALL\n\t\t--\n\t\tSELECT\t[duration_s] = datediff(SECOND, r.start_time, r.collection_time), r.[collection_time], r.[session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE(r.[sql_command],r.[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = r.additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), r.[login_name], r.wait_info, r.[blocking_session_id], [blocked_session_count] = replace(r.blocked_session_count,',',''),\n\t\t\t\tr.[status], r.open_tran_count, r.[host_name], r.[database_name], r.[program_name], r.tasks, r.granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], r.additional_info,\n\t\t\t\tCAST (B.LEVEL + RIGHT (CAST ((1000 + r.session_id) AS VARCHAR (100)), 4) AS VARCHAR (1000)) AS LEVEL\n\t\t\t\t,[head_blocker] = case when B.[head_blocker] is null then B.session_id else B.[head_blocker] end ,r.query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock)\n\t\tINNER JOIN T_BLOCKERS AS B ON\tr.collection_time = B.collection_time AND r.blocking_session_id = B.session_id\n\t\tWHERE\t r.blocking_session_id <> r.session_id\n\t)\n\tSELECT\t[collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), \n\t\t\t[ddd hh:mm:ss] = right('0000'+convert(varchar, duration_s/86400),3)+ ' '+convert(varchar,dateadd(SECOND,duration_s,'1900-01-01 00:00:00'),108), \n\t\t\t[session_id], \n\t\t\t[BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1) \n\t\t\t\t\t\t\t+\tCASE\tWHEN (LEN(LEVEL)/4 - 1) = 0\n\t\t\t\t\t\t\t\t\t\tTHEN 'HEAD - '\n\t\t\t\t\t\t\t\t\t\tELSE '|------ ' \n\t\t\t\t\t\t\t\tEND\n\t\t\t\t\t\t\t+\tCAST (r.session_id AS NVARCHAR (10)) + N' ' + ISNULL((CASE WHEN LEFT(ISNULL(r.[sql_text],''),1) = '(' THEN SUBSTRING(ISNULL(r.[sql_text],''),CHARINDEX('exec',ISNULL(r.[sql_text],'')),LEN(ISNULL(r.[sql_text],''))) ELSE ISNULL(r.[sql_text],'') END),''),\n\t\t\t[blocking_session_id], [blocked_session_count] = case when [blocked_session_count] = 0 then null else [blocked_session_count] end, r.wait_info, [login_name], [program_name], [host_name], tasks, command, [database_name], status, r.open_tran_count, r.CPU, [used_memory_kb] = (r.used_memory*8.0), [reads_kb] = (r.[reads]*8.0), r.[writes], [tempdb_allocations_kb] = tempdb_allocations*8.0, tempdb_current_kb = tempdb_current*8.0, granted_memory_kb = granted_memory * 8.0\n\t\t\t--,r.query_plan ,r.additional_info\n\tFROM\tT_BLOCKERS AS r ORDER BY collection_time, LEVEL;\nend\nelse\n select 'No Blocking Found for Time Window' as [No Result]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@sql_instance varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect\t[time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), h.collection_time),\n\t\t\t\t[SqlServer] = h.physical_memory_in_use_kb,\n\t\t\t\t[Total_Ram] = asi.total_physical_memory_kb,\n\t\t\t\t[Available_Memory] = h.available_physical_memory_kb\nfrom dbo.all_server_volatile_info_history h\njoin dbo.all_server_stable_info asi\ton asi.srv_name = h.srv_name\nwhere h.collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc)\nand h.srv_name = @sql_instance\norder by time\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql , @params, @sql_instance, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -3645,30 +3695,15 @@ } } ], - "title": "Blocking Details - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", - "type": "table" - } - ], - "repeat": "datasource", - "title": "Blocking Tree (Queries) - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 20 - }, - "id": 151, - "panels": [ + "title": "SQL Server Process Memory", + "type": "timeseries" + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", "fieldConfig": { "defaults": { "color": { @@ -3678,12 +3713,12 @@ "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", - "axisLabel": "", + "axisLabel": "% CPU", "axisPlacement": "auto", "barAlignment": 0, "barWidthFactor": 0.6, "drawStyle": "line", - "fillOpacity": 0, + "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "legend": false, @@ -3692,23 +3727,19 @@ }, "insertNulls": false, "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, "lineWidth": 1, "pointSize": 5, "scaleDistribution": { - "log": 10, - "type": "log" + "type": "linear" }, - "showPoints": "always", - "spanNulls": false, + "showPoints": "never", + "spanNulls": true, "stacking": { "group": "A", - "mode": "normal" + "mode": "none" }, "thresholdsStyle": { - "mode": "off" + "mode": "line+area" } }, "mappings": [], @@ -3716,31 +3747,33 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "transparent", + "value": null } ] }, - "unit": "short" + "unit": "mbytes" }, "overrides": [] }, "gridPos": { - "h": 11, + "h": 10, "w": 24, "x": 0, - "y": 252 + "y": 315 }, - "id": 149, + "id": 196, "options": { "legend": { "calcs": [ - "mean", + "lastNotNull", + "p98", "max" ], "displayMode": "table", "placement": "right", "showLegend": true, - "sortBy": "Mean", + "sortBy": "98th %", "sortDesc": true }, "tooltip": { @@ -3756,115 +3789,35 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "editorMode": "code", "format": "time_series", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], w.login_name as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" - } - ], - "title": "[${server}] - Lead Blockers - Logins - Blocked Count", - "type": "timeseries" - }, - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "log": 10, - "type": "log" - }, - "showPoints": "always", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\n;with cte_process_cpu as (\n\tselect pc.collection_time_utc, pc.host_name, pc.object, pc.counter,\n\t\t\tpc.value, \n\t\t\tpc.instance,\n\t\t\tprocess = case when CHARINDEX('#',s1.instance) > 0 then left(s1.instance, CHARINDEX('#',s1.instance)-1) else s1.instance end\n\tfrom $perfmon_table_name pc\n\touter apply (select instance = case when CHARINDEX('$',pc.instance) > 0 then left(pc.instance, CHARINDEX('$',pc.instance)-1) else pc.instance end ) s1\n\twhere collection_time_utc between @start_time_utc and @end_time_utc\n\tand pc.host_name = @perfmon_host_name\n\tand object = 'process' and counter = 'working set'\n\tand pc.instance not in ('_total','idle')\n)\nselect\t[time] = collection_time_utc, --p.host_name, p.object, p.counter, \n\t\t[metric] = p.process,\n\t\t[value] = floor(sum(p.value)*1.0/1024.0/1024.0)\nfrom cte_process_cpu p\ngroup by collection_time_utc, p.process\nhaving floor(sum(p.value)*1.0/1024.0/1024.0) > 0\norder by [time]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ { - "color": "green" + "parameters": [], + "type": "function" } - ] - }, - "unit": "short" - }, - "overrides": [] - }, - "gridPos": { - "h": 11, - "w": 24, - "x": 0, - "y": 410 - }, - "id": 152, - "options": { - "legend": { - "calcs": [ - "mean", - "max" - ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true - }, - "tooltip": { - "mode": "multi", - "sort": "desc" - } - }, - "pluginVersion": "8.4.4", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "format": "time_series", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], \n w.login_name+' {{'+(case when len(w.program_name)>35 then left(w.program_name,35)+'..' else w.program_name end)+'}}' as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name, w.program_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "[${server}] - Lead Blockers - Programs - Blocked Count", + "title": "OS Processes Memory Utilization - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "timeseries" } ], - "repeat": "datasource", - "title": "Lead Blockers - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "title": "Trend - Memory Utilization @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -3873,111 +3826,101 @@ "h": 1, "w": 24, "x": 0, - "y": 21 + "y": 20 }, - "id": 59, + "id": 198, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "description": "select * from dbo.alert_history where error_number = 15457;", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 10, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "log": 2, - "type": "log" - }, - "showPoints": "never", - "spanNulls": true, - "stacking": { - "group": "A", - "mode": "none" + "align": "center", + "cellOptions": { + "type": "auto", + "wrapText": false }, - "thresholdsStyle": { - "mode": "line+area" - } + "filterable": true, + "inspect": true }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "transparent" - }, - { - "color": "orange", - "value": 0 - }, + "color": "green", + "value": null + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "time" + }, + "properties": [ { - "color": "red", - "value": 2 + "id": "custom.width", + "value": 227 } ] }, - "unit": "short" - }, - "overrides": [] + { + "matcher": { + "id": "byName", + "options": "error_number" + }, + "properties": [ + { + "id": "custom.width", + "value": 172 + } + ] + } + ] }, "gridPos": { - "h": 7, + "h": 8, "w": 24, "x": 0, - "y": 31 + "y": 44 }, - "id": 57, + "id": 197, "options": { - "legend": { - "calcs": [ - "mean", - "lastNotNull", - "max" + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "bottom", - "showLegend": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "desc" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ { - "alias": "", + "dataset": "AdventureWorks2019", "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, "editorMode": "code", - "format": "time_series", - "hide": false, + "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name\nand (\t(pc.object = (@object_name+':memory manager')\n\t\tand pc.counter in ('memory grants outstanding','memory grants pending'))\n\t)\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @perfmon_host_name varchar(255);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\n\r\nset @sql_instance = '$server';\r\nset @perfmon_host_name = '$perfmon_host_name';\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"\r\nselect\t[time] = collection_time_utc, ah.error_number,\r\n\t\t--[database] = case when ah.error_number = 15457 then null else database_name end,\r\n\t\t[remarks] = ah.[error_message]\r\nfrom dbo.alert_history ah\r\nwhere collection_time_utc between @start_time_utc and @end_time_utc\r\nand ah.error_number in (15457)\r\n--and ah.error_number in (5084)\r\norder by time\r\n\"\r\nset quoted_identifier on;\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -3998,52 +3941,36 @@ } } ], - "title": "Memory Grants Pending", - "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "Trend - Memory Grants Pending", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 22 - }, - "id": 179, - "panels": [ + "title": "Server Configuration Changes", + "type": "table" + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "description": "select * from dbo.alert_history where error_number = 5084;", "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "custom": { - "align": "auto", + "align": "center", "cellOptions": { - "type": "auto" + "type": "auto", + "wrapText": false }, "filterable": true, - "inspect": false + "inspect": true }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" - }, - { - "color": "red", - "value": 80 + "color": "green", + "value": null } ] } @@ -4052,71 +3979,193 @@ { "matcher": { "id": "byName", - "options": "sql_command" + "options": "time" }, "properties": [ { "id": "custom.width", - "value": 352 + "value": 173 } ] }, { "matcher": { "id": "byName", - "options": "program_name" + "options": "error_number" }, "properties": [ { "id": "custom.width", - "value": 169 + "value": 133 } ] }, { "matcher": { "id": "byName", - "options": "session_id" + "options": "database" }, "properties": [ { "id": "custom.width", - "value": 105 + "value": 227 } ] }, { "matcher": { "id": "byName", - "options": "CPU" + "options": "remarks" }, "properties": [ { - "id": "unit", - "value": "ms" + "id": "custom.align", + "value": "left" } ] - }, - { - "matcher": { - "id": "byName", - "options": "granted_memory_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, + } + ] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 52 + }, + "id": 199, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "dataset": "AdventureWorks2019", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @perfmon_host_name varchar(255);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\n\r\nset @sql_instance = '$server';\r\nset @perfmon_host_name = '$perfmon_host_name';\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"\r\nselect\t[time] = collection_time_utc, ah.error_number,\r\n\t\t[database] = database_name,\r\n\t\t[remarks] = ah.[error_message]\r\nfrom dbo.alert_history ah\r\nwhere collection_time_utc between @start_time_utc and @end_time_utc\r\n--and ah.error_number in (15457)\r\nand ah.error_number in (5084)\r\norder by time\r\n\"\r\nset quoted_identifier on;\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ { - "id": "displayName", - "value": "granted_memory" + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Database Configuration Changes", + "type": "table" + } + ], + "title": "Server & Database Config Changes", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 21 + }, + "id": 113, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "Blocking Details____In Selected Time Duration____Since____${__from:date:YYYY-MM-DD HH.mm}___till___${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}____", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "BLOCKING_TREE" + }, + "properties": [ + { + "id": "custom.width", + "value": 308 } ] }, { "matcher": { "id": "byName", - "options": "reads_kb" + "options": "CPU" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "writes" + }, + "properties": [ + { + "id": "unit", + "value": "short" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "used_memory_kb" }, "properties": [ { @@ -4125,19 +4174,23 @@ }, { "id": "displayName", - "value": "reads" + "value": "used_memory" } ] }, { "matcher": { "id": "byName", - "options": "writes" + "options": "reads_kb" }, "properties": [ { "id": "unit", - "value": "short" + "value": "kbytes" + }, + { + "id": "displayName", + "value": "reads" } ] }, @@ -4176,7 +4229,7 @@ { "matcher": { "id": "byName", - "options": "used_memory_kb" + "options": "granted_memory_kb" }, "properties": [ { @@ -4185,19 +4238,19 @@ }, { "id": "displayName", - "value": "used_memory" + "value": "granted_memory" } ] } ] }, "gridPos": { - "h": 11, + "h": 8, "w": 24, "x": 0, - "y": 439 + "y": 982 }, - "id": 177, + "id": 111, "options": { "cellHeight": "sm", "footer": { @@ -4209,9 +4262,14 @@ "show": false }, "showHeader": true, - "sortBy": [] + "sortBy": [ + { + "desc": false, + "displayName": "collection_time" + } + ] }, - "pluginVersion": "10.1.0", + "pluginVersion": "11.3.1", "targets": [ { "alias": "", @@ -4222,7 +4280,7 @@ "editorMode": "code", "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\n\n--declare @delta_minutes int;\ndeclare @program_name nvarchar(500);\ndeclare @login_name nvarchar(255);\ndeclare @database nvarchar(500) = '$database';\ndeclare @session_id int;\ndeclare @session_host_name nvarchar(125);\ndeclare @query_pattern nvarchar(500);\ndeclare @duration int;\ndeclare @memory_grant_threshold_mb int = $memory_grant_threshold_mb;\n\nset @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\nset @duration = case when ltrim(rtrim('$duration')) <> '' then $duration else 0 end;\nif len(ltrim(rtrim('$program_name'))) > 0\n set @program_name = '$program_name'\nif len(ltrim(rtrim('$login_name'))) > 0\n set @login_name = '$login_name'\nif len(ltrim(rtrim('$session_host_name'))) > 0\n set @session_host_name = '$session_host_name'\nif len(ltrim(rtrim('$query_pattern'))) > 0\n set @query_pattern = '$query_pattern'\nif len(ltrim(rtrim('$session_id'))) > 0 and (case when '$session_id' like '%[^0-9.]%' then 'invalid' when '$session_id' like '%.%.%' then 'invalid' else 'valid' end) = 'valid'\n set @session_id = convert(int,'$session_id');\n\nset @sql_instance = '$server';\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\n--set @start_time_utc = dateadd(second,$sqlserver_start_time_utc/1000,'1970-01-01 00:00:00');\nset @end_time_utc = $__timeTo();\n--set @end_time_utc = $__timeFrom();\n--set @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2,\n\t\t\t\t@program_name nvarchar(500), @login_name nvarchar(255), @database nvarchar(500),\n\t\t\t\t@session_id int, @session_host_name nvarchar(125), @query_pattern nvarchar(500),\n\t\t\t\t@duration int, @memory_grant_threshold_mb int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor Dashboard $__dashboard: LongRunningQueries */\nset nocount on;\t\t\t\nif exists ( select * from $whoisactive_table_name w with (nolock)\n where w.collection_time = (select max(i.collection_time) from $whoisactive_table_name i with (nolock)) \n and granted_memory >= (@memory_grant_threshold_mb*1024/8) )\nbegin\n ;with t_WhoIsActive as (\n select [collection_time] = convert(varchar,w.collection_time,120), w.session_id, \n w.program_name, w.login_name, w.database_name, w.host_name,\n w.status, w.CPU, w.used_memory, w.open_tran_count, [duration_ms] = datediff(MILLISECOND, start_time, w.collection_time),\n w.wait_info, granted_memory, tempdb_current,\n sql_command = case when w.sql_command is not null then left(replace(replace(convert(nvarchar(max),w.sql_command),char(13)+char(10),''),'= (@memory_grant_threshold_mb*1024/8)\n )\n select [collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), w.session_id,\n [ddd hh:mm:ss.mss] = right('0000'+convert(varchar, duration_ms/86400000),3)+ ' '+convert(varchar,dateadd(MILLISECOND,duration_ms,'1900-01-01 00:00:00'),114), \n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, \n granted_memory_kb = (w.granted_memory * 8.0), w.open_tran_count, w.wait_info, w.sql_command, w.blocked_session_count, \n w.blocking_session_id, [reads_kb] = (w.[reads]*8.0), w.writes, [tempdb_allocations_kb] = (w.tempdb_allocations*8.0), \n tempdb_current_kb = (tempdb_current*8.0), w.tasks, w.percent_complete, [used_memory_kb] = (w.used_memory*8.0),\n start_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [start_time])\n from t_WhoIsActive w\n order by granted_memory desc, used_memory desc, collection_time\nend\nELSE\n select 'No long running query found for time window' as [No Result]\n\"\nset quoted_identifier on;\n--print @sql\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc, \n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc,\n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\n\ndeclare @collection_time datetime;\nselect @collection_time = max(l.collection_time) from dbo.WhoIsActive l;\n\n-- Get Blocking Tree\nif exists (select 1/0 from dbo.WhoIsActive w with (nolock) where w.collection_time = @collection_time and (blocking_session_id is not null or blocked_session_count > 0))\nbegin\n\t;WITH T_BLOCKERS AS (\n\t\t-- Find block Leaders\n\t\tSELECT\t[duration_s] = datediff(SECOND, start_time, r.collection_time), [collection_time], [session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE([sql_command],[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), [login_name], wait_info, [blocking_session_id], [blocked_session_count] = replace(blocked_session_count,',',''),\n\t\t\t\t[status], open_tran_count, [host_name], [database_name], [program_name], tasks, granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], additional_info,\n\t\t\t\t[LEVEL] = CAST (REPLICATE ('0', 4-LEN (CAST (r.session_id AS VARCHAR))) + CAST (r.session_id AS VARCHAR) AS VARCHAR (1000))\n\t\t\t\t,[head_blocker] = session_id ,query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock) WHERE r.collection_time = @collection_time and (r.blocking_session_id IS NULL AND blocked_session_count > 0)\n\t\t--\t\n\t\tUNION ALL\n\t\t--\n\t\tSELECT\t[duration_s] = datediff(SECOND, r.start_time, r.collection_time), r.[collection_time], r.[session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE(r.[sql_command],r.[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = r.additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), r.[login_name], r.wait_info, r.[blocking_session_id], [blocked_session_count] = replace(r.blocked_session_count,',',''),\n\t\t\t\tr.[status], r.open_tran_count, r.[host_name], r.[database_name], r.[program_name], r.tasks, r.granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], r.additional_info,\n\t\t\t\tCAST (B.LEVEL + RIGHT (CAST ((1000 + r.session_id) AS VARCHAR (100)), 4) AS VARCHAR (1000)) AS LEVEL\n\t\t\t\t,[head_blocker] = case when B.[head_blocker] is null then B.session_id else B.[head_blocker] end ,r.query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock)\n\t\tINNER JOIN T_BLOCKERS AS B ON\tr.collection_time = B.collection_time AND r.blocking_session_id = B.session_id\n\t\tWHERE\t r.blocking_session_id <> r.session_id\n\t)\n\tSELECT\t[collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), \n\t\t\t[ddd hh:mm:ss] = right('0000'+convert(varchar, duration_s/86400),3)+ ' '+convert(varchar,dateadd(SECOND,duration_s,'1900-01-01 00:00:00'),108), \n\t\t\t[session_id], \n\t\t\t[BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1) \n\t\t\t\t\t\t\t+\tCASE\tWHEN (LEN(LEVEL)/4 - 1) = 0\n\t\t\t\t\t\t\t\t\t\tTHEN 'HEAD - '\n\t\t\t\t\t\t\t\t\t\tELSE '|------ ' \n\t\t\t\t\t\t\t\tEND\n\t\t\t\t\t\t\t+\tCAST (r.session_id AS NVARCHAR (10)) + N' ' + ISNULL((CASE WHEN LEFT(ISNULL(r.[sql_text],''),1) = '(' THEN SUBSTRING(ISNULL(r.[sql_text],''),CHARINDEX('exec',ISNULL(r.[sql_text],'')),LEN(ISNULL(r.[sql_text],''))) ELSE ISNULL(r.[sql_text],'') END),''),\n\t\t\t[blocking_session_id], [blocked_session_count] = case when [blocked_session_count] = 0 then null else [blocked_session_count] end, r.wait_info, [login_name], [program_name], [host_name], tasks, command, [database_name], status, r.open_tran_count, r.CPU, [used_memory_kb] = (r.used_memory*8.0), [reads_kb] = (r.[reads]*8.0), r.[writes], [tempdb_allocations_kb] = tempdb_allocations*8.0, tempdb_current_kb = tempdb_current*8.0, granted_memory_kb = granted_memory * 8.0\n\t\t\t--,r.query_plan ,r.additional_info\n\tFROM\tT_BLOCKERS AS r ORDER BY collection_time, LEVEL;\nend\nelse\n select 'No Blocking Found for Time Window' as [No Result]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -4243,11 +4301,12 @@ } } ], - "title": "Memory Consumers Over __ $memory_grant_threshold_mb MB__ \"- WhoIsActive Data @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "title": "Blocking Details - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "table" } ], - "title": "M/r Consumers - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "repeat": "datasource", + "title": "Blocking Tree (Queries) - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -4256,28 +4315,57 @@ "h": 1, "w": 24, "x": 0, - "y": 23 + "y": 22 }, - "id": 188, + "id": 151, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select [collection_time], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], [log_used_pct], [version_store_mb], [version_store_pct]\nfrom dbo.tempdb_space_usage tsu\nwhere 1=1\nand tsu.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)", + "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "center", - "cellOptions": { - "type": "auto" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "filterable": false, - "inspect": true + "insertNulls": false, + "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "always", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "normal" + }, + "thresholdsStyle": { + "mode": "off" + } }, "mappings": [], "thresholds": { @@ -4285,359 +4373,506 @@ "steps": [ { "color": "green" - }, - { - "color": "red", - "value": 80 - } - ] - } - }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "collection_time_utc" - }, - "properties": [ - { - "id": "displayName", - "value": "Collection Time" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-background" - } - }, - { - "id": "thresholds", - "value": { - "mode": "absolute", - "steps": [ - { - "color": "super-light-purple" - } - ] - } } ] }, - { - "matcher": { - "id": "byName", - "options": "data_size_mb" - }, - "properties": [ + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 11, + "w": 24, + "x": 0, + "y": 983 + }, + "id": 149, + "options": { + "legend": { + "calcs": [ + "mean", + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "Mean", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "format": "time_series", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], w.login_name as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" + } + ], + "title": "[${server}] - Lead Blockers - Logins - Blocked Count", + "type": "timeseries" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "always", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "normal" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ { - "id": "displayName", - "value": "Data Size" + "color": "green" + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 11, + "w": 24, + "x": 0, + "y": 1141 + }, + "id": 152, + "options": { + "legend": { + "calcs": [ + "mean", + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "Mean", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "8.4.4", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "format": "time_series", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], \n w.login_name+' {{'+(case when len(w.program_name)>35 then left(w.program_name,35)+'..' else w.program_name end)+'}}' as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name, w.program_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" + } + ], + "title": "[${server}] - Lead Blockers - Programs - Blocked Count", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Lead Blockers - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 23 + }, + "id": 59, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 2, + "type": "log" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "line+area" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "transparent", + "value": null }, { - "id": "unit", - "value": "mbytes" + "color": "orange", + "value": 0 }, { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Data File Growth Trend", - "url": "d/distributed_perfmon?var-server=${server}&var-database=tempdb&viewPanel=115" - } - ] + "color": "red", + "value": 2 } ] }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 444 + }, + "id": 57, + "options": { + "legend": { + "calcs": [ + "mean", + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "editorMode": "code", + "format": "time_series", + "hide": false, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name\nand (\t(pc.object = (@object_name+':memory manager')\n\t\tand pc.counter in ('memory grants outstanding','memory grants pending'))\n\t)\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Memory Grants Pending", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Trend - Memory Grants Pending", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 24 + }, + "id": 179, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ { "matcher": { "id": "byName", - "options": "data_used_mb" + "options": "sql_command" }, "properties": [ { - "id": "displayName", - "value": "Data Used" - }, - { - "id": "unit", - "value": "mbytes" - }, - { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Tempdb Free Space Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=89" - } - ] + "id": "custom.width", + "value": 352 } ] }, { "matcher": { "id": "byName", - "options": "data_used_pct" + "options": "program_name" }, "properties": [ { - "id": "displayName", - "value": "Data Used %" - }, + "id": "custom.width", + "value": 169 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "session_id" + }, + "properties": [ { - "id": "custom.cellOptions", - "value": { - "type": "color-background" - } - }, + "id": "custom.width", + "value": 105 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "CPU" + }, + "properties": [ { "id": "unit", - "value": "percent" - }, - { - "id": "thresholds", - "value": { - "mode": "absolute", - "steps": [ - { - "color": "green" - }, - { - "color": "#EAB839", - "value": 70 - }, - { - "color": "#EF843C", - "value": 80 - }, - { - "color": "super-light-red", - "value": 90 - }, - { - "color": "red", - "value": 95 - } - ] - } - }, - { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Tempdb % Free Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=89" - } - ] + "value": "ms" } ] }, { "matcher": { "id": "byName", - "options": "log_size_mb" + "options": "granted_memory_kb" }, "properties": [ - { - "id": "displayName", - "value": "Log Size" - }, { "id": "unit", - "value": "mbytes" + "value": "kbytes" }, { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Log Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" - } - ] + "id": "displayName", + "value": "granted_memory" } ] }, { "matcher": { "id": "byName", - "options": "log_used_mb" + "options": "reads_kb" }, "properties": [ { - "id": "displayName", - "value": "Log Used" + "id": "unit", + "value": "kbytes" }, { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Log Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" - } - ] - }, + "id": "displayName", + "value": "reads" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "writes" + }, + "properties": [ { "id": "unit", - "value": "mbytes" + "value": "short" } ] }, { "matcher": { "id": "byName", - "options": "log_used_pct" + "options": "tempdb_allocations_kb" }, "properties": [ - { - "id": "displayName", - "value": "Log Used %" - }, { "id": "unit", - "value": "percent" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-background" - } + "value": "kbytes" }, { - "id": "thresholds", - "value": { - "mode": "absolute", - "steps": [ - { - "color": "green" - }, - { - "color": "#EAB839", - "value": 70 - }, - { - "color": "orange", - "value": 80 - }, - { - "color": "red", - "value": 90 - } - ] - } + "id": "displayName", + "value": "tempdb_allocations" } ] }, { "matcher": { "id": "byName", - "options": "version_store_mb" + "options": "tempdb_current_kb" }, "properties": [ - { - "id": "displayName", - "value": "Version Store" - }, { "id": "unit", - "value": "mbytes" + "value": "kbytes" }, { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Version Store Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=90" - } - ] + "id": "displayName", + "value": "tempdb_current" } ] }, { "matcher": { "id": "byName", - "options": "version_store_pct" + "options": "used_memory_kb" }, "properties": [ - { - "id": "displayName", - "value": "Version Store %" - }, { "id": "unit", - "value": "percent" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-background" - } - }, - { - "id": "thresholds", - "value": { - "mode": "absolute", - "steps": [ - { - "color": "green" - }, - { - "color": "#EAB839", - "value": 70 - }, - { - "color": "orange", - "value": 80 - }, - { - "color": "red", - "value": 90 - } - ] - } + "value": "kbytes" }, { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - VersionStore Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=90" - } - ] + "id": "displayName", + "value": "used_memory" } ] } ] }, "gridPos": { - "h": 4, + "h": 11, "w": 24, "x": 0, - "y": 255 + "y": 67 }, - "id": 189, + "id": 177, "options": { "cellHeight": "sm", "footer": { "countRows": false, - "enablePagination": false, "fields": "", "reducer": [ "sum" ], "show": false }, - "showHeader": true + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ { - "dataset": "AdventureWorks2019", + "alias": "", "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" @@ -4645,7 +4880,7 @@ "editorMode": "code", "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\r\n\r\nset @sql_instance = '$server';\r\n\r\n--set @params = N'';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor Dashboard Monitoring - Live - Distributed: TempdbSaver - tempdb_space_usage */\r\nset nocount on;\t\t\t\r\nselect [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], [log_used_pct], [version_store_mb], [version_store_pct]\r\nfrom dbo.tempdb_space_usage tsu\r\nwhere 1=1\r\nand tsu.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)\r\n\"\r\nset quoted_identifier on;\r\n--print @sql\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql, @params;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\n\n--declare @delta_minutes int;\ndeclare @program_name nvarchar(500);\ndeclare @login_name nvarchar(255);\ndeclare @database nvarchar(500) = '$database';\ndeclare @session_id int;\ndeclare @session_host_name nvarchar(125);\ndeclare @query_pattern nvarchar(500);\ndeclare @duration int;\ndeclare @memory_grant_threshold_mb int = $memory_grant_threshold_mb;\n\nset @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\nset @duration = case when ltrim(rtrim('$duration')) <> '' then $duration else 0 end;\nif len(ltrim(rtrim('$program_name'))) > 0\n set @program_name = '$program_name'\nif len(ltrim(rtrim('$login_name'))) > 0\n set @login_name = '$login_name'\nif len(ltrim(rtrim('$session_host_name'))) > 0\n set @session_host_name = '$session_host_name'\nif len(ltrim(rtrim('$query_pattern'))) > 0\n set @query_pattern = '$query_pattern'\nif len(ltrim(rtrim('$session_id'))) > 0 and (case when '$session_id' like '%[^0-9.]%' then 'invalid' when '$session_id' like '%.%.%' then 'invalid' else 'valid' end) = 'valid'\n set @session_id = convert(int,'$session_id');\n\nset @sql_instance = '$server';\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\n--set @start_time_utc = dateadd(second,$sqlserver_start_time_utc/1000,'1970-01-01 00:00:00');\nset @end_time_utc = $__timeTo();\n--set @end_time_utc = $__timeFrom();\n--set @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2,\n\t\t\t\t@program_name nvarchar(500), @login_name nvarchar(255), @database nvarchar(500),\n\t\t\t\t@session_id int, @session_host_name nvarchar(125), @query_pattern nvarchar(500),\n\t\t\t\t@duration int, @memory_grant_threshold_mb int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor Dashboard $__dashboard: LongRunningQueries */\nset nocount on;\t\t\t\nif exists ( select * from $whoisactive_table_name w with (nolock)\n where w.collection_time = (select max(i.collection_time) from $whoisactive_table_name i with (nolock)) \n and granted_memory >= (@memory_grant_threshold_mb*1024/8) )\nbegin\n ;with t_WhoIsActive as (\n select [collection_time] = convert(varchar,w.collection_time,120), w.session_id, \n w.program_name, w.login_name, w.database_name, w.host_name,\n w.status, w.CPU, w.used_memory, w.open_tran_count, [duration_ms] = datediff(MILLISECOND, start_time, w.collection_time),\n w.wait_info, granted_memory, tempdb_current,\n sql_command = case when w.sql_command is not null then left(replace(replace(convert(nvarchar(max),w.sql_command),char(13)+char(10),''),'= (@memory_grant_threshold_mb*1024/8)\n )\n select [collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), w.session_id,\n [ddd hh:mm:ss.mss] = right('0000'+convert(varchar, duration_ms/86400000),3)+ ' '+convert(varchar,dateadd(MILLISECOND,duration_ms,'1900-01-01 00:00:00'),114), \n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, \n granted_memory_kb = (w.granted_memory * 8.0), w.open_tran_count, w.wait_info, w.sql_command, w.blocked_session_count, \n w.blocking_session_id, [reads_kb] = (w.[reads]*8.0), w.writes, [tempdb_allocations_kb] = (w.tempdb_allocations*8.0), \n tempdb_current_kb = (tempdb_current*8.0), w.tasks, w.percent_complete, [used_memory_kb] = (w.used_memory*8.0),\n start_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [start_time])\n from t_WhoIsActive w\n order by granted_memory desc, used_memory desc, collection_time\nend\nELSE\n select 'No long running query found for time window' as [No Result]\n\"\nset quoted_identifier on;\n--print @sql\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc, \n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc,\n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;", "refId": "A", "sql": { "columns": [ @@ -4666,15 +4901,29 @@ } } ], - "title": "TempdbSaver - tempdb_space_usage", + "title": "Memory Consumers Over __ $memory_grant_threshold_mb MB__ \"- WhoIsActive Data @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "table" - }, + } + ], + "title": "M/r Consumers - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 25 + }, + "id": 188, + "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select [collection_time], [usage_rank], [spid], [login_name], [program_name], [host_name], [host_process_id], [is_active_session], [open_transaction_count], [transaction_isolation_level], [size_bytes], [transaction_begin_time], [is_snapshot], [log_bytes], [log_rsvd], [action_taken] \nfrom dbo.tempdb_space_consumers tsc \nwhere 1=1 \nand tsc.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)\nand (tsc.size_bytes > 0 or tsc.is_snapshot = 1)", + "description": "select [collection_time], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], [log_used_pct], [version_store_mb], [version_store_pct]\nfrom dbo.tempdb_space_usage tsu\nwhere 1=1\nand tsu.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)", "fieldConfig": { "defaults": { "color": { @@ -4693,7 +4942,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -4725,7 +4975,8 @@ "mode": "absolute", "steps": [ { - "color": "super-light-purple" + "color": "super-light-purple", + "value": null } ] } @@ -4810,7 +5061,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -4921,7 +5173,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -4992,7 +5245,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -5027,9 +5281,9 @@ "h": 4, "w": 24, "x": 0, - "y": 391 + "y": 68 }, - "id": 190, + "id": 189, "options": { "cellHeight": "sm", "footer": { @@ -5054,7 +5308,7 @@ "editorMode": "code", "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\r\n\r\nset @sql_instance = '$server';\r\n\r\n--set @params = N'';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor Dashboard Monitoring - Live - Distributed: TempdbSaver - tempdb_space_usage */\r\nset nocount on;\r\nif exists ( select 1/0 from dbo.tempdb_space_consumers tsc with (nolock)\r\n where 1=1 \r\n and tsc.collection_time = (select top 1 tsu.collection_time from tempdb_space_usage tsu order by tsu.collection_time desc)\r\n and (tsc.size_bytes > 0 or tsc.is_snapshot = 1)\r\n )\r\nbegin\t\r\n select [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), [usage_rank], [spid], [login_name], \r\n [program_name], [host_name], [host_process_id], [is_active_session], [open_transaction_count], [transaction_isolation_level], [size_bytes], \r\n [transaction_begin_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), transaction_begin_time), [is_snapshot], [log_bytes], [log_rsvd], [action_taken] \r\n from dbo.tempdb_space_consumers tsc \r\n where 1=1 \r\n and tsc.collection_time = (select top 1 tsu.collection_time from tempdb_space_usage tsu order by tsu.collection_time desc)\r\n and (tsc.size_bytes > 0 or tsc.is_snapshot = 1)\r\nend\r\nELSE\r\n select 'No tempdb consumers record in latest snapshot.' as [No Result]\r\n\"\r\nset quoted_identifier on;\r\n--print @sql\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql, @params;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params;", + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\r\n\r\nset @sql_instance = '$server';\r\n\r\n--set @params = N'';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor Dashboard Monitoring - Live - Distributed: TempdbSaver - tempdb_space_usage */\r\nset nocount on;\t\t\t\r\nselect [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], [log_used_pct], [version_store_mb], [version_store_pct]\r\nfrom dbo.tempdb_space_usage tsu\r\nwhere 1=1\r\nand tsu.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)\r\n\"\r\nset quoted_identifier on;\r\n--print @sql\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql, @params;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params;", "refId": "A", "sql": { "columns": [ @@ -5075,29 +5329,15 @@ } } ], - "title": "TempdbSaver - tempdb_space_consumers", + "title": "TempdbSaver - tempdb_space_usage", "type": "table" - } - ], - "title": "TempdbSaver - Latest @ ${tempdb_space_usage_collection_time_utc:date:YYYY-MM-DD HH.mm}", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 24 - }, - "id": 191, - "panels": [ + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select [collection_time], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [log_used_mb], [exists_valid_autogrowing_file], [log_used_pct], [log_used_pct_threshold], [log_used_gb_threshold], [spid], [transaction_start_time], [login_name], [program_name], [host_name], [host_process_id], [command], [additional_info], [action_taken], [sql_text], [is_pct_threshold_valid], [is_gb_threshold_valid], [threshold_condition], [thresholds_validated]\nfrom dbo.log_space_consumers lsc\nwhere 1=1\nand lsc.collection_time = (select top 1 i.collection_time from dbo.log_space_consumers i order by i.collection_time desc)", + "description": "select [collection_time], [usage_rank], [spid], [login_name], [program_name], [host_name], [host_process_id], [is_active_session], [open_transaction_count], [transaction_isolation_level], [size_bytes], [transaction_begin_time], [is_snapshot], [log_bytes], [log_rsvd], [action_taken] \nfrom dbo.tempdb_space_consumers tsc \nwhere 1=1 \nand tsc.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)\nand (tsc.size_bytes > 0 or tsc.is_snapshot = 1)", "fieldConfig": { "defaults": { "color": { @@ -5108,7 +5348,7 @@ "cellOptions": { "type": "auto" }, - "filterable": true, + "filterable": false, "inspect": true }, "mappings": [], @@ -5116,7 +5356,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -5148,7 +5389,8 @@ "mode": "absolute", "steps": [ { - "color": "super-light-purple" + "color": "super-light-purple", + "value": null } ] } @@ -5158,12 +5400,12 @@ { "matcher": { "id": "byName", - "options": "log_size_mb" + "options": "data_size_mb" }, "properties": [ { "id": "displayName", - "value": "Log Size" + "value": "Data Size" }, { "id": "unit", @@ -5174,8 +5416,8 @@ "value": [ { "targetBlank": true, - "title": "Perfmon - Log Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" + "title": "Perfmon - Data File Growth Trend", + "url": "d/distributed_perfmon?var-server=${server}&var-database=tempdb&viewPanel=115" } ] } @@ -5184,42 +5426,38 @@ { "matcher": { "id": "byName", - "options": "log_used_mb" + "options": "data_used_mb" }, "properties": [ { "id": "displayName", - "value": "Log Used" + "value": "Data Used" + }, + { + "id": "unit", + "value": "mbytes" }, { "id": "links", "value": [ { "targetBlank": true, - "title": "Perfmon - Log Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" + "title": "Perfmon - Tempdb Free Space Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=89" } ] - }, - { - "id": "unit", - "value": "mbytes" } ] }, { "matcher": { "id": "byName", - "options": "log_used_pct" + "options": "data_used_pct" }, "properties": [ { "id": "displayName", - "value": "Log Used %" - }, - { - "id": "unit", - "value": "percent" + "value": "Data Used %" }, { "id": "custom.cellOptions", @@ -5227,121 +5465,45 @@ "type": "color-background" } }, + { + "id": "unit", + "value": "percent" + }, { "id": "thresholds", "value": { "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", "value": 70 }, { - "color": "orange", + "color": "#EF843C", "value": 80 }, { - "color": "red", + "color": "super-light-red", "value": 90 - } - ] - } - }, - { - "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Perfmon - Log % Usage Trend", - "url": "d/distributed_perfmon?var-server=$server&var-database=${__data.fields.database_name}&viewPanel=123" - } - ] - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "database_name" - }, - "properties": [ - { - "id": "displayName", - "value": "Database" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-text" - } - }, - { - "id": "custom.align", - "value": "left" - }, - { - "id": "thresholds", - "value": { - "mode": "absolute", - "steps": [ + }, { - "color": "blue" + "color": "red", + "value": 95 } ] } }, { "id": "links", - "value": [ - { - "targetBlank": true, - "title": "Disk Space - Database Files Usage", - "url": "d/disk_space?var-server=${server}&var-database=${__data.fields.database_name}&viewPanel=30" - } - ] - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "recovery_model" - }, - "properties": [ - { - "id": "displayName", - "value": "Recovery Model" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-text" - } - }, - { - "id": "mappings", - "value": [ - { - "options": { - "SIMPLE": { - "color": "green", - "index": 0 - } - }, - "type": "value" - }, + "value": [ { - "options": { - "pattern": "^((?!SIMPLE).)*$", - "result": { - "color": "yellow", - "index": 1 - } - }, - "type": "regex" + "targetBlank": true, + "title": "Perfmon - Tempdb % Free Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=89" } ] } @@ -5350,107 +5512,68 @@ { "matcher": { "id": "byName", - "options": "log_reuse_wait_desc" + "options": "log_size_mb" }, "properties": [ { "id": "displayName", - "value": "Log Reuse Wait Desc" + "value": "Log Size" }, { - "id": "custom.cellOptions", - "value": { - "type": "color-text" - } + "id": "unit", + "value": "mbytes" }, { - "id": "mappings", + "id": "links", "value": [ { - "options": { - "NOTHING": { - "color": "green", - "index": 0 - } - }, - "type": "value" - }, - { - "options": { - "pattern": "^((?!NOTHING).)*$", - "result": { - "color": "yellow", - "index": 1 - } - }, - "type": "regex" + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" } ] - }, - { - "id": "custom.minWidth", - "value": 180 } ] }, { "matcher": { "id": "byName", - "options": "exists_valid_autogrowing_file" + "options": "log_used_mb" }, "properties": [ { "id": "displayName", - "value": "Autogrow" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-text" - } - }, - { - "id": "unit", - "value": "bool_on_off" + "value": "Log Used" }, { - "id": "mappings", + "id": "links", "value": [ { - "options": { - "match": "true", - "result": { - "color": "green", - "index": 0, - "text": "Yes" - } - }, - "type": "special" - }, - { - "options": { - "match": "false", - "result": { - "color": "yellow", - "index": 1, - "text": "No" - } - }, - "type": "special" + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" } ] + }, + { + "id": "unit", + "value": "mbytes" } ] }, { "matcher": { "id": "byName", - "options": "thresholds_validated" + "options": "log_used_pct" }, "properties": [ { "id": "displayName", - "value": "Attention Needed" + "value": "Log Used %" + }, + { + "id": "unit", + "value": "percent" }, { "id": "custom.cellOptions", @@ -5459,246 +5582,147 @@ } }, { - "id": "unit", - "value": "bool" - }, - { - "id": "mappings", - "value": [ - { - "options": { - "match": "true", - "result": { - "color": "yellow", - "index": 1, - "text": "Yes" - } - }, - "type": "special" - }, - { - "options": { - "match": "false", - "result": { - "color": "green", - "index": 2, - "text": "No" - } + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null }, - "type": "special" - } - ] - } - ] - } - ] - }, - "gridPos": { - "h": 11, - "w": 24, - "x": 0, - "y": 441 - }, - "id": 192, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "enablePagination": true, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true - }, - "pluginVersion": "10.1.0", - "targets": [ - { - "dataset": "AdventureWorks2019", - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "editorMode": "code", - "format": "table", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\r\n\r\nset @sql_instance = '$server';\r\n\r\n--set @params = N'';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor Dashboard Monitoring - Live - Distributed: TempdbSaver - log_space_consumers */\r\nset nocount on;\t\t\t\r\nselect [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [log_used_mb], [exists_valid_autogrowing_file], [log_used_pct], \r\n --[log_used_pct_threshold], [log_used_gb_threshold], [spid], [transaction_start_time], [login_name], [program_name], [host_name], [host_process_id], [command], [additional_info], [action_taken], [sql_text], [is_pct_threshold_valid], [is_gb_threshold_valid], [threshold_condition], */\r\n [thresholds_validated]\r\nfrom dbo.log_space_consumers lsc\r\nwhere 1=1\r\nand lsc.collection_time = (select top 1 i.collection_time from dbo.log_space_consumers i order by i.collection_time desc)\r\norder by [log_size_mb] desc, database_name\r\n\"\r\nset quoted_identifier on;\r\n--print @sql\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql, @params;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params;", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } - } - ], - "title": "LogSaver - log_space_consumers", - "type": "table" - } - ], - "title": "LogSaver - Latest @ ${log_space_consumers_collection_time_utc:date:YYYY-MM-DD HH.mm}", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 25 - }, - "id": 140, - "panels": [ - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "", - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 10, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "never", - "spanNulls": true, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "min": 0, - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "transparent" + { + "color": "#EAB839", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 + } + ] + } } ] }, - "unit": "short" - }, - "overrides": [ { "matcher": { "id": "byName", - "options": "winsock rejected connections/sec" + "options": "version_store_mb" }, "properties": [ { - "id": "color", - "value": { - "fixedColor": "red", - "mode": "fixed" - } + "id": "displayName", + "value": "Version Store" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Version Store Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=90" + } + ] } ] }, { "matcher": { "id": "byName", - "options": "user connections" + "options": "version_store_pct" }, "properties": [ + { + "id": "displayName", + "value": "Version Store %" + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, { "id": "thresholds", "value": { "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", - "value": 500 + "value": 70 + }, + { + "color": "orange", + "value": 80 }, { "color": "red", - "value": 1000 + "value": 90 } ] } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - VersionStore Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=90" + } + ] } ] } ] }, "gridPos": { - "h": 8, + "h": 4, "w": 24, "x": 0, - "y": 257 + "y": 72 }, - "id": 138, + "id": 190, "options": { - "legend": { - "calcs": [ - "max", - "p99" + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "desc" - } + "showHeader": true }, "pluginVersion": "11.3.1", "targets": [ { - "alias": "", + "dataset": "AdventureWorks2019", "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, "editorMode": "code", - "format": "time_series", + "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\n\nselect pc.collection_time_utc as time, \n\t\t(case when counter = 'rejected connections/sec' then 'winsock rejected connections/sec' else counter end) as metric,\n\t--object, counter as metric, instance\n\tpc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand host_name = '$perfmon_host_name' \n and ( (object = 'microsoft winsock bsp' and counter = 'rejected connections/sec')\n or (pc.object = (@object_name+':general statistics') and pc.counter in ('user connections'))\n\t ) \norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\r\n\r\nset @sql_instance = '$server';\r\n\r\n--set @params = N'';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor Dashboard Monitoring - Live - Distributed: TempdbSaver - tempdb_space_usage */\r\nset nocount on;\r\nif exists ( select 1/0 from dbo.tempdb_space_consumers tsc with (nolock)\r\n where 1=1 \r\n and tsc.collection_time = (select top 1 tsu.collection_time from tempdb_space_usage tsu order by tsu.collection_time desc)\r\n and (tsc.size_bytes > 0 or tsc.is_snapshot = 1)\r\n )\r\nbegin\t\r\n select [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), [usage_rank], [spid], [login_name], \r\n [program_name], [host_name], [host_process_id], [is_active_session], [open_transaction_count], [transaction_isolation_level], [size_bytes], \r\n [transaction_begin_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), transaction_begin_time), [is_snapshot], [log_bytes], [log_rsvd], [action_taken] \r\n from dbo.tempdb_space_consumers tsc \r\n where 1=1 \r\n and tsc.collection_time = (select top 1 tsu.collection_time from tempdb_space_usage tsu order by tsu.collection_time desc)\r\n and (tsc.size_bytes > 0 or tsc.is_snapshot = 1)\r\nend\r\nELSE\r\n select 'No tempdb consumers record in latest snapshot.' as [No Result]\r\n\"\r\nset quoted_identifier on;\r\n--print @sql\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql, @params;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params;", "refId": "A", "sql": { "columns": [ @@ -5719,12 +5743,11 @@ } } ], - "title": "microsoft winsock bsp -> rejected connections/sec", - "type": "timeseries" + "title": "TempdbSaver - tempdb_space_consumers", + "type": "table" } ], - "repeat": "datasource", - "title": "SQL Connections & Winsock Rejections", + "title": "TempdbSaver - Latest @ ${tempdb_space_usage_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -5735,32 +5758,34 @@ "x": 0, "y": 26 }, - "id": 117, + "id": 191, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "description": "select [collection_time], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [log_used_mb], [exists_valid_autogrowing_file], [log_used_pct], [log_used_pct_threshold], [log_used_gb_threshold], [spid], [transaction_start_time], [login_name], [program_name], [host_name], [host_process_id], [command], [additional_info], [action_taken], [sql_text], [is_pct_threshold_valid], [is_gb_threshold_valid], [threshold_condition], [thresholds_validated]\nfrom dbo.log_space_consumers lsc\nwhere 1=1\nand lsc.collection_time = (select top 1 i.collection_time from dbo.log_space_consumers i order by i.collection_time desc)", "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "custom": { - "align": "auto", + "align": "center", "cellOptions": { "type": "auto" }, "filterable": true, - "inspect": false + "inspect": true }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -5773,310 +5798,405 @@ { "matcher": { "id": "byName", - "options": "sql_command" + "options": "collection_time_utc" }, "properties": [ { - "id": "custom.width", - "value": 352 + "id": "displayName", + "value": "Collection Time" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple", + "value": null + } + ] + } } ] }, { "matcher": { "id": "byName", - "options": "program_name" + "options": "log_size_mb" }, "properties": [ { - "id": "custom.width", - "value": 169 + "id": "displayName", + "value": "Log Size" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" + } + ] } ] }, { "matcher": { "id": "byName", - "options": "session_id" + "options": "log_used_mb" }, "properties": [ { - "id": "custom.width", - "value": 108 + "id": "displayName", + "value": "Log Used" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=tempdb&viewPanel=38" + } + ] + }, + { + "id": "unit", + "value": "mbytes" } ] }, { "matcher": { "id": "byName", - "options": "CPU" + "options": "log_used_pct" }, "properties": [ + { + "id": "displayName", + "value": "Log Used %" + }, { "id": "unit", - "value": "ms" + "value": "percent" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "#EAB839", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log % Usage Trend", + "url": "d/distributed_perfmon?var-server=$server&var-database=${__data.fields.database_name}&viewPanel=123" + } + ] } ] }, { "matcher": { "id": "byName", - "options": "granted_memory_kb" + "options": "database_name" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "displayName", + "value": "Database" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "custom.align", + "value": "left" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue", + "value": null + } + ] + } }, { - "id": "displayName", - "value": "granted_memory" + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space - Database Files Usage", + "url": "d/disk_space?var-server=${server}&var-database=${__data.fields.database_name}&viewPanel=30" + } + ] } ] }, { "matcher": { "id": "byName", - "options": "reads_kb" + "options": "recovery_model" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "displayName", + "value": "Recovery Model" }, { - "id": "displayName", - "value": "reads" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "writes" - }, - "properties": [ + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, { - "id": "unit", - "value": "short" + "id": "mappings", + "value": [ + { + "options": { + "SIMPLE": { + "color": "green", + "index": 0 + } + }, + "type": "value" + }, + { + "options": { + "pattern": "^((?!SIMPLE).)*$", + "result": { + "color": "yellow", + "index": 1 + } + }, + "type": "regex" + } + ] } ] }, { "matcher": { "id": "byName", - "options": "tempdb_allocations_kb" + "options": "log_reuse_wait_desc" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "displayName", + "value": "Log Reuse Wait Desc" }, { - "id": "displayName", - "value": "tempdb_allocations" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "tempdb_current_kb" - }, - "properties": [ + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, { - "id": "unit", - "value": "kbytes" + "id": "mappings", + "value": [ + { + "options": { + "NOTHING": { + "color": "green", + "index": 0 + } + }, + "type": "value" + }, + { + "options": { + "pattern": "^((?!NOTHING).)*$", + "result": { + "color": "yellow", + "index": 1 + } + }, + "type": "regex" + } + ] }, { - "id": "displayName", - "value": "tempdb_current" + "id": "custom.minWidth", + "value": 180 } ] }, { "matcher": { "id": "byName", - "options": "used_memory_kb" + "options": "exists_valid_autogrowing_file" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "displayName", + "value": "Autogrow" }, { - "id": "displayName", - "value": "used_memory" - } - ] - } - ] - }, - "gridPos": { - "h": 7, - "w": 24, - "x": 0, - "y": 258 - }, - "id": 115, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true, - "sortBy": [] - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "editorMode": "code", - "format": "table", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @duration int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @duration = case when ltrim(rtrim('$duration')) <> '' then $duration else 0 end; \nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @duration int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nif exists ( select * from dbo.WhoIsActive w with (nolock)\n where w.collection_time = (select max(i.collection_time) from dbo.WhoIsActive i) \n and datediff(minute,start_time,collection_time) >= @duration )\nbegin\n ;with t_WhoIsActive as (\n select [collection_time], w.session_id, /* = convert(varchar,w.collection_time,120) */\n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, w.granted_memory, w.used_memory, w.open_tran_count, w.wait_info,\n sql_command = case when w.sql_command is not null then left(replace(replace(convert(nvarchar(max),w.sql_command),char(13)+char(10),''),'= @duration\n )\n select [collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), w.session_id,\n [ddd hh:mm:ss] = right('0000'+convert(varchar, duration_s/86400),3)+ ' '+convert(varchar,dateadd(SECOND,duration_s,'1900-01-01 00:00:00'),108), \n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, \n granted_memory_kb = (w.granted_memory * 8.0), w.open_tran_count, w.wait_info, w.sql_command, w.blocked_session_count, \n w.blocking_session_id, [reads_kb] = (w.[reads]*8.0), w.writes, [tempdb_allocations_kb] = (w.tempdb_allocations*8.0), \n tempdb_current_kb = (tempdb_current*8.0), w.tasks, w.percent_complete, [used_memory_kb] = (w.used_memory*8.0),\n start_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [start_time])\n\t\t\t \n from t_WhoIsActive w\n order by w.collection_time DESC, w.start_time ASC\nend\nELSE\n select 'No long running query found for time window' as [No Result]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @duration;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @duration;", - "refId": "A", - "sql": { - "columns": [ + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ + "id": "unit", + "value": "bool_on_off" + }, { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } - } - ], - "title": "WhoIsActive Data", - "type": "table" - } - ], - "repeat": "datasource", - "title": "Long Running Queries", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 27 - }, - "id": 29, - "panels": [ - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 10, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "never", - "spanNulls": true, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "line+area" - } + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "green", + "index": 0, + "text": "Yes" + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "yellow", + "index": 1, + "text": "No" + } + }, + "type": "special" + } + ] + } + ] }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ + { + "matcher": { + "id": "byName", + "options": "thresholds_validated" + }, + "properties": [ { - "color": "red" + "id": "displayName", + "value": "Attention Needed" }, { - "color": "transparent", - "value": 600 + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "unit", + "value": "bool" + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "yellow", + "index": 1, + "text": "Yes" + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "green", + "index": 2, + "text": "No" + } + }, + "type": "special" + } + ] } ] - }, - "unit": "s" - }, - "overrides": [] + } + ] }, "gridPos": { - "h": 10, + "h": 11, "w": 24, "x": 0, - "y": 259 + "y": 77 }, - "id": 8, + "id": 192, "options": { - "legend": { - "calcs": [ - "lastNotNull", - "min" + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "bottom", - "showLegend": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "desc" - } + "showHeader": true }, "pluginVersion": "11.3.1", "targets": [ { - "alias": "", + "dataset": "AdventureWorks2019", "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, "editorMode": "code", - "format": "time_series", + "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' \n\t\t\t\t\t\t then 'sqlserver' else LOWER('$perfmon_service_name') end);\n\nselect pc.collection_time_utc as time\n\t,counter as metric\n\t--,object, counter, instance\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':buffer manager')\nand pc.counter = 'page life expectancy'\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\r\n\r\nset @sql_instance = '$server';\r\n\r\n--set @params = N'';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor Dashboard Monitoring - Live - Distributed: TempdbSaver - log_space_consumers */\r\nset nocount on;\t\t\t\r\nselect [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time), [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [log_used_mb], [exists_valid_autogrowing_file], [log_used_pct], \r\n --[log_used_pct_threshold], [log_used_gb_threshold], [spid], [transaction_start_time], [login_name], [program_name], [host_name], [host_process_id], [command], [additional_info], [action_taken], [sql_text], [is_pct_threshold_valid], [is_gb_threshold_valid], [threshold_condition], */\r\n [thresholds_validated]\r\nfrom dbo.log_space_consumers lsc\r\nwhere 1=1\r\nand lsc.collection_time = (select top 1 i.collection_time from dbo.log_space_consumers i order by i.collection_time desc)\r\norder by [log_size_mb] desc, database_name\r\n\"\r\nset quoted_identifier on;\r\n--print @sql\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql, @params;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params;", "refId": "A", "sql": { "columns": [ @@ -6097,12 +6217,11 @@ } } ], - "title": "Page Life Expectancy", - "type": "timeseries" + "title": "LogSaver - log_space_consumers", + "type": "table" } ], - "repeat": "datasource", - "title": "Trend - Page Life Expectancy", + "title": "LogSaver - Latest @ ${log_space_consumers_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -6111,16 +6230,16 @@ "h": 1, "w": 24, "x": 0, - "y": 28 + "y": 27 }, - "id": 27, + "id": 140, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", + "description": "", "fieldConfig": { "defaults": { "color": { @@ -6130,7 +6249,6 @@ "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", - "axisGridShow": true, "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, @@ -6148,8 +6266,7 @@ "lineWidth": 1, "pointSize": 5, "scaleDistribution": { - "log": 10, - "type": "log" + "type": "linear" }, "showPoints": "never", "spanNulls": true, @@ -6162,39 +6279,80 @@ } }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" - }, - { - "color": "red", - "value": 80 + "color": "transparent" } ] }, "unit": "short" }, - "overrides": [] + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "winsock rejected connections/sec" + }, + "properties": [ + { + "id": "color", + "value": { + "fixedColor": "red", + "mode": "fixed" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "user connections" + }, + "properties": [ + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 500 + }, + { + "color": "red", + "value": 1000 + } + ] + } + } + ] + } + ] }, "gridPos": { - "h": 7, + "h": 8, "w": 24, "x": 0, - "y": 260 + "y": 988 }, - "id": 18, + "id": 138, "options": { "legend": { "calcs": [ - "lastNotNull", "max", "p99" ], "displayMode": "table", - "placement": "bottom", - "showLegend": true + "placement": "right", + "showLegend": true, + "sortBy": "Mean", + "sortDesc": true }, "tooltip": { "mode": "multi", @@ -6212,7 +6370,7 @@ "editorMode": "code", "format": "time_series", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':sql statistics') \nand pc.counter = 'batch requests/sec'\nand pc.value <> 0.0\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\n\nselect pc.collection_time_utc as time, \n\t\t(case when counter = 'rejected connections/sec' then 'winsock rejected connections/sec' else counter end) as metric,\n\t--object, counter as metric, instance\n\tpc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand host_name = '$perfmon_host_name' \n and ( (object = 'microsoft winsock bsp' and counter = 'rejected connections/sec')\n or (pc.object = (@object_name+':general statistics') and pc.counter in ('user connections'))\n\t ) \norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -6233,12 +6391,12 @@ } } ], - "title": "Batch Requests Per Second", + "title": "microsoft winsock bsp -> rejected connections/sec", "type": "timeseries" } ], "repeat": "datasource", - "title": "Trend - Batch Request/Sec", + "title": "SQL Connections & Winsock Rejections", "type": "row" }, { @@ -6247,16 +6405,15 @@ "h": 1, "w": 24, "x": 0, - "y": 29 + "y": 28 }, - "id": 126, + "id": 117, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from sys.dm_exec_requests r where blocking_session_id <> 0", "fieldConfig": { "defaults": { "color": { @@ -6267,11 +6424,10 @@ "cellOptions": { "type": "auto" }, + "filterable": true, "inspect": false }, "mappings": [], - "max": 100, - "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -6279,118 +6435,162 @@ "color": "green" }, { - "color": "dark-orange", - "value": 500 + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_command" + }, + "properties": [ + { + "id": "custom.width", + "value": 352 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "program_name" + }, + "properties": [ + { + "id": "custom.width", + "value": 169 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "session_id" + }, + "properties": [ + { + "id": "custom.width", + "value": 108 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "CPU" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "granted_memory_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" }, { - "color": "dark-red", - "value": 700 + "id": "displayName", + "value": "granted_memory" } ] }, - "unit": "none" - }, - "overrides": [] - }, - "gridPos": { - "h": 7, - "w": 24, - "x": 0, - "y": 261 - }, - "id": 124, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, login_name, status, client_net_address, client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des on des.session_id = dec.session_id\ngroup by host_name, login_name, status, client_net_address, client_interface_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" - } - ], - "title": "Connections", - "type": "table" - }, - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "select * from sys.dm_exec_connections", - "fieldConfig": { - "defaults": { - "color": { - "mode": "thresholds" + { + "matcher": { + "id": "byName", + "options": "reads_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "reads" + } + ] }, - "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" + { + "matcher": { + "id": "byName", + "options": "writes" }, - "filterable": true, - "inspect": false + "properties": [ + { + "id": "unit", + "value": "short" + } + ] }, - "mappings": [], - "min": 0, - "thresholds": { - "mode": "absolute", - "steps": [ + { + "matcher": { + "id": "byName", + "options": "tempdb_allocations_kb" + }, + "properties": [ { - "color": "green" + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "tempdb_allocations" } ] }, - "unit": "none" - }, - "overrides": [ { "matcher": { - "id": "byType", - "options": "string" + "id": "byName", + "options": "tempdb_current_kb" }, "properties": [ { - "id": "custom.width" + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "tempdb_current" } ] }, { "matcher": { - "id": "byType", - "options": "number" + "id": "byName", + "options": "used_memory_kb" }, "properties": [ { - "id": "custom.width", - "value": 100 + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "used_memory" } ] } ] }, "gridPos": { - "h": 6, - "w": 8, + "h": 7, + "w": 24, "x": 0, - "y": 309 + "y": 989 }, - "id": 131, + "id": 115, "options": { "cellHeight": "sm", "footer": { @@ -6412,90 +6612,130 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "editorMode": "code", "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\nwhere len(ltrim(rtrim(client_interface_name))) > 0\ngroup by client_interface_name\norder by counts desc, client_interface_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @duration int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @duration = case when ltrim(rtrim('$duration')) <> '' then $duration else 0 end; \nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @duration int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nif exists ( select * from dbo.WhoIsActive w with (nolock)\n where w.collection_time = (select max(i.collection_time) from dbo.WhoIsActive i) \n and datediff(minute,start_time,collection_time) >= @duration )\nbegin\n ;with t_WhoIsActive as (\n select [collection_time], w.session_id, /* = convert(varchar,w.collection_time,120) */\n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, w.granted_memory, w.used_memory, w.open_tran_count, w.wait_info,\n sql_command = case when w.sql_command is not null then left(replace(replace(convert(nvarchar(max),w.sql_command),char(13)+char(10),''),'= @duration\n )\n select [collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), w.session_id,\n [ddd hh:mm:ss] = right('0000'+convert(varchar, duration_s/86400),3)+ ' '+convert(varchar,dateadd(SECOND,duration_s,'1900-01-01 00:00:00'),108), \n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, \n granted_memory_kb = (w.granted_memory * 8.0), w.open_tran_count, w.wait_info, w.sql_command, w.blocked_session_count, \n w.blocking_session_id, [reads_kb] = (w.[reads]*8.0), w.writes, [tempdb_allocations_kb] = (w.tempdb_allocations*8.0), \n tempdb_current_kb = (tempdb_current*8.0), w.tasks, w.percent_complete, [used_memory_kb] = (w.used_memory*8.0),\n start_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [start_time])\n\t\t\t \n from t_WhoIsActive w\n order by w.collection_time DESC, w.start_time ASC\nend\nELSE\n select 'No long running query found for time window' as [No Result]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @duration;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @duration;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "Connections by Interface", + "title": "WhoIsActive Data", "type": "table" - }, + } + ], + "repeat": "datasource", + "title": "Long Running Queries", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 29 + }, + "id": 29, + "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from sys.dm_exec_connections", + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "filterable": true, - "inspect": false + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "line+area" + } }, "mappings": [], - "min": 0, "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" - } - ] - }, - "unit": "none" - }, - "overrides": [ - { - "matcher": { - "id": "byType", - "options": "string" - }, - "properties": [ + "color": "red" + }, { - "id": "custom.width" + "color": "transparent", + "value": 600 } ] }, - { - "matcher": { - "id": "byType", - "options": "number" - }, - "properties": [ - { - "id": "custom.width", - "value": 100 - } - ] - } - ] + "unit": "s" + }, + "overrides": [] }, "gridPos": { - "h": 12, - "w": 8, - "x": 8, - "y": 309 + "h": 10, + "w": 24, + "x": 0, + "y": 990 }, - "id": 128, + "id": 8, "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" + "legend": { + "calcs": [ + "lastNotNull", + "min" ], - "show": false + "displayMode": "table", + "placement": "bottom", + "showLegend": true }, - "showHeader": true, - "sortBy": [] + "tooltip": { + "mode": "multi", + "sort": "desc" + } }, "pluginVersion": "11.3.1", "targets": [ @@ -6505,90 +6745,133 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by host_name\norder by counts desc, host_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' \n\t\t\t\t\t\t then 'sqlserver' else LOWER('$perfmon_service_name') end);\n\nselect pc.collection_time_utc as time\n\t,counter as metric\n\t--,object, counter, instance\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':buffer manager')\nand pc.counter = 'page life expectancy'\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "Host Connections", - "type": "table" - }, + "title": "Page Life Expectancy", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Trend - Page Life Expectancy", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 30 + }, + "id": 27, + "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from sys.dm_exec_connections", + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisGridShow": true, + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "filterable": true, - "inspect": false + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } }, "mappings": [], - "min": 0, "thresholds": { "mode": "absolute", "steps": [ { "color": "green" - } - ] - }, - "unit": "none" - }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "login_name" - }, - "properties": [ + }, { - "id": "custom.width" + "color": "red", + "value": 80 } ] }, - { - "matcher": { - "id": "byName", - "options": "counts" - }, - "properties": [ - { - "id": "custom.width", - "value": 100 - } - ] - } - ] + "unit": "short" + }, + "overrides": [] }, "gridPos": { - "h": 12, - "w": 8, - "x": 16, - "y": 309 + "h": 7, + "w": 24, + "x": 0, + "y": 991 }, - "id": 129, + "id": 18, "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" + "legend": { + "calcs": [ + "lastNotNull", + "max", + "p99" ], - "show": false + "displayMode": "table", + "placement": "bottom", + "showLegend": true }, - "showHeader": true, - "sortBy": [] + "tooltip": { + "mode": "multi", + "sort": "desc" + } }, "pluginVersion": "11.3.1", "targets": [ @@ -6598,20 +6881,54 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect login_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by login_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':sql statistics') \nand pc.counter = 'batch requests/sec'\nand pc.value <> 0.0\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "Login Connections", - "type": "table" - }, + "title": "Batch Requests Per Second", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Trend - Batch Request/Sec", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 31 + }, + "id": 126, + "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from sys.dm_exec_connections", + "description": "select * from sys.dm_exec_requests r where blocking_session_id <> 0", "fieldConfig": { "defaults": { "color": { @@ -6622,43 +6939,38 @@ "cellOptions": { "type": "auto" }, - "filterable": true, "inspect": false }, "mappings": [], + "max": 100, "min": 0, "thresholds": { "mode": "absolute", "steps": [ { "color": "green" + }, + { + "color": "dark-orange", + "value": 500 + }, + { + "color": "dark-red", + "value": 700 } ] }, "unit": "none" }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "counts" - }, - "properties": [ - { - "id": "custom.width", - "value": 100 - } - ] - } - ] + "overrides": [] }, "gridPos": { - "h": 6, - "w": 8, + "h": 7, + "w": 24, "x": 0, - "y": 315 + "y": 992 }, - "id": 130, + "id": 124, "options": { "cellHeight": "sm", "footer": { @@ -6669,8 +6981,7 @@ ], "show": false }, - "showHeader": true, - "sortBy": [] + "showHeader": true }, "pluginVersion": "11.3.1", "targets": [ @@ -6681,73 +6992,34 @@ "uid": "${sqlmonitor_datasource}" }, "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect status, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by status\norder by counts desc, status\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, login_name, status, client_net_address, client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des on des.session_id = dec.session_id\ngroup by host_name, login_name, status, client_net_address, client_interface_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A" } ], - "title": "Connections By Status", + "title": "Connections", "type": "table" - } - ], - "repeat": "datasource", - "title": "SQL Connections - Distribution", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 30 - }, - "id": 79, - "panels": [ + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "bars", - "fillOpacity": 100, - "gradientMode": "hue", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 1, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -6756,46 +7028,53 @@ } ] }, - "unit": "s" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byType", + "options": "string" }, "properties": [ { - "id": "unit", - "value": "s" + "id": "custom.width" + } + ] + }, + { + "matcher": { + "id": "byType", + "options": "number" + }, + "properties": [ + { + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 11, - "w": 24, + "h": 6, + "w": 8, "x": 0, - "y": 40 + "y": 1040 }, - "id": 144, + "id": 131, "options": { - "legend": { - "calcs": [ - "last", - "mean" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": false + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -6805,81 +7084,35 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 100;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc\n\toption(recompile);\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select *\n--from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [cpu_s] = sum(cpu_time_seconds), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, cpu_s, counts\n\t\t,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[CpuRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY cpu_s DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, (case when [cpu_s__delta] < 0 then 0 else [cpu_s__delta] end) as value\nfrom #os_task_list\nwhere CpuRank <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by collection_time_utc, CpuRank, cpu_s desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\nwhere len(ltrim(rtrim(client_interface_name))) > 0\ngroup by client_interface_name\norder by counts desc, client_interface_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Process - CPU Time - Histrogram - $cpu_delta_minutes Minute Interval", - "type": "timeseries" + "title": "Connections by Interface", + "type": "table" }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, - "lineWidth": 1, - "pointSize": 3, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -6888,46 +7121,53 @@ } ] }, - "unit": "s" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byType", + "options": "string" }, "properties": [ { - "id": "unit", - "value": "s" + "id": "custom.width" + } + ] + }, + { + "matcher": { + "id": "byType", + "options": "number" + }, + "properties": [ + { + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 10, - "w": 24, - "x": 0, - "y": 118 + "h": 12, + "w": 8, + "x": 8, + "y": 1040 }, - "id": 145, + "id": 128, "options": { - "legend": { - "calcs": [ - "last", - "mean" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -6937,93 +7177,35 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 100;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc;\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select *\n--from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [cpu_s] = sum(cpu_time_seconds), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, cpu_s, counts\n\t\t,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[CpuRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY cpu_s DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, (case when [cpu_s__delta] < 0 then 0 else [cpu_s__delta] end) as value\nfrom #os_task_list\nwhere CpuRank <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by collection_time_utc, CpuRank, cpu_s desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by host_name\norder by counts desc, host_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Process - CPU Time - Utilization - $cpu_delta_minutes Minute Interval", - "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "OS Tasks (Processes) - CPU Utilization", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 31 - }, - "id": 82, - "panels": [ + "title": "Host Connections", + "type": "table" + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "bars", - "fillOpacity": 100, - "gradientMode": "hue", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 1, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -7032,46 +7214,53 @@ } ] }, - "unit": "kbytes" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byName", + "options": "login_name" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "custom.width" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "counts" + }, + "properties": [ + { + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 10, - "w": 24, - "x": 0, - "y": 41 + "h": 12, + "w": 8, + "x": 16, + "y": 1040 }, - "id": 146, + "id": 129, "options": { - "legend": { - "calcs": [ - "mean", - "max" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -7081,81 +7270,35 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 10;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc\n\toption(recompile);\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select * from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [memory_kb] = sum([memory_kb]), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, [memory_kb], counts\n\t\t--,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[RowRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY [memory_kb] DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, [memory_kb] as value\nfrom #os_task_list\nwhere [RowRank] <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by [time], [RowRank], [memory_kb] desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect login_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by login_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Processes - Memory - Histrogram - $cpu_delta_minutes Minute Interval", - "type": "timeseries" + "title": "Login Connections", + "type": "table" }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, - "lineWidth": 1, - "pointSize": 3, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -7164,46 +7307,42 @@ } ] }, - "unit": "kbytes" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byName", + "options": "counts" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 10, - "w": 24, + "h": 6, + "w": 8, "x": 0, - "y": 98 + "y": 1046 }, - "id": 147, + "id": 130, "options": { - "legend": { - "calcs": [ - "mean", - "max" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -7213,36 +7352,17 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 10;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc\n\toption(recompile);\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select * from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [memory_kb] = sum([memory_kb]), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, [memory_kb], counts\n\t\t--,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[RowRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY [memory_kb] DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, [memory_kb] as value\nfrom #os_task_list\nwhere [RowRank] <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by [time], [RowRank], [memory_kb] desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect status, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by status\norder by counts desc, status\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Processes - Memory Utilization - $cpu_delta_minutes Minute Interval", - "type": "timeseries" + "title": "Connections By Status", + "type": "table" } ], - "repeat": "cpu_delta_minutes", - "title": "OS Tasks (Processes) - Memory Utilization", + "repeat": "datasource", + "title": "SQL Connections - Distribution", "type": "row" }, { @@ -7434,7 +7554,7 @@ "h": 8, "w": 24, "x": 0, - "y": 874 + "y": 1681 }, "id": 86, "options": { @@ -8089,7 +8209,7 @@ "h": 16, "w": 24, "x": 0, - "y": 875 + "y": 1682 }, "id": 185, "options": { @@ -8323,7 +8443,7 @@ "h": 16, "w": 24, "x": 0, - "y": 111 + "y": 918 }, "id": 173, "options": { @@ -8429,7 +8549,7 @@ "h": 15, "w": 24, "x": 0, - "y": 112 + "y": 919 }, "id": 133, "options": { @@ -8508,7 +8628,7 @@ "h": 7, "w": 24, "x": 0, - "y": 923 + "y": 1730 }, "id": 119, "options": { @@ -8733,8 +8853,8 @@ }, { "current": { - "text": "LAB", - "value": "LAB" + "text": "", + "value": "" }, "datasource": { "type": "mssql", @@ -8795,8 +8915,8 @@ }, { "current": { - "text": "15.0.4455.2", - "value": "15.0.4455.2" + "text": "17.0.925.4", + "value": "17.0.925.4" }, "datasource": { "type": "mssql", @@ -8867,8 +8987,8 @@ }, { "current": { - "text": "1763105136058", - "value": "1763105136058" + "text": "1768974398217", + "value": "1768974398217" }, "datasource": { "type": "mssql", @@ -8885,38 +9005,6 @@ "regex": "", "type": "query" }, - { - "current": { - "text": "dbo.vw_os_task_list", - "value": "dbo.vw_os_task_list" - }, - "hide": 2, - "label": "OS Tasks Table Name", - "name": "os_task_list_table_name", - "query": "dbo.vw_os_task_list", - "skipUrlSync": true, - "type": "constant" - }, - { - "current": { - "text": "1763105163871", - "value": "1763105163871" - }, - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select top 1 collection_time_utc\nfrom $os_task_list_table_name with (nolock)\nwhere host_name = @perfmon_host_name\norder by collection_time_utc desc;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "hide": 2, - "includeAll": false, - "label": "Collection Time UTC", - "name": "os_task_list_collection_time_utc", - "options": [], - "query": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select top 1 collection_time_utc\nfrom $os_task_list_table_name with (nolock)\nwhere host_name = @perfmon_host_name\norder by collection_time_utc desc;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "refresh": 2, - "regex": "", - "type": "query" - }, { "current": { "text": "dbo.WhoIsActive", @@ -8931,8 +9019,8 @@ }, { "current": { - "text": "1763105162130", - "value": "1763105162130" + "text": "1768974379500", + "value": "1768974379500" }, "datasource": { "type": "mssql", @@ -8990,8 +9078,8 @@ }, { "current": { - "text": "1763101975480", - "value": "1763101975480" + "text": "1768866330053", + "value": "1768866330053" }, "datasource": { "type": "mssql", @@ -9010,8 +9098,8 @@ }, { "current": { - "text": "1763105160003", - "value": "1763105160003" + "text": "1768974440983", + "value": "1768974440983" }, "datasource": { "type": "mssql", @@ -9030,8 +9118,8 @@ }, { "current": { - "text": "8", - "value": "8" + "text": "16", + "value": "16" }, "datasource": { "type": "mssql", @@ -9050,8 +9138,8 @@ }, { "current": { - "text": "1", - "value": "1" + "text": "6", + "value": "6" }, "datasource": { "type": "mssql", @@ -9070,8 +9158,8 @@ }, { "current": { - "text": "92", - "value": "92" + "text": "84", + "value": "84" }, "datasource": { "type": "mssql", @@ -9090,8 +9178,8 @@ }, { "current": { - "text": "94", - "value": "94" + "text": "18", + "value": "18" }, "datasource": { "type": "mssql", @@ -9110,8 +9198,8 @@ }, { "current": { - "text": "5", - "value": "5" + "text": "81", + "value": "81" }, "datasource": { "type": "mssql", @@ -9130,8 +9218,8 @@ }, { "current": { - "text": "475256", - "value": "475256" + "text": "414816", + "value": "414816" }, "datasource": { "type": "mssql", @@ -9222,8 +9310,8 @@ }, { "current": { - "text": "15328176", - "value": "15328176" + "text": "9158576", + "value": "9158576" }, "datasource": { "type": "mssql", @@ -9262,8 +9350,8 @@ }, { "current": { - "text": "3162040", - "value": "3162040" + "text": "5997756", + "value": "5997756" }, "datasource": { "type": "mssql", @@ -9342,8 +9430,8 @@ }, { "current": { - "text": "3216", - "value": "3216" + "text": "108142", + "value": "108142" }, "datasource": { "type": "mssql", @@ -9362,8 +9450,8 @@ }, { "current": { - "text": "15", - "value": "15" + "text": "17", + "value": "17" }, "datasource": { "type": "mssql", @@ -9382,8 +9470,8 @@ }, { "current": { - "text": "4455", - "value": "4455" + "text": "925", + "value": "925" }, "datasource": { "type": "mssql", @@ -9402,8 +9490,8 @@ }, { "current": { - "text": "17", - "value": "17" + "text": "19", + "value": "19" }, "datasource": { "type": "mssql", @@ -9442,8 +9530,8 @@ }, { "current": { - "text": "3", - "value": "3" + "text": "10", + "value": "10" }, "datasource": { "type": "mssql", @@ -9474,8 +9562,8 @@ }, { "current": { - "text": "1763103605525", - "value": "1763103605525" + "text": "1768974004419", + "value": "1768974004419" }, "datasource": { "type": "mssql", @@ -9612,8 +9700,8 @@ }, { "current": { - "text": "1763105101871", - "value": "1763105101871" + "text": "1768974302982", + "value": "1768974302982" }, "datasource": { "type": "mssql", @@ -9695,8 +9783,8 @@ }, { "current": { - "text": "1763105101444", - "value": "1763105101444" + "text": "1768974301136", + "value": "1768974301136" }, "datasource": { "type": "mssql", @@ -9715,8 +9803,8 @@ }, { "current": { - "text": "1763105101323", - "value": "1763105101323" + "text": "1768974301435", + "value": "1768974301435" }, "datasource": { "type": "mssql", @@ -9735,8 +9823,8 @@ }, { "current": { - "text": "8", - "value": "8" + "text": "9", + "value": "9" }, "datasource": { "type": "mssql", @@ -9755,8 +9843,8 @@ }, { "current": { - "text": "15", - "value": "15" + "text": "8", + "value": "8" }, "datasource": { "type": "mssql", @@ -9811,6 +9899,6 @@ "timezone": "browser", "title": "Monitoring - Live - Distributed", "uid": "distributed_live_dashboard", - "version": 24, + "version": 42, "weekStart": "" } \ No newline at end of file diff --git a/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Perfmon Counters - Quest Softwares - Distributed - INTERNAL.json b/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Perfmon Counters - Quest Softwares - Distributed - INTERNAL.json index 78782e7..46a833d 100644 --- a/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Perfmon Counters - Quest Softwares - Distributed - INTERNAL.json +++ b/Grafana-Dashboards/For-Internal-Use-Only/Monitoring - Perfmon Counters - Quest Softwares - Distributed - INTERNAL.json @@ -232,7 +232,7 @@ "h": 5, "w": 24, "x": 0, - "y": 9973 + "y": 18292 }, "id": 64, "options": { @@ -364,7 +364,7 @@ "h": 6, "w": 24, "x": 0, - "y": 2 + "y": 90314 }, "id": 26, "options": { @@ -465,7 +465,7 @@ "h": 11, "w": 24, "x": 0, - "y": 7246 + "y": 102391 }, "id": 58, "options": { @@ -587,7 +587,7 @@ "h": 7, "w": 24, "x": 0, - "y": 7257 + "y": 102402 }, "id": 60, "options": { @@ -709,7 +709,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -826,7 +827,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -838,7 +840,7 @@ "h": 11, "w": 24, "x": 0, - "y": 5608 + "y": 14 }, "id": 30, "options": { @@ -944,7 +946,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -960,7 +963,7 @@ "h": 8, "w": 24, "x": 0, - "y": 5619 + "y": 25 }, "id": 66, "options": { @@ -1096,7 +1099,7 @@ "h": 6, "w": 24, "x": 0, - "y": 4 + "y": 46903 }, "id": 105, "options": { @@ -1218,7 +1221,7 @@ "h": 15, "w": 24, "x": 0, - "y": 4843 + "y": 55347 }, "id": 106, "options": { @@ -1355,7 +1358,7 @@ "h": 5, "w": 24, "x": 0, - "y": 5 + "y": 18211 }, "id": 110, "options": { @@ -1363,7 +1366,7 @@ "calcs": [ "max", "p99", - "p98" + "last" ], "displayMode": "table", "placement": "right", @@ -1476,14 +1479,14 @@ "h": 6, "w": 24, "x": 0, - "y": 4825 + "y": 18216 }, "id": 112, "options": { "legend": { "calcs": [ "p99", - "p98" + "last" ], "displayMode": "table", "placement": "right", @@ -1531,6 +1534,122 @@ "title": "Logical Disk - Latency", "type": "timeseries" }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "Disk Transfer/Sec => The number of reads and writes per second to disk ", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 2, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + }, + "unit": "none" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 18222 + }, + "id": 157, + "options": { + "legend": { + "calcs": [ + "max", + "p99" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "99th %", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "none" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @perfmon_object varchar(125);\ndeclare @disk_drive varchar(255) = '$disk_drive';\ndeclare @database varchar(255) = '$database';\n\nset @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\nset @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @perfmon_object = '$perfmon_object';\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @perfmon_object varchar(125), @disk_drive varchar(255), @database varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect pc.collection_time_utc as time\n\t,instance+' ('+replace(counter,'disk ','')+')' as metric\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name \nand pc.object = 'logicaldisk' and pc.counter in ('disk transfers/sec')\n--and pc.object = 'logicaldisk' and pc.counter in ('disk reads/sec','disk writes/sec')\nand (pc.instance <> '_total' and pc.instance not like 'harddiskvolume%')\n\"+(case when @disk_drive is null then '-- ' else '' end)+\"and (pc.instance+'\\') = @disk_drive\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object, @disk_drive, @database;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object, @disk_drive, @database;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Logical Disk - IOPS", + "type": "timeseries" + }, { "datasource": { "type": "mssql", @@ -1596,7 +1715,7 @@ "h": 7, "w": 24, "x": 0, - "y": 4831 + "y": 18230 }, "id": 114, "options": { @@ -1731,7 +1850,7 @@ "h": 5, "w": 24, "x": 0, - "y": 6 + "y": 51137 }, "id": 34, "options": { @@ -1853,7 +1972,7 @@ "h": 6, "w": 24, "x": 0, - "y": 4199 + "y": 51142 }, "id": 68, "options": { @@ -1974,7 +2093,7 @@ "h": 7, "w": 24, "x": 0, - "y": 4205 + "y": 51148 }, "id": 69, "options": { @@ -2029,6 +2148,124 @@ ], "title": "Physical Disk - Throughput", "type": "timeseries" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "Disk Transfers/Sec => The number of disk reads and writes per second", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 2, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + }, + "unit": "none" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 51155 + }, + "id": 158, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "p99", + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "99th %", + "sortDesc": true + }, + "tooltip": { + "mode": "single", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @perfmon_object varchar(125);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @perfmon_object = '$perfmon_object';\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @perfmon_object varchar(125)';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect pc.collection_time_utc as time\n\t,instance+' ('+replace(counter,'disk ','')+')' as metric\n\t--,object, counter, instance\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name \nand pc.object = 'physicaldisk' and pc.counter in ('disk transfers/sec') and instance like '%:%'\n--and pc.object = 'physicaldisk' and pc.counter in ('disk bytes/sec','disk write bytes/sec','disk read bytes/sec')\nand pc.instance <> '_total'\norder by time\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object;\n\t\telse\n\t\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object;\nend", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Physical Disk - IOPS", + "type": "timeseries" } ], "repeat": "datasource", @@ -2110,7 +2347,7 @@ "h": 7, "w": 24, "x": 0, - "y": 7 + "y": 95152 }, "id": 71, "options": { @@ -2247,7 +2484,7 @@ "h": 15, "w": 24, "x": 0, - "y": 15 + "y": 95160 }, "id": 38, "options": { @@ -2375,7 +2612,7 @@ "h": 12, "w": 24, "x": 0, - "y": 30 + "y": 95175 }, "id": 115, "options": { @@ -2518,7 +2755,7 @@ "h": 9, "w": 24, "x": 0, - "y": 1897 + "y": 97042 }, "id": 119, "options": { @@ -2644,7 +2881,7 @@ "h": 10, "w": 24, "x": 0, - "y": 1906 + "y": 97051 }, "id": 121, "options": { @@ -2767,7 +3004,7 @@ "h": 15, "w": 24, "x": 0, - "y": 1916 + "y": 97061 }, "id": 123, "options": { @@ -2903,7 +3140,7 @@ "h": 10, "w": 24, "x": 0, - "y": 2666 + "y": 97811 }, "id": 22, "options": { @@ -3037,7 +3274,7 @@ "h": 6, "w": 24, "x": 0, - "y": 2677 + "y": 97822 }, "id": 126, "options": { @@ -3158,7 +3395,7 @@ "h": 7, "w": 24, "x": 0, - "y": 2683 + "y": 97828 }, "id": 14, "options": { @@ -3280,7 +3517,7 @@ "h": 17, "w": 24, "x": 0, - "y": 2690 + "y": 97835 }, "id": 124, "options": { @@ -3414,7 +3651,7 @@ "h": 13, "w": 24, "x": 0, - "y": 2956 + "y": 98101 }, "id": 153, "options": { @@ -3549,7 +3786,7 @@ "h": 9, "w": 24, "x": 0, - "y": 2970 + "y": 98115 }, "id": 77, "options": { @@ -3670,7 +3907,7 @@ "h": 12, "w": 24, "x": 0, - "y": 2979 + "y": 98124 }, "id": 128, "options": { @@ -3807,7 +4044,7 @@ "h": 6, "w": 24, "x": 0, - "y": 3116 + "y": 98261 }, "id": 46, "options": { @@ -3928,7 +4165,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3122 + "y": 98267 }, "id": 129, "options": { @@ -4049,7 +4286,7 @@ "h": 7, "w": 24, "x": 0, - "y": 3130 + "y": 98275 }, "id": 78, "options": { @@ -4171,7 +4408,7 @@ "h": 7, "w": 24, "x": 0, - "y": 3137 + "y": 98282 }, "id": 130, "options": { @@ -4308,7 +4545,7 @@ "h": 5, "w": 24, "x": 0, - "y": 19640 + "y": 114785 }, "id": 134, "options": { @@ -4429,7 +4666,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19718 + "y": 114863 }, "id": 137, "options": { @@ -4550,7 +4787,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19724 + "y": 114869 }, "id": 135, "options": { @@ -4671,7 +4908,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19730 + "y": 114875 }, "id": 136, "options": { @@ -4806,7 +5043,7 @@ "h": 5, "w": 11, "x": 0, - "y": 3242 + "y": 98387 }, "id": 85, "options": { @@ -4920,7 +5157,7 @@ "h": 5, "w": 13, "x": 11, - "y": 3242 + "y": 98387 }, "id": 89, "options": { @@ -5038,7 +5275,7 @@ "h": 5, "w": 11, "x": 0, - "y": 3247 + "y": 98392 }, "id": 88, "options": { @@ -5152,7 +5389,7 @@ "h": 5, "w": 13, "x": 11, - "y": 3247 + "y": 98392 }, "id": 90, "options": { @@ -5287,7 +5524,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3253 + "y": 98398 }, "id": 151, "options": { @@ -5407,7 +5644,7 @@ "h": 7, "w": 24, "x": 0, - "y": 3261 + "y": 98406 }, "id": 16, "options": { @@ -5543,7 +5780,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3370 + "y": 98515 }, "id": 80, "options": { @@ -5665,7 +5902,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3378 + "y": 98523 }, "id": 79, "options": { @@ -5787,7 +6024,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3386 + "y": 98531 }, "id": 82, "options": { @@ -5923,7 +6160,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19644 + "y": 114789 }, "id": 102, "options": { @@ -6044,7 +6281,7 @@ "h": 8, "w": 24, "x": 0, - "y": 19650 + "y": 114795 }, "id": 100, "options": { @@ -6176,7 +6413,7 @@ "h": 8, "w": 24, "x": 0, - "y": 22300 + "y": 117445 }, "id": 148, "options": { @@ -6272,7 +6509,7 @@ "h": 8, "w": 24, "x": 0, - "y": 22308 + "y": 117453 }, "id": 147, "options": { @@ -6383,7 +6620,7 @@ "h": 5, "w": 24, "x": 0, - "y": 22301 + "y": 117446 }, "id": 149, "options": { @@ -6602,8 +6839,8 @@ }, { "current": { - "text": "1738399874944", - "value": "1738399874944" + "text": "1768969085762", + "value": "1768969085762" }, "datasource": { "type": "mssql", @@ -6851,6 +7088,6 @@ "timezone": "", "title": "Monitoring - Perfmon Counters - Quest Softwares - Distributed", "uid": "distributed_perfmon", - "version": 23, + "version": 34, "weekStart": "" } \ No newline at end of file diff --git a/Grafana-Dashboards/Monitoring - Live - All Servers.json b/Grafana-Dashboards/Monitoring - Live - All Servers.json index ef647d0..070b91b 100644 --- a/Grafana-Dashboards/Monitoring - Live - All Servers.json +++ b/Grafana-Dashboards/Monitoring - Live - All Servers.json @@ -1,5 +1,8063 @@ { - "error": { - "message": "Datasource ygPVA4snk was not found" - } + "__inputs": [ + { + "name": "DS_SQLMONITOR", + "label": "SQLMonitor", + "description": "", + "type": "datasource", + "pluginId": "mssql", + "pluginName": "Microsoft SQL Server" + } + ], + "__elements": {}, + "__requires": [ + { + "type": "panel", + "id": "dashlist", + "name": "Dashboard list", + "version": "" + }, + { + "type": "grafana", + "id": "grafana", + "name": "Grafana", + "version": "11.3.1" + }, + { + "type": "datasource", + "id": "mssql", + "name": "Microsoft SQL Server", + "version": "11.3.0" + }, + { + "type": "datasource", + "id": "prometheus", + "name": "Prometheus", + "version": "1.0.0" + }, + { + "type": "panel", + "id": "table", + "name": "Table", + "version": "" + } + ], + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "datasource", + "uid": "grafana" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "target": { + "limit": 100, + "matchAny": false, + "tags": [], + "type": "dashboard" + }, + "type": "dashboard" + } + ] + }, + "description": "Monitor Server Using Live PerfMon Metrics", + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 0, + "id": null, + "links": [], + "panels": [ + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 0 + }, + "id": 243, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sqlserver_start_time_utc" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "os_uptime" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "major_version_number" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "minor_version_number" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "cpu_count" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "scheduler_count" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Is MS Supported" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "false": { + "color": "red", + "index": 1, + "text": "UnSupported" + }, + "true": { + "color": "green", + "index": 0, + "text": "Supported" + } + }, + "type": "value" + } + ] + }, + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "total_physical_memory_kb" + }, + "properties": [ + { + "id": "displayName", + "value": "RAM" + }, + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "custom.align", + "value": "center" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "srv_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Server" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Individual Server", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.srv_name}&var-perfmon_host_name=${__data.fields.host_name}" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "domain" + }, + "properties": [ + { + "id": "displayName", + "value": "Domain" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "host_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Host Name" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Individual Server", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.srv_name}&var-perfmon_host_name=${__data.fields.host_name}" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "CPU (OS / SQL)" + }, + "properties": [ + { + "id": "custom.align", + "value": "center" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "product_version" + }, + "properties": [ + { + "id": "displayName", + "value": "Version" + }, + { + "id": "custom.align", + "value": "center" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "SQLServer Updates", + "url": "https://sqlserverupdates.com/sql-server-2019-updates/" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "os_uptime_days" + }, + "properties": [ + { + "id": "displayName", + "value": "OS Uptime" + }, + { + "id": "custom.align", + "value": "center" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "os_start_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "OS Start Time" + }, + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sqlserver_start_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "SqlServer Start Time" + }, + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "MS Support" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "Extended Support": { + "color": "orange", + "index": 1 + }, + "Main Support": { + "color": "green", + "index": 0 + }, + "No Support": { + "color": "red", + "index": 2 + } + }, + "type": "value" + } + ] + } + ] + } + ] + }, + "gridPos": { + "h": 6, + "w": 24, + "x": 0, + "y": 1 + }, + "id": 219, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\n;with t_cte as (\n\tselect\tsrv_name, domain, host_name, product_version, major_version_number, minor_version_number, cpu_count, \n\t scheduler_count, total_physical_memory_kb, os_start_time_utc, sqlserver_start_time_utc\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [os_uptime]\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [sqlserver_uptime]\n\t\t\t,datediff(day,os_start_time_utc,GETUTCDATE()) as [os_uptime_days]\n\tfrom dbo.vw_all_server_info\n)\nselect srv_name, domain, host_name, product_version, major_version_number, minor_version_number\n ,[CPU (OS / SQL)] = convert(varchar,cpu_count)+' / '+convert(varchar,scheduler_count)\n ,total_physical_memory_kb ,cpu_count, scheduler_count, [os_uptime_days], os_start_time_utc, sqlserver_start_time_utc, [os_uptime], [sqlserver_uptime]\n\t\t ,[Is MS Supported] = case when c.MainstreamSupportEndDate < getdate() then convert(bit,0) else convert(bit,1) end\n\t\t\t\t,[MS Support] = case when c.MainstreamSupportEndDate > getdate()\n\t\t\t\t\t\tthen 'Mainstream Support'\n\t\t\t\t\t\twhen c.ExtendedSupportEndDate > getdate()\n\t\t\t\t\t\tthen 'Extended Support'\n\t\t\t\t\t\telse 'No Support'\n\t\t\t\t\t\tend\nfrom t_cte cte\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t\t,[MinorVersionNumber]\n\t\t\t\t,[Branch]\n\t\t\t\t,[Url]\n\t\t\t\t,[ReleaseDate]\n\t\t\t\t,[MainstreamSupportEndDate]\n\t\t\t\t,[ExtendedSupportEndDate]\n\t\t\t\t,[MajorVersionName]\n\t\t\t\t,[MinorVersionName]\t \n\t\tfrom [master].[dbo].[SqlServerVersions] as c\n\t\twhere [MajorVersionNumber] = cte.major_version_number\n\t\tand [MinorVersionNumber] <= cte.minor_version_number\n\t\torder by [MinorVersionNumber] desc\n) as c\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t,[MinorVersionNumber]\n\t\t\t,[Branch]\n\t\t\t,[Url]\n\t\t\t,[ReleaseDate]\n\t\t\t,[MainstreamSupportEndDate]\n\t\t\t,[ExtendedSupportEndDate]\n\t\t\t,[MajorVersionName]\n\t\t\t,[MinorVersionName]\t \n\tfrom [master].[dbo].[SqlServerVersions] as c\n\twhere [MajorVersionNumber] = cte.major_version_number\n\torder by [MinorVersionNumber] desc\n) as l;\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "All Servers - Basic Details @ ${all_server_stable_info__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Servers - Basic Info - \"${basic_info_servers_count}\" Total - \"${unsupported_servers_count}\" Non Supported @ ${all_server_stable_info__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 1 + }, + "id": 848, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + }, + "unit": "string" + }, + "overrides": [ + { + "matcher": { + "id": "byType", + "options": "time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byRegexp", + "options": ".*__latency_minutes" + }, + "properties": [ + { + "id": "unit", + "value": "m" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 20 + }, + { + "color": "red", + "value": 40 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + } + ] + }, + { + "matcher": { + "id": "byRegexp", + "options": ".*__latency_days" + }, + "properties": [ + { + "id": "unit", + "value": "d" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 8 + }, + { + "color": "red", + "value": 15 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "srv_name" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "displayName", + "value": "Server" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Server Details", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.srv_name}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "performance_counters__latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "performance_counters" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "resource_consumption__latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "resource_consumption" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "WhoIsActive__latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "WhoIsActive" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "disk_space__latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "disk_space" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 60 + }, + { + "color": "red", + "value": 90 + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "file_io_stats__latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "file_io_stats" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "wait_stats__latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "wait_stats" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "BlitzIndex__latency_days" + }, + "properties": [ + { + "id": "displayName", + "value": "BlitzIndex" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "BlitzIndex_Mode0__latency_days" + }, + "properties": [ + { + "id": "displayName", + "value": "BlitzIndex_Mode0" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "BlitzIndex_Mode1__latency_days" + }, + "properties": [ + { + "id": "displayName", + "value": "BlitzIndex_Mode1" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "BlitzIndex_Mode4__latency_days" + }, + "properties": [ + { + "id": "displayName", + "value": "BlitzIndex_Mode4" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Issues" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 2 + }, + "id": 850, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "hide": false, + "rawQuery": true, + "rawSql": "declare @_latency_minutes_threshold int = 20;\r\ndeclare @_latency_days_threshold int = 3;\r\ndeclare @t_servers_exception table (srv_name varchar(125));\r\n\r\ninsert @t_servers_exception -- list of servers that have permanent issues\r\nvalues ('192.168.1.21'),('192.168.1.22');\r\n\r\nselect\tcli.srv_name, id.jobs_server,\r\n\t\tcli.performance_counters__latency_minutes,\r\n\t\txevent_metrics__latency_minutes = case when (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11) then cli.xevent_metrics__latency_minutes \r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\r\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.xevent_metrics__latency_minutes end,\r\n\t\tWhoIsActive__latency_minutes = case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then cli.WhoIsActive__latency_minutes \r\n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\r\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.WhoIsActive__latency_minutes end,\r\n\t\tcli.disk_space__latency_minutes, \r\n\t\tcli.file_io_stats__latency_minutes,\r\n\t\tcli.sql_agent_job_stats__latency_minutes,\r\n\t\tcli.memory_clerks__latency_minutes,\r\n\t\tcli.wait_stats__latency_minutes,\r\n\t\tcli.BlitzIndex__latency_days,\r\n\t\tcli.BlitzIndex_Mode0__latency_days,\r\n\t\t--cli.BlitzIndex_Mode1__latency_days,\r\n\t\t--cli.BlitzIndex_Mode4__latency_days,\r\n\t\t--collection_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), cli.collection_time)\r\n\t\t[Issues] = (case when cli.performance_counters__latency_minutes > @_latency_minutes_threshold then 'performance_counters,' else '' end)+\r\n\t\t\t\t\t(case when cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11 then 'xevent_metrics' else '' end)+\r\n\t\t\t\t\t(case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then 'WhoIsActive,' else '' end)+\r\n\t\t\t\t\t(case when cli.disk_space__latency_minutes > @_latency_minutes_threshold*3 then 'disk_space,' else '' end)+\r\n\t\t\t\t\t(case when cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3 then 'file_io_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2 then 'wait_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5 then 'sql_agent_job_stats,' else '' end)+\r\n\t\t\t\t\t(case when cli.memory_clerks__latency_minutes > @_latency_minutes_threshold then 'memory_clerks,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex__latency_days > @_latency_days_threshold then 'BlitzIndex,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode0,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode1,' else '' end)+\r\n\t\t\t\t\t(case when cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode4,' else '' end)\r\n\t\t--,cli.host_name ,si.major_version_number ,si.product_version ,si.edition, vi.os_cpu, vi.sql_cpu\r\nfrom dbo.all_server_collection_latency_info cli\r\njoin dbo.all_server_stable_info si on si.srv_name = cli.srv_name\r\njoin dbo.all_server_volatile_info vi on vi.srv_name = cli.srv_name\r\nouter apply (select STUFF((select ', '+id.collector_powershell_jobs_server from (select distinct id.collector_powershell_jobs_server from dbo.instance_details id where id.sql_instance = cli.srv_name and id.host_name = cli.host_name) id for xml path('')), 1, 1, '') as jobs_server ) id\r\nwhere 1=1\r\nand (\tcli.performance_counters__latency_minutes > @_latency_minutes_threshold\r\n\t\tor (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11)\r\n\t\tor (cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20)\r\n\t\tor cli.disk_space__latency_minutes > @_latency_minutes_threshold*3\r\n\t\tor cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3\r\n\t\tor cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2\r\n\t\tor cli.BlitzIndex__latency_days > @_latency_days_threshold\r\n\t\tor cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4\r\n\t\t--or cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4\r\n\t\t--or cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4\r\n\t\tor cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5\r\n\t\tor cli.memory_clerks__latency_minutes > @_latency_minutes_threshold\r\n\t)\r\nand cli.srv_name not in (select e.srv_name from @t_servers_exception e)\r\n\r\n", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Servers with Data Collection Issues @ ${collection_latency_info__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "Servers with Data Collection Issues - \"${servers_with_data_issue_count}\" Total @ ${collection_latency_info__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 2 + }, + "id": 846, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "USE DBA\ngo\n\nselect /* Offline or Linked Server not working */ *\nfrom dbo.instance_details id where id.is_enabled = 1 \n\tand (id.is_available = 0 or id.is_linked_server_working = 0);\ngo\n\nexec /* Check why linked server not working */ dbo.usp_GetAllServerInfo \n\t\t@servers = 'ATPR0SVDNTRY160,196.1.115.244', \n\t\t@output = 'srv_name, os_start_time_utc', \n\t\t@verbose = 2\ngo", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "mode": "basic", + "type": "color-background" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "transparent" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "custom.width", + "value": 127 + }, + { + "id": "displayName", + "value": "Server" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Distributed Dashboard", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.sql_instance}" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_available" + }, + "properties": [ + { + "id": "displayName", + "value": "Is Available" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "0": { + "color": "red", + "index": 1, + "text": "No" + }, + "1": { + "color": "green", + "index": 0, + "text": "Yes" + } + }, + "type": "value" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_linked_server_working" + }, + "properties": [ + { + "id": "displayName", + "value": "Linked Server Working" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "0": { + "color": "red", + "index": 1, + "text": "No" + }, + "1": { + "color": "green", + "index": 0, + "text": "Yes" + } + }, + "type": "value" + }, + { + "options": { + "match": "null", + "result": { + "color": "transparent", + "index": 2 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_alias" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "red", + "index": 0 + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "dark-green", + "index": 1 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance_port" + }, + "properties": [ + { + "id": "custom.width", + "value": 165 + } + ] + } + ] + }, + "gridPos": { + "h": 5, + "w": 24, + "x": 0, + "y": 3 + }, + "id": 844, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect sql_instance, sql_instance_port, [host_name], is_available,\n\t\t\tis_linked_server_working = case when is_available = 0 then null else is_linked_server_working end, \n\t\t\t--[tsql jobs server] = collector_tsql_jobs_server, \n\t\t--[powershell jobs server] = collector_powershell_jobs_server, \n\t\t[perfmon data server] = data_destination_sql_instance, \n\t\tis_alias,\n\t\t last_unavailability_time_utc\nfrom dbo.instance_details id\nwhere is_enabled = 1 and is_alias = 0\nand (is_available = 0 or is_linked_server_working = 0)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = id.sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "CRITICAL - OFFLINE/Linked Server Issue - \"${offline_servers_count}\" Core Instances", + "type": "table" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "USE DBA\ngo\n\nselect /* Offline or Linked Server not working */ *\nfrom dbo.instance_details id where id.is_enabled = 1 \n\tand (id.is_available = 0 or id.is_linked_server_working = 0);\ngo\n\nexec /* Check why linked server not working */ dbo.usp_GetAllServerInfo \n\t\t@servers = 'ATPR0SVDNTRY160,196.1.115.244', \n\t\t@output = 'srv_name, os_start_time_utc', \n\t\t@verbose = 2\ngo", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "mode": "basic", + "type": "color-background" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "transparent" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "custom.width", + "value": 127 + }, + { + "id": "displayName", + "value": "Alias Server" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Distributed Dashboard", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.sql_instance}" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_available" + }, + "properties": [ + { + "id": "displayName", + "value": "Is Available" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "0": { + "color": "red", + "index": 1, + "text": "No" + }, + "1": { + "color": "green", + "index": 0, + "text": "Yes" + } + }, + "type": "value" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_linked_server_working" + }, + "properties": [ + { + "id": "displayName", + "value": "Linked Server Working" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "0": { + "color": "red", + "index": 1, + "text": "No" + }, + "1": { + "color": "green", + "index": 0, + "text": "Yes" + } + }, + "type": "value" + }, + { + "options": { + "match": "null", + "result": { + "color": "transparent", + "index": 2 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "source_sql_instance" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Distributed Dashboard", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.source_sql_instance}" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_alias" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "dark-green", + "index": 1 + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "red", + "index": 2 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance_port" + }, + "properties": [ + { + "id": "custom.width", + "value": 166 + } + ] + } + ] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 8 + }, + "id": 887, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect sql_instance, sql_instance_port, source_sql_instance, is_available,\n\t\t\tis_linked_server_working = case when is_available = 0 then null else is_linked_server_working end, \t\t\t\n\t\t\t--[tsql jobs server] = collector_tsql_jobs_server, [host_name], \n\t\t\t--[powershell jobs server] = collector_powershell_jobs_server, [perfmon data server] = data_destination_sql_instance, \n\t\t\t[host_name], is_alias\n\t\t \t--last_unavailability_time_utc\nfrom dbo.instance_details id\nwhere is_enabled = 1 and is_alias = 1\nand (is_available = 0 or is_linked_server_working = 0)\nand exists (select * from dbo.instance_details srv \n\t\t\twhere srv.is_enabled = 1 and srv.is_alias = 0\n\t\t\tand srv.sql_instance = id.source_sql_instance \n\t\t\tand srv.host_name = id.host_name\n\t\t\tand (srv.is_available = 0 or coalesce(srv.is_linked_server_working,1) = 0)\n\t\t\t\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = id.sql_instance)\n\t\t\t)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "CRITICAL - OFFLINE/Linked Server Issue - \"${offline_alias_count}\" Alias Instances", + "type": "table" + } + ], + "repeat": "datasource", + "title": "CRITICAL - OFFLINE/Linked Server Issue - \"${offline_servers_count}\" Core Instances - \"${offline_alias_count}\" Alias Instances", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 3 + }, + "id": 872, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select sas.sql_instance, id.sql_instance_with_port, sas.startup_type_desc, sas.status_desc,\n\t\tsas.servicename, sas.service_account\nfrom dbo.services_all_servers sas\ncross apply (select top 1 sql_instance_with_port = coalesce(id.sql_instance+','+id.sql_instance_port,id.sql_instance) \n\t\t\t\tfrom dbo.instance_details id where id.is_enabled = 1 \n\t\t\t\tand id.is_available = 1 and id.sql_instance = sas.sql_instance) id\nwhere 1=1\nand sas.service_type = 'Agent'\nand sas.status_desc <> 'Running'", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "center", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "displayName", + "value": "SQL Instance" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance_with_port" + }, + "properties": [ + { + "id": "displayName", + "value": "SQL Instance (port)" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "startup_type_desc" + }, + "properties": [ + { + "id": "displayName", + "value": "Startup Type" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "Automatic": { + "color": "green", + "index": 0 + }, + "Disabled": { + "color": "orange", + "index": 1 + } + }, + "type": "value" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "status_desc" + }, + "properties": [ + { + "id": "displayName", + "value": "Status" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "Stopped": { + "color": "red", + "index": 0 + } + }, + "type": "value" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "servicename" + }, + "properties": [ + { + "id": "displayName", + "value": "Service Name" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "service_account" + }, + "properties": [ + { + "id": "displayName", + "value": "Service Account" + } + ] + } + ] + }, + "gridPos": { + "h": 6, + "w": 24, + "x": 0, + "y": 408 + }, + "id": 871, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect sas.sql_instance, id.sql_instance_with_port, sas.startup_type_desc, sas.status_desc,\n\t\tsas.servicename, sas.service_account\nfrom dbo.services_all_servers sas\ncross apply (select top 1 sql_instance_with_port = coalesce(id.sql_instance+','+id.sql_instance_port,id.sql_instance) \n\t\t\t\tfrom dbo.instance_details id where id.is_enabled = 1 \n\t\t\t\tand id.is_available = 1 and id.sql_instance = sas.sql_instance) id\nwhere 1=1\nand sas.service_type = 'Agent'\nand sas.status_desc <> 'Running'\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = sas.sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "SQLAgent Service is OFFLINE @ ${services_all_servers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "CRITICAL - SQLAgent Service - \"${offline_agent_count}\" OFFLINE @ ${services_all_servers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 4 + }, + "id": 870, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select * from dbo.backups_all_servers", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "displayName", + "value": "Server" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "purple" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Database" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_backups_count" + }, + "properties": [ + { + "id": "displayName", + "value": "Total Log Backups" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_creation_date_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Db Created Date" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "recovery_model" + }, + "properties": [ + { + "id": "displayName", + "value": "Recovery Model" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "dark-blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_backup_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Bkp Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_backup_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Bkp Size" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_compressed_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Compressed" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_backup_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Bkp Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_backup_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Bkp Size" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_compressed_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Compressed" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_backup_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Bkp Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_backup_file" + }, + "properties": [ + { + "id": "displayName", + "value": "Full File" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_backup_file" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff File" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_backup_file" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog File" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_latency_days" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Delay" + }, + { + "id": "unit", + "value": "d" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "orange", + "value": 8 + }, + { + "color": "red", + "value": 15 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_latency_hours" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Delay" + }, + { + "id": "unit", + "value": "h" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "yellow", + "value": 24 + }, + { + "color": "orange", + "value": 32 + }, + { + "color": "red", + "value": 48 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Delay" + }, + { + "id": "unit", + "value": "m" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 30 + }, + { + "color": "orange", + "value": 120 + }, + { + "color": "red", + "value": 360 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_backup_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Bkp Size" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_compressed_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Compressed" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 401 + }, + "id": 874, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.2.0", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "hide": false, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n--declare @sql_instance varchar(255);\n--declare @backup_type varchar(2000);\n--declare @backup_description varchar(2000);\n--declare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n--declare @delta_minutes int;\n--declare @disk_drive varchar(255) = '$disk_drive';\n--declare @database varchar(255) = '$database';\n--declare @filter_for_latency varchar(50) = '$filter_for_latency';\ndeclare @full_threshold_days int = $full_threshold_days;\ndeclare @diff_threshold_hours int = $diff_threshold_hours;\ndeclare @tlog_threshold_minutes int = $tlog_threshold_minutes;\ndeclare @_has_inventory bit = 0;\n\n\n--set @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n--set @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n--set @delta_minutes = $cpu_delta_minutes;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @backup_type = '${backup_type:csv}';\n--set @backup_description = case when ltrim(rtrim('$backup_description')) = '' then null else ltrim(rtrim('$backup_description')) end;\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2, \n\t\t\t\t\t\t\t\t@full_threshold_days int, @diff_threshold_hours int, @tlog_threshold_minutes int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - Dashboard [$__dashboard] */\nset nocount on;\n\n;with t_backups as (\n\t\tselect [collection_time_utc], [sql_instance], [database_name], [backup_type], [log_backups_count], [backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description]\n\t\tfrom dbo.backups_all_servers bas\n)\n,t_pivot as (\n\t\tselect\t[sql_instance], [database_name]\n\t\t\t\t,[recovery_model] = max([recovery_model])\n\t\t\t\t,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[log_backups_count] = max([log_backups_count])\n\t\t\t\t,[database_creation_date_utc] = max([database_creation_date_utc])\n\t\t\t\t,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end)\n\t\tfrom t_backups bkp\n\t\twhere 1=1\n\t\tgroup by [sql_instance], [database_name]\n)\n,t_latency as (\n\t\t\tselect \t[sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(day,[full_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\t\t\t\t\t\t\n\t\t\t\t\t\t\t[diff_latency_hours] = case when [diff_backup_time_utc] is null \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen @full_threshold_days * 24\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(hour,[diff_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen recovery_model <> 'SIMPLE'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when [tlog_backup_time_utc] is null then @full_threshold_days * 1440\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t when [tlog_backup_time_utc] is not null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t then datediff(minute,[tlog_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t else null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t end\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb],\n\t\t\t\t\t\t\t[tlog_compressed_size_mb], [log_backups_count],\n\t\t\t\t\t\t\t[database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file]\n\t\t\tfrom t_pivot as bkp\n\t\t\twhere 1=1\n)\nselect [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days], [diff_latency_hours], [tlog_latency_minutes],\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], \n\t\t\t\t\t\t\t[tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], \n\t\t\t\t\t\t\t[full_backup_file], [diff_backup_file], [tlog_backup_file]\nfrom t_latency as l\nwhere 1=1\nAND (\t\t(full_latency_days is null or full_latency_days >= @full_threshold_days)\n\t\tOR \t(diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours)\n\t\tOR\t(tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes)\n\t\t)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = l.sql_instance and (s.hadr_strategy is null or s.hadr_strategy <> 'ag'))\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc, @full_threshold_days, @diff_threshold_hours, \n\t\t\t\t\t\t\t\t\t\t\t\t@tlog_threshold_minutes;", + "refId": "Query-Db-File-Stats-Since-Startup", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "\"${nonag_database_counts_with_backup_issue}\" __Non AG__ Dbs @ ${backups_all_servers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select * from dbo.backups_all_servers", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "displayName", + "value": "Server" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "purple" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Database" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_backups_count" + }, + "properties": [ + { + "id": "displayName", + "value": "Total Log Backups" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_creation_date_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Db Created Date" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "recovery_model" + }, + "properties": [ + { + "id": "displayName", + "value": "Recovery Model" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "dark-blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_backup_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Bkp Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_backup_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Bkp Size" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_compressed_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Compressed" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_backup_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Bkp Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_backup_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Bkp Size" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_compressed_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Compressed" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_backup_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Bkp Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_backup_file" + }, + "properties": [ + { + "id": "displayName", + "value": "Full File" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_backup_file" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff File" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_backup_file" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog File" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "full_latency_days" + }, + "properties": [ + { + "id": "displayName", + "value": "Full Delay" + }, + { + "id": "unit", + "value": "d" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "orange", + "value": 8 + }, + { + "color": "red", + "value": 15 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "diff_latency_hours" + }, + "properties": [ + { + "id": "displayName", + "value": "Diff Delay" + }, + { + "id": "unit", + "value": "h" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "yellow", + "value": 24 + }, + { + "color": "orange", + "value": 32 + }, + { + "color": "red", + "value": 48 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_latency_minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Delay" + }, + { + "id": "unit", + "value": "m" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 30 + }, + { + "color": "orange", + "value": 120 + }, + { + "color": "red", + "value": 360 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_backup_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Bkp Size" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tlog_compressed_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "TLog Compressed" + }, + { + "id": "unit", + "value": "mbytes" + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 409 + }, + "id": 888, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.2.0", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "hide": false, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n--declare @sql_instance varchar(255);\n--declare @backup_type varchar(2000);\n--declare @backup_description varchar(2000);\n--declare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n--declare @delta_minutes int;\n--declare @disk_drive varchar(255) = '$disk_drive';\n--declare @database varchar(255) = '$database';\n--declare @filter_for_latency varchar(50) = '$filter_for_latency';\ndeclare @full_threshold_days int = $full_threshold_days;\ndeclare @diff_threshold_hours int = $diff_threshold_hours;\ndeclare @tlog_threshold_minutes int = $tlog_threshold_minutes;\ndeclare @_has_inventory bit = 0;\n\n\n--set @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n--set @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n--set @delta_minutes = $cpu_delta_minutes;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @backup_type = '${backup_type:csv}';\n--set @backup_description = case when ltrim(rtrim('$backup_description')) = '' then null else ltrim(rtrim('$backup_description')) end;\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2, \n\t\t\t\t\t\t\t\t@full_threshold_days int, @diff_threshold_hours int, @tlog_threshold_minutes int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - Dashboard [Monitoring - Live - All Servers] */\nset nocount on;\n\n;with t_backups as (\n\tselect [collection_time_utc], [sql_instance], s.ag_replicas, [database_name], [backup_type], [log_backups_count], \n\t\t\t[backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description]\n\t\t\t,db_rowid = ROW_NUMBER()over(partition by s.ag_replicas, [database_name], [backup_type] order by [backup_start_date_utc] desc)\n\tfrom dbo.backups_all_servers bas\n\tjoin dbo.sma_sql_servers s on s.server = bas.sql_instance and s.is_decommissioned = 0 and s.is_onboarded = 1 and s.hadr_strategy = 'ag'\n\twhere 1=1\n\tand charindex(',',s.ag_replicas) > 0\n)\n,t_pivot as (\n\t\tselect\tag_replicas, [sql_instance], [database_name]\n\t\t\t\t,[recovery_model] = max([recovery_model])\n\t\t\t\t,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[log_backups_count] = max([log_backups_count])\n\t\t\t\t,[database_creation_date_utc] = max([database_creation_date_utc])\n\t\t\t\t,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end)\n\t\tfrom t_backups bkp\n\t\twhere 1=1\n\t\tand db_rowid = 1\n\t\tgroup by ag_replicas, [sql_instance], [database_name]\n)\n,t_latency as (\n\t\t\tselect \tag_replicas, [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(day,[full_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\t\t\t\t\t\t\n\t\t\t\t\t\t\t[diff_latency_hours] = case when [diff_backup_time_utc] is null \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen @full_threshold_days * 24\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(hour,[diff_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen recovery_model <> 'SIMPLE'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when [tlog_backup_time_utc] is null then @full_threshold_days * 1440\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t when [tlog_backup_time_utc] is not null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t then datediff(minute,[tlog_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t else null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t end\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb],\n\t\t\t\t\t\t\t[tlog_compressed_size_mb], [log_backups_count],\n\t\t\t\t\t\t\t[database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file]\n\t\t\tfrom t_pivot as bkp\n\t\t\twhere 1=1\n)\nselect ag_replicas, [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days], [diff_latency_hours], [tlog_latency_minutes],\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], \n\t\t\t\t\t\t\t[tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], \n\t\t\t\t\t\t\t[full_backup_file], [diff_backup_file], [tlog_backup_file]\nfrom t_latency as l\nwhere 1=1\nAND (\t\t(full_latency_days is null or full_latency_days >= @full_threshold_days)\n\t\tOR \t(diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours)\n\t\tOR\t(tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes)\n\t\t)\norder by ag_replicas, database_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc, @full_threshold_days, @diff_threshold_hours, \n\t\t\t\t\t\t\t\t\t\t\t\t@tlog_threshold_minutes;", + "refId": "Query-Db-File-Stats-Since-Startup", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "\"${ag_database_counts_with_backup_issue}\" __AG__ Dbs @ ${backups_all_servers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Backup Issues - Require ATTENTION - \"${nonag_database_counts_with_backup_issue}\" Non AG & \"${ag_database_counts_with_backup_issue}\" AG Dbs @ ${backups_all_servers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 5 + }, + "id": 866, + "panels": [ + { + "datasource": { + "default": false, + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "CollectionTimeUTC" + }, + "properties": [ + { + "id": "displayName", + "value": "Collection Time" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "displayName", + "value": "SQL Instance" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Server Dashboard ", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.sql_instance}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "JobName" + }, + "properties": [ + { + "id": "displayName", + "value": "Job Name" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + { + "id": "custom.minWidth", + "value": 250 + }, + { + "id": "displayName", + "value": "Job Name" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Job-Delay-Minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "Execution Delay" + }, + { + "id": "unit", + "value": "m" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-yellow" + }, + { + "color": "#EAB839", + "value": 30 + }, + { + "color": "orange", + "value": 60 + }, + { + "color": "red", + "value": 90 + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Last_RunTime" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + }, + { + "id": "displayName", + "value": "Last Run" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Last_Run_Duration_Seconds" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Last Duration" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Last_Run_Outcome" + }, + "properties": [ + { + "id": "displayName", + "value": "Last Outcome" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "Failed": { + "color": "red", + "index": 1 + }, + "Succeeded": { + "color": "green", + "index": 2 + } + }, + "type": "value" + }, + { + "options": { + "match": "null", + "result": { + "color": "super-light-yellow", + "index": 0 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Successfull_Execution_ClockTime_Threshold_Minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "Success ClockTime Threshold" + }, + { + "id": "unit", + "value": "m" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Expected_Max_Duration_Minutes" + }, + "properties": [ + { + "id": "displayName", + "value": "Expected Max Duration" + }, + { + "id": "unit", + "value": "m" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Last_Successful_Execution_Hours" + }, + "properties": [ + { + "id": "displayName", + "value": "Last Successful Execution" + }, + { + "id": "unit", + "value": "h" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Running_Since_Min" + }, + "properties": [ + { + "id": "displayName", + "value": "Running Since" + }, + { + "id": "unit", + "value": "m" + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 402 + }, + "id": 864, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.2.0", + "targets": [ + { + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @_buffer_time_minutes int = 30;\r\ndeclare @_sql nvarchar(max);\r\ndeclare @_params nvarchar(max);\r\n\r\nset @_params = N'@_buffer_time_minutes int';\r\nset quoted_identifier off;\r\nset @_sql = \"\r\nselect\t/* [Tsql-Stop-Job] = 'exec msdb.dbo.sp_stop_job @job_name = '''+sj.JobName+'''' ,\r\n\t\t[Tsql-Start-Job] = 'exec msdb.dbo.sp_start_job @job_name = '''+sj.JobName+'''' , \r\n\t\t*/\r\n\t\t[CollectionTimeUTC] = [UpdatedDateUTC],\r\n\t\tsj.[sql_instance], [JobName],\r\n\t\t[Job-Delay-Minutes] = case when sj.Last_Successful_ExecutionTime is null then 10080 else datediff(minute, sj.Last_Successful_ExecutionTime, dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@_buffer_time_minutes),getutcdate())) end,\r\n\t\t [Last_RunTime], [Last_Run_Duration_Seconds], [Last_Run_Outcome], \r\n\t\t [Successfull_Execution_ClockTime_Threshold_Minutes], \r\n\t\t [Expected_Max_Duration_Minutes],\r\n\t\t [Last_Successful_ExecutionTime], [Last_Successful_Execution_Hours], \r\n\t\t [Running_Since], [Running_StepName], [Running_Since_Min] \r\nfrom dbo.sql_agent_jobs_all_servers sj\r\ninner join dbo.services_all_servers sas\r\n\ton sas.sql_instance = sj.sql_instance\r\n\tand sas.service_type = 'Agent'\r\n\tand sas.status_desc = 'Running'\r\nwhere 1=1\r\nand exists (select 1/0 from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1)\r\nand sj.JobCategory = '(dba) SQLMonitor'\r\nand sj.JobName like '(dba) %'\r\nand sj.IsDisabled = 0\r\nand (\tisnull(sj.Last_Successful_ExecutionTime,sj.Last_RunTime) < dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@_buffer_time_minutes),getutcdate())\r\n\t\tand (sj.Last_Successful_ExecutionTime is not null or sj.Last_RunTime is not null)\r\n\t)\r\n--order by Last_Run_Outcome\r\n\"\r\nset quoted_identifier off;\r\n\r\nexec sp_executesql @_sql, @_params, @_buffer_time_minutes = @_buffer_time_minutes;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "All Servers - SQLMonitor Jobs - Require ATTENTION @ ${sql_agent_jobs__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Servers - SQLMonitor Jobs - \"${dba_jobs_with_issue_counts}\" Require ATTENTION @ ${sql_agent_jobs__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 6 + }, + "id": 854, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "displayName", + "value": "Server" + }, + { + "id": "custom.width", + "value": 127 + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space Dashboard", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&viewPanel=23" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "host_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Host" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space Dashboard", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&viewPanel=23" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "disk_volume" + }, + "properties": [ + { + "id": "displayName", + "value": "Disk" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Drive Space Trend", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&var-disk_drive=${__data.fields.disk_volume}&viewPanel=26" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "label" + }, + "properties": [ + { + "id": "displayName", + "value": "Disk Label" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "capacity_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Capacity" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space - Utilization Trend", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&var-disk_drive=${__data.fields.disk_volume}&viewPanel=26" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "free_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Free Space" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space - All Drives Current Sizes", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&viewPanel=23" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "state" + }, + "properties": [ + { + "id": "displayName", + "value": "Status" + }, + { + "id": "mappings", + "value": [ + { + "options": { + "Critical": { + "color": "red", + "index": 0 + }, + "Warning": { + "color": "orange", + "index": 1 + } + }, + "type": "value" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "used_pct" + }, + "properties": [ + { + "id": "displayName", + "value": "Used %" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 80 + }, + { + "color": "orange", + "value": 90 + }, + { + "color": "red", + "value": 95 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space - Utilization % Trend", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&var-disk_drive=${__data.fields.disk_volume}&viewPanel=22" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "updated_date_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Collection Time" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + } + ] + } + ] + }, + "gridPos": { + "h": 12, + "w": 24, + "x": 0, + "y": 411 + }, + "id": 852, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @_disk_warning_pct decimal(20,2) = ${disk_warning_pct};\r\ndeclare @_disk_critical_pct decimal(20,2) = ${disk_critical_pct};\r\ndeclare @_disk_threshold_gb decimal(20,2) = ${disk_threshold_gb};\r\ndeclare @_large_disk_threshold_pct decimal(20,2) = ${large_disk_threshold_pct};\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\ndeclare @_sql nvarchar(max);\r\ndeclare @_params nvarchar(max);\r\n\r\nset @_params = '@disk_warning_pct decimal(20,2), @disk_critical_pct decimal(20,2), @disk_threshold_gb decimal(20,2), @large_disk_threshold_pct decimal(20,2)';\r\n\r\nset quoted_identifier off;\r\nset @_sql = \"\r\nselect\tds.updated_date_utc, ds.sql_instance, ds.host_name, ds.disk_volume, ds.label, ds.capacity_mb, ds.free_mb,\r\n\t\t[state] = case when (ds.free_mb*100.0/ds.capacity_mb) < (100.0-@disk_critical_pct) then 'Critical' else 'Warning' end,\r\n\t\t--[free_pct] = convert(numeric(20,2),ds.free_mb*100.0/ds.capacity_mb),\r\n\t\t[used_pct] = 100.0-convert(numeric(20,2),ds.free_mb*100.0/ds.capacity_mb)\r\n\t\t--ds.block_size, ds.filesystem, \r\n --, ds.collection_time_utc\r\nfrom dbo.disk_space_all_servers ds\r\nwhere ds.updated_date_utc >= dateadd(minute,-60,getutcdate())\r\nand (\t(\t(ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct)\r\n\t\t\tand ds.free_mb < (@disk_threshold_gb)*1024\r\n\t \t)\r\n\t\tor ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free %\r\n\t\t)\r\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ds.sql_instance)\r\n\";\r\nset quoted_identifier off;\r\n\r\nexec sp_executesql @_sql, @_params, @_disk_warning_pct, @_disk_critical_pct, @_disk_threshold_gb , @_large_disk_threshold_pct;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Disk Space - All Servers @ ${disk_space__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Servers - Disk Utilization - Require ATTENTION - \"${critical_disks_counts}\" CRITICAL - \"${warning_disks_counts}\" WARNING @ ${disk_space__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 7 + }, + "id": 868, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "dbo.ag_health_state_all_servers", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "replica_server_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Replica" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Replica Ag Latency Trend", + "url": "d/ag_health_state?var-server=${__data.fields.sql_instance}&var-replica_server_name=${__data.fields.replica_server_name}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_primary_replica" + }, + "properties": [ + { + "id": "displayName", + "value": "Is Primary" + }, + { + "id": "unit", + "value": "bool" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "super-light-green", + "index": 0 + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "super-light-blue", + "index": 1 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Database" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Database Ag Latency Trend", + "url": "d/ag_health_state?var-server=${__data.fields.sql_instance}&var-database=${__data.fields.database_name}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "ag_name" + }, + "properties": [ + { + "id": "displayName", + "value": "AG" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "ag_listener" + }, + "properties": [ + { + "id": "displayName", + "value": "Listener" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue" + } + ] + } + }, + { + "id": "custom.minWidth", + "value": 180 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_local" + }, + "properties": [ + { + "id": "displayName", + "value": "Is Local" + }, + { + "id": "unit", + "value": "bool" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "super-light-green", + "index": 0 + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "super-light-blue", + "index": 1 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "synchronization_state_desc" + }, + "properties": [ + { + "id": "displayName", + "value": "Sync State" + }, + { + "id": "mappings", + "value": [ + { + "options": { + "INITIALIZING": { + "color": "light-yellow", + "index": 4 + }, + "NOT SYNCHRONIZING": { + "color": "red", + "index": 1 + }, + "REVERTING": { + "color": "super-light-yellow", + "index": 3 + }, + "SYNCHRONIZED": { + "color": "green", + "index": 0 + }, + "SYNCHRONIZING": { + "color": "super-light-green", + "index": 2 + } + }, + "type": "value" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "synchronization_health_desc" + }, + "properties": [ + { + "id": "displayName", + "value": "Sync Health" + }, + { + "id": "mappings", + "value": [ + { + "options": { + "HEALTHY": { + "color": "green", + "index": 0 + }, + "NOT_HEALTHY": { + "color": "red", + "index": 1 + }, + "PARTIALLY_HEALTHY": { + "color": "yellow", + "index": 2 + } + }, + "type": "value" + } + ] + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "latency_seconds" + }, + "properties": [ + { + "id": "displayName", + "value": "Latency" + }, + { + "id": "unit", + "value": "s" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "super-light-yellow", + "value": 120 + }, + { + "color": "super-light-orange", + "value": 240 + }, + { + "color": "orange", + "value": 300 + }, + { + "color": "super-light-red", + "value": 600 + }, + { + "color": "red", + "value": 1800 + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "redo_queue_size" + }, + "properties": [ + { + "id": "displayName", + "value": "Redo Queue" + }, + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "super-light-yellow", + "value": 1000000 + }, + { + "color": "light-orange", + "value": 10000000 + }, + { + "color": "red", + "value": 100000000 + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_send_queue_size" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Send Queue" + }, + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "super-light-yellow", + "value": 1000000 + }, + { + "color": "light-orange", + "value": 10000000 + }, + { + "color": "red", + "value": 100000000 + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "last_redone_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Last Redone" + }, + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_send_rate" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "redo_rate" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "estimated_redo_completion_time_min" + }, + "properties": [ + { + "id": "displayName", + "value": "Approx Redo Time" + }, + { + "id": "unit", + "value": "m" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "last_commit_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Last Commit" + }, + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_suspended" + }, + "properties": [ + { + "id": "displayName", + "value": "Is Suspended" + }, + { + "id": "unit", + "value": "bool" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "light-red", + "index": 0 + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "super-light-green", + "index": 1 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "suspend_reason_desc" + }, + "properties": [ + { + "id": "displayName", + "value": "Suspend Reason" + }, + { + "id": "unit", + "value": "bool" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "is_distributed" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "super-light-green", + "index": 0 + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "super-light-blue", + "index": 1 + } + }, + "type": "special" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "dark-blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Instance Ag Latency Details", + "url": "d/ag_health_state?var-server=${__data.fields.sql_instance}&viewPanel=147" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "replica_server" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "replica_database" + }, + "properties": [ + { + "id": "custom.minWidth", + "value": 250 + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + } + ] + } + ] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 412 + }, + "id": 867, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "set nocount on;\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\ndeclare @_latency_minutes int;\ndeclare @_redo_queue_size_gb int;\ndeclare @_log_send_queue_size_gb int;\ndeclare @_filter_out_offline_sqlagent bit = 1;\ndeclare @_has_inventory bit = 0;\n\nset @_has_inventory = convert(bit,'${has_inventory}');\nset @_latency_minutes = 30;\nset @_redo_queue_size_gb = 10;\nset @_log_send_queue_size_gb = 10;\n\nset @_params = N'@_latency_minutes int, @_redo_queue_size_gb int, @_log_send_queue_size_gb int';\n\nset @_sql = '\nif object_id(''tempdb..#replica_servers'') is not null\n\tdrop table #replica_servers\nselect distinct ahs.replica_server_name\ninto #replica_servers\nfrom dbo.ag_health_state_all_servers ahs\nwhere 1=1;\n\nselect\tsql_instance, [replica_database] = replica_server_name+'' || ''+database_name,\n\t\t--replica_server_name, database_name, \n\t\tag_name, is_primary_replica, \n\t\tag_listener, is_local, synchronization_state_desc, synchronization_health_desc, \n\t\tlatency_seconds, log_send_queue_size, redo_queue_size, last_redone_time, log_send_rate, \n\t\tredo_rate, estimated_redo_completion_time_min, last_commit_time, is_suspended, \n\t\tsuspend_reason_desc, is_distributed, replica_server = rs.srv_name, updated_date_utc, collection_time_utc\nfrom dbo.ag_health_state_all_servers ahs\nleft join (\tselect replica_server = rs.replica_server_name, srv_name = max(asi.srv_name)\n\t\tfrom #replica_servers rs\n\t\tjoin dbo.vw_all_server_info asi\n\t\t\ton rs.replica_server_name in (asi.machine_name, asi.server_name)\n\t\tgroup by rs.replica_server_name\n\t) rs\n\ton rs.replica_server = ahs.replica_server_name\nwhere 1=1\nand (\tahs.synchronization_health_desc <> ''HEALTHY''\n\tor\tahs.synchronization_state_desc not in (''SYNCHRONIZED'',''SYNCHRONIZING'')\n\tor\t(ahs.latency_seconds is not null and ahs.latency_seconds >= @_latency_minutes*60)\n\tor\t(ahs.log_send_queue_size is not null and ahs.log_send_queue_size >= @_log_send_queue_size_gb*1024*1024)\n\tor\t(ahs.redo_queue_size is not null and ahs.redo_queue_size >= @_redo_queue_size_gb*1024*1024)\n\t)\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ahs.sql_instance)\n'+(case when @_filter_out_offline_sqlagent = 0 then '--' else '' end)+'and exists (select 1/0 from dbo.services_all_servers sas where sas.sql_instance = ahs.sql_instance and sas.service_type = ''Agent''\tand sas.status_desc = ''Running'');\n';\n\nexec sp_executesql @_sql, @_params, @_latency_minutes, @_redo_queue_size_gb, @_log_send_queue_size_gb;\n", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "All Servers - AlwaysOn Latency - dbo.ag_health_state_all_servers @ ${ag_health_state__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Servers - AlwaysOn Latency - \"${ag_issue_counts}\" Require ATTENTION @ ${ag_health_state__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 8 + }, + "id": 858, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "center", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "displayName", + "value": "Server" + }, + { + "id": "custom.width", + "value": 127 + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space Utilization", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-database=${__data.fields.database_name}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_name" + }, + "properties": [ + { + "id": "displayName", + "value": "Database" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue" + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space Dashboard", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-database=${__data.fields.database_name}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "recovery_model" + }, + "properties": [ + { + "id": "displayName", + "value": "Recovery Model" + }, + { + "id": "custom.width", + "value": 127 + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "SIMPLE": { + "color": "text", + "index": 0 + } + }, + "type": "value" + }, + { + "options": { + "pattern": "^((?!SIMPLE).)*$", + "result": { + "color": "yellow", + "index": 1 + } + }, + "type": "regex" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_reuse_wait_desc" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Reuse Wait Desc" + }, + { + "id": "custom.width", + "value": 165 + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "NOTHING": { + "color": "green", + "index": 0 + } + }, + "type": "value" + }, + { + "options": { + "pattern": "^((?!NOTHING).)*$", + "result": { + "color": "yellow", + "index": 1 + } + }, + "type": "regex" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Size" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=${__data.fields.database_name}&viewPanel=38" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_used_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Used" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 102400 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=${__data.fields.database_name}&viewPanel=38" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_used_pct" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Used %" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 + } + ] + } + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log % Usage Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=${__data.fields.database_name}&viewPanel=123" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_used_pct_threshold" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_used_gb_threshold" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "updated_date_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Updated Date" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Collection Time" + }, + { + "id": "unit", + "value": "dateTimeAsSystem" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "spid" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "sp_WhatsRunning - Session Details", + "url": "d/WhoIsActive?var-server=${__data.fields.sql_instance}&var-session_id=${__data.fields.spid}&viewPanel=132" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "login_name" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "sp_WhatsRunning - Sessions for Login", + "url": "d/WhoIsActive?var-server=${__data.fields.sql_instance}&var-login_name=${__data.fields.login_name}&viewPanel=132" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "program_name" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "sp_WhatsRunning - Session Details for Program", + "url": "d/WhoIsActive?var-server=${__data.fields.sql_instance}&var-program_name=${__data.fields.program_name}&viewPanel=132" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "host_name" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "sp_WhatsRunning - Session Details for Host", + "url": "d/WhoIsActive?var-server=${__data.fields.sql_instance}&var-session_host_name=${__data.fields.host_name}&viewPanel=132" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "exists_valid_autogrowing_file" + }, + "properties": [ + { + "id": "displayName", + "value": "Autogrowth" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "mappings", + "value": [ + { + "options": { + "match": "true", + "result": { + "color": "green", + "index": 0, + "text": "Yes" + } + }, + "type": "special" + }, + { + "options": { + "match": "false", + "result": { + "color": "orange", + "index": 1, + "text": "No" + } + }, + "type": "special" + } + ] + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 413 + }, + "id": 856, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.1.4", + "targets": [ + { + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @_log_used_pct float;\r\ndeclare @_log_used_gb float;\r\ndeclare @_only_threshold_validated bit;\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\n/* When @_only_threshold_validated = 1, then @_log_used_pct & @_log_used_gb are not used */\r\n\r\ndeclare @_sqltext nvarchar(max);\r\ndeclare @_params nvarchar(max);\r\n\r\nset @_log_used_pct = ${log_used_pct_threshold};\r\nset @_log_used_gb = ${log_used_gb_threshold};\r\nset @_only_threshold_validated = case when '${only_validated_log_thresholds}' = 'Yes' then 1 else 0 end;\r\n\r\nset @_params = '@only_threshold_validated bit, @log_used_pct float, @log_used_gb float';\r\n\r\nset @_sqltext = '\r\nselect\t[collection_time_utc] = [updated_date_utc],\r\n\t\t[sql_instance], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [exists_valid_autogrowing_file],\r\n\t\t[log_used_mb], [log_used_pct], [log_used_pct_threshold], [log_used_gb_threshold], [spid], \r\n\t\t[transaction_start_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [transaction_start_time]), \r\n\t\t[login_name], [program_name], [host_name], [host_process_id], [command], [additional_info], \r\n\t\t[action_taken], [sql_text]\t\t\r\nfrom dbo.log_space_consumers_all_servers ls\r\nwhere 1=1\r\n'+(case when @_only_threshold_validated = 1 then '' else '--' end)+'and ls.thresholds_validated = @only_threshold_validated\r\n'+(case when @_only_threshold_validated = 1 then '--' else '' end)+'and ( (ls.log_used_pct > @log_used_pct)\tor (ls.log_used_mb > (@log_used_gb*1024)) )\r\nand (ls.updated_date_utc >= dateadd(minute,-60,getutcdate())\r\n and ls.collection_time_utc >= dateadd(minute,-20,getutcdate())\r\n\t\t)\r\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ls.sql_instance)\r\n';\r\n\r\nexec sp_executesql @_sqltext, @_params, @only_threshold_validated=@_only_threshold_validated, @log_used_pct=@_log_used_pct, @log_used_gb=@_log_used_gb;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Log Space Consumers @ ${log_space_consumers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Servers - Log Space Utilization - \"${log_space_issue_counts}\" Require ATTENTION @ ${log_space_consumers__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 9 + }, + "id": 862, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select *\nfrom dbo.tempdb_space_consumers tsc\nwhere 1=1\nand tsc.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_consumers i order by i.collection_time desc)", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "custom.width", + "value": 150 + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue" + } + ] + } + }, + { + "id": "displayName", + "value": "Server" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Disk Space for TempDb", + "url": "d/disk_space?var-server=${__data.fields.sql_instance}&var-database=tempdb" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "data_size_mb" + }, + "properties": [ + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "displayName", + "value": "Data Size" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Data File Growth Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=115" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "data_used_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Data Used" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Tempdb Free Space Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=89" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "data_used_pct" + }, + "properties": [ + { + "id": "displayName", + "value": "Data Used %" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "super-light-red", + "value": 90 + }, + { + "color": "red", + "value": 95 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Tempdb % Free Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=89" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_size_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Size" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=38" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_used_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Used" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=38" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "log_used_pct" + }, + "properties": [ + { + "id": "displayName", + "value": "Log Used %" + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - Log Usage % Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=123" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "version_store_mb" + }, + "properties": [ + { + "id": "displayName", + "value": "Version Store" + }, + { + "id": "unit", + "value": "mbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Tempdb Space Consumers on Individual Server Dashboard", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.sql_instance}&viewPanel=190" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "version_store_pct" + }, + "properties": [ + { + "id": "displayName", + "value": "Version Store %" + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "#EAB839", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - VersionStore Usage Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=90" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "custom.hidden", + "value": false + }, + { + "id": "displayName", + "value": "Collection Time" + }, + { + "id": "unit", + "value": "dateTimeAsIso" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "basic", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple" + } + ] + } + }, + { + "id": "custom.width", + "value": 160 + } + ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 316 + }, + "id": 860, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": true, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @_data_used_pct float = ${tempdb_used_pct_threshold};\r\ndeclare @_data_used_gb float = ${tempdb_used_gb_threshold};\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\ndeclare @_sqltext nvarchar(max);\r\ndeclare @_params nvarchar(max);\r\n\r\nset @_params = '@data_used_pct float, @data_used_gb float';\r\n\r\n\r\nset @_sqltext = '\r\nselect\t[collection_time_utc] = [updated_date_utc],\r\n\t\t[sql_instance], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], \r\n\t\t[log_used_pct], [version_store_mb], [version_store_pct]\r\nfrom dbo.tempdb_space_usage_all_servers su\r\nwhere (su.data_used_pct > @data_used_pct\r\n\tor su.data_used_mb > (@data_used_gb*1024) -- 200 gb\r\n\t)\r\nand (su.updated_date_utc >= dateadd(minute,-60,getutcdate())\r\n and su.collection_time_utc >= dateadd(minute,-20,getutcdate())\r\n\t)\r\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = su.sql_instance)\r\n'\r\nexec sp_executesql @_sqltext, @_params, @data_used_pct = @_data_used_pct, @data_used_gb = @_data_used_gb;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "TempDb Usage @ ${tempdb_space_usage__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "All Servers - Tempdb Utilization - \"${tempdb_space_issue_counts}\" Require ATTENTION @ ${tempdb_space_usage__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 10 + }, + "id": 889, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto", + "wrapText": true + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "error_number" + }, + "properties": [ + { + "id": "custom.width", + "value": 120 + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Error KB", + "url": "https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-${__data.fields.error_number}-database-engine-error" + } + ] + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "red", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_severity" + }, + "properties": [ + { + "id": "custom.width", + "value": 128 + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "orange", + "value": null + }, + { + "color": "red", + "value": 20 + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_message" + }, + "properties": [ + { + "id": "custom.align", + "value": "left" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance | occurrences | last_occurred" + }, + "properties": [ + { + "id": "custom.align", + "value": "left" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instances" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 11 + }, + "id": 890, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "dataset": "AdventureWorks2019", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "expr": "", + "format": "table", + "legendFormat": "__auto", + "range": true, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(max);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\r\n\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\n\r\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor - All Server Alert History */\r\nset nocount on;\r\n;with t_aggregated_messages as (\r\nselect a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), last_occurred = max(a.collection_time_utc), error_message = coalesce(max(m.text), max(a.error_message))\r\n --a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \r\n --a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\r\nfrom dbo.alert_history_all_servers a\r\nleft join sys.messages m \r\n on m.language_id = 1033 and m.message_id = a.error_number\r\nwhere 1 = 1\r\nand a.collection_time_utc between @start_time_utc and @end_time_utc\r\nand (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\r\n then 0\r\n when a.error_number = 1204 then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\r\n then 0\r\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\r\n then 0\r\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\r\n then 0\r\n when a.error_number = 5084 and error_severity = 10 and a.error_message like 'Setting database option MULTI_USER to ON%'\r\n then 0\r\n when a.error_number = 5084 and error_severity = 10 and a.error_message like 'Setting database option SINGLE_USER to ON%'\r\n then 0\r\n when a.error_number = 14151 and error_severity = 18 and a.error_message like '%Named Pipes Provider: Could not open a connection to SQL Server%'\r\n then 0\r\n when a.error_number = 17828 and error_severity = 20\r\n then 0\r\n when a.error_number in (17832, 17836)\r\n then 0\r\n else 1\r\n end\r\n ) = 1\r\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\r\ngroup by a.sql_instance, a.error_number, a.error_severity\r\n--order by error_severity desc, occurrences desc, sql_instance\r\n)\r\n,t_unique_errors as (\r\nselect error_number, error_severity, error_message\r\n --,sql_instance, occurrences, last_occurred\r\nfrom t_aggregated_messages\r\ngroup by error_number, error_severity, error_message\r\n)\r\nselect error_number, error_severity, error_message,\r\n [sql_instance | occurrences | last_occurred] = \r\n stuff(( SELECT ', ' + cc.[sql_instance | occurrences | last_occurred]\r\n FROM t_aggregated_messages am\r\n outer apply (select [last_occurred_localtime] = DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), last_occurred) ) lt\r\n outer apply (select [sql_instance | occurrences | last_occurred] = '('+sql_instance+' | '+convert(varchar(10),occurrences)+' | '+convert(varchar,lt.last_occurred_localtime,120)+')') cc\r\n where am.error_number = ue.error_number and am.error_severity = ue.error_severity\r\n order by occurrences desc\r\n FOR XML PATH('')\r\n ), 1, 2, ''),\r\n [sql_instances] = \r\n stuff(( SELECT ', ' + cc.sql_instance_quoted\r\n FROM t_aggregated_messages am\r\n outer apply (select sql_instance_quoted = quotename(sql_instance,'''')) cc\r\n where am.error_number = ue.error_number and am.error_severity = ue.error_severity\r\n FOR XML PATH('')\r\n ), 1, 2, '')\r\nfrom t_unique_errors ue\r\n\"\r\nset quoted_identifier on;\r\n\r\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Alerts Aggregated by Type - \"${alert_history_aggregated_row_count}\" Total - Between ____${__from:date:YYYY-MM-DD HH.mm}___to___${__to:date:YYYY-MM-DD HH.mm}____", + "type": "table" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto", + "wrapText": false + }, + "filterable": true, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "collection_time" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": true + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "dark-blue", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_instance" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": true + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue", + "value": null + } + ] + } + }, + { + "id": "custom.width", + "value": 218 + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Server Health Dashboard", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.sql_instance}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "server_name" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "database_name" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": true + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-blue", + "value": null + } + ] + } + }, + { + "id": "custom.width", + "value": 189 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_number" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "red", + "value": null + } + ] + } + }, + { + "id": "custom.width", + "value": 124 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_severity" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-text" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "yellow", + "value": null + }, + { + "color": "red", + "value": 20 + } + ] + } + }, + { + "id": "custom.width", + "value": 126 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "error_message" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "type": "color-background", + "wrapText": false + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-yellow", + "value": null + } + ] + } + }, + { + "id": "custom.minWidth", + "value": 400 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "host_instance" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "updated_time_utc" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "collection_time" + }, + "properties": [ + { + "id": "custom.width", + "value": 152 + } + ] + } + ] + }, + "gridPos": { + "h": 12, + "w": 24, + "x": 0, + "y": 21 + }, + "id": 873, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "dataset": "AdventureWorks2019", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(max);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\ndeclare @_has_inventory bit = 0;\r\nset @_has_inventory = convert(bit,'${has_inventory}');\r\n\r\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\r\n\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\n\r\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"/* SQLMonitor - All Server Alert History */\r\nset nocount on;\r\nselect a.collection_time_utc, a.sql_instance, a.database_name, a.error_number, \r\n a.error_severity, a.error_message \r\n --, a.host_instance, a.updated_time_utc, a.server_name\r\nfrom dbo.alert_history_all_servers a\r\nwhere 1 = 1\r\nand a.collection_time_utc between @start_time_utc and @end_time_utc\r\nand (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\r\n then 0\r\n when a.error_number = 1204 then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\r\n then 0\r\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\r\n then 0\r\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\r\n then 0\r\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\r\n then 0\r\n else 1\r\n end\r\n ) = 1\r\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\r\norder by 1 desc, 2\r\n\"\r\nset quoted_identifier on;\r\n\r\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "All Servers - Alert History - \"${alert_history_row_count}\" Total - Between ____${__from:date:YYYY-MM-DD HH.mm}___to___${__to:date:YYYY-MM-DD HH.mm}____", + "type": "table" + } + ], + "title": "All Servers - Alert History - \"${alert_history_aggregated_row_count}/${alert_history_row_count}\" Total - Between ____${__from:date:YYYY-MM-DD HH.mm}___to___${__to:date:YYYY-MM-DD HH.mm}____", + "type": "row" + }, + { + "collapsed": false, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 11 + }, + "id": 245, + "panels": [], + "title": "All Servers - Health Metrics - \"${core_metrics_issue_counts}\" Require ATTENTION @ ${all_server_volatile_info__collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "datasource": { + "default": false, + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "center", + "cellOptions": { + "type": "auto", + "wrapText": true + }, + "filterable": true, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "none" + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "os_cpu" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "color" + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 + } + ] + } + }, + { + "id": "displayName", + "value": "OS CPU" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - CPU Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.srv_name}&viewPanel=62" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "sql_cpu" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "percent" + }, + { + "id": "color" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "#EAB839", + "value": 65 + }, + { + "color": "orange", + "value": 70 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + { + "id": "displayName", + "value": "SQL CPU" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Perfmon - CPU Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.srv_name}&viewPanel=62" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "waits_per_core_per_minute" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "s" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "yellow", + "value": 180 + }, + { + "color": "orange", + "value": 240 + }, + { + "color": "red", + "value": 300 + } + ] + } + }, + { + "id": "displayName", + "value": "Waits Per Core Per Minute" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Wait Stats Dashboard", + "url": "d/wait_stats?var-server=${__data.fields.srv_name}&from=now-30m&to=now" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "blocked_counts" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "short" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "yellow", + "value": 1 + }, + { + "color": "orange", + "value": 5 + }, + { + "color": "red", + "value": 10 + } + ] + } + }, + { + "id": "displayName", + "value": "Blocked Over '60' Seconds" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Blocked Processes Trend", + "url": "d/distributed_perfmon?var-server=${__data.fields.srv_name}&viewPanel=16" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "blocked_duration_max_seconds" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "s" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "super-light-yellow", + "value": 60 + }, + { + "color": "yellow", + "value": 120 + }, + { + "color": "super-light-orange", + "value": 300 + }, + { + "color": "orange", + "value": 600 + }, + { + "color": "red", + "value": 1800 + } + ] + } + }, + { + "id": "displayName", + "value": "Longest Blocking" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Blocking Tree With Queries", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.srv_name}&viewPanel=111" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "available_physical_memory_kb" + }, + "properties": [ + { + "id": "displayName", + "value": "Available Memory" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "red", + "value": null + }, + { + "color": "orange", + "value": 512000 + }, + { + "color": "yellow", + "value": 2097152 + }, + { + "color": "green", + "value": 4194304 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "SQL M/r Consumption", + "url": "d/distributed_perfmon?var-server=${__data.fields.srv_name}&viewPanel=26" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "system_high_memory_signal_state" + }, + "properties": [ + { + "id": "custom.hidden" + }, + { + "id": "unit", + "value": "string" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "displayName", + "value": "OS Memory State" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "physical_memory_in_use_kb" + }, + "properties": [ + { + "id": "displayName", + "value": "Used SQL Memory" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "purple", + "value": null + } + ] + } + }, + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "SQL M/r Usage", + "url": "d/distributed_perfmon?var-server=${__data.fields.srv_name}&viewPanel=30" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "memory_grants_pending" + }, + "properties": [ + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "unit", + "value": "none" + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 1 + } + ] + } + }, + { + "id": "displayName", + "value": "Memory Grant Pending" + }, + { + "id": "unit", + "value": "short" + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Memory Consumers", + "url": "d/WhoIsActive?var-server=${__data.fields.srv_name}&viewPanel=122" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "srv_name" + }, + "properties": [ + { + "id": "custom.width", + "value": 127 + }, + { + "id": "displayName", + "value": "Server" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "blue", + "value": null + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Live Health", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.srv_name}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "connection_count" + }, + "properties": [ + { + "id": "displayName", + "value": "SQL Connections" + }, + { + "id": "custom.cellOptions", + "value": { + "mode": "gradient", + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "#EAB839", + "value": 800 + }, + { + "color": "#EF843C", + "value": 1000 + }, + { + "color": "red", + "value": 1200 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Connection Distribution", + "url": "d/distributed_live_dashboard?var-server=${__data.fields.srv_name}&viewPanel=124" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "avg_disk_latency_ms" + }, + "properties": [ + { + "id": "displayName", + "value": "Disk Latency" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "#EAB839", + "value": 10 + }, + { + "color": "orange", + "value": 15 + }, + { + "color": "red", + "value": 20 + } + ] + } + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Wait Stats Dashboard", + "url": "d/wait_stats?var-server=${__data.fields.srv_name}&from=now-30m&to=now" + }, + { + "targetBlank": true, + "title": "Perfmon - Disk Latency", + "url": "d/distributed_perfmon?var-server=${__data.fields.srv_name}&viewPanel=112" + }, + { + "targetBlank": true, + "title": "File IO Stats", + "url": "d/database_file_io_stats?var-server=${__data.fields.srv_name}&viewPanel=18&from=now-30m&to=now" + } + ] + }, + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "issue_rank" + }, + "properties": [ + { + "id": "custom.hidden", + "value": true + } + ] + } + ] + }, + "gridPos": { + "h": 12, + "w": 24, + "x": 0, + "y": 12 + }, + "id": 842, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @os_cpu_threshold decimal(20,2) = 70;\ndeclare @sql_cpu_threshold decimal(20,2) = 65;\ndeclare @blocked_counts_threshold int = 1;\ndeclare @blocked_duration_max_seconds_threshold bigint = 60;\ndeclare @available_physical_memory_kb_threshold bigint = (4*1024*1024);\ndeclare @system_high_memory_signal_state_threshold varchar(20) = 'Low';\n--declare @physical_memory_in_use_kb_threshold decimal(20,2);\ndeclare @memory_grants_pending_threshold int = 1;\ndeclare @connection_count_threshold int = 1000;\ndeclare @waits_per_core_per_minute_threshold decimal(20,2) = 180;\ndeclare @avg_disk_latency_ms int = 10;\ndeclare @_has_inventory bit = 0;\n\nset @_has_inventory = convert(bit,'${has_inventory}');\n\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n\nset @params = N'@os_cpu_threshold decimal(20,2), @sql_cpu_threshold decimal(20,2), @blocked_counts_threshold int, \n\t\t\t\t@blocked_duration_max_seconds_threshold bigint, @available_physical_memory_kb_threshold bigint, \n\t\t\t\t@system_high_memory_signal_state_threshold varchar(20), @memory_grants_pending_threshold int, \n\t\t\t\t@connection_count_threshold int, @waits_per_core_per_minute_threshold decimal(20,2), @avg_disk_latency_ms int';\n\nset quoted_identifier off;\nset @sql = \"\n;with t_cte as (\n\tselect\tsrv_name, os_cpu, sql_cpu, blocked_counts, blocked_duration_max_seconds, avg_disk_latency_ms, available_physical_memory_kb,\n physical_memory_in_use_kb, memory_grants_pending, connection_count, waits_per_core_per_minute, system_high_memory_signal_state\n ,issue_rank = (case when os_cpu >= 90 then 3 when os_cpu >= @os_cpu_threshold then 1 else 0 end) +\n (case when os_cpu >= 80 then 3 when sql_cpu >= @sql_cpu_threshold then 1 else 0 end) +\n (case when blocked_counts >= 20 then 5\n when blocked_counts >= 10 then 4\n when blocked_counts >= 5 then 2\n when blocked_counts >= @blocked_counts_threshold then 1\n else 0\n end) +\n (case when blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold then 1 else 0 end) +\n (case when ( available_physical_memory_kb < @available_physical_memory_kb_threshold and system_high_memory_signal_state = @system_high_memory_signal_state_threshold ) then 1 else 0 end) +\n (case when memory_grants_pending > 10 then 5\n when memory_grants_pending > 5 then 4\n when memory_grants_pending > @memory_grants_pending_threshold then 2\n else 0\n end) +\n (case when connection_count >= @connection_count_threshold then 1 else 0 end) +\n (case when avg_disk_latency_ms >= 100 then 5\n when avg_disk_latency_ms >= 80 then 4\n when avg_disk_latency_ms >= 50 then 3\n when avg_disk_latency_ms >= 35 then 2\n when avg_disk_latency_ms >= @avg_disk_latency_ms then 1\n else 0\n end) +\n (case when waits_per_core_per_minute > @waits_per_core_per_minute_threshold then 1 else 0 end)\n\tfrom dbo.vw_all_server_info\n)\nselect *\nfrom t_cte cte\nwhere 1=1\nand ( os_cpu >= @os_cpu_threshold\n or sql_cpu >= @sql_cpu_threshold \n or blocked_counts >= @blocked_counts_threshold\n or blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold\n or ( available_physical_memory_kb < @available_physical_memory_kb_threshold and system_high_memory_signal_state = @system_high_memory_signal_state_threshold )\n or memory_grants_pending > @memory_grants_pending_threshold\n or connection_count >= @connection_count_threshold\n or waits_per_core_per_minute > @waits_per_core_per_minute_threshold\n or avg_disk_latency_ms >= @avg_disk_latency_ms\n)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = cte.srv_name)\norder by issue_rank desc, avg_disk_latency_ms desc, srv_name\n\";\nset quoted_identifier off;\n\n--print @sql\nexec dbo.sp_executesql @sql, @params, @os_cpu_threshold, @sql_cpu_threshold, @blocked_counts_threshold, @blocked_duration_max_seconds_threshold, \n@available_physical_memory_kb_threshold, @system_high_memory_signal_state_threshold, @memory_grants_pending_threshold, \n@connection_count_threshold, @waits_per_core_per_minute_threshold, @avg_disk_latency_ms;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Servers Need Help - Health Metrics @ ${collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 24 + }, + "id": 121, + "panels": [ + { + "description": "", + "fieldConfig": { + "defaults": {}, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 462 + }, + "id": 119, + "options": { + "folderId": 4, + "maxItems": 10, + "query": "", + "showHeadings": false, + "showRecentlyViewed": false, + "showSearch": true, + "showStarred": false, + "tags": [] + }, + "pluginVersion": "9.4.7", + "title": "Other Dashboards", + "type": "dashlist" + } + ], + "title": "Other Dashboards", + "type": "row" + } + ], + "refresh": "5s", + "schemaVersion": 40, + "tags": [ + "mssql", + "sqlmonitor", + "All Servers", + "Alerts" + ], + "templating": { + "list": [ + { + "current": {}, + "description": "SQLMonitor Inventory Server Name", + "hide": 2, + "includeAll": false, + "label": "SQLMonitor Data Source", + "name": "sqlmonitor_datasource", + "options": [], + "query": "mssql", + "refresh": 1, + "regex": "", + "type": "datasource" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\nselect distinct srvname = sql_instance from dbo.instance_details\nwhere is_available = 1 and is_enabled =1\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "hide": 2, + "includeAll": true, + "label": "Sql Instance", + "multi": true, + "name": "server", + "options": [], + "query": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\nselect distinct srvname = sql_instance from dbo.instance_details\nwhere is_available = 1 and is_enabled =1\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refresh": 2, + "regex": "", + "sort": 1, + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "SELECT DB_NAME() as [db_name]", + "hide": 2, + "includeAll": false, + "label": "Inventory Database", + "name": "inventory_db", + "options": [], + "query": "SELECT DB_NAME() as [db_name]", + "refresh": 1, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "select collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time)) from dbo.all_server_volatile_info", + "hide": 2, + "includeAll": false, + "label": "Collection Time UTC", + "name": "collection_time_utc", + "options": [], + "query": "select collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time)) from dbo.all_server_volatile_info", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": { + "text": "70", + "value": "70" + }, + "includeAll": false, + "label": "Tempdb Used %", + "name": "tempdb_used_pct_threshold", + "options": [ + { + "selected": false, + "text": "30", + "value": "30" + }, + { + "selected": false, + "text": "50", + "value": "50" + }, + { + "selected": true, + "text": "70", + "value": "70" + }, + { + "selected": false, + "text": "80", + "value": "80" + }, + { + "selected": false, + "text": "90", + "value": "90" + }, + { + "selected": false, + "text": "95", + "value": "95" + } + ], + "query": "30,50,70,80,90,95", + "type": "custom" + }, + { + "current": { + "text": "20", + "value": "20" + }, + "includeAll": false, + "label": "Tempdb Used Gb", + "name": "tempdb_used_gb_threshold", + "options": [ + { + "selected": false, + "text": "2", + "value": "2" + }, + { + "selected": true, + "text": "20", + "value": "20" + }, + { + "selected": false, + "text": "50", + "value": "50" + }, + { + "selected": false, + "text": "100", + "value": "100" + }, + { + "selected": false, + "text": "200", + "value": "200" + }, + { + "selected": false, + "text": "350", + "value": "350" + }, + { + "selected": false, + "text": "500", + "value": "500" + }, + { + "selected": false, + "text": "800", + "value": "800" + }, + { + "selected": false, + "text": "1000", + "value": "1000" + }, + { + "selected": false, + "text": "1500", + "value": "1500" + } + ], + "query": "2,20,50,100,200,350,500,800,1000,1500", + "type": "custom" + }, + { + "current": { + "text": "No", + "value": "No" + }, + "description": "When yes, log space threshold won't be considered. Rather threshold are used based on server level LogSaver jobs", + "includeAll": false, + "label": "Pre Validated Logs", + "name": "only_validated_log_thresholds", + "options": [ + { + "selected": false, + "text": "Yes", + "value": "Yes" + }, + { + "selected": true, + "text": "No", + "value": "No" + } + ], + "query": "Yes,No", + "type": "custom" + }, + { + "current": { + "text": "70", + "value": "70" + }, + "description": "Effective only when {Pre Validated Logs} is NO", + "includeAll": false, + "label": "Log Used %", + "name": "log_used_pct_threshold", + "options": [ + { + "selected": false, + "text": "30", + "value": "30" + }, + { + "selected": false, + "text": "50", + "value": "50" + }, + { + "selected": true, + "text": "70", + "value": "70" + }, + { + "selected": false, + "text": "80", + "value": "80" + }, + { + "selected": false, + "text": "90", + "value": "90" + }, + { + "selected": false, + "text": "95", + "value": "95" + } + ], + "query": "30,50,70,80,90,95", + "type": "custom" + }, + { + "current": { + "text": "20", + "value": "20" + }, + "description": "Effective only when {Pre Validated Logs} is NO", + "includeAll": false, + "label": "Log Used Gb", + "name": "log_used_gb_threshold", + "options": [ + { + "selected": false, + "text": "2", + "value": "2" + }, + { + "selected": true, + "text": "20", + "value": "20" + }, + { + "selected": false, + "text": "50", + "value": "50" + }, + { + "selected": false, + "text": "100", + "value": "100" + }, + { + "selected": false, + "text": "200", + "value": "200" + }, + { + "selected": false, + "text": "350", + "value": "350" + }, + { + "selected": false, + "text": "500", + "value": "500" + }, + { + "selected": false, + "text": "800", + "value": "800" + }, + { + "selected": false, + "text": "1000", + "value": "1000" + }, + { + "selected": false, + "text": "1500", + "value": "1500" + } + ], + "query": "2,20,50,100,200,350,500,800,1000,1500", + "type": "custom" + }, + { + "current": { + "text": "80", + "value": "80" + }, + "includeAll": false, + "label": "Disk Warning %", + "name": "disk_warning_pct", + "options": [ + { + "selected": false, + "text": "30", + "value": "30" + }, + { + "selected": false, + "text": "50", + "value": "50" + }, + { + "selected": false, + "text": "70", + "value": "70" + }, + { + "selected": true, + "text": "80", + "value": "80" + }, + { + "selected": false, + "text": "85", + "value": "85" + }, + { + "selected": false, + "text": "90", + "value": "90" + } + ], + "query": "30,50,70,80,85,90", + "type": "custom" + }, + { + "current": { + "text": "90", + "value": "90" + }, + "includeAll": false, + "label": "Disk Critical %", + "name": "disk_critical_pct", + "options": [ + { + "selected": true, + "text": "90", + "value": "90" + }, + { + "selected": false, + "text": "92", + "value": "92" + }, + { + "selected": false, + "text": "95", + "value": "95" + }, + { + "selected": false, + "text": "98", + "value": "98" + } + ], + "query": "90,92,95,98", + "type": "custom" + }, + { + "current": { + "text": "250", + "value": "250" + }, + "description": "Free Space in Gb to segregate Large Disk from Small Disk", + "includeAll": false, + "label": "Free Disk Boundary (Gb)", + "name": "disk_threshold_gb", + "options": [ + { + "selected": false, + "text": "1", + "value": "1" + }, + { + "selected": false, + "text": "5", + "value": "5" + }, + { + "selected": false, + "text": "10", + "value": "10" + }, + { + "selected": false, + "text": "20", + "value": "20" + }, + { + "selected": false, + "text": "50", + "value": "50" + }, + { + "selected": false, + "text": "100", + "value": "100" + }, + { + "selected": false, + "text": "200", + "value": "200" + }, + { + "selected": true, + "text": "250", + "value": "250" + }, + { + "selected": false, + "text": "350", + "value": "350" + }, + { + "selected": false, + "text": "500", + "value": "500" + }, + { + "selected": false, + "text": "800", + "value": "800" + }, + { + "selected": false, + "text": "1024", + "value": "1024" + } + ], + "query": "1,5,10,20,50,100,200,250,350,500,800,1024", + "type": "custom" + }, + { + "current": { + "text": "95", + "value": "95" + }, + "description": "Disk full threshold to consider when disk has more than {Free Disk Boundary} gb free space", + "includeAll": false, + "label": "Large Disk %", + "name": "large_disk_threshold_pct", + "options": [ + { + "selected": false, + "text": "85", + "value": "85" + }, + { + "selected": false, + "text": "90", + "value": "90" + }, + { + "selected": false, + "text": "92", + "value": "92" + }, + { + "selected": true, + "text": "95", + "value": "95" + }, + { + "selected": false, + "text": "97", + "value": "97" + }, + { + "selected": false, + "text": "98", + "value": "98" + } + ], + "query": "85,90,92,95,97,98", + "type": "custom" + }, + { + "current": { + "text": "8", + "value": "8" + }, + "includeAll": false, + "label": "Full Threshold (Days)", + "name": "full_threshold_days", + "options": [ + { + "selected": false, + "text": "1", + "value": "1" + }, + { + "selected": false, + "text": "3", + "value": "3" + }, + { + "selected": false, + "text": "7", + "value": "7" + }, + { + "selected": true, + "text": "8", + "value": "8" + }, + { + "selected": false, + "text": "9", + "value": "9" + }, + { + "selected": false, + "text": "10", + "value": "10" + }, + { + "selected": false, + "text": "15", + "value": "15" + }, + { + "selected": false, + "text": "30", + "value": "30" + }, + { + "selected": false, + "text": "90", + "value": "90" + }, + { + "selected": false, + "text": "366", + "value": "366" + } + ], + "query": "1,3,7,8,9,10,15,30,90,366", + "type": "custom" + }, + { + "current": { + "text": "26", + "value": "26" + }, + "includeAll": false, + "label": "Diff Threshold (Hrs)", + "name": "diff_threshold_hours", + "options": [ + { + "selected": false, + "text": "4", + "value": "4" + }, + { + "selected": false, + "text": "8", + "value": "8" + }, + { + "selected": false, + "text": "12", + "value": "12" + }, + { + "selected": false, + "text": "24", + "value": "24" + }, + { + "selected": true, + "text": "26", + "value": "26" + }, + { + "selected": false, + "text": "28", + "value": "28" + }, + { + "selected": false, + "text": "32", + "value": "32" + }, + { + "selected": false, + "text": "36", + "value": "36" + }, + { + "selected": false, + "text": "48", + "value": "48" + } + ], + "query": "4,8,12,24,26,28,32,36,48", + "type": "custom" + }, + { + "current": { + "text": "60", + "value": "60" + }, + "includeAll": false, + "label": "TLog Threshold (Min)", + "name": "tlog_threshold_minutes", + "options": [ + { + "selected": false, + "text": "5", + "value": "5" + }, + { + "selected": false, + "text": "10", + "value": "10" + }, + { + "selected": false, + "text": "15", + "value": "15" + }, + { + "selected": false, + "text": "20", + "value": "20" + }, + { + "selected": false, + "text": "30", + "value": "30" + }, + { + "selected": false, + "text": "45", + "value": "45" + }, + { + "selected": true, + "text": "60", + "value": "60" + }, + { + "selected": false, + "text": "90", + "value": "90" + }, + { + "selected": false, + "text": "120", + "value": "120" + }, + { + "selected": false, + "text": "240", + "value": "240" + }, + { + "selected": false, + "text": "360", + "value": "360" + } + ], + "query": "5,10,15,20,30,45,60,90,120,240,360", + "type": "custom" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect count(sql_instance)\nfrom dbo.instance_details id\nwhere is_enabled = 1 and is_alias = 0\nand (is_available = 0 or is_linked_server_working = 0)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = id.sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "hide": 2, + "includeAll": false, + "label": "Offline Servers Count", + "name": "offline_servers_count", + "options": [], + "query": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect count(sql_instance)\nfrom dbo.instance_details id\nwhere is_enabled = 1 and is_alias = 0\nand (is_available = 0 or is_linked_server_working = 0)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = id.sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect count(distinct sql_instance)\nfrom dbo.instance_details id\nwhere is_enabled = 1 and is_alias = 1\nand (is_available = 0 or is_linked_server_working = 0)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = id.source_sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "hide": 2, + "includeAll": false, + "label": "Offline Alias Count", + "name": "offline_alias_count", + "options": [], + "query": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect count(distinct sql_instance)\nfrom dbo.instance_details id\nwhere is_enabled = 1 and is_alias = 1\nand (is_available = 0 or is_linked_server_working = 0)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = id.source_sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect count(*) as counts\nfrom dbo.services_all_servers sas\ncross apply (select top 1 sql_instance_with_port = coalesce(id.sql_instance+','+id.sql_instance_port,id.sql_instance) \n\t\t\t\tfrom dbo.instance_details id where id.is_enabled = 1 \n\t\t\t\tand id.is_available = 1 and id.sql_instance = sas.sql_instance) id\nwhere 1=1\nand sas.service_type = 'Agent'\nand sas.status_desc <> 'Running'\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = sas.sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "hide": 2, + "includeAll": false, + "label": "Offline SQLAgent Services Count", + "name": "offline_agent_count", + "options": [], + "query": "declare @sql nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\nselect count(*) as counts\nfrom dbo.services_all_servers sas\ncross apply (select top 1 sql_instance_with_port = coalesce(id.sql_instance+','+id.sql_instance_port,id.sql_instance) \n\t\t\t\tfrom dbo.instance_details id where id.is_enabled = 1 \n\t\t\t\tand id.is_available = 1 and id.sql_instance = sas.sql_instance) id\nwhere 1=1\nand sas.service_type = 'Agent'\nand sas.status_desc <> 'Running'\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = sas.sql_instance)\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\n;with t_cte as (\n\tselect\tsrv_name, domain, host_name, product_version, major_version_number, minor_version_number, cpu_count, \n\t scheduler_count, total_physical_memory_kb, os_start_time_utc, sqlserver_start_time_utc\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [os_uptime]\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [sqlserver_uptime]\n\t\t\t,datediff(day,os_start_time_utc,GETUTCDATE()) as [os_uptime_days]\n\tfrom dbo.vw_all_server_info\n)\nselect count(*)\n/*\nselect srv_name, domain, host_name, product_version, major_version_number, minor_version_number\n ,[CPU (OS / SQL)] = convert(varchar,cpu_count)+' / '+convert(varchar,scheduler_count)\n ,total_physical_memory_kb ,cpu_count, scheduler_count, [os_uptime_days], os_start_time_utc, sqlserver_start_time_utc, [os_uptime], [sqlserver_uptime]\n\t\t ,[Is MS Supported] = case when c.MainstreamSupportEndDate < getdate() then convert(bit,0) else convert(bit,1) end\n*/\nfrom t_cte cte\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t\t,[MinorVersionNumber]\n\t\t\t\t,[Branch]\n\t\t\t\t,[Url]\n\t\t\t\t,[ReleaseDate]\n\t\t\t\t,[MainstreamSupportEndDate]\n\t\t\t\t,[ExtendedSupportEndDate]\n\t\t\t\t,[MajorVersionName]\n\t\t\t\t,[MinorVersionName]\t \n\t\tfrom [master].[dbo].[SqlServerVersions] as c\n\t\twhere [MajorVersionNumber] = cte.major_version_number\n\t\tand [MinorVersionNumber] <= cte.minor_version_number\n\t\torder by [MinorVersionNumber] desc\n) as c\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t,[MinorVersionNumber]\n\t\t\t,[Branch]\n\t\t\t,[Url]\n\t\t\t,[ReleaseDate]\n\t\t\t,[MainstreamSupportEndDate]\n\t\t\t,[ExtendedSupportEndDate]\n\t\t\t,[MajorVersionName]\n\t\t\t,[MinorVersionName]\t \n\tfrom [master].[dbo].[SqlServerVersions] as c\n\twhere [MajorVersionNumber] = cte.major_version_number\n\torder by [MinorVersionNumber] desc\n) as l;\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "hide": 2, + "includeAll": false, + "label": "Basic Info Servers Count", + "name": "basic_info_servers_count", + "options": [], + "query": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\n;with t_cte as (\n\tselect\tsrv_name, domain, host_name, product_version, major_version_number, minor_version_number, cpu_count, \n\t scheduler_count, total_physical_memory_kb, os_start_time_utc, sqlserver_start_time_utc\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [os_uptime]\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [sqlserver_uptime]\n\t\t\t,datediff(day,os_start_time_utc,GETUTCDATE()) as [os_uptime_days]\n\tfrom dbo.vw_all_server_info\n)\nselect count(*)\n/*\nselect srv_name, domain, host_name, product_version, major_version_number, minor_version_number\n ,[CPU (OS / SQL)] = convert(varchar,cpu_count)+' / '+convert(varchar,scheduler_count)\n ,total_physical_memory_kb ,cpu_count, scheduler_count, [os_uptime_days], os_start_time_utc, sqlserver_start_time_utc, [os_uptime], [sqlserver_uptime]\n\t\t ,[Is MS Supported] = case when c.MainstreamSupportEndDate < getdate() then convert(bit,0) else convert(bit,1) end\n*/\nfrom t_cte cte\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t\t,[MinorVersionNumber]\n\t\t\t\t,[Branch]\n\t\t\t\t,[Url]\n\t\t\t\t,[ReleaseDate]\n\t\t\t\t,[MainstreamSupportEndDate]\n\t\t\t\t,[ExtendedSupportEndDate]\n\t\t\t\t,[MajorVersionName]\n\t\t\t\t,[MinorVersionName]\t \n\t\tfrom [master].[dbo].[SqlServerVersions] as c\n\t\twhere [MajorVersionNumber] = cte.major_version_number\n\t\tand [MinorVersionNumber] <= cte.minor_version_number\n\t\torder by [MinorVersionNumber] desc\n) as c\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t,[MinorVersionNumber]\n\t\t\t,[Branch]\n\t\t\t,[Url]\n\t\t\t,[ReleaseDate]\n\t\t\t,[MainstreamSupportEndDate]\n\t\t\t,[ExtendedSupportEndDate]\n\t\t\t,[MajorVersionName]\n\t\t\t,[MinorVersionName]\t \n\tfrom [master].[dbo].[SqlServerVersions] as c\n\twhere [MajorVersionNumber] = cte.major_version_number\n\torder by [MinorVersionNumber] desc\n) as l;\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refresh": 1, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\n;with t_cte as (\n\tselect\tsrv_name, domain, host_name, product_version, major_version_number, minor_version_number, cpu_count, \n\t scheduler_count, total_physical_memory_kb, os_start_time_utc, sqlserver_start_time_utc\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [os_uptime]\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [sqlserver_uptime]\n\t\t\t,datediff(day,os_start_time_utc,GETUTCDATE()) as [os_uptime_days]\n\tfrom dbo.vw_all_server_info\n)\nselect count(*)\nfrom t_cte cte\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t\t,[MinorVersionNumber]\n\t\t\t\t,[Branch]\n\t\t\t\t,[Url]\n\t\t\t\t,[ReleaseDate]\n\t\t\t\t,[MainstreamSupportEndDate]\n\t\t\t\t,[ExtendedSupportEndDate]\n\t\t\t\t,[MajorVersionName]\n\t\t\t\t,[MinorVersionName]\t \n\t\tfrom [master].[dbo].[SqlServerVersions] as c\n\t\twhere [MajorVersionNumber] = cte.major_version_number\n\t\tand [MinorVersionNumber] <= cte.minor_version_number\n\t\torder by [MinorVersionNumber] desc\n) as c\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t,[MinorVersionNumber]\n\t\t\t,[Branch]\n\t\t\t,[Url]\n\t\t\t,[ReleaseDate]\n\t\t\t,[MainstreamSupportEndDate]\n\t\t\t,[ExtendedSupportEndDate]\n\t\t\t,[MajorVersionName]\n\t\t\t,[MinorVersionName]\t \n\tfrom [master].[dbo].[SqlServerVersions] as c\n\twhere [MajorVersionNumber] = cte.major_version_number\n\torder by [MinorVersionNumber] desc\n) as l\nwhere 1=1\nand c.ExtendedSupportEndDate < getdate();\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "hide": 2, + "includeAll": false, + "label": "Out of Supported Servers Count", + "name": "unsupported_servers_count", + "options": [], + "query": "declare @sql nvarchar(max);\nset quoted_identifier off;\nset @sql = \"\n/* $__dashboard */\n;with t_cte as (\n\tselect\tsrv_name, domain, host_name, product_version, major_version_number, minor_version_number, cpu_count, \n\t scheduler_count, total_physical_memory_kb, os_start_time_utc, sqlserver_start_time_utc\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,os_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [os_uptime]\n\t\t\t,Concat\n\t\t\t(\n\t\t\t\tRIGHT('000'+CAST(ISNULL((datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 / 24), 0) AS VARCHAR(3)),3)\n\t\t\t\t,' '\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t\t,':'\n\t\t\t\t,RIGHT('00'+CAST(ISNULL(datediff(second,sqlserver_start_time_utc,GETUTCDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)\n\t\t\t) as [sqlserver_uptime]\n\t\t\t,datediff(day,os_start_time_utc,GETUTCDATE()) as [os_uptime_days]\n\tfrom dbo.vw_all_server_info\n)\nselect count(*)\nfrom t_cte cte\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t\t,[MinorVersionNumber]\n\t\t\t\t,[Branch]\n\t\t\t\t,[Url]\n\t\t\t\t,[ReleaseDate]\n\t\t\t\t,[MainstreamSupportEndDate]\n\t\t\t\t,[ExtendedSupportEndDate]\n\t\t\t\t,[MajorVersionName]\n\t\t\t\t,[MinorVersionName]\t \n\t\tfrom [master].[dbo].[SqlServerVersions] as c\n\t\twhere [MajorVersionNumber] = cte.major_version_number\n\t\tand [MinorVersionNumber] <= cte.minor_version_number\n\t\torder by [MinorVersionNumber] desc\n) as c\nouter apply (\n\tselect\ttop 1 [MajorVersionNumber]\n\t\t\t--,[@server_minor_version_number] = @server_minor_version_number\n\t\t\t,[MinorVersionNumber]\n\t\t\t,[Branch]\n\t\t\t,[Url]\n\t\t\t,[ReleaseDate]\n\t\t\t,[MainstreamSupportEndDate]\n\t\t\t,[ExtendedSupportEndDate]\n\t\t\t,[MajorVersionName]\n\t\t\t,[MinorVersionName]\t \n\tfrom [master].[dbo].[SqlServerVersions] as c\n\twhere [MajorVersionNumber] = cte.major_version_number\n\torder by [MinorVersionNumber] desc\n) as l\nwhere 1=1\nand c.ExtendedSupportEndDate < getdate();\n\";\nset quoted_identifier off;\n\nexec dbo.sp_executesql @sql;", + "refresh": 1, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @_latency_minutes_threshold int = 20;\ndeclare @_latency_days_threshold int = 3;\ndeclare @t_servers_exception table (srv_name varchar(125));\n\ninsert @t_servers_exception -- list of servers that have permanent issues\nvalues ('196.1.115.169'),('196.1.115.212');\n\nselect\tcount(*) /*\n cli.srv_name, id.jobs_server,\n\t\tcli.performance_counters__latency_minutes,\n\t\txevent_metrics__latency_minutes = case when (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11) then cli.xevent_metrics__latency_minutes \n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.xevent_metrics__latency_minutes end,\n\t\tWhoIsActive__latency_minutes = case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then cli.WhoIsActive__latency_minutes \n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.WhoIsActive__latency_minutes end,\n\t\tcli.os_task_list__latency_minutes,\n\t\tcli.disk_space__latency_minutes, \n\t\tcli.file_io_stats__latency_minutes,\n\t\tcli.sql_agent_job_stats__latency_minutes,\n\t\tcli.memory_clerks__latency_minutes,\n\t\tcli.wait_stats__latency_minutes,\n\t\tcli.BlitzIndex__latency_days,\n\t\tcli.BlitzIndex_Mode0__latency_days,\n\t\t--cli.BlitzIndex_Mode1__latency_days,\n\t\t--cli.BlitzIndex_Mode4__latency_days,\n\t\t--collection_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), cli.collection_time)\n\t\t[Issues] = (case when cli.performance_counters__latency_minutes > @_latency_minutes_threshold then 'performance_counters,' else '' end)+\n\t\t\t\t\t(case when cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11 then 'xevent_metrics' else '' end)+\n\t\t\t\t\t(case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then 'WhoIsActive,' else '' end)+\n\t\t\t\t\t(case when cli.os_task_list__latency_minutes > @_latency_minutes_threshold then 'os_task_list,' else '' end)+\n\t\t\t\t\t(case when cli.disk_space__latency_minutes > @_latency_minutes_threshold*3 then 'disk_space,' else '' end)+\n\t\t\t\t\t(case when cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3 then 'file_io_stats,' else '' end)+\n\t\t\t\t\t(case when cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2 then 'wait_stats,' else '' end)+\n\t\t\t\t\t(case when cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5 then 'sql_agent_job_stats,' else '' end)+\n\t\t\t\t\t(case when cli.memory_clerks__latency_minutes > @_latency_minutes_threshold then 'memory_clerks,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex__latency_days > @_latency_days_threshold then 'BlitzIndex,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode0,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode1,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode4,' else '' end)\n\t\t--,cli.host_name ,si.major_version_number ,si.product_version ,si.edition, vi.os_cpu, vi.sql_cpu\n*/\nfrom dbo.all_server_collection_latency_info cli\njoin dbo.all_server_stable_info si on si.srv_name = cli.srv_name\njoin dbo.all_server_volatile_info vi on vi.srv_name = cli.srv_name\nouter apply (select STUFF((select ', '+id.collector_powershell_jobs_server from (select distinct id.collector_powershell_jobs_server from dbo.instance_details id where id.sql_instance = cli.srv_name and id.host_name = cli.host_name) id for xml path('')), 1, 1, '') as jobs_server ) id\nwhere 1=1\nand (\tcli.performance_counters__latency_minutes > @_latency_minutes_threshold\n\t\tor (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11)\n\t\tor (cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20)\n\t\tor cli.os_task_list__latency_minutes > @_latency_minutes_threshold\n\t\tor cli.disk_space__latency_minutes > @_latency_minutes_threshold*3\n\t\tor cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3\n\t\tor cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2\n\t\tor cli.BlitzIndex__latency_days > @_latency_days_threshold\n\t\tor cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4\n\t\t--or cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4\n\t\t--or cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4\n\t\tor cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5\n\t\tor cli.memory_clerks__latency_minutes > @_latency_minutes_threshold\n\t)\nand cli.srv_name not in (select e.srv_name from @t_servers_exception e)\n\n", + "hide": 2, + "includeAll": false, + "label": "Servers Count With Data Issue", + "name": "servers_with_data_issue_count", + "options": [], + "query": "declare @_latency_minutes_threshold int = 20;\ndeclare @_latency_days_threshold int = 3;\ndeclare @t_servers_exception table (srv_name varchar(125));\n\ninsert @t_servers_exception -- list of servers that have permanent issues\nvalues ('196.1.115.169'),('196.1.115.212');\n\nselect\tcount(*) /*\n cli.srv_name, id.jobs_server,\n\t\tcli.performance_counters__latency_minutes,\n\t\txevent_metrics__latency_minutes = case when (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11) then cli.xevent_metrics__latency_minutes \n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.xevent_metrics__latency_minutes end,\n\t\tWhoIsActive__latency_minutes = case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then cli.WhoIsActive__latency_minutes \n\t\t\t\t\t\t\t\t\t\t\t\t\twhen cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold then @_latency_minutes_threshold-1\n\t\t\t\t\t\t\t\t\t\t\t\t\telse cli.WhoIsActive__latency_minutes end,\n\t\tcli.os_task_list__latency_minutes,\n\t\tcli.disk_space__latency_minutes, \n\t\tcli.file_io_stats__latency_minutes,\n\t\tcli.sql_agent_job_stats__latency_minutes,\n\t\tcli.memory_clerks__latency_minutes,\n\t\tcli.wait_stats__latency_minutes,\n\t\tcli.BlitzIndex__latency_days,\n\t\tcli.BlitzIndex_Mode0__latency_days,\n\t\t--cli.BlitzIndex_Mode1__latency_days,\n\t\t--cli.BlitzIndex_Mode4__latency_days,\n\t\t--collection_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), cli.collection_time)\n\t\t[Issues] = (case when cli.performance_counters__latency_minutes > @_latency_minutes_threshold then 'performance_counters,' else '' end)+\n\t\t\t\t\t(case when cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11 then 'xevent_metrics' else '' end)+\n\t\t\t\t\t(case when cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 then 'WhoIsActive,' else '' end)+\n\t\t\t\t\t(case when cli.os_task_list__latency_minutes > @_latency_minutes_threshold then 'os_task_list,' else '' end)+\n\t\t\t\t\t(case when cli.disk_space__latency_minutes > @_latency_minutes_threshold*3 then 'disk_space,' else '' end)+\n\t\t\t\t\t(case when cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3 then 'file_io_stats,' else '' end)+\n\t\t\t\t\t(case when cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2 then 'wait_stats,' else '' end)+\n\t\t\t\t\t(case when cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5 then 'sql_agent_job_stats,' else '' end)+\n\t\t\t\t\t(case when cli.memory_clerks__latency_minutes > @_latency_minutes_threshold then 'memory_clerks,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex__latency_days > @_latency_days_threshold then 'BlitzIndex,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode0,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode1,' else '' end)+\n\t\t\t\t\t(case when cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4 then 'BlitzIndex_Mode4,' else '' end)\n\t\t--,cli.host_name ,si.major_version_number ,si.product_version ,si.edition, vi.os_cpu, vi.sql_cpu\n*/\nfrom dbo.all_server_collection_latency_info cli\njoin dbo.all_server_stable_info si on si.srv_name = cli.srv_name\njoin dbo.all_server_volatile_info vi on vi.srv_name = cli.srv_name\nouter apply (select STUFF((select ', '+id.collector_powershell_jobs_server from (select distinct id.collector_powershell_jobs_server from dbo.instance_details id where id.sql_instance = cli.srv_name and id.host_name = cli.host_name) id for xml path('')), 1, 1, '') as jobs_server ) id\nwhere 1=1\nand (\tcli.performance_counters__latency_minutes > @_latency_minutes_threshold\n\t\tor (cli.xevent_metrics__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20 and si.major_version_number >= 11)\n\t\tor (cli.WhoIsActive__latency_minutes > @_latency_minutes_threshold*2 and vi.sql_cpu > 20)\n\t\tor cli.os_task_list__latency_minutes > @_latency_minutes_threshold\n\t\tor cli.disk_space__latency_minutes > @_latency_minutes_threshold*3\n\t\tor cli.file_io_stats__latency_minutes > @_latency_minutes_threshold*3\n\t\tor cli.wait_stats__latency_minutes > @_latency_minutes_threshold*2\n\t\tor cli.BlitzIndex__latency_days > @_latency_days_threshold\n\t\tor cli.BlitzIndex_Mode0__latency_days > @_latency_days_threshold*4\n\t\t--or cli.BlitzIndex_Mode1__latency_days > @_latency_days_threshold*4\n\t\t--or cli.BlitzIndex_Mode4__latency_days > @_latency_days_threshold*4\n\t\tor cli.sql_agent_job_stats__latency_minutes > @_latency_minutes_threshold*1.5\n\t\tor cli.memory_clerks__latency_minutes > @_latency_minutes_threshold\n\t)\nand cli.srv_name not in (select e.srv_name from @t_servers_exception e)\n\n", + "refresh": 1, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n--declare @sql_instance varchar(255);\n--declare @backup_type varchar(2000);\n--declare @backup_description varchar(2000);\n--declare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n--declare @delta_minutes int;\n--declare @disk_drive varchar(255) = '$disk_drive';\n--declare @database varchar(255) = '$database';\n--declare @filter_for_latency varchar(50) = '$filter_for_latency';\ndeclare @full_threshold_days int = $full_threshold_days;\ndeclare @diff_threshold_hours int = $diff_threshold_hours;\ndeclare @tlog_threshold_minutes int = $tlog_threshold_minutes;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n--set @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n--set @delta_minutes = $cpu_delta_minutes;\n\n--set @backup_type = '${backup_type:csv}';\n--set @backup_description = case when ltrim(rtrim('$backup_description')) = '' then null else ltrim(rtrim('$backup_description')) end;\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2, \n\t\t\t\t\t\t\t\t@full_threshold_days int, @diff_threshold_hours int, @tlog_threshold_minutes int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - Dashboard [$__dashboard] */\nset nocount on;\n\n;with t_backups as (\n\t\tselect [collection_time_utc], [sql_instance], [database_name], [backup_type], [log_backups_count], [backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description]\n\t\tfrom dbo.backups_all_servers bas\n)\n,t_pivot as (\n\t\tselect\t[sql_instance], [database_name]\n\t\t\t\t,[recovery_model] = max([recovery_model])\n\t\t\t\t,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[log_backups_count] = max([log_backups_count])\n\t\t\t\t,[database_creation_date_utc] = max([database_creation_date_utc])\n\t\t\t\t,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end)\n\t\tfrom t_backups bkp\n\t\twhere 1=1\n\t\tgroup by [sql_instance], [database_name]\n)\n,t_latency as (\n\t\t\tselect \t[sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(day,[full_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\t\t\t\t\t\t\n\t\t\t\t\t\t\t[diff_latency_hours] = case when [diff_backup_time_utc] is null \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen @full_threshold_days * 24\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(hour,[diff_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen recovery_model <> 'SIMPLE'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when [tlog_backup_time_utc] is null then @full_threshold_days * 1440\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t when [tlog_backup_time_utc] is not null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t then datediff(minute,[tlog_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t else null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t end\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb],\n\t\t\t\t\t\t\t[tlog_compressed_size_mb], [log_backups_count],\n\t\t\t\t\t\t\t[database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file]\n\t\t\tfrom t_pivot as bkp\n\t\t\twhere 1=1\n)\nselect count(*)\n/*\nselect [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days], [diff_latency_hours], [tlog_latency_minutes],\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], \n\t\t\t\t\t\t\t[tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], \n\t\t\t\t\t\t\t[full_backup_file], [diff_backup_file], [tlog_backup_file]\n*/\nfrom t_latency as l\nwhere 1=1\nAND (\t\t(full_latency_days is null or full_latency_days >= @full_threshold_days)\n\t\tOR \t(diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours)\n\t\tOR\t(tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes)\n\t\t)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = l.sql_instance and (s.hadr_strategy is null or s.hadr_strategy <> 'ag'))\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc, @full_threshold_days, @diff_threshold_hours, \n\t\t\t\t\t\t\t\t\t\t\t\t@tlog_threshold_minutes;", + "hide": 2, + "includeAll": false, + "label": "Database Count With Backup Issue", + "name": "nonag_database_counts_with_backup_issue", + "options": [], + "query": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n--declare @sql_instance varchar(255);\n--declare @backup_type varchar(2000);\n--declare @backup_description varchar(2000);\n--declare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n--declare @delta_minutes int;\n--declare @disk_drive varchar(255) = '$disk_drive';\n--declare @database varchar(255) = '$database';\n--declare @filter_for_latency varchar(50) = '$filter_for_latency';\ndeclare @full_threshold_days int = $full_threshold_days;\ndeclare @diff_threshold_hours int = $diff_threshold_hours;\ndeclare @tlog_threshold_minutes int = $tlog_threshold_minutes;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n--set @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n--set @delta_minutes = $cpu_delta_minutes;\n\n--set @backup_type = '${backup_type:csv}';\n--set @backup_description = case when ltrim(rtrim('$backup_description')) = '' then null else ltrim(rtrim('$backup_description')) end;\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2, \n\t\t\t\t\t\t\t\t@full_threshold_days int, @diff_threshold_hours int, @tlog_threshold_minutes int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - Dashboard [$__dashboard] */\nset nocount on;\n\n;with t_backups as (\n\t\tselect [collection_time_utc], [sql_instance], [database_name], [backup_type], [log_backups_count], [backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description]\n\t\tfrom dbo.backups_all_servers bas\n)\n,t_pivot as (\n\t\tselect\t[sql_instance], [database_name]\n\t\t\t\t,[recovery_model] = max([recovery_model])\n\t\t\t\t,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[log_backups_count] = max([log_backups_count])\n\t\t\t\t,[database_creation_date_utc] = max([database_creation_date_utc])\n\t\t\t\t,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end)\n\t\tfrom t_backups bkp\n\t\twhere 1=1\n\t\tgroup by [sql_instance], [database_name]\n)\n,t_latency as (\n\t\t\tselect \t[sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(day,[full_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\t\t\t\t\t\t\n\t\t\t\t\t\t\t[diff_latency_hours] = case when [diff_backup_time_utc] is null \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen @full_threshold_days * 24\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(hour,[diff_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen recovery_model <> 'SIMPLE'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when [tlog_backup_time_utc] is null then @full_threshold_days * 1440\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t when [tlog_backup_time_utc] is not null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t then datediff(minute,[tlog_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t else null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t end\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb],\n\t\t\t\t\t\t\t[tlog_compressed_size_mb], [log_backups_count],\n\t\t\t\t\t\t\t[database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file]\n\t\t\tfrom t_pivot as bkp\n\t\t\twhere 1=1\n)\nselect count(*)\n/*\nselect [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days], [diff_latency_hours], [tlog_latency_minutes],\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], \n\t\t\t\t\t\t\t[tlog_backup_size_mb], [tlog_compressed_size_mb], [log_backups_count], [database_creation_date_utc], \n\t\t\t\t\t\t\t[full_backup_file], [diff_backup_file], [tlog_backup_file]\n*/\nfrom t_latency as l\nwhere 1=1\nAND (\t\t(full_latency_days is null or full_latency_days >= @full_threshold_days)\n\t\tOR \t(diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours)\n\t\tOR\t(tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes)\n\t\t)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = l.sql_instance and (s.hadr_strategy is null or s.hadr_strategy <> 'ag'))\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc, @full_threshold_days, @diff_threshold_hours, \n\t\t\t\t\t\t\t\t\t\t\t\t@tlog_threshold_minutes;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n--declare @sql_instance varchar(255);\n--declare @backup_type varchar(2000);\n--declare @backup_description varchar(2000);\n--declare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n--declare @delta_minutes int;\n--declare @disk_drive varchar(255) = '$disk_drive';\n--declare @database varchar(255) = '$database';\n--declare @filter_for_latency varchar(50) = '$filter_for_latency';\ndeclare @full_threshold_days int = $full_threshold_days;\ndeclare @diff_threshold_hours int = $diff_threshold_hours;\ndeclare @tlog_threshold_minutes int = $tlog_threshold_minutes;\ndeclare @_has_inventory bit = 0;\n\n\n--set @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n--set @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n--set @delta_minutes = $cpu_delta_minutes;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @backup_type = '${backup_type:csv}';\n--set @backup_description = case when ltrim(rtrim('$backup_description')) = '' then null else ltrim(rtrim('$backup_description')) end;\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2, \n\t\t\t\t\t\t\t\t@full_threshold_days int, @diff_threshold_hours int, @tlog_threshold_minutes int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - Dashboard [Monitoring - Live - All Servers] */\nset nocount on;\n\n;with t_backups as (\n\tselect [collection_time_utc], [sql_instance], s.ag_replicas, [database_name], [backup_type], [log_backups_count], \n\t\t\t[backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description]\n\t\t\t,db_rowid = ROW_NUMBER()over(partition by s.ag_replicas, [database_name], [backup_type] order by [backup_start_date_utc] desc)\n\tfrom dbo.backups_all_servers bas\n\tjoin dbo.sma_sql_servers s on s.server = bas.sql_instance and s.is_decommissioned = 0 and s.is_onboarded = 1 and s.hadr_strategy = 'ag'\n\twhere 1=1\n\tand charindex(',',s.ag_replicas) > 0\n)\n,t_pivot as (\n\t\tselect\tag_replicas, [sql_instance], [database_name]\n\t\t\t\t,[recovery_model] = max([recovery_model])\n\t\t\t\t,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[log_backups_count] = max([log_backups_count])\n\t\t\t\t,[database_creation_date_utc] = max([database_creation_date_utc])\n\t\t\t\t,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end)\n\t\tfrom t_backups bkp\n\t\twhere 1=1\n\t\tand db_rowid = 1\n\t\tgroup by ag_replicas, [sql_instance], [database_name]\n)\n,t_latency as (\n\t\t\tselect \tag_replicas, [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(day,[full_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\t\t\t\t\t\t\n\t\t\t\t\t\t\t[diff_latency_hours] = case when [diff_backup_time_utc] is null \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen @full_threshold_days * 24\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(hour,[diff_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen recovery_model <> 'SIMPLE'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when [tlog_backup_time_utc] is null then @full_threshold_days * 1440\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t when [tlog_backup_time_utc] is not null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t then datediff(minute,[tlog_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t else null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t end\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb],\n\t\t\t\t\t\t\t[tlog_compressed_size_mb], [log_backups_count],\n\t\t\t\t\t\t\t[database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file]\n\t\t\tfrom t_pivot as bkp\n\t\t\twhere 1=1\n)\nselect count(*)\nfrom t_latency as l\nwhere 1=1\nAND (\t\t(full_latency_days is null or full_latency_days >= @full_threshold_days)\n\t\tOR \t(diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours)\n\t\tOR\t(tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes)\n\t\t)\n--order by ag_replicas, database_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc, @full_threshold_days, @diff_threshold_hours, \n\t\t\t\t\t\t\t\t\t\t\t\t@tlog_threshold_minutes;", + "hide": 2, + "includeAll": false, + "label": "Database Count With Backup Issue", + "name": "ag_database_counts_with_backup_issue", + "options": [], + "query": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n--declare @sql_instance varchar(255);\n--declare @backup_type varchar(2000);\n--declare @backup_description varchar(2000);\n--declare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n--declare @delta_minutes int;\n--declare @disk_drive varchar(255) = '$disk_drive';\n--declare @database varchar(255) = '$database';\n--declare @filter_for_latency varchar(50) = '$filter_for_latency';\ndeclare @full_threshold_days int = $full_threshold_days;\ndeclare @diff_threshold_hours int = $diff_threshold_hours;\ndeclare @tlog_threshold_minutes int = $tlog_threshold_minutes;\ndeclare @_has_inventory bit = 0;\n\n\n--set @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n--set @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n--set @delta_minutes = $cpu_delta_minutes;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @backup_type = '${backup_type:csv}';\n--set @backup_description = case when ltrim(rtrim('$backup_description')) = '' then null else ltrim(rtrim('$backup_description')) end;\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2, \n\t\t\t\t\t\t\t\t@full_threshold_days int, @diff_threshold_hours int, @tlog_threshold_minutes int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - Dashboard [Monitoring - Live - All Servers] */\nset nocount on;\n\n;with t_backups as (\n\tselect [collection_time_utc], [sql_instance], s.ag_replicas, [database_name], [backup_type], [log_backups_count], \n\t\t\t[backup_start_date_utc], [backup_finish_date_utc], [latest_backup_location], [backup_size_mb], [compressed_backup_size_mb], [first_lsn], [last_lsn], [checkpoint_lsn], [database_backup_lsn], [database_creation_date_utc], [backup_software], [recovery_model], [compatibility_level], [device_type], [description]\n\t\t\t,db_rowid = ROW_NUMBER()over(partition by s.ag_replicas, [database_name], [backup_type] order by [backup_start_date_utc] desc)\n\tfrom dbo.backups_all_servers bas\n\tjoin dbo.sma_sql_servers s on s.server = bas.sql_instance and s.is_decommissioned = 0 and s.is_onboarded = 1 and s.hadr_strategy = 'ag'\n\twhere 1=1\n\tand charindex(',',s.ag_replicas) > 0\n)\n,t_pivot as (\n\t\tselect\tag_replicas, [sql_instance], [database_name]\n\t\t\t\t,[recovery_model] = max([recovery_model])\n\t\t\t\t,[full_backup_time_utc] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[full_backup_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[full_compressed_size_mb] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[diff_backup_time_utc] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[diff_backup_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[diff_compressed_size_mb] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[tlog_backup_time_utc] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_finish_date_utc] else null end)\n\t\t\t\t,[tlog_backup_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[backup_size_mb] else null end)\n\t\t\t\t,[tlog_compressed_size_mb] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[compressed_backup_size_mb] else null end)\n\t\t\t\t,[log_backups_count] = max([log_backups_count])\n\t\t\t\t,[database_creation_date_utc] = max([database_creation_date_utc])\n\t\t\t\t,[full_backup_file] = max(case when bkp.[backup_type] = 'Full Database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[diff_backup_file] = max(case when bkp.[backup_type] = 'Differential database Backup' then bkp.[latest_backup_location] else null end)\n\t\t\t\t,[tlog_backup_file] = max(case when bkp.[backup_type] = 'Transaction Log Backup' then bkp.[latest_backup_location] else null end)\n\t\tfrom t_backups bkp\n\t\twhere 1=1\n\t\tand db_rowid = 1\n\t\tgroup by ag_replicas, [sql_instance], [database_name]\n)\n,t_latency as (\n\t\t\tselect \tag_replicas, [sql_instance], [database_name], [recovery_model], \t\t\t\t\n\t\t\t\t\t\t\t[full_latency_days] = case when [full_backup_time_utc] is null then @full_threshold_days * 10\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(day,[full_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\t\t\t\t\t\t\n\t\t\t\t\t\t\t[diff_latency_hours] = case when [diff_backup_time_utc] is null \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when (datediff(day,[full_backup_time_utc],getutcdate()) > @full_threshold_days) and (@full_threshold_days >= 7)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen @full_threshold_days * 24\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen (datediff(day,[full_backup_time_utc],getutcdate())*24) > @diff_threshold_hours\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen ( (datediff(day,[full_backup_time_utc],getutcdate())-1) * 24 )\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse datediff(hour,[diff_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[tlog_latency_minutes] = case when recovery_model = 'SIMPLE' then null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\twhen recovery_model <> 'SIMPLE'\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tthen\tcase when [tlog_backup_time_utc] is null then @full_threshold_days * 1440\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t when [tlog_backup_time_utc] is not null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t then datediff(minute,[tlog_backup_time_utc],getutcdate())\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t else null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t end\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\telse null\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tend,\n\t\t\t\t\t\t\t[full_backup_time_utc], [diff_backup_time_utc], [tlog_backup_time_utc], \n\t\t\t\t\t\t\t[full_backup_size_mb], [full_compressed_size_mb], [diff_backup_size_mb], [diff_compressed_size_mb], [tlog_backup_size_mb],\n\t\t\t\t\t\t\t[tlog_compressed_size_mb], [log_backups_count],\n\t\t\t\t\t\t\t[database_creation_date_utc], [full_backup_file], [diff_backup_file], [tlog_backup_file]\n\t\t\tfrom t_pivot as bkp\n\t\t\twhere 1=1\n)\nselect count(*)\nfrom t_latency as l\nwhere 1=1\nAND (\t\t(full_latency_days is null or full_latency_days >= @full_threshold_days)\n\t\tOR \t(diff_latency_hours is not null and diff_latency_hours >= @diff_threshold_hours)\n\t\tOR\t(tlog_latency_minutes is not null and tlog_latency_minutes >= @tlog_threshold_minutes)\n\t\t)\n--order by ag_replicas, database_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc, @full_threshold_days, @diff_threshold_hours, \n\t\t\t\t\t\t\t\t\t\t\t\t@tlog_threshold_minutes;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @_buffer_time_minutes int = 30;\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\nset @_params = N'@_buffer_time_minutes int';\nset quoted_identifier off;\nset @_sql = \"\nselect\tcount(*) /* [Tsql-Stop-Job] = 'exec msdb.dbo.sp_stop_job @job_name = '''+sj.JobName+'''' ,\n\t\t[Tsql-Start-Job] = 'exec msdb.dbo.sp_start_job @job_name = '''+sj.JobName+'''' , \n\t\t\n\t\t[CollectionTimeUTC] = [UpdatedDateUTC],\n\t\t[sql_instance] = sql_instance_with_port, [JobName],\n\t\t[Job-Delay-Minutes] = case when sj.Last_Successful_ExecutionTime is null then 10080 else datediff(minute, sj.Last_Successful_ExecutionTime, dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@_buffer_time_minutes),getutcdate())) end,\n\t\t [Last_RunTime], [Last_Run_Duration_Seconds], [Last_Run_Outcome], \n\t\t [Successfull_Execution_ClockTime_Threshold_Minutes], \n\t\t [Expected_Max_Duration_Minutes],\n\t\t [Last_Successful_ExecutionTime], [Last_Successful_Execution_Hours], \n\t\t [Running_Since], [Running_StepName], [Running_Since_Min] \n*/\nfrom dbo.sql_agent_jobs_all_servers sj\ninner join dbo.services_all_servers sas\n\ton sas.sql_instance = sj.sql_instance\n\tand sas.service_type = 'Agent'\n\tand sas.status_desc = 'Running'\ncross apply (select top 1 sql_instance_with_port = coalesce(id.sql_instance+','+sql_instance_port,id.sql_instance) from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1) id\nwhere 1=1\n--and exists (select 1/0 from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1)\nand sj.JobCategory = '(dba) SQLMonitor'\nand sj.JobName like '(dba) %'\nand sj.IsDisabled = 0\nand (\tdateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@_buffer_time_minutes),getutcdate()) > sj.Last_Successful_ExecutionTime\n\t\t\tor sj.Last_Successful_ExecutionTime is null\n\t\t)\n--order by Last_Run_Outcome\n\"\nset quoted_identifier off;\n\nexec sp_executesql @_sql, @_params, @_buffer_time_minutes = @_buffer_time_minutes;", + "hide": 2, + "includeAll": false, + "label": "DBA Jobs With Issue", + "name": "dba_jobs_with_issue_counts", + "options": [], + "query": "declare @_buffer_time_minutes int = 30;\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\nset @_params = N'@_buffer_time_minutes int';\nset quoted_identifier off;\nset @_sql = \"\nselect\tcount(*) /* [Tsql-Stop-Job] = 'exec msdb.dbo.sp_stop_job @job_name = '''+sj.JobName+'''' ,\n\t\t[Tsql-Start-Job] = 'exec msdb.dbo.sp_start_job @job_name = '''+sj.JobName+'''' , \n\t\t\n\t\t[CollectionTimeUTC] = [UpdatedDateUTC],\n\t\t[sql_instance] = sql_instance_with_port, [JobName],\n\t\t[Job-Delay-Minutes] = case when sj.Last_Successful_ExecutionTime is null then 10080 else datediff(minute, sj.Last_Successful_ExecutionTime, dateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@_buffer_time_minutes),getutcdate())) end,\n\t\t [Last_RunTime], [Last_Run_Duration_Seconds], [Last_Run_Outcome], \n\t\t [Successfull_Execution_ClockTime_Threshold_Minutes], \n\t\t [Expected_Max_Duration_Minutes],\n\t\t [Last_Successful_ExecutionTime], [Last_Successful_Execution_Hours], \n\t\t [Running_Since], [Running_StepName], [Running_Since_Min] \n*/\nfrom dbo.sql_agent_jobs_all_servers sj\ninner join dbo.services_all_servers sas\n\ton sas.sql_instance = sj.sql_instance\n\tand sas.service_type = 'Agent'\n\tand sas.status_desc = 'Running'\ncross apply (select top 1 sql_instance_with_port = coalesce(id.sql_instance+','+sql_instance_port,id.sql_instance) from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1) id\nwhere 1=1\n--and exists (select 1/0 from dbo.instance_details id where id.sql_instance = sj.sql_instance and id.is_enabled = 1)\nand sj.JobCategory = '(dba) SQLMonitor'\nand sj.JobName like '(dba) %'\nand sj.IsDisabled = 0\nand (\tdateadd(minute,-(sj.Successfull_Execution_ClockTime_Threshold_Minutes+@_buffer_time_minutes),getutcdate()) > sj.Last_Successful_ExecutionTime\n\t\t\tor sj.Last_Successful_ExecutionTime is null\n\t\t)\n--order by Last_Run_Outcome\n\"\nset quoted_identifier off;\n\nexec sp_executesql @_sql, @_params, @_buffer_time_minutes = @_buffer_time_minutes;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @_disk_warning_pct decimal(20,2) = 80;\ndeclare @_disk_critical_pct decimal(20,2) = 90;\ndeclare @_disk_threshold_gb decimal(20,2) = 200;\ndeclare @_large_disk_threshold_pct decimal(20,2) = 95;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\nset @_params = '@disk_warning_pct decimal(20,2), @disk_critical_pct decimal(20,2), @disk_threshold_gb decimal(20,2), @large_disk_threshold_pct decimal(20,2)';\n\nset quoted_identifier off;\nset @_sql = \"\nselect\tcount(*) \nfrom dbo.disk_space_all_servers ds\nwhere ds.updated_date_utc >= dateadd(minute,-60,getutcdate())\nand (\t(\t(ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct)\n\t\t\tand ds.free_mb < (@disk_threshold_gb)*1024\n\t \t)\n\t\tor ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free %\n\t)\nand (ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_critical_pct)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ds.sql_instance)\n\";\nset quoted_identifier off;\n\nexec sp_executesql @_sql, @_params, @_disk_warning_pct, @_disk_critical_pct, @_disk_threshold_gb , @_large_disk_threshold_pct;", + "hide": 2, + "includeAll": false, + "label": "Disks in Critical State", + "name": "critical_disks_counts", + "options": [], + "query": "declare @_disk_warning_pct decimal(20,2) = 80;\ndeclare @_disk_critical_pct decimal(20,2) = 90;\ndeclare @_disk_threshold_gb decimal(20,2) = 200;\ndeclare @_large_disk_threshold_pct decimal(20,2) = 95;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\nset @_params = '@disk_warning_pct decimal(20,2), @disk_critical_pct decimal(20,2), @disk_threshold_gb decimal(20,2), @large_disk_threshold_pct decimal(20,2)';\n\nset quoted_identifier off;\nset @_sql = \"\nselect\tcount(*) \nfrom dbo.disk_space_all_servers ds\nwhere ds.updated_date_utc >= dateadd(minute,-60,getutcdate())\nand (\t(\t(ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct)\n\t\t\tand ds.free_mb < (@disk_threshold_gb)*1024\n\t \t)\n\t\tor ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free %\n\t)\nand (ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_critical_pct)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ds.sql_instance)\n\";\nset quoted_identifier off;\n\nexec sp_executesql @_sql, @_params, @_disk_warning_pct, @_disk_critical_pct, @_disk_threshold_gb , @_large_disk_threshold_pct;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @_disk_warning_pct decimal(20,2) = 80;\ndeclare @_disk_critical_pct decimal(20,2) = 90;\ndeclare @_disk_threshold_gb decimal(20,2) = 200;\ndeclare @_large_disk_threshold_pct decimal(20,2) = 95;\ndeclare @_critical_disks_counts int = 0;\ndeclare @_has_inventory bit = 0;\n\nset @_has_inventory = convert(bit,'${has_inventory}');\nset @_critical_disks_counts = '${critical_disks_counts}';\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\nset @_params = '@disk_warning_pct decimal(20,2), @disk_critical_pct decimal(20,2), @disk_threshold_gb decimal(20,2), @large_disk_threshold_pct decimal(20,2), @critical_disks_counts int';\n\nset quoted_identifier off;\nset @_sql = \"\nselect counts = count(*)-@critical_disks_counts\nfrom dbo.disk_space_all_servers ds\nwhere ds.updated_date_utc >= dateadd(minute,-60,getutcdate())\nand (\t(\t(ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct)\n\t\t\tand ds.free_mb < (@disk_threshold_gb)*1024\n\t \t)\n\t\tor ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free %\n\t)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ds.sql_instance)\n\";\nset quoted_identifier off;\n\nexec sp_executesql @_sql, @_params, @_disk_warning_pct, @_disk_critical_pct, @_disk_threshold_gb , @_large_disk_threshold_pct, @_critical_disks_counts;", + "hide": 2, + "includeAll": false, + "label": "Disks in Warning State", + "name": "warning_disks_counts", + "options": [], + "query": "declare @_disk_warning_pct decimal(20,2) = 80;\ndeclare @_disk_critical_pct decimal(20,2) = 90;\ndeclare @_disk_threshold_gb decimal(20,2) = 200;\ndeclare @_large_disk_threshold_pct decimal(20,2) = 95;\ndeclare @_critical_disks_counts int = 0;\ndeclare @_has_inventory bit = 0;\n\nset @_has_inventory = convert(bit,'${has_inventory}');\nset @_critical_disks_counts = '${critical_disks_counts}';\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\nset @_params = '@disk_warning_pct decimal(20,2), @disk_critical_pct decimal(20,2), @disk_threshold_gb decimal(20,2), @large_disk_threshold_pct decimal(20,2), @critical_disks_counts int';\n\nset quoted_identifier off;\nset @_sql = \"\nselect counts = count(*)-@critical_disks_counts\nfrom dbo.disk_space_all_servers ds\nwhere ds.updated_date_utc >= dateadd(minute,-60,getutcdate())\nand (\t(\t(ds.free_mb*100.0/ds.capacity_mb) < (100-@disk_warning_pct)\n\t\t\tand ds.free_mb < (@disk_threshold_gb)*1024\n\t \t)\n\t\tor ( (ds.free_mb*100.0/ds.capacity_mb) < (100-@large_disk_threshold_pct)) -- free %\n\t)\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ds.sql_instance)\n\";\nset quoted_identifier off;\n\nexec sp_executesql @_sql, @_params, @_disk_warning_pct, @_disk_critical_pct, @_disk_threshold_gb , @_large_disk_threshold_pct, @_critical_disks_counts;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @_sqltext nvarchar(max);\ndeclare @_params nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset @_sqltext = '\nselect\tcount(*)\nfrom dbo.log_space_consumers_all_servers ls\nwhere 1=1\nand ls.thresholds_validated = 1\nand (ls.updated_date_utc >= dateadd(minute,-60,getutcdate())\n and ls.collection_time_utc >= dateadd(minute,-20,getutcdate())\n\t\t)\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ls.sql_instance)\n';\n\nexec sp_executesql @_sqltext, @_params;", + "hide": 2, + "includeAll": false, + "label": "Log Space Issue Counts", + "name": "log_space_issue_counts", + "options": [], + "query": "declare @_sqltext nvarchar(max);\ndeclare @_params nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset @_sqltext = '\nselect\tcount(*)\nfrom dbo.log_space_consumers_all_servers ls\nwhere 1=1\nand ls.thresholds_validated = 1\nand (ls.updated_date_utc >= dateadd(minute,-60,getutcdate())\n and ls.collection_time_utc >= dateadd(minute,-20,getutcdate())\n\t\t)\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ls.sql_instance)\n';\n\nexec sp_executesql @_sqltext, @_params;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "declare @_sqltext nvarchar(max);\ndeclare @_params nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset @_sqltext = '\nselect\tcount(*)\nfrom dbo.tempdb_space_usage_all_servers su\nwhere (su.data_used_pct > 80.0\n\tor su.data_used_mb > (200.0*1024) -- 200 gb\n\t)\nand (su.updated_date_utc >= dateadd(minute,-60,getutcdate())\n and su.collection_time_utc >= dateadd(minute,-20,getutcdate())\n\t)\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = su.sql_instance)\n'\nexec sp_executesql @_sqltext, @_params;", + "hide": 2, + "includeAll": false, + "label": "Tempdb Space Issue Counts", + "name": "tempdb_space_issue_counts", + "options": [], + "query": "declare @_sqltext nvarchar(max);\ndeclare @_params nvarchar(max);\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset @_sqltext = '\nselect\tcount(*)\nfrom dbo.tempdb_space_usage_all_servers su\nwhere (su.data_used_pct > 80.0\n\tor su.data_used_mb > (200.0*1024) -- 200 gb\n\t)\nand (su.updated_date_utc >= dateadd(minute,-60,getutcdate())\n and su.collection_time_utc >= dateadd(minute,-20,getutcdate())\n\t)\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = su.sql_instance)\n'\nexec sp_executesql @_sqltext, @_params;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @os_cpu_threshold decimal(20,2) = 70;\ndeclare @sql_cpu_threshold decimal(20,2) = 65;\ndeclare @blocked_counts_threshold int = 1;\ndeclare @blocked_duration_max_seconds_threshold bigint = 60;\ndeclare @available_physical_memory_kb_threshold bigint = (4*1024*1024);\ndeclare @system_high_memory_signal_state_threshold varchar(20) = 'Low';\n--declare @physical_memory_in_use_kb_threshold decimal(20,2);\ndeclare @memory_grants_pending_threshold int = 1;\ndeclare @connection_count_threshold int = 1500;\ndeclare @waits_per_core_per_minute_threshold decimal(20,2) = 180;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n\nset @params = N'@os_cpu_threshold decimal(20,2), @sql_cpu_threshold decimal(20,2), @blocked_counts_threshold int, \n\t\t\t\t@blocked_duration_max_seconds_threshold bigint, @available_physical_memory_kb_threshold bigint, \n\t\t\t\t@system_high_memory_signal_state_threshold varchar(20), @memory_grants_pending_threshold int, \n\t\t\t\t@connection_count_threshold int, @waits_per_core_per_minute_threshold decimal(20,2)';\n\nset quoted_identifier off;\nset @sql = \"\n;with t_cte as (\n\tselect\tsrv_name, os_cpu, sql_cpu, blocked_counts, blocked_duration_max_seconds, available_physical_memory_kb, system_high_memory_signal_state, physical_memory_in_use_kb, memory_grants_pending, connection_count, waits_per_core_per_minute\n\tfrom dbo.vw_all_server_info asi\n where 1=1\n \"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = asi.srv_name)\n)\nselect count(*)\nfrom t_cte cte\nwhere 1=1\nand ( os_cpu >= @os_cpu_threshold\n or sql_cpu >= @sql_cpu_threshold \n or blocked_counts >= @blocked_counts_threshold\n or blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold\n or ( available_physical_memory_kb < @available_physical_memory_kb_threshold and system_high_memory_signal_state = @system_high_memory_signal_state_threshold )\n or memory_grants_pending > @memory_grants_pending_threshold\n or connection_count >= @connection_count_threshold\n or waits_per_core_per_minute > @waits_per_core_per_minute_threshold\n)\n\";\nset quoted_identifier off;\n\n--print @sql\nexec dbo.sp_executesql @sql, @params, @os_cpu_threshold, @sql_cpu_threshold, @blocked_counts_threshold, @blocked_duration_max_seconds_threshold, @available_physical_memory_kb_threshold, @system_high_memory_signal_state_threshold, @memory_grants_pending_threshold, @connection_count_threshold, @waits_per_core_per_minute_threshold;", + "hide": 2, + "includeAll": false, + "label": "Core Metrics Issue Count", + "name": "core_metrics_issue_counts", + "options": [], + "query": "declare @os_cpu_threshold decimal(20,2) = 70;\ndeclare @sql_cpu_threshold decimal(20,2) = 65;\ndeclare @blocked_counts_threshold int = 1;\ndeclare @blocked_duration_max_seconds_threshold bigint = 60;\ndeclare @available_physical_memory_kb_threshold bigint = (4*1024*1024);\ndeclare @system_high_memory_signal_state_threshold varchar(20) = 'Low';\n--declare @physical_memory_in_use_kb_threshold decimal(20,2);\ndeclare @memory_grants_pending_threshold int = 1;\ndeclare @connection_count_threshold int = 1500;\ndeclare @waits_per_core_per_minute_threshold decimal(20,2) = 180;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\n\nset @params = N'@os_cpu_threshold decimal(20,2), @sql_cpu_threshold decimal(20,2), @blocked_counts_threshold int, \n\t\t\t\t@blocked_duration_max_seconds_threshold bigint, @available_physical_memory_kb_threshold bigint, \n\t\t\t\t@system_high_memory_signal_state_threshold varchar(20), @memory_grants_pending_threshold int, \n\t\t\t\t@connection_count_threshold int, @waits_per_core_per_minute_threshold decimal(20,2)';\n\nset quoted_identifier off;\nset @sql = \"\n;with t_cte as (\n\tselect\tsrv_name, os_cpu, sql_cpu, blocked_counts, blocked_duration_max_seconds, available_physical_memory_kb, system_high_memory_signal_state, physical_memory_in_use_kb, memory_grants_pending, connection_count, waits_per_core_per_minute\n\tfrom dbo.vw_all_server_info asi\n where 1=1\n \"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = asi.srv_name)\n)\nselect count(*)\nfrom t_cte cte\nwhere 1=1\nand ( os_cpu >= @os_cpu_threshold\n or sql_cpu >= @sql_cpu_threshold \n or blocked_counts >= @blocked_counts_threshold\n or blocked_duration_max_seconds >= @blocked_duration_max_seconds_threshold\n or ( available_physical_memory_kb < @available_physical_memory_kb_threshold and system_high_memory_signal_state = @system_high_memory_signal_state_threshold )\n or memory_grants_pending > @memory_grants_pending_threshold\n or connection_count >= @connection_count_threshold\n or waits_per_core_per_minute > @waits_per_core_per_minute_threshold\n)\n\";\nset quoted_identifier off;\n\n--print @sql\nexec dbo.sp_executesql @sql, @params, @os_cpu_threshold, @sql_cpu_threshold, @blocked_counts_threshold, @blocked_duration_max_seconds_threshold, @available_physical_memory_kb_threshold, @system_high_memory_signal_state_threshold, @memory_grants_pending_threshold, @connection_count_threshold, @waits_per_core_per_minute_threshold;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "set nocount on;\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\ndeclare @_latency_minutes int;\ndeclare @_redo_queue_size_gb int;\ndeclare @_log_send_queue_size_gb int;\ndeclare @_filter_out_offline_sqlagent bit = 1;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset @_latency_minutes = 30;\nset @_redo_queue_size_gb = 10;\nset @_log_send_queue_size_gb = 10;\n\nset @_params = N'@_latency_minutes int, @_redo_queue_size_gb int, @_log_send_queue_size_gb int';\n\nset @_sql = '\nif object_id(''tempdb..#replica_servers'') is not null\n\tdrop table #replica_servers\nselect distinct ahs.replica_server_name\ninto #replica_servers\nfrom dbo.ag_health_state_all_servers ahs\nwhere 1=1;\n\nselect\tcount(*) /* sql_instance, [replica_database] = replica_server_name+'' || ''+database_name,\n\t\t--replica_server_name, database_name, \n\t\tag_name, is_primary_replica, \n\t\tag_listener, is_local, synchronization_state_desc, synchronization_health_desc, \n\t\tlatency_seconds, log_send_queue_size, redo_queue_size, last_redone_time, log_send_rate, \n\t\tredo_rate, estimated_redo_completion_time_min, last_commit_time, is_suspended, \n\t\tsuspend_reason_desc, is_distributed, replica_server = rs.srv_name, updated_date_utc, collection_time_utc */\nfrom dbo.ag_health_state_all_servers ahs\nleft join (\tselect replica_server = rs.replica_server_name, srv_name = max(asi.srv_name)\n\t\tfrom #replica_servers rs\n\t\tjoin dbo.vw_all_server_info asi\n\t\t\ton rs.replica_server_name in (asi.machine_name, asi.server_name)\n\t\tgroup by rs.replica_server_name\n\t) rs\n\ton rs.replica_server = ahs.replica_server_name\nwhere 1=1\nand (\tahs.synchronization_health_desc <> ''HEALTHY''\n\tor\tahs.synchronization_state_desc not in (''SYNCHRONIZED'',''SYNCHRONIZING'')\n\tor\t(ahs.latency_seconds is not null and ahs.latency_seconds >= @_latency_minutes*60)\n\tor\t(ahs.log_send_queue_size is not null and ahs.log_send_queue_size >= @_log_send_queue_size_gb*1024*1024)\n\tor\t(ahs.redo_queue_size is not null and ahs.redo_queue_size >= @_redo_queue_size_gb*1024*1024)\n\t)\n'+(case when @_filter_out_offline_sqlagent = 0 then '--' else '' end)+'and exists (select 1/0 from dbo.services_all_servers sas where sas.sql_instance = ahs.sql_instance and sas.service_type = ''Agent''\tand sas.status_desc = ''Running'')\n\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ahs.sql_instance)\n';\n\nexec sp_executesql @_sql, @_params, @_latency_minutes, @_redo_queue_size_gb, @_log_send_queue_size_gb;\n", + "hide": 2, + "includeAll": false, + "label": "Ag Issue Counts", + "name": "ag_issue_counts", + "options": [], + "query": "set nocount on;\n\ndeclare @_sql nvarchar(max);\ndeclare @_params nvarchar(max);\n\ndeclare @_latency_minutes int;\ndeclare @_redo_queue_size_gb int;\ndeclare @_log_send_queue_size_gb int;\ndeclare @_filter_out_offline_sqlagent bit = 1;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\nset @_latency_minutes = 30;\nset @_redo_queue_size_gb = 10;\nset @_log_send_queue_size_gb = 10;\n\nset @_params = N'@_latency_minutes int, @_redo_queue_size_gb int, @_log_send_queue_size_gb int';\n\nset @_sql = '\nif object_id(''tempdb..#replica_servers'') is not null\n\tdrop table #replica_servers\nselect distinct ahs.replica_server_name\ninto #replica_servers\nfrom dbo.ag_health_state_all_servers ahs\nwhere 1=1;\n\nselect\tcount(*) /* sql_instance, [replica_database] = replica_server_name+'' || ''+database_name,\n\t\t--replica_server_name, database_name, \n\t\tag_name, is_primary_replica, \n\t\tag_listener, is_local, synchronization_state_desc, synchronization_health_desc, \n\t\tlatency_seconds, log_send_queue_size, redo_queue_size, last_redone_time, log_send_rate, \n\t\tredo_rate, estimated_redo_completion_time_min, last_commit_time, is_suspended, \n\t\tsuspend_reason_desc, is_distributed, replica_server = rs.srv_name, updated_date_utc, collection_time_utc */\nfrom dbo.ag_health_state_all_servers ahs\nleft join (\tselect replica_server = rs.replica_server_name, srv_name = max(asi.srv_name)\n\t\tfrom #replica_servers rs\n\t\tjoin dbo.vw_all_server_info asi\n\t\t\ton rs.replica_server_name in (asi.machine_name, asi.server_name)\n\t\tgroup by rs.replica_server_name\n\t) rs\n\ton rs.replica_server = ahs.replica_server_name\nwhere 1=1\nand (\tahs.synchronization_health_desc <> ''HEALTHY''\n\tor\tahs.synchronization_state_desc not in (''SYNCHRONIZED'',''SYNCHRONIZING'')\n\tor\t(ahs.latency_seconds is not null and ahs.latency_seconds >= @_latency_minutes*60)\n\tor\t(ahs.log_send_queue_size is not null and ahs.log_send_queue_size >= @_log_send_queue_size_gb*1024*1024)\n\tor\t(ahs.redo_queue_size is not null and ahs.redo_queue_size >= @_redo_queue_size_gb*1024*1024)\n\t)\n'+(case when @_filter_out_offline_sqlagent = 0 then '--' else '' end)+'and exists (select 1/0 from dbo.services_all_servers sas where sas.sql_instance = ahs.sql_instance and sas.service_type = ''Agent''\tand sas.status_desc = ''Running'')\n\n'+(case when @_has_inventory = 0 then '--' else '' end)+'and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = ahs.sql_instance)\n';\n\nexec sp_executesql @_sql, @_params, @_latency_minutes, @_redo_queue_size_gb, @_log_send_queue_size_gb;\n", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(max);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - All Server Alert History */\nset nocount on;\nselect count(*)\nfrom dbo.alert_history_all_servers a\nwhere 1 = 1\nand a.collection_time_utc between @start_time_utc and @end_time_utc\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "description": "Alert History Row Count", + "hide": 2, + "includeAll": false, + "label": "Alert History Row Count", + "name": "alert_history_row_count", + "options": [], + "query": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(max);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - All Server Alert History */\nset nocount on;\nselect count(*)\nfrom dbo.alert_history_all_servers a\nwhere 1 = 1\nand a.collection_time_utc between @start_time_utc and @end_time_utc\n\"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "select all_server_stable_info__collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time))\nfrom dbo.all_server_stable_info", + "hide": 2, + "includeAll": false, + "label": "StableInfo Collection Time", + "name": "all_server_stable_info__collection_time_utc", + "options": [], + "query": "select all_server_stable_info__collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time))\nfrom dbo.all_server_stable_info", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "select all_server_volatile_info__collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time))\nfrom dbo.all_server_volatile_info", + "hide": 2, + "includeAll": false, + "label": "Volatile Info Collection Time", + "name": "all_server_volatile_info__collection_time_utc", + "options": [], + "query": "select all_server_volatile_info__collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time))\nfrom dbo.all_server_volatile_info", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "select all_server_collection_latency_info__collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time))\nfrom dbo.all_server_collection_latency_info cli", + "hide": 2, + "includeAll": false, + "label": "Collection Latency Time", + "name": "collection_latency_info__collection_time_utc", + "options": [], + "query": "select all_server_collection_latency_info__collection_time_utc = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), max(collection_time))\nfrom dbo.all_server_collection_latency_info cli", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "select services_all_servers__collection_time_utc = max(collection_time_utc)\nfrom dbo.services_all_servers sas", + "hide": 2, + "includeAll": false, + "label": "Services Collection Time", + "name": "services_all_servers__collection_time_utc", + "options": [], + "query": "select services_all_servers__collection_time_utc = max(collection_time_utc)\nfrom dbo.services_all_servers sas", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "select backups_all_servers__collection_time_utc = max(bas.collection_time_utc)\nfrom dbo.backups_all_servers bas", + "hide": 2, + "includeAll": false, + "label": "Backups Collection Time", + "name": "backups_all_servers__collection_time_utc", + "options": [], + "query": "select backups_all_servers__collection_time_utc = max(bas.collection_time_utc)\nfrom dbo.backups_all_servers bas", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "select sql_agent_jobs__collection_time_utc = max(sj.CollectionTimeUTC)\nfrom dbo.sql_agent_jobs_all_servers sj", + "hide": 2, + "includeAll": false, + "label": "SQLAgent Jobs collection Time", + "name": "sql_agent_jobs__collection_time_utc", + "options": [], + "query": "select sql_agent_jobs__collection_time_utc = max(sj.CollectionTimeUTC)\nfrom dbo.sql_agent_jobs_all_servers sj", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "select disk_space__collection_time_utc = max(ds.collection_time_utc)\nfrom dbo.disk_space_all_servers ds", + "hide": 2, + "includeAll": false, + "label": "Disk Space Collection Time", + "name": "disk_space__collection_time_utc", + "options": [], + "query": "select disk_space__collection_time_utc = max(ds.collection_time_utc)\nfrom dbo.disk_space_all_servers ds", + "refresh": 1, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "select ag_health_state__collection_time_utc = max(ahs.collection_time_utc)\nfrom dbo.ag_health_state_all_servers ahs", + "hide": 2, + "includeAll": false, + "label": "Ag Health State Collection Time", + "name": "ag_health_state__collection_time_utc", + "options": [], + "query": "select ag_health_state__collection_time_utc = max(ahs.collection_time_utc)\nfrom dbo.ag_health_state_all_servers ahs", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "select log_space_consumers__collection_time_utc = max(ls.collection_time_utc)\nfrom dbo.log_space_consumers_all_servers ls", + "hide": 2, + "includeAll": false, + "label": "Log Space Consumers Collection Time", + "name": "log_space_consumers__collection_time_utc", + "options": [], + "query": "select log_space_consumers__collection_time_utc = max(ls.collection_time_utc)\nfrom dbo.log_space_consumers_all_servers ls", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "select tempdb_space_usage__collection_time_utc = max(su.collection_time_utc)\nfrom dbo.tempdb_space_usage_all_servers su", + "hide": 2, + "includeAll": false, + "label": "Tempdb Space Usage Collection Time", + "name": "tempdb_space_usage__collection_time_utc", + "options": [], + "query": "select tempdb_space_usage__collection_time_utc = max(su.collection_time_utc)\nfrom dbo.tempdb_space_usage_all_servers su", + "refresh": 2, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "definition": "if object_id('dbo.sma_servers') is not null\n select [has_inventory] = 1\nelse\n select [has_inventory] = 0", + "hide": 2, + "includeAll": false, + "label": "Is Inventory Present", + "name": "has_inventory", + "options": [], + "query": "if object_id('dbo.sma_servers') is not null\n select [has_inventory] = 1\nelse\n select [has_inventory] = 0", + "refresh": 1, + "regex": "", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "definition": "-- alert_history_aggregated_row_count\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(max);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - All Server Alert History */\nset nocount on;\n;with cte_aggregated as (\n select a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), error_message = max(m.text)\n --a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \n --a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\n from dbo.alert_history_all_servers a\n left join sys.messages m \n on m.language_id = 1033 and m.message_id = a.error_number\n where 1 = 1\n and a.collection_time_utc between @start_time_utc and @end_time_utc\n and (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\n then 0\n when a.error_number = 1204 then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\n then 0\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\n then 0\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\n then 0\n else 1\n end\n ) = 1\n \"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\n group by a.sql_instance, a.error_number, a.error_severity\n)\nselect count(*) from cte_aggregated\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "description": "Alert History Aggregated Row Count", + "hide": 2, + "label": "Alert History Aggregated Row Count", + "name": "alert_history_aggregated_row_count", + "options": [], + "query": "-- alert_history_aggregated_row_count\ndeclare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(max);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @_has_inventory bit = 0;\nset @_has_inventory = convert(bit,'${has_inventory}');\n\n--set @sql_instance = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\n\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\n\nset @params = N'@start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor - All Server Alert History */\nset nocount on;\n;with cte_aggregated as (\n select a.sql_instance, a.error_number, a.error_severity, occurrences = count(*), error_message = max(m.text)\n --a.collection_time_utc, a.sql_instance, a.server_name, a.database_name, a.error_number, \n --a.error_severity, a.error_message, a.host_instance, a.updated_time_utc\n from dbo.alert_history_all_servers a\n left join sys.messages m \n on m.language_id = 1033 and m.message_id = a.error_number\n where 1 = 1\n and a.collection_time_utc between @start_time_utc and @end_time_utc\n and (case when a.error_number = 50000 and error_severity = 20 and a.error_message like '%application intent is set to read only%'\n then 0\n when a.error_number = 1204 then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Could not create constraint or index. See previous errors. '\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 The instance of the SQL Server Database Engine cannot obtain a LOCK resource at this time%'\n then 0\n when a.error_number = 17806 and error_severity = 20 and a.error_message like 'SSPI handshake failed with error code 0x8009030c%'\n then 0\n when a.error_number = 50000 and error_severity = 20 and a.error_message like 'Error: 50000 Severity: 20 State: 1 Lock request time out period exceeded. '\n then 0\n when a.error_number = 976 and error_severity = 14 and a.error_message like '%Either data movement is suspended or the availability replica is not enabled for read access%'\n then 0\n else 1\n end\n ) = 1\n \"+(case when @_has_inventory = 0 then '--' else '' end)+\"and exists (select * from dbo.sma_servers s where s.is_decommissioned = 0 and s.is_onboarded = 1 and s.server = a.sql_instance)\n group by a.sql_instance, a.error_number, a.error_severity\n)\nselect count(*) from cte_aggregated\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql, @params, @start_time_utc, @end_time_utc;", + "refresh": 2, + "regex": "", + "type": "query" + } + ] + }, + "time": { + "from": "now-4h", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "30s", + "1m", + "2m", + "5m" + ] + }, + "timezone": "browser", + "title": "Monitoring - Live - All Servers", + "uid": "distributed_live_dashboard_all_servers", + "version": 28, + "weekStart": "" } \ No newline at end of file diff --git a/Grafana-Dashboards/Monitoring - Live - Distributed.json b/Grafana-Dashboards/Monitoring - Live - Distributed.json index 8667af3..06c58b8 100644 --- a/Grafana-Dashboards/Monitoring - Live - Distributed.json +++ b/Grafana-Dashboards/Monitoring - Live - Distributed.json @@ -15,13 +15,6 @@ "value": "dbo.vw_performance_counters", "description": "" }, - { - "name": "VAR_OS_TASK_LIST_TABLE_NAME", - "type": "constant", - "label": "OS Tasks Table Name", - "value": "dbo.vw_os_task_list", - "description": "" - }, { "name": "VAR_WHOISACTIVE_TABLE_NAME", "type": "constant", @@ -469,22 +462,34 @@ "mode": "absolute", "steps": [ { - "color": "dark-blue", + "color": "green", "value": null + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 90 } ] }, - "unit": "s" + "unit": "percent" }, "overrides": [] }, "gridPos": { - "h": 2, - "w": 5, + "h": 3, + "w": 3, "x": 11, "y": 1 }, - "id": 83, + "id": 158, "options": { "colorMode": "value", "graphMode": "none", @@ -492,17 +497,11 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [ - "last" - ], + "calcs": [], "fields": "/.*/", - "values": true + "values": false }, "showPercentChange": false, - "text": { - "titleSize": 8, - "valueSize": 18 - }, "textMode": "auto", "wideLayout": true }, @@ -514,31 +513,72 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "editorMode": "code", "format": "table", - "rawQuery": true, - "rawSql": "select [processor_info] = '$processor_name'", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ + "rawSql": "SELECT $ring_buffer_os_cpu;", + "refId": "A" + } + ], + "title": "OS CPU @ ${ring_buffer_event_time:date:YYYY-MM-DD}", + "type": "stat" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "", + "fieldConfig": { + "defaults": { + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ { - "property": { - "type": "string" - }, - "type": "groupBy" + "color": "green", + "value": null } - ], - "limit": 50 - } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 3, + "w": 3, + "x": 14, + "y": 1 + }, + "id": 161, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [], + "fields": "/.*/", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "format": "table", + "rawSql": "SELECT $ring_buffer_idle_cpu;", + "refId": "A" } ], - "title": "Processor Info", + "title": "Idle CPU @ ${ring_buffer_event_time:date:HH.mm}", "type": "stat" }, { @@ -549,14 +589,29 @@ "description": "", "fieldConfig": { "defaults": { - "decimals": 2, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "dark-blue", + "color": "dark-red", "value": null + }, + { + "color": "yellow", + "value": 300 + }, + { + "color": "orange", + "value": 600 + }, + { + "color": "light-green", + "value": 601 + }, + { + "color": "dark-green", + "value": 1200 } ] }, @@ -565,12 +620,12 @@ "overrides": [] }, "gridPos": { - "h": 2, - "w": 3, - "x": 16, + "h": 3, + "w": 2, + "x": 17, "y": 1 }, - "id": 69, + "id": 15, "options": { "colorMode": "value", "graphMode": "none", @@ -578,17 +633,11 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [ - "last" - ], + "calcs": [], "fields": "/.*/", - "values": true + "values": false }, "showPercentChange": false, - "text": { - "titleSize": 8, - "valueSize": 18 - }, "textMode": "auto", "wideLayout": true }, @@ -600,12 +649,31 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, + "editorMode": "code", "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect virtual_machine_type_desc\nfrom sys.dm_os_sys_info as osi\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "refId": "A" + "rawQuery": true, + "rawSql": "select $ple;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "Machine Type", + "title": "PLE", "type": "stat" }, { @@ -966,12 +1034,31 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, + "editorMode": "code", "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect count(*) as counts\nfrom $os_task_list_table_name otl with (nolock)\nwhere collection_time_utc = (select top 1 i.collection_time_utc from $os_task_list_table_name i with (nolock)\n where i.host_name = @perfmon_host_name order by i.collection_time_utc desc)\nand otl.host_name = @perfmon_host_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "refId": "A" + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nselect proc_counts = count(*)\nfrom $perfmon_table_name pc\nwhere 1=1\n--and collection_time_utc between @start_time_utc and @end_time_utc\nand collection_time_utc = (\n select max(i.collection_time_utc) from $perfmon_table_name i \n where i.collection_time_utc >= dateadd(minute, -15, getutcdate())\n and i.host_name = @perfmon_host_name and object = 'process' and counter = '% processor time'\n )\nand pc.host_name = @perfmon_host_name\nand object = 'process' and counter = '% processor time'\nand pc.instance not in ('_total','idle')\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "OS Processes @ ${os_task_list_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "title": "OS Processes @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "stat" }, { @@ -1052,34 +1139,22 @@ "mode": "absolute", "steps": [ { - "color": "green", + "color": "dark-blue", "value": null - }, - { - "color": "yellow", - "value": 70 - }, - { - "color": "orange", - "value": 80 - }, - { - "color": "red", - "value": 90 } ] }, - "unit": "percent" + "unit": "s" }, "overrides": [] }, "gridPos": { - "h": 3, - "w": 3, + "h": 2, + "w": 5, "x": 11, - "y": 3 + "y": 4 }, - "id": 158, + "id": 83, "options": { "colorMode": "value", "graphMode": "none", @@ -1087,11 +1162,17 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [], + "calcs": [ + "last" + ], "fields": "/.*/", - "values": false + "values": true }, "showPercentChange": false, + "text": { + "titleSize": 8, + "valueSize": 18 + }, "textMode": "auto", "wideLayout": true }, @@ -1103,12 +1184,31 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, + "editorMode": "code", "format": "table", - "rawSql": "SELECT $ring_buffer_os_cpu;", - "refId": "A" + "rawQuery": true, + "rawSql": "select [processor_info] = '$processor_name'", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "OS CPU @ ${ring_buffer_event_time:date:YYYY-MM-DD}", + "title": "Processor Info", "type": "stat" }, { @@ -1119,27 +1219,28 @@ "description": "", "fieldConfig": { "defaults": { + "decimals": 2, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green", + "color": "dark-blue", "value": null } ] }, - "unit": "percent" + "unit": "s" }, "overrides": [] }, "gridPos": { - "h": 3, + "h": 2, "w": 3, - "x": 14, - "y": 3 + "x": 16, + "y": 4 }, - "id": 161, + "id": 69, "options": { "colorMode": "value", "graphMode": "none", @@ -1147,87 +1248,17 @@ "orientation": "auto", "percentChangeColorMode": "standard", "reduceOptions": { - "calcs": [], + "calcs": [ + "last" + ], "fields": "/.*/", - "values": false + "values": true }, "showPercentChange": false, - "textMode": "auto", - "wideLayout": true - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${DS_SQLMONITOR}" - }, - "format": "table", - "rawSql": "SELECT $ring_buffer_idle_cpu;", - "refId": "A" - } - ], - "title": "Idle CPU @ ${ring_buffer_event_time:date:HH.mm}", - "type": "stat" - }, - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "", - "fieldConfig": { - "defaults": { - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "dark-red", - "value": null - }, - { - "color": "yellow", - "value": 300 - }, - { - "color": "orange", - "value": 600 - }, - { - "color": "light-green", - "value": 601 - }, - { - "color": "dark-green", - "value": 1200 - } - ] - }, - "unit": "s" - }, - "overrides": [] - }, - "gridPos": { - "h": 3, - "w": 2, - "x": 17, - "y": 3 - }, - "id": 15, - "options": { - "colorMode": "value", - "graphMode": "none", - "justifyMode": "center", - "orientation": "auto", - "percentChangeColorMode": "standard", - "reduceOptions": { - "calcs": [], - "fields": "/.*/", - "values": false + "text": { + "titleSize": 8, + "valueSize": 18 }, - "showPercentChange": false, "textMode": "auto", "wideLayout": true }, @@ -1239,31 +1270,12 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "editorMode": "code", "format": "table", - "rawQuery": true, - "rawSql": "select $ple;", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect virtual_machine_type_desc\nfrom sys.dm_os_sys_info as osi\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", + "refId": "A" } ], - "title": "PLE", + "title": "Machine Type", "type": "stat" }, { @@ -1738,7 +1750,7 @@ { "matcher": { "id": "byName", - "options": "IsClustered" + "options": "IsHadrEnabled" }, "properties": [ { @@ -1758,7 +1770,7 @@ "x": 14, "y": 7 }, - "id": 74, + "id": 168, "options": { "colorMode": "value", "graphMode": "none", @@ -1786,11 +1798,11 @@ "uid": "${DS_SQLMONITOR}" }, "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsClustered')) as IsClustered --, CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "refId": "A" } ], - "title": "IsClustered", + "title": "IsHadrEnabled", "type": "stat" }, { @@ -1817,7 +1829,7 @@ { "matcher": { "id": "byName", - "options": "IsHadrEnabled" + "options": "IsClustered" }, "properties": [ { @@ -1837,7 +1849,7 @@ "x": 16, "y": 7 }, - "id": 168, + "id": 74, "options": { "colorMode": "value", "graphMode": "none", @@ -1865,11 +1877,11 @@ "uid": "${DS_SQLMONITOR}" }, "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--select CONVERT(varchar,SERVERPROPERTY('IsClustered'))+' / '+CONVERT(varchar,SERVERPROPERTY('IsHadrEnabled'))\nselect CONVERT(bit,SERVERPROPERTY('IsClustered')) as IsClustered --, CONVERT(bit,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "refId": "A" } ], - "title": "IsHadrEnabled", + "title": "IsClustered", "type": "stat" }, { @@ -3339,7 +3351,7 @@ "h": 9, "w": 24, "x": 0, - "y": 18 + "y": 632 }, "id": 181, "options": { @@ -3454,7 +3466,8 @@ "mode": "absolute", "steps": [ { - "color": "transparent" + "color": "transparent", + "value": null } ] }, @@ -3466,14 +3479,14 @@ "h": 8, "w": 24, "x": 0, - "y": 28 + "y": 62 }, "id": 16, "options": { "legend": { "calcs": [ "lastNotNull", - "p99" + "p98" ], "displayMode": "table", "placement": "right", @@ -3520,213 +3533,109 @@ ], "title": "CPU - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "Trend - CPU Utilization", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 19 - }, - "id": 113, - "panels": [ + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "Blocking Details____In Selected Time Duration____Since____${__from:date:YYYY-MM-DD HH.mm}___till___${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}____", + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "% CPU", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "filterable": true, - "inspect": false + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "line+area" + } }, "mappings": [], + "max": 100, + "min": 0, "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" + "color": "transparent", + "value": null } ] - } + }, + "unit": "percent" }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "BLOCKING_TREE" - }, - "properties": [ + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 240 + }, + "id": 193, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "p98" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "98th %", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\n;with cte_process_cpu as (\n\tselect pc.collection_time_utc, pc.host_name, pc.object, pc.counter,\n\t\t\tpc.value, \n\t\t\tpc.instance,\n\t\t\tprocess = case when CHARINDEX('#',s1.instance) > 0 then left(s1.instance, CHARINDEX('#',s1.instance)-1) else s1.instance end\n\tfrom $perfmon_table_name pc\n\touter apply (select instance = case when CHARINDEX('$',pc.instance) > 0 then left(pc.instance, CHARINDEX('$',pc.instance)-1) else pc.instance end ) s1\n\twhere collection_time_utc between @start_time_utc and @end_time_utc\n\tand pc.host_name = @perfmon_host_name\n\t--and collection_time_utc = (select max(i.collection_time_utc) from dbo.performance_counters i)\n\tand object = 'process' and counter = '% processor time'\n\tand pc.instance not in ('_total','idle')\n)\nselect\t[time] = collection_time_utc, --p.host_name, p.object, p.counter, \n\t\t[metric] = p.process,\n\t\t[value] = ceiling(sum(p.value)*1.0/si.cpu_count)\nfrom cte_process_cpu p\nouter apply (select si.cpu_count from sys.dm_os_sys_info si) si\ngroup by collection_time_utc, p.process, si.cpu_count\nhaving ceiling(sum(p.value)*1.0/si.cpu_count) > 0\norder by [time]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ { - "id": "custom.width", - "value": 308 - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "CPU" - }, - "properties": [ - { - "id": "unit", - "value": "ms" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "writes" - }, - "properties": [ - { - "id": "unit", - "value": "short" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "used_memory_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "used_memory" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "reads_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "reads" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "tempdb_allocations_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "tempdb_allocations" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "tempdb_current_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "tempdb_current" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "granted_memory_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "granted_memory" - } - ] - } - ] - }, - "gridPos": { - "h": 8, - "w": 24, - "x": 0, - "y": 251 - }, - "id": 111, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true, - "sortBy": [ - { - "desc": false, - "displayName": "collection_time" - } - ] - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${DS_SQLMONITOR}" - }, - "editorMode": "code", - "format": "table", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\n\ndeclare @collection_time datetime;\nselect @collection_time = max(l.collection_time) from dbo.WhoIsActive l;\n\n-- Get Blocking Tree\nif exists (select 1/0 from dbo.WhoIsActive w with (nolock) where w.collection_time = @collection_time and (blocking_session_id is not null or blocked_session_count > 0))\nbegin\n\t;WITH T_BLOCKERS AS (\n\t\t-- Find block Leaders\n\t\tSELECT\t[duration_s] = datediff(SECOND, start_time, r.collection_time), [collection_time], [session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE([sql_command],[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), [login_name], wait_info, [blocking_session_id], [blocked_session_count] = replace(blocked_session_count,',',''),\n\t\t\t\t[status], open_tran_count, [host_name], [database_name], [program_name], tasks, granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], additional_info,\n\t\t\t\t[LEVEL] = CAST (REPLICATE ('0', 4-LEN (CAST (r.session_id AS VARCHAR))) + CAST (r.session_id AS VARCHAR) AS VARCHAR (1000))\n\t\t\t\t,[head_blocker] = session_id ,query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock) WHERE r.collection_time = @collection_time and (r.blocking_session_id IS NULL AND blocked_session_count > 0)\n\t\t--\t\n\t\tUNION ALL\n\t\t--\n\t\tSELECT\t[duration_s] = datediff(SECOND, r.start_time, r.collection_time), r.[collection_time], r.[session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE(r.[sql_command],r.[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = r.additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), r.[login_name], r.wait_info, r.[blocking_session_id], [blocked_session_count] = replace(r.blocked_session_count,',',''),\n\t\t\t\tr.[status], r.open_tran_count, r.[host_name], r.[database_name], r.[program_name], r.tasks, r.granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], r.additional_info,\n\t\t\t\tCAST (B.LEVEL + RIGHT (CAST ((1000 + r.session_id) AS VARCHAR (100)), 4) AS VARCHAR (1000)) AS LEVEL\n\t\t\t\t,[head_blocker] = case when B.[head_blocker] is null then B.session_id else B.[head_blocker] end ,r.query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock)\n\t\tINNER JOIN T_BLOCKERS AS B ON\tr.collection_time = B.collection_time AND r.blocking_session_id = B.session_id\n\t\tWHERE\t r.blocking_session_id <> r.session_id\n\t)\n\tSELECT\t[collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), \n\t\t\t[ddd hh:mm:ss] = right('0000'+convert(varchar, duration_s/86400),3)+ ' '+convert(varchar,dateadd(SECOND,duration_s,'1900-01-01 00:00:00'),108), \n\t\t\t[session_id], \n\t\t\t[BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1) \n\t\t\t\t\t\t\t+\tCASE\tWHEN (LEN(LEVEL)/4 - 1) = 0\n\t\t\t\t\t\t\t\t\t\tTHEN 'HEAD - '\n\t\t\t\t\t\t\t\t\t\tELSE '|------ ' \n\t\t\t\t\t\t\t\tEND\n\t\t\t\t\t\t\t+\tCAST (r.session_id AS NVARCHAR (10)) + N' ' + ISNULL((CASE WHEN LEFT(ISNULL(r.[sql_text],''),1) = '(' THEN SUBSTRING(ISNULL(r.[sql_text],''),CHARINDEX('exec',ISNULL(r.[sql_text],'')),LEN(ISNULL(r.[sql_text],''))) ELSE ISNULL(r.[sql_text],'') END),''),\n\t\t\t[blocking_session_id], [blocked_session_count] = case when [blocked_session_count] = 0 then null else [blocked_session_count] end, r.wait_info, [login_name], [program_name], [host_name], tasks, command, [database_name], status, r.open_tran_count, r.CPU, [used_memory_kb] = (r.used_memory*8.0), [reads_kb] = (r.[reads]*8.0), r.[writes], [tempdb_allocations_kb] = tempdb_allocations*8.0, tempdb_current_kb = tempdb_current*8.0, granted_memory_kb = granted_memory * 8.0\n\t\t\t--,r.query_plan ,r.additional_info\n\tFROM\tT_BLOCKERS AS r ORDER BY collection_time, LEVEL;\nend\nelse\n select 'No Blocking Found for Time Window' as [No Result]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" + "parameters": [], + "type": "function" } ], "groupBy": [ @@ -3741,12 +3650,12 @@ } } ], - "title": "Blocking Details - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", - "type": "table" + "title": "OS Processes CPU Utilization - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "timeseries" } ], "repeat": "datasource", - "title": "Blocking Tree (Queries) - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "title": "Trend - CPU Utilization @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -3755,16 +3664,17 @@ "h": 1, "w": 24, "x": 0, - "y": 20 + "y": 19 }, - "id": 151, + "id": 195, "panels": [ { "datasource": { + "default": false, "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", + "description": "select physical_memory_in_use_kb from sys.dm_os_process_memory", "fieldConfig": { "defaults": { "color": { @@ -3779,7 +3689,7 @@ "barAlignment": 0, "barWidthFactor": 0.6, "drawStyle": "line", - "fillOpacity": 0, + "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "legend": false, @@ -3788,20 +3698,16 @@ }, "insertNulls": false, "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, "lineWidth": 1, "pointSize": 5, "scaleDistribution": { - "log": 10, - "type": "log" + "type": "linear" }, - "showPoints": "always", - "spanNulls": false, + "showPoints": "never", + "spanNulls": true, "stacking": { "group": "A", - "mode": "normal" + "mode": "none" }, "thresholdsStyle": { "mode": "off" @@ -3812,29 +3718,31 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, - "unit": "short" + "unit": "kbytes" }, "overrides": [] }, "gridPos": { - "h": 11, + "h": 10, "w": 24, "x": 0, - "y": 252 + "y": 305 }, - "id": 149, + "id": 194, "options": { "legend": { "calcs": [ - "mean", - "max" + "last", + "max", + "mean" ], "displayMode": "table", - "placement": "right", + "placement": "bottom", "showLegend": true, "sortBy": "Mean", "sortDesc": true @@ -3852,12 +3760,31 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, + "editorMode": "code", "format": "time_series", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], w.login_name as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@sql_instance varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect\t[time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), h.collection_time),\n\t\t\t\t[SqlServer] = h.physical_memory_in_use_kb,\n\t\t\t\t[Total_Ram] = asi.total_physical_memory_kb,\n\t\t\t\t[Available_Memory] = h.available_physical_memory_kb\nfrom dbo.all_server_volatile_info_history h\njoin dbo.all_server_stable_info asi\ton asi.srv_name = h.srv_name\nwhere h.collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc)\nand h.srv_name = @sql_instance\norder by time\n\"\nset quoted_identifier on;\n\nexec dbo.sp_executesql @sql , @params, @sql_instance, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "[${server}] - Lead Blockers - Logins - Blocked Count", + "title": "SQL Server Process Memory", "type": "timeseries" }, { @@ -3865,44 +3792,43 @@ "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { + "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", - "axisLabel": "", + "axisLabel": "% CPU", "axisPlacement": "auto", "barAlignment": 0, + "barWidthFactor": 0.6, "drawStyle": "line", - "fillOpacity": 0, + "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, + "insertNulls": false, "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, "lineWidth": 1, "pointSize": 5, "scaleDistribution": { - "log": 10, - "type": "log" + "type": "linear" }, - "showPoints": "always", - "spanNulls": false, + "showPoints": "never", + "spanNulls": true, "stacking": { "group": "A", - "mode": "normal" + "mode": "none" }, "thresholdsStyle": { - "mode": "off" + "mode": "line+area" } }, "mappings": [], @@ -3910,31 +3836,33 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "transparent", + "value": null } ] }, - "unit": "short" + "unit": "mbytes" }, "overrides": [] }, "gridPos": { - "h": 11, + "h": 10, "w": 24, "x": 0, - "y": 410 + "y": 315 }, - "id": 152, + "id": 196, "options": { "legend": { "calcs": [ - "mean", + "lastNotNull", + "p98", "max" ], "displayMode": "table", "placement": "right", "showLegend": true, - "sortBy": "Mean", + "sortBy": "98th %", "sortDesc": true }, "tooltip": { @@ -3942,7 +3870,7 @@ "sort": "desc" } }, - "pluginVersion": "8.4.4", + "pluginVersion": "11.3.1", "targets": [ { "alias": "", @@ -3950,130 +3878,138 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, + "editorMode": "code", "format": "time_series", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], \n w.login_name+' {{'+(case when len(w.program_name)>35 then left(w.program_name,35)+'..' else w.program_name end)+'}}' as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name, w.program_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" - } - ], - "title": "[${server}] - Lead Blockers - Programs - Blocked Count", - "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "Lead Blockers - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\n;with cte_process_cpu as (\n\tselect pc.collection_time_utc, pc.host_name, pc.object, pc.counter,\n\t\t\tpc.value, \n\t\t\tpc.instance,\n\t\t\tprocess = case when CHARINDEX('#',s1.instance) > 0 then left(s1.instance, CHARINDEX('#',s1.instance)-1) else s1.instance end\n\tfrom $perfmon_table_name pc\n\touter apply (select instance = case when CHARINDEX('$',pc.instance) > 0 then left(pc.instance, CHARINDEX('$',pc.instance)-1) else pc.instance end ) s1\n\twhere collection_time_utc between @start_time_utc and @end_time_utc\n\tand pc.host_name = @perfmon_host_name\n\tand object = 'process' and counter = 'working set'\n\tand pc.instance not in ('_total','idle')\n)\nselect\t[time] = collection_time_utc, --p.host_name, p.object, p.counter, \n\t\t[metric] = p.process,\n\t\t[value] = floor(sum(p.value)*1.0/1024.0/1024.0)\nfrom cte_process_cpu p\ngroup by collection_time_utc, p.process\nhaving floor(sum(p.value)*1.0/1024.0/1024.0) > 0\norder by [time]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "OS Processes Memory Utilization - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "timeseries" + } + ], + "title": "Trend - Memory Utilization @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, "w": 24, "x": 0, - "y": 21 + "y": 20 }, - "id": 59, + "id": 198, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "description": "select * from dbo.alert_history where error_number = 15457;", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 10, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "log": 2, - "type": "log" - }, - "showPoints": "never", - "spanNulls": true, - "stacking": { - "group": "A", - "mode": "none" + "align": "center", + "cellOptions": { + "type": "auto", + "wrapText": false }, - "thresholdsStyle": { - "mode": "line+area" - } + "filterable": true, + "inspect": true }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "transparent" - }, - { - "color": "orange", - "value": 0 - }, + "color": "green", + "value": null + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "time" + }, + "properties": [ { - "color": "red", - "value": 2 + "id": "custom.width", + "value": 227 } ] }, - "unit": "short" - }, - "overrides": [] + { + "matcher": { + "id": "byName", + "options": "error_number" + }, + "properties": [ + { + "id": "custom.width", + "value": 172 + } + ] + } + ] }, "gridPos": { - "h": 7, + "h": 8, "w": 24, "x": 0, - "y": 31 + "y": 44 }, - "id": 57, + "id": 197, "options": { - "legend": { - "calcs": [ - "mean", - "lastNotNull", - "max" + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "bottom", - "showLegend": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "desc" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ { - "alias": "", + "dataset": "AdventureWorks2019", "datasource": { "type": "mssql", "uid": "${DS_SQLMONITOR}" }, "editorMode": "code", - "format": "time_series", - "hide": false, + "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name\nand (\t(pc.object = (@object_name+':memory manager')\n\t\tand pc.counter in ('memory grants outstanding','memory grants pending'))\n\t)\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @perfmon_host_name varchar(255);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\n\r\nset @sql_instance = '$server';\r\nset @perfmon_host_name = '$perfmon_host_name';\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"\r\nselect\t[time] = collection_time_utc, ah.error_number,\r\n\t\t--[database] = case when ah.error_number = 15457 then null else database_name end,\r\n\t\t[remarks] = ah.[error_message]\r\nfrom dbo.alert_history ah\r\nwhere collection_time_utc between @start_time_utc and @end_time_utc\r\nand ah.error_number in (15457)\r\n--and ah.error_number in (5084)\r\norder by time\r\n\"\r\nset quoted_identifier on;\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -4094,52 +4030,36 @@ } } ], - "title": "Memory Grants Pending", - "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "Trend - Memory Grants Pending", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 22 - }, - "id": 179, - "panels": [ + "title": "Server Configuration Changes", + "type": "table" + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, + "description": "select * from dbo.alert_history where error_number = 5084;", "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "custom": { - "align": "auto", + "align": "center", "cellOptions": { - "type": "auto" + "type": "auto", + "wrapText": false }, "filterable": true, - "inspect": false + "inspect": true }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" - }, - { - "color": "red", - "value": 80 + "color": "green", + "value": null } ] } @@ -4148,92 +4068,218 @@ { "matcher": { "id": "byName", - "options": "sql_command" + "options": "time" }, "properties": [ { "id": "custom.width", - "value": 352 + "value": 173 } ] }, { "matcher": { "id": "byName", - "options": "program_name" + "options": "error_number" }, "properties": [ { "id": "custom.width", - "value": 169 + "value": 133 } ] }, { "matcher": { "id": "byName", - "options": "session_id" + "options": "database" }, "properties": [ { "id": "custom.width", - "value": 105 - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "CPU" - }, - "properties": [ - { - "id": "unit", - "value": "ms" + "value": 227 } ] }, { "matcher": { "id": "byName", - "options": "granted_memory_kb" + "options": "remarks" }, "properties": [ { - "id": "unit", - "value": "kbytes" - }, - { - "id": "displayName", - "value": "granted_memory" + "id": "custom.align", + "value": "left" } ] + } + ] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 52 + }, + "id": 199, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "enablePagination": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "dataset": "AdventureWorks2019", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" }, - { - "matcher": { - "id": "byName", - "options": "reads_kb" - }, - "properties": [ - { - "id": "unit", - "value": "kbytes" - }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\r\ndeclare @params nvarchar(max);\r\ndeclare @sql_instance varchar(255);\r\ndeclare @perfmon_host_name varchar(255);\r\ndeclare @start_time_utc datetime2;\r\ndeclare @end_time_utc datetime2;\r\n\r\nset @sql_instance = '$server';\r\nset @perfmon_host_name = '$perfmon_host_name';\r\nset @start_time_utc = $__timeFrom();\r\nset @end_time_utc = $__timeTo();\r\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\r\n\r\nset quoted_identifier off;\r\nset @sql = \"\r\nselect\t[time] = collection_time_utc, ah.error_number,\r\n\t\t[database] = database_name,\r\n\t\t[remarks] = ah.[error_message]\r\nfrom dbo.alert_history ah\r\nwhere collection_time_utc between @start_time_utc and @end_time_utc\r\n--and ah.error_number in (15457)\r\nand ah.error_number in (5084)\r\norder by time\r\n\"\r\nset quoted_identifier on;\r\n\r\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\r\nif ($is_local = 1)\r\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\r\nelse\r\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ { - "id": "displayName", - "value": "reads" + "parameters": [], + "type": "function" } - ] - }, - { - "matcher": { - "id": "byName", - "options": "writes" - }, - "properties": [ + ], + "groupBy": [ { - "id": "unit", - "value": "short" + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Database Configuration Changes", + "type": "table" + } + ], + "title": "Server & Database Config Changes", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 21 + }, + "id": 113, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "Blocking Details____In Selected Time Duration____Since____${__from:date:YYYY-MM-DD HH.mm}___till___${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}____", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "BLOCKING_TREE" + }, + "properties": [ + { + "id": "custom.width", + "value": 308 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "CPU" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "writes" + }, + "properties": [ + { + "id": "unit", + "value": "short" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "used_memory_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "used_memory" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "reads_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "reads" } ] }, @@ -4272,7 +4318,7 @@ { "matcher": { "id": "byName", - "options": "used_memory_kb" + "options": "granted_memory_kb" }, "properties": [ { @@ -4281,19 +4327,19 @@ }, { "id": "displayName", - "value": "used_memory" + "value": "granted_memory" } ] } ] }, "gridPos": { - "h": 11, + "h": 8, "w": 24, "x": 0, - "y": 439 + "y": 982 }, - "id": 177, + "id": 111, "options": { "cellHeight": "sm", "footer": { @@ -4305,9 +4351,14 @@ "show": false }, "showHeader": true, - "sortBy": [] + "sortBy": [ + { + "desc": false, + "displayName": "collection_time" + } + ] }, - "pluginVersion": "10.1.0", + "pluginVersion": "11.3.1", "targets": [ { "alias": "", @@ -4318,7 +4369,7 @@ "editorMode": "code", "format": "table", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\n\n--declare @delta_minutes int;\ndeclare @program_name nvarchar(500);\ndeclare @login_name nvarchar(255);\ndeclare @database nvarchar(500) = '$database';\ndeclare @session_id int;\ndeclare @session_host_name nvarchar(125);\ndeclare @query_pattern nvarchar(500);\ndeclare @duration int;\ndeclare @memory_grant_threshold_mb int = $memory_grant_threshold_mb;\n\nset @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\nset @duration = case when ltrim(rtrim('$duration')) <> '' then $duration else 0 end;\nif len(ltrim(rtrim('$program_name'))) > 0\n set @program_name = '$program_name'\nif len(ltrim(rtrim('$login_name'))) > 0\n set @login_name = '$login_name'\nif len(ltrim(rtrim('$session_host_name'))) > 0\n set @session_host_name = '$session_host_name'\nif len(ltrim(rtrim('$query_pattern'))) > 0\n set @query_pattern = '$query_pattern'\nif len(ltrim(rtrim('$session_id'))) > 0 and (case when '$session_id' like '%[^0-9.]%' then 'invalid' when '$session_id' like '%.%.%' then 'invalid' else 'valid' end) = 'valid'\n set @session_id = convert(int,'$session_id');\n\nset @sql_instance = '$server';\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\n--set @start_time_utc = dateadd(second,$sqlserver_start_time_utc/1000,'1970-01-01 00:00:00');\nset @end_time_utc = $__timeTo();\n--set @end_time_utc = $__timeFrom();\n--set @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2,\n\t\t\t\t@program_name nvarchar(500), @login_name nvarchar(255), @database nvarchar(500),\n\t\t\t\t@session_id int, @session_host_name nvarchar(125), @query_pattern nvarchar(500),\n\t\t\t\t@duration int, @memory_grant_threshold_mb int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor Dashboard $__dashboard: LongRunningQueries */\nset nocount on;\t\t\t\nif exists ( select * from $whoisactive_table_name w with (nolock)\n where w.collection_time = (select max(i.collection_time) from $whoisactive_table_name i with (nolock)) \n and granted_memory >= (@memory_grant_threshold_mb*1024/8) )\nbegin\n ;with t_WhoIsActive as (\n select [collection_time] = convert(varchar,w.collection_time,120), w.session_id, \n w.program_name, w.login_name, w.database_name, w.host_name,\n w.status, w.CPU, w.used_memory, w.open_tran_count, [duration_ms] = datediff(MILLISECOND, start_time, w.collection_time),\n w.wait_info, granted_memory, tempdb_current,\n sql_command = case when w.sql_command is not null then left(replace(replace(convert(nvarchar(max),w.sql_command),char(13)+char(10),''),'= (@memory_grant_threshold_mb*1024/8)\n )\n select [collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), w.session_id,\n [ddd hh:mm:ss.mss] = right('0000'+convert(varchar, duration_ms/86400000),3)+ ' '+convert(varchar,dateadd(MILLISECOND,duration_ms,'1900-01-01 00:00:00'),114), \n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, \n granted_memory_kb = (w.granted_memory * 8.0), w.open_tran_count, w.wait_info, w.sql_command, w.blocked_session_count, \n w.blocking_session_id, [reads_kb] = (w.[reads]*8.0), w.writes, [tempdb_allocations_kb] = (w.tempdb_allocations*8.0), \n tempdb_current_kb = (tempdb_current*8.0), w.tasks, w.percent_complete, [used_memory_kb] = (w.used_memory*8.0),\n start_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [start_time])\n from t_WhoIsActive w\n order by granted_memory desc, used_memory desc, collection_time\nend\nELSE\n select 'No long running query found for time window' as [No Result]\n\"\nset quoted_identifier on;\n--print @sql\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc, \n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc,\n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\n\ndeclare @collection_time datetime;\nselect @collection_time = max(l.collection_time) from dbo.WhoIsActive l;\n\n-- Get Blocking Tree\nif exists (select 1/0 from dbo.WhoIsActive w with (nolock) where w.collection_time = @collection_time and (blocking_session_id is not null or blocked_session_count > 0))\nbegin\n\t;WITH T_BLOCKERS AS (\n\t\t-- Find block Leaders\n\t\tSELECT\t[duration_s] = datediff(SECOND, start_time, r.collection_time), [collection_time], [session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE([sql_command],[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), [login_name], wait_info, [blocking_session_id], [blocked_session_count] = replace(blocked_session_count,',',''),\n\t\t\t\t[status], open_tran_count, [host_name], [database_name], [program_name], tasks, granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], additional_info,\n\t\t\t\t[LEVEL] = CAST (REPLICATE ('0', 4-LEN (CAST (r.session_id AS VARCHAR))) + CAST (r.session_id AS VARCHAR) AS VARCHAR (1000))\n\t\t\t\t,[head_blocker] = session_id ,query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock) WHERE r.collection_time = @collection_time and (r.blocking_session_id IS NULL AND blocked_session_count > 0)\n\t\t--\t\n\t\tUNION ALL\n\t\t--\n\t\tSELECT\t[duration_s] = datediff(SECOND, r.start_time, r.collection_time), r.[collection_time], r.[session_id], \n\t\t\t\t[sql_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE(r.[sql_command],r.[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'',''), \n\t\t\t\tcommand = r.additional_info.value('(/additional_info/command_type)[1]','varchar(125)'), r.[login_name], r.wait_info, r.[blocking_session_id], [blocked_session_count] = replace(r.blocked_session_count,',',''),\n\t\t\t\tr.[status], r.open_tran_count, r.[host_name], r.[database_name], r.[program_name], r.tasks, r.granted_memory, r.CPU, r.used_memory, r.[tempdb_allocations], r.[tempdb_current], r.[reads], r.[writes], r.[physical_io], r.additional_info,\n\t\t\t\tCAST (B.LEVEL + RIGHT (CAST ((1000 + r.session_id) AS VARCHAR (100)), 4) AS VARCHAR (1000)) AS LEVEL\n\t\t\t\t,[head_blocker] = case when B.[head_blocker] is null then B.session_id else B.[head_blocker] end ,r.query_plan\n\t\tFROM\tdbo.WhoIsActive AS r with (nolock)\n\t\tINNER JOIN T_BLOCKERS AS B ON\tr.collection_time = B.collection_time AND r.blocking_session_id = B.session_id\n\t\tWHERE\t r.blocking_session_id <> r.session_id\n\t)\n\tSELECT\t[collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), \n\t\t\t[ddd hh:mm:ss] = right('0000'+convert(varchar, duration_s/86400),3)+ ' '+convert(varchar,dateadd(SECOND,duration_s,'1900-01-01 00:00:00'),108), \n\t\t\t[session_id], \n\t\t\t[BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1) \n\t\t\t\t\t\t\t+\tCASE\tWHEN (LEN(LEVEL)/4 - 1) = 0\n\t\t\t\t\t\t\t\t\t\tTHEN 'HEAD - '\n\t\t\t\t\t\t\t\t\t\tELSE '|------ ' \n\t\t\t\t\t\t\t\tEND\n\t\t\t\t\t\t\t+\tCAST (r.session_id AS NVARCHAR (10)) + N' ' + ISNULL((CASE WHEN LEFT(ISNULL(r.[sql_text],''),1) = '(' THEN SUBSTRING(ISNULL(r.[sql_text],''),CHARINDEX('exec',ISNULL(r.[sql_text],'')),LEN(ISNULL(r.[sql_text],''))) ELSE ISNULL(r.[sql_text],'') END),''),\n\t\t\t[blocking_session_id], [blocked_session_count] = case when [blocked_session_count] = 0 then null else [blocked_session_count] end, r.wait_info, [login_name], [program_name], [host_name], tasks, command, [database_name], status, r.open_tran_count, r.CPU, [used_memory_kb] = (r.used_memory*8.0), [reads_kb] = (r.[reads]*8.0), r.[writes], [tempdb_allocations_kb] = tempdb_allocations*8.0, tempdb_current_kb = tempdb_current*8.0, granted_memory_kb = granted_memory * 8.0\n\t\t\t--,r.query_plan ,r.additional_info\n\tFROM\tT_BLOCKERS AS r ORDER BY collection_time, LEVEL;\nend\nelse\n select 'No Blocking Found for Time Window' as [No Result]\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -4339,11 +4390,12 @@ } } ], - "title": "Memory Consumers Over __ $memory_grant_threshold_mb MB__ \"- WhoIsActive Data @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "title": "Blocking Details - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "table" } ], - "title": "M/r Consumers - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "repeat": "datasource", + "title": "Blocking Tree (Queries) - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", "type": "row" }, { @@ -4352,28 +4404,57 @@ "h": 1, "w": 24, "x": 0, - "y": 23 + "y": 22 }, - "id": 188, + "id": 151, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select [collection_time], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], [log_used_pct], [version_store_mb], [version_store_pct]\nfrom dbo.tempdb_space_usage tsu\nwhere 1=1\nand tsu.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)", + "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "center", - "cellOptions": { - "type": "auto" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "filterable": false, - "inspect": true + "insertNulls": false, + "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "always", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "normal" + }, + "thresholdsStyle": { + "mode": "off" + } }, "mappings": [], "thresholds": { @@ -4381,47 +4462,619 @@ "steps": [ { "color": "green" - }, - { - "color": "red", - "value": 80 } ] - } + }, + "unit": "short" }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "collection_time_utc" - }, - "properties": [ - { - "id": "displayName", - "value": "Collection Time" - }, - { - "id": "custom.cellOptions", - "value": { - "type": "color-background" - } - }, - { - "id": "thresholds", - "value": { - "mode": "absolute", - "steps": [ - { - "color": "super-light-purple" - } - ] - } - } - ] + "overrides": [] + }, + "gridPos": { + "h": 11, + "w": 24, + "x": 0, + "y": 983 + }, + "id": 149, + "options": { + "legend": { + "calcs": [ + "mean", + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "Mean", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" }, - { - "matcher": { - "id": "byName", + "format": "time_series", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], w.login_name as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" + } + ], + "title": "[${server}] - Lead Blockers - Logins - Blocked Count", + "type": "timeseries" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select * from $whoisactive_table_name\nwhere (w.blocking_session_id is null and blocked_session_count > 0)", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "always", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "normal" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 11, + "w": 24, + "x": 0, + "y": 1141 + }, + "id": 152, + "options": { + "legend": { + "calcs": [ + "mean", + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "Mean", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "8.4.4", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "format": "time_series", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time) as [time], \n w.login_name+' {{'+(case when len(w.program_name)>35 then left(w.program_name,35)+'..' else w.program_name end)+'}}' as metric, sum(convert(int,replace(w.blocked_session_count,',',''))) as [value]\nfrom $whoisactive_table_name w with (nolock)\nwhere (collection_time between DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @start_time_utc) and DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), @end_time_utc))\nand (w.blocking_session_id is null and replace(w.blocked_session_count,',','') > 0) -- get lead blockers only\ngroup by collection_time, w.login_name, w.program_name\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" + } + ], + "title": "[${server}] - Lead Blockers - Programs - Blocked Count", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Lead Blockers - @ ${perfmon_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 23 + }, + "id": 59, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 2, + "type": "log" + }, + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "line+area" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "transparent", + "value": null + }, + { + "color": "orange", + "value": 0 + }, + { + "color": "red", + "value": 2 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 444 + }, + "id": 57, + "options": { + "legend": { + "calcs": [ + "mean", + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "time_series", + "hide": false, + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name\nand (\t(pc.object = (@object_name+':memory manager')\n\t\tand pc.counter in ('memory grants outstanding','memory grants pending'))\n\t)\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Memory Grants Pending", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Trend - Memory Grants Pending", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 24 + }, + "id": 179, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "sql_command" + }, + "properties": [ + { + "id": "custom.width", + "value": 352 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "program_name" + }, + "properties": [ + { + "id": "custom.width", + "value": 169 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "session_id" + }, + "properties": [ + { + "id": "custom.width", + "value": 105 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "CPU" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "granted_memory_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "granted_memory" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "reads_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "reads" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "writes" + }, + "properties": [ + { + "id": "unit", + "value": "short" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tempdb_allocations_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "tempdb_allocations" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tempdb_current_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "tempdb_current" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "used_memory_kb" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + }, + { + "id": "displayName", + "value": "used_memory" + } + ] + } + ] + }, + "gridPos": { + "h": 11, + "w": 24, + "x": 0, + "y": 67 + }, + "id": 177, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" + ], + "show": false + }, + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "table", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @crlf nchar(2) = nchar(13)+nchar(10);\n\n--declare @delta_minutes int;\ndeclare @program_name nvarchar(500);\ndeclare @login_name nvarchar(255);\ndeclare @database nvarchar(500) = '$database';\ndeclare @session_id int;\ndeclare @session_host_name nvarchar(125);\ndeclare @query_pattern nvarchar(500);\ndeclare @duration int;\ndeclare @memory_grant_threshold_mb int = $memory_grant_threshold_mb;\n\nset @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\nset @duration = case when ltrim(rtrim('$duration')) <> '' then $duration else 0 end;\nif len(ltrim(rtrim('$program_name'))) > 0\n set @program_name = '$program_name'\nif len(ltrim(rtrim('$login_name'))) > 0\n set @login_name = '$login_name'\nif len(ltrim(rtrim('$session_host_name'))) > 0\n set @session_host_name = '$session_host_name'\nif len(ltrim(rtrim('$query_pattern'))) > 0\n set @query_pattern = '$query_pattern'\nif len(ltrim(rtrim('$session_id'))) > 0 and (case when '$session_id' like '%[^0-9.]%' then 'invalid' when '$session_id' like '%.%.%' then 'invalid' else 'valid' end) = 'valid'\n set @session_id = convert(int,'$session_id');\n\nset @sql_instance = '$server';\n--set @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\n--set @start_time_utc = dateadd(second,$sqlserver_start_time_utc/1000,'1970-01-01 00:00:00');\nset @end_time_utc = $__timeTo();\n--set @end_time_utc = $__timeFrom();\n--set @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2,\n\t\t\t\t@program_name nvarchar(500), @login_name nvarchar(255), @database nvarchar(500),\n\t\t\t\t@session_id int, @session_host_name nvarchar(125), @query_pattern nvarchar(500),\n\t\t\t\t@duration int, @memory_grant_threshold_mb int';\n\nset quoted_identifier off;\nset @sql = \"/* SQLMonitor Dashboard $__dashboard: LongRunningQueries */\nset nocount on;\t\t\t\nif exists ( select * from $whoisactive_table_name w with (nolock)\n where w.collection_time = (select max(i.collection_time) from $whoisactive_table_name i with (nolock)) \n and granted_memory >= (@memory_grant_threshold_mb*1024/8) )\nbegin\n ;with t_WhoIsActive as (\n select [collection_time] = convert(varchar,w.collection_time,120), w.session_id, \n w.program_name, w.login_name, w.database_name, w.host_name,\n w.status, w.CPU, w.used_memory, w.open_tran_count, [duration_ms] = datediff(MILLISECOND, start_time, w.collection_time),\n w.wait_info, granted_memory, tempdb_current,\n sql_command = case when w.sql_command is not null then left(replace(replace(convert(nvarchar(max),w.sql_command),char(13)+char(10),''),'= (@memory_grant_threshold_mb*1024/8)\n )\n select [collection_time] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [collection_time]), w.session_id,\n [ddd hh:mm:ss.mss] = right('0000'+convert(varchar, duration_ms/86400000),3)+ ' '+convert(varchar,dateadd(MILLISECOND,duration_ms,'1900-01-01 00:00:00'),114), \n w.program_name, w.login_name, w.database_name, w.host_name, w.status, w.CPU, \n granted_memory_kb = (w.granted_memory * 8.0), w.open_tran_count, w.wait_info, w.sql_command, w.blocked_session_count, \n w.blocking_session_id, [reads_kb] = (w.[reads]*8.0), w.writes, [tempdb_allocations_kb] = (w.tempdb_allocations*8.0), \n tempdb_current_kb = (tempdb_current*8.0), w.tasks, w.percent_complete, [used_memory_kb] = (w.used_memory*8.0),\n start_time = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), [start_time])\n from t_WhoIsActive w\n order by granted_memory desc, used_memory desc, collection_time\nend\nELSE\n select 'No long running query found for time window' as [No Result]\n\"\nset quoted_identifier on;\n--print @sql\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc, \n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql, @params, @perfmon_host_name, @start_time_utc, @end_time_utc,\n\t\t\t\t\t@program_name, @login_name, @database, @session_id, @session_host_name, \n\t\t\t\t\t@query_pattern, @duration, @memory_grant_threshold_mb;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Memory Consumers Over __ $memory_grant_threshold_mb MB__ \"- WhoIsActive Data @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "table" + } + ], + "title": "M/r Consumers - ACTIVE - [sp_WhoIsActive] @ ${whoisactive_collection_time_utc:date:YYYY-MM-DD HH.mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 25 + }, + "id": 188, + "panels": [ + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "select [collection_time], [data_size_mb], [data_used_mb], [data_used_pct], [log_size_mb], [log_used_mb], [log_used_pct], [version_store_mb], [version_store_pct]\nfrom dbo.tempdb_space_usage tsu\nwhere 1=1\nand tsu.collection_time = (select top 1 i.collection_time from dbo.tempdb_space_usage i order by i.collection_time desc)", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "center", + "cellOptions": { + "type": "auto" + }, + "filterable": false, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "displayName", + "value": "Collection Time" + }, + { + "id": "custom.cellOptions", + "value": { + "type": "color-background" + } + }, + { + "id": "thresholds", + "value": { + "mode": "absolute", + "steps": [ + { + "color": "super-light-purple", + "value": null + } + ] + } + } + ] + }, + { + "matcher": { + "id": "byName", "options": "data_size_mb" }, "properties": [ @@ -4497,7 +5150,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -4608,7 +5262,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -4679,7 +5334,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -4714,7 +5370,7 @@ "h": 4, "w": 24, "x": 0, - "y": 255 + "y": 68 }, "id": 189, "options": { @@ -4789,7 +5445,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -4821,7 +5478,8 @@ "mode": "absolute", "steps": [ { - "color": "super-light-purple" + "color": "super-light-purple", + "value": null } ] } @@ -4906,7 +5564,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -5017,7 +5676,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -5088,7 +5748,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -5123,7 +5784,7 @@ "h": 4, "w": 24, "x": 0, - "y": 391 + "y": 72 }, "id": 190, "options": { @@ -5184,7 +5845,7 @@ "h": 1, "w": 24, "x": 0, - "y": 24 + "y": 26 }, "id": 191, "panels": [ @@ -5212,7 +5873,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -5244,7 +5906,8 @@ "mode": "absolute", "steps": [ { - "color": "super-light-purple" + "color": "super-light-purple", + "value": null } ] } @@ -5329,7 +5992,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "#EAB839", @@ -5384,7 +6048,8 @@ "mode": "absolute", "steps": [ { - "color": "blue" + "color": "blue", + "value": null } ] } @@ -5593,7 +6258,7 @@ "h": 11, "w": 24, "x": 0, - "y": 441 + "y": 77 }, "id": 192, "options": { @@ -5609,7 +6274,7 @@ }, "showHeader": true }, - "pluginVersion": "10.1.0", + "pluginVersion": "11.3.1", "targets": [ { "dataset": "AdventureWorks2019", @@ -5654,7 +6319,7 @@ "h": 1, "w": 24, "x": 0, - "y": 25 + "y": 27 }, "id": 140, "panels": [ @@ -5763,7 +6428,7 @@ "h": 8, "w": 24, "x": 0, - "y": 257 + "y": 988 }, "id": 138, "options": { @@ -5829,7 +6494,7 @@ "h": 1, "w": 24, "x": 0, - "y": 26 + "y": 28 }, "id": 117, "panels": [ @@ -6012,7 +6677,7 @@ "h": 7, "w": 24, "x": 0, - "y": 258 + "y": 989 }, "id": 115, "options": { @@ -6074,7 +6739,7 @@ "h": 1, "w": 24, "x": 0, - "y": 27 + "y": 29 }, "id": 29, "panels": [ @@ -6119,142 +6784,7 @@ "mode": "none" }, "thresholdsStyle": { - "mode": "line+area" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "red" - }, - { - "color": "transparent", - "value": 600 - } - ] - }, - "unit": "s" - }, - "overrides": [] - }, - "gridPos": { - "h": 10, - "w": 24, - "x": 0, - "y": 259 - }, - "id": 8, - "options": { - "legend": { - "calcs": [ - "lastNotNull", - "min" - ], - "displayMode": "table", - "placement": "bottom", - "showLegend": true - }, - "tooltip": { - "mode": "multi", - "sort": "desc" - } - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${DS_SQLMONITOR}" - }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' \n\t\t\t\t\t\t then 'sqlserver' else LOWER('$perfmon_service_name') end);\n\nselect pc.collection_time_utc as time\n\t,counter as metric\n\t--,object, counter, instance\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':buffer manager')\nand pc.counter = 'page life expectancy'\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } - } - ], - "title": "Page Life Expectancy", - "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "Trend - Page Life Expectancy", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 28 - }, - "id": 27, - "panels": [ - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisGridShow": true, - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 10, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 5, - "scaleDistribution": { - "log": 10, - "type": "log" - }, - "showPoints": "never", - "spanNulls": true, - "stacking": { - "group": "A", - "mode": "none" - }, - "thresholdsStyle": { - "mode": "off" + "mode": "line+area" } }, "mappings": [], @@ -6262,31 +6792,30 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "red" }, { - "color": "red", - "value": 80 + "color": "transparent", + "value": 600 } ] }, - "unit": "short" + "unit": "s" }, "overrides": [] }, "gridPos": { - "h": 7, + "h": 10, "w": 24, "x": 0, - "y": 260 + "y": 990 }, - "id": 18, + "id": 8, "options": { "legend": { "calcs": [ "lastNotNull", - "max", - "p99" + "min" ], "displayMode": "table", "placement": "bottom", @@ -6308,7 +6837,7 @@ "editorMode": "code", "format": "time_series", "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':sql statistics') \nand pc.counter = 'batch requests/sec'\nand pc.value <> 0.0\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' \n\t\t\t\t\t\t then 'sqlserver' else LOWER('$perfmon_service_name') end);\n\nselect pc.collection_time_utc as time\n\t,counter as metric\n\t--,object, counter, instance\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':buffer manager')\nand pc.counter = 'page life expectancy'\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A", "sql": { "columns": [ @@ -6329,12 +6858,12 @@ } } ], - "title": "Batch Requests Per Second", + "title": "Page Life Expectancy", "type": "timeseries" } ], "repeat": "datasource", - "title": "Trend - Batch Request/Sec", + "title": "Trend - Page Life Expectancy", "type": "row" }, { @@ -6343,255 +6872,95 @@ "h": 1, "w": 24, "x": 0, - "y": 29 + "y": 30 }, - "id": 126, + "id": 27, "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from sys.dm_exec_requests r where blocking_session_id <> 0", - "fieldConfig": { - "defaults": { - "color": { - "mode": "thresholds" - }, - "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" - }, - "inspect": false - }, - "mappings": [], - "max": 100, - "min": 0, - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green" - }, - { - "color": "dark-orange", - "value": 500 - }, - { - "color": "dark-red", - "value": 700 - } - ] - }, - "unit": "none" - }, - "overrides": [] - }, - "gridPos": { - "h": 7, - "w": 24, - "x": 0, - "y": 261 - }, - "id": 124, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${DS_SQLMONITOR}" - }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, login_name, status, client_net_address, client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des on des.session_id = dec.session_id\ngroup by host_name, login_name, status, client_net_address, client_interface_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" - } - ], - "title": "Connections", - "type": "table" - }, - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "select * from sys.dm_exec_connections", + "description": "https://sqlperformance.com/2014/10/sql-performance/knee-jerk-page-life-expectancy", "fieldConfig": { "defaults": { "color": { - "mode": "thresholds" + "mode": "palette-classic" }, "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" - }, - "filterable": true, - "inspect": false - }, - "mappings": [], - "min": 0, - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green" - } - ] - }, - "unit": "none" - }, - "overrides": [ - { - "matcher": { - "id": "byType", - "options": "string" + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisGridShow": true, + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false }, - "properties": [ - { - "id": "custom.width" - } - ] - }, - { - "matcher": { - "id": "byType", - "options": "number" + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "log": 10, + "type": "log" }, - "properties": [ - { - "id": "custom.width", - "value": 100 - } - ] - } - ] - }, - "gridPos": { - "h": 6, - "w": 8, - "x": 0, - "y": 309 - }, - "id": 131, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true, - "sortBy": [] - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${DS_SQLMONITOR}" - }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\nwhere len(ltrim(rtrim(client_interface_name))) > 0\ngroup by client_interface_name\norder by counts desc, client_interface_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" - } - ], - "title": "Connections by Interface", - "type": "table" - }, - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "select * from sys.dm_exec_connections", - "fieldConfig": { - "defaults": { - "color": { - "mode": "thresholds" - }, - "custom": { - "align": "auto", - "cellOptions": { - "type": "auto" + "showPoints": "never", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" }, - "filterable": true, - "inspect": false + "thresholdsStyle": { + "mode": "off" + } }, "mappings": [], - "min": 0, "thresholds": { "mode": "absolute", "steps": [ { - "color": "green" - } - ] - }, - "unit": "none" - }, - "overrides": [ - { - "matcher": { - "id": "byType", - "options": "string" - }, - "properties": [ - { - "id": "custom.width" - } - ] - }, - { - "matcher": { - "id": "byType", - "options": "number" - }, - "properties": [ + "color": "green" + }, { - "id": "custom.width", - "value": 100 + "color": "red", + "value": 80 } ] - } - ] + }, + "unit": "short" + }, + "overrides": [] }, "gridPos": { - "h": 12, - "w": 8, - "x": 8, - "y": 309 + "h": 7, + "w": 24, + "x": 0, + "y": 991 }, - "id": 128, + "id": 18, "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" + "legend": { + "calcs": [ + "lastNotNull", + "max", + "p99" ], - "show": false + "displayMode": "table", + "placement": "bottom", + "showLegend": true }, - "showHeader": true, - "sortBy": [] + "tooltip": { + "mode": "multi", + "sort": "desc" + } }, "pluginVersion": "11.3.1", "targets": [ @@ -6601,20 +6970,54 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by host_name\norder by counts desc, host_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @object_name varchar(255);\nset @object_name = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'sqlserver' else LOWER('$perfmon_service_name') end);\nselect pc.collection_time_utc as time, \n\tcounter as metric\n\t--,instance,\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc \nand host_name = @perfmon_host_name and pc.object = (@object_name+':sql statistics') \nand pc.counter = 'batch requests/sec'\nand pc.value <> 0.0\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } } ], - "title": "Host Connections", - "type": "table" - }, + "title": "Batch Requests Per Second", + "type": "timeseries" + } + ], + "repeat": "datasource", + "title": "Trend - Batch Request/Sec", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 31 + }, + "id": 126, + "panels": [ { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from sys.dm_exec_connections", + "description": "select * from sys.dm_exec_requests r where blocking_session_id <> 0", "fieldConfig": { "defaults": { "color": { @@ -6625,54 +7028,38 @@ "cellOptions": { "type": "auto" }, - "filterable": true, "inspect": false }, "mappings": [], + "max": 100, "min": 0, "thresholds": { "mode": "absolute", "steps": [ { "color": "green" + }, + { + "color": "dark-orange", + "value": 500 + }, + { + "color": "dark-red", + "value": 700 } ] }, "unit": "none" }, - "overrides": [ - { - "matcher": { - "id": "byName", - "options": "login_name" - }, - "properties": [ - { - "id": "custom.width" - } - ] - }, - { - "matcher": { - "id": "byName", - "options": "counts" - }, - "properties": [ - { - "id": "custom.width", - "value": 100 - } - ] - } - ] + "overrides": [] }, "gridPos": { - "h": 12, - "w": 8, - "x": 16, - "y": 309 + "h": 7, + "w": 24, + "x": 0, + "y": 992 }, - "id": 129, + "id": 124, "options": { "cellHeight": "sm", "footer": { @@ -6683,8 +7070,7 @@ ], "show": false }, - "showHeader": true, - "sortBy": [] + "showHeader": true }, "pluginVersion": "11.3.1", "targets": [ @@ -6695,11 +7081,11 @@ "uid": "${DS_SQLMONITOR}" }, "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect login_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by login_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, login_name, status, client_net_address, client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des on des.session_id = dec.session_id\ngroup by host_name, login_name, status, client_net_address, client_interface_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", "refId": "A" } ], - "title": "Login Connections", + "title": "Connections", "type": "table" }, { @@ -6736,162 +7122,48 @@ "overrides": [ { "matcher": { - "id": "byName", - "options": "counts" + "id": "byType", + "options": "string" }, "properties": [ { - "id": "custom.width", - "value": 100 - } - ] - } - ] - }, - "gridPos": { - "h": 6, - "w": 8, - "x": 0, - "y": 315 - }, - "id": 130, - "options": { - "cellHeight": "sm", - "footer": { - "countRows": false, - "fields": "", - "reducer": [ - "sum" - ], - "show": false - }, - "showHeader": true, - "sortBy": [] - }, - "pluginVersion": "11.3.1", - "targets": [ - { - "alias": "", - "datasource": { - "type": "mssql", - "uid": "${DS_SQLMONITOR}" - }, - "format": "table", - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect status, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by status\norder by counts desc, status\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", - "refId": "A" - } - ], - "title": "Connections By Status", - "type": "table" - } - ], - "repeat": "datasource", - "title": "SQL Connections - Distribution", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 30 - }, - "id": 79, - "panels": [ - { - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "description": "select * from [$inventory_db].$os_task_list_table_name", - "fieldConfig": { - "defaults": { - "color": { - "mode": "palette-classic" - }, - "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "bars", - "fillOpacity": 100, - "gradientMode": "hue", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 1, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" - }, - "thresholdsStyle": { - "mode": "off" - } - }, - "mappings": [], - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "green" + "id": "custom.width" } ] }, - "unit": "s" - }, - "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byType", + "options": "number" }, "properties": [ { - "id": "unit", - "value": "s" + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 11, - "w": 24, + "h": 6, + "w": 8, "x": 0, - "y": 40 + "y": 1040 }, - "id": 144, + "id": 131, "options": { - "legend": { - "calcs": [ - "last", - "mean" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": false + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -6901,81 +7173,35 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 100;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc\n\toption(recompile);\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select *\n--from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [cpu_s] = sum(cpu_time_seconds), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, cpu_s, counts\n\t\t,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[CpuRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY cpu_s DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, (case when [cpu_s__delta] < 0 then 0 else [cpu_s__delta] end) as value\nfrom #os_task_list\nwhere CpuRank <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by collection_time_utc, CpuRank, cpu_s desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect client_interface_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\nwhere len(ltrim(rtrim(client_interface_name))) > 0\ngroup by client_interface_name\norder by counts desc, client_interface_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Process - CPU Time - Histrogram - $cpu_delta_minutes Minute Interval", - "type": "timeseries" + "title": "Connections by Interface", + "type": "table" }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, - "lineWidth": 1, - "pointSize": 3, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -6984,46 +7210,53 @@ } ] }, - "unit": "s" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byType", + "options": "string" }, "properties": [ { - "id": "unit", - "value": "s" + "id": "custom.width" + } + ] + }, + { + "matcher": { + "id": "byType", + "options": "number" + }, + "properties": [ + { + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 10, - "w": 24, - "x": 0, - "y": 118 + "h": 12, + "w": 8, + "x": 8, + "y": 1040 }, - "id": 145, + "id": 128, "options": { - "legend": { - "calcs": [ - "last", - "mean" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -7033,93 +7266,35 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 100;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc;\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select *\n--from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [cpu_s] = sum(cpu_time_seconds), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, cpu_s, counts\n\t\t,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[CpuRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY cpu_s DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, (case when [cpu_s__delta] < 0 then 0 else [cpu_s__delta] end) as value\nfrom #os_task_list\nwhere CpuRank <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by collection_time_utc, CpuRank, cpu_s desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect host_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by host_name\norder by counts desc, host_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Process - CPU Time - Utilization - $cpu_delta_minutes Minute Interval", - "type": "timeseries" - } - ], - "repeat": "datasource", - "title": "OS Tasks (Processes) - CPU Utilization", - "type": "row" - }, - { - "collapsed": true, - "gridPos": { - "h": 1, - "w": 24, - "x": 0, - "y": 31 - }, - "id": 82, - "panels": [ + "title": "Host Connections", + "type": "table" + }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "bars", - "fillOpacity": 100, - "gradientMode": "hue", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineWidth": 1, - "pointSize": 1, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -7128,46 +7303,53 @@ } ] }, - "unit": "kbytes" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byName", + "options": "login_name" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "custom.width" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "counts" + }, + "properties": [ + { + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 10, - "w": 24, - "x": 0, - "y": 41 - }, - "id": 146, - "options": { - "legend": { - "calcs": [ - "mean", - "max" + "h": 12, + "w": 8, + "x": 16, + "y": 1040 + }, + "id": 129, + "options": { + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -7177,81 +7359,35 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 10;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc\n\toption(recompile);\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select * from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [memory_kb] = sum([memory_kb]), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, [memory_kb], counts\n\t\t--,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[RowRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY [memory_kb] DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, [memory_kb] as value\nfrom #os_task_list\nwhere [RowRank] <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by [time], [RowRank], [memory_kb] desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect login_name, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by login_name\norder by counts desc, login_name\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Processes - Memory - Histrogram - $cpu_delta_minutes Minute Interval", - "type": "timeseries" + "title": "Login Connections", + "type": "table" }, { "datasource": { "type": "mssql", "uid": "${sqlmonitor_datasource}" }, - "description": "select * from [$inventory_db].$os_task_list_table_name", + "description": "select * from sys.dm_exec_connections", "fieldConfig": { "defaults": { "color": { - "mode": "palette-classic" + "mode": "thresholds" }, "custom": { - "axisBorderShow": false, - "axisCenteredZero": false, - "axisColorMode": "text", - "axisLabel": "", - "axisPlacement": "auto", - "barAlignment": 0, - "barWidthFactor": 0.6, - "drawStyle": "line", - "fillOpacity": 0, - "gradientMode": "none", - "hideFrom": { - "legend": false, - "tooltip": false, - "viz": false - }, - "insertNulls": false, - "lineInterpolation": "linear", - "lineStyle": { - "fill": "solid" - }, - "lineWidth": 1, - "pointSize": 3, - "scaleDistribution": { - "type": "linear" - }, - "showPoints": "auto", - "spanNulls": false, - "stacking": { - "group": "A", - "mode": "normal" + "align": "auto", + "cellOptions": { + "type": "auto" }, - "thresholdsStyle": { - "mode": "off" - } + "filterable": true, + "inspect": false }, "mappings": [], + "min": 0, "thresholds": { "mode": "absolute", "steps": [ @@ -7260,46 +7396,42 @@ } ] }, - "unit": "kbytes" + "unit": "none" }, "overrides": [ { "matcher": { - "id": "byFrameRefID", - "options": "A" + "id": "byName", + "options": "counts" }, "properties": [ { - "id": "unit", - "value": "kbytes" + "id": "custom.width", + "value": 100 } ] } ] }, "gridPos": { - "h": 10, - "w": 24, + "h": 6, + "w": 8, "x": 0, - "y": 98 + "y": 1046 }, - "id": 147, + "id": 130, "options": { - "legend": { - "calcs": [ - "mean", - "max" + "cellHeight": "sm", + "footer": { + "countRows": false, + "fields": "", + "reducer": [ + "sum" ], - "displayMode": "table", - "placement": "right", - "showLegend": true, - "sortBy": "Mean", - "sortDesc": true + "show": false }, - "tooltip": { - "mode": "multi", - "sort": "none" - } + "showHeader": true, + "sortBy": [] }, "pluginVersion": "11.3.1", "targets": [ @@ -7309,36 +7441,17 @@ "type": "mssql", "uid": "${DS_SQLMONITOR}" }, - "editorMode": "code", - "format": "time_series", - "rawQuery": true, - "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @delta_minutes int;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @delta_minutes = $cpu_delta_minutes;\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @delta_minutes int';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\ndeclare @TopRowFilter tinyint = 10;\n\nif object_id('tempdb..#dates') is not null\n\tdrop table #dates;\ncreate table #dates (collection_time_utc datetime2 not null primary key);\ndeclare @_collection_time_utc datetime2;\ndeclare @_collection_time_utc_prev datetime2;\n\ndeclare cur_dates cursor local fast_forward for\n\tselect collection_time_utc\n\tfrom $os_task_list_table_name\n\twhere host_name = @perfmon_host_name\n\tand collection_time_utc between @start_time_utc and @end_time_utc\n\tgroup by collection_time_utc\n\torder by collection_time_utc\n\toption(recompile);\n\nopen cur_dates;\nfetch next from cur_dates into @_collection_time_utc;\n\nwhile @@fetch_status = 0\nbegin\n\t-- Set start timestamp\n\tif(@_collection_time_utc_prev is null)\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\t-- Choose next timestamp based on @delta_minutes\n\tif (@_collection_time_utc_prev is not null)\n\t\tand (@_collection_time_utc >= dateadd(minute,@delta_minutes,@_collection_time_utc_prev))\n\tbegin\n\t\tinsert #dates values (@_collection_time_utc);\n\t\tset @_collection_time_utc_prev = @_collection_time_utc;\n\tend\n\t\n\tfetch next from cur_dates into @_collection_time_utc;\nend\n\nclose cur_dates;\ndeallocate cur_dates;\n\n--select * from #dates;\n\nif object_id('tempdb..#os_task_list') is not null\n\tdrop table #os_task_list;\nwith cte_tasks as (\n\tselect tl.collection_time_utc, tl.task_name, [memory_kb] = sum([memory_kb]), [counts] = count(*)\n\tfrom $os_task_list_table_name tl join #dates d on d.collection_time_utc = tl.collection_time_utc\n\twhere (tl.collection_time_utc between @start_time_utc and @end_time_utc)\n\tand tl.host_name = @perfmon_host_name\n\tand task_name not in ('System Idle Process','') -- add other harmless processes here\n\tgroup by tl.collection_time_utc, tl.task_name\n)\nselect\tcollection_time_utc, task_name, [memory_kb], counts\n\t\t--,[cpu_s__delta] = isnull(cpu_s - (lag(cpu_s) over (partition by task_name order by collection_time_utc)),0)\n\t\t,[RowRank] = ROW_NUMBER() OVER(PARTITION BY [collection_time_utc] ORDER BY [memory_kb] DESC)\ninto #os_task_list\nfrom cte_tasks;\n\n\nselect collection_time_utc as time, task_name as metric, [memory_kb] as value\nfrom #os_task_list\nwhere [RowRank] <= @TopRowFilter\nand collection_time_utc > (select min(m.collection_time_utc) from #os_task_list m)\norder by [time], [RowRank], [memory_kb] desc;\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\n\telse\n\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @delta_minutes;\nend", - "refId": "A", - "sql": { - "columns": [ - { - "parameters": [], - "type": "function" - } - ], - "groupBy": [ - { - "property": { - "type": "string" - }, - "type": "groupBy" - } - ], - "limit": 50 - } + "format": "table", + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect status, count(*) as counts\nfrom sys.dm_exec_connections as dec with (nolock) \njoin sys.dm_exec_sessions as des with (nolock) on des.session_id = dec.session_id\ngroup by status\norder by counts desc, status\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc;", + "refId": "A" } ], - "title": "[${server}] - OS Processes - Memory Utilization - $cpu_delta_minutes Minute Interval", - "type": "timeseries" + "title": "Connections By Status", + "type": "table" } ], - "repeat": "cpu_delta_minutes", - "title": "OS Tasks (Processes) - Memory Utilization", + "repeat": "datasource", + "title": "SQL Connections - Distribution", "type": "row" }, { @@ -7530,7 +7643,7 @@ "h": 8, "w": 24, "x": 0, - "y": 874 + "y": 1681 }, "id": 86, "options": { @@ -8185,7 +8298,7 @@ "h": 16, "w": 24, "x": 0, - "y": 875 + "y": 1682 }, "id": 185, "options": { @@ -8419,7 +8532,7 @@ "h": 16, "w": 24, "x": 0, - "y": 111 + "y": 918 }, "id": 173, "options": { @@ -8525,7 +8638,7 @@ "h": 15, "w": 24, "x": 0, - "y": 112 + "y": 919 }, "id": 133, "options": { @@ -8604,7 +8717,7 @@ "h": 7, "w": 24, "x": 0, - "y": 923 + "y": 1730 }, "id": 119, "options": { @@ -8943,43 +9056,6 @@ "regex": "", "type": "query" }, - { - "hide": 2, - "label": "OS Tasks Table Name", - "name": "os_task_list_table_name", - "query": "${VAR_OS_TASK_LIST_TABLE_NAME}", - "skipUrlSync": true, - "type": "constant", - "current": { - "value": "${VAR_OS_TASK_LIST_TABLE_NAME}", - "text": "${VAR_OS_TASK_LIST_TABLE_NAME}", - "selected": false - }, - "options": [ - { - "value": "${VAR_OS_TASK_LIST_TABLE_NAME}", - "text": "${VAR_OS_TASK_LIST_TABLE_NAME}", - "selected": false - } - ] - }, - { - "current": {}, - "datasource": { - "type": "mssql", - "uid": "${sqlmonitor_datasource}" - }, - "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select top 1 collection_time_utc\nfrom $os_task_list_table_name with (nolock)\nwhere host_name = @perfmon_host_name\norder by collection_time_utc desc;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "hide": 2, - "includeAll": false, - "label": "Collection Time UTC", - "name": "os_task_list_collection_time_utc", - "options": [], - "query": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select top 1 collection_time_utc\nfrom $os_task_list_table_name with (nolock)\nwhere host_name = @perfmon_host_name\norder by collection_time_utc desc;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", - "refresh": 2, - "regex": "", - "type": "query" - }, { "hide": 2, "label": "WhoIsActive Table Name", @@ -9004,7 +9080,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select top 1 DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time )\nfrom $whoisactive_table_name with (nolock)\norder by collection_time desc\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9021,7 +9097,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @start_time datetime = $__timeFrom()\ndeclare @end_time datetime = $__timeTo();\n\ndeclare @_duration_minute int = datediff(minute,@start_time,@end_time);\nselect [@delta_minutes] = (case when @_duration_minute <= 30 then 1 \n when @_duration_minute <= 60 then 2\n when @_duration_minute <= 120 then 5\n when @_duration_minute <= 480 then 10\n when @_duration_minute <= 1440 then 15\n when @_duration_minute <= 2880 then 20\n when @_duration_minute <= 10080 then 30\n else 60 end);\n", "hide": 2, @@ -9037,7 +9113,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select count(*) as counts from sys.dm_os_schedulers dos where status = 'VISIBLE ONLINE' and is_online = 1;\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9054,7 +9130,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), sqlserver_start_time) from sys.dm_os_sys_info as osi;\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9071,7 +9147,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT --event_time\n\t\tevent_time_utc\n\t\t/*\n\t\t,system_cpu\n\t\t,sql_cpu\n\t\t,idle_system_cpu\n\t\t,user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_user_mode]\n\t\t,kernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t,page_faults_kb\n\t\t*/\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9088,7 +9164,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT /*\n\t\tevent_time\n\t\tevent_time_utc\n\t\t*/\n\t\tsystem_cpu\n\t\t/*\n\t\t,sql_cpu\n\t\t,idle_system_cpu\n\t\t,user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_user_mode]\n\t\t,kernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t,page_faults_kb\n\t\t*/\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9105,7 +9181,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT /*\n\t\tevent_time\n\t\t,event_time_utc\n\t\t,system_cpu\n\t\t*/\n\t\tsql_cpu\n\t\t/*\n\t\t,idle_system_cpu\n\t\t,user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_user_mode]\n\t\t,kernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t,page_faults_kb\n\t\t*/\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9122,7 +9198,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT /*\n\t\tevent_time\n\t\t,event_time_utc\n\t\t,system_cpu\n\t\t,sql_cpu\n\t\t*/\n\t\tidle_system_cpu\n\t\t/*\n\t\t,user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_user_mode]\n\t\t,kernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t,page_faults_kb\n\t\t*/\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9139,7 +9215,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT /*\n\t\tevent_time\n\t\t,event_time_utc\n\t\t,system_cpu\n\t\t,sql_cpu\n\t\t,idle_system_cpu\n\t\t*/\n\t\tcoalesce(user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms),0) as [pcnt_user_mode]\n\t\t/*\n\t\t,kernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t,page_faults_kb\n\t\t*/\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9156,7 +9232,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT /*\n\t\tevent_time\n\t\t,event_time_utc\n\t\t,system_cpu\n\t\t,sql_cpu\n\t\t,idle_system_cpu\n\t\t,user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_user_mode]\n\t\t*/\n\t\tkernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t/*\n\t\t,page_faults_kb\n\t\t*/\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9173,7 +9249,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nSELECT /*\n\t\tevent_time\n\t\t,event_time_utc\n\t\t,system_cpu\n\t\t,sql_cpu\n\t\t,idle_system_cpu\n\t\t,user_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_user_mode]\n\t\t,kernel_mode_time_ms * 100 / (user_mode_time_ms + kernel_mode_time_ms) as [pcnt_kernel_mode]\n\t\t*/\n\t\tpage_faults_kb\nFROM (\n\t\tSELECT\tDATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS event_time\n\t\t\t\t,DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE())) AS event_time_utc\n\t\t\t\t,100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS idle_system_cpu\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/UserModeTime)[1]', 'bigint')/10000 AS user_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/KernelModeTime)[1]', 'bigint')/10000 AS kernel_mode_time_ms\n\t\t\t\t,record.value('(Record/SchedulerMonitorEvent/SystemHealth/PageFaults)[1]', 'bigint')*8.0 AS page_faults_kb\n\t\t\t\t,record\n\t\tFROM (\tSELECT\tTOP 1 timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now\n\t\t\t\tFROM sys.dm_os_ring_buffers orb cross apply sys.dm_os_sys_info osi\n\t\t\t\tWHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR'\n\t\t\t\tAND record LIKE '%%'\n\t\t\t\tORDER BY [timestamp] DESC\n\t\t) AS rd\n) as t;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9210,7 +9286,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect count(*) as blocked_counts --, max(wait_time)/1000 as wait_time_s\nfrom sys.dm_exec_requests r with (nolock) \nwhere r.blocking_session_id <> 0\nand wait_time >= ($blocked_threshold_seconds*1000) -- Over 60 seconds\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9227,7 +9303,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect coalesce(max(wait_time)/1000,0) as wait_time_s --,count(*) as blocked_counts\nfrom sys.dm_exec_requests r with (nolock) \nwhere r.blocking_session_id <> 0\nand wait_time >= ($blocked_threshold_seconds*1000); -- Over 60 seconds\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9244,7 +9320,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect\tosm.total_physical_memory_kb\n\t\t--,osm.available_physical_memory_kb\n\t\t--,case when system_high_memory_signal_state = 1 then 'High' else 'Low' end as [Memory State]\n\t\t--,opm.physical_memory_in_use_kb\n\t\t--,opm.memory_utilization_percentage\nfrom sys.dm_os_sys_memory osm, sys.dm_os_process_memory opm;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9261,7 +9337,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect\t--osm.total_physical_memory_kb\n\t\tosm.available_physical_memory_kb\n\t\t--,case when system_high_memory_signal_state = 1 then 'High' else 'Low' end as [Memory State]\n\t\t--,opm.physical_memory_in_use_kb\n\t\t--,opm.memory_utilization_percentage\nfrom sys.dm_os_sys_memory osm, sys.dm_os_process_memory opm;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9278,7 +9354,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect\t--osm.total_physical_memory_kb\n\t\t--osm.available_physical_memory_kb\n\t\tconvert(int,system_high_memory_signal_state)\n\t\t--,opm.physical_memory_in_use_kb\n\t\t--,opm.memory_utilization_percentage\nfrom sys.dm_os_sys_memory osm, sys.dm_os_process_memory opm;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9295,7 +9371,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect\t--osm.total_physical_memory_kb\n\t\t--osm.available_physical_memory_kb\n\t\t--case when system_high_memory_signal_state = 1 then 'High' else 'Low' end as [Memory State]\n\t\topm.physical_memory_in_use_kb\n\t\t--,opm.memory_utilization_percentage\nfrom sys.dm_os_sys_memory osm, sys.dm_os_process_memory opm;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9312,7 +9388,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect\t--osm.total_physical_memory_kb\n\t\t--osm.available_physical_memory_kb\n\t\t--case when system_high_memory_signal_state = 1 then 'High' else 'Low' end as [Memory State]\n\t\t--opm.physical_memory_in_use_kb\n\t\topm.memory_utilization_percentage\nfrom sys.dm_os_sys_memory osm, sys.dm_os_process_memory opm;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9329,7 +9405,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "exec dbo.usp_GetAllServerInfo \n\t\t\t\t@servers = '$server'\n\t\t\t\t,@output = 'memory_grants_pending'", "hide": 2, @@ -9346,7 +9422,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nWITH ple AS \n (\n SELECT TOP 1 \n pc.*,\n [idx_dollar] = CHARINDEX('$', pc.object_name),\n [idx_colon] = CHARINDEX(':', pc.object_name)\n FROM sys.dm_os_performance_counters pc\n WHERE counter_name = N'Page life expectancy'\n )\n SELECT \n [instance_id] = SUBSTRING(object_name, [idx_dollar] + 1, [idx_colon] - [idx_dollar] - 1)\n FROM ple\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9363,7 +9439,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"declare @object_name varchar(255);\nset @object_name = case when @@servicename is null then 'MSSQL$$instance_id'\n\t\t\t\t\t\twhen @@servicename = 'MSSQLSERVER' then 'SQLServer' \n\t\t\t\t\t\telse 'MSSQL$'+@@SERVICENAME \n\t\t\t\t\t\tend;\n\nSELECT cntr_value \nFROM sys.dm_os_performance_counters WITH (NOLOCK) \nWHERE [object_name] like (@object_name+':Buffer Manager%') AND counter_name = N'Page life expectancy'\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9380,7 +9456,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"declare @server_major_version_number tinyint;\nSET @server_major_version_number = CONVERT(tinyint, SERVERPROPERTY('ProductMajorVersion'))\n\nif @server_major_version_number is null\nbegin\n\t;with t_versions as \n\t( select CONVERT(varchar,SERVERPROPERTY('ProductVersion')) as ProductVersion\n\t\t\t ,LEFT(CONVERT(varchar,SERVERPROPERTY('ProductVersion')), CHARINDEX('.',CONVERT(varchar,SERVERPROPERTY('ProductVersion')))-1) AS MajorVersion\n\t)\n\tselect @server_major_version_number = MajorVersion from t_versions;\nend\n\nselect\t[@server_major_version_number] = @server_major_version_number;\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9397,7 +9473,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\ndeclare @server_product_version varchar(20);\ndeclare @server_major_version_number tinyint;\ndeclare @server_minor_version_number smallint;\n\nSET @server_product_version = CONVERT(varchar,SERVERPROPERTY('ProductVersion'));\nSET @server_major_version_number = CONVERT(tinyint, SERVERPROPERTY('ProductMajorVersion'));\n\nif @server_major_version_number is null\nbegin\n\t;with t_versions as \n\t( select CONVERT(varchar,SERVERPROPERTY('ProductVersion')) as ProductVersion\n\t\t\t ,LEFT(CONVERT(varchar,SERVERPROPERTY('ProductVersion')), CHARINDEX('.',CONVERT(varchar,SERVERPROPERTY('ProductVersion')))-1) AS MajorVersion\n\t)\n\tselect @server_major_version_number = MajorVersion from t_versions;\nend\n\ndeclare @server_minor_version_number_intermediate varchar(20);\nset @server_minor_version_number_intermediate = REPLACE(@server_product_version,CONVERT(varchar,@server_major_version_number)+'.'+CONVERT(varchar, SERVERPROPERTY('ProductMinorVersion'))+'.','');\n\nif(@server_minor_version_number_intermediate is null)\nbegin\n\t;with t_versions as\n\t( select replace(@server_product_version,CONVERT(varchar,@server_major_version_number)+'.','') as VrsnString )\n\tselect @server_minor_version_number_intermediate = REPLACE(@server_product_version,CONVERT(varchar,@server_major_version_number)+'.'+LEFT(VrsnString,CHARINDEX('.',VrsnString)-1)+'.','')\n\tfrom t_versions;\nend\n\nset @server_minor_version_number = left(@server_minor_version_number_intermediate,charindex('.',@server_minor_version_number_intermediate)-1);\n\nSELECT\t[@server_minor_version_number] = @server_minor_version_number\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9414,7 +9490,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select count(*) as counts from sys.dm_exec_connections with (nolock);\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9431,7 +9507,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\n--\tQuery to find what's is running on server\nSELECT\tCOUNT(*) as active_request_count\nFROM\tsys.dm_exec_sessions AS s\nLEFT JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id\nOUTER APPLY sys.dm_exec_sql_text(r.sql_handle) AS st\nOUTER APPLY sys.dm_exec_query_plan(r.plan_handle) AS bqp\nOUTER APPLY sys.dm_exec_text_query_plan(r.plan_handle,r.statement_start_offset, r.statement_end_offset) as sqp\nWHERE\ts.session_id != @@SPID\n\tAND (\t(CASE\tWHEN\ts.session_id IN (select ri.blocking_session_id from sys.dm_exec_requests as ri )\n\t\t\t\t\t--\tGet sessions involved in blocking (including system sessions)\n\t\t\t\t\tTHEN\t1\n\t\t\t\t\tWHEN\tr.blocking_session_id IS NOT NULL AND r.blocking_session_id <> 0\n\t\t\t\t\tTHEN\t1\n\t\t\t\t\tELSE\t0\n\t\t\tEND) = 1\n\t\t\tOR\n\t\t\t(CASE\tWHEN\ts.session_id > 50\n\t\t\t\t\t\t\tAND r.session_id IS NOT NULL -- either some part of session has active request\n\t\t\t\t\t\t\t--AND ISNULL(open_resultset_count,0) > 0 -- some result is open\n\t\t\t\t\t\t\tAND s.status <> 'sleeping'\n\t\t\t\t\tTHEN\t1\n\t\t\t\t\tELSE\t0\n\t\t\tEND) = 1\n\t\t\tOR\n\t\t\t(CASE\tWHEN\ts.session_id > 50\n\t\t\t\t\t\t\tAND ISNULL(r.open_transaction_count,0) > 0\n\t\t\t\t\tTHEN\t1\n\t\t\t\t\tELSE\t0\n\t\t\tEND) = 1\n\t\t);\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9448,7 +9524,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\n\nset @sql_instance = '$server';\n\nset quoted_identifier off;\nset @sql = \"declare @waits_seconds__per_core_per_minute bigint;\nexec usp_waits_per_core_per_minute @waits_seconds__per_core_per_minute = @waits_seconds__per_core_per_minute output;\nselect [waits_seconds__per_core_per_minute] = @waits_seconds__per_core_per_minute;\n\"\nset quoted_identifier on;\n\nif (@sql_instance = SERVERPROPERTY('SERVERNAME'))\n exec dbo.sp_executesql @sql;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql;", "hide": 2, @@ -9485,7 +9561,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect top 1 collection_time_utc\nfrom $diskspace_table_name with (nolock)\norder by collection_time_utc desc\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9502,7 +9578,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "select [object_name] = (case when '$perfmon_service_name' = 'MSSQLSERVER' then 'SQLServer' else '$perfmon_service_name' end);", "hide": 2, @@ -9519,7 +9595,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select CONVERT(tinyint,SERVERPROPERTY('IsClustered')) as IsClustered;\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9536,7 +9612,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"select CONVERT(tinyint,SERVERPROPERTY('IsHadrEnabled')) as IsHadrEnabled;\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9553,7 +9629,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "select case when $is_hadr_enabled = 1 then 'AG' else 'Cluster' end", "hide": 2, @@ -9608,7 +9684,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect top 1 js.[UpdatedDateUTC]\nfrom [dbo].[sql_agent_job_stats] js\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9648,7 +9724,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "exec dbo.usp_GetAllServerInfo \n\t\t\t\t@servers = '$server'\n\t\t\t\t,@output = 'host_distribution'", "hide": 2, @@ -9665,7 +9741,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "exec dbo.usp_GetAllServerInfo \n\t\t\t\t@servers = '$server'\n\t\t\t\t,@output = 'processor_name'", "hide": 2, @@ -9682,7 +9758,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect top 1 [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time)\nfrom [dbo].[tempdb_space_usage] tsu\norder by collection_time desc\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9699,7 +9775,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @params = N'@perfmon_host_name varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nselect top 1 [collection_time_utc] = DATEADD(mi, DATEDIFF(mi, getdate(), getutcdate()), collection_time)\nfrom [dbo].[log_space_consumers] lsc\norder by collection_time desc\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name;", "hide": 2, @@ -9716,7 +9792,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\n\nset @sql_instance = '$server';\n\nset quoted_identifier off;\nset @sql = \"declare @avg_disk_wait_ms bigint;\nexec usp_avg_disk_wait_ms @avg_disk_wait_ms = @avg_disk_wait_ms output;\nselect [avg_disk_wait_ms] = @avg_disk_wait_ms;\n\"\nset quoted_identifier on;\n\nif (@sql_instance = SERVERPROPERTY('SERVERNAME'))\n exec dbo.sp_executesql @sql;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql;", "hide": 2, @@ -9733,7 +9809,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${sqlmonitor_datasource}" + "uid": "${DS_SQLMONITOR}" }, "definition": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\n\nset @sql_instance = '$server';\n\nset quoted_identifier off;\nset @sql = \"exec usp_avg_disk_latency_ms\"\nset quoted_identifier on;\n\nif (@sql_instance = SERVERPROPERTY('SERVERNAME'))\n exec dbo.sp_executesql @sql;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql;", "hide": 2, @@ -9750,7 +9826,7 @@ "current": {}, "datasource": { "type": "mssql", - "uid": "${DS_SQLMONITOR}" + "uid": "${sqlmonitor_datasource}" }, "definition": "declare @sql_instance varchar(255);\nset @sql_instance = '${server}';\n\n--select @sql_instance;\n\nselect top 1 [InstanceScopeFeaturesOnly] = coalesce(JSON_VALUE(more_info, '$.InstanceScopeFeaturesOnly'), 'false')\nfrom dbo.instance_details id\nwhere 1=1\nand id.sql_instance = @sql_instance;\n", "hide": 2, @@ -9781,6 +9857,6 @@ "timezone": "browser", "title": "Monitoring - Live - Distributed", "uid": "distributed_live_dashboard", - "version": 24, + "version": 42, "weekStart": "" } \ No newline at end of file diff --git a/Grafana-Dashboards/Monitoring - Perfmon Counters - Quest Softwares - Distributed.json b/Grafana-Dashboards/Monitoring - Perfmon Counters - Quest Softwares - Distributed.json index 67b3009..a666682 100644 --- a/Grafana-Dashboards/Monitoring - Perfmon Counters - Quest Softwares - Distributed.json +++ b/Grafana-Dashboards/Monitoring - Perfmon Counters - Quest Softwares - Distributed.json @@ -44,6 +44,12 @@ "name": "Microsoft SQL Server", "version": "11.3.0" }, + { + "type": "datasource", + "id": "prometheus", + "name": "Prometheus", + "version": "1.0.0" + }, { "type": "panel", "id": "timeseries", @@ -284,7 +290,7 @@ "h": 5, "w": 24, "x": 0, - "y": 9973 + "y": 18292 }, "id": 64, "options": { @@ -416,7 +422,7 @@ "h": 6, "w": 24, "x": 0, - "y": 2 + "y": 90314 }, "id": 26, "options": { @@ -517,7 +523,7 @@ "h": 11, "w": 24, "x": 0, - "y": 7246 + "y": 102391 }, "id": 58, "options": { @@ -639,7 +645,7 @@ "h": 7, "w": 24, "x": 0, - "y": 7257 + "y": 102402 }, "id": 60, "options": { @@ -761,7 +767,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -878,7 +885,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null } ] }, @@ -890,7 +898,7 @@ "h": 11, "w": 24, "x": 0, - "y": 5608 + "y": 14 }, "id": 30, "options": { @@ -996,7 +1004,8 @@ "mode": "absolute", "steps": [ { - "color": "green" + "color": "green", + "value": null }, { "color": "red", @@ -1012,7 +1021,7 @@ "h": 8, "w": 24, "x": 0, - "y": 5619 + "y": 25 }, "id": 66, "options": { @@ -1148,7 +1157,7 @@ "h": 6, "w": 24, "x": 0, - "y": 4 + "y": 46903 }, "id": 105, "options": { @@ -1270,7 +1279,7 @@ "h": 15, "w": 24, "x": 0, - "y": 4843 + "y": 55347 }, "id": 106, "options": { @@ -1407,7 +1416,7 @@ "h": 5, "w": 24, "x": 0, - "y": 5 + "y": 18211 }, "id": 110, "options": { @@ -1415,7 +1424,7 @@ "calcs": [ "max", "p99", - "p98" + "last" ], "displayMode": "table", "placement": "right", @@ -1528,14 +1537,14 @@ "h": 6, "w": 24, "x": 0, - "y": 4825 + "y": 18216 }, "id": 112, "options": { "legend": { "calcs": [ "p99", - "p98" + "last" ], "displayMode": "table", "placement": "right", @@ -1583,6 +1592,122 @@ "title": "Logical Disk - Latency", "type": "timeseries" }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "Disk Transfer/Sec => The number of reads and writes per second to disk ", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 2, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + }, + "unit": "none" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 18222 + }, + "id": 157, + "options": { + "legend": { + "calcs": [ + "max", + "p99" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "99th %", + "sortDesc": true + }, + "tooltip": { + "mode": "multi", + "sort": "none" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @perfmon_object varchar(125);\ndeclare @disk_drive varchar(255) = '$disk_drive';\ndeclare @database varchar(255) = '$database';\n\nset @database = case when ltrim(rtrim(@database)) = '__All__' then null else @database end;\nset @disk_drive = case when ltrim(rtrim(@disk_drive)) = '__All__' then null else @disk_drive end;\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @perfmon_object = '$perfmon_object';\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @perfmon_object varchar(125), @disk_drive varchar(255), @database varchar(255)';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect pc.collection_time_utc as time\n\t,instance+' ('+replace(counter,'disk ','')+')' as metric\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name \nand pc.object = 'logicaldisk' and pc.counter in ('disk transfers/sec')\n--and pc.object = 'logicaldisk' and pc.counter in ('disk reads/sec','disk writes/sec')\nand (pc.instance <> '_total' and pc.instance not like 'harddiskvolume%')\n\"+(case when @disk_drive is null then '-- ' else '' end)+\"and (pc.instance+'\\') = @disk_drive\norder by time\n\"\nset quoted_identifier on;\n\n--if (@sql_instance = SERVERPROPERTY('SERVERNAME'))\nif ($is_local = 1)\n exec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object, @disk_drive, @database;\nelse\n exec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object, @disk_drive, @database;", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Logical Disk - IOPS", + "type": "timeseries" + }, { "datasource": { "type": "mssql", @@ -1648,7 +1773,7 @@ "h": 7, "w": 24, "x": 0, - "y": 4831 + "y": 18230 }, "id": 114, "options": { @@ -1783,7 +1908,7 @@ "h": 5, "w": 24, "x": 0, - "y": 6 + "y": 51137 }, "id": 34, "options": { @@ -1905,7 +2030,7 @@ "h": 6, "w": 24, "x": 0, - "y": 4199 + "y": 51142 }, "id": 68, "options": { @@ -2026,7 +2151,7 @@ "h": 7, "w": 24, "x": 0, - "y": 4205 + "y": 51148 }, "id": 69, "options": { @@ -2081,6 +2206,124 @@ ], "title": "Physical Disk - Throughput", "type": "timeseries" + }, + { + "datasource": { + "type": "mssql", + "uid": "${sqlmonitor_datasource}" + }, + "description": "Disk Transfers/Sec => The number of disk reads and writes per second", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 2, + "scaleDistribution": { + "log": 10, + "type": "log" + }, + "showPoints": "always", + "spanNulls": true, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + } + ] + }, + "unit": "none" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 51155 + }, + "id": 158, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "p99", + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "99th %", + "sortDesc": true + }, + "tooltip": { + "mode": "single", + "sort": "desc" + } + }, + "pluginVersion": "11.3.1", + "targets": [ + { + "alias": "", + "datasource": { + "type": "mssql", + "uid": "${DS_SQLMONITOR}" + }, + "editorMode": "code", + "format": "time_series", + "rawQuery": true, + "rawSql": "declare @sql nvarchar(max);\ndeclare @params nvarchar(max);\ndeclare @sql_instance varchar(255);\ndeclare @perfmon_host_name varchar(255);\ndeclare @start_time_utc datetime2;\ndeclare @end_time_utc datetime2;\ndeclare @perfmon_object varchar(125);\n\nset @sql_instance = '$server';\nset @perfmon_host_name = '$perfmon_host_name';\nset @start_time_utc = $__timeFrom();\nset @end_time_utc = $__timeTo();\nset @perfmon_object = '$perfmon_object';\nset @params = N'@perfmon_host_name varchar(255), @start_time_utc datetime2, @end_time_utc datetime2, @perfmon_object varchar(125)';\n\nset quoted_identifier off;\nset @sql = \"\nset nocount on;\nselect pc.collection_time_utc as time\n\t,instance+' ('+replace(counter,'disk ','')+')' as metric\n\t--,object, counter, instance\n\t,pc.value\nfrom $perfmon_table_name pc with (nolock)\nwhere collection_time_utc between @start_time_utc and @end_time_utc\nand pc.host_name = @perfmon_host_name \nand pc.object = 'physicaldisk' and pc.counter in ('disk transfers/sec') and instance like '%:%'\n--and pc.object = 'physicaldisk' and pc.counter in ('disk bytes/sec','disk write bytes/sec','disk read bytes/sec')\nand pc.instance <> '_total'\norder by time\n\"\nset quoted_identifier on;\n\nif '$InstanceScopeFeaturesOnly' = 'true'\n\tselect [time], remarks as metric, [value]\n\tfrom (values ('This feature is not available for this SQLInstance',getutcdate(), 0)) dummy(remarks, [time], value)\n\torder by [time]\nelse\nbegin\n\tif ($is_local = 1)\n\t\t\texec dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object;\n\t\telse\n\t\t\texec [$server].[$dba_db].dbo.sp_executesql @sql , @params, @perfmon_host_name, @start_time_utc, @end_time_utc, @perfmon_object;\nend", + "refId": "A", + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + } + } + ], + "title": "Physical Disk - IOPS", + "type": "timeseries" } ], "repeat": "datasource", @@ -2162,7 +2405,7 @@ "h": 7, "w": 24, "x": 0, - "y": 7 + "y": 95152 }, "id": 71, "options": { @@ -2299,7 +2542,7 @@ "h": 15, "w": 24, "x": 0, - "y": 15 + "y": 95160 }, "id": 38, "options": { @@ -2427,7 +2670,7 @@ "h": 12, "w": 24, "x": 0, - "y": 30 + "y": 95175 }, "id": 115, "options": { @@ -2570,7 +2813,7 @@ "h": 9, "w": 24, "x": 0, - "y": 1897 + "y": 97042 }, "id": 119, "options": { @@ -2696,7 +2939,7 @@ "h": 10, "w": 24, "x": 0, - "y": 1906 + "y": 97051 }, "id": 121, "options": { @@ -2819,7 +3062,7 @@ "h": 15, "w": 24, "x": 0, - "y": 1916 + "y": 97061 }, "id": 123, "options": { @@ -2955,7 +3198,7 @@ "h": 10, "w": 24, "x": 0, - "y": 2666 + "y": 97811 }, "id": 22, "options": { @@ -3089,7 +3332,7 @@ "h": 6, "w": 24, "x": 0, - "y": 2677 + "y": 97822 }, "id": 126, "options": { @@ -3210,7 +3453,7 @@ "h": 7, "w": 24, "x": 0, - "y": 2683 + "y": 97828 }, "id": 14, "options": { @@ -3332,7 +3575,7 @@ "h": 17, "w": 24, "x": 0, - "y": 2690 + "y": 97835 }, "id": 124, "options": { @@ -3466,7 +3709,7 @@ "h": 13, "w": 24, "x": 0, - "y": 2956 + "y": 98101 }, "id": 153, "options": { @@ -3601,7 +3844,7 @@ "h": 9, "w": 24, "x": 0, - "y": 2970 + "y": 98115 }, "id": 77, "options": { @@ -3722,7 +3965,7 @@ "h": 12, "w": 24, "x": 0, - "y": 2979 + "y": 98124 }, "id": 128, "options": { @@ -3859,7 +4102,7 @@ "h": 6, "w": 24, "x": 0, - "y": 3116 + "y": 98261 }, "id": 46, "options": { @@ -3980,7 +4223,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3122 + "y": 98267 }, "id": 129, "options": { @@ -4101,7 +4344,7 @@ "h": 7, "w": 24, "x": 0, - "y": 3130 + "y": 98275 }, "id": 78, "options": { @@ -4223,7 +4466,7 @@ "h": 7, "w": 24, "x": 0, - "y": 3137 + "y": 98282 }, "id": 130, "options": { @@ -4360,7 +4603,7 @@ "h": 5, "w": 24, "x": 0, - "y": 19640 + "y": 114785 }, "id": 134, "options": { @@ -4481,7 +4724,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19718 + "y": 114863 }, "id": 137, "options": { @@ -4602,7 +4845,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19724 + "y": 114869 }, "id": 135, "options": { @@ -4723,7 +4966,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19730 + "y": 114875 }, "id": 136, "options": { @@ -4858,7 +5101,7 @@ "h": 5, "w": 11, "x": 0, - "y": 3242 + "y": 98387 }, "id": 85, "options": { @@ -4972,7 +5215,7 @@ "h": 5, "w": 13, "x": 11, - "y": 3242 + "y": 98387 }, "id": 89, "options": { @@ -5090,7 +5333,7 @@ "h": 5, "w": 11, "x": 0, - "y": 3247 + "y": 98392 }, "id": 88, "options": { @@ -5204,7 +5447,7 @@ "h": 5, "w": 13, "x": 11, - "y": 3247 + "y": 98392 }, "id": 90, "options": { @@ -5339,7 +5582,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3253 + "y": 98398 }, "id": 151, "options": { @@ -5459,7 +5702,7 @@ "h": 7, "w": 24, "x": 0, - "y": 3261 + "y": 98406 }, "id": 16, "options": { @@ -5595,7 +5838,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3370 + "y": 98515 }, "id": 80, "options": { @@ -5717,7 +5960,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3378 + "y": 98523 }, "id": 79, "options": { @@ -5839,7 +6082,7 @@ "h": 8, "w": 24, "x": 0, - "y": 3386 + "y": 98531 }, "id": 82, "options": { @@ -5975,7 +6218,7 @@ "h": 6, "w": 24, "x": 0, - "y": 19644 + "y": 114789 }, "id": 102, "options": { @@ -6096,7 +6339,7 @@ "h": 8, "w": 24, "x": 0, - "y": 19650 + "y": 114795 }, "id": 100, "options": { @@ -6228,7 +6471,7 @@ "h": 8, "w": 24, "x": 0, - "y": 22300 + "y": 117445 }, "id": 148, "options": { @@ -6324,7 +6567,7 @@ "h": 8, "w": 24, "x": 0, - "y": 22308 + "y": 117453 }, "id": 147, "options": { @@ -6435,7 +6678,7 @@ "h": 5, "w": 24, "x": 0, - "y": 22301 + "y": 117446 }, "id": 149, "options": { @@ -6875,6 +7118,6 @@ "timezone": "", "title": "Monitoring - Perfmon Counters - Quest Softwares - Distributed", "uid": "distributed_perfmon", - "version": 23, + "version": 34, "weekStart": "" } \ No newline at end of file diff --git a/PowerShell-Scripts-Miscellaneous/CPU_Memory_OS_Processes_Perfmon.ps1 b/PowerShell-Scripts-Miscellaneous/CPU_Memory_OS_Processes_Perfmon.ps1 new file mode 100644 index 0000000..c77c502 --- /dev/null +++ b/PowerShell-Scripts-Miscellaneous/CPU_Memory_OS_Processes_Perfmon.ps1 @@ -0,0 +1,45 @@ +# Get top processes with CPU Utilization +$cores = (Get-CimInstance Win32_ComputerSystem).NumberOfLogicalProcessors + +Get-Counter "\Process(*)\% Processor Time" | +Select -ExpandProperty CounterSamples | +Select InstanceName, +@{Name = "CPU_Percent"; Expression = { [math]::Round($_.CookedValue / $cores, 2) } } | +Where-Object { $_.CPU_Percent -gt 0 } | Sort-Object -Property CPU_Percent -Descending | ogv + + + + +# Get Memory Utilization using Perfmon +Get-Counter "\Process(*)\Working Set" | +Select -ExpandProperty CounterSamples | +Select InstanceName, @{l = 'MemoryMB'; e = { [math]::Round($_.CookedValue / 1024.0 / 1024.0, 2) } } | +Sort-Object -Property MemoryMB -Descending | Select * -First 20 + + + +# Get Memory Utilization using Get-Process +$procGroups = Get-Process | +Select Name, Id, +@{n = "WorkingSet(MB)"; e = { [math]::Round($_.WorkingSet64 / 1MB, 2) } }, +@{n = "Private(MB)"; e = { [math]::Round($_.PrivateMemorySize64 / 1MB, 2) } } | +Group-Object -Property Name + +[System.Collections.ArrayList]$allProcesses = @() +foreach ($group in $procGroups) { + $procName = $group.Name + $procCount = $group.Count + $procInstances = @() + $procInstances += $group.Group + $procMemoryTotal = 0.0 + + foreach ($inst in $procInstances) { + $instMem = $inst.'WorkingSet(MB)' + $procMemoryTotal = $procMemoryTotal + $instMem + } + + $allProcesses.Add([PSCustomObject]@{Name = $procName; WorkingSet_MB = $procMemoryTotal }) +} + +$allProcesses | Sort-Object -Property WorkingSet_MB -Descending | Select -First 20 | ogv + diff --git a/PowerShell-Scripts-Miscellaneous/PowerPlan.ps1 b/PowerShell-Scripts-Miscellaneous/PowerPlan.ps1 new file mode 100644 index 0000000..c483f2c --- /dev/null +++ b/PowerShell-Scripts-Miscellaneous/PowerPlan.ps1 @@ -0,0 +1,9 @@ +# find existing powerplans +powercfg -l + +# add "Ultimate Performance" mode option +powercfg -duplicatescheme e9a42b02-d5df-448d-aa00-03f14749eb61 + +# Enable "Ultimate Performance" mode +powercfg -l +powercfg.exe -SETACTIVE <> \ No newline at end of file diff --git a/README.md b/README.md index 363e9d8..11b0348 100644 --- a/README.md +++ b/README.md @@ -1,399 +1,104 @@ -# SQLMonitor - SQLServer Monitoring & Alerting - -If you are a developer, or DBA who manages Microsoft SQL Servers, it becomes important to understand current load vs usual load when SQL Server is slow. This repository contains scripts that will help you to setup baseline on individual SQL Server instances, and then visualize the collected data using Grafana through one Inventory server with Linked Server for individual SQL Server instances. - -Navigation -- [SQLMonitor - SQLServer Monitoring \& Alerting](#sqlmonitor---sqlserver-monitoring--alerting) - - [Why SQLMonitor?](#why-sqlmonitor) - - [Features](#features) - - [Live Dashboard - Basic Metrics](#live-dashboard---basic-metrics) - - [Live Dashboard - Perfmon Counters - Quest Softwares](#live-dashboard---perfmon-counters---quest-softwares) - - [Portal Credentials](#portal-credentials) - - [How to Setup](#how-to-setup) - - [Jobs for SQLMonitor](#jobs-for-sqlmonitor) - - [Download SQLMonitor](#download-sqlmonitor) - - [Execute Wrapper Script](#execute-wrapper-script) - - [Below is sample code present in `Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1`](#below-is-sample-code-present-in-wrapper-sampleswrapper-installsqlmonitorps1) - - [Setup Grafana Dashboards](#setup-grafana-dashboards) - - [Remove SQLMonitor](#remove-sqlmonitor) - - [Below is sample code present in `Wrapper-Samples/Wrapper-RemoveSQLMonitor.ps1`](#below-is-sample-code-present-in-wrapper-sampleswrapper-removesqlmonitorps1) - - [Support](#support) - - [Related Links](#related-links) +# SQLMonitor -## Why SQLMonitor? -SQLMonitor is designed as open-source tool to replace expensive enterprise monitoring or to simply fill the gap and monitor all environments such as DEV, TEST, QA/UAT & PROD. +**Open-source SQL Server monitoring & alerting — built on SQL Agent jobs, Grafana, Prometheus and Python.** -[![YouTube Tutorial on SQLMonitor](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/YouTube-Thumbnail-Live-All-Servers.png)](https://ajaydwivedi.com/youtube/sqlmonitor)
+[![Live Demo](https://img.shields.io/badge/live%20demo-sqlmonitor.ajaydwivedi.com-brightgreen)](https://sqlmonitor.ajaydwivedi.com) +[![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://imajaydwivedi.github.io/SQLMonitor/) +[![YouTube](https://img.shields.io/badge/YouTube-tutorials-red)](https://ajaydwivedi.com/youtube/sqlmonitor) +[![Slack](https://img.shields.io/badge/Slack-%23sqlmonitor-purple)](https://ajaydwivedi.com/sqlmonitor/slack) -### Features -- Simple & customizable as metric collection happens through SQL Agent jobs. -- Easy to debug since entire SQLMonitor tools is built of just few tables, stored procedures & sql agent jobs. -- Grafana based Central & Individual dashboards to analyze metrics -- Collection jobs using stored procedures with data loading utilizing very small sized perfmon/xevent files puts very minimal performance overhead. -- Highly optimized grafana dashboard queries using dynamically Parameterized tsql makes the data visualization to scale well even when dashboard users increase. -- Near to zero manual configuration required. Purging controlled through just one table/job. -- Depending on version of SQL Server, tables are automatically "Hourly" partitioned & Compressed. So index or other maintenance not even required. -- Utilizing Memory Optimized tables on central server for core stability metric storage gives it Unlimited scalability. -- Tools has capability to allow same or different sql instance as Data Target. Thus gives high flexibility & scalability. -- Works with all supported SQL Servers (with some limitations on 2008R2 like XEvent not available). -- Alert Engine built with Python & SQLServer with PagerDuty, Slack & Email target. +> Replace expensive enterprise monitoring with something you can install on a single inventory server, point at the rest of your fleet, and open in Grafana ten minutes later — from DEV all the way through PROD. -![](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor-AlertEngine/Sma-Slack-3-Images-Gif.gif)
+📖 **Full documentation:** +--- -## Live Dashboard - Basic Metrics -You can visit [https://sqlmonitor.ajaydwivedi.com](https://sqlmonitor.ajaydwivedi.com/d/distributed_live_dashboard/monitoring-live-distributed?orgId=1&refresh=5s) for live dashboard for basic real time monitoring.

+## Live Demo -![](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/Live-Dashboards-All.gif)
- - -## Live Dashboard - Perfmon Counters - Quest Softwares -Visit [https://sqlmonitor.ajaydwivedi.com](https://sqlmonitor.ajaydwivedi.com/d/distributed_perfmon/monitoring-perfmon-counters-quest-softwares-distributed?orgId=1&refresh=5m) for live dashboard of all Perfmon counters suggested in [SQL Server Perfmon Counters of Interest - Quest Software](https://drive.google.com/file/d/1LB7Joo6055T1FfPcholXByazOX55e5b8/view?usp=sharing).

- -![](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/Quest-Dashboards-All.gif)
- -### Portal Credentials -Database/Grafana Portal | User Name | Password ------------- | --------- | --------- -[https://sqlmonitor.ajaydwivedi.com/](https://sqlmonitor.ajaydwivedi.com/dashboards?tag=sqlmonitor) | guest | ajaydwivedi-guest -Sql Instance -> sqlmonitor.ajaydwivedi.com:1433 | grafana | grafana - -## How to Setup -SQLMonitor supports both Central & Distributed topology. In preferred distributed topology, each SQL Server instance monitors itself. The required objects like tables, view, functions, procedures, scripts, jobs etc. are created on the monitored instance itself. - -SQLMonitor utilizes PowerShell script to collect various metric from operating system including setting up Perfmon data collector, pushing the collected perfmon data to sql tables, collecting os processes running etc. - -For collecting metrics available from inside SQL Server, it used standard tsql procedures. - -All the objects are created in [`DBA`] databases. Only few stored procedures that should have capability to be executed from context of any databases are created in [master] database. - -For both OS metrics & SQL metric, SQL Agent jobs are used as schedulers. Each job has its own schedule which may differ in frequency of data collection from every one minute to once a week. - -![](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/SQLMonitor-Distributed-Topology.png)
- -### Jobs for SQLMonitor - -Following are few of the SQLMonitor data collection jobs. Each of these jobs is set to `(dba) SQLMonitor` job category along with fixed naming convention of `(dba) *********`. - -
- - SQLAgent Jobs Created by SQLMonitor - -| Job Name | Job Category | Schedule | Job Type | Location | -| ---------------------------------:|:----------------:|:----------------:|:----------:|:----------------------:| -| (dba) Check-InstanceAvailability | (dba) SQLMonitor | Every 1 minute | PowerShell | Inventory Server | -| (dba) Get-AllServerInfo | (dba) SQLMonitor | Every 1 minute | TSQL | Inventory Server | -| (dba) Get-AllServerCollectedData | (dba) SQLMonitor | Every 5 minute | TSQL | Inventory Server | -| (dba) Update-SqlServerVersions | (dba) SQLMonitor | Once a week | PowerShell | Inventory Server | -| (dba) Collect-PerfmonData | (dba) SQLMonitor | Every 2 minute | PowerShell | PowerShell Jobs Server | -| (dba) Check-SQLAgentJobs | (dba) SQLMonitor | Every 5 minute | TSQL | Tsql Jobs Server | -| (dba) Collect-AgHealthState | (dba) SQLMonitor | Every 2 minute | TSQL | Tsql Jobs Server | -| (dba) Collect-DiskSpace | (dba) SQLMonitor | Every 30 minutes | PowerShell | PowerShell Jobs Server | -| (dba) Collect-FileIOStats | (dba) SQLMonitor | Every 10 minute | TSQL | Tsql Jobs Server | -| (dba) Collect-MemoryClerks | (dba) SQLMonitor | Every 2 minute | TSQL | Tsql Jobs Server | -| (dba) Collect-OSProcesses | (dba) SQLMonitor | Every 2 minute | PowerShell | PowerShell Jobs Server | -| (dba) Collect-PrivilegedInfo | (dba) SQLMonitor | Every 10 minute | TSQL | Tsql Jobs Server | -| (dba) Collect-WaitStats | (dba) SQLMonitor | Every 10 minutes | TSQL | Tsql Jobs Server | -| (dba) Collect-XEvents | (dba) SQLMonitor | Every minute | TSQL | Tsql Jobs Server | -| (dba) Partitions-Maintenance | (dba) SQLMonitor | Every Day | TSQL | Tsql Jobs Server | -| (dba) Purge-Tables | (dba) SQLMonitor | Every Day | TSQL | Tsql Jobs Server | -| (dba) Remove-XEventFiles | (dba) SQLMonitor | Every 4 hours | PowerShell | PowerShell Jobs Server | -| (dba) Run-Blitz | (dba) SQLMonitor | Once a Week | TSQL | Tsql Jobs Server | -| (dba) Run-BlitzIndex | (dba) SQLMonitor | Every Day | TSQL | Tsql Jobs Server | -| (dba) Run-BlitzIndex - Weekly | (dba) SQLMonitor | Once a Week | TSQL | Tsql Jobs Server | -| (dba) Run-LogSaver | (dba) SQLMonitor | Every 5 minutes | TSQL | Tsql Jobs Server | -| (dba) Run-TempDbSaver | (dba) SQLMonitor | Every 5 minutes | TSQL | Tsql Jobs Server | -| (dba) Run-WhoIsActive | (dba) SQLMonitor | Every 2 minute | TSQL | Tsql Jobs Server | - ----- - -`PowerShell Jobs Server` can be same SQL Instance that is being baselined, or some other server in same Cluster network, or some some other server in same network, or even Inventory Server. - -`Tsql Jobs Server` can be same SQL Instance that is being baselined, or some other server in same Cluster network, or some some other server in same network, or even Inventory Server. - -
- -### Download SQLMonitor -Download SQLMonitor repository on your central server from where you deploy your scripts on all other servers. Say, after closing SQLMonitor, our local repo directory is `D:\Ajay-Dwivedi\GitHub-Personal\SQLMonitor`. - -If the local SQLMonitor repo folder already exists, simply pull the latest from master branch. - -### Execute Wrapper Script -Create a directory named Private inside SQLMonitor, and copy the scripts of `SQLMonitor\Wrapper-Samples\` into `SQLMonitor\Private\` folder. -Open the script `D:\Ajay-Dwivedi\GitHub-Personal\SQLMonitor\Private\Wrapper-InstallSQLMonitor.ps1`. Replace the appropriate values for parameters, and execute the script. - -#### Below is sample code present in `Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1` - -
-Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1 - -```Wrapper-InstallSQLMonitor - -#$DomainCredential = Get-Credential -UserName 'Lab\SQLServices' -Message 'AD Account' -#$saAdmin = Get-Credential -UserName 'sa' -Message 'sa' -#$localAdmin = Get-Credential -UserName 'Administrator' -Message 'Local Admin' - -cls -Import-Module dbatools; -$params = @{ - SqlInstanceToBaseline = 'Workstation' - DbaDatabase = 'DBA' - #HostName = 'Workstation' - #RetentionDays = 7 - DbaToolsFolderPath = 'D:\Github\dbatools' # Download using Save-Module command - #FirstResponderKitZipFile = 'D:\Softwares\SQL-Server-First-Responder-Kit-20231010.zip' # Download from Releases section - #DarlingDataZipFile = 'D:\Softwares\DarlingData-main.zip' # Download from Code dropdown - #OlaHallengrenSolutionZipFile = 'D:\Github\sql-server-maintenance-solution-master.zip' # Download from Code dropdown - #RemoteSQLMonitorPath = 'C:\SQLMonitor' - InventoryServer = 'SQLMonitor' - InventoryDatabase = 'DBA' - DbaGroupMailId = 'some_dba_mail_id@gmail.com' - #SqlCredential = $personal - #WindowsCredential = $DomainCredential - <# - SkipSteps = @( "1__sp_WhoIsActive", "2__AllDatabaseObjects", "3__XEventSession", - "4__FirstResponderKitObjects", "5__DarlingDataObjects", "6__OlaHallengrenSolutionObjects", - "7__sp_WhatIsRunning", "8__usp_GetAllServerInfo", "9__CopyDbaToolsModule2Host", - "10__CopyPerfmonFolder2Host", "11__SetupPerfmonDataCollector", "12__CreateCredentialProxy", - "13__CreateJobCollectDiskSpace", "14__CreateJobCollectOSProcesses", "15__CreateJobCollectPerfmonData", - "16__CreateJobCollectWaitStats", "17__CreateJobCollectXEvents", "18__CreateJobCollectFileIOStats", - "19__CreateJobPartitionsMaintenance", "20__CreateJobPurgeTables", "21__CreateJobRemoveXEventFiles", - "22__CreateJobRunLogSaver", "23__CreateJobRunTempDbSaver", "24__CreateJobRunWhoIsActive", - "25__CreateJobRunBlitzIndex", "26__CreateJobRunBlitz", "27__CreateJobRunBlitzIndexWeekly", - "28__CreateJobCollectMemoryClerks", "29__CreateJobCollectPrivilegedInfo", "30__CreateJobCollectAgHealthState", - "31__CreateJobCheckSQLAgentJobs", "32__CreateJobUpdateSqlServerVersions", "33__CreateJobCheckInstanceAvailability", - "34__CreateJobGetAllServerInfo", "35__CreateJobGetAllServerCollectedData", "36__WhoIsActivePartition", - "37__BlitzIndexPartition", "38__BlitzPartition", "39__EnablePageCompression", - "40__GrafanaLogin", "41__LinkedServerOnInventory", "42__LinkedServerForDataDestinationInstance", - "43__AlterViewsForDataDestinationInstance") - #> - #OnlySteps = @( "2__AllDatabaseObjects", "29__CreateJobCollectAgHealthState" ) - #StartAtStep = '1__sp_WhoIsActive' - #StopAtStep = '39__AlterViewsForDataDestinationInstance' - #DropCreatePowerShellJobs = $true - #DryRun = $false - #SkipRDPSessionSteps = $true - #SkipPowerShellJobs = $true - #SkipTsqlJobs = $true - #SkipMailProfileCheck = $true - #skipCollationCheck = $true - #SkipWindowsAdminAccessTest = $true - #SkipDriveCheck = $true - #SkipPingCheck = $true - #SkipMultiServerviewsUpgrade = $false - #ForceSetupOfTaskSchedulerJobs = $true - #SqlInstanceAsDataDestination = 'Workstation' - #SqlInstanceForPowershellJobs = 'Workstation' - #SqlInstanceForTsqlJobs = 'Workstation' - #ConfirmValidationOfMultiInstance = $true - #ConfirmSetupOfTaskSchedulerJobs = $true - #HasCustomizedTsqlJobs = $true - #HasCustomizedPowerShellJobs = $true - #OverrideCustomizedTsqlJobs = $false - #OverrideCustomizedPowerShellJobs = $false - #UpdateSQLAgentJobsThreshold = $false - #XEventDirectory = 'D:\MSSQL15.MSSQLSERVER\XEvents\' - #JobsExecutionWaitTimeoutMinutes = 15 -} - - -#$preSQL = "EXEC dbo.usp_check_sql_agent_jobs @default_mail_recipient = 'sqlagentservice@gmail.com', @drop_recreate = 1" -#$postSQL = Get-Content "D:\GitHub-Personal\SQLMonitor\DDLs\Update-SQLAgentJobsThreshold.sql" -#D:\GitHub\SQLMonitor\SQLMonitor\Install-SQLMonitor.ps1 @Params #-Debug -PreQuery $preSQL -PostQuery $postSQL -D:\GitHub\SQLMonitor\SQLMonitor\Install-SQLMonitor.ps1 @Params - -#Get-Help F:\GitHub\SQLMonitor\SQLMonitor\Install-SQLMonitor.ps1 -ShowWindow - -<# -$dropWhoIsActive = @" -if object_id('dbo.WhoIsActive_Staging') is not null - drop table dbo.WhoIsActive_Staging; - -if object_id('dbo.WhoIsActive') is not null - drop table dbo.WhoIsActive; -"@; -F:\GitHub\SQLMonitor\SQLMonitor\Install-SQLMonitor.ps1 @Params -PreQuery $dropWhoIsActive -#> - -<# -# **************** Download other github repos/modules/files *********************** - -# **__ SQLMonitor __** -Invoke-WebRequest https://github.com/imajaydwivedi/SQLMonitor/archive/refs/heads/dev.zip ` - -OutFile "$($env:USERPROFILE)\Downloads\sqlmonitor.zip" - -# **__ dbatools & dbatools.library __** -Save-Module dbatools -Path "$($env:USERPROFILE)\Downloads\" - -# **__ PoshRSJob on Inventory __** -Install-Module PoshRSJob -Scope AllUsers -Verbose -Save-Module PoshRSJob -Path "$($env:USERPROFILE)\Downloads\" - -# **__ Darling Data __** -Invoke-WebRequest https://github.com/erikdarlingdata/DarlingData/archive/refs/heads/main.zip ` - -OutFile "$($env:USERPROFILE)\Downloads\DarlingData-main.zip" - -# **__ Ola Hallengren Maintenance Solution __** -Invoke-WebRequest https://github.com/olahallengren/sql-server-maintenance-solution/archive/refs/heads/master.zip ` - -OutFile "$($env:USERPROFILE)\Downloads\sql-server-maintenance-solution-master.zip" - -# **__ First Responder Kit from latest release __** -if ($true) { - $repo = "BrentOzarULTD/SQL-Server-First-Responder-Kit" - $tags = "https://api.github.com/repos/$repo/tags" - - $tagName = (Invoke-WebRequest $tags | ConvertFrom-Json)[0].name - $releaseZip = "https://github.com/$repo/archive/refs/tags/$tagName.zip" - - Invoke-WebRequest $releaseZip ` - -OutFile "$($env:USERPROFILE)\Downloads\SQL-Server-First-Responder-Kit-$tagName.zip" -} - -# **__ PoshRSJob - Download from Github __** -if ($true) { - $repo = "proxb/PoshRSJob" - $releases = "https://api.github.com/repos/$repo/releases" - - $tagName = (Invoke-WebRequest $releases | ConvertFrom-Json)[0].tag_name - $releaseZip = "https://github.com/$repo/releases/download/$tagName/PoshRSJob.zip" - - Invoke-WebRequest $releaseZip ` - -OutFile "$($env:USERPROFILE)\Downloads\PoshRSJob.zip" -} - -#> - -<# -Get-DbaDbMailProfile -SqlInstance '192.168.56.31' -SqlCredential $personalCredential -Copy-DbaDbMail -Source '192.168.56.15' -Destination '192.168.56.31' -SourceSqlCredential $personalCredential -DestinationSqlCredential $personalCredential # Lab -New-DbaCredential -SqlInstance 'xy' -Identity $LabCredential.UserName -SecurePassword $LabCredential.Password -Force # -SqlCredential $SqlCredential -EnableException -New-DbaAgentProxy -SqlInstance 'xy' -Name $LabCredential.UserName -ProxyCredential $LabCredential.UserName -SubSystem PowerShell,CmdExec - -Enable-PSRemoting -Force -SkipNetworkProfileCheck # remote machine -Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value SQLMonitor.Lab.com -Concatenate -Force # remote machine -Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Set-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Value 1 - -# Incase -#Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value * -Force # run on local machine -#Set-NetConnectionProfile -NetworkCategory Private # Execute this only if above command fails - -Enter-PSSession -ComputerName '192.168.56.31' -Credential $localAdmin -Authentication Negotiate -Test-WSMan '192.168.56.31' -Credential $localAdmin -Authentication Negotiate - -Get-ChildItem C:\SQLMonitor -Recurse -File | Unblock-File -Verbose -#> - -<# -# Add SQLAgent Service Account to below local windows groups. - # Computer Management > System Tools > Local Users and Groups > Groups -1) Administrators -2) Performance Log Users -3) Performance Monitor Users -#> +| | | +|---|---| +| 🔗 Grafana | | +| 👤 Login | `guest` / `ajaydwivedi-guest` | +| 💾 SQL | `sqlmonitor.ajaydwivedi.com:1433` — user `grafana` / `grafana` (read-only on `DBA`) | -``` -
- ----- - -> [!IMPORTANT] -> To get a better understand of SQLMonitor installation, I would recommend to watch [YouTube Playlist](https://ajaydwivedi.com/youtube/sqlmonitor) [https://ajaydwivedi.com/youtube/sqlmonitor](https://ajaydwivedi.com/youtube/sqlmonitor). - - -### Setup Grafana Dashboards -Download Grafana which is open source visualization tool. Install & configure same. +[![YouTube Tutorial](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/YouTube-Thumbnail-Live-All-Servers.png)](https://ajaydwivedi.com/youtube/sqlmonitor) -Create a datasource on Grafana that connects to your Inventory Server. Say, we set it with name 'SQLMonitor'. Use `grafana` as login & password while setting up this data source. The `grafana` sql login is created on each server being baselined with `db_datareader` on `DBA` database. +![Live Dashboards](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/Live-Dashboards-All.gif) -At next step, import all the dashboard `*.json` files on path `D:\Ajay-Dwivedi\GitHub-Personal\SQLMonitor\Grafana-Dashboards` into `SQLServer` folder on grafana portal. While importing each JSON file, we need to explicitly choose `SQLMonitor` Data Source & Folder we created in above steps. +--- -## Remove SQLMonitor -Similar to `Wrapper-InstallSQLMonitor`, we have `Wrapper-RemoveSQLMonitor` that can help us remove SQLMonitor for a particular baselined server. -Ensure that all scripts from folder `\SQLMonitor\Wrapper-Samples\` are copied into `\SQLMonitor\Private\` folder. +## What you get -Open script `D:\Ajay-Dwivedi\GitHub-Personal\SQLMonitor\Private\Wrapper-RemoveSQLMonitor.ps1`. Replace the appropriate values for parameters, and execute the script. +- **17 Grafana dashboards** — distributed live, per-instance deep-dive, Blitz-family diagnostics, AG health, backups, disk, wait stats, workload. +- **46 SQL Agent jobs** in one `(dba) SQLMonitor` category — polling DMVs, OS, WMI, XEvents, Perfmon on every instance. +- **One central `DBA` database** on an inventory server aggregates the fleet via linked servers and fans it out to Grafana. +- **Python alert engine** with Slack, PagerDuty and email routing — 18 built-in alerts, all threshold-tunable without touching code. +- **Prometheus path** via [`sql_exporter`](https://github.com/burningalchemist/sql_exporter) + `windows_exporter` for shops that prefer pull-based metrics. +- **AI Agent** (Streamlit + LangChain + Ollama) that answers natural-language questions against the inventory. +- **First-Responder-Kit, Darling-Data, Ola-Hallengren, sp_WhoIsActive** installed and scheduled by default — so the diagnostic tooling is already there when you need it. -#### Below is sample code present in `Wrapper-Samples/Wrapper-RemoveSQLMonitor.ps1` +![Alert Engine](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor-AlertEngine/Sma-Slack-3-Images-Gif.gif) -
-Wrapper-Samples/Wrapper-RemoveSQLMonitor.ps1 +--- -```Wrapper-RemoveSQLMonitor +## Quick install -#$DomainCredential = Get-Credential -UserName 'Lab\SQLServices' -Message 'AD Account' -#$saAdmin = Get-Credential -UserName 'sa' -Message 'sa' -#$localAdmin = Get-Credential -UserName 'Administrator' -Message 'Local Admin' +Minimal happy-path (see the [full deployment guide](https://imajaydwivedi.github.io/SQLMonitor/deployment/) for prerequisites, parameters and troubleshooting): -cls -Import-Module dbatools; -$params = @{ - SqlInstanceToBaseline = 'Experiment' - #DbaDatabase = 'DBA' - #HostName = 'Experiment' - InventoryServer = 'SQLMonitor' - InventoryDatabase = 'DBA' - #RemoteSQLMonitorPath = 'C:\SQLMonitor' - #SqlCredential = $saAdmin - #WindowsCredential = $localAdmin - #SkipRDPSessionSteps = $true - #SkipSteps = @("43__RemovePerfmonFilesFromDisk") - #StartAtStep = '30__DropLogin_Grafana' - #StopAtStep = '11__RemoveJob_RunBlitzIndex' - #SqlInstanceForTsqlJobs = 'Experiment\SQL2019' - #SqlInstanceAsDataDestination = 'Experiment\SQL2019' - #SqlInstanceForPowershellJobs = 'Experiment\SQL2019' - SkipDropTable = $true - #SkipRemoveJob = $true - #SkipDropProc = $true - #SkipDropView = $true - #ConfirmValidationOfMultiInstance = $true - #ActionType = "Update" - #OnlySteps = @("16__RemoveJob_RunBlitz","70__DropTable_Blitz") - #DryRun = $false -} +```powershell +# 1. Clone onto the inventory server +git clone https://github.com/imajaydwivedi/SQLMonitor.git C:\SQLMonitor +cd C:\SQLMonitor\SQLMonitor -#$preSQL = "EXEC dbo.usp_check_sql_agent_jobs @default_mail_recipient = 'sqlagentservice@gmail.com', @drop_recreate = 1" -#$postSQL = Get-Content "D:\GitHub-Personal\SQLMonitor\DDLs\Update-SQLAgentJobsThreshold.sql" -#D:\GitHub\SQLMonitor\SQLMonitor\Remove-SQLMonitor.ps1 @Params #-Debug -PreQuery $preSQL -PostQuery $postSQL -D:\GitHub\SQLMonitor\SQLMonitor\Remove-SQLMonitor.ps1 @Params +# 2. Copy the wrapper template and edit it (credentials, instance name, host) +Copy-Item .\Wrapper-Samples\Wrapper-InstallSQLMonitor.ps1 .\Private\Wrapper-InstallSQLMonitor.ps1 +notepad .\Private\Wrapper-InstallSQLMonitor.ps1 +# 3. Run it +.\Private\Wrapper-InstallSQLMonitor.ps1 -Verbose +``` -#Get-DbaDbMailProfile -SqlInstance '192.168.56.31' -SqlCredential $personalCredential -#Copy-DbaDbMail -Source '192.168.56.15' -Destination '192.168.56.31' -SourceSqlCredential $personalCredential -DestinationSqlCredential $personalCredential # Lab -#New-DbaCredential -SqlInstance 'xy' -Identity $LabCredential.UserName -SecurePassword $LabCredential.Password -Force # -SqlCredential $SqlCredential -EnableException -#New-DbaAgentProxy -SqlInstance 'xy' -Name $LabCredential.UserName -ProxyCredential $LabCredential.UserName -SubSystem PowerShell,CmdExec -<# +Install Grafana, import the JSON dashboards from `Grafana-Dashboards/`, point them at the inventory SQL datasource — done. -Enable-PSRemoting -Force # run on remote machine -Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value * -Force # run on local machine -Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value 192.168.56.15 -Force -#Set-NetConnectionProfile -NetworkCategory Private # Execute this only if above command fails +Removing an instance is the exact inverse: -Enter-PSSession -ComputerName '192.168.56.31' -Credential $localAdmin -Authentication Negotiate -Test-WSMan '192.168.56.31' -Credential $localAdmin -Authentication Negotiate +```powershell +Copy-Item .\Wrapper-Samples\Wrapper-RemoveSQLMonitor.ps1 .\Private\Wrapper-RemoveSQLMonitor.ps1 +.\Private\Wrapper-RemoveSQLMonitor.ps1 -Verbose +``` -#> +--- +## Documentation -``` -
+Everything lives on the docs site — . Quick index: +| Section | What you'll find | +|---|---| +| [Architecture](https://imajaydwivedi.github.io/SQLMonitor/architecture/) | Topology, data flow, components, data model | +| [Deployment](https://imajaydwivedi.github.io/SQLMonitor/deployment/) | Prerequisites, installer walkthrough, parameter reference, 59 install steps, upgrade, removal, troubleshooting | +| [Dashboards](https://imajaydwivedi.github.io/SQLMonitor/dashboards/) | Catalog of all 17 Grafana dashboards, live-demo links, Grafana variable conventions | +| [Jobs & Collectors](https://imajaydwivedi.github.io/SQLMonitor/jobs/) | Authoritative list of all 46 SQL Agent jobs with schedules and source links | +| [Alerting](https://imajaydwivedi.github.io/SQLMonitor/alerting/) | The Python Alert Engine, 18 built-in alerts, Slack/PagerDuty wiring | +| [Prometheus path](https://imajaydwivedi.github.io/SQLMonitor/prometheus/) | `sql_exporter` setup, collector files, migration from Perfmon | +| [AI Agent](https://imajaydwivedi.github.io/SQLMonitor/ai-agent/) | Streamlit + LangChain + Ollama agent over the inventory | +| [Support & FAQ](https://imajaydwivedi.github.io/SQLMonitor/support/) | Getting help, filing bugs, common questions | +--- ## Support -For community support regarding this tool, kindly join [#sqlmonitor](https://ajaydwivedi.com/sqlmonitor/slack) channel on [sqlcommunity.slack.com](https://ajaydwivedi.com/join/slack) slack workspace. -For paid support, reach out to me directly on [#sqlmonitor](https://ajaydwivedi.com/go/slack) slack channel. +- **Community:** [`#sqlmonitor`](https://ajaydwivedi.com/sqlmonitor/slack) on [sqlcommunity.slack.com](https://ajaydwivedi.com/join/slack) +- **Bugs:** [GitHub Issues](https://github.com/imajaydwivedi/SQLMonitor/issues) +- **Paid / private:** DM on Slack, or via + +## Related links -## Related Links -- Github Repo -> [https://ajaydwivedi.com/github/sqlmonitor](https://ajaydwivedi.com/github/sqlmonitor) -- Demo Site -> [https://ajaydwivedi.com/demo/sqlmonitor](https://ajaydwivedi.com/demo/sqlmonitor) -- Demo site Credentials -> [https://ajaydwivedi.com/go/sqlmonitor](https://ajaydwivedi.com/go/sqlmonitor) -- YouTube Playlist -> [https://ajaydwivedi.com/youtube/sqlmonitor](https://ajaydwivedi.com/youtube/sqlmonitor) -- Blogs -> [https://ajaydwivedi.com/category/sqlmonitor](https://ajaydwivedi.com/category/sqlmonitor) -- Community Help on **#sqlmonitor** channel -> [https://ajaydwivedi.com/sqlmonitor/slack](https://ajaydwivedi.com/sqlmonitor/slack) +- GitHub: +- Live demo: +- YouTube playlist: +- Blog posts: ------------------------------ +--- Thanks :smiley:. Subscribe for updates :thumbsup: diff --git a/SQLMonitor.sln b/SQLMonitor.sln new file mode 100644 index 0000000..3a45c85 --- /dev/null +++ b/SQLMonitor.sln @@ -0,0 +1,82 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# SQL Server Management Studio Version 22 +VisualStudioVersion = 18.0.11205.157 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Alerting", "Alerting\Alerting.ssmssqlproj", "{F85AF121-8497-4D2D-9E7C-F9B04A636F99}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Credential-Manager", "Credential-Manager\Credential-Manager.ssmssqlproj", "{759C6EAC-8635-46B1-8746-6B2DBB75E895}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "DDLs", "DDLs\DDLs.ssmssqlproj", "{304BEC50-2BB2-4862-8757-C6E01A906360}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Grafana-Dashboards", "Grafana-Dashboards\Grafana-Dashboards.ssmssqlproj", "{E03F3544-C2B9-4465-89EB-8C2859A42631}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "NoteBooks", "NoteBooks\NoteBooks.ssmssqlproj", "{3B98D83D-CEB9-44A6-AFC9-EFF43C4EA60F}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Sql-Queries", "Sql-Queries\Sql-Queries.ssmssqlproj", "{D2B4B1E9-4683-4D0A-A0D8-CF1DB6887879}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Temp-Scripts", "Temp-Scripts\Temp-Scripts.ssmssqlproj", "{97B43977-BFB0-4921-AA96-BAF970743910}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "TSQLTextNormalizer", "TSQLTextNormalizer\TSQLTextNormalizer.ssmssqlproj", "{5E60F32C-BCBD-47E8-BB3A-7F16722A421D}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work", "Work\Work.ssmssqlproj", "{719D9F57-63FF-4A99-870B-BAC8B25845E2}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Sql-Queries-Work", "Sql-Queries-Work\Sql-Queries-Work.ssmssqlproj", "{71007107-7371-4FA7-9510-357C3738812B}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-SOC-Project", "Work-SOC-Project\Work-SOC-Project.ssmssqlproj", "{BB731120-D924-4882-BF4A-54C0DA7FF385}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-VAPT-Login-Expiry", "Work-VAPT-Login-Expiry\Work-VAPT-Login-Expiry.ssmssqlproj", "{7A265B02-D3F6-42C3-86D4-C1BD8BA2111B}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-Fix-SQLMonitor-Issues", "Work-Fix-SQLMonitor-Issues\Work-Fix-SQLMonitor-Issues.ssmssqlproj", "{8CA435C9-FBE2-4D4D-AE50-C005D9875119}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-Backup-Issues", "Work-Backup-Issues\Work-Backup-Issues.ssmssqlproj", "{C2EDA1A4-8CA2-494C-8C6B-8E4BE49E6E64}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-Yogesh-Query-Tuning", "Work-Yogesh-Query-Tuning\Work-Yogesh-Query-Tuning.ssmssqlproj", "{241D7782-2556-4BD9-8063-A72B7BAF7950}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-TOM-GM-DR-Replication", "Work-TOM-GM-DR-Replication\Work-TOM-GM-DR-Replication.ssmssqlproj", "{604139B8-C349-4340-BE05-54655C544C89}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-LAMA", "Work-LAMA\Work-LAMA.ssmssqlproj", "{B144CA7D-0D76-483A-8B34-7DE7617B21B5}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Work-DDLs", "Work-DDLs\Work-DDLs.ssmssqlproj", "{2BFAB0E9-5F6A-4E18-A5A9-75ECC0BDAAA0}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Volatile", "Volatile\Volatile.ssmssqlproj", "{F3E4D04A-F293-49FF-8A4B-2A8931F9CA7F}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Private", "Private\Private.ssmssqlproj", "{E7A8BAFA-E3DE-4E05-88B0-0C993FDDE9DE}" +EndProject +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "AI-Agent", "AI-Agent\AI-Agent.ssmssqlproj", "{ACC48069-6D68-4598-51C6-E0FC1800C934}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Default|Default = Default|Default + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F85AF121-8497-4D2D-9E7C-F9B04A636F99}.Default|Default.ActiveCfg = Default + {759C6EAC-8635-46B1-8746-6B2DBB75E895}.Default|Default.ActiveCfg = Default + {304BEC50-2BB2-4862-8757-C6E01A906360}.Default|Default.ActiveCfg = Default + {E03F3544-C2B9-4465-89EB-8C2859A42631}.Default|Default.ActiveCfg = Default + {3B98D83D-CEB9-44A6-AFC9-EFF43C4EA60F}.Default|Default.ActiveCfg = Default + {D2B4B1E9-4683-4D0A-A0D8-CF1DB6887879}.Default|Default.ActiveCfg = Default + {97B43977-BFB0-4921-AA96-BAF970743910}.Default|Default.ActiveCfg = Default + {5E60F32C-BCBD-47E8-BB3A-7F16722A421D}.Default|Default.ActiveCfg = Default + {719D9F57-63FF-4A99-870B-BAC8B25845E2}.Default|Default.ActiveCfg = Default + {71007107-7371-4FA7-9510-357C3738812B}.Default|Default.ActiveCfg = Default + {BB731120-D924-4882-BF4A-54C0DA7FF385}.Default|Default.ActiveCfg = Default + {7A265B02-D3F6-42C3-86D4-C1BD8BA2111B}.Default|Default.ActiveCfg = Default + {8CA435C9-FBE2-4D4D-AE50-C005D9875119}.Default|Default.ActiveCfg = Default + {C2EDA1A4-8CA2-494C-8C6B-8E4BE49E6E64}.Default|Default.ActiveCfg = Default + {241D7782-2556-4BD9-8063-A72B7BAF7950}.Default|Default.ActiveCfg = Default + {604139B8-C349-4340-BE05-54655C544C89}.Default|Default.ActiveCfg = Default + {B144CA7D-0D76-483A-8B34-7DE7617B21B5}.Default|Default.ActiveCfg = Default + {2BFAB0E9-5F6A-4E18-A5A9-75ECC0BDAAA0}.Default|Default.ActiveCfg = Default + {F3E4D04A-F293-49FF-8A4B-2A8931F9CA7F}.Default|Default.ActiveCfg = Default + {E7A8BAFA-E3DE-4E05-88B0-0C993FDDE9DE}.Default|Default.ActiveCfg = Default + {ACC48069-6D68-4598-51C6-E0FC1800C934}.Default|Default.ActiveCfg = Default + {9B3A6454-3A40-48F7-A939-81918A511CB4}.Default|Default.ActiveCfg = Default + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E15EC377-70AB-49B2-A6DB-C1371A8691D7} + EndGlobalSection +EndGlobal diff --git a/SQLMonitor/DBA_PerfMon_All_Counters_Template.xml b/SQLMonitor/DBA_PerfMon_All_Counters_Template.xml index 4564662..03076d9 100644 Binary files a/SQLMonitor/DBA_PerfMon_All_Counters_Template.xml and b/SQLMonitor/DBA_PerfMon_All_Counters_Template.xml differ diff --git a/SQLMonitor/Install-SQLMonitor.ps1 b/SQLMonitor/Install-SQLMonitor.ps1 index 5d09ca6..a5c9ed4 100644 Binary files a/SQLMonitor/Install-SQLMonitor.ps1 and b/SQLMonitor/Install-SQLMonitor.ps1 differ diff --git a/SQLMonitor/Remove-SQLMonitor.ps1 b/SQLMonitor/Remove-SQLMonitor.ps1 index 33b39e8..5aa0add 100644 Binary files a/SQLMonitor/Remove-SQLMonitor.ps1 and b/SQLMonitor/Remove-SQLMonitor.ps1 differ diff --git a/SQLMonitor/Steps-Addition-N-Removal-4-InstallSQLMonitor.ps1 b/SQLMonitor/Steps-Addition-N-Removal-4-InstallSQLMonitor.ps1 index b2ba245..70810f1 100644 --- a/SQLMonitor/Steps-Addition-N-Removal-4-InstallSQLMonitor.ps1 +++ b/SQLMonitor/Steps-Addition-N-Removal-4-InstallSQLMonitor.ps1 @@ -5,7 +5,7 @@ Param ( [String]$Action = "AddStep", [Parameter(Mandatory=$false)] - [String]$StepName = "51__CreateJobComputeAllServerVolatileInfoHistoryHourly", + [String]$StepName = "45__CreateJobGetAllServerAlertHistory", [Parameter(Mandatory=$false)] [Bool]$PrintUserFriendlyFormat = $true, diff --git a/SQLMonitor/Steps-Addition-N-Removal-4-RemoveSQLMonitor.ps1 b/SQLMonitor/Steps-Addition-N-Removal-4-RemoveSQLMonitor.ps1 index 03f6422..a8f9336 100644 --- a/SQLMonitor/Steps-Addition-N-Removal-4-RemoveSQLMonitor.ps1 +++ b/SQLMonitor/Steps-Addition-N-Removal-4-RemoveSQLMonitor.ps1 @@ -2,10 +2,10 @@ Param ( [Parameter(Mandatory=$false)] [ValidateSet("AddStep", "RemoveStep")] - [String]$Action = "AddStep", + [String]$Action = "RemoveStep", [Parameter(Mandatory=$false)] - [String]$StepName = "55__DropProc_UspCollectPerformanceMetrics", + [String]$StepName = "92__DropTable_OsTaskList", [Parameter(Mandatory=$false)] [Bool]$PrintUserFriendlyFormat = $true, diff --git a/SQLMonitor/collect_all_server_alert_messages.py b/SQLMonitor/collect_all_server_alert_messages.py index 374bd50..6f23f80 100644 --- a/SQLMonitor/collect_all_server_alert_messages.py +++ b/SQLMonitor/collect_all_server_alert_messages.py @@ -221,7 +221,7 @@ def pool_handler(): #for allrows in p.map(query_server, servers): #for row in allrows: #final_result.append(row) - + #total_servers_count = len(servers) #success_servers_count = len(final_result) success_servers_count = len(successful_servers) @@ -239,10 +239,10 @@ def pool_handler(): for row in successful_servers: pt_successful_servers.add_row([row,]) #print(row) - print(f"\nServers with successful connectivity: {success_servers_count}/{total_servers_count}") + print(f"\nServers with successful connectivity: {success_servers_count}/{total_servers_count}") print(pt_successful_servers) else: - print(f"\nNo server with successful connectivity.") + print(f"\nNo server with successful connectivity.") if failed_servers_count > 0: #print(failed_servers) @@ -250,10 +250,10 @@ def pool_handler(): for row in failed_servers: pt_failed_servers.add_row([row,]) #print(row) - print(f"\nServers with failed connectivity: {failed_servers_count}/{total_servers_count}") + print(f"\nServers with failed connectivity: {failed_servers_count}/{total_servers_count}") print(pt_failed_servers) else: - print(f"\nNo server with failed connectivity.") + print(f"\nNo server with failed connectivity.") def update_inventory(): diff --git a/SQLMonitor/sqlserver-versions-update.ps1 b/SQLMonitor/sqlserver-versions-update.ps1 index 850553e..2fe19f7 100644 --- a/SQLMonitor/sqlserver-versions-update.ps1 +++ b/SQLMonitor/sqlserver-versions-update.ps1 @@ -17,6 +17,8 @@ $ErrorActionPreference = 'Stop' $currentTime = Get-Date "$(Get-Date -Format yyyyMMMdd_HHmm) {0,-10} {1}" -f 'INFO:', "Fetch file from Internet.." +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::SystemDefault +#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $SqlServerVersionsQuery = (Invoke-WebRequest $GitHubURL -UseBasicParsing).Content "$(Get-Date -Format yyyyMMMdd_HHmm) {0,-10} {1}" -f 'INFO:', "Execute query against [$SqlInstance].[$Database].$TableName.." diff --git a/Sql-Queries/RingBuffer-PerfMon-CPU-Memory.sql b/Sql-Queries/RingBuffer-PerfMon-CPU-Memory.sql index d0b812d..c93e659 100644 --- a/Sql-Queries/RingBuffer-PerfMon-CPU-Memory.sql +++ b/Sql-Queries/RingBuffer-PerfMon-CPU-Memory.sql @@ -1,8 +1,8 @@ -- https://www.sqlskills.com/blogs/jonathan/identifying-external-memory-pressure-with-dm_os_ring_buffers-and-ring_buffer_resource_monitor/ USE master; -/* Version: v0.2 - Update Date: 25-Mar-2022 +/* Version: v0.3 + Update Date: 10-Jan-2026 */ SET NOCOUNT ON; @@ -181,16 +181,12 @@ DECLARE @system_cpu_utilization VARCHAR(2000); DECLARE @sql_cpu_utilization VARCHAR(2000); ;WITH T_Cpu_Ring_Buffer AS ( - SELECT EventTime, - CASE WHEN system_cpu_utilization_post_sp2 IS NOT NULL THEN system_cpu_utilization_post_sp2 ELSE system_cpu_utilization_pre_sp2 END AS system_cpu_utilization, - CASE WHEN sql_cpu_utilization_post_sp2 IS NOT NULL THEN sql_cpu_utilization_post_sp2 ELSE sql_cpu_utilization_pre_sp2 END AS sql_cpu_utilization + SELECT EventTime, system_cpu_utilization, sql_cpu_utilization ,ROW_NUMBER()OVER(PARTITION BY CAST(EventTime as smalldatetime) ORDER BY EventTime ASC) as cpu_minute_id FROM ( SELECT record.value('(Record/@id)[1]', 'int') AS record_id, DATEADD (ms, -1 * (ts_now - [timestamp]), GETDATE()) AS EventTime, - 100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu_utilization_post_sp2, - record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu_utilization_post_sp2, - 100-record.value('(Record/SchedluerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu_utilization_pre_sp2, - record.value('(Record/SchedluerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu_utilization_pre_sp2 + 100-record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system_cpu_utilization, + record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sql_cpu_utilization FROM ( SELECT timestamp, CONVERT (xml, record) AS record, cpu_ticks / (cpu_ticks/ms_ticks) as ts_now FROM sys.dm_os_ring_buffers cross apply sys.dm_os_sys_info WHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR' diff --git a/Sql-Queries/SCH-Remove-Replication.sql b/Sql-Queries/SCH-Remove-Replication.sql new file mode 100644 index 0000000..2ebe2c0 --- /dev/null +++ b/Sql-Queries/SCH-Remove-Replication.sql @@ -0,0 +1,152 @@ +-- Purpose: +-- Cleanup replication/distribution leftovers only. +-- +-- Usage: +-- Run each section on the server noted in the header. +-- Review results before executing destructive statements. + +------------------------------------------------------------------------------- +-- [Run on: Experiment] +------------------------------------------------------------------------------- +use [master] +go + +if db_id(N'DBATools') is not null exec master.dbo.sp_removedbreplication N'DBATools'; +if db_id(N'DBA') is not null exec master.dbo.sp_removedbreplication N'DBA'; +exec master.dbo.sp_dropdistributor @no_checks = 1, @ignore_distributor = 1; +go + +------------------------------------------------------------------------------- +-- [Run on: DEMO\SQL2019] +------------------------------------------------------------------------------- +use [master] +go + +if db_id(N'DBA') is not null exec master.dbo.sp_removedbreplication N'DBA'; +if db_id(N'DBATools') is not null exec master.dbo.sp_removedbreplication N'DBATools'; +exec master.dbo.sp_dropdistributor @no_checks = 1, @ignore_distributor = 1; +go + +------------------------------------------------------------------------------- +-- [Run on: localhost / distributor] Inventory before cleanup. +------------------------------------------------------------------------------- +use [master] +go + +set nocount on; +select @@servername as server_name, + serverproperty('IsDistributor') as is_distributor, + serverproperty('IsPublisher') as is_publisher, + serverproperty('IsSubscriber') as is_subscriber; + +select name, is_distributor, is_published, is_subscribed, is_merge_published +from sys.databases +order by name; +go + +------------------------------------------------------------------------------- +-- [Run on: localhost / distributor] Remove orphaned distpublisher row. +------------------------------------------------------------------------------- +begin tran; +delete from msdb.dbo.MSdistpublishers where name = N'EXPERIMENT'; +commit tran; +go + +------------------------------------------------------------------------------- +-- [Run on: localhost / distributor] Find blocking sessions on distribution. +------------------------------------------------------------------------------- +select s.session_id, + s.login_name, + s.host_name, + s.program_name, + db_name(c.database_id) as db_name, + s.status +from sys.dm_exec_sessions s +join sys.dm_exec_connections c on s.session_id = c.session_id +where c.database_id = db_id(N'distribution'); +go + +-- Example if distribution drop is blocked: +-- kill 85; +go + +------------------------------------------------------------------------------- +-- [Run on: localhost / distributor] Drop distribution DB and distributor. +------------------------------------------------------------------------------- +if db_id(N'distribution') is not null +begin + alter database [distribution] set single_user with rollback immediate; + exec master.dbo.sp_dropdistributiondb @database = N'distribution'; +end +go + +exec master.dbo.sp_dropdistributor @no_checks = 1, @ignore_distributor = 1; +go + +------------------------------------------------------------------------------- +-- [Run on: localhost] Remove replication SQL Agent jobs. +------------------------------------------------------------------------------- +declare @job_name sysname; + +declare cur_repl_jobs cursor local fast_forward for + select j.name + from msdb.dbo.sysjobs j + join msdb.dbo.syscategories c on j.category_id = c.category_id + where c.category_class = 1 + and c.name like N'REPL%'; + +open cur_repl_jobs; +fetch next from cur_repl_jobs into @job_name; +while @@fetch_status = 0 +begin + exec msdb.dbo.sp_delete_job @job_name = @job_name; + fetch next from cur_repl_jobs into @job_name; +end +close cur_repl_jobs; +deallocate cur_repl_jobs; +go + +------------------------------------------------------------------------------- +-- [Run on: localhost] Remove jobs owned by distributor_admin and drop login. +------------------------------------------------------------------------------- +declare @owned_job sysname; + +declare cur_owned_jobs cursor local fast_forward for + select name from msdb.dbo.sysjobs where suser_sname(owner_sid) = N'distributor_admin'; + +open cur_owned_jobs; +fetch next from cur_owned_jobs into @owned_job; +while @@fetch_status = 0 +begin + exec msdb.dbo.sp_delete_job @job_name = @owned_job; + fetch next from cur_owned_jobs into @owned_job; +end +close cur_owned_jobs; +deallocate cur_owned_jobs; + +if exists (select 1 from sys.server_principals where name = N'distributor_admin') + drop login [distributor_admin]; +go + +------------------------------------------------------------------------------- +-- [Run on each host after cleanup] Final verification. +------------------------------------------------------------------------------- +set nocount on; +select @@servername as server_name, + serverproperty('IsDistributor') as is_distributor, + serverproperty('IsPublisher') as is_publisher, + serverproperty('IsSubscriber') as is_subscriber; + +select d.name, d.is_distributor, d.is_published, d.is_subscribed, d.is_merge_published +from sys.databases d +where d.is_distributor = 1 or d.is_published = 1 or d.is_subscribed = 1 or d.is_merge_published = 1; + +select count(*) as repl_jobs +from msdb.dbo.sysjobs j +join msdb.dbo.syscategories c on j.category_id = c.category_id +where c.category_class = 1 and c.name like N'REPL%'; + +select count(*) as distributor_admin_owned_jobs +from msdb.dbo.sysjobs +where suser_sname(owner_sid) = N'distributor_admin'; +go \ No newline at end of file diff --git a/Sql-Queries/Sql-Queries.ssmssqlproj b/Sql-Queries/Sql-Queries.ssmssqlproj index d018f98..7642dd5 100644 --- a/Sql-Queries/Sql-Queries.ssmssqlproj +++ b/Sql-Queries/Sql-Queries.ssmssqlproj @@ -1,5 +1,5 @@ - + @@ -104,6 +104,20 @@ NotSpecified Microsoft SQL Server Management Studio - Query + + 2026-03-17T09:28:41.0656995+05:30 + SQL + SQLMonitor + + Windows Authentication + + 30 + True + True + 0 + NotSpecified + Microsoft SQL Server Management Studio - Query + @@ -372,6 +386,12 @@ SCH-Job-[(dba) __ReSetup-PerfmonDataCollector].sql + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:SQLMonitor:True + SQLMonitor + + SCH-Remove-Replication.sql + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True (local) diff --git a/Sql-Queries/check-domain-ip-hostname.sql b/Sql-Queries/check-domain-ip-hostname.sql index 68d313c..e17f61c 100644 --- a/Sql-Queries/check-domain-ip-hostname.sql +++ b/Sql-Queries/check-domain-ip-hostname.sql @@ -87,11 +87,6 @@ from dbo.vw_performance_counters pc with (nolock) order by pc.collection_time_utc desc go -select top 1 'vw_os_task_list' as QueryData, getutcdate() as current_time_utc, collection_time_utc, pc.host_name -from dbo.vw_os_task_list pc with (nolock) -order by pc.collection_time_utc desc -go - select top 1 'dbo.xevent_metrics' as QueryDate, getdate() as [getdate()], rc.* from dbo.xevent_metrics rc order by event_time desc diff --git a/Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1 b/Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1 index f289a9c..9869ac5 100644 --- a/Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1 +++ b/Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1 @@ -29,17 +29,17 @@ $params = @{ "4__FirstResponderKitObjects", "5__DarlingDataObjects", "6__OlaHallengrenSolutionObjects", "7__sp_WhatIsRunning", "8__usp_GetAllServerInfo", "9__CopyDbaToolsModule2Host", "10__CopyPerfmonFolder2Host", "11__SetupPerfmonDataCollector", "12__CreateCredentialProxy", - "13__CreateJobCollectDiskSpace", "14__CreateJobCollectOSProcesses", "15__CreateJobCollectPerfmonData", - "16__CreateJobCollectWaitStats", "17__CreateJobCollectXEvents", "18__CreateJobCollectFileIOStats", - "19__CreateJobPartitionsMaintenance", "20__CreateJobPurgeTables", "21__CreateJobRemoveXEventFiles", - "22__CreateJobRunLogSaver", "23__CreateJobRunTempDbSaver", "24__CreateJobRunWhoIsActive", - "25__CreateJobRunBlitz", "25__CreateJobRunBlitzIndex", "27__CreateJobRunBlitzIndexWeekly", - "28__CreateJobCollectMemoryClerks", "29__CreateJobCollectPrivilegedInfo", "30__CreateJobCollectAgHealthState", - "31__CreateJobCheckSQLAgentJobs", "32__CreateJobCaptureAlertMessages", "33__CreateSQLAgentAlerts", - "34__CreateJobUpdateSqlServerVersions", "35__CreateJobCheckInstanceAvailability", "36__CreateJobGetAllServerStableInfo", - "37__CreateJobGetAllServerVolatileInfo", "38__CreateJobGetAllServerCollectionLatencyInfo", "39__CreateJobGetAllServerSqlAgentJobs", - "40__CreateJobGetAllServerDiskSpace", "41__CreateJobGetAllServerLogSpaceConsumers", "42__CreateJobGetAllServerTempdbSpaceUsage", - "43__CreateJobGetAllServerAgHealthState", "44__CreateJobGetAllServerServices", "45__CreateJobGetAllServerBackups", + "13__CreateJobCollectDiskSpace", "14__CreateJobCollectPerfmonData", "15__CreateJobCollectWaitStats", + "16__CreateJobCollectXEvents", "17__CreateJobCollectFileIOStats", "18__CreateJobPartitionsMaintenance", + "19__CreateJobPurgeTables", "20__CreateJobRemoveXEventFiles", "21__CreateJobRunLogSaver", + "22__CreateJobRunTempDbSaver", "23__CreateJobRunWhoIsActive", "24__CreateJobRunBlitz", + "24__CreateJobRunBlitzIndex", "26__CreateJobRunBlitzIndexWeekly", "27__CreateJobCollectMemoryClerks", + "28__CreateJobCollectPrivilegedInfo", "29__CreateJobCollectAgHealthState", "30__CreateJobCheckSQLAgentJobs", + "31__CreateJobCaptureAlertMessages", "32__CreateSQLAgentAlerts", "33__CreateJobUpdateSqlServerVersions", + "34__CreateJobCheckInstanceAvailability", "35__CreateJobGetAllServerStableInfo", "36__CreateJobGetAllServerVolatileInfo", + "37__CreateJobGetAllServerCollectionLatencyInfo", "38__CreateJobGetAllServerSqlAgentJobs", "39__CreateJobGetAllServerDiskSpace", + "40__CreateJobGetAllServerLogSpaceConsumers", "41__CreateJobGetAllServerTempdbSpaceUsage", "42__CreateJobGetAllServerAgHealthState", + "43__CreateJobGetAllServerServices", "44__CreateJobGetAllServerBackups", "45__CreateJobGetAllServerAlertHistory", "46__CreateJobGetAllServerDashboardMail", "47__CreateJobStopStuckSQLMonitorJobs", "48__CreateJobCollectLoginExpirationInfo", "49__CreateJobPopulateInventoryTables", "50__CreateJobSendLoginExpiryEmails", "51__CreateJobComputeAllServerVolatileInfoHistoryHourly", "52__WhoIsActivePartition", "53__BlitzIndexPartition", "54__BlitzPartition", diff --git a/docs/ai-agent.md b/docs/ai-agent.md new file mode 100644 index 0000000..878a79d --- /dev/null +++ b/docs/ai-agent.md @@ -0,0 +1,120 @@ +# AI Agent + +The `AI-Agent/` folder ships a set of **LangChain + LLM** agents that sit on top of the inventory `DBA` database. You ask natural-language questions (“which instances are blocking right now?”, “top waits on AgHost-1B last 24h”) and the agent translates them into T-SQL, runs them against the inventory, and summarises the result. + +!!! warning "Experimental" + + The AI Agent is not part of the core monitoring path. It is a thin Streamlit app that depends on an LLM you host yourself (Ollama / OpenAI). Treat it like a lab utility — do not point it at production credentials without reviewing the queries it generates. + +## What's in the folder + +| File | Purpose | +|---|---| +| `sql_db_agent_4_sqlmonitor_using_ollama.py` | Main agent — talks to the SQLMonitor inventory DB via a [LangChain `SQLDatabase`](https://python.langchain.com/docs/integrations/toolkits/sql_database) toolkit, backed by a local **Ollama** model (default `qwen2:latest`). Streamlit UI. | +| `sql_db_agent_using_ollama.py` | Generic variant you can point at any database. | +| `first_agent_using_ollama.py` / `first_agent_using_openai.py` | Hello-world agents demonstrating the provider swap. | +| `csv_agent_using_ollama.py` | CSV-first example — useful for trying the agent without a SQL instance. | +| `questions_sql_agent.md` | Curated list of questions that work well against the inventory schema — good starting set. | +| `requirements.txt` | Python deps: `langchain`, `langchain-community`, `langchain-ollama`, `streamlit`, `sqlalchemy`, `pyodbc`, `python-dotenv`. | + +## Setup + +```bash +cd ~/GitHub/SQLMonitor/AI-Agent +python -m venv venv +source venv/bin/activate # .\venv\Scripts\activate on Windows +pip install -r requirements.txt +``` + +Install the SQL Server ODBC driver: + +```bash +# Ubuntu / Debian +curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - +curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list +sudo apt-get update +sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18 +``` + +## Environment variables + +Create a `.env` in `AI-Agent/` (do **not** commit it): + +```bash +# LLM provider +OLLAMA_MODEL=qwen2:latest # any model pulled into your local Ollama +# OPENAI_API_KEY_SECRET=sk-... # if using the OpenAI variant + +# SQL Server (inventory) +SQLMONITOR_INVENTORY_SERVER=sqlmonitor.corp.example.com +SQLMONITOR_LOGIN_NAME=grafana # read-only login is enough +SQLMONITOR_LOGIN_PASSWORD=grafana +SQLMONITOR_DATABASE=DBA + +# Optional: Slack webhook for notifications +SLACK_WEBHOOK_URL_AIAGENT=https://hooks.slack.com/... +``` + +The agent picks these up automatically via `python-dotenv` on startup. + +## Run it + +```bash +streamlit run sql_db_agent_4_sqlmonitor_using_ollama.py +``` + +Then open `http://localhost:8501`. + +Sample prompts (see `questions_sql_agent.md` for more): + +- *"Which instances have less than 10% free disk space?"* +- *"Show me the top 5 waits fleet-wide in the last hour."* +- *"Which AG databases are behind on their redo queue?"* +- *"List SQL Agent jobs that failed in the last 24 hours."* +- *"Which server has had the most blocking incidents this week?"* + +## How it works + +```mermaid +flowchart LR + U[User question] --> LLM[LLM
(Ollama / OpenAI)] + LLM -->|tool call| Tk[LangChain SQLDatabase
Toolkit] + Tk -->|reads schema| Inv[(DBA inventory DB
read-only login)] + Tk -->|generates T-SQL| Inv + Inv -->|rows| Tk + Tk --> LLM + LLM --> Ans[Natural-language
answer + SQL shown] +``` + +1. The agent introspects the `DBA` schema once and feeds the table/column list to the LLM as context. +2. For each question the LLM plans one or more `SELECT` queries. +3. LangChain's SQL toolkit runs them under the **read-only** login you configured — it refuses `INSERT`/`UPDATE`/`DELETE`/`DROP` by default (see `create_sql_agent(..., agent_type="openai-tools")`). +4. Results are formatted as Markdown and streamed into the Streamlit UI. + +## Safety notes + +- **Always use a least-privilege SQL login.** The shipped example uses the same `grafana` read-only login the dashboards use. Do not give the agent a `sysadmin` account. +- **LLMs hallucinate schema.** If a question returns "no rows" but you expect data, look at the SQL the agent generated (visible in the Streamlit output) — it may have guessed a column name that doesn't exist. +- **Rate-limit on large tables.** The agent will happily run `SELECT TOP 1000 * FROM dbo.xevent_metrics` and page the whole thing into the LLM context. Configure `SQLDatabase(..., sample_rows_in_table_info=3)` or point it at a filtered view. +- **Keep prompts out of logs.** The provided Streamlit app echoes the SQL in the UI only — it does not log prompts/responses by default. If you add PagerDuty/Slack notifications, strip PII first. + +## Extending + +The agent is deliberately thin — add your own tools to it: + +```python +from langchain.tools import Tool + +custom = Tool( + name="get_blitz_findings", + func=lambda sql_instance: run_stored_proc("usp_get_blitz", sql_instance), + description="Return sp_Blitz findings for a given sql_instance" +) +agent = create_sql_agent(llm=llm, db=db, extra_tools=[custom], verbose=True) +``` + +## Further reading + +- Recipe book of working prompts: [`questions_sql_agent.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/AI-Agent/questions_sql_agent.md) +- Setup cheatsheet (Slack, Ollama reset, ODBC driver install): [`setup-ai-agent.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/AI-Agent/setup-ai-agent.md) +- LangChain SQL agent docs: diff --git a/docs/alerting.md b/docs/alerting.md new file mode 100644 index 0000000..9843096 --- /dev/null +++ b/docs/alerting.md @@ -0,0 +1,142 @@ +# Alerting + +SQLMonitor ships with a standalone **Alert Engine** written in Python (Flask + APScheduler + Slack SDK + Waitress) that reads the inventory `DBA` database and fires alerts to Slack (primary), PagerDuty and email. It is completely independent of SQL Server's built-in Database Mail / `sp_send_dbmail` path — though you can still use those alongside if you wish. + +## What the engine does + +| | | +|---|---| +| **Source of truth** | The `Alerting/` folder in the repo — specifically `SQLMonitorAlertEngineApp.py` (the server) and `SmaAlerts/Alert-*.py` (one file per alert). | +| **Scheduler** | [APScheduler](https://apscheduler.readthedocs.io/) `BackgroundScheduler` — every alert runs on its own `frequency_minutes` cadence. | +| **Web surface** | Flask app on port **5000** (served by Waitress in production) — handles Slack interactivity (acknowledge / snooze buttons), health checks and manual triggers. | +| **State** | Active alerts, history and de-duplication keys live in `DBA.dbo.sma_alerts` on the inventory server (schema created by `Alerting/Deployment-Instructions/SCH-Create-Alert-Objects.sql`). | + +## Built-in alerts + +Each file in `Alerting/SmaAlerts/` is a self-contained alert that can be executed standalone or registered with the scheduler. There are 18 shipped alerts: + +| Alert | Source | Fires when | +|---|---|---| +| `Alert-Cpu` | `all_server_volatile_info` | Host CPU % crosses configured warn/crit thresholds | +| `Alert-AvailableMemory` | `all_server_volatile_info` | Node available memory falls under threshold | +| `Alert-SqlMemory` | `all_server_volatile_info` | SQL Server target-vs-total memory discrepancy | +| `Alert-MemoryGrantsPending` | `all_server_volatile_info` | Pending memory grants > 0 for N minutes | +| `Alert-DiskSpace` | `disk_space_all_servers` | Free % or free GB below per-drive threshold | +| `Alert-DiskLatency` | perfmon | Avg Disk sec/Read or Write above threshold | +| `Alert-LogSpace` | `log_space_consumers_all_servers` | DB t-log % used > threshold | +| `Alert-Tempdb` | `tempdb_space_usage_all_servers` | Tempdb usage / version-store growth | +| `Alert-SqlBlocking` | `WhoIsActive` | Blocking chain persists > N seconds | +| `Alert-WaitsPerCorePerMinute` | `wait_stats` | Fleet-wide wait pressure | +| `Alert-AgLatency` | AG DMVs | Availability-Group redo/log-send lag | +| `Alert-AgDbBackupIssue` | `backups_all_servers` | AG database without a recent backup | +| `Alert-NonAgDbBackupIssue` | `backups_all_servers` | Standalone database without a recent backup | +| `Alert-OfflineServer` | `all_server_volatile_info` | Host has not reported in N minutes | +| `Alert-OfflineAgent` | `sql_agent_jobs_all_servers` | SQL Agent process down | +| `Alert-SqlMonitorJobs` | `sql_agent_jobs_all_servers` | A `(dba) *` job is failing repeatedly | +| `Alert-SqlMonitorDataCollection` | `all_server_collection_latency_info` | A collection is stale — dashboards will go red | +| `Alert-LoginExpiry` | `all_server_logins` | SQL Login password expiring soon | +| `Alert-NonSupportedServer` | `sma_sql_instance` | An instance below min supported SQL version joined inventory | + +Each alert is independently tunable. Parameters live in `DBA.dbo.sma_parameters` (keyed by alert name) so you never edit Python to change a threshold. + +## Deploying the Alert Engine + +### Option 1 — Podman / Docker container (recommended) + +The repo ships a `Dockerfile` that installs ODBC Driver 18, the Python dependencies from `requirements.txt`, and exposes port 5000. + +```bash +cd ~/GitHub/SQLMonitor/Alerting + +# Put the inventory SQL password in an env var (don't bake it into the image) +export MSSQLPASSWORD='' + +podman build -t sqlmonitor-alert-engine . + +podman run --replace -d --name sqlmonitor-alert-engine -p 5000:5000 \ + -e inventory_server='sqlmonitor' \ + -e login_password="$MSSQLPASSWORD" \ + sqlmonitor-alert-engine + +curl http://localhost:5000/ # should return the health page +podman logs -f sqlmonitor-alert-engine +``` + +To autostart on boot (Podman on a systemd host): + +```bash +podman generate systemd --name sqlmonitor-alert-engine --files --new +sudo mv container-sqlmonitor-alert-engine.service /etc/systemd/system/ +sudo systemctl enable --now container-sqlmonitor-alert-engine +``` + +Full annotated flow is in [`Alerting/Deployment-Instructions/alertengine-on-podman.sh`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Alerting/Deployment-Instructions/alertengine-on-podman.sh). + +### Option 2 — Python venv on Windows/Linux + +```powershell +cd E:\GitHub\SQLMonitor\Alerting +python -m venv AlertEngineVenv +AlertEngineVenv\Scripts\activate.bat +pip install -r requirements.txt +python SQLMonitorAlertEngineApp.py --inventory_server sqlmonitor --login_password '' +``` + +On Linux install the ODBC driver first: + +```bash +bash Alerting/Deployment-Instructions/install-ms-odbc-drivers-18-sqlserver.sh +pip install -r Alerting/requirements.txt +``` + +## Inventory-side objects + +Run `Alerting/Deployment-Instructions/SCH-Create-Alert-Objects.sql` on the inventory server once. It creates: + +- `dbo.sma_alerts` — the flat table of every alert instance (open & resolved) +- `dbo.sma_parameters` — per-alert thresholds, cadences, severity, owner team +- `dbo.usp_insert_sma_alert`, `usp_get_active_alert_by_key`, `usp_get_active_alert_by_state`, `usp_get_alert_by_id`, `usp_update_alert_slack_ts_value` — the CRUD surface the engine uses. + +`DML-Populate-Inventory-Tables.sql` seeds the owner-team / on-call routing table. + +## Slack & PagerDuty integration + +The engine expects a Slack bot token in the **Credential Manager** (`DBA.dbo.sma_credential`): + +| Credential name | Used for | +|---|---| +| `slack_bot_token` | `xoxb-*` token — posts messages, updates them on resolve, handles button clicks | +| `slack_signing_secret` | Verifies the incoming Slack event payloads | +| `pagerduty_integration_key` | Events API v2 key per service | +| `smtp_host`, `smtp_port`, `smtp_user`, `smtp_password` | Fallback e-mail | + +Add credentials via the helper proc: + +```sql +EXEC DBA.dbo.usp_set_sm_credential + @credential_name = N'slack_bot_token', + @credential_value = N'xoxb-...'; +``` + +The engine reads them at startup through `SmaAlertPackage/CommonFunctions/get_sm_credential.py`. + +## Operating the engine + +| Endpoint | Purpose | +|---|---| +| `GET /` | Health page listing scheduled alerts and their next run | +| `POST /slack/events` | Receives Slack events (ack / snooze / resolve clicks) | +| `POST /slack/commands` | Slash-command entrypoint | +| `GET /run-alert?name=` | Manually trigger a single alert (useful for smoke tests) | + +Logs go to `Alerting/Logs/(dba) SQLMonitorAlertEngineApp.log` by default; redirect to your log shipper if desired. + +## Extending — writing your own alert + +1. Copy any file in `Alerting/SmaAlerts/` (e.g. `Alert-Cpu.py`) and rename it `Alert-MyThing.py`. +2. Keep the same arg-parser skeleton — the engine passes all of these from config. +3. Replace the SQL query / threshold logic. Use the `SmaAlert` / `SmaCpuAlert` base classes in `SmaAlertPackage/AlertClasses/` for consistent formatting. +4. Add a row in `dbo.sma_parameters` with your alert name, cadence and thresholds. +5. Restart the engine — APScheduler will pick up the new alert on the next boot. + +See [Alert-Cpu.py](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Alerting/SmaAlerts/Alert-Cpu.py) as the canonical pattern. diff --git a/docs/architecture/components.md b/docs/architecture/components.md new file mode 100644 index 0000000..4fcec14 --- /dev/null +++ b/docs/architecture/components.md @@ -0,0 +1,60 @@ +# Components + +Map of the repository. Every top-level folder has a single purpose — this page tells you which one to open for what. + +## Top-level folder map + +| Folder | Purpose | Representative files | +|---|---|---| +| **`SQLMonitor/`** | Deployment orchestration. The install/remove wrappers and all PowerShell collectors. | `Install-SQLMonitor.ps1`, `Remove-SQLMonitor.ps1`, `perfmon-collector-*.ps1`, `disk-space-collector.ps1`, `tasklist-push-to-sqlserver.ps1`, `check-instance-availability.py`, `collect_all_server_alert_messages.py` | +| **`DDLs/`** | All SQL objects: tables, views, procs, jobs, partition schemes. | `SCH-Create-All-Objects.sql`, `SCH-Create-Inventory-Specific-Objects.sql`, 60+ `SCH-Job-*.sql`, 40+ `SCH-usp_*.sql`, `*-Partitioning.sql` | +| **`Grafana-Dashboards/`** | 17+ production-ready dashboard JSONs. | `Monitoring - Live - Distributed.json`, `WhoIsActive - SQL Server Queries - Workload.json`, `XEvent - Workload.json`, `t___*.json` | +| **`Alerting/`** | Python alert engine (Flask + APScheduler). | `AlertEngineApp.py`, `SQLMonitorAlertEngineApp.py`, `SmaAlertPackage/`, `Dockerfile`, `requirements.txt`, `Deployment-Instructions/` | +| **`sql_exporter/`** | Optional Prometheus path: `burningalchemist/sql_exporter` collector configs + windows_exporter config. | `sql_exporter.yml`, `mssql_*.collector.yml`, `windows_exporter_config.yml`, `SQL-Exporter-Metrics-*.md` | +| **`TSQLTextNormalizer/`** | C# CLR assembly that normalizes + hashes T-SQL text (used by XEvent workload). | `TSqlNormalizer.cs`, `TSqlVisitor.cs`, `SCH-Assembly-[SQLMonitorAssembly].sql` | +| **`Python-Scripts/`** | Ancillary Python tools (multi-server runners, alert raisers, IP updaters, etc.). | `Run-MultiServer-Parallel-Pool.py`, `Raise-*Alert-*.py`, `Send-SlackAlert.py`, `cloudflare-ddns-update-Python.py` | +| **`PowerShell-Scripts-Miscellaneous/`** | Standalone PowerShell utilities not called by the installer. | `Run-MultiServerScript.ps1`, `Disable-ExpiredLogins.ps1`, `Encrypt-SQLDatabases.ps1`, `Test-Port.ps1` | +| **`Credential-Manager/`** | SQL-based credential store for Python scripts. | `SCH-[dbo].[credential_manager].sql`, `SCH-[dbo].[usp_add_credential].sql`, … | +| **`Permissions/`** | Role, login, and proxy permission scripts. | … | +| **`AI-Agent/`** | Experimental LLM agent over the SQLMonitor database. | `sql_db_agent_4_sqlmonitor_using_ollama.py`, `setup-ai-agent.md`, `requirements.txt` | +| **`Partition-Table-Demo-Scripts/`** | Standalone partitioning reference / demo scripts. | `dbo.usp_partition_maintenance.sql`, `Create-Partition-Function-Scheme-n-SampleTable-SampleData.sql` | +| **`Sql-Queries/`** | Ad-hoc diagnostic queries for DBAs. | `QRY-WhoIsActive-Infra.sql`, `QRY-XEventMetrics-*.sql`, `QRY-sp_BlitzCache-Specific.sql`, `QRY-Forecast-Database-Growth.sql` | +| **`Wrapper-Samples/`** | Sample wrapper scripts for multi-server install/update/remove runs. Copy to `Private/` and edit. | `Wrapper-InstallSQLMonitor.ps1`, `Wrapper-RemoveSQLMonitor.ps1`, `Wrapper-All-Servers-Update-SQLMonitor-Objects.ps1` | +| **`XEvent/`** | XEvent session definition + extraction queries (reference). | `01) XEvent Definition.sql`, `02) XEvent Extraction.sql` | +| **`replication-latency-infra/`** | Add-on for tracking replication / readable-secondary latency. | `replication-latency-infra.ssmssqlproj` | +| **`NoteBooks/`** | Azure Data Studio / Jupyter notebooks. | … | + +## Key scripts you'll actually touch + +### `SQLMonitor/Install-SQLMonitor.ps1` + +The single entry point for onboarding an instance. Accepts 40+ parameters, supports **59 named install steps** (each skippable via `SkipSteps`/`OnlySteps`/`StartAtStep`/`StopAtStep`). Full parameter reference in [Deployment → Parameters](../deployment/parameters.md); step list in [Install Steps](../deployment/steps.md). + +### `SQLMonitor/Remove-SQLMonitor.ps1` + +Reverses the install. Supports **145+ named removal steps** (jobs → procs → views → tables → proxies → inventory entry), each controllable via `SkipSteps`/`OnlySteps`/`StartAtStep`/`StopAtStep`. + +### `Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1` + +**Do not edit this directly.** Copy it to `Private/Wrapper-InstallSQLMonitor.ps1`, fill in your environment, and run from there. `Private/` is in `.gitignore`. + +### `Alerting/SQLMonitorAlertEngineApp.py` + +The alert engine daemon. Runs as a Flask app + APScheduler. Dockerfile included; deploy via `Alerting/Deployment-Instructions/alertengine-on-podman.sh` or as a Windows service. + +## Database objects: naming conventions + +- **Tables** — `dbo.snake_case` for collection tables (`dbo.wait_stats`, `dbo.file_io_stats`). `dbo.all_server_*` for inventory aggregates. +- **Procs** — `dbo.usp_collect_*`, `dbo.usp_get_*`, `dbo.usp_compute_*`, `dbo.usp_run_*`, `dbo.usp_purge_*`. +- **Jobs** — `(dba) -`, category `(dba) SQLMonitor`. See the full [Jobs & Collectors catalog](../jobs.md). +- **Proxy / credential** — whatever you supply to `Install-SQLMonitor.ps1`; a SQL Agent proxy is created for PowerShell & CmdExec subsystems. +- **Grafana login** — `grafana` with `db_datareader` on `DBA` (created by step `40__GrafanaLogin`). + +## CLR assembly + +`TSQLTextNormalizer.dll` exposes T-SQL functions used by: + +- `dbo.usp_collect_xevent_metrics_hashed` — normalizes `sql_text` and hashes it to `query_hash`, so workload views can "group by logical query" instead of "group by parameter literal". +- `dbo.fn_get_hash_for_string` — scalar helper used in multiple procs. + +The assembly is signed with `CLR_FETCH_QUERY.snk` and registered via `TSQLTextNormalizer/SCH-Assembly-[SQLMonitorAssembly].sql` during install step `2__AllDatabaseObjects`. diff --git a/docs/architecture/data-flow.md b/docs/architecture/data-flow.md new file mode 100644 index 0000000..ecc9db7 --- /dev/null +++ b/docs/architecture/data-flow.md @@ -0,0 +1,105 @@ +# Data Flow + +How one metric travels from "event happened on the host" to "you see it in Grafana and get paged". + +## The canonical flow (Perfmon CPU example) + +``` mermaid +sequenceDiagram + autonumber + participant OS as Windows Perfmon (BLG) + participant HOST as Monitored host disk + participant JOB as (dba) Collect-PerfmonData + participant T as dbo.performance_counters + participant INVJ as (dba) Get-AllServerVolatileInfo + participant INV as Inventory.DBA.dbo.all_server_volatile_info + participant G as Grafana + participant A as Alert Engine + + OS->>HOST: Writes BLG every 10s via logman + JOB->>HOST: Every 2 min: consume BLG → XML → bulk insert + JOB->>T: INSERT rows (event_time, counter, value) + INVJ->>T: SELECT delta over linked server + INVJ->>INV: UPSERT aggregates (per server) + G->>INV: Dashboard panel query + G-->>-G: Render + A->>INV: usp_get_active_alert_by_* procs + A-->>A: Threshold check + A-->>A: Route to PagerDuty / Slack / Email +``` + +## Pattern: three kinds of collection + +SQLMonitor uses three distinct collection patterns depending on what is being measured. + +### 1. Host-side file ingest (Perfmon, OS processes, Tasklist) + +Files on disk → PowerShell reader → bulk insert. + +| Job | Script | Destination table | Notes | +|---|---|---|---| +| `(dba) Collect-PerfmonData` | `SQLMonitor/perfmon-collector-push-to-sqlserver.ps1` | `dbo.performance_counters` | Reads `.blg` from `logman` data collector | +| `(dba) Collect-OSProcesses` | `SQLMonitor/perfmon-collector-push-to-sqlserver.ps1` (tasklist branch) | `dbo.os_task_list` | CPU / memory per process | +| `(dba) Collect-DiskSpace` | `SQLMonitor/disk-space-collector.ps1` | `dbo.disk_space` | Uses `Get-Volume` on the host | + +### 2. In-engine T-SQL capture (WaitStats, FileIO, MemoryClerks, XEvents, BlitzFirst, …) + +Stored procedure → table. Delta or snapshot depending on metric. + +| Job | Procedure | Destination table | Capture style | +|---|---|---|---| +| `(dba) Collect-WaitStats` | `dbo.usp_collect_wait_stats` | `dbo.wait_stats` | Cumulative snapshot (compute delta at read-time) | +| `(dba) Collect-FileIOStats` | `dbo.usp_collect_file_io_stats` | `dbo.file_io_stats` | Cumulative snapshot | +| `(dba) Collect-MemoryClerks` | `dbo.usp_collect_memory_clerks` | `dbo.memory_clerks` | Point-in-time | +| `(dba) Collect-XEvents` | `dbo.usp_collect_xevent_metrics` | `dbo.xevent_metrics` | Consumes XE files, inserts normalized+hashed rows | +| `(dba) Collect-AgHealthState` | `dbo.usp_collect_ag_health_state` | `dbo.ag_health_state` | Point-in-time | +| `(dba) Run-WhoIsActive` | `dbo.usp_run_WhoIsActive` | `dbo.WhoIsActive` | Append per invocation | +| `(dba) Run-Blitz` / `Run-BlitzIndex` | `dbo.sp_Blitz` / `dbo.sp_BlitzIndex` | `dbo.blitz*` tables | Append weekly/daily | +| `(dba) Run-LogSaver` | `dbo.usp_LogSaver` | `dbo.log_space_consumers` | Finds txn-log fillers; can kill | +| `(dba) Run-TempDbSaver` | `dbo.usp_TempDbSaver` | `dbo.tempdb_space_usage` | Finds tempdb consumers; can kill | + +### 3. Inventory aggregation (Get-AllServer*) + +Per-instance tables → linked-server pull → `all_server_*` table on inventory. + +| Job | Reads | Writes | +|---|---|---| +| `(dba) Get-AllServerVolatileInfo` | Remote `DBA` DB on each server via linked server | `dbo.all_server_volatile_info`, `dbo.all_server_volatile_info_history` (Memory-Optimized) | +| `(dba) Get-AllServerStableInfo` | Remote `DBA` DB | `dbo.all_server_stable_info`, `dbo.all_server_stable_info_history` | +| `(dba) Get-AllServerBackups` | `msdb.dbo.backupset` on each server | `dbo.backups_all_servers` | +| `(dba) Get-AllServerSqlAgentJobs` | `msdb` on each server | `dbo.sql_agent_jobs_all_servers` | +| `(dba) Get-AllServerAgHealthState` | `dbo.ag_health_state` on each server | `dbo.ag_health_state_all_servers` | +| `(dba) Get-AllServerDiskSpace` | `dbo.disk_space` on each server | `dbo.disk_space_all_servers` | +| `(dba) Get-AllServerLogSpaceConsumers` | `dbo.log_space_consumers` on each server | `dbo.log_space_consumers_all_servers` | +| `(dba) Get-AllServerTempdbSpaceUsage` | `dbo.tempdb_space_usage` on each server | `dbo.tempdb_space_usage_all_servers` | +| `(dba) Get-AllServerServices` | `sys.dm_server_services` on each server | `dbo.services_all_servers` | +| `(dba) Get-AllServerCollectionLatencyInfo` | Metadata on each server | `dbo.all_server_collection_latency_info` | + +Aggregation is orchestrated by `dbo.usp_GetAllServerInfo` and its variants. + +## Lifecycle: partition → compress → purge + +``` mermaid +flowchart LR + INS[Collector INSERT] --> P1[Hourly partition
created by usp_partition_maintenance] + P1 --> CMP[Page compression
via usp_enable_page_compression] + CMP --> OLD[Old partition
exceeds retention] + OLD --> PURGE[usp_purge_tables
truncates partition] +``` + +Retention is declarative: `dbo.purge_table` holds one row per table with its retention days. The `(dba) Partitions-Maintenance` and `(dba) Purge-Tables` jobs do the rest. + +## Alert path + +``` mermaid +flowchart LR + T[(Collection tables)] --> USP[usp_get_active_alert_by_key
usp_get_active_alert_by_state] + USP --> ENG[Alert Engine - Python Flask] + ENG -->|new| SLACK[Slack] + ENG -->|new| PD[PagerDuty] + ENG -->|new| MAIL[DatabaseMail] + ENG -->|update| SLACK + ENG -->|ack/resolve| PD +``` + +Alert state is persisted in `dbo.sma_alerts` so re-raises, updates and resolutions are idempotent across restarts. See **[Alerting](../alerting.md)** for the full engine description. diff --git a/docs/architecture/data-model.md b/docs/architecture/data-model.md new file mode 100644 index 0000000..22423dc --- /dev/null +++ b/docs/architecture/data-model.md @@ -0,0 +1,95 @@ +# Data Model + +SQLMonitor stores everything in two databases: + +- **`DBA` on each monitored instance** — the "collection" tables populated by local SQL Agent jobs. +- **`DBA` on the inventory / observability server** — the `all_server_*` aggregate tables populated by `(dba) Get-AllServer*` jobs. + +## Collection tables (per monitored instance) + +Created by `DDLs/SCH-Create-All-Objects.sql`. Hourly-partitioned where the edition supports it, otherwise heap or clustered index. + +| Table | Populated by | Key columns | Retention default | +|---|---|---|---| +| `dbo.performance_counters` | `(dba) Collect-PerfmonData` | `collection_time`, `object_name`, `counter_name`, `instance_name`, `cntr_value` | Driven by `dbo.purge_table` | +| `dbo.wait_stats` | `(dba) Collect-WaitStats` | `collection_time`, `wait_type`, `wait_time_ms`, `signal_wait_time_ms`, `waiting_tasks_count` | … | +| `dbo.file_io_stats` | `(dba) Collect-FileIOStats` | `collection_time`, `database_id`, `file_id`, `num_of_reads`, `io_stall_read_ms`, `num_of_writes`, `io_stall_write_ms` | … | +| `dbo.memory_clerks` | `(dba) Collect-MemoryClerks` | `collection_time`, `type`, `pages_kb`, `virtual_memory_reserved_kb` | … | +| `dbo.ag_health_state` | `(dba) Collect-AgHealthState` | `collection_time`, `ag_name`, `replica_server_name`, `role`, `synchronization_state`, `is_local` | … | +| `dbo.disk_space` | `(dba) Collect-DiskSpace` | `collection_time`, `drive_letter`, `total_size_mb`, `free_space_mb` | … | +| `dbo.os_task_list` | `(dba) Collect-OSProcesses` | `collection_time`, `image_name`, `pid`, `cpu_percent`, `memory_mb` | … | +| `dbo.xevent_metrics` | `(dba) Collect-XEvents` | `event_time`, `event_name`, `session_id`, `database_id`, `cpu_time`, `logical_reads`, `duration`, `query_hash`, `sql_text_normalized` | **Heavy**; tight retention | +| `dbo.xevent_metrics_hashed` | `dbo.usp_collect_xevent_metrics_hashed` | `event_time_start`, `query_hash`, `execution_count`, aggregated times/reads/writes | Longer retention than raw | +| `dbo.WhoIsActive` | `(dba) Run-WhoIsActive` | `collection_time`, `session_id`, `login_name`, `wait_info`, `sql_text`, `blocking_session_id`, `reads`, `writes` | 7-14 days typical | +| `dbo.WhoIsActive_Staging` | `sp_WhoIsActive` into → merged into `WhoIsActive` | transient | +| `dbo.blitz` / `dbo.blitz_index` / `dbo.blitz_index_mode*` | `(dba) Run-Blitz*` | `CheckDate`, `CheckID`, `DatabaseName`, `Details` | Long retention (weekly run) | +| `dbo.log_space_consumers` | `(dba) Run-LogSaver` | `collection_time`, `session_id`, `log_reuse_wait_desc`, `log_used_gb` | Short | +| `dbo.tempdb_space_usage` | `(dba) Run-TempDbSaver` | `collection_time`, `session_id`, `space_used_mb` | Short | +| `dbo.alert_history` | `(dba) Capture-AlertMessages` (triggered by SQL Agent alerts) | `collection_time`, `error_num`, `error_severity`, `error_message` | Long | + +## Inventory tables (on the inventory server only) + +Created by `DDLs/SCH-Create-Inventory-Specific-Objects.sql`. Core stability metrics use **Memory-Optimized** tables for write throughput. + +| Table | Populated by | Shape | +|---|---|---| +| `dbo.sma_sql_instance` | `(dba) Populate Inventory Tables` + `dbo.usp_populate_sma_sql_instance` | Master list of instances in scope | +| `dbo.all_server_volatile_info` | `(dba) Get-AllServerVolatileInfo` | Current snapshot per server (CPU %, memory %, active requests, blocking, …) | +| `dbo.all_server_volatile_info_history` | same | Memory-Optimized; short history | +| `dbo.all_server_volatile_info_history_hourly` | `(dba) Compute-AllServerVolatileInfoHistoryHourly` | Hourly roll-up, long retention | +| `dbo.all_server_stable_info` | `(dba) Get-AllServerStableInfo` | Config-ish facts (edition, CU, cores, max-memory, …) | +| `dbo.all_server_stable_info_history` | same | Change history | +| `dbo.all_server_collection_latency_info` | `(dba) Get-AllServerCollectionLatencyInfo` | Per-collector freshness (is collection actually happening?) | +| `dbo.backups_all_servers` | `(dba) Get-AllServerBackups` | DB backup history normalized across fleet | +| `dbo.sql_agent_jobs_all_servers` | `(dba) Get-AllServerSqlAgentJobs` | Job inventory + last outcomes | +| `dbo.ag_health_state_all_servers` | `(dba) Get-AllServerAgHealthState` | Availability group state across fleet | +| `dbo.disk_space_all_servers` | `(dba) Get-AllServerDiskSpace` | Free/used per drive per server | +| `dbo.services_all_servers` | `(dba) Get-AllServerServices` | SQL services running/stopped per host | +| `dbo.tempdb_space_usage_all_servers` | `(dba) Get-AllServerTempdbSpaceUsage` | … | +| `dbo.log_space_consumers_all_servers` | `(dba) Get-AllServerLogSpaceConsumers` | … | +| `dbo.alert_history_all_servers` | `(dba) Get-AllServerAlertHistory` | Fleet-wide alert messages | + +## Partition scheme + +Large collection tables use hourly partitioning via `DDLs/SCH-*-Partitioning.sql` and the daily `(dba) Partitions-Maintenance` job (`dbo.usp_partition_maintenance`). + +```text +PARTITION FUNCTION pf_hourly (datetime2) +AS RANGE RIGHT FOR VALUES ( + '2024-12-01 00:00', + '2024-12-01 01:00', + '2024-12-01 02:00', + ... +); +PARTITION SCHEME ps_hourly AS PARTITION pf_hourly ALL TO ([PRIMARY]); +``` + +The maintenance proc: + +1. Adds forward partitions so inserts never hit the unbounded tail. +2. Merges / truncates old partitions per `dbo.purge_table.retention_days`. +3. Rebuilds page compression on recently-sealed partitions via `dbo.usp_enable_page_compression`. + +## Purge control + +Every purgeable table has a row in `dbo.purge_table`: + +| Column | Meaning | +|---|---| +| `schema_name`, `table_name` | target | +| `retention_days` | how long to keep | +| `date_key_column` | column used for purging | +| `is_partitioned` | use SWITCH/TRUNCATE PARTITION vs. DELETE | +| `active` | toggle purging on/off without a code change | + +`(dba) Purge-Tables` (daily) — runs `dbo.usp_purge_tables`, which iterates this table. + +## Views (for Grafana consumption) + +The installer creates `all_server_*` **views** in the local `DBA` database of every monitored instance *when* `SqlInstanceAsDataDestination` points at a different SQL instance — these views transparently union-from-linked-server, letting dashboards query "locally" regardless of physical location. See install step `43__AlterViewsForDataDestinationInstance`. + +## Further reading + +- [Jobs & Collectors catalog](../jobs.md) — every job, its schedule, and which table it writes. +- [Install Steps](../deployment/steps.md) — the exact order in which objects are created. +- `DDLs/SCH-Create-All-Objects.sql` — the authoritative schema script. diff --git a/docs/architecture/index.md b/docs/architecture/index.md new file mode 100644 index 0000000..035cd80 --- /dev/null +++ b/docs/architecture/index.md @@ -0,0 +1,68 @@ +# Architecture + +SQLMonitor's architecture has three logical tiers: + +1. **Collection tier** — SQL Agent jobs (plus PowerShell and Python scripts) capture metrics from the monitored instance and its host OS. +2. **Storage tier** — collected metrics land in hourly-partitioned, page-compressed tables inside the `DBA` database. Core stability metrics use Memory-Optimized tables on the inventory server. +3. **Consumption tier** — Grafana dashboards, the Python alert engine, optional Prometheus (`sql_exporter`), and an optional AI agent all read from those tables. + +``` mermaid +flowchart LR + subgraph HOST["Monitored Windows host"] + PERF[Perfmon BLG files] + XE[XEvent .xel files] + OS[OS processes / Tasklist] + end + subgraph INST["Monitored SQL instance (DBA database)"] + AGENT[SQL Agent jobs] + PROCS[Stored procedures
usp_collect_*] + TABLES[(Collection tables
hourly-partitioned)] + end + subgraph INV["Inventory server (DBA database)"] + AGG[Aggregation jobs
Get-AllServer*] + ALL[(all_server_*
tables, Memory-Optimized)] + end + subgraph OBS["Observability plane"] + GRAF[Grafana] + PROM[Prometheus + sql_exporter] + ALERT[Python Alert Engine] + AI[AI Agent - Ollama] + end + PAGER[PagerDuty / Slack / Email] + + PERF & XE & OS --> AGENT + AGENT --> PROCS --> TABLES + TABLES -- linked server --> AGG + AGG --> ALL + ALL --> GRAF + TABLES --> GRAF + TABLES --> PROM + ALL --> ALERT --> PAGER + ALL --> AI +``` + +## Reading guide + +- **[Topology](topology.md)** — the two deployment shapes (Distributed and Central) and where each SQL Agent job runs in each. +- **[Data Flow](data-flow.md)** — how a single metric (CPU %, wait stats, XEvent workload, …) flows from the OS to your screen, including purge and partition lifecycle. +- **[Components](components.md)** — a map of every folder in the repo and what it does. +- **[Data Model](data-model.md)** — the table layout (per-instance collection tables vs. `all_server_*` inventory tables), partitioning and purging. + +## The three tiers at a glance + +| Tier | Lives on | Files in repo | Runs as | +|---|---|---|---| +| Collection | Each monitored instance *or* a dedicated collector instance | `DDLs/SCH-Job-*.sql`, `SQLMonitor/*.ps1`, `DDLs/SCH-usp_collect_*.sql` | SQL Agent jobs (T-SQL, PowerShell proxy, or CmdExec calling `python`) | +| Storage | Monitored instance (`DBA` DB) + inventory instance (`DBA` DB) | `DDLs/SCH-Create-All-Objects.sql`, `DDLs/SCH-Create-Inventory-Specific-Objects.sql`, `DDLs/SCH-*-Partitioning.sql` | Tables & views, automatically partitioned hourly, page-compressed | +| Consumption | Grafana / Prometheus / alert host | `Grafana-Dashboards/*.json`, `sql_exporter/*.yml`, `Alerting/*.py` | Grafana data source, Prometheus job, Flask+APScheduler service | + +## Why three tiers matter + +- **Failure isolation.** The Grafana host can be recycled or re-imaged without any metric loss — data lives in SQL. +- **Multiple readers.** Grafana, the alert engine, the AI agent, a BI tool, or your own ad-hoc T-SQL can all query the same truth. +- **Performance.** Collection writes are light; reads are amortized across many consumers. + +## Where to go next + +- If you are installing for the first time → start with **[Deployment → Prerequisites](../deployment/prerequisites.md)**. +- If you are evaluating → continue with **[Topology](topology.md)**. diff --git a/docs/architecture/topology.md b/docs/architecture/topology.md new file mode 100644 index 0000000..284c0b3 --- /dev/null +++ b/docs/architecture/topology.md @@ -0,0 +1,106 @@ +# Topology + +SQLMonitor supports two deployment shapes. Both ship with the exact same scripts, stored procedures and dashboards — only the *location* of the SQL Agent jobs changes. + +## Distributed topology (recommended default) + +Every SQL Server instance collects its own metrics. A dedicated **Inventory server** holds the aggregation jobs and the Grafana data source. + +``` mermaid +flowchart LR + subgraph Inst1[SQL Server A] + A_AGENT[SQL Agent] + A_DB[(DBA)] + end + subgraph Inst2[SQL Server B] + B_AGENT[SQL Agent] + B_DB[(DBA)] + end + subgraph Inst3[SQL Server C] + C_AGENT[SQL Agent] + C_DB[(DBA)] + end + subgraph INV[Inventory Server] + INV_AGENT[SQL Agent
Get-AllServer* jobs] + INV_DB[(DBA
all_server_* tables)] + end + GRAF[Grafana] + ALRT[Alert Engine] + + A_AGENT --> A_DB + B_AGENT --> B_DB + C_AGENT --> C_DB + A_DB <-. linked server .-> INV_DB + B_DB <-. linked server .-> INV_DB + C_DB <-. linked server .-> INV_DB + INV_AGENT --> INV_DB + INV_DB --> GRAF + INV_DB --> ALRT +``` + +![SQLMonitor Distributed topology](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/SQLMonitor-Distributed-Topology.png?raw=true) + +**Who runs what:** + +| Role | Jobs it runs | Typical instance | +|---|---|---| +| **Monitored instance** | All `(dba) Collect-*`, `(dba) Run-*`, `(dba) Check-SQLAgentJobs`, `(dba) Partitions-Maintenance`, `(dba) Purge-Tables`, `(dba) Remove-XEventFiles` | Every SQL instance in scope | +| **Inventory server** | All `(dba) Get-AllServer*`, `(dba) Check-InstanceAvailability`, `(dba) Update-SqlServerVersions`, `(dba) Populate Inventory Tables`, `(dba) Stop-StuckSQLMonitorJobs`, `(dba) Get-AllServerDashboardMail` | One dedicated instance | + +**When to choose it:** medium-to-large fleets where you want the collection load *on* each instance, each instance has a reliable SQL Agent, and you can accept a linked-server hop for aggregation. + +## Central topology + +One central observability SQL Server runs **all** the jobs and polls every monitored instance via linked servers or direct ADO.NET connections. + +``` mermaid +flowchart LR + subgraph Inst1[SQL Server A] + A_DB[(DBA)] + end + subgraph Inst2[SQL Server B] + B_DB[(DBA)] + end + subgraph Inst3[SQL Server C] + C_DB[(DBA)] + end + subgraph CEN[Central Inventory & Collector] + CEN_AGENT[SQL Agent
ALL jobs] + CEN_DB[(DBA
per-server + all_server_* tables)] + end + GRAF[Grafana] + ALRT[Alert Engine] + + A_DB <--> CEN_AGENT + B_DB <--> CEN_AGENT + C_DB <--> CEN_AGENT + CEN_AGENT --> CEN_DB + CEN_DB --> GRAF + CEN_DB --> ALRT +``` + +**Who runs what:** every SQLMonitor job runs on the central instance. Monitored instances only need: + +- Objects in `DBA` database (views + procs for the collector to call). +- The `grafana` login with `db_datareader` on `DBA`. +- Network access from the central instance. + +**When to choose it:** small fleets, or when you cannot install SQL Agent jobs on every instance (Managed Instance, RDS with limited Agent, locked-down production). + +## Hybrid variants + +The `Install-SQLMonitor.ps1` parameters `SqlInstanceForTsqlJobs` and `SqlInstanceForPowershellJobs` let you route *specific job types* to a different server than the monitored instance: + +- **T-SQL Jobs Server** — handles all T-SQL collection. Can be the monitored instance, another instance in the same cluster, or the inventory server. +- **PowerShell Jobs Server** — handles Perfmon, OS-process and disk-space collection (which require PowerShell and SMB access to the host). Same placement options. + +This lets you, for example, keep heavyweight Perfmon collection off the monitored instance while still using SQL-Agent-native orchestration. + +## Choosing a topology + +Use **Distributed** unless one of these applies, in which case use **Central**: + +- :material-cloud: Monitored instance is Azure SQL Managed Instance / RDS with restricted Agent. +- :material-lock: Security baseline forbids adding monitoring objects on PROD. +- :material-numeric-1-circle-outline: You have a very small fleet (1–3 servers). +- :material-server-off: Monitored instance is an older edition where partitioning/Memory-Optimized isn't attractive on the host. diff --git a/docs/assets/favicon.svg b/docs/assets/favicon.svg new file mode 100644 index 0000000..11c8b18 --- /dev/null +++ b/docs/assets/favicon.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/docs/assets/logo.svg b/docs/assets/logo.svg new file mode 100644 index 0000000..aef4ea8 --- /dev/null +++ b/docs/assets/logo.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/docs/dashboards/index.md b/docs/dashboards/index.md new file mode 100644 index 0000000..6804398 --- /dev/null +++ b/docs/dashboards/index.md @@ -0,0 +1,78 @@ +# Dashboards + +SQLMonitor ships **17 production-ready Grafana dashboards** (plus a handful of community add-ons). Every JSON under [`Grafana-Dashboards/`](https://github.com/imajaydwivedi/SQLMonitor/tree/dev/Grafana-Dashboards) is ready to import. + +All dashboards use a single data source named exactly **`SQLMonitor`** of type **Microsoft SQL Server**, pointing at the inventory server's `DBA` database. Create that data source once, then import the JSONs. + +[:material-rocket-launch: Try the live demo](live-demo.md){ .md-button .md-button--primary } +[:material-book-cog-outline: Variables & conventions](variables.md){ .md-button } + +## At a glance + +### Live / operations + +| Dashboard | File | Live URL | What it shows | +|---|---|---|---| +| **Monitoring — Live — Distributed** | [`Monitoring - Live - Distributed.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/Monitoring%20-%20Live%20-%20Distributed.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/distributed_live_dashboard/monitoring-live-distributed) | Core live metrics for one instance: CPU%, memory %, waits per core / minute, active requests, blocking, transactions/sec, IO latency, disk space. | +| **Monitoring — Live — All Servers** | [`Monitoring - Live - All Servers.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/Monitoring%20-%20Live%20-%20All%20Servers.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/distributed_live_dashboard_all_servers/monitoring-live-all-servers) | Fleet-wide heatmap of core health metrics — single pane for an entire environment. | +| **Monitoring — Live — Job Activity Monitor** | [`Monitoring - Live - All Servers - Job Activity Monitor.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/Monitoring%20-%20Live%20-%20All%20Servers%20-%20Job%20Activity%20Monitor.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/job_activity_monitor/monitoring-live-all-servers-job-activity-monitor) | Currently-running & recently-failed SQL Agent jobs across the fleet. | +| **Monitoring — Perfmon Counters — Quest Softwares — Distributed** | [`Monitoring - Perfmon Counters - Quest Softwares - Distributed.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/Monitoring%20-%20Perfmon%20Counters%20-%20Quest%20Softwares%20-%20Distributed.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/distributed_perfmon/monitoring-perfmon-counters-quest-softwares-distributed) | Every Windows Perfmon counter Quest Software recommends for SQL baselining. | +| **Core Metrics — Trend** | [`Core Metrics - Trend.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/Core%20Metrics%20-%20Trend.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/core_metrics_trend/core-metrics-trend) | Long-range trends for CPU, memory, waits, IO. Same metric set as Live-Distributed but over days/weeks. | + +### Workload + +| Dashboard | File | Live URL | What it shows | +|---|---|---|---| +| **WhoIsActive — SQL Server Queries — Workload** | [`WhoIsActive - SQL Server Queries - Workload.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/WhoIsActive%20-%20SQL%20Server%20Queries%20-%20Workload.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/WhoIsActive/whoisactive-sql-server-queries-workload) | Persisted `sp_WhoIsActive` output: live + historical. Top blockers, long-runners, top CPU / reads / writes. | +| **XEvent — Workload** | [`XEvent - Workload.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/XEvent%20-%20Workload.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/XEvents/xevent-workload) | Normalized / hashed query workload captured by the `XEventMetrics` session — per-query CPU, reads, duration, exec count. | +| **XEvent — Trend** | [`XEvent - Trend.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/XEvent%20-%20Trend.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/XEvents-Trends/xevent-trend) | Trend of total CPU / reads / duration across the workload; click-through to per-query. | +| **Wait Stats** | [`Wait Stats.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/Wait%20Stats.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/wait_stats/wait-stats) | Delta-computed wait statistics over time, grouped by category. | + +### Diagnostics + +| Dashboard | File | Live URL | What it shows | +|---|---|---|---| +| **t___Blitz_Server_Health_Analysis** | [`t___Blitz_Server_Health_Analysis.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/t___Blitz_Server_Health_Analysis.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/t___Blitz_Server_Health_Analysis/t-blitz-server-health-analysis) | `sp_Blitz` findings browsable by priority, category, database. | +| **t___BlitzIndex_Analysis** | [`t___BlitzIndex_Analysis.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/t___BlitzIndex_Analysis.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/t___BlitzIndex_Analysis/t-blitzindex-analysis) | Duplicate indexes, unused indexes, heaps, missing-index recommendations. | + +### Infrastructure + +| Dashboard | File | Live URL | What it shows | +|---|---|---|---| +| **t___Ag Health State** | [`t___Ag Health State.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/t___Ag%20Health%20State.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/ag_health_state/t-ag-health-state) | Availability Group replica sync state, failover history, send/redo queue sizes. | +| **t___Backup_History** | [`t___Backup_History.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/t___Backup_History.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/backup_history/t-backup-history) | Fleet-wide backup calendar — FULL/DIFF/LOG freshness, size, duration. | +| **t___Disk Space** | [`t___Disk Space.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/t___Disk%20Space.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/disk_space/t-disk-space) | Per-host, per-drive free-space trend with forecasting. | +| **t___Database File IO Stats** | [`t___Database File IO Stats.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/t___Database%20File%20IO%20Stats.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/database_file_io_stats/t-database-file-io-stats) | Per-database-file read/write latency, IOPS, MB/s — delta over `sys.dm_io_virtual_file_stats`. | +| **t__DBA_Inventory (DBA Inventory)** | [`DBA Inventory.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/DBA%20Inventory.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/t__DBA_Inventory/dba-inventory) | One row per monitored instance: edition, CU, cores, max memory, last-seen, owner, login expiry. | + +### Alerts + +| Dashboard | File | Live URL | What it shows | +|---|---|---|---| +| **SQLMonitor-Alerts** | [`SQLMonitor-Alerts.json`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/SQLMonitor-Alerts.json) | [:material-link: open](https://sqlmonitor.ajaydwivedi.com/d/alerts/sqlmonitor-alerts) | Live alert stream from the [Alert Engine](../alerting.md) with links to PagerDuty / Slack / Email evidence. | + +## Importing + +=== "Grafana UI" + + 1. Grafana → Dashboards → **Import**. + 2. Upload the JSON from `Grafana-Dashboards/`. + 3. At the **SQLMonitor** data source dropdown, select the data source you created. + 4. (Recommended) Set the **Folder** to `SQLMonitor` to keep them grouped. + +=== "Grafana API" + + ```bash + for f in Grafana-Dashboards/*.json; do + curl -sS -u "$GRAF_USER:$GRAF_PASS" \ + -H "Content-Type: application/json" \ + -X POST "$GRAF_URL/api/dashboards/db" \ + --data @<(jq '{dashboard: ., overwrite: true, folderUid: "SQLMonitor"}' "$f") + done + ``` + +=== "Terraform" + + Use [`grafana_dashboard`](https://registry.terraform.io/providers/grafana/grafana/latest/docs/resources/dashboard) with `config_json = file("...")`. + +See **[Variables & Conventions](variables.md)** for the dashboard variable contract, and **[Live Demo](live-demo.md)** for how the live instance is kept up to date. diff --git a/docs/dashboards/live-demo.md b/docs/dashboards/live-demo.md new file mode 100644 index 0000000..d546008 --- /dev/null +++ b/docs/dashboards/live-demo.md @@ -0,0 +1,82 @@ +# Live Demo + +A fully-provisioned SQLMonitor instance is kept publicly available so you can click around before deploying. + +!!! info "Credentials" + + | Portal | URL | Username | Password | + |---|---|---|---| + | Grafana | | `guest` | `ajaydwivedi-guest` | + | SQL instance | `sqlmonitor.ajaydwivedi.com:1433` | `grafana` | `grafana` | + + The SQL endpoint is read-only (grafana login) and exposes the inventory `DBA` database — point your SSMS at it if you want to poke at the raw tables. + +## Dashboards on the live instance + +| Dashboard | Link | +|---|---| +| Monitoring — Live — Distributed | | +| Monitoring — Live — All Servers | | +| Monitoring — Live — Job Activity Monitor | | +| Monitoring — Perfmon Counters — Quest Software | | +| Core Metrics — Trend | | +| WhoIsActive — Workload | | +| XEvent — Workload | | +| XEvent — Trend | | +| Wait Stats | | +| Blitz Server Health Analysis | | +| BlitzIndex Analysis | | +| AG Health State | | +| Backup History | | +| Disk Space | | +| Database File IO Stats | | +| DBA Inventory | | +| SQLMonitor-Alerts | | + +## What's in the demo environment + +The live demo monitors a small lab fleet (inventory server + a couple of Availability-Group hosts). That means you get real data for: + +- AG health state and failover (the hosts rotate primary occasionally on purpose). +- A modest but continuous workload from a test harness. +- Real Perfmon data, real WhoIsActive output, real Blitz findings. +- The **Alert Engine** firing real (demo) alerts into a sandboxed Slack & PagerDuty. + +## Looking at the tables directly + +Point SSMS (or Azure Data Studio, or DBeaver, or `sqlcmd`) at: + +```text +Server: sqlmonitor.ajaydwivedi.com,1433 +Auth: SQL Server Authentication +User: grafana +Pass: grafana +DB: DBA +``` + +Some queries to try: + +```sql +-- Fleet snapshot +SELECT sql_instance, cpu_percent, active_requests, blocked_requests, collection_time_utc +FROM dbo.all_server_volatile_info +ORDER BY sql_instance; + +-- Top waits in the last hour, fleet-wide +SELECT TOP 20 sql_instance, wait_type, SUM(wait_time_ms_delta)/1000.0 AS wait_sec +FROM DBA.dbo.vw_wait_stats_deltas +WHERE collection_time > DATEADD(HOUR, -1, GETDATE()) +GROUP BY sql_instance, wait_type +ORDER BY wait_sec DESC; + +-- Newest alert messages +SELECT TOP 20 sql_instance, error_severity, collection_time, error_message +FROM DBA.dbo.alert_history_all_servers +ORDER BY collection_time DESC; +``` + +## What this demo does **not** demonstrate + +- **Multi-hundred-server scale.** The demo fleet is intentionally small. For scale guidance see [Architecture](../architecture/index.md). +- **Production alert routing.** Slack and PagerDuty are sandboxed — they demonstrate the *flow* but not a real oncall experience. +- **Sensitive data.** The demo strips real query text where possible. Your own deployment will see its own queries in plaintext in `dbo.xevent_metrics`; plan accordingly. diff --git a/docs/dashboards/variables.md b/docs/dashboards/variables.md new file mode 100644 index 0000000..dd0acb8 --- /dev/null +++ b/docs/dashboards/variables.md @@ -0,0 +1,92 @@ +# Variables & Conventions + +SQLMonitor's dashboards follow a strict naming and templating contract so that: + +- Every panel in every dashboard works for any monitored instance without manual edits. +- Cross-dashboard drill-downs (click WhoIsActive → open XEvent trend for the same query) work out of the box. +- Queries recycle execution plans across concurrent users. + +## The variable contract + +Every dashboard declares (at minimum) these template variables: + +| Variable | Bound to | Populated from | Used in | +|---|---|---|---| +| `$sqlmonitor_datasource` | The `SQLMonitor` data source UID | Grafana's data-source picker | Every query | +| `$server` | `sql_instance` value | `SELECT sql_instance FROM dbo.sma_sql_instance WHERE is_active = 1 ORDER BY 1` | Instance filter | +| `$inventory_db` | Inventory DB name | Constant, default `DBA` | `FROM $inventory_db.dbo.all_server_*` | +| `$dba_db` | DBA DB name on target | Constant, default `DBA` | `FROM $dba_db.dbo.*` | +| `$host_name` | Windows host of `$server` | `SELECT host_name FROM dbo.sma_sql_instance WHERE sql_instance = '$server'` | Perfmon joins | +| `$perfmon_host_name` | host used by Perfmon tables (may differ from `$host_name` for FCIs) | same | Perfmon queries | +| `$is_local` | `1` when inventory == monitored | SQL expression | Switch between local table and linked-server view | +| `$ip` / `$fqdn` | Network identity for the instance | `dbo.sma_sql_instance` | Info panels | +| `$sql_schedulers` | `CPU count` | `sys.dm_os_schedulers` | Normalization (waits per core, etc.) | +| `$sqlserver_start_time_utc` | Last SQL Server restart | `sys.dm_os_sys_info` | "Since Startup" views | +| `$collection_time_utc` | Latest collection timestamp | Per-table | "As of…" panel titles | + +The hidden `*_table_name` variables (`$diskspace_table_name`, `$fileiostats_table_name`, …) let a dashboard query the **local** collection table when `is_local=1` and a **linked-server view** otherwise — same query, two destinations. + +## The `$server` change bus + +Changing `$server` should cascade automatically to `$host_name`, `$sql_schedulers`, `$ip`, etc. This is achieved by declaring the derived variables **after** `$server` with SQL like: + +```sql +-- $host_name +SELECT host_name +FROM $inventory_db.dbo.sma_sql_instance +WHERE sql_instance = '$server' +``` + +…and setting "Refresh: On Dashboard Load" + "On Time Range Change" so the chain re-evaluates. + +## Time filtering + +Every time-series query uses `$__timeFilter` on a `datetime2` column named `collection_time` or `event_time`: + +```sql +SELECT collection_time AS time, wait_type, wait_time_ms +FROM $dba_db.dbo.wait_stats +WHERE $__timeFilter(collection_time) + AND sql_instance = '$server'; +``` + +## Cross-dashboard drill-down URL conventions + +Panels use these patterns for **Data Links** (see `_README-Grafana-Variables.sql`): + +| From | To | Link template | +|---|---|---| +| Any fleet grid | Live Distributed for the clicked row | `d/distributed_live_dashboard?var-server=${__data.fields.srv_name}` | +| Live Distributed | WhoIsActive for a session | `d/WhoIsActive?var-server=${server}&var-session_id=${__data.fields.spid}&viewPanel=132` | +| Live Distributed | Perfmon for a tempdb sub-panel | `d/distributed_perfmon?var-server=${__data.fields.sql_instance}&var-database=tempdb&viewPanel=115` | +| Wait Stats | sqlskills wait docs | `https://www.sqlskills.com/help/waits/${__value.raw}` | +| Disk Space | Disk Space (drilled) | `d/disk_space?var-server=${__data.fields.sql_instance}&var-perfmon_host_name=${__data.fields.host_name}&var-disk_drive=${__data.fields.disk_volume}&viewPanel=26` | + +## Dynamic SQL pattern (why queries are so readable) + +A recurring pattern you'll see inside SQLMonitor panels: + +```sql +DECLARE @server sysname = '$server'; +DECLARE @is_local bit = $is_local; +DECLARE @sql nvarchar(max) = +N'SELECT collection_time AS time, counter, value + FROM ' + QUOTENAME('$dba_db') + '.dbo.performance_counters + WHERE $__timeFilter(collection_time) + AND sql_instance = @server'; +IF @is_local = 0 + SET @sql = REPLACE(@sql, N'.dbo.performance_counters', N'.dbo.vw_performance_counters_remote'); +EXEC sys.sp_executesql @sql, N'@server sysname', @server; +``` + +This yields **one compiled plan** regardless of which instance is selected, because the static plan is `sys.sp_executesql` with a `sysname` parameter. + +## Adding a new dashboard + +1. Base it on `Monitoring - Live - Distributed.json` so you inherit the variable chain. +2. Tag it `mssql, sqlmonitor, `. The Grafana folder & search filters rely on these. +3. Use `$__timeFilter()` on every time series. +4. Never hard-code `DBA` or an instance name — always use `$dba_db` / `$server`. +5. Export with `Share → Export → Export for sharing externally` to strip data-source UIDs. + +See [`_README-Grafana-Variables.sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Grafana-Dashboards/_README-Grafana-Variables.sql) for more URL templates. diff --git a/docs/deployment/index.md b/docs/deployment/index.md new file mode 100644 index 0000000..d6ee7be --- /dev/null +++ b/docs/deployment/index.md @@ -0,0 +1,50 @@ +# Deployment + +SQLMonitor deploys in three phases: + +1. **Bootstrap the central server** — install Grafana, the DBA database, and the SQLMonitor repo on a machine that will orchestrate deployment. +2. **Onboard an instance** — run `Install-SQLMonitor.ps1` against the target instance. This creates objects, loads 3rd-party kits (First Responder Kit, Darling Data, Ola Hallengren), sets up Perfmon, creates jobs, creates partitions, sets up linked servers, and wires up the Grafana login. +3. **Import dashboards** — import the JSONs in `Grafana-Dashboards/` into the `SQLMonitor` folder in your Grafana instance, pointing them at the `SQLMonitor` data source. + +## Pages in this section + +- **[Prerequisites](prerequisites.md)** — what you need in place before running the installer. +- **[Install Walkthrough](install.md)** — step-by-step first install. +- **[Install-SQLMonitor Parameters](parameters.md)** — full reference for every parameter of `Install-SQLMonitor.ps1`. +- **[Install Steps](steps.md)** — every named install step, in order, and what it does. +- **[Upgrade](upgrade.md)** — how to refresh an already-deployed instance. +- **[Remove](remove.md)** — `Remove-SQLMonitor.ps1` walkthrough. +- **[Troubleshooting](troubleshooting.md)** — common errors and fixes. + +## High-level flow + +``` mermaid +flowchart LR + A[Close repo to
central server] --> B[Install Grafana
+ data source] + B --> C[Copy Wrapper-Samples/
*.ps1 to Private/] + C --> D[Edit Private/
Wrapper-InstallSQLMonitor.ps1] + D --> E[Run wrapper →
Install-SQLMonitor.ps1] + E --> F[59 install steps] + F --> G[Import
Grafana-Dashboards/*.json] + G --> H[Deploy Alert Engine
optional] + H --> I[Deploy sql_exporter
optional] +``` + +## Deploy-time decisions + +Before you run the installer, decide: + +| Decision | Options | Where it maps in the installer | +|---|---|---| +| Topology | Distributed vs Central vs Hybrid | `SqlInstanceForTsqlJobs`, `SqlInstanceForPowershellJobs`, `SqlInstanceAsDataDestination` | +| Inventory server identity | Hostname of the observability SQL instance | `InventoryServer` | +| Data destination | Same as monitored instance? Or push elsewhere? | `SqlInstanceAsDataDestination` | +| Memory-Optimized usage | On by default; disable for non-supporting editions | `MemoryOptimizedObjectsUsage` | +| Retention | Per-table (via `dbo.purge_table`) or a single fleet default | `RetentionDays` | +| 3rd-party kits | Use bundled ZIPs or reference existing installs | `FirstResponderKitZipFile`, `DarlingDataZipFile`, `OlaHallengrenSolutionZipFile` | +| Managed Instance? | If yes, a different disk-space / perfmon path is used | `IsManagedInstance` | + +## What the installer is not + +- **Not idempotent-across-versions by default.** It is idempotent for the same version; when upgrading SQLMonitor itself, run it again — it will upgrade objects in place. See [Upgrade](upgrade.md). +- **Not a replacement for change control.** The installer creates SQL Agent jobs, proxies, credentials, linked servers and logins on both the monitored and inventory instances. Treat it as a production change. diff --git a/docs/deployment/install.md b/docs/deployment/install.md new file mode 100644 index 0000000..8484aff --- /dev/null +++ b/docs/deployment/install.md @@ -0,0 +1,133 @@ +# Install Walkthrough + +End-to-end, first-time install of SQLMonitor on a single SQL Server instance. + +> **Prerequisites not met?** See [Prerequisites](prerequisites.md) first. + +## 1. Clone the repo + +On your deployer machine: + +```powershell +# Pick a permanent home +$repoRoot = 'C:\Code\GitHub' +git clone https://github.com/imajaydwivedi/SQLMonitor.git $repoRoot\SQLMonitor +cd $repoRoot\SQLMonitor +``` + +## 2. Copy the wrapper to `Private/` + +`Wrapper-Samples/` contains sanitized wrappers. **Do not edit them in-place** — the `Private/` folder is in `.gitignore` so your secrets won't leak. + +```powershell +New-Item -ItemType Directory -Path .\Private -Force | Out-Null +Copy-Item .\Wrapper-Samples\Wrapper-InstallSQLMonitor.ps1 .\Private\Wrapper-InstallSQLMonitor.ps1 +``` + +## 3. Edit your wrapper + +Open `Private\Wrapper-InstallSQLMonitor.ps1` and fill in: + +```powershell +# --- Credentials --- +$SqlCredential = Get-Credential -UserName 'sa' -Message 'SA password' +$WindowsCredential = Get-Credential -UserName 'DOMAIN\svc_sqlmonitor' -Message 'Domain svc acct' +$DbaGroupMailId = 'dba@contoso.com' + +# --- Topology --- +$SqlInstanceToBaseline = 'DB-01' # the instance you are onboarding +$InventoryServer = 'SQLMon-Inv-01' # where all_server_* live +$HostName = 'DB-01-VM' # Windows host of the monitored instance +$SqlInstanceAsDataDestination = 'DB-01' # same instance = Distributed +$SqlInstanceForTsqlJobs = 'DB-01' # default: same as monitored +$SqlInstanceForPowershellJobs = 'DB-01' # default: same as monitored + +# --- 3rd-party zips (downloaded in step 0 of the wrapper) --- +$FirstResponderKitZipFile = 'C:\Code\GitHub\SQLMonitor\BrentOzarULTD-SQL-Server-First-Responder-Kit-*.zip' +$DarlingDataZipFile = 'C:\Code\GitHub\SQLMonitor\DarlingData-main.zip' +$OlaHallengrenSolutionZipFile = 'C:\Code\GitHub\SQLMonitor\sql-server-maintenance-solution-master.zip' +``` + +## 4. Dry-run first + +Run the wrapper with `-WhatIf` style control by using `-OnlySteps 1__CreateDbaDatabase`: + +```powershell +cd $repoRoot\SQLMonitor +.\Private\Wrapper-InstallSQLMonitor.ps1 ` + -OnlySteps '1__CreateDbaDatabase' ` + -Verbose +``` + +If this succeeds, database `DBA` exists on `DB-01`, the SQL recovery model & file sizes are set, and Query Store is turned on. + +## 5. Run the full install + +```powershell +.\Private\Wrapper-InstallSQLMonitor.ps1 -Verbose +``` + +You will see each step announce itself: + +```text +[Start-Step] 1__CreateDbaDatabase +[Start-Step] 2__AllDatabaseObjects +[Start-Step] 3__FirstResponderKit +... +[Start-Step] 59__DartingData +[Install-SQLMonitor] FINISHED in 00:12:43 +``` + +First install on a modest instance takes **5–15 minutes**, most of which is the First-Responder-Kit / Darling-Data / Ola-Hallengren object deployment. + +## 6. Verify + +### On the monitored instance + +```sql +USE DBA; +SELECT name FROM sys.tables WHERE name LIKE 'performance_counters%'; -- collection tables exist +SELECT TOP 10 collection_time, object_name, counter_name FROM dbo.performance_counters ORDER BY 1 DESC; +SELECT job_id, name, enabled FROM msdb.dbo.sysjobs WHERE name LIKE '(dba) %' ORDER BY name; +``` + +Expected: 40+ `(dba) *` jobs, enabled, owned by `sa`. + +### On the inventory instance + +```sql +USE DBA; +SELECT sql_instance, host_name, last_modified FROM dbo.sma_sql_instance ORDER BY sql_instance; +SELECT TOP 10 * FROM dbo.all_server_volatile_info; +``` + +After 5–10 minutes the `all_server_volatile_info` table should show one row per onboarded instance. + +### In Grafana + +1. Add / confirm data source: + - Type: **Microsoft SQL Server**. + - Name: **`SQLMonitor`** (exact). + - Host: `:1433`. + - Database: `DBA`. + - User / Password: `grafana` / ``. +2. Import each JSON from `Grafana-Dashboards/` into a folder named `SQLMonitor`: + - Grafana → Dashboards → Import → Upload JSON file → pick the `SQLMonitor` data source. +3. Open `Monitoring - Live - Distributed`. Within a minute or two of data collection, panels will populate. + +## 7. Next steps + +- Review [Install Steps](steps.md) to understand what the installer just did. +- If panels stay empty after 15 minutes, see [Troubleshooting](troubleshooting.md). +- Onboard more servers by changing `$SqlInstanceToBaseline` / `$HostName` and re-running. Or use `Wrapper-All-Servers-*` scripts for parallel multi-server onboarding. +- Set up [Alerting](../alerting.md) once you have a week of baseline data. + +## Parallel multi-server install + +For a fleet, use `Wrapper-Samples/Wrapper-All-Servers-*.ps1` with `PoshRSJob`. Copy one to `Private/`, edit the server list, run: + +```powershell +.\Private\Wrapper-Install-SQLMonitor-Servers-Parallel.ps1 -Verbose +``` + +This parallelizes `Install-SQLMonitor.ps1` across instances (default 6 concurrent runspaces). diff --git a/docs/deployment/parameters.md b/docs/deployment/parameters.md new file mode 100644 index 0000000..1a2596c --- /dev/null +++ b/docs/deployment/parameters.md @@ -0,0 +1,165 @@ +# Install-SQLMonitor Parameters + +Full reference for every parameter of `SQLMonitor/Install-SQLMonitor.ps1`. Parameter names and defaults are verified against the script header. + +!!! tip "Authoritative source" + The parameter block in [`Install-SQLMonitor.ps1`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/SQLMonitor/Install-SQLMonitor.ps1) is the source of truth. Re-check there for the most recent parameter list. + +## Identity & placement + +| Parameter | Required | Default | Purpose | +|---|---|---|---| +| `SqlInstanceToBaseline` | :material-check: | | The monitored SQL instance onto which SQLMonitor objects and jobs are installed. | +| `InventoryServer` | :material-check: | | SQL instance holding `all_server_*` tables. | +| `DbaToolsFolderPath` | :material-check: | | Path to the `dbatools` PowerShell module folder on the deployer (e.g. `D:\Downloads\dbatools\2.1.12\`). Used to import the module across PSRemoting sessions. | +| `InventoryDatabase` | | `DBA` | Database name on the inventory server. | +| `DbaDatabase` | | `DBA` | Name of the database created on the monitored instance. | +| `HostName` | | | Windows hostname of the monitored instance (PSRemoting target). Required for non-Managed-Instance installs. Must match the `SERVERPROPERTY('ComputerNamePhysicalNetBIOS')` of a non-clustered host or the physical node for an FCI/AG. | +| `SqlInstanceAsDataDestination` | | `$SqlInstanceToBaseline` | Where collection data is pushed. Set to a different instance to separate monitoring writes from the monitored instance. | +| `SqlInstanceForTsqlJobs` | | `$SqlInstanceToBaseline` | Where T-SQL SQL-Agent jobs are registered. | +| `SqlInstanceForPowershellJobs` | | `$SqlInstanceToBaseline` | Where PowerShell / CmdExec jobs are registered (Perfmon, disk space, OS processes). | +| `SQLMonitorPath` | | repo root | Path to this cloned SQLMonitor repo on the deployer. Auto-detected from the script location when not supplied. | +| `RemoteSQLMonitorPath` | | `C:\SQLMonitor` | Where collectors and Perfmon BLGs live on the monitored host. | +| `XEventDirectory` | | | Override folder for the XEvent session's file target. Default is under `RemoteSQLMonitorPath`. | + +## Credentials & mail + +| Parameter | Required | Default | Purpose | +|---|---|---|---| +| `SqlCredential` | | Current Windows identity | SQL credential used for CREATE LOGIN, CREATE DATABASE, job creation, etc. Omit to use integrated auth. | +| `WindowsCredential` | | Current Windows identity | Windows credential used for PSRemoting and wrapped by the SQL Agent proxy. | +| `DbaGroupMailId` | | | One or more email addresses that Database-Mail jobs send to (dashboard mail, login-expiry mail, stuck-job alerts). Can be a DL or comma-separated list. | + +## Third-party kits + +| Parameter | Default | Purpose | +|---|---|---| +| `FirstResponderKitZipFile` | | Path to the First-Responder-Kit zip. Expanded and scripts run against the `DBA` database in step `4__FirstResponderKitObjects`. | +| `DarlingDataZipFile` | | Path to the `DarlingData-main.zip` release. Used by step `5__DarlingDataObjects`. | +| `OlaHallengrenSolutionZipFile` | | Path to `sql-server-maintenance-solution-master.zip`. Used by step `6__OlaHallengrenSolutionObjects`. | + +## Behavior toggles + +| Parameter | Default | Purpose | +|---|---|---| +| `MemoryOptimizedObjectsUsage` | `$true` | Use Memory-Optimized tables for `all_server_volatile_info*`. Set `$false` on Standard Edition inventory servers. | +| `IsManagedInstance` | `$false` | Skip host-side steps (Perfmon, disk-space via Windows, OS processes). | +| `InstanceScopeFeaturesOnly` | `$false` | Install only per-instance collectors; skip all inventory-side jobs/tables. | +| `RetentionDays` | script default | Default retention. Overridable per-table via `dbo.purge_table`. | +| `JobsExecutionWaitTimeoutMinutes` | `5` | How long the installer waits for a newly-created job's first run to complete. | +| `DryRun` | `$false` | Print what would be done without making changes. | +| `PreQuery` / `PostQuery` | | Free-form T-SQL executed before/after the step sequence. | +| `ReturnInlineErrorMessage` | `$false` | Surface inner-exception messages verbatim for machine parsing. | +| `GrafanaDashboardPortal` | `https://sqlmonitor.ajaydwivedi.com:3000/` | Value stamped into the `dashboard_url` columns so dashboard emails point at the right Grafana. | + +## Skip / force flags + +Fine-grained flags to opt out of specific validations or re-run specific steps destructively. + +| Parameter | Default | Purpose | +|---|---|---| +| `DropCreatePowerShellJobs` | `$false` | Drop & recreate PowerShell-subsystem jobs even if they exist. | +| `DropCreateWhoIsActiveTable` | `$false` | Drop & recreate `dbo.WhoIsActive` (wipes history). | +| `DropCreateLinkedServerOnInventory` | `$false` | Drop & recreate linked server on inventory. | +| `DropCreateLinkedServerForDataDestinationServer` | `$false` | Drop & recreate linked server for a non-local data-destination instance. | +| `SkipPowerShellJobs` | `$false` | Do not create PowerShell-subsystem jobs. | +| `SkipTsqlJobs` | `$false` | Do not create T-SQL jobs. | +| `SkipRDPSessionSteps` | `$false` | Do not attempt anything over PSRemoting (headless / locked-down hosts). | +| `SkipInventorySteps` | `$false` | Do not touch the inventory server. | +| `SkipMultiMailJobSteps` | `$true` | Do not create dashboard-mail jobs (kept off by default to avoid inbox floods). | +| `SkipMultiServerviewsUpgrade` | `$true` | Do not reshape multi-server views during upgrade. | +| `SkipWindowsAdminAccessTest` | `$false` | Skip the admin-rights precheck on the monitored host. | +| `SkipMailProfileCheck` | `$false` | Don't fail if Database Mail isn't configured on the inventory. | +| `SkipCollationCheck` | `$false` | Skip the collation match check between monitored and inventory instances. | +| `SkipPageCompression` | `$false` | Skip step `55__EnablePageCompression`. | +| `SkipDriveCheck` | `$false` | Skip drive-space pre-check. | +| `SkipPingCheck` | `$false` | Skip ICMP reachability check. | +| `HasCustomizedTsqlJobs` | `$false` | Declare that the target has customized T-SQL jobs you want preserved. | +| `HasCustomizedPowerShellJobs` | `$false` | Same for PowerShell jobs. | +| `OverrideCustomizedTsqlJobs` | `$false` | Overwrite customized T-SQL jobs anyway. | +| `OverrideCustomizedPowerShellJobs` | `$false` | Overwrite customized PowerShell jobs anyway. | +| `ForceTSQLStepType4TsqlJobs` | `$true` | Force job steps to run as T-SQL subsystem where applicable. | +| `ForceSetupOfTaskSchedulerJobs` | `$false` | Set up Windows Task Scheduler jobs in addition to SQL Agent jobs. | +| `ConfirmValidationOfMultiInstance` | `$false` | Required when onboarding an instance that has multiple SQL services on one host. | +| `ConfirmSetupOfTaskSchedulerJobs` | `$false` | Required confirmation flag when `ForceSetupOfTaskSchedulerJobs=$true`. | +| `UpdateSQLAgentJobsThreshold` | `$true` | Refresh `dbo.sql_agent_job_thresholds` with defaults for new jobs. | + +## Step control + +All four parameters below accept the same 59-value set. See [Install Steps](steps.md) for the full list. + +| Parameter | Behavior | +|---|---| +| `OnlySteps` | Run **only** these steps (useful for partial re-runs). | +| `SkipSteps` | Run everything **except** these steps. | +| `StartAtStep` | Resume from this step (inclusive); skip everything before. | +| `StopAtStep` | Stop after this step (inclusive). | + +Examples: + +```powershell +# Just re-register the jobs +.\Install-SQLMonitor.ps1 @commonArgs -OnlySteps 'CreateJobCategory','CreateJobs' + +# Resume a failed install from step 7 +.\Install-SQLMonitor.ps1 @commonArgs -StartAtStep '7__PerfmonDataCollectorSet' + +# Everything except the kits +.\Install-SQLMonitor.ps1 @commonArgs -SkipSteps 'FirstResponderKit','DarlingData','OlaHallengrenSolution' +``` + +## Parameter combinations by scenario + +### Small lab / single server (Distributed, monitored = inventory) + +```powershell +$commonArgs = @{ + SqlInstanceToBaseline = 'Lab-01' + HostName = 'Lab-01-VM' + InventoryServer = 'Lab-01' + SqlInstanceAsDataDestination = 'Lab-01' + SqlCredential = $SqlCredential + WindowsCredential = $WindowsCredential + DbaGroupMailId = 'me@example.com' + ... +} +``` + +### Fleet with dedicated inventory (Distributed) + +```powershell +$commonArgs = @{ + SqlInstanceToBaseline = 'Prod-Sales-01' + HostName = 'Prod-Sales-01-VM' + InventoryServer = 'SQLMon-Inv-01' + SqlInstanceAsDataDestination = 'Prod-Sales-01' + ... +} +``` + +### Central topology (all jobs on inventory) + +```powershell +$commonArgs = @{ + SqlInstanceToBaseline = 'Prod-Sales-01' + HostName = 'SQLMon-Inv-01-VM' # host of the central collector + InventoryServer = 'SQLMon-Inv-01' + SqlInstanceAsDataDestination = 'SQLMon-Inv-01' + SqlInstanceForTsqlJobs = 'SQLMon-Inv-01' + SqlInstanceForPowershellJobs = 'SQLMon-Inv-01' + ... +} +``` + +### Managed Instance / RDS (no host) + +```powershell +$commonArgs = @{ + SqlInstanceToBaseline = 'mi-prod-01.database.windows.net' + InventoryServer = 'SQLMon-Inv-01' + IsManagedInstance = $true + SqlInstanceForPowershellJobs = 'SQLMon-Inv-01' # host-side collectors run on inventory + SkipSteps = @('7__PerfmonDataCollectorSet','8__SetupCollectorTask') + ... +} +``` diff --git a/docs/deployment/prerequisites.md b/docs/deployment/prerequisites.md new file mode 100644 index 0000000..8481c75 --- /dev/null +++ b/docs/deployment/prerequisites.md @@ -0,0 +1,115 @@ +# Prerequisites + +Everything SQLMonitor expects to find before `Install-SQLMonitor.ps1` is run. + +## 1. Central deployer machine + +The machine *from which* you run the installer (can be your workstation, a jump host, or the inventory server itself). + +- Windows 10 / 11 / Server 2016+ with PowerShell 5.1 **or** PowerShell 7+. +- Git (to clone the repo) or ability to download the repo ZIP. +- `dbatools` PowerShell module — the installer uses it heavily. Download in advance: + + ```powershell + Save-Module dbatools -Path "$env:USERPROFILE\Downloads\" + ``` + +- `PoshRSJob` PowerShell module (for parallel multi-server wrappers): + + ```powershell + Install-Module PoshRSJob -Scope AllUsers + ``` + +- Local admin rights on the monitored instance's Windows host (or a `WindowsCredential` that has them) — used for Perfmon data-collector setup, copying files to `C:\SQLMonitor`, and PsRemoting checks. +- Network access to: + - Monitored instance (SQL, 1433 or custom) and its Windows host (SMB, WinRM). + - Inventory server (SQL). + - Data destination server, if different. + +## 2. Monitored SQL Server instance + +Supported editions: **SQL Server 2012 onwards** (any edition that supports Extended Events). SQL Server 2008 R2 works but XEvent collection is disabled. + +Before running the installer: + +- `sa`-equivalent SQL credentials (or a Windows login that is `sysadmin`). +- A dedicated **Windows domain account** for the SQL Agent proxy (used by PowerShell and CmdExec subsystems). This account needs: + - `Administrators`, `Performance Log Users`, `Performance Monitor Users` membership on the monitored host. + - Read access to `$env:USERPROFILE\Downloads\` on the deployer (for initial copy). +- **Database Mail** profile configured on the inventory server (for dashboard mail / login-expiry mail). You can skip this with `-SkipMailProfileCheck $true`. +- **TDE / Encryption compatibility**: if your instance is TDE-encrypted, confirm the data destination is also TDE-capable (or use the same instance). + +## 3. Inventory SQL Server instance + +- A SQL Server instance that will hold the `all_server_*` aggregates. +- Enterprise / Developer edition **recommended** for Memory-Optimized tables (the installer's default). For Standard Edition, run with `-MemoryOptimizedObjectsUsage $false`. +- Can be **the same** instance you are monitoring (small fleets / labs), or a dedicated observability server. +- `sa`-equivalent credentials. + +## 4. Grafana + +- Grafana 10+ (any distribution: Grafana OSS, Grafana Cloud, self-hosted). +- A **Microsoft SQL Server** data source pointed at your inventory server, named `SQLMonitor`. +- Credentials: a SQL login named `grafana` with `db_datareader` on the `DBA` database. The installer creates this login on every instance and copies its SID from the inventory server so the same login password works cluster-wide (step `56__GrafanaLogin`). +- Optional but recommended: a folder in Grafana called **SQLMonitor** to house the dashboards. + +## 5. Third-party kits (optional but bundled into the install flow) + +Download in advance and point the installer at the ZIPs: + +- **First Responder Kit** — latest release from [BrentOzarULTD/SQL-Server-First-Responder-Kit](https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/releases). Supply via `FirstResponderKitZipFile`. +- **Darling Data** — `main.zip` from [erikdarlingdata/DarlingData](https://github.com/erikdarlingdata/DarlingData). Supply via `DarlingDataZipFile`. +- **Ola Hallengren Maintenance Solution** — `master.zip` from [olahallengren/sql-server-maintenance-solution](https://github.com/olahallengren/sql-server-maintenance-solution). Supply via `OlaHallengrenSolutionZipFile`. + +The exact download commands are documented in [`Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/Wrapper-Samples/Wrapper-InstallSQLMonitor.ps1). + +## 6. PowerShell remoting (deployer ↔ monitored host) + +The installer uses PSRemoting to lay down `C:\SQLMonitor\*`, set up Perfmon's `logman` data collector, and run the collectors as scheduled tasks where needed. + +On the **monitored host**, one-time setup: + +```powershell +Enable-PSRemoting -Force -SkipNetworkProfileCheck +Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value "SQLMonitor.Lab.com" -Concatenate -Force +Set-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Value 1 +``` + +On the **deployer**: + +```powershell +Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value "*" -Force # or scope to hostnames +``` + +## 7. Ports and firewall + +| From → To | Port | Reason | +|---|---|---| +| Deployer → Monitored host | 445 (SMB) | Copy SQLMonitor files to `C:\SQLMonitor`, ingest Perfmon .blg | +| Deployer → Monitored host | 5985/5986 (WinRM) | PSRemoting | +| Monitored instance ↔ Inventory instance | 1433 (or custom SQL) | Linked server aggregation | +| Grafana → Inventory instance | 1433 | Dashboard queries | +| Alert Engine → Inventory instance | 1433 | Alert reads + writes | +| sql_exporter → Monitored instance (if using Prom path) | 1433 | Metric scrapes | +| Prometheus → sql_exporter | 9399 | Scraping | +| Prometheus → windows_exporter | 9182 | Scraping | + +## Pre-flight check + +A fast sanity check before you run the installer: + +```powershell +# 1. Can you reach the monitored instance? +Invoke-Sqlcmd -ServerInstance 'MyInstance' -Query "SELECT @@VERSION" -TrustServerCertificate + +# 2. Can PSRemoting work? +Test-WSMan 'MyInstanceHost' -Credential $WindowsCredential -Authentication Negotiate + +# 3. Is the dbatools module loadable? +Import-Module 'D:\Downloads\dbatools\\dbatools.psd1' -Force + +# 4. Does the inventory instance have Database Mail? +Invoke-Sqlcmd -ServerInstance 'Inventory' -Query "SELECT name FROM msdb.dbo.sysmail_profile" +``` + +When all four succeed, continue to **[Install Walkthrough](install.md)**. diff --git a/docs/deployment/remove.md b/docs/deployment/remove.md new file mode 100644 index 0000000..3d869c9 --- /dev/null +++ b/docs/deployment/remove.md @@ -0,0 +1,85 @@ +# Remove SQLMonitor + +`SQLMonitor/Remove-SQLMonitor.ps1` is the inverse of the installer. It removes an instance from SQLMonitor in the correct order — alerts → jobs → procs → views → tables → proxies → linked servers → inventory row. + +## Quick uninstall + +Copy the sample wrapper and run: + +```powershell +Copy-Item .\Wrapper-Samples\Wrapper-RemoveSQLMonitor.ps1 .\Private\Wrapper-RemoveSQLMonitor.ps1 +# Edit Private\Wrapper-RemoveSQLMonitor.ps1 to set SqlInstanceToBaseline / HostName / InventoryServer +.\Private\Wrapper-RemoveSQLMonitor.ps1 -Verbose +``` + +## What gets removed + +The removal runs in this order to respect dependencies: + +| Phase | Steps | What | +|---|---|---| +| 1 | `1__Remove_SQLAgentAlerts` | SQL Agent alerts for severities & specific error numbers | +| 2 | `2__*`–`30__RemoveJob_*` | All `(dba) *` SQL Agent jobs (both per-instance and inventory) | +| 3 | `31__*`–`68__DropProc_*` | All `dbo.usp_*` and helper procs | +| 4 | `69__*`–`75__DropView_*` | `vw_*` views and `sma_*` views | +| 5 | `77__DropXEvent_XEventMetrics` | The Extended Event session | +| 6 | `78__DropLinkedServer` | Linked server from inventory to this instance | +| 7 | `79__DropLogin_Grafana` | The `grafana` login *on this instance only* (leaves the inventory login alone) | +| 8 | `80__*`–`142__DropTable_*` | All `dbo.*` collection and inventory tables | +| 9 | `143__RemovePerfmonFilesFromDisk` | `.blg` files under `RemoteSQLMonitorPath` on the host | +| 10 | `144__RemoveXEventFilesFromDisk` | `.xel` files under `RemoteSQLMonitorPath` on the host | +| 11 | `145__DropProxy` | SQL Agent proxy | +| 12 | `146__DropCredential` | SQL credential | +| 13 | `147__RemoveInstanceFromInventory` | Delete the row from `dbo.sma_sql_instance` on inventory | + +## Partial removal + +Use `-OnlySteps`, `-SkipSteps`, `-StartAtStep`, `-StopAtStep` exactly as with the installer. + +### Common recipes + +```powershell +# Just decommission the jobs, keep the data +.\Remove-SQLMonitor.ps1 @commonArgs -StopAtStep '30__RemoveJob_StopStuckSQLMonitorJobs' + +# Remove everything EXCEPT historical Blitz tables (keep the tuning history) +.\Remove-SQLMonitor.ps1 @commonArgs -SkipSteps '91__DropTable_Blitz','92__DropTable_BlitzWho','93__DropTable_BlitzCache','102__DropTable_BlitzIndex' + +# Leave this instance in the inventory so the grafana UI still lists it as 'offline' +.\Remove-SQLMonitor.ps1 @commonArgs -SkipSteps '147__RemoveInstanceFromInventory' +``` + +## What does NOT get removed + +Out of an abundance of caution the remove script leaves these alone: + +- **The `DBA` database itself** — you may use it for other things. Drop it yourself with `DROP DATABASE DBA` if desired. +- **First-Responder-Kit / Darling-Data / Ola-Hallengren objects** — those were installed by name, not by SQLMonitor. Drop them via their own uninstall scripts if desired. +- **`sp_WhoIsActive`** — kept. Drop with `DROP PROC sp_WhoIsActive` if desired. +- **Database Mail profile & accounts** — kept. +- **Grafana dashboards** — delete via Grafana UI or API. +- **Anything under `C:\SQLMonitor\Private\`** — user-owned files are never touched. + +## Removing the inventory server itself + +If you want to tear down the **entire** SQLMonitor deployment (not just one monitored instance): + +1. Run `Remove-SQLMonitor.ps1` against **every** monitored instance first, one at a time. +2. Then run it against the inventory server with `SqlInstanceToBaseline = $InventoryServer`. This drops the inventory-scoped jobs, procs and `all_server_*` tables. +3. Finally, drop the `DBA` database on the inventory if desired. + +## Verifying removal + +```sql +USE DBA; +SELECT name FROM sys.tables WHERE name LIKE 'performance_counters%'; -- should be empty +SELECT name FROM msdb.dbo.sysjobs WHERE name LIKE '(dba) %'; -- should be empty +SELECT name FROM sys.server_principals WHERE name = 'grafana'; -- should return 0 rows +``` + +On the inventory: + +```sql +USE DBA; +SELECT sql_instance FROM dbo.sma_sql_instance WHERE sql_instance = 'MyInstance'; -- should be empty +``` diff --git a/docs/deployment/steps.md b/docs/deployment/steps.md new file mode 100644 index 0000000..3fa3944 --- /dev/null +++ b/docs/deployment/steps.md @@ -0,0 +1,119 @@ +# Install Steps + +`Install-SQLMonitor.ps1` runs up to 59 named steps in order. This page explains what each step does so you can `-SkipSteps`, `-StartAtStep`, `-StopAtStep`, or `-OnlySteps` with confidence. + +!!! note "Authoritative source" + The canonical step list is the `ValidateSet` on `-OnlySteps` in [`Install-SQLMonitor.ps1`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/SQLMonitor/Install-SQLMonitor.ps1). This page mirrors that list. + +## Phase 1 — Core database objects (steps 1–8) + +| # | Step | What it does | +|---|---|---| +| 1 | `1__sp_WhoIsActive` | Installs Adam Machanic's `sp_WhoIsActive` into `DBA`. | +| 2 | `2__AllDatabaseObjects` | Runs `DDLs/SCH-Create-All-Objects.sql` — tables, views, procs, the CLR assembly and all core `dbo.*` objects. The heaviest step. | +| 3 | `3__XEventSession` | Creates the `XEventMetrics` Extended Event session targeting a file under `RemoteSQLMonitorPath`. | +| 4 | `4__FirstResponderKitObjects` | Expands `FirstResponderKitZipFile` and runs its Install-All-Scripts into `DBA`. | +| 5 | `5__DarlingDataObjects` | Expands `DarlingDataZipFile` and installs `sp_HumanEvents`, `sp_PressureDetector`, etc. into `DBA`. | +| 6 | `6__OlaHallengrenSolutionObjects` | Installs the Ola Hallengren maintenance solution into `DBA`. | +| 7 | `7__sp_WhatIsRunning` | Installs the `dbo.sp_WhatIsRunning` wrapper proc. | +| 8 | `8__usp_GetAllServerInfo` | Installs `dbo.usp_GetAllServerInfo` and its family of helpers. | + +## Phase 2 — Host-side setup (steps 9–12) + +| # | Step | What it does | +|---|---|---| +| 9 | `9__CopyDbaToolsModule2Host` | Copies the `dbatools` module from the deployer to the monitored host so jobs can `Import-Module` it locally. | +| 10 | `10__CopyPerfmonFolder2Host` | Copies `SQLMonitor/*.ps1` collectors to `C:\SQLMonitor\` (or `RemoteSQLMonitorPath`). | +| 11 | `11__SetupPerfmonDataCollector` | Creates a Perfmon `logman` Data Collector Set that writes `.blg` files every 10 seconds. | +| 12 | `12__CreateCredentialProxy` | Creates the SQL credential and SQL Agent proxy that wrap `WindowsCredential`, used by all PowerShell and CmdExec subsystem job steps. | + +## Phase 3 — Per-instance collection jobs (steps 13–31) + +| # | Step | Creates job | +|---|---|---| +| 13 | `13__CreateJobCollectDiskSpace` | `(dba) Collect-DiskSpace` | +| 14 | `14__CreateJobCollectPerfmonData` | `(dba) Collect-PerfmonData` | +| 15 | `15__CreateJobCollectWaitStats` | `(dba) Collect-WaitStats` | +| 16 | `16__CreateJobCollectXEvents` | `(dba) Collect-XEvents` | +| 17 | `17__CreateJobCollectFileIOStats` | `(dba) Collect-FileIOStats` | +| 18 | `18__CreateJobPartitionsMaintenance` | `(dba) Partitions-Maintenance` | +| 19 | `19__CreateJobPurgeTables` | `(dba) Purge-Tables` | +| 20 | `20__CreateJobRemoveXEventFiles` | `(dba) Remove-XEventFiles` | +| 21 | `21__CreateJobRunLogSaver` | `(dba) Run-LogSaver` | +| 22 | `22__CreateJobRunTempDbSaver` | `(dba) Run-TempDbSaver` | +| 23 | `23__CreateJobRunWhoIsActive` | `(dba) Run-WhoIsActive` | +| 24 | `24__CreateJobRunBlitz` | `(dba) Run-Blitz` | +| 24 | `24__CreateJobRunBlitzIndex` | `(dba) Run-BlitzIndex` | +| 26 | `26__CreateJobRunBlitzIndexWeekly` | `(dba) Run-BlitzIndexWeekly` | +| 27 | `27__CreateJobCollectMemoryClerks` | `(dba) Collect-MemoryClerks` | +| 28 | `28__CreateJobCollectPrivilegedInfo` | `(dba) Collect-PrivilegedInfo` | +| 29 | `29__CreateJobCollectAgHealthState` | `(dba) Collect-AgHealthState` | +| 30 | `30__CreateJobCheckSQLAgentJobs` | `(dba) Check-SQLAgentJobs` | +| 31 | `31__CreateJobCaptureAlertMessages` | `(dba) Capture-AlertMessages` | + +## Phase 4 — SQL Agent alerts & availability (step 32–34) + +| # | Step | What it does | +|---|---|---| +| 32 | `32__CreateSQLAgentAlerts` | Creates SQL Agent alerts for severities 17–25 and specific error numbers (deadlocks, …), routed to `(dba) Capture-AlertMessages`. | +| 33 | `33__CreateJobUpdateSqlServerVersions` | `(dba) Update-SqlServerVersions` — keeps `dbo.sql_server_versions` fresh with CU metadata. | +| 34 | `34__CreateJobCheckInstanceAvailability` | `(dba) Check-InstanceAvailability` — pings every instance in `dbo.sma_sql_instance` from the inventory server. | + +## Phase 5 — Inventory aggregation jobs (steps 35–51) + +These steps run on the **inventory** server. Skipped when `-SkipInventorySteps $true` or when `InstanceScopeFeaturesOnly $true`. + +| # | Step | Creates job | +|---|---|---| +| 35 | `35__CreateJobGetAllServerStableInfo` | `(dba) Get-AllServerStableInfo` | +| 36 | `36__CreateJobGetAllServerVolatileInfo` | `(dba) Get-AllServerVolatileInfo` | +| 37 | `37__CreateJobGetAllServerCollectionLatencyInfo` | `(dba) Get-AllServerCollectionLatencyInfo` | +| 38 | `38__CreateJobGetAllServerSqlAgentJobs` | `(dba) Get-AllServerSqlAgentJobs` | +| 39 | `39__CreateJobGetAllServerDiskSpace` | `(dba) Get-AllServerDiskSpace` | +| 40 | `40__CreateJobGetAllServerLogSpaceConsumers` | `(dba) Get-AllServerLogSpaceConsumers` | +| 41 | `41__CreateJobGetAllServerTempdbSpaceUsage` | `(dba) Get-AllServerTempdbSpaceUsage` | +| 42 | `42__CreateJobGetAllServerAgHealthState` | `(dba) Get-AllServerAgHealthState` | +| 43 | `43__CreateJobGetAllServerServices` | `(dba) Get-AllServerServices` | +| 44 | `44__CreateJobGetAllServerBackups` | `(dba) Get-AllServerBackups` | +| 45 | `45__CreateJobGetAllServerAlertHistory` | `(dba) Get-AllServerAlertHistory` | +| 46 | `46__CreateJobGetAllServerDashboardMail` | `(dba) Get-AllServerDashboardMail` | +| 47 | `47__CreateJobStopStuckSQLMonitorJobs` | `(dba) Stop-StuckSQLMonitorJobs` | +| 48 | `48__CreateJobCollectLoginExpirationInfo` | `(dba) Collect-LoginExpirationInfo` | +| 49 | `49__CreateJobPopulateInventoryTables` | `(dba) Populate Inventory Tables` | +| 50 | `50__CreateJobSendLoginExpiryEmails` | `(dba) Send login expiry eMails` | +| 51 | `51__CreateJobComputeAllServerVolatileInfoHistoryHourly` | `(dba) Compute-AllServerVolatileInfoHistoryHourly` | + +## Phase 6 — Partitioning & compression (steps 52–55) + +| # | Step | What it does | +|---|---|---| +| 52 | `52__WhoIsActivePartition` | Applies hourly partition function / scheme to `dbo.WhoIsActive`. | +| 53 | `53__BlitzIndexPartition` | Partitions the `dbo.blitz_index*` tables. | +| 54 | `54__BlitzPartition` | Partitions the `dbo.blitz*` tables (non-index variants). | +| 55 | `55__EnablePageCompression` | Runs `dbo.usp_enable_page_compression` to compress eligible tables (skipped if `SkipPageCompression=$true`). | + +## Phase 7 — Cross-instance wiring (steps 56–59) + +| # | Step | What it does | +|---|---|---| +| 56 | `56__GrafanaLogin` | Creates the `grafana` login with a SID that matches across the fleet; grants `db_datareader` on `DBA`. | +| 57 | `57__LinkedServerOnInventory` | Creates the linked server *from* inventory *to* the monitored instance (used by `Get-AllServer*` jobs). | +| 58 | `58__LinkedServerForDataDestinationInstance` | Creates the linked server *from* the monitored instance *to* `SqlInstanceAsDataDestination` (only when they differ). | +| 59 | `59__AlterViewsForDataDestinationInstance` | Re-creates `vw_*` / `all_server_*` views on the monitored instance so they transparently read from the data-destination instance via the linked server. | + +## Reading the logs + +Each step logs one or more `[Start-Step]` and `[End-Step]` lines plus a duration. Re-runs with `-StartAtStep` skip everything before the named step **but still run pre-flight validations** (credential, PSRemoting, collation, …) — this is why resuming at step 40 still takes ~30 seconds to get to actual work. + +## Partial re-runs: common recipes + +```powershell +# Just refresh the collection procs and jobs +-OnlySteps '2__AllDatabaseObjects', '15__CreateJobCollectWaitStats', '17__CreateJobCollectFileIOStats' + +# Add Perfmon after an MI-flavored install +-OnlySteps '10__CopyPerfmonFolder2Host', '11__SetupPerfmonDataCollector', '14__CreateJobCollectPerfmonData' + +# Re-sync the grafana login SID only +-OnlySteps '56__GrafanaLogin' -DropCreatedLogin $true +``` diff --git a/docs/deployment/troubleshooting.md b/docs/deployment/troubleshooting.md new file mode 100644 index 0000000..29438e1 --- /dev/null +++ b/docs/deployment/troubleshooting.md @@ -0,0 +1,153 @@ +# Troubleshooting + +Common errors during install, collection, or dashboarding — and what to do about them. + +## Install-time + +### `The term 'Invoke-DbaQuery' is not recognized` + +The `dbatools` module wasn't importable in the runspace. Check `DbaToolsFolderPath` points at a folder whose layout is: + +```text +DbaToolsFolderPath\ + \ + dbatools.psd1 + dbatools.psm1 + ... +``` + +Try: + +```powershell +Save-Module dbatools -Path 'D:\Downloads\dbatools' -Repository PSGallery +``` + +then pass `-DbaToolsFolderPath 'D:\Downloads\dbatools'`. + +### `Access is denied` from PSRemoting + +The wrapper can't reach the monitored host as `WindowsCredential`. Check, on the **host**: + +```powershell +Enable-PSRemoting -Force -SkipNetworkProfileCheck +Set-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System ` + -Name LocalAccountTokenFilterPolicy -Value 1 +``` + +…and on the **deployer**: + +```powershell +Set-Item WSMAN:\Localhost\Client\TrustedHosts -Value 'MyHost.lab.com' -Concatenate -Force +``` + +If the host is cross-domain, pass a domain-qualified credential (`DOMAIN\svc_sqlmonitor`) or use CredSSP. + +### `Linked server creation failed: Msg 15457` + +Means you're creating a linked server with SQL auth but the target instance rejects the password. Re-check: + +- `grafana` SID matches between inventory and monitored instance (step `56__GrafanaLogin` with `-DropCreatedLogin $true` to force-sync). +- `TrustServerCertificate` or a valid cert on the target — TLS 1.2+ is mandatory on modern Windows SQL clients. + +### `Invalid column name 'X'` during `2__AllDatabaseObjects` + +Schema drift from a partial previous run. Rerun step 2 with an elevated error level: + +```powershell +-OnlySteps '2__AllDatabaseObjects' -Verbose -ReturnInlineErrorMessage $true +``` + +If a specific table is the culprit and you can afford to lose its data: + +```sql +DROP TABLE dbo.; +-- then re-run step 2 +``` + +### `The XEvent session 'XEventMetrics' already exists` + +Either drop and re-create or skip: + +```sql +IF EXISTS (SELECT 1 FROM sys.server_event_sessions WHERE name = 'XEventMetrics') + DROP EVENT SESSION XEventMetrics ON SERVER; +``` + +Then `-OnlySteps '3__XEventSession'`. + +## Collection-time + +### `(dba) Collect-PerfmonData` keeps failing + +Step output usually includes one of: + +- **`logman: The data collector set was not found.`** — run `-OnlySteps '11__SetupPerfmonDataCollector'` again. Confirm the collector exists: `logman query DBA` on the host. +- **`Access to the path '\\host\C$\SQLMonitor\Perfmon\*.blg' is denied`** — the SQL Agent proxy account lacks SMB read on `C$` of the host. Grant it via a local group or re-use an admin account. +- **`No .blg files found`** — the Perfmon data collector is stopped. Start it: `logman start DBA` on the host. + +### `(dba) Collect-XEvents` inserts no rows + +Check `.xel` files are being created: + +```powershell +Get-ChildItem -Path 'C:\SQLMonitor\XEvents' -Filter '*.xel' | Select-Object Name, LastWriteTime +``` + +If files exist but the job inserts zero rows, the XEvent session might be started but not firing. Confirm: + +```sql +SELECT name, startup_state FROM sys.server_event_sessions WHERE name = 'XEventMetrics'; +SELECT * FROM sys.dm_xe_sessions WHERE name = 'XEventMetrics'; -- is it running? +``` + +Start it: `ALTER EVENT SESSION XEventMetrics ON SERVER STATE = START;` + +### `(dba) Get-AllServerVolatileInfo` errors with `Login failed for user 'grafana'` + +The `grafana` login SIDs don't match between inventory and the target. Re-run: + +```powershell +-OnlySteps '56__GrafanaLogin' -DropCreatedLogin $true +``` + +## Dashboarding + +### Grafana panels say "No data" + +1. Test the data source: Grafana → Connections → Data sources → `SQLMonitor` → **Save & test**. +2. If the test passes, run a panel's SQL directly in SSMS with Grafana's variable values substituted. Most "No data" is a time-filter problem — the panel default `$__timeFilter` resolves to a window where your collection hasn't run yet. +3. Confirm collection: `SELECT MAX(collection_time) FROM DBA.dbo.all_server_volatile_info` — should be within the last 1–2 minutes. + +### Grafana: "Error evaluating ..." + +Usually means a required variable is empty. Open the variable editor (top of the dashboard) and ensure `$instance` (or the relevant variable) has a value. Some dashboards will not render when a `multi-value` variable is `All` — pick one instance. + +### Dashboard is slow + +Open `Monitoring - Live - Distributed` → Panel title → Inspect → Query. Copy the SQL to SSMS and `SET STATISTICS TIME, IO ON`. Common causes: + +- Missing non-clustered index on `collection_time` + instance — `dbo.usp_enable_page_compression` also creates the expected indexes; re-run step 55. +- `all_server_volatile_info_history` hasn't been hourly-rolled-up. Run `(dba) Compute-AllServerVolatileInfoHistoryHourly` manually. +- 20+ concurrent dashboard viewers — SQLMonitor's dynamically-parameterized queries help but a dedicated read replica of inventory is the long-term answer. + +## Alert Engine + +### Engine starts but fires nothing + +Check `Alerting/SQLMonitorAlertEngineApp.py` logs: likely `usp_get_active_alert_by_*` returns zero rows. Confirm: + +```sql +SELECT * FROM dbo.alert_history WHERE collection_time > DATEADD(HOUR, -1, GETDATE()); +SELECT * FROM dbo.sma_alerts WHERE state = 'active'; +``` + +If `alert_history` is empty, the `(dba) Capture-AlertMessages` job isn't writing there — look at its history. + +## Where to report a bug + +If nothing here helps, open an issue: . Include: + +- `Install-SQLMonitor.ps1` output with `-Verbose -ReturnInlineErrorMessage $true`. +- SQL Server version: `SELECT @@VERSION`. +- Windows version of the deployer and host. +- `dbatools` version: `Get-Module dbatools -ListAvailable`. diff --git a/docs/deployment/upgrade.md b/docs/deployment/upgrade.md new file mode 100644 index 0000000..7626d2b --- /dev/null +++ b/docs/deployment/upgrade.md @@ -0,0 +1,107 @@ +# Upgrade + +Upgrading SQLMonitor is **the same as installing it** — the installer is idempotent for object creation and updates DDL in place. The only difference is you want to target a specific subset of steps. + +## 1. Pull the latest code + +```powershell +cd C:\Code\GitHub\SQLMonitor +git fetch +git checkout dev # or 'main' if you pin there +git pull +``` + +## 2. Review the change log + +Skim the commits since your last upgrade: + +```powershell +git log --oneline ..HEAD -- SQLMonitor/ DDLs/ +``` + +Anything that touched `DDLs/SCH-Create-All-Objects.sql`, `SCH-usp_*.sql`, `SCH-Job-*.sql` or `SQLMonitor/*.ps1` is material. `DDLs/SCH-[dbo].[purge_table].sql` changes may add new tables to purge. + +## 3. Dry-run on a non-prod instance first + +```powershell +.\Private\Wrapper-InstallSQLMonitor.ps1 -DryRun $true -Verbose +``` + +Any step that would drop/recreate state (linked servers, WhoIsActive table, PowerShell jobs) announces itself. + +## 4. Apply to prod + +**Targeted upgrade** (recommended): re-run only the steps that changed. Example after a DDL-only change: + +```powershell +.\Private\Wrapper-InstallSQLMonitor.ps1 -OnlySteps '2__AllDatabaseObjects', '55__EnablePageCompression' -Verbose +``` + +**Full re-run** (safe, takes 5–15 min): no `-OnlySteps`. The installer will: + +- `CREATE OR ALTER` every proc, view and function. +- Re-register every `(dba) *` job, preserving schedule *if the schedule hasn't changed*. +- Re-create the XEvent session if its definition drifted. +- Skip First-Responder-Kit / Darling-Data / Ola re-install unless those zips changed. + +## 5. Upgrading the wrapper scripts + +`Wrapper-Samples/` evolves too. After pulling, **diff** your `Private/Wrapper-*.ps1` against the new `Wrapper-Samples/Wrapper-*.ps1`: + +```powershell +git diff --no-index Private\Wrapper-InstallSQLMonitor.ps1 Wrapper-Samples\Wrapper-InstallSQLMonitor.ps1 +``` + +Apply any new parameters or logic to your `Private/` copy. + +## 6. Upgrading Grafana dashboards + +Dashboards evolve independently of the T-SQL codebase. After pulling: + +1. In Grafana, **Export** your current dashboards with `Share → Export → Save to file` (back them up). +2. Import the new JSONs from `Grafana-Dashboards/`, letting Grafana overwrite by UID. +3. Re-apply any org-specific tags, variable defaults, or panel hacks you had. + +## 7. Upgrading the Alert Engine + +See `Alerting/Deployment-Instructions/alertengine-on-podman.sh`: + +```bash +cd Alerting +podman build -t sqlmonitor/alert-engine:latest . +podman stop alertengine +podman rm alertengine +podman run -d --name alertengine \ + -p 5001:5001 \ + --env-file /etc/sqlmonitor/alert-engine.env \ + sqlmonitor/alert-engine:latest +``` + +## 8. Upgrading sql_exporter / Prometheus + +See [Prometheus / sql_exporter](../prometheus.md) — `sql_exporter` collectors are plain YAML; most upgrades are: + +```bash +# pull new YAMLs +cd /opt/sql_exporter +cp -a /opt/sqlmonitor-repo/sql_exporter/*.yml . +systemctl restart sql_exporter +``` + +## Roll-back + +Because the installer uses `CREATE OR ALTER`, there is no automatic rollback. To revert: + +- Pin your repo to the previous commit (`git checkout `). +- Re-run the installer with `-OnlySteps '2__AllDatabaseObjects'` — this re-applies the older DDL. +- Schedule and job changes won't revert automatically — manually drop/re-create jobs whose schedules changed. + +## When a DDL change is destructive + +If a schema change needs to **drop and recreate** a table (rare), the installer will detect it and log: + +```text +[WARN] Table dbo.xevent_metrics schema drift detected. Dropping and recreating. +``` + +Losing the data in that table is usually acceptable (telemetry is re-populated within hours), but back up first if you are unsure. diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..7f39f99 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,98 @@ +--- +title: SQLMonitor +description: Open-source SQL Server monitoring & alerting built on SQL Agent jobs, Grafana and Python. +hide: + - navigation +--- + +# SQLMonitor + +**Open-source Microsoft SQL Server monitoring, baselining, and alerting — powered by SQL Agent jobs and Grafana.** + +SQLMonitor helps DBAs and developers understand *current load vs. usual load* across every SQL Server instance they operate — **DEV, TEST, UAT and PROD** — without the price tag of enterprise monitoring tools. + +
+ +- :material-view-dashboard-outline: **Live dashboards** + + Ready-to-import Grafana dashboards for server health, wait stats, query workload, disk space, backups, AGs, and more. + + [Browse dashboards →](dashboards/index.md) + +- :material-server-network: **SQL-Agent-native collection** + + Metric collection is plain SQL Agent jobs calling stored procedures — easy to read, debug and extend. + + [How it works →](architecture/index.md) + +- :material-download: **One-command deploy** + + A single PowerShell wrapper script onboards an instance end-to-end: objects, jobs, proxies, partitions, linked servers, logins. + + [Install SQLMonitor →](deployment/install.md) + +- :material-bell-alert-outline: **Alerting built-in** + + Python-based alert engine with routing to **PagerDuty, Slack and Email**. + + [Alerting guide →](alerting.md) + +
+ +--- + +## Live demo + +A fully-provisioned lab instance is available for you to click around: + +[:material-rocket-launch: Open live dashboard](https://sqlmonitor.ajaydwivedi.com/d/distributed_live_dashboard/monitoring-live-distributed?orgId=1&refresh=5s){ .md-button .md-button--primary } +[:material-presentation-play: Job Activity Monitor](https://sqlmonitor.ajaydwivedi.com/d/job_activity_monitor/monitoring-live-all-servers-job-activity-monitor){ .md-button } + +| Portal | URL | User | Password | +|---|---|---|---| +| Grafana | [sqlmonitor.ajaydwivedi.com](https://sqlmonitor.ajaydwivedi.com/dashboards?tag=sqlmonitor) | `guest` | `ajaydwivedi-guest` | +| SQL Instance | `sqlmonitor.ajaydwivedi.com:1433` | `grafana` | `grafana` | + +![Live dashboards montage](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor/Live-Dashboards-All.gif?raw=true) + +--- + +## What you get + +| Capability | What it looks like | +|---|---| +| **Core health metrics** — CPU, Memory, Disk, I/O, Waits, Transactions, Active Requests, Blocking | [Monitoring - Live - Distributed](https://sqlmonitor.ajaydwivedi.com/d/distributed_live_dashboard/monitoring-live-distributed) | +| **Quest-recommended Perfmon counters** across every instance | [Monitoring - Perfmon Counters](https://sqlmonitor.ajaydwivedi.com/d/distributed_perfmon/monitoring-perfmon-counters-quest-softwares-distributed) | +| **WhoIsActive workload** — long runners, blockers, top consumers | [WhoIsActive - Workload](https://sqlmonitor.ajaydwivedi.com/d/WhoIsActive) | +| **Extended Events workload** — normalized & hashed query trends | [XEvent - Workload](https://sqlmonitor.ajaydwivedi.com/d/XEvents) / [Trend](https://sqlmonitor.ajaydwivedi.com/d/XEvents-Trends) | +| **Blitz Server Health & BlitzIndex Analysis** | [Blitz Server Health](https://sqlmonitor.ajaydwivedi.com/d/t___Blitz_Server_Health_Analysis) / [BlitzIndex](https://sqlmonitor.ajaydwivedi.com/d/t___BlitzIndex_Analysis) | +| **Availability Group health**, **Backup History**, **Disk Space**, **File IO Stats** | [AG Health](https://sqlmonitor.ajaydwivedi.com/d/ag_health_state) / [Backups](https://sqlmonitor.ajaydwivedi.com/d/backup_history) / [Disk](https://sqlmonitor.ajaydwivedi.com/d/disk_space) / [File IO](https://sqlmonitor.ajaydwivedi.com/d/database_file_io_stats) | +| **Alerting** via **PagerDuty / Slack / Email** | [Alert Engine](alerting.md) | +| **Optional** Prometheus path via `sql_exporter` + Windows Exporter | [Prometheus](prometheus.md) | + +![Alert engine](https://github.com/imajaydwivedi/Images/blob/master/SQLMonitor-AlertEngine/Sma-Slack-3-Images-Gif.gif?raw=true) + +--- + +## 60-second elevator pitch + +!!! info "Why teams adopt SQLMonitor" + + - **No SaaS.** Everything runs on your own SQL Servers and Grafana. + - **Boring tech on purpose.** Just SQL Agent jobs, stored procedures, T-SQL tables, PowerShell and Python. Debug it like any other DBA asset. + - **Scales cheaply.** Hourly-partitioned tables with page compression. Memory-Optimized tables on the central inventory server. Dynamically parameterized Grafana queries so concurrent dashboard users do not thrash the monitor. + - **Flexible topology.** Distributed (each instance monitors itself) **or** Central (one inventory server polls everyone) — same codebase. + - **Broad SQL Server support.** All supported SQL Server versions (XEvent collection requires 2012+). + +--- + +## Where next? + +- New here? Start with **[Why SQLMonitor](why.md)** and **[Architecture](architecture/index.md)**. +- Ready to install? Jump to **[Deployment → Install Walkthrough](deployment/install.md)**. +- Looking for a specific dashboard? See the **[Dashboards Index](dashboards/index.md)**. +- Want to alert on something? See **[Alerting](alerting.md)**. + +[:fontawesome-brands-github: Source on GitHub](https://github.com/imajaydwivedi/SQLMonitor){ .md-button } +[:fontawesome-brands-youtube: YouTube Playlist](https://ajaydwivedi.com/youtube/sqlmonitor){ .md-button } +[:fontawesome-brands-slack: #sqlmonitor on Slack](https://ajaydwivedi.com/sqlmonitor/slack){ .md-button } diff --git a/docs/jobs.md b/docs/jobs.md new file mode 100644 index 0000000..1b6a9f5 --- /dev/null +++ b/docs/jobs.md @@ -0,0 +1,173 @@ +# Jobs & Collectors + +Every SQL instance under SQLMonitor — including the inventory server — runs a set of SQL Agent jobs in the **`(dba) SQLMonitor`** category. They are the entire data-collection plane: they poll the OS, DMVs, WMI, XEvents and Perfmon, and persist everything into the `DBA` database for Grafana and the alert engine to consume. + +## What you get out of the box + +- **46 named jobs** installed in the `(dba) SQLMonitor` category +- Grouped by purpose below (Collection, Aggregation, Ops/Health, Maintenance, Diagnostics) +- Every job is defined by a single `DDLs/SCH-Job-[*].sql` script — the installer simply runs them +- Schedules range from **every 10 seconds** (alert polling) to **weekly** (Blitz reports) +- Most jobs run as **CmdExec** under the `sqlmonitor` proxy — they shell out to `sqlcmd` so long-running T-SQL cannot pin the job worker thread + +## The per-instance vs. inventory split + +| Where it runs | What it does | +|---|---| +| **Every monitored instance** | The `Collect-*` family — polls local DMVs/OS into tables on that instance's local `DBA` database (per-instance tables like `dbo.wait_stats`, `dbo.file_io_stats`, `dbo.xevent_metrics`). | +| **Inventory server only** | The `Get-AllServer*` family — pulls from every linked server into the inventory's `dbo.*_all_servers` tables. This is what the Grafana dashboards read from. | +| **Inventory server only** | The `Run-Blitz*`, `Partitions-Maintenance`, `Purge-Tables`, dashboard-mail — fleet-wide diagnostics & housekeeping. | + +See [Architecture → Data Flow](architecture/data-flow.md) for the pull path. + +## Anatomy of a collector job + +Every `(dba) Collect-*` and `(dba) Get-AllServer*` job follows the same shape (by design — makes `Install-SQLMonitor.ps1` idempotent): + +```sql +-- DDLs/SCH-Job-[(dba) Collect-WaitStats].sql (excerpt) +EXEC msdb.dbo.sp_add_job @job_name = N'(dba) Collect-WaitStats', + @category_name = N'(dba) SQLMonitor', + @owner_login_name = N'sa', + @description = N'Collect cumulative wait stats on server into dbo.wait_stats.'; + +EXEC msdb.dbo.sp_add_jobstep @step_name = N'Collect Data', + @subsystem = N'CmdExec', + @proxy_name = N'sqlmonitor', -- standard SQLMonitor proxy + @command = N'sqlcmd -S $(ESCAPE_SQUOTE(SRVR)) -d DBA -E -Q "EXEC dbo.usp_collect_wait_stats"'; + +EXEC msdb.dbo.sp_add_jobschedule @freq_type = 4, -- daily + @freq_subday_type = 4, @freq_subday_interval = 10; -- every 10 minutes +``` + +That means: + +1. The **business logic lives in the stored procedure** (`dbo.usp_collect_wait_stats`), not in the job. Jobs are deliberately thin wrappers — you can re-run the underlying proc from SSMS whenever you like. +2. The **SQL Agent proxy `sqlmonitor`** is used instead of the SQL Agent service account. The proxy maps to a Windows credential with just enough rights to read Perfmon/Registry/Services. See [Deployment → Prerequisites](deployment/prerequisites.md). +3. The **schedule** is encoded in `freq_type`/`freq_subday_type`/`freq_subday_interval`. The tables below decode those into human form. + +## Monitoring the collectors themselves + +Jobs that are themselves not collecting on time will cause dashboards to go stale. SQLMonitor ships three self-monitors for that: + +| Self-monitor | What it does | +|---|---| +| `(dba) Check-SQLAgentJobs` | Writes every job's status into `dbo.sql_agent_jobs`. Feeds the **Job Activity Monitor** dashboard. | +| `(dba) Stop-StuckSQLMonitorJobs` | Kills & restarts any `(dba) *` job that has been running longer than its configured ceiling — usually because `sqlcmd` got stuck on a network hiccup. | +| `(dba) Get-AllServerCollectionLatencyInfo` | Populates `dbo.all_server_collection_latency_info` with per-table staleness, which drives the red "stale" indicators in the Distributed dashboard. | + +## Scheduling conventions + +The `Install-SQLMonitor.ps1` `-JobCollectionFrequency` parameter (see [Parameters](deployment/parameters.md)) can **uniformly increase** every 1-minute job to 2, 5, 10 or 30 minutes. Use this on larger fleets where the inventory server can't keep up with 1-minute pulls. Set it in your `Wrapper-Install-SQLMonitor.ps1` — not by editing individual `SCH-Job-*.sql` files. + +--- + +## Full job catalog + +Below is the authoritative list of all 46 jobs that `Install-SQLMonitor.ps1` creates, grouped by purpose. Links point at the exact `SCH-Job-*.sql` script the installer uses. + +### Collection + +| Job | Schedule | Subsystem | Script | Description | +|---|---|---|---|---| +| `(dba) Collect Login Expiration Info` | Once daily | CmdExec | [`SCH-Job-[(dba) Collect Login Expiration Info].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect%20Login%20Expiration%20Info].sql) | This job collects all info about SQL Server logins that are active on all servers, and populates them on dbo.all_server_ | +| `(dba) Collect-AgHealthState` | Every 2 minutes | CmdExec | [`SCH-Job-[(dba) Collect-AgHealthState].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-AgHealthState].sql) | Collect availability group health state. | +| `(dba) Collect-AllServerAlertMessages` | Every 10 seconds | CmdExec | [`SCH-Job-[(dba) Collect-AllServerAlertMessages].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-AllServerAlertMessages].sql) | Get Alert Messages from All Servers | +| `(dba) Collect-DiskSpace` | Every 30 minutes | CmdExec | [`SCH-Job-[(dba) Collect-DiskSpace-ManagedInstance].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-DiskSpace-ManagedInstance].sql) | Collect disk space on server into dbo.disk_space. | +| `(dba) Collect-DiskSpace` | Every 10 minutes | CmdExec | [`SCH-Job-[(dba) Collect-DiskSpace].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-DiskSpace].sql) | Get Disk Space info | +| `(dba) Collect-FileIOStats` | Every 10 minutes | CmdExec | [`SCH-Job-[(dba) Collect-FileIOStats].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-FileIOStats].sql) | Collect cumulative file io stats on server into dbo.file_io_stats. | +| `(dba) Collect-MemoryClerks` | Every 2 minutes | CmdExec | [`SCH-Job-[(dba) Collect-MemoryClerks].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-MemoryClerks].sql) | Collect top memory consumers on server into dbo.memory_clerks. | +| `(dba) Collect-OSProcesses` | Every 2 minutes | CmdExec | [`SCH-Job-[(dba) Collect-OSProcesses].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-OSProcesses].sql) | Get OS Processes CPU & Memory | +| `(dba) Collect-PerfmonData` | Every 30 seconds | CmdExec | [`SCH-Job-[(dba) Collect-PerfmonData-ManagedInstance].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-PerfmonData-ManagedInstance].sql) | Collect disk space on server into dbo.disk_space. | +| `(dba) Collect-PerfmonData` | Every 60 seconds | CmdExec | [`SCH-Job-[(dba) Collect-PerfmonData].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-PerfmonData].sql) | This job captures Perfmon data as per template https://github.com/imajaydwivedi/SqlServer-Baselining-Grafana/blob/maste | +| `(dba) Collect-PrivilegedInfo` | Every 10 minutes | CmdExec | [`SCH-Job-[(dba) Collect-PrivilegedInfo].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-PrivilegedInfo].sql) | Collect information that generally need Sysadmin access. | +| `(dba) Collect-WaitStats` | Every 10 minutes | CmdExec | [`SCH-Job-[(dba) Collect-WaitStats].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-WaitStats].sql) | Collect cumulative wait stats on server into dbo.wait_stats. | +| `(dba) Collect-XEvents` | Every 1 minute | CmdExec | [`SCH-Job-[(dba) Collect-XEvents].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Collect-XEvents].sql) | This job consumes data from XEvent [xevent_metrics] into dbo.xevent_metrics. | + +### Aggregation + +| Job | Schedule | Subsystem | Script | Description | +|---|---|---|---|---| +| `(dba) Get-AllServerAgHealthState` | Every 1 minute | CmdExec | [`SCH-Job-[(dba) Get-AllServerAgHealthState].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerAgHealthState].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.ag_health_state_all_servers https://ajaydwiv | +| `(dba) Get-AllServerAlertHistory` | Every 30 seconds | CmdExec | [`SCH-Job-[(dba) Get-AllServerAlertHistory].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerAlertHistory].sql) | This job execute procedure usp_GetAllServerCollectedData and populates in table dbo.alert_history_all_servers https://a | +| `(dba) Get-AllServerBackups` | Every 15 minutes | CmdExec | [`SCH-Job-[(dba) Get-AllServerBackups].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerBackups].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.backups_all_servers https://ajaydwivedi.com/ | +| `(dba) Get-AllServerCollectionLatencyInfo` | Every 15 minutes | CmdExec | [`SCH-Job-[(dba) Get-AllServerCollectionLatencyInfo].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerCollectionLatencyInfo].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.all_server_collection_latency_info https://a | +| `(dba) Get-AllServerDashboardMail` | Every 8 hours | CmdExec | [`SCH-Job-[(dba) Get-AllServerDashboardMail].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerDashboardMail].sql) | Send Mail for All Critical Metrics Every 8 Hours | +| `(dba) Get-AllServerDiskSpace` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Get-AllServerDiskSpace].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerDiskSpace].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.disk_space_all_servers https://ajaydwivedi.c | +| `(dba) Get-AllServerLogSpaceConsumers` | Every 1 minute | CmdExec | [`SCH-Job-[(dba) Get-AllServerLogSpaceConsumers].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerLogSpaceConsumers].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.log_space_consumers_all_servers https://ajay | +| `(dba) Get-AllServerServices` | Every 10 minutes | CmdExec | [`SCH-Job-[(dba) Get-AllServerServices].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerServices].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.services_all_servers https://ajaydwivedi.com | +| `(dba) Get-AllServerSqlAgentJobs` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Get-AllServerSqlAgentJobs].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerSqlAgentJobs].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.sql_agent_jobs_all_servers https://ajaydwive | +| `(dba) Get-AllServerStableInfo` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Get-AllServerStableInfo].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerStableInfo].sql) | This job execute procedure usp_GetAllServerInfo and populates tables dbo.all_server_stable_info & dbo.all_server_stable_ | +| `(dba) Get-AllServerTempdbSpaceUsage` | Every 1 minute | CmdExec | [`SCH-Job-[(dba) Get-AllServerTempdbSpaceUsage].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerTempdbSpaceUsage].sql) | This job execute procedure usp_GetAllServerInfo and populates in table dbo.tempdb_space_usage_all_servers https://ajayd | +| `(dba) Get-AllServerVolatileInfo` | Every 30 seconds | CmdExec | [`SCH-Job-[(dba) Get-AllServerVolatileInfo].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Get-AllServerVolatileInfo].sql) | This job execute procedure usp_GetAllServerInfo and populates tables dbo.all_server_volatile_info & dbo.all_server_volat | +| `(dba) Populate Inventory Tables` | Once daily | CmdExec | [`SCH-Job-[(dba) Populate Inventory Tables].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Populate%20Inventory%20Tables].sql) | This job populates Inventory tables using SQLMonitor data | + +### Ops/Health + +| Job | Schedule | Subsystem | Script | Description | +|---|---|---|---|---| +| `(dba) Capture-AlertMessages` | (no schedule) | TSQL | [`SCH-Job-[(dba) Capture-AlertMessages].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Capture-AlertMessages].sql) | Job is trigged through SQL Agent Alerts. Purpose: Collect Alert Messages in table [dbo].[alert_history]. https://ajayd | +| `(dba) Check-InstanceAvailability` | Every 2 minutes | CmdExec | [`SCH-Job-[(dba) Check-InstanceAvailability].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Check-InstanceAvailability].sql) | Check if SQL Instances are online. For additional validation, [DDLs\SCH-usp_ | +| `(dba) Check-SQLAgentJobs` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Check-SQLAgentJobs].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Check-SQLAgentJobs].sql) | Collect SQL Agent Job Stats. | +| `(dba) Send Login Expiry EMails` | Once daily | CmdExec | [`SCH-Job-[(dba) Send Login Expiry EMails].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Send%20Login%20Expiry%20EMails].sql) | This job executes procedure dbo.usp_send_login_expiry_emails, and send login password expiry email notification to owner | +| `(dba) Stop-StuckSQLMonitorJobs` | Every 1 hour | CmdExec | [`SCH-Job-[(dba) Stop-StuckSQLMonitorJobs].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Stop-StuckSQLMonitorJobs].sql) | This job stops/restarts any stuck SQLMonitor job. | +| `(dba) Test-WindowsAdminAccess` | (no schedule) | PowerShell | [`SCH-Job-[(dba) Test-WindowsAdminAccess].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Test-WindowsAdminAccess].sql) | This job is temporary for testing if Proxy is required for calling PowerShell scripts. https://ajaydwivedi.com/github/s | +| `(dba) Update-SQLMonitorIP` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Update-SQLMonitorIP].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Update-SQLMonitorIP].sql) | Update ip address of ajaydwivedi.ddns.net | +| `Update-SQLMonitorIPAddress` | Every 5 minutes | CmdExec | [`SCH-Job-[Update-SQLMonitorIPAddress].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[Update-SQLMonitorIPAddress].sql) | This job execute python script that keeps ip address updated for sqlmonitor.ajaydwivedi.com. https://github.com/imajaydw | + +### Maintenance + +| Job | Schedule | Subsystem | Script | Description | +|---|---|---|---|---| +| `(dba) Compute-AllServerVolatileInfoHistoryHourly` | Once daily | CmdExec | [`SCH-Job-[(dba) Compute-AllServerVolatileInfoHistoryHourly].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Compute-AllServerVolatileInfoHistoryHourly].sql) | Aggregate data from dbo.all_server_volatile_info_history and save it into hourly trend in table dbo.all_server_volatile_ | +| `(dba) Enable-PageCompression` | One-time | TSQL | [`SCH-Job-[(dba) Enable-PageCompression].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Enable-PageCompression].sql) | This job purges data from SQLMonitor tables mentioned in dbo.purge_table. | +| `(dba) Partitions-Maintenance` | Once daily | CmdExec | [`SCH-Job-[(dba) Partitions-Maintenance].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Partitions-Maintenance].sql) | This job takes care of creating new partitions and removing old partitions | +| `(dba) Purge-Tables` | Once daily | CmdExec | [`SCH-Job-[(dba) Purge-Tables].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Purge-Tables].sql) | This job purges data from SQLMonitor tables mentioned in dbo.purge_table. | +| `(dba) Remove-XEventFiles` | Every 30 minutes | CmdExec | [`SCH-Job-[(dba) Remove-XEventFiles].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Remove-XEventFiles].sql) | Remove files generated by XEvent [xevent_metrics] from disk. | +| `(dba) Update-SqlServerVersions` | Weekly (Wed,Fri) | CmdExec | [`SCH-Job-[(dba) Update-SqlServerVersions].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Update-SqlServerVersions].sql) | This job fetches script "https://raw.githubusercontent.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/dev/SqlServerVer | + +### Diagnostics + +| Job | Schedule | Subsystem | Script | Description | +|---|---|---|---|---| +| `(dba) Run-Blitz` | Weekly (Sat) | CmdExec | [`SCH-Job-[(dba) Run-Blitz].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Run-Blitz].sql) | Capture Overall Server Health Checks | +| `(dba) Run-BlitzIndex` | Once daily | CmdExec | [`SCH-Job-[(dba) Run-BlitzIndex].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Run-BlitzIndex].sql) | Capture index usage details into SQL Table . | +| `(dba) Run-BlitzIndex - Weekly` | Weekly (Sat) | CmdExec | [`SCH-Job-[(dba) Run-BlitzIndex - Weekly].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Run-BlitzIndex%20-%20Weekly].sql) | Capture index usage details into SQL Table . | +| `(dba) Run-LogSaver` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Run-LogSaver].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Run-LogSaver].sql) | Find what transactions are causing database log file to get full, and kill session if required. https://ajaydwivedi.com | +| `(dba) Run-TempDbSaver` | Every 5 minutes | CmdExec | [`SCH-Job-[(dba) Run-TempDbSaver].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Run-TempDbSaver].sql) | Find what is consuming space in tempdb, and kill the session if required. | +| `(dba) Run-WhoIsActive` | Every 2 minutes | CmdExec | [`SCH-Job-[(dba) Run-WhoIsActive].sql`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/DDLs/SCH-Job-[(dba)%20Run-WhoIsActive].sql) | Capture sp_WhoIsActive result into dbo.WhoIsActive. | + +_Total: 46 jobs in the `(dba) SQLMonitor` category._ + + +## Triggering jobs outside their schedule + +Every collector can be run manually: + +```sql +-- On the target instance +EXEC msdb.dbo.sp_start_job @job_name = N'(dba) Collect-WaitStats'; + +-- Or run the proc directly (same effect, faster iteration) +EXEC DBA.dbo.usp_collect_wait_stats; +``` + +For the inventory side, use `@sql_instance` to scope: + +```sql +EXEC DBA.dbo.usp_GetAllServerInfo @collection_name = N'volatile_info'; +``` + +## Disabling collectors + +If a collector is noisy for your environment, disable it rather than dropping it — that way `Install-SQLMonitor.ps1` will not recreate it on the next upgrade (the installer respects existing job enabled/disabled state): + +```sql +EXEC msdb.dbo.sp_update_job @job_name = N'(dba) Collect-XEvents', @enabled = 0; +``` + +## Related reading + +- [Deployment → Install Steps](deployment/steps.md) — maps each job-creation step name to the SQL file. +- [Architecture → Components](architecture/components.md) — where these jobs fit in the overall flow. +- [Alerting](alerting.md) — how `dbo.alert_history` is populated and consumed. diff --git a/docs/prometheus.md b/docs/prometheus.md new file mode 100644 index 0000000..d09cbee --- /dev/null +++ b/docs/prometheus.md @@ -0,0 +1,164 @@ +# Prometheus / sql_exporter Path + +SQLMonitor's classic architecture pulls everything into SQL Server tables. For shops that already run **Prometheus + Grafana** — or want lower-latency, cardinality-rich metrics — there is a parallel path that publishes the same signal set as Prometheus metrics via [burningalchemist/sql_exporter](https://github.com/burningalchemist/sql_exporter). + +The two paths are **complementary**, not mutually exclusive. Most of the Grafana dashboards can switch between a SQL datasource and a Prometheus datasource using the same panel queries wrapped in `$__interval`. + +## What you get + +| | | +|---|---| +| **Exporter** | `sql_exporter` (Go binary) running on every monitored instance on TCP **9399**. | +| **Scrape cadence** | 15s default (configurable in `prometheus.yml`). | +| **Collectors** | 7 YAML files under `sql_exporter/`, one per signal family. | +| **Prometheus dashboard** | `SQL-Exporter-Metrics-Dashboard-External.json` — drop-in replacement for "Monitoring — Live — All Servers". | +| **Alerting rules** | `sql_exporter/alert-engine/Alert-Rules-SQLMonitor.yaml` — Grafana unified alerting format. | + +## Files in the repo + +| File | Purpose | +|---|---| +| `sql_exporter.yml` | Top-level exporter config — targets, credentials, job groups. | +| `mssql_dba_cached.collector.yml` | Low-churn metrics cached server-side (disk space, databases, file IO). | +| `mssql_dba_regular.collector.yml` | The bread-and-butter volatile metrics (CPU, memory, active/blocked requests, wait stats). | +| `mssql_dba_stableinfo.collector.yml` | Instance- and host-level stable facts (SQL version, core count, RAM). | +| `mssql_dba_aghealth.collector.yml` | AG primary/secondary state, redo/log-send queue. | +| `mssql_dba_whoisactive.collector.yml` | Top concurrent queries — higher cardinality. | +| `mssql_sqlagent_jobs.collector.yml` | SQL Agent job status / outcome / duration / next-run / 24h-failure count from `msdb`. | +| `mssql_backup_history.collector.yml` | Per-(database, backup_type) last-time / size / duration / age from `msdb.dbo.backupset`. | +| `mssql_xevent.collector.yml` | 5-minute aggregates from `DBA.dbo.xevent_metrics` (guarded with an existence check; no-op where the XEvent collector proc isn't installed). | +| `mssql_standard.collector.yml` | The upstream [sql_exporter](https://github.com/burningalchemist/sql_exporter/tree/master/examples/mssql_standard) stock collector. | +| `windows_exporter_config.yml` | Matching config for [windows_exporter](https://github.com/prometheus-community/windows_exporter) — host-level CPU, disk, network. | +| `tessell-metrics.collector.yml` | Managed-instance variant (no `xp_cmdshell`). | + +Full metric reference: [`SQL-Exporter-Metrics-Documentation.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/sql_exporter/SQL-Exporter-Metrics-Documentation.md) and the [quick-ref](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/sql_exporter/SQL-Exporter-Metrics-QuickRef.md). + +## Install — Windows + +```powershell +# 1. Drop the binary + config in place +# (Download sql_exporter.exe from https://github.com/burningalchemist/sql_exporter/releases) +Copy-Item .\sql_exporter.exe C:\Program Files\sql_exporter\ +Copy-Item .\sql_exporter\*.yml C:\Program Files\sql_exporter\ + +# 2. Register as a service +New-Service -Name sql_exporter ` + -BinaryPathName 'C:\Program Files\sql_exporter\sql_exporter.exe --config.file C:\Program Files\sql_exporter\sql_exporter.yml' ` + -StartupType Automatic ` + -DisplayName 'SQL Exporter for Prometheus' + +# 3. Set the service logon account to the sqlmonitor proxy user, then start +Start-Service sql_exporter + +# 4. Open the scrape port +New-NetFirewallRule -DisplayName 'SQL Exporter (TCP/9399)' ` + -Direction Inbound -Protocol TCP -LocalPort 9399 -Action Allow + +# 5. Smoke test +Invoke-WebRequest http://localhost:9399/metrics -UseBasicParsing | Select-Object -ExpandProperty Content | Select-Object -First 20 +``` + +## Install — macOS (launchd) + +Full step-by-step (including plist and log rotation) is in [`README-sql_exporter.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/sql_exporter/README-sql_exporter.md). Summary: + +```bash +brew install prometheus grafana +sudo cp com.sql_exporter.plist /Library/LaunchDaemons/ +sudo launchctl load /Library/LaunchDaemons/com.sql_exporter.plist +curl http://localhost:9399/metrics | head +``` + +## Wire into Prometheus + +Add a job to `prometheus.yml`: + +```yaml +scrape_configs: + - job_name: sqlmonitor_mssql + scrape_interval: 15s + static_configs: + - targets: + - sqlnode-01.corp:9399 + - sqlnode-02.corp:9399 + - aghost-1a.corp:9399 + - aghost-1b.corp:9399 + relabel_configs: + - source_labels: [__address__] + regex: '([^:]+):.*' + target_label: sql_instance + replacement: '$1' +``` + +For AGs add `sqlmonitor_ag` with `scrape_interval: 30s` pointing at the same targets — the `mssql_dba_aghealth` collector is slightly heavier. + +## Wire into Grafana + +1. Add a Prometheus datasource named **`Prometheus`** (the dashboards query it by that exact name). +2. Import `sql_exporter/SQL-Exporter-Metrics-Dashboard-External.json` — this is the Prometheus counterpart of the SQL-backed distributed dashboard. +3. Optionally import the unified-alerting YAMLs from `sql_exporter/alert-engine/` — they define the same CPU / memory / wait / blocking alerts the Python engine provides, but run inside Grafana Alerting instead. + +## Prometheus-backed dashboard pack + +The repo ships a parallel set of Grafana dashboards under [`sql_exporter/Prometheus-Dashboards/`](https://github.com/imajaydwivedi/SQLMonitor/tree/dev/sql_exporter/Prometheus-Dashboards) that mirror the SQL-backed dashboards in [`Grafana-Dashboards/`](https://github.com/imajaydwivedi/SQLMonitor/tree/dev/Grafana-Dashboards) but source every panel from Prometheus metrics. + +Phase 1 ships 12 dashboards covering the fleet's core signal set: + +| UID | Title | Data panels | +|---|---|---:| +| `prom_core_metrics_trend` | Core Metrics - Trend | 9 | +| `prom_wait_stats` | Wait Stats | 4 | +| `prom_disk_space` | Disk Space | 5 | +| `prom_ag_health_state` | Ag Health State | 3 | +| `prom_sql_agent_jobs` | SQL Agent Jobs | 6 | +| `prom_backup_history` | Backup History | 6 | +| `prom_xevent_trend` | XEvent - Trend | 4 | +| `prom_database_file_io_stats` | Database File IO Stats | 12 | +| `prom_dba_inventory` | DBA Inventory | 6 (+8 deep-links) | +| `prom_monitoring_live_all_servers` | Monitoring - Live - All Servers | 15 (+6 deep-links) | +| `prom_monitoring_live_distributed` | Monitoring - Live - Distributed | 52 (+6 deep-links) | +| `prom_monitoring_perfmon_quest` | Monitoring - Perfmon Counters - Quest Softwares - Distributed | 51 (+4 deep-links) | + +Panels that depend on the SQLMonitor central inventory database (alert history, AG-vs-nonAG backup split, LAMA config-change deltas, `dm_os_memory_clerks` snapshot, tempdb/log_space cache tables, `sql_server_patching`) render as markdown **deep-link tiles** that jump back to the SQL-backed dashboard so every source section remains visible. + +### Regenerating the dashboards + +Each dashboard is built from a small Python spec: + +```bash +cd sql_exporter/Prometheus-Dashboards +python3 generate.py # rebuild every *.json +python3 generate.py backup # filter: rebuild only backup_history +python3 _tools/validate.py # structural + expr sanity check +``` + +High-fidelity PromQL patterns used across the specs: + +- `increase(metric[$__range])` — selective-duration deltas (File IO, Wait Stats). +- `@ end() offset $__range` — prior-window comparison tables (day-over-day). +- `quantile_over_time($percentile_q, (expr)[$trend_window:])` — percentile trends. +- `topk($top_n, sum by (...) (...))` — XEvent / wait-type / memory-consumer trends. +- `time() - timestamp(up == 1)` — data-collection-issue detection. + +See the folder's [`README.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/sql_exporter/Prometheus-Dashboards/README.md) for the full spec-driven workflow and Grafana API bulk-import snippet. + +## Choosing between the two paths + +| Consideration | SQL table path (classic) | Prometheus path | +|---|---|---| +| Installation on monitored hosts | SQL Agent jobs only | Requires `sql_exporter` + `windows_exporter` service | +| Retention | You decide (SQL partitioned tables, months-to-years) | Prometheus TSDB (weeks) or Mimir/Thanos for long-term | +| Query shape | Ad-hoc T-SQL, great for forensic deep dives | PromQL, great for fleet aggregates and alerting | +| Historical dashboards | Fast (pre-aggregated tables) | Fast (per-scrape time series) | +| WhoIsActive / Blitz | Native | Cardinality spike — run on a separate scrape job | +| Alerting | `Alerting/` Python engine | Grafana Alerting / Alertmanager | + +A common production topology uses **both**: the SQL path for long retention + forensic tooling, and Prometheus for low-latency fleet dashboards and PagerDuty-grade alert routing. + +## Migrating from Perfmon file-collection to sql_exporter + +Historically SQLMonitor captured host Perfmon counters into BLG files and parsed them server-side. That path is still available, but shops that already run Prometheus usually switch to `windows_exporter` + `sql_exporter` instead. The migration story (mapping old counters to Prometheus metric names) is tracked in [`Perfmon-SQL-Exporter-Migration-Progress.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/sql_exporter/Perfmon-SQL-Exporter-Migration-Progress.md). + +## PromQL quick-start + +A tutorial tailored to the SQLMonitor metric set lives at [`_README-PromQL-Querying.md`](https://github.com/imajaydwivedi/SQLMonitor/blob/dev/sql_exporter/_README-PromQL-Querying.md) — covers the common `rate()`, `increase()`, `topk()` patterns for wait stats, CPU, IOPS and AG health. diff --git a/docs/support.md b/docs/support.md new file mode 100644 index 0000000..01f36f0 --- /dev/null +++ b/docs/support.md @@ -0,0 +1,115 @@ +# Support + +## Community support (free) + +The fastest place to get help is the **`#sqlmonitor`** channel on the SQL Community Slack. + +[:fontawesome-brands-slack: Join `#sqlmonitor` on Slack](https://ajaydwivedi.com/sqlmonitor/slack){ .md-button .md-button--primary }   [:fontawesome-brands-slack: Request workspace invite](https://ajaydwivedi.com/join/slack){ .md-button } + +When asking a question please include: + +- SQL Server major version & edition (Standard/Enterprise) +- OS (Windows / Linux / managed instance) +- `Install-SQLMonitor.ps1` step where the error occurred (the script writes a step name before every block) +- The relevant snippet of the installer transcript — use `-Verbose` when re-running +- `SELECT @@VERSION` and the output of `DBA.dbo.usp_get_sma_health` + +For schema questions it also helps to paste the top ~50 rows of the affected table. + +## Paid / private support + +For dedicated consulting, customizations, or time-bound production help, reach out directly over Slack DM on the same workspace, or: + +- GitHub sponsors: +- Blog contact form: + +## Filing bugs + +Use [GitHub Issues](https://github.com/imajaydwivedi/SQLMonitor/issues) for: + +- Installer failures that are reproducible +- Dashboard panels showing wrong numbers +- SQL Agent jobs failing with the same error repeatedly +- Typos / documentation fixes + +Please **don't** file an issue for "how do I…" — use Slack for that; GitHub Issues is best for defects that have a fix. + +### Good issue template + +```markdown +**SQL Server version:** 2019 Enterprise 15.0.4316.3 +**OS:** Windows Server 2022 +**SQLMonitor commit:** +**Step that failed:** `08__CreateProc_usp_collect_wait_stats` + +**What I expected:** +Step completes and creates `dbo.usp_collect_wait_stats`. + +**What happened:** +Msg 102, Level 15, State 1, Procedure usp_collect_wait_stats, Line 42 +Incorrect syntax near '...'. + +**Reproduction:** +1. Fresh SQL 2019 Enterprise install +2. Copy `Wrapper-Samples\Wrapper-InstallSQLMonitor.ps1` to `Private\` +3. Run with `-OnlySteps '08__CreateProc_usp_collect_wait_stats'` + +**Installer transcript:** +``` + +## Frequently asked questions + +### Does it support Azure SQL Database / Managed Instance? + +Azure SQL **Managed Instance** is supported — the installer detects it and skips steps that require OS-level access (xp_cmdshell, Perfmon files, XEvent files on disk). Use the `*-ManagedInstance.sql` variants. + +Azure SQL **Database** (PaaS) is *not* supported by the collector side (no SQL Agent jobs on DB-as-a-service). You can still add it as a target for the **Prometheus path** using `sql_exporter`. + +### Can I point all my instances at an Azure SQL Database inventory? + +No. The inventory server is read/written by SQL Agent jobs across every monitored instance — it needs cross-database queries, linked servers, and a SQL Agent. Use a VM or Managed Instance as the inventory. + +### How much overhead does the collection add? + +Under 1% CPU on a typical OLTP workload. The single most expensive collector is `Collect-XEvents` (which parses the query workload XEvent file) — if you are near capacity, reduce its frequency or disable it. + +### Does it work on SQL Server 2008 R2? + +Partially. `dbo.xevent_metrics` is unavailable (no XEvents on 2008R2). Everything else — wait stats, Perfmon, Blitz, WhoIsActive — works. The installer detects the version and skips unsupported steps. + +### Can I use my own Grafana / does it need to be on the inventory? + +Any Grafana 9.x+ works. Grafana is usually run on the inventory host for network-locality, but you can run it anywhere that can reach the inventory SQL port (1433 by default) and the `sql_exporter` ports (9399) if you use the Prometheus path. + +### How do I update an existing deployment? + +`git pull` + re-run your `Wrapper-Install-SQLMonitor.ps1`. The installer is idempotent — every step checks before it changes anything. See [Deployment → Upgrade](deployment/upgrade.md). + +### The dashboards show "no data" for one instance — what do I check? + +1. `dbo.all_server_collection_latency_info` on the inventory — is the instance listed and recent? +2. `dbo.sql_agent_jobs_all_servers` — are the `(dba) *` jobs succeeding on that instance? +3. Linked server test: `SELECT TOP 1 * FROM [MyInstance].DBA.sys.tables;` +4. Grafana variable `sql_instance` — does it include the instance? (Common cause: instance not yet in `dbo.sma_sql_instance`.) + +See [Deployment → Troubleshooting](deployment/troubleshooting.md) for the full decision tree. + +## Related links + +- **GitHub Repo:** +- **Live Demo:** — credentials on the [Live Demo](dashboards/live-demo.md) page +- **YouTube Playlist:** +- **Blog Posts:** +- **Slack Community:** + +## Contributing + +PRs welcome. Please: + +1. Branch off `dev`, not `main`. +2. Keep collector changes backwards-compatible — the installer is idempotent and upgrade paths must not break existing deployments. +3. Add/update the corresponding `SCH-Job-*.sql` alongside any new stored procedure. +4. If you change a dashboard, export it via Grafana's “Share → Export → Save to file” so we get the deterministic JSON. +5. Update `mkdocs.yml` and `docs/*.md` for any user-visible change. + +See the repo [`CONTRIBUTING` guidance in the wiki](https://github.com/imajaydwivedi/SQLMonitor) for the full checklist. diff --git a/docs/why.md b/docs/why.md new file mode 100644 index 0000000..3020ecd --- /dev/null +++ b/docs/why.md @@ -0,0 +1,74 @@ +# Why SQLMonitor? + +SQLMonitor was built to solve a practical problem: **DBA teams need to see, correlate and alert on SQL Server behavior across every environment — without paying per-instance licenses** and without locking themselves into a SaaS that cannot see their on-prem DEV/UAT clusters. + +## The problem + +Existing monitoring products typically fall into one of three buckets: + +- **Enterprise-priced tools** (SolarWinds DPA, Quest Spotlight, Redgate SQL Monitor, SentryOne/SolarWinds). Excellent, but per-instance licensing usually means you only buy coverage for PROD — which is exactly where you *least* want to tune under fire, because you have no baseline from non-prod to compare against. +- **Generic APMs** (Datadog, New Relic). They surface *some* SQL metrics, but rarely the DBA-specific diagnostics (wait stats, WhoIsActive-style live workload, BlitzIndex recommendations, AG health, file IO latency bucketed per drive). +- **Home-grown scripts.** Every team has them. They tend to be undocumented, not partitioned, not purged, and no one knows which server runs the scheduler. + +SQLMonitor is a fourth option: an **open, script-first** toolkit that: + +- monitors **every** environment (DEV/UAT/QA/PROD) because there is no per-instance cost, +- uses **SQL-Agent-native** collection so any DBA can read, debug and extend it, +- ships with **ready-made Grafana dashboards** for the things DBAs actually look at. + +## Design principles + +### 1. Boring, legible tech + +Collection is SQL Agent jobs calling stored procedures. Visualization is Grafana reading SQL Server directly (or, optionally, Prometheus via [`burningalchemist/sql_exporter`][sqlexp]). No agents, no collectors-of-collectors, no proprietary binary protocols. + +### 2. Minimal footprint on the monitored instance + +- Perfmon data is captured to **small BLG/XML files** and bulk-loaded — not a polling T-SQL query storm. +- Extended Events sessions use **small file targets** that are consumed and removed by `(dba) Collect-XEvents` + `(dba) Remove-XEventFiles`. +- Query text is **normalized and hashed** by a CLR assembly (`TSQLTextNormalizer`) so we store one row per logical query, not per parameter variant. +- Tables are **hourly-partitioned and page-compressed** (where the edition supports it). Purging is declarative in `dbo.purge_table`. + +### 3. Scales through data-engineering, not hardware + +- Core stability metrics land in **Memory-Optimized tables** on the inventory server. +- Grafana panel queries are **dynamically parameterized T-SQL** so they use the same execution plans regardless of `$__timeFilter` range — letting the plan cache actually work when 20 people open dashboards at once. +- Data destination is decoupled: the instance being baselined can push to a *different* SQL instance of your choice. Large fleets push to a dedicated observability SQL server. + +### 4. One switch between Central and Distributed + +SQLMonitor supports both [topologies](architecture/topology.md) with the same codebase. Small shops use **Distributed** (each instance monitors itself, a single inventory server aggregates). Large fleets use **Central** with all jobs running on the observability server. Same jobs, same tables, same dashboards. + +### 5. Alert on what the dashboard shows + +The [Alert Engine](alerting.md) reads the same tables the dashboards read. If you can graph it, you can alert on it. + +## What SQLMonitor is *not* + +- **Not a SaaS.** You host it. That is the point. +- **Not a replacement for Brent Ozar's [First Responder Kit][fr]** — SQLMonitor *orchestrates* `sp_Blitz`, `sp_BlitzIndex`, `sp_WhoIsActive` and persists their output; it does not re-implement them. +- **Not a silver bullet for badly-tuned queries.** It tells you *where* to look; tuning is still your job. + +## When NOT to use SQLMonitor + +- You only have one SQL instance and already pay for Redgate / Quest / Datadog and are happy with it — don't switch for the sake of it. +- You are on Linux SQL Server with no Windows host anywhere in the picture — several collectors (Perfmon BLG ingestion, OS process capture) rely on Windows PowerShell. The SQL-only collectors still work, but some dashboards will be empty. +- You want **zero code on the monitored instance**. SQLMonitor installs objects into the `DBA` database on every instance. + +## Feature summary + +- :material-check: Distributed **or** Central topology — same repo +- :material-check: Ships 17+ production-ready Grafana dashboards +- :material-check: Perfmon + XEvent + WaitStats + FileIO + MemoryClerks + AG + Backups + DiskSpace + WhoIsActive + OS Processes +- :material-check: BrentOzar First Responder Kit (sp_Blitz / sp_BlitzIndex / sp_BlitzCache) scheduled & persisted +- :material-check: Darling Data (`sp_HumanEvents`, etc.) bundled +- :material-check: Ola Hallengren Maintenance Solution bundled +- :material-check: Python alert engine with **PagerDuty / Slack / Email** routing +- :material-check: Optional Prometheus path via `sql_exporter` + `windows_exporter` +- :material-check: CLR-based **T-SQL text normalizer** for hashed workload views +- :material-check: Automatic hourly partitioning & page compression +- :material-check: Declarative purging via `dbo.purge_table` +- :material-check: Optional **AI agent** (Ollama + LangChain) over the collected data + +[sqlexp]: https://github.com/burningalchemist/sql_exporter +[fr]: https://www.brentozar.com/first-aid/ diff --git a/hooks/external_links.py b/hooks/external_links.py new file mode 100644 index 0000000..f7b9a1f --- /dev/null +++ b/hooks/external_links.py @@ -0,0 +1,50 @@ +"""MkDocs hook: open external links in a new tab. + +Rewrites every rendered ```` whose target host is outside the +documentation's own ``site_url`` to include ``target="_blank"`` and +``rel="noopener noreferrer"``. + +Internal links (relative URLs, fragments, and links that point at the +configured ``site_url`` host) are left alone so in-site navigation keeps its +single-tab behaviour. +""" +from __future__ import annotations + +import re +from urllib.parse import urlparse + + +_ANCHOR_RE = re.compile(r"]*?)href=\"([^\"]+)\"([^>]*)>", re.IGNORECASE) + + +def _is_external(href: str, site_host: str) -> bool: + if not href: + return False + if href.startswith(("#", "/", "mailto:", "tel:", "javascript:")): + return False + parsed = urlparse(href) + if not parsed.scheme or not parsed.netloc: + return False + if parsed.scheme not in ("http", "https"): + return False + if site_host and parsed.netloc.lower() == site_host.lower(): + return False + return True + + +def _rewrite(match: "re.Match[str]", site_host: str) -> str: + pre, href, post = match.group(1), match.group(2), match.group(3) + attrs = f"{pre}{post}" + if not _is_external(href, site_host): + return match.group(0) + if re.search(r"\btarget\s*=", attrs, flags=re.IGNORECASE): + return match.group(0) + return ( + f'' + ) + + +def on_page_content(html: str, page, config, files): # noqa: D401, ARG001 + site_url = config.get("site_url") or "" + site_host = urlparse(site_url).netloc if site_url else "" + return _ANCHOR_RE.sub(lambda m: _rewrite(m, site_host), html) diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..bdf36fe --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,130 @@ +site_name: SQLMonitor +site_description: Open-source SQL Server monitoring & alerting built on SQL Agent jobs, Grafana, Prometheus and Python. +site_url: https://imajaydwivedi.github.io/SQLMonitor/ +site_author: Ajay Kumar Dwivedi +repo_url: https://github.com/imajaydwivedi/SQLMonitor +repo_name: imajaydwivedi/SQLMonitor +edit_uri: edit/dev/docs/ +copyright: Copyright © Ajay Kumar Dwivedi + +docs_dir: docs +site_dir: site + +theme: + name: material + logo: assets/logo.svg + favicon: assets/favicon.svg + icon: + repo: fontawesome/brands/github + palette: + - media: "(prefers-color-scheme: light)" + scheme: default + primary: indigo + accent: indigo + toggle: + icon: material/weather-night + name: Switch to dark mode + - media: "(prefers-color-scheme: dark)" + scheme: slate + primary: indigo + accent: indigo + toggle: + icon: material/weather-sunny + name: Switch to light mode + features: + - navigation.instant + - navigation.tracking + - navigation.tabs + - navigation.sections + - navigation.top + - navigation.indexes + - navigation.footer + - toc.follow + - search.suggest + - search.highlight + - search.share + - content.code.copy + - content.code.annotate + - content.action.edit + - content.tabs.link + +markdown_extensions: + - admonition + - attr_list + - def_list + - footnotes + - md_in_html + - tables + - toc: + permalink: true + toc_depth: 3 + - pymdownx.details + - pymdownx.highlight: + anchor_linenums: true + line_spans: __span + pygments_lang_class: true + - pymdownx.inlinehilite + - pymdownx.snippets + - pymdownx.superfences: + custom_fences: + - name: mermaid + class: mermaid + format: !!python/name:pymdownx.superfences.fence_code_format + - pymdownx.tabbed: + alternate_style: true + - pymdownx.tasklist: + custom_checkbox: true + - pymdownx.emoji: + emoji_index: !!python/name:material.extensions.emoji.twemoji + emoji_generator: !!python/name:material.extensions.emoji.to_svg + +plugins: + - search + - glightbox: + touchNavigation: true + zoomable: true + - minify: + minify_html: true + +hooks: + - hooks/external_links.py + +extra: + social: + - icon: fontawesome/brands/github + link: https://github.com/imajaydwivedi/SQLMonitor + - icon: fontawesome/brands/youtube + link: https://ajaydwivedi.com/youtube/sqlmonitor + - icon: fontawesome/brands/slack + link: https://ajaydwivedi.com/sqlmonitor/slack + - icon: fontawesome/brands/linkedin + link: https://www.linkedin.com/in/imajaydwivedi/ + generator: false + +nav: + - Home: index.md + - Why SQLMonitor: why.md + - Architecture: + - architecture/index.md + - Topology: architecture/topology.md + - Data Flow: architecture/data-flow.md + - Components: architecture/components.md + - Data Model: architecture/data-model.md + - Deployment: + - deployment/index.md + - Prerequisites: deployment/prerequisites.md + - Install Walkthrough: deployment/install.md + - Install-SQLMonitor Parameters: deployment/parameters.md + - Install Steps: deployment/steps.md + - Upgrade: deployment/upgrade.md + - Remove: deployment/remove.md + - Troubleshooting: deployment/troubleshooting.md + - Dashboards: + - dashboards/index.md + - Variables & Conventions: dashboards/variables.md + - Live Demo: dashboards/live-demo.md + - Jobs & Collectors: jobs.md + - Alerting: alerting.md + - Prometheus / sql_exporter: prometheus.md + - AI Agent: ai-agent.md + - Support & FAQ: support.md diff --git a/sql_exporter/CAdvisor-Container-Resource-Monitoring.json b/sql_exporter/CAdvisor-Container-Resource-Monitoring.json new file mode 100644 index 0000000..d44f1f8 --- /dev/null +++ b/sql_exporter/CAdvisor-Container-Resource-Monitoring.json @@ -0,0 +1,1325 @@ +{ + "__inputs": [ + { + "name": "DS_PROMETHEUS", + "label": "prometheus", + "description": "", + "type": "datasource", + "pluginId": "prometheus", + "pluginName": "Prometheus" + } + ], + "__elements": {}, + "__requires": [ + { + "type": "grafana", + "id": "grafana", + "name": "Grafana", + "version": "12.4.1" + }, + { + "type": "datasource", + "id": "prometheus", + "name": "Prometheus", + "version": "1.0.0" + }, + { + "type": "panel", + "id": "stat", + "name": "Stat", + "version": "" + }, + { + "type": "panel", + "id": "timeseries", + "name": "Time series", + "version": "" + } + ], + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 1, + "links": [], + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current CPU utilization for SQL Server as a percentage of the configured container CPU quota.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "red", + "value": 90 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 0, + "y": 0 + }, + "id": 1, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * sum(rate(container_cpu_usage_seconds_total{job=\"cadvisor\",container_name=\"sqlserver\",cpu=\"total\"}[$__rate_interval])) / sum(container_spec_cpu_quota{job=\"cadvisor\",container_name=\"sqlserver\"} / container_spec_cpu_period{job=\"cadvisor\",container_name=\"sqlserver\"})", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "SQL Server CPU %", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current CPU utilization for Postgres as a percentage of the configured container CPU quota.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "red", + "value": 90 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 8, + "y": 0 + }, + "id": 2, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * sum(rate(container_cpu_usage_seconds_total{job=\"cadvisor\",container_name=\"postgres\",cpu=\"total\"}[$__rate_interval])) / sum(container_spec_cpu_quota{job=\"cadvisor\",container_name=\"postgres\"} / container_spec_cpu_period{job=\"cadvisor\",container_name=\"postgres\"})", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "Postgres CPU %", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current CPU utilization for MongoDB as a percentage of the configured container CPU quota.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "red", + "value": 90 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 16, + "y": 0 + }, + "id": 3, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * sum(rate(container_cpu_usage_seconds_total{job=\"cadvisor\",container_name=\"mongodb\",cpu=\"total\"}[$__rate_interval])) / sum(container_spec_cpu_quota{job=\"cadvisor\",container_name=\"mongodb\"} / container_spec_cpu_period{job=\"cadvisor\",container_name=\"mongodb\"})", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "MongoDB CPU %", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current working set memory for SQL Server.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 0, + "y": 4 + }, + "id": 4, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum(container_memory_working_set_bytes{job=\"cadvisor\",container_name=\"sqlserver\"})", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "SQL Server Memory", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current working set memory for Postgres.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 8, + "y": 4 + }, + "id": 5, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum(container_memory_working_set_bytes{job=\"cadvisor\",container_name=\"postgres\"})", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "Postgres Memory", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current working set memory for MongoDB.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 16, + "y": 4 + }, + "id": 6, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum(container_memory_working_set_bytes{job=\"cadvisor\",container_name=\"mongodb\"})", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "MongoDB Memory", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current combined disk read + write throughput for SQL Server.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "Bps" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 0, + "y": 8 + }, + "id": 7, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum(rate(container_blkio_device_usage_total{job=\"cadvisor\",container_name=\"sqlserver\",operation=~\"Read|Write\"}[$__rate_interval]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "SQL Server Disk I/O", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current combined disk read + write throughput for Postgres.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "Bps" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 8, + "y": 8 + }, + "id": 8, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum(rate(container_blkio_device_usage_total{job=\"cadvisor\",container_name=\"postgres\",operation=~\"Read|Write\"}[$__rate_interval]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "Postgres Disk I/O", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current combined disk read + write throughput for MongoDB.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "Bps" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 8, + "x": 16, + "y": 8 + }, + "id": 9, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum(rate(container_blkio_device_usage_total{job=\"cadvisor\",container_name=\"mongodb\",operation=~\"Read|Write\"}[$__rate_interval]))", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "MongoDB Disk I/O", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "CPU utilization for each database container as a percentage of its configured CPU quota.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 4, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 0, + "y": 12 + }, + "id": 10, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max", + "mean" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * sum by (container_name) (rate(container_cpu_usage_seconds_total{job=\"cadvisor\",container_name=~\"$Container\",cpu=\"total\"}[$__rate_interval])) / sum by (container_name) (container_spec_cpu_quota{job=\"cadvisor\",container_name=~\"$Container\"} / container_spec_cpu_period{job=\"cadvisor\",container_name=~\"$Container\"})", + "legendFormat": "{{container_name}}", + "range": true, + "refId": "A" + } + ], + "title": "CPU Utilization % of Limit", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Actual CPU consumption rate in cores used by each database container.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 4, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 8, + "y": 12 + }, + "id": 11, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max", + "mean" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum by (container_name) (rate(container_cpu_usage_seconds_total{job=\"cadvisor\",container_name=~\"$Container\",cpu=\"total\"}[$__rate_interval]))", + "legendFormat": "{{container_name}}", + "range": true, + "refId": "A" + } + ], + "title": "CPU Used Cores", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Current working set memory used by each database container. No memory limit is configured right now, so working set is shown instead of percent of limit.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 4, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 16, + "y": 12 + }, + "id": 12, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max", + "mean" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum by (container_name) (container_memory_working_set_bytes{job=\"cadvisor\",container_name=~\"$Container\"})", + "legendFormat": "{{container_name}}", + "range": true, + "refId": "A" + } + ], + "title": "Memory Working Set", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Per-container disk read throughput from cAdvisor blkio metrics.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 4, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "Bps" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 0, + "y": 20 + }, + "id": 13, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max", + "mean" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum by (container_name) (rate(container_blkio_device_usage_total{job=\"cadvisor\",container_name=~\"$Container\",operation=\"Read\"}[$__rate_interval]))", + "legendFormat": "{{container_name}}", + "range": true, + "refId": "A" + } + ], + "title": "Disk Read Throughput", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Per-container disk write throughput from cAdvisor blkio metrics.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 4, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "Bps" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 8, + "y": 20 + }, + "id": 14, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max", + "mean" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum by (container_name) (rate(container_blkio_device_usage_total{job=\"cadvisor\",container_name=~\"$Container\",operation=\"Write\"}[$__rate_interval]))", + "legendFormat": "{{container_name}}", + "range": true, + "refId": "A" + } + ], + "title": "Disk Write Throughput", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Combined read + write throughput for each database container.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 4, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "never", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + } + ] + }, + "unit": "Bps" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 16, + "y": 20 + }, + "id": 15, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max", + "mean" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sum by (container_name) (rate(container_blkio_device_usage_total{job=\"cadvisor\",container_name=~\"$Container\",operation=~\"Read|Write\"}[$__rate_interval]))", + "legendFormat": "{{container_name}}", + "range": true, + "refId": "A" + } + ], + "title": "Total Disk Throughput", + "type": "timeseries" + } + ], + "preload": false, + "refresh": "30s", + "schemaVersion": 42, + "tags": [ + "Docker", + "cAdvisor", + "Prometheus", + "Containers", + "SQLMonitor" + ], + "templating": { + "list": [ + { + "current": { + "text": "", + "value": "${DS_PROMETHEUS}", + "selected": true + }, + "description": "Prometheus data source for cAdvisor metrics", + "hide": 2, + "includeAll": false, + "label": "Data Source", + "name": "DS_PROMETHEUS", + "options": [], + "query": "prometheus", + "refresh": 1, + "regex": "", + "type": "datasource" + }, + { + "allValue": "sqlserver|postgres|mongodb", + "current": { + "selected": true, + "text": [ + "All" + ], + "value": [ + "$__all" + ] + }, + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "definition": "label_values(container_memory_working_set_bytes{job=\"cadvisor\",container_name!=\"\"}, container_name)", + "description": "Filter trend panels to one or more database containers.", + "includeAll": true, + "label": "Container", + "multi": true, + "name": "Container", + "options": [], + "query": "label_values(container_memory_working_set_bytes{job=\"cadvisor\",container_name!=\"\"}, container_name)", + "refresh": 1, + "regex": "/^(sqlserver|postgres|mongodb)$/", + "regexApplyTo": "value", + "sort": 1, + "type": "query" + } + ] + }, + "time": { + "from": "now-6h", + "to": "now" + }, + "timepicker": {}, + "timezone": "browser", + "title": "Docker Container Resource Monitoring", + "uid": "cadvisor-container-res-v1", + "version": 1, + "weekStart": "" +} \ No newline at end of file diff --git a/sql_exporter/Prometheus-Dashboards/.gitignore b/sql_exporter/Prometheus-Dashboards/.gitignore new file mode 100644 index 0000000..7a60b85 --- /dev/null +++ b/sql_exporter/Prometheus-Dashboards/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc diff --git a/sql_exporter/Prometheus-Dashboards/SQL-Exporter-Metrics-Dashboard-External.json b/sql_exporter/Prometheus-Dashboards/SQL-Exporter-Metrics-Dashboard-External.json new file mode 100644 index 0000000..35ccfa4 --- /dev/null +++ b/sql_exporter/Prometheus-Dashboards/SQL-Exporter-Metrics-Dashboard-External.json @@ -0,0 +1,9731 @@ +{ + "__inputs": [ + { + "name": "DS_PROMETHEUS", + "label": "prometheus", + "description": "", + "type": "datasource", + "pluginId": "prometheus", + "pluginName": "Prometheus" + }, + { + "name": "VAR_TOP_K_WAITS", + "type": "constant", + "label": "TOP_K_WAITS", + "value": "12", + "description": "" + } + ], + "__elements": {}, + "__requires": [ + { + "type": "grafana", + "id": "grafana", + "name": "Grafana", + "version": "12.4.1" + }, + { + "type": "datasource", + "id": "prometheus", + "name": "Prometheus", + "version": "1.0.0" + }, + { + "type": "panel", + "id": "stat", + "name": "Stat", + "version": "" + }, + { + "type": "panel", + "id": "table", + "name": "Table", + "version": "" + }, + { + "type": "panel", + "id": "timeseries", + "name": "Time series", + "version": "" + } + ], + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 1, + "links": [], + "panels": [ + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 0 + }, + "id": 1, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "mssql_sqlserver_uptime_seconds => SQL Server uptime in seconds since the SQL Server service started.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "#6ED0E0", + "value": 5184000 + }, + { + "color": "#EAB839", + "value": 7776000 + }, + { + "color": "red", + "value": 10368000 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 0, + "y": 75 + }, + "id": 66, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "mssql_sqlserver_uptime_seconds{instance=\"$Server\"}", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "SQL Uptime", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "mssql_os_uptime_seconds => Operating system uptime in seconds since the host booted.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "#6ED0E0", + "value": 5184000 + }, + { + "color": "#EAB839", + "value": 7776000 + }, + { + "color": "red", + "value": 10368000 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 3, + "y": 75 + }, + "id": 67, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "mssql_os_uptime_seconds{instance=\"$Server\"}", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "OS Uptime", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_user_connections: Current user connections from SQL Server general statistics.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 6, + "y": 75 + }, + "id": 4, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_user_connections{instance=\"$Server\"}", + "refId": "A" + } + ], + "title": "User Connections", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_batch_requests: Number of command batches received.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 9, + "y": 75 + }, + "id": 5, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "sum(rate(mssql_batch_requests{instance=\"$Server\"}[$__rate_interval]))", + "refId": "A" + } + ], + "title": "Batch Req/sec", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_cpu_utilization_percentage: CPU utilization percentages from the SQL Server scheduler monitor ring buffer.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "max": 100, + "min": 0, + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "red", + "value": 90 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 12, + "y": 75 + }, + "id": 6, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_cpu_utilization_percentage{instance=\"$Server\",scope=\"sqlserver\"}", + "refId": "A" + } + ], + "title": "SQL CPU %", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_memory_utilization_percentage: The percentage of committed memory that is in the working set.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "max": 100, + "min": 0, + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "yellow", + "value": 75 + }, + { + "color": "red", + "value": 90 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 15, + "y": 75 + }, + "id": 7, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_memory_utilization_percentage{instance=\"$Server\"}", + "refId": "A" + } + ], + "title": "Memory Util %", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_page_life_expectancy_seconds: The minimum number of seconds a page will stay in the buffer pool on this node without references.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "red", + "value": 0 + }, + { + "color": "yellow", + "value": 300 + }, + { + "color": "green", + "value": 1000 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 18, + "y": 75 + }, + "id": 8, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_page_life_expectancy_seconds{instance=\"$Server\"}", + "refId": "A" + } + ], + "title": "PLE (sec)", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_percent_log_used: Transaction log space used as a percentage per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "max": 100, + "min": 0, + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "yellow", + "value": 70 + }, + { + "color": "red", + "value": 90 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 4, + "w": 3, + "x": 21, + "y": 75 + }, + "id": 9, + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "max(mssql_database_percent_log_used{instance=\"$Server\"})", + "refId": "A" + } + ], + "title": "Max Log Used %", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_batch_requests: Number of command batches received.\n- mssql_user_connections: Current user connections from SQL Server general statistics.\n- mssql_sql_attentions_total: SQL attentions since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 392 + }, + "id": 10, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_batch_requests{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Batch req/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_user_connections{instance=\"$Server\"}", + "legendFormat": "User connections", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_sql_attentions_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "SQL attentions/sec", + "refId": "C" + } + ], + "title": "Workload Quick Trend", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_cpu_utilization_percentage: CPU utilization percentages from the SQL Server scheduler monitor ring buffer.\n- mssql_memory_utilization_percentage: The percentage of committed memory that is in the working set.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 392 + }, + "id": 11, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_cpu_utilization_percentage{instance=\"$Server\"}", + "legendFormat": "{{scope}} CPU %", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_memory_utilization_percentage{instance=\"$Server\"}", + "legendFormat": "Memory util %", + "refId": "B" + } + ], + "title": "CPU / Memory Quick Trend", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_processes_blocked: Currently blocked processes.\n- mssql_deadlocks: Number of lock requests that resulted in a deadlock.\n- mssql_database_percent_log_used: Transaction log space used as a percentage per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 392 + }, + "id": 12, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_processes_blocked{instance=\"$Server\"}", + "legendFormat": "Blocked processes", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_deadlocks{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Deadlocks/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "max(mssql_database_percent_log_used{instance=\"$Server\"})", + "legendFormat": "Max DB log used %", + "refId": "C" + } + ], + "title": "Blocking / Deadlocks / Log Pressure", + "type": "timeseries" + } + ], + "title": "Overview @ ${local_time_seconds:date:YYYY-MM-DD HH:mm:ss}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 1 + }, + "id": 13, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_up: SQL Server instance availability.\n- mssql_service_info: SQL Server service availability and metadata.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 6, + "x": 0, + "y": 76 + }, + "id": 14, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_up{instance=\"$Server\"}", + "legendFormat": "SQL Instance", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_service_info{instance=\"$Server\"}", + "legendFormat": "{{service_name_str}}", + "refId": "A" + } + ], + "title": "Instance & Service Availability", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_log_apply_pending_queue: Log apply pending queue.\n- mssql_log_remaining_for_undo: Log remaining for undo.\n- mssql_log_send_queue: Log send queue.\n- mssql_transaction_delay: Transaction delay for database replicas.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 6, + "x": 6, + "y": 76 + }, + "id": 15, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_log_apply_pending_queue{instance=\"$Server\"}", + "legendFormat": "Apply pending queue", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_log_remaining_for_undo{instance=\"$Server\"}", + "legendFormat": "Remaining for undo", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_log_send_queue{instance=\"$Server\"}", + "legendFormat": "Log send queue", + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_transaction_delay{instance=\"$Server\"}", + "legendFormat": "Transaction delay", + "refId": "D" + } + ], + "title": "HA / Replica Queue Gauges", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_local_time_seconds: Local time in seconds since epoch (Unix time).", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + }, + "unit": "dateTimeAsIso" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 4, + "x": 12, + "y": 76 + }, + "id": 16, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_local_time_seconds{instance=\"$Server\"} * 1000", + "instant": true, + "refId": "A" + } + ], + "title": "Exporter Local Time", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_sqlserver_uptime_seconds: SQL Server uptime in seconds since the SQL Server service started.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 4, + "x": 16, + "y": 76 + }, + "id": 54, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_sqlserver_uptime_seconds{instance=\"$Server\"}", + "instant": true, + "refId": "A" + } + ], + "title": "SQL Server Uptime", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_os_uptime_seconds: Operating system uptime in seconds since the host booted.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 4, + "x": 20, + "y": 76 + }, + "id": 55, + "options": { + "colorMode": "value", + "graphMode": "none", + "justifyMode": "center", + "orientation": "auto", + "percentChangeColorMode": "standard", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "showPercentChange": false, + "textMode": "auto", + "wideLayout": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_os_uptime_seconds{instance=\"$Server\"}", + "instant": true, + "refId": "A" + } + ], + "title": "OS Uptime", + "type": "stat" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_service_info: SQL Server instance/service availability and metadata.", + "fieldConfig": { + "defaults": { + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 83 + }, + "id": 17, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_service_info{instance=\"$Server\"}", + "format": "table", + "instant": true, + "refId": "A" + } + ], + "title": "Service & Instance Metadata Snapshot", + "type": "table" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_info: Info metric with database state and AG status. Value is always 1.", + "fieldConfig": { + "defaults": { + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 381 + }, + "id": 18, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_database_info{instance=\"$Server\"}", + "format": "table", + "instant": true, + "refId": "A" + } + ], + "title": "Database Inventory Snapshot", + "type": "table" + } + ], + "title": "Availability & Inventory", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 2 + }, + "id": 19, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_connections: Number of active connections.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1513 + }, + "id": 20, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_connections{instance=\"$Server\"}", + "legendFormat": "{{db}}", + "refId": "A" + } + ], + "title": "Connections by Database", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_logins: Logins since SQL Server startup.\n- mssql_logouts: Logouts since SQL Server startup.\n- mssql_connection_reset: Connection resets since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1513 + }, + "id": 21, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_logins{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Logins/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_logouts{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Logouts/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_connection_reset{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Connection resets/sec", + "refId": "C" + } + ], + "title": "Login / Logout / Reset Rates", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_active_cursors: Current active cursors by cursor type.\n- mssql_sql_attentions_total: SQL attentions since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1513 + }, + "id": 22, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_active_cursors{instance=\"$Server\"}", + "legendFormat": "{{cursor_type}}", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_sql_attentions_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "SQL attentions/sec", + "refId": "B" + } + ], + "title": "Active Cursors & Attentions", + "type": "timeseries" + } + ], + "title": "Workload, Sessions & Connections", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 3 + }, + "id": 23, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_cpu_utilization_percentage: CPU utilization percentages from the SQL Server scheduler monitor ring buffer.\n- mssql_resource_pool_cpu_usage_percentage: CPU usage percentage by resource pool.\n- mssql_workload_group_cpu_usage_percentage: CPU usage percentage by workload group.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1514 + }, + "id": 24, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_cpu_utilization_percentage{instance=\"$Server\"}", + "legendFormat": "{{scope}} CPU %", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_resource_pool_cpu_usage_percentage{instance=\"$Server\"}", + "legendFormat": "Pool {{resource_pool}}", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_workload_group_cpu_usage_percentage{instance=\"$Server\"}", + "legendFormat": "Group {{workload_group}}", + "refId": "C" + } + ], + "title": "CPU by Scope / Pool / Workload Group", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_batch_requests: Number of command batches received.\n- mssql_sql_compilations: SQL compilations since SQL Server startup.\n- mssql_sql_recompilations: SQL recompilations since SQL Server startup.\n- mssql_sql_auto_params_total: Automatic parameterization activity counters since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1514 + }, + "id": 25, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_batch_requests{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Batch req/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_sql_compilations{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Compilations/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_sql_recompilations{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Recompilations/sec", + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_sql_auto_params_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Auto params {{mode}}/sec", + "refId": "D" + } + ], + "title": "Batch Requests / Compilations / Recompilations", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_access_methods_total: Access methods counters since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1514 + }, + "id": 26, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_access_methods_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{operation}}", + "refId": "A" + } + ], + "title": "Access Methods Activity", + "type": "timeseries" + } + ], + "title": "CPU, Compilation & Execution Patterns", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 4 + }, + "id": 27, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_host_physical_memory_bytes: Host physical memory in bytes.\n- mssql_os_memory: OS physical memory, used and available.\n- mssql_os_page_file: OS page file, used and available.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1515 + }, + "id": 28, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_host_physical_memory_bytes{instance=\"$Server\"}", + "legendFormat": "Host {{state}}", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_os_memory{instance=\"$Server\"}", + "legendFormat": "OS {{state}}", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_os_page_file{instance=\"$Server\"}", + "legendFormat": "Page file {{state}}", + "refId": "C" + } + ], + "title": "Host / OS / Page File Memory", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_resident_memory_bytes: SQL Server resident memory size (AKA working set).\n- mssql_virtual_memory_bytes: SQL Server committed virtual memory size.\n- mssql_memory_grants_outstanding: Outstanding workspace memory grants.\n- mssql_memory_grants_pending: Pending workspace memory grants.\n- mssql_sql_process_memory_bytes: SQL Server process memory allocations in bytes.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1515 + }, + "id": 29, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_resident_memory_bytes{instance=\"$Server\"}", + "legendFormat": "Resident bytes", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_virtual_memory_bytes{instance=\"$Server\"}", + "legendFormat": "Virtual bytes", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_memory_grants_outstanding{instance=\"$Server\"}", + "legendFormat": "Grants outstanding", + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_memory_grants_pending{instance=\"$Server\"}", + "legendFormat": "Grants pending", + "refId": "D" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_sql_process_memory_bytes{instance=\"$Server\"}", + "legendFormat": "SQL proc {{state}}", + "refId": "E" + } + ], + "title": "SQL Process & Memory Grants", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_memory_manager_bytes: SQL Server memory manager allocations in bytes.\n- mssql_memory_utilization_percentage: The percentage of committed memory that is in the working set.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1515 + }, + "id": 30, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_memory_manager_bytes{instance=\"$Server\"}", + "legendFormat": "{{state}}", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_memory_utilization_percentage{instance=\"$Server\"}", + "legendFormat": "Memory util %", + "refId": "B" + } + ], + "title": "Memory Manager Breakdown", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_buffer_database_pages: Database pages in the SQL Server buffer pool.\n- mssql_buffer_target_pages: Target pages in the SQL Server buffer pool.\n- mssqlbuffer_pool_committed: SQL Server buffer pool committed bytes.\n- mssqlbuffer_pool_committed_target: SQL Server buffer pool committed target bytes.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1606 + }, + "id": 31, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_buffer_database_pages{instance=\"$Server\"}", + "legendFormat": "Database pages", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_buffer_target_pages{instance=\"$Server\"}", + "legendFormat": "Target pages", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssqlbuffer_pool_committed{instance=\"$Server\"}", + "legendFormat": "Committed bytes", + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssqlbuffer_pool_committed_target{instance=\"$Server\"}", + "legendFormat": "Committed target bytes", + "refId": "D" + } + ], + "title": "Buffer Pool Capacity", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_buffer_cache_hit_ratio: Ratio of requests that hit the buffer cache\n- mssql_page_life_expectancy_seconds: The minimum number of seconds a page will stay in the buffer pool on this node without references.\n- mssql_page_fault_count: The number of page faults that were incurred by the SQL Server process.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1606 + }, + "id": 32, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_buffer_cache_hit_ratio{instance=\"$Server\"}", + "legendFormat": "Cache hit ratio", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_page_life_expectancy_seconds{instance=\"$Server\"}", + "legendFormat": "PLE", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_page_fault_count{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Page faults/sec", + "refId": "C" + } + ], + "title": "Buffer Cache Health", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_buffer_manager_operations_total: Buffer manager operation counters since SQL Server startup.\n- mssql_checkpoint_pages_sec: Checkpoint Pages Per Second", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1606 + }, + "id": 33, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_buffer_manager_operations_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{operation}}/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_checkpoint_pages_sec{instance=\"$Server\"}", + "legendFormat": "Checkpoint pages/sec", + "refId": "B" + } + ], + "title": "Buffer Manager Operations / Checkpoints", + "type": "timeseries" + } + ], + "title": "Memory & Buffer Pool", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 5 + }, + "id": 34, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_page_lookups: Buffer manager page lookups since SQL Server startup.\n- mssql_page_reads: Buffer manager page reads since SQL Server startup.\n- mssql_page_writes: Buffer manager page writes since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1516 + }, + "id": 35, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_page_lookups{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Lookups/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_page_reads{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Reads/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_page_writes{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Writes/sec", + "refId": "C" + } + ], + "title": "Page Lookup / Read / Write Rates", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_io_stall_seconds: Stall time in seconds per database and I/O operation.\n- mssql_io_stall_total_seconds: Total stall time in seconds per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1516 + }, + "id": 36, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_io_stall_seconds{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} {{operation}} stall/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_io_stall_total_seconds{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} total stall/sec", + "refId": "B" + } + ], + "title": "I/O Stall by Database", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_average_latch_wait_time_ms: Average latch wait time metric from SQL Server performance counters.\n- mssql_network_io_waits_ms: Network IO wait time metric from SQL Server wait statistics.\n- mssql_page_io_latch_waits_ms: Page IO latch wait time metric from SQL Server wait statistics.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "ms" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1516 + }, + "id": 37, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_average_latch_wait_time_ms{instance=\"$Server\"}", + "legendFormat": "Avg latch wait ms", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_network_io_waits_ms{instance=\"$Server\"}", + "legendFormat": "Network IO waits ms", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_page_io_latch_waits_ms{instance=\"$Server\"}", + "legendFormat": "Page IO latch waits ms", + "refId": "C" + } + ], + "title": "Latch / Network / Page I/O Wait Gauges", + "type": "timeseries" + } + ], + "title": "I/O, Pages & Latches", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 6 + }, + "id": 38, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_active_transactions_count: Count of active transactions from sys.dm_tran_active_transactions.\n- mssql_database_active_transactions: Current active transactions per database.\n- mssql_longest_transaction_running_time_seconds: Longest transaction running time in seconds.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1517 + }, + "id": 39, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_active_transactions_count{instance=\"$Server\"}", + "legendFormat": "Active tx count", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_database_active_transactions{instance=\"$Server\"}", + "legendFormat": "{{db}} active tx", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_longest_transaction_running_time_seconds{instance=\"$Server\"}", + "legendFormat": "Longest tx sec", + "refId": "C" + } + ], + "title": "Transaction Activity", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_processes_blocked: Currently blocked processes.\n- mssql_lock_waits_total: Lock waits since SQL Server startup.\n- mssql_lock_wait_time_ms_total: Accumulated lock wait time in milliseconds since SQL Server startup.\n- mssql_deadlocks: Number of lock requests that resulted in a deadlock.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "ms" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1517 + }, + "id": 40, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_processes_blocked{instance=\"$Server\"}", + "legendFormat": "Blocked processes", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_lock_waits_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Lock waits/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_lock_wait_time_ms_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Lock wait ms/sec", + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_deadlocks{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Deadlocks/sec", + "refId": "D" + } + ], + "title": "Blocking / Lock Waits / Deadlocks", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_waits_in_progress: Current waits in progress by wait category.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1517 + }, + "id": 41, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_waits_in_progress{instance=\"$Server\"}", + "legendFormat": "{{wait_type}}", + "refId": "A" + } + ], + "title": "Waits in Progress", + "type": "timeseries" + } + ], + "title": "Transactions, Locks & Waits", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 7 + }, + "id": 42, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_file_size_bytes: Database file sizes in bytes.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1518 + }, + "id": 43, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_database_file_size_bytes{instance=\"$Server\"}", + "legendFormat": "{{db}} {{state}}", + "refId": "A" + } + ], + "title": "Database File Sizes", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_percent_log_used: Transaction log space used as a percentage per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "percent" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1518 + }, + "id": 44, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_database_percent_log_used{instance=\"$Server\"}", + "legendFormat": "{{db}}", + "refId": "A" + } + ], + "title": "Database Log Used %", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_xtp_memory_used_bytes: XTP memory usage in bytes per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1518 + }, + "id": 45, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_database_xtp_memory_used_bytes{instance=\"$Server\"}", + "legendFormat": "{{db}}", + "refId": "A" + } + ], + "title": "XTP Memory by Database", + "type": "timeseries" + } + ], + "title": "Database Storage & File Layout", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 8 + }, + "id": 46, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_log_flushes_total: Database log flushes since SQL Server startup.\n- mssql_database_log_bytes_flushed_total: Database log bytes flushed since SQL Server startup.\n- mssql_database_log_waits_total: Database log flush waits since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1519 + }, + "id": 47, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_database_log_flushes_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} flushes/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_database_log_bytes_flushed_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} bytes/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_database_log_waits_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} waits/sec", + "refId": "C" + } + ], + "title": "Log Flushes / Bytes / Waits", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_database_log_flush_wait_time_ms_total: Accumulated database log flush wait time in milliseconds since SQL Server startup.\n- mssql_database_log_events_total: Database log event counts since SQL Server startup.\n- mssql_log_growths: Number of times the transaction log has been expanded, per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "ms" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1519 + }, + "id": 48, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_database_log_flush_wait_time_ms_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} flush wait ms/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_database_log_events_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} {{event}}/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_log_growths{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{db}} growths/sec", + "refId": "C" + } + ], + "title": "Log Wait Time / Events / Growths", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_mirrored_write_transactions: Mirrored write transactions since SQL Server startup.\n- mssql_redo_blocked: Redo blocked events since SQL Server startup.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1519 + }, + "id": 49, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_mirrored_write_transactions{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Mirrored write tx/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_redo_blocked{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Redo blocked/sec", + "refId": "B" + } + ], + "title": "Mirroring / Redo Movement", + "type": "timeseries" + } + ], + "title": "Transaction Log, Redo & Data Movement", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 9 + }, + "id": 50, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_sql_errors_total: SQL error counters since SQL Server startup.\n- mssql_user_errors: Number of user errors.\n- mssql_kill_connection_errors: Number of severe errors that caused SQL Server to kill the connection.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 0, + "y": 1520 + }, + "id": 51, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_sql_errors_total{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "{{error_type}}/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_user_errors{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "User errors/sec", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_kill_connection_errors{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Kill connection errors/sec", + "refId": "C" + } + ], + "title": "SQL Errors & Connection Kills", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_tempdb_active_temp_tables: Active temp tables from SQL Server general statistics.\n- mssql_tempdb_space_bytes: tempdb free space and version store size in bytes.\n- mssql_temp_tables_for_destruction: Temp tables waiting to be destroyed.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 8, + "y": 1520 + }, + "id": 52, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_tempdb_active_temp_tables{instance=\"$Server\"}", + "legendFormat": "Active temp tables", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_tempdb_space_bytes{instance=\"$Server\"}", + "legendFormat": "tempdb {{state}}", + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_temp_tables_for_destruction{instance=\"$Server\"}", + "legendFormat": "Temp tables for destruction", + "refId": "C" + } + ], + "title": "Tempdb Space & Temp Objects", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "- mssql_user_connections: Current user connections from SQL Server general statistics.\n- mssql_page_fault_count: The number of page faults that were incurred by the SQL Server process.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 2, + "pointSize": 3, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "short" + }, + "overrides": [] + }, + "gridPos": { + "h": 7, + "w": 8, + "x": 16, + "y": 1520 + }, + "id": 53, + "options": { + "legend": { + "calcs": [ + "lastNotNull", + "max" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "rate(mssql_page_fault_count{instance=\"$Server\"}[$__rate_interval])", + "legendFormat": "Page faults/sec", + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "expr": "mssql_user_connections{instance=\"$Server\"}", + "legendFormat": "User connections", + "refId": "B" + } + ], + "title": "Misc Operational Counters", + "type": "timeseries" + } + ], + "title": "Errors & Tempdb", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 10 + }, + "id": 57, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Latest waits snapshot using panel time override so it stays on current data instead of the dashboard time range.", + "fieldConfig": { + "defaults": { + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Total Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Wait Time" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Resource Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "custom.width", + "value": 161 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Signal Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Signal Wait" + }, + { + "id": "custom.hideFrom.viz", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait %" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + }, + { + "id": "displayName", + "value": "Pcnt" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Time Sampled (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Time Sample" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "% Signal Wait" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + }, + { + "id": "custom.width", + "value": 139 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Avg Time Per Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Avg Time Per Wait" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Time Sample" + }, + "properties": [ + { + "id": "custom.width", + "value": 121 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait Type" + }, + "properties": [ + { + "id": "custom.width", + "value": 822 + }, + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "${__value.raw} - Wait Document Online", + "url": "https://www.sqlskills.com/help/waits/${__value.raw}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait Rank" + }, + "properties": [ + { + "id": "custom.hideFrom.viz", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Pcnt" + }, + "properties": [ + { + "id": "custom.width", + "value": 81 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait Time" + }, + "properties": [ + { + "id": "custom.width", + "value": 133 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Waiting Tasks Count" + }, + "properties": [ + { + "id": "displayName", + "value": "No of Waits" + }, + { + "id": "unit", + "value": "short" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Waits Per Core Per Minute" + }, + "properties": [ + { + "id": "custom.width", + "value": 213 + }, + { + "id": "unit", + "value": "s" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "No of Waits" + }, + "properties": [ + { + "id": "custom.width", + "value": 127 + } + ] + } + ] + }, + "gridPos": { + "h": 11, + "w": 19, + "x": 0, + "y": 691 + }, + "hideTimeOverride": true, + "id": 61, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "bottomk($TOP_K_WAITS, mssql_waits__wait_rank_no{instance=\"$Server\"})", + "format": "table", + "instant": true, + "refId": "wait_rank_no" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})", + "format": "table", + "instant": true, + "refId": "wait_time_seconds" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "mssql_waits__resource_time_seconds{instance=\"$Server\"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "refId": "resource_time_seconds" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "mssql_waits__signal_time_seconds{instance=\"$Server\"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})", + "format": "table", + "instant": true, + "refId": "signal_time_seconds" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "mssql_waits__waiting_tasks_count{instance=\"$Server\"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})", + "format": "table", + "instant": true, + "refId": "waiting_tasks_count" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "mssql_waits__wait_percentage{instance=\"$Server\"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})", + "format": "table", + "instant": true, + "refId": "wait_percentage" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "0*topk($TOP_K_WAITS,mssql_waits__wait_time_seconds{instance=\"$Server\"}) + scalar(mssql_sqlserver_uptime_seconds{instance=\"$Server\"})", + "format": "table", + "instant": true, + "refId": "time_sampled_seconds" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"}) / ${SQLCPUCount:raw} / (scalar(mssql_sqlserver_uptime_seconds{instance=\"$Server\"}) / 60)", + "format": "table", + "instant": true, + "refId": "waits_per_core_per_minute" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * (mssql_waits__signal_time_seconds{instance=\"$Server\"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})) / (topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"}))", + "format": "table", + "instant": true, + "refId": "percent_signal_wait" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"})) / (mssql_waits__waiting_tasks_count{instance=\"$Server\"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance=\"$Server\"}))", + "format": "table", + "instant": true, + "refId": "avg_time_per_wait_seconds" + } + ], + "timeFrom": "1h", + "title": "Latest Waits Snapshot (ignores dashboard time range)", + "transformations": [ + { + "id": "joinByField", + "options": { + "byField": "wait_type", + "mode": "outer" + } + }, + { + "id": "organize", + "options": { + "excludeByName": { + "Time": true, + "Time 1": true, + "Time 10": true, + "Time 2": true, + "Time 3": true, + "Time 4": true, + "Time 5": true, + "Time 6": true, + "Time 7": true, + "Time 8": true, + "Time 9": true, + "__name__ 1": true, + "__name__ 2": true, + "__name__ 3": true, + "__name__ 4": true, + "__name__ 5": true, + "__name__ 6": true, + "instance 1": true, + "instance 10": true, + "instance 2": true, + "instance 3": true, + "instance 4": true, + "instance 5": true, + "instance 6": true, + "instance 7": true, + "instance 8": true, + "instance 9": true, + "job 1": true, + "job 10": true, + "job 2": true, + "job 3": true, + "job 4": true, + "job 5": true, + "job 6": true, + "job 7": true, + "job 8": true, + "job 9": true, + "wait_type 1": true, + "wait_type 2": true, + "wait_type 3": true, + "wait_type 4": true, + "wait_type 5": true, + "wait_type 6": true, + "wait_type 7": true, + "wait_type 8": true, + "wait_type 9": true + }, + "includeByName": {}, + "indexByName": { + "Time 1": 12, + "Time 10": 18, + "Time 2": 9, + "Time 3": 10, + "Time 4": 11, + "Time 5": 13, + "Time 6": 14, + "Time 7": 15, + "Time 8": 16, + "Time 9": 17, + "Value #avg_time_per_wait_seconds": 8, + "Value #percent_signal_wait": 6, + "Value #resource_time_seconds": 5, + "Value #signal_time_seconds": 32, + "Value #time_sampled_seconds": 0, + "Value #wait_percentage": 2, + "Value #wait_rank_no": 22, + "Value #wait_time_seconds": 3, + "Value #waiting_tasks_count": 7, + "Value #waits_per_core_per_minute": 4, + "__name__ 1": 19, + "__name__ 2": 23, + "__name__ 3": 26, + "__name__ 4": 29, + "__name__ 5": 33, + "__name__ 6": 36, + "instance 1": 20, + "instance 10": 45, + "instance 2": 24, + "instance 3": 27, + "instance 4": 30, + "instance 5": 34, + "instance 6": 37, + "instance 7": 39, + "instance 8": 41, + "instance 9": 43, + "job 1": 21, + "job 10": 46, + "job 2": 25, + "job 3": 28, + "job 4": 31, + "job 5": 35, + "job 6": 38, + "job 7": 40, + "job 8": 42, + "job 9": 44, + "wait_type": 1 + }, + "renameByName": { + "Value #A": "Wait Rank", + "Value #B": "Total Wait (s)", + "Value #C": "Resource Wait (s)", + "Value #D": "Signal Wait (s)", + "Value #E": "Waiting Tasks Count", + "Value #F": "Wait %", + "Value #G": "Time Sampled (s)", + "Value #H": "Waits Per Core Per Minute", + "Value #I": "% Signal Wait", + "Value #J": "Avg Time Per Wait (s)", + "Value #avg_time_per_wait_seconds": "Avg Time Per Wait (s)", + "Value #percent_signal_wait": "% Signal Wait", + "Value #resource_time_seconds": "Resource Wait (s)", + "Value #signal_time_seconds": "Signal Wait (s)", + "Value #time_sampled_seconds": "Time Sampled (s)", + "Value #wait_percentage": "Wait %", + "Value #wait_rank_no": "Wait Rank", + "Value #wait_time_seconds": "Total Wait (s)", + "Value #waiting_tasks_count": "Waiting Tasks Count", + "Value #waits_per_core_per_minute": "Waits Per Core Per Minute", + "wait_type": "Wait Type" + } + } + } + ], + "type": "table" + } + ], + "title": "Wait Stats - Since Startup - \"__$SQLCPUCount CPUs__\" @ ${local_time_seconds:date:YYYY-MM-DD HH:mm:ss}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 11 + }, + "id": 60, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Waits snapshot evaluated at the dashboard start timestamp. Prometheus will use the nearest sample at or before the requested second within lookback delta.", + "fieldConfig": { + "defaults": { + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Total Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Wait Time" + }, + { + "id": "custom.width", + "value": 133 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Resource Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "custom.width", + "value": 161 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Signal Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "custom.hideFrom.viz", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait %" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + }, + { + "id": "displayName", + "value": "Pcnt" + }, + { + "id": "custom.width", + "value": 81 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Time Sampled (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Time Sample" + }, + { + "id": "custom.width", + "value": 121 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "% Signal Wait" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + }, + { + "id": "custom.width", + "value": 139 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Avg Time Per Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Avg Time Per Wait" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait Type" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "${__value.raw} - Wait Document Online", + "url": "https://www.sqlskills.com/help/waits/${__value.raw}" + } + ] + }, + { + "id": "custom.width", + "value": 800 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait Rank" + }, + "properties": [ + { + "id": "custom.hideFrom.viz", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Waiting Tasks Count" + }, + "properties": [ + { + "id": "displayName", + "value": "No of Waits" + }, + { + "id": "unit", + "value": "short" + }, + { + "id": "custom.width", + "value": 127 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Waits Per Core Per Minute" + }, + "properties": [ + { + "id": "custom.width", + "value": 208 + }, + { + "id": "unit", + "value": "s" + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 19, + "x": 0, + "y": 1522 + }, + "id": 62, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sort((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)", + "format": "table", + "instant": true, + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)", + "format": "table", + "instant": true, + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "max by (wait_type) (mssql_waits__resource_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)", + "format": "table", + "instant": true, + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "max by (wait_type) (mssql_waits__signal_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)", + "format": "table", + "instant": true, + "refId": "D" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "max by (wait_type) (mssql_waits__waiting_tasks_count{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)", + "format": "table", + "instant": true, + "refId": "E" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "max by (wait_type) (mssql_waits__wait_percentage{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)", + "format": "table", + "instant": true, + "refId": "F" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(0 * (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS))) + scalar(max(mssql_sqlserver_uptime_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))", + "format": "table", + "instant": true, + "refId": "G" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "((max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)) / ${SQLCPUCount:raw} / scalar(max(mssql_sqlserver_uptime_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) * 60", + "format": "table", + "instant": true, + "refId": "H" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * ((max by (wait_type) (mssql_waits__signal_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)) / (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)))", + "format": "table", + "instant": true, + "refId": "I" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS)) / (max by (wait_type) (mssql_waits__waiting_tasks_count{instance=\"$Server\"} @ ${__from:date:seconds}) and on(wait_type) ((max by (wait_type) (mssql_waits__wait_rank_no{instance=\"$Server\"} @ ${__from:date:seconds})) <= $TOP_K_WAITS))", + "format": "table", + "instant": true, + "refId": "J" + } + ], + "title": "Waits Snapshot @ Dashboard Start (${__from:date:YYYY-MM-DD HH.mm:ss})", + "transformations": [ + { + "id": "joinByField", + "options": { + "byField": "wait_type", + "mode": "outer" + } + }, + { + "id": "organize", + "options": { + "excludeByName": { + "Time": true, + "Time 1": true, + "Time 2": true, + "Time 3": true, + "Time 4": true, + "Time 5": true, + "Time 6": true, + "Time 7": true, + "Time 8": true, + "Time 9": true, + "wait_type 1": true, + "wait_type 2": true, + "wait_type 3": true, + "wait_type 4": true, + "wait_type 5": true, + "wait_type 6": true, + "wait_type 7": true, + "wait_type 8": true, + "wait_type 9": true + }, + "includeByName": {}, + "indexByName": { + "Time 1": 11, + "Time 10": 20, + "Time 2": 12, + "Time 3": 13, + "Time 4": 14, + "Time 5": 15, + "Time 6": 16, + "Time 7": 17, + "Time 8": 18, + "Time 9": 19, + "Value #A": 2, + "Value #B": 4, + "Value #C": 6, + "Value #D": 8, + "Value #E": 9, + "Value #F": 3, + "Value #G": 0, + "Value #H": 5, + "Value #I": 7, + "Value #J": 10, + "wait_type": 1 + }, + "renameByName": { + "Value #A": "Wait Rank", + "Value #B": "Total Wait (s)", + "Value #C": "Resource Wait (s)", + "Value #D": "Signal Wait (s)", + "Value #E": "Waiting Tasks Count", + "Value #F": "Wait %", + "Value #G": "Time Sampled (s)", + "Value #H": "Waits Per Core Per Minute", + "Value #I": "% Signal Wait", + "Value #J": "Avg Time Per Wait (s)", + "wait_type": "Wait Type" + } + } + } + ], + "type": "table" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Delta waits for the selected dashboard time range, calculated as end snapshot minus start snapshot.", + "fieldConfig": { + "defaults": { + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Total Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Wait Time" + }, + { + "id": "custom.width", + "value": 133 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Resource Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "custom.width", + "value": 161 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Signal Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait %" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + }, + { + "id": "displayName", + "value": "Pcnt" + }, + { + "id": "custom.width", + "value": 81 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Time Sampled (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Time Sample" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "% Signal Wait" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + }, + { + "id": "custom.width", + "value": 139 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Avg Time Per Wait (s)" + }, + "properties": [ + { + "id": "unit", + "value": "s" + }, + { + "id": "displayName", + "value": "Avg Time Per Wait" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Wait Type" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "${__value.raw} - Wait Document Online", + "url": "https://www.sqlskills.com/help/waits/${__value.raw}" + } + ] + }, + { + "id": "custom.width", + "value": 802 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Waiting Tasks Count" + }, + "properties": [ + { + "id": "displayName", + "value": "No of Waits" + }, + { + "id": "unit", + "value": "short" + }, + { + "id": "custom.width", + "value": 127 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Waits Per Core Per Minute" + }, + "properties": [ + { + "id": "custom.width", + "value": 213 + }, + { + "id": "unit", + "value": "s" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "Time Sample" + }, + "properties": [ + { + "id": "custom.width", + "value": 121 + } + ] + } + ] + }, + "gridPos": { + "h": 9, + "w": 19, + "x": 0, + "y": 1544 + }, + "id": 63, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [] + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "sort_desc(topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))", + "format": "table", + "instant": true, + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(max by (wait_type) (mssql_waits__resource_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__resource_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds})) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))", + "format": "table", + "instant": true, + "refId": "B" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(max by (wait_type) (mssql_waits__signal_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__signal_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds})) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))", + "format": "table", + "instant": true, + "refId": "C" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(max by (wait_type) (mssql_waits__waiting_tasks_count{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__waiting_tasks_count{instance=\"$Server\"} @ ${__from:date:seconds})) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))", + "format": "table", + "instant": true, + "refId": "D" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * (((max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))) / scalar(sum((max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))", + "format": "table", + "instant": true, + "refId": "E" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(0 * (sort_desc(topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds})))))) + scalar(max(mssql_sqlserver_uptime_seconds{instance=\"$Server\"} @ ${__to:date:seconds}) - max(mssql_sqlserver_uptime_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))", + "format": "table", + "instant": true, + "refId": "F" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "(((((max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))) / ${SQLCPUCount:raw}) / scalar(max(mssql_sqlserver_uptime_seconds{instance=\"$Server\"} @ ${__to:date:seconds}) - max(mssql_sqlserver_uptime_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) * 60", + "format": "table", + "instant": true, + "refId": "G" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "100 * ((((max by (wait_type) (mssql_waits__signal_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__signal_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))) / ((((max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds})))))))", + "format": "table", + "instant": true, + "refId": "H" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "((((max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds}))))) / ((((max by (wait_type) (mssql_waits__waiting_tasks_count{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__waiting_tasks_count{instance=\"$Server\"} @ ${__from:date:seconds}))) and on(wait_type) (topk($TOP_K_WAITS, (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__to:date:seconds})) - (max by (wait_type) (mssql_waits__wait_time_seconds{instance=\"$Server\"} @ ${__from:date:seconds})))))))", + "format": "table", + "instant": true, + "refId": "I" + } + ], + "title": "Waits Delta for Selected Time Range (${__from:date:YYYY-MM-DD HH.mm:ss} → ${__to:date:YYYY-MM-DD HH.mm:ss})", + "transformations": [ + { + "id": "joinByField", + "options": { + "byField": "wait_type", + "mode": "outer" + } + }, + { + "id": "sortBy", + "options": { + "sort": [ + { + "desc": true, + "field": "Value #A" + } + ] + } + }, + { + "id": "calculateField", + "options": { + "alias": "Row Index", + "mode": "index", + "replaceFields": false + } + }, + { + "id": "calculateField", + "options": { + "alias": "Wait Rank", + "binary": { + "left": { + "matcher": { + "id": "byName", + "options": "Row Index" + } + }, + "operator": "+", + "right": { + "fixed": "1" + } + }, + "mode": "binary", + "reduce": { + "reducer": "sum" + }, + "replaceFields": false + } + }, + { + "id": "organize", + "options": { + "excludeByName": { + "Row Index": true, + "Time": true, + "Time 1": true, + "Time 2": true, + "Time 3": true, + "Time 4": true, + "Time 5": true, + "Time 6": true, + "Time 7": true, + "Time 8": true, + "Value #B": false, + "Value #C": true, + "Wait Rank": true, + "wait_type 1": true, + "wait_type 2": true, + "wait_type 3": true, + "wait_type 4": true, + "wait_type 5": true, + "wait_type 6": true, + "wait_type 7": true, + "wait_type 8": true + }, + "includeByName": {}, + "indexByName": { + "Row Index": 19, + "Time 1": 18, + "Time 2": 20, + "Time 3": 15, + "Time 4": 12, + "Time 5": 10, + "Time 6": 11, + "Time 7": 14, + "Time 8": 16, + "Time 9": 17, + "Value #A": 3, + "Value #B": 5, + "Value #C": 6, + "Value #D": 8, + "Value #E": 2, + "Value #F": 0, + "Value #G": 4, + "Value #H": 7, + "Value #I": 9, + "Wait Rank": 13, + "wait_type": 1 + }, + "renameByName": { + "Value #A": "Total Wait (s)", + "Value #B": "Resource Wait (s)", + "Value #C": "Signal Wait (s)", + "Value #D": "Waiting Tasks Count", + "Value #E": "Wait %", + "Value #F": "Time Sampled (s)", + "Value #G": "Waits Per Core Per Minute", + "Value #H": "% Signal Wait", + "Value #I": "Avg Time Per Wait (s)", + "wait_type": "Wait Type" + } + } + } + ], + "type": "table" + } + ], + "title": "Waits Stats - Selective - \"__$SQLCPUCount CPUs__\" @ $__to", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 12 + }, + "id": 65, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisBorderShow": false, + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "barWidthFactor": 0.6, + "drawStyle": "bars", + "fillOpacity": 100, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "showValues": false, + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + } + ] + }, + "unit": "s" + }, + "overrides": [] + }, + "gridPos": { + "h": 12, + "w": 24, + "x": 0, + "y": 1523 + }, + "id": 68, + "interval": "5m", + "options": { + "legend": { + "calcs": [ + "max" + ], + "displayMode": "table", + "placement": "right", + "showLegend": true, + "sortBy": "Max", + "sortDesc": true + }, + "tooltip": { + "hideZeros": false, + "mode": "multi", + "sort": "desc" + } + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "expr": "topk(15, (increase(mssql_waits__resource_time_seconds{instance=\"$Server\", wait_type!=\"CXCONSUMER\"}[$__interval])) > 0 )", + "format": "time_series", + "instant": false, + "legendFormat": "{{wait_type}}", + "range": true, + "refId": "A" + } + ], + "title": "Wait Stats", + "type": "timeseries" + } + ], + "title": "Wait Stats - Trend - \"__$SQLCPUCount CPUs__\" @ ${local_time_seconds:date:YYYY-MM-DD HH:mm:ss}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 13 + }, + "id": 70, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "footer": { + "reducers": [] + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "collection_time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "unique_key" + }, + "properties": [ + { + "id": "custom.width", + "value": 242 + }, + { + "id": "custom.hideFrom.viz", + "value": true + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "reads" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "used_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "writes" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tempdb_allocations" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "physical_reads" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "physical_io" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tempdb_current" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "granted_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "requested_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "max_used_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "login_time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "start_time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "cpu" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "query_plan" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Paste The Plan", + "url": "http://localhost:8080/pastetheplan-bridge.html?key=${__data.fields.unique_key}&server=${Server}" + } + ] + } + ] + } + ] + }, + "gridPos": { + "h": 7, + "w": 24, + "x": 0, + "y": 88 + }, + "id": 75, + "options": { + "cellHeight": "sm", + "showHeader": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__start_time{instance=\"$Server\"} * 1000\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "start_time" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__reads{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "reads" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__used_memory{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "used_memory" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__cpu{instance=\"$Server\"}\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "cpu" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__open_tran_count{instance=\"$Server\"}\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "open_tran_count" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__writes{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "writes" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__tempdb_allocations{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "tempdb_allocations" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__percent_complete{instance=\"$Server\"}\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "percent_complete" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__collection_time_utc{instance=\"$Server\"} * 1000\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "collection_time_utc" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__tasks{instance=\"$Server\"}\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "tasks" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__physical_reads{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "physical_reads" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__physical_io{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "physical_io" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__tempdb_current{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "tempdb_current" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__granted_memory{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "granted_memory" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__requested_memory{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "requested_memory" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__max_used_memory{instance=\"$Server\"} * 8\nand on(unique_key)\n(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} >= scalar(max(mssql_whoisactive__collection_time_utc{instance=\"$Server\"})))", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "max_used_memory" + } + ], + "title": "WhoIsActive", + "transformations": [ + { + "id": "filterFieldsByName", + "options": { + "include": { + "names": [ + "additional_info", + "avg_elapsed_time", + "blocked_session_count", + "blocking_session_id", + "context_switches", + "database_name", + "host_name", + "implicit_tran", + "login_name", + "login_time", + "memory_info", + "program_name", + "query_plan", + "request_id", + "session_id", + "sql_command", + "sql_text", + "status", + "unique_key", + "wait_info", + "Value #start_time", + "Value #reads", + "Value #used_memory", + "Value #cpu", + "Value #open_tran_count", + "Value #writes", + "Value #tempdb_allocations", + "Value #percent_complete", + "Value #collection_time_utc", + "Value #tasks", + "Value #physical_reads", + "Value #physical_io", + "Value #tempdb_current", + "Value #granted_memory", + "Value #requested_memory", + "Value #max_used_memory", + "tran_log_writes" + ] + } + } + }, + { + "id": "joinByField", + "options": { + "byField": "unique_key", + "mode": "outer" + } + }, + { + "id": "organize", + "options": { + "excludeByName": { + "Time 1": true, + "Time 2": true, + "Time 3": true, + "Time 4": true, + "Time 5": true, + "Time 6": true, + "Time 7": true, + "Time 8": true, + "Value #tempdb_allocations": false, + "__name__ 1": true, + "__name__ 2": true, + "__name__ 3": true, + "__name__ 4": true, + "__name__ 5": true, + "__name__ 6": true, + "exported_job 1": true, + "exported_job 2": true, + "exported_job 3": true, + "exported_job 4": true, + "exported_job 5": true, + "exported_job 6": true, + "exported_job 7": true, + "exported_job 8": true, + "instance 1": true, + "instance 2": true, + "instance 3": true, + "instance 4": true, + "instance 5": true, + "instance 6": true, + "instance 7": true, + "instance 8": true, + "job 1": true, + "job 2": true, + "job 3": true, + "job 4": true, + "job 5": true, + "job 6": true, + "job 7": true, + "job 8": true, + "target 1": true, + "target 2": true, + "target 3": true, + "target 4": true, + "target 5": true, + "target 6": true, + "target 7": true, + "target 8": true, + "unique_key": false + }, + "includeByName": {}, + "indexByName": { + "Time 1": 8, + "Time 2": 27, + "Time 3": 34, + "Time 4": 41, + "Time 5": 48, + "Time 6": 55, + "Time 7": 62, + "Time 8": 68, + "Value #collection_time_utc": 0, + "Value #cpu": 47, + "Value #open_tran_count": 54, + "Value #reads": 33, + "Value #start_time": 26, + "Value #tempdb_allocations": 67, + "Value #used_memory": 40, + "Value #writes": 61, + "__name__ 1": 9, + "__name__ 2": 28, + "__name__ 3": 35, + "__name__ 4": 42, + "__name__ 5": 49, + "__name__ 6": 56, + "additional_info": 10, + "blocked_session_count": 11, + "context_switches": 12, + "database_name": 4, + "exported_job 1": 13, + "exported_job 2": 29, + "exported_job 3": 36, + "exported_job 4": 43, + "exported_job 5": 50, + "exported_job 6": 57, + "exported_job 7": 63, + "exported_job 8": 69, + "host_name": 14, + "instance 1": 15, + "instance 2": 30, + "instance 3": 37, + "instance 4": 44, + "instance 5": 51, + "instance 6": 58, + "instance 7": 64, + "instance 8": 70, + "job 1": 16, + "job 2": 31, + "job 3": 38, + "job 4": 45, + "job 5": 52, + "job 6": 59, + "job 7": 65, + "job 8": 71, + "login_name": 3, + "login_time": 17, + "physical_io": 18, + "physical_reads": 19, + "program_name": 2, + "query_plan": 20, + "request_id": 21, + "session_id": 1, + "sql_command": 7, + "sql_text": 22, + "status": 5, + "target 1": 23, + "target 2": 32, + "target 3": 39, + "target 4": 46, + "target 5": 53, + "target 6": 60, + "target 7": 66, + "target 8": 72, + "tasks": 24, + "tempdb_current": 25, + "wait_info": 6 + }, + "renameByName": { + "Value #collection_time_utc": "collection_time", + "Value #cpu": "cpu", + "Value #granted_memory": "granted_memory", + "Value #max_used_memory": "max_used_memory", + "Value #open_tran_count": "open_tran_count", + "Value #percent_complete": "percent_complete", + "Value #physical_io": "physical_io", + "Value #physical_reads": "physical_reads", + "Value #reads": "reads", + "Value #requested_memory": "requested_memory", + "Value #start_time": "start_time", + "Value #tasks": "tasks", + "Value #tempdb_allocations": "tempdb_allocations", + "Value #tempdb_current": "tempdb_current", + "Value #used_memory": "used_memory", + "Value #writes": "writes" + } + } + } + ], + "type": "table" + } + ], + "title": "WhoIsActive - Latest Snapshot - ${whoisactive_collection_time:date:YYYY-MM-DD HH:mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 14 + }, + "id": 72, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Active sessions captured by sp_WhoIsActive. Respects the dashboard time range — all snapshots collected within the selected window are shown as individual rows (e.g. 2 sessions at 7:00 am + 3 sessions at 7:02 am = 5 rows). Rows are joined by session_id across metrics; the Prometheus scrape timestamp becomes the Collection Time column.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": true + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Collection Time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + }, + { + "id": "custom.width", + "value": 155 + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "start_time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "cpu" + }, + "properties": [ + { + "id": "unit", + "value": "ms" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "used_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "percent_complete" + }, + "properties": [ + { + "id": "unit", + "value": "percent" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "reads" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "writes" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tempdb_allocations" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "physical_reads" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "physical_io" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "tempdb_current" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "granted_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "requested_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "max_used_memory" + }, + "properties": [ + { + "id": "unit", + "value": "kbytes" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "query_plan" + }, + "properties": [ + { + "id": "links", + "value": [ + { + "targetBlank": true, + "title": "Paste The Plan", + "url": "http://localhost:8080/pastetheplan-bridge.html?key=${__data.fields.unique_key}&server=${Server}" + } + ] + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "unique_key" + }, + "properties": [ + { + "id": "custom.hideFrom.viz", + "value": true + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 89 + }, + "id": 76, + "options": { + "cellHeight": "sm", + "showHeader": true, + "sortBy": [ + { + "desc": true, + "displayName": "Collection Time" + } + ] + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__start_time{instance=\"$Server\"} * 1000", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "start_time" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__reads{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "reads" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__used_memory{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "used_memory" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__cpu{instance=\"$Server\"}", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "cpu" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__open_tran_count{instance=\"$Server\"}", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "open_tran_count" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__writes{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "writes" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__tempdb_allocations{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "tempdb_allocations" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__percent_complete{instance=\"$Server\"}", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "percent_complete" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__collection_time_utc{instance=\"$Server\"} * 1000", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "collection_time_utc" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__physical_reads{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "physical_reads" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__physical_io{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "physical_io" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__tempdb_current{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "tempdb_current" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__granted_memory{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "granted_memory" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__requested_memory{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "requested_memory" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_whoisactive__max_used_memory{instance=\"$Server\"} * 8", + "format": "table", + "instant": false, + "legendFormat": "__auto", + "range": true, + "refId": "max_used_memory" + } + ], + "title": "WhoIsActive — Session History (respects dashboard time range)", + "transformations": [ + { + "id": "filterFieldsByName", + "options": { + "byVariable": false, + "exclude": { + "names": [ + "__name__", + "exported_job", + "instance", + "job", + "target" + ] + }, + "include": { + "names": [ + "additional_info", + "avg_elapsed_time", + "blocked_session_count", + "blocking_session_id", + "context_switches", + "database_name", + "host_name", + "login_name", + "login_time", + "program_name", + "request_id", + "session_id", + "sql_command", + "sql_text", + "status", + "unique_key", + "query_plan", + "wait_info", + "Value #start_time", + "Value #reads", + "Value #used_memory", + "Value #cpu", + "Value #open_tran_count", + "Value #writes", + "Value #tempdb_allocations", + "Value #percent_complete", + "Value #collection_time_utc", + "Value #physical_reads", + "Value #physical_io", + "Value #tempdb_current", + "Value #granted_memory", + "Value #requested_memory", + "Value #max_used_memory", + "memory_info" + ] + } + } + }, + { + "id": "joinByField", + "options": { + "byField": "unique_key", + "mode": "outer" + } + }, + { + "id": "organize", + "options": { + "excludeByName": { + "Value #collection_time_utc": false, + "__name__": true, + "additional_info": false, + "avg_elapsed_time": false, + "context_switches": false, + "exported_job": true, + "implicit_tran": true, + "instance": true, + "job": true, + "login_time": false, + "memory_info": false, + "query_plan": false, + "request_id": false, + "target": true, + "tasks": true, + "tran_log_writes": true, + "tran_start_time": true, + "unique_key": false + }, + "includeByName": {}, + "indexByName": { + "Value #collection_time_utc": 0, + "Value #cpu": 16, + "Value #open_tran_count": 12, + "Value #percent_complete": 21, + "Value #reads": 13, + "Value #start_time": 30, + "Value #tempdb_allocations": 17, + "Value #used_memory": 15, + "Value #writes": 14, + "additional_info": 26, + "avg_elapsed_time": 11, + "blocked_session_count": 8, + "blocking_session_id": 9, + "context_switches": 27, + "database_name": 4, + "granted_memory": 23, + "host_name": 29, + "login_name": 3, + "login_time": 31, + "max_used_memory": 24, + "physical_io": 20, + "physical_reads": 19, + "program_name": 2, + "query_plan": 28, + "request_id": 32, + "requested_memory": 22, + "session_id": 1, + "sql_command": 7, + "sql_text": 10, + "status": 5, + "tempdb_current": 18, + "unique_key": 25, + "wait_info": 6 + }, + "renameByName": { + "Time": "Collection Time", + "Value #collection_time_utc": "Collection Time", + "Value #cpu": "cpu", + "Value #granted_memory": "granted_memory", + "Value #max_used_memory": "max_used_memory", + "Value #open_tran_count": "open_tran_count", + "Value #percent_complete": "percent_complete", + "Value #physical_io": "physical_io", + "Value #physical_reads": "physical_reads", + "Value #reads": "reads", + "Value #requested_memory": "requested_memory", + "Value #start_time": "start_time", + "Value #tempdb_allocations": "tempdb_allocations", + "Value #tempdb_current": "tempdb_current", + "Value #used_memory": "used_memory", + "Value #writes": "writes" + } + } + } + ], + "type": "table" + } + ], + "title": "WhoIsActive - For Duration - Latest @ ${whoisactive_collection_time:date:YYYY-MM-DD HH:mm}", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 15 + }, + "id": 78, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Latest collected values from sys.configurations — one row per configuration setting.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 12, + "w": 24, + "x": 0, + "y": 90 + }, + "id": 77, + "options": { + "cellHeight": "sm", + "showHeader": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_config__value{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "config_value" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_config__value_in_use{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "config_value_in_use" + } + ], + "title": "Stable Info - sys.configurations - Latest", + "transformations": [ + { + "id": "merge", + "options": {} + }, + { + "id": "organize", + "options": { + "excludeByName": { + "Time": true, + "__name__": true, + "exported_job": true, + "instance": true, + "job": true, + "target": true + }, + "includeByName": {}, + "indexByName": { + "Value #config_value": 2, + "Value #config_value_in_use": 3, + "config_description": 1, + "config_name": 0 + }, + "renameByName": { + "Value #config_value": "Value", + "Value #config_value_in_use": "Value In Use", + "config_description": "Description", + "config_name": "Config Name" + } + } + } + ], + "type": "table" + } + ], + "title": "Stable Info - sys.configurations - Latest", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 16 + }, + "id": 80, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Configuration value change events within the selected time range. Each row represents one detected change between consecutive 10-minute collections.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Updated Time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 17 + }, + "id": 89, + "options": { + "cellHeight": "sm", + "showHeader": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_config__value{instance=\"$Server\"} offset 10m != mssql_config__value{instance=\"$Server\"}", + "format": "table", + "instant": false, + "interval": "10m", + "legendFormat": "__auto", + "range": true, + "refId": "old_value" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_config__value{instance=\"$Server\"} != mssql_config__value{instance=\"$Server\"} offset 10m", + "format": "table", + "instant": false, + "interval": "10m", + "legendFormat": "__auto", + "range": true, + "refId": "new_value" + } + ], + "title": "Stable Info - sys.configurations - Changed in Range", + "transformations": [ + { + "id": "merge", + "options": {} + }, + { + "id": "organize", + "options": { + "excludeByName": { + "__name__": true, + "exported_job": true, + "instance": true, + "job": true, + "target": true + }, + "indexByName": { + "Time": 0, + "Value #new_value": 4, + "Value #old_value": 3, + "config_description": 2, + "config_name": 1 + }, + "renameByName": { + "Time": "Updated Time", + "Value #new_value": "New Value", + "Value #old_value": "Old Value", + "config_description": "Description", + "config_name": "Config Name" + } + } + }, + { + "id": "sortBy", + "options": { + "fields": {}, + "sort": [ + { + "desc": true, + "field": "Updated Time" + } + ] + } + } + ], + "type": "table" + } + ], + "title": "Stable Info - sys.configurations - Changes", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 17 + }, + "id": 82, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Latest sys.databases flag and feature settings — one row per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + } + ] + }, + "gridPos": { + "h": 9, + "w": 24, + "x": 0, + "y": 1 + }, + "id": 87, + "options": { + "cellHeight": "sm", + "showHeader": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__collection_time_utc{instance=\"$Server\"} * 1000", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "collection_time_utc" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_read_only{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_read_only" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_auto_close_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_auto_close_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_auto_shrink_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_auto_shrink_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_in_standby{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_in_standby" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_cleanly_shutdown{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_cleanly_shutdown" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_supplemental_logging_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_supplemental_logging_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_read_committed_snapshot_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_read_committed_snapshot_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_auto_create_stats_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_auto_create_stats_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_auto_create_stats_incremental_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_auto_create_stats_incremental_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_auto_update_stats_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_auto_update_stats_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_auto_update_stats_async_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_auto_update_stats_async_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_ansi_null_default_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_ansi_null_default_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_ansi_nulls_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_ansi_nulls_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_ansi_padding_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_ansi_padding_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_ansi_warnings_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_ansi_warnings_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_arithabort_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_arithabort_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_concat_null_yields_null_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_concat_null_yields_null_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_numeric_roundabort_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_numeric_roundabort_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_quoted_identifier_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_quoted_identifier_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_recursive_triggers_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_recursive_triggers_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_cursor_close_on_commit_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_cursor_close_on_commit_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_local_cursor_default{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_local_cursor_default" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_fulltext_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_fulltext_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_trustworthy_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_trustworthy_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_db_chaining_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_db_chaining_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_parameterization_forced{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_parameterization_forced" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_master_key_encrypted_by_server{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_master_key_encrypted_by_server" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_query_store_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_query_store_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_published{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_published" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_subscribed{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_subscribed" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_merge_published{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_merge_published" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_distributor{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_distributor" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_sync_with_backup{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_sync_with_backup" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_broker_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_broker_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_date_correlation_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_date_correlation_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_cdc_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_cdc_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_encrypted{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_encrypted" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_honor_broker_priority_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_honor_broker_priority_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_nested_triggers_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_nested_triggers_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_transform_noise_words_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_transform_noise_words_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_memory_optimized_elevate_to_snapshot_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_memory_optimized_elevate_to_snapshot_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_federation_member{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_federation_member" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_remote_data_archive_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_remote_data_archive_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_mixed_page_allocation_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_mixed_page_allocation_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_temporal_history_retention_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_temporal_history_retention_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_result_set_caching_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_result_set_caching_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_accelerated_database_recovery_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_accelerated_database_recovery_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_tempdb_spill_to_remote_store{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_tempdb_spill_to_remote_store" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_stale_page_detection_on{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_stale_page_detection_on" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_memory_optimized_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_memory_optimized_enabled" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__is_data_retention_enabled{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "is_data_retention_enabled" + } + ], + "title": "Stable Info - sys.databases - Latest - Settings & Flags", + "transformations": [ + { + "id": "joinByField", + "options": { + "byField": "db_name", + "mode": "outer" + } + }, + { + "id": "filterFieldsByName", + "options": { + "include": { + "names": [ + "db_name", + "Value #collection_time_utc", + "Value #is_read_only", + "Value #is_auto_close_on", + "Value #is_auto_shrink_on", + "Value #is_in_standby", + "Value #is_cleanly_shutdown", + "Value #is_supplemental_logging_enabled", + "Value #is_read_committed_snapshot_on", + "Value #is_auto_create_stats_on", + "Value #is_auto_create_stats_incremental_on", + "Value #is_auto_update_stats_on", + "Value #is_auto_update_stats_async_on", + "Value #is_ansi_null_default_on", + "Value #is_ansi_nulls_on", + "Value #is_ansi_padding_on", + "Value #is_ansi_warnings_on", + "Value #is_arithabort_on", + "Value #is_concat_null_yields_null_on", + "Value #is_numeric_roundabort_on", + "Value #is_quoted_identifier_on", + "Value #is_recursive_triggers_on", + "Value #is_cursor_close_on_commit_on", + "Value #is_local_cursor_default", + "Value #is_fulltext_enabled", + "Value #is_trustworthy_on", + "Value #is_db_chaining_on", + "Value #is_parameterization_forced", + "Value #is_master_key_encrypted_by_server", + "Value #is_query_store_on", + "Value #is_published", + "Value #is_subscribed", + "Value #is_merge_published", + "Value #is_distributor", + "Value #is_sync_with_backup", + "Value #is_broker_enabled", + "Value #is_date_correlation_on", + "Value #is_cdc_enabled", + "Value #is_encrypted", + "Value #is_honor_broker_priority_on", + "Value #is_nested_triggers_on", + "Value #is_transform_noise_words_on", + "Value #is_memory_optimized_elevate_to_snapshot_on", + "Value #is_federation_member", + "Value #is_remote_data_archive_enabled", + "Value #is_mixed_page_allocation_on", + "Value #is_temporal_history_retention_enabled", + "Value #is_result_set_caching_on", + "Value #is_accelerated_database_recovery_on", + "Value #is_tempdb_spill_to_remote_store", + "Value #is_stale_page_detection_on", + "Value #is_memory_optimized_enabled", + "Value #is_data_retention_enabled" + ] + } + } + }, + { + "id": "organize", + "options": { + "indexByName": { + "Value #collection_time_utc": 1, + "Value #is_accelerated_database_recovery_on": 48, + "Value #is_ansi_null_default_on": 13, + "Value #is_ansi_nulls_on": 14, + "Value #is_ansi_padding_on": 15, + "Value #is_ansi_warnings_on": 16, + "Value #is_arithabort_on": 17, + "Value #is_auto_close_on": 3, + "Value #is_auto_create_stats_incremental_on": 10, + "Value #is_auto_create_stats_on": 9, + "Value #is_auto_shrink_on": 4, + "Value #is_auto_update_stats_async_on": 12, + "Value #is_auto_update_stats_on": 11, + "Value #is_broker_enabled": 35, + "Value #is_cdc_enabled": 37, + "Value #is_cleanly_shutdown": 6, + "Value #is_concat_null_yields_null_on": 18, + "Value #is_cursor_close_on_commit_on": 22, + "Value #is_data_retention_enabled": 52, + "Value #is_date_correlation_on": 36, + "Value #is_db_chaining_on": 26, + "Value #is_distributor": 33, + "Value #is_encrypted": 38, + "Value #is_federation_member": 43, + "Value #is_fulltext_enabled": 24, + "Value #is_honor_broker_priority_on": 39, + "Value #is_in_standby": 5, + "Value #is_local_cursor_default": 23, + "Value #is_master_key_encrypted_by_server": 28, + "Value #is_memory_optimized_elevate_to_snapshot_on": 42, + "Value #is_memory_optimized_enabled": 51, + "Value #is_merge_published": 32, + "Value #is_mixed_page_allocation_on": 45, + "Value #is_nested_triggers_on": 40, + "Value #is_numeric_roundabort_on": 19, + "Value #is_parameterization_forced": 27, + "Value #is_published": 30, + "Value #is_query_store_on": 29, + "Value #is_quoted_identifier_on": 20, + "Value #is_read_committed_snapshot_on": 8, + "Value #is_read_only": 2, + "Value #is_recursive_triggers_on": 21, + "Value #is_remote_data_archive_enabled": 44, + "Value #is_result_set_caching_on": 47, + "Value #is_stale_page_detection_on": 50, + "Value #is_subscribed": 31, + "Value #is_supplemental_logging_enabled": 7, + "Value #is_sync_with_backup": 34, + "Value #is_tempdb_spill_to_remote_store": 49, + "Value #is_temporal_history_retention_enabled": 46, + "Value #is_transform_noise_words_on": 41, + "Value #is_trustworthy_on": 25, + "db_name": 0 + }, + "renameByName": { + "Value #collection_time_utc": "collection_time_utc", + "Value #is_accelerated_database_recovery_on": "is_accelerated_database_recovery_on", + "Value #is_ansi_null_default_on": "is_ansi_null_default_on", + "Value #is_ansi_nulls_on": "is_ansi_nulls_on", + "Value #is_ansi_padding_on": "is_ansi_padding_on", + "Value #is_ansi_warnings_on": "is_ansi_warnings_on", + "Value #is_arithabort_on": "is_arithabort_on", + "Value #is_auto_close_on": "is_auto_close_on", + "Value #is_auto_create_stats_incremental_on": "is_auto_create_stats_incremental_on", + "Value #is_auto_create_stats_on": "is_auto_create_stats_on", + "Value #is_auto_shrink_on": "is_auto_shrink_on", + "Value #is_auto_update_stats_async_on": "is_auto_update_stats_async_on", + "Value #is_auto_update_stats_on": "is_auto_update_stats_on", + "Value #is_broker_enabled": "is_broker_enabled", + "Value #is_cdc_enabled": "is_cdc_enabled", + "Value #is_cleanly_shutdown": "is_cleanly_shutdown", + "Value #is_concat_null_yields_null_on": "is_concat_null_yields_null_on", + "Value #is_cursor_close_on_commit_on": "is_cursor_close_on_commit_on", + "Value #is_data_retention_enabled": "is_data_retention_enabled", + "Value #is_date_correlation_on": "is_date_correlation_on", + "Value #is_db_chaining_on": "is_db_chaining_on", + "Value #is_distributor": "is_distributor", + "Value #is_encrypted": "is_encrypted", + "Value #is_federation_member": "is_federation_member", + "Value #is_fulltext_enabled": "is_fulltext_enabled", + "Value #is_honor_broker_priority_on": "is_honor_broker_priority_on", + "Value #is_in_standby": "is_in_standby", + "Value #is_local_cursor_default": "is_local_cursor_default", + "Value #is_master_key_encrypted_by_server": "is_master_key_encrypted_by_server", + "Value #is_memory_optimized_elevate_to_snapshot_on": "is_memory_optimized_elevate_to_snapshot_on", + "Value #is_memory_optimized_enabled": "is_memory_optimized_enabled", + "Value #is_merge_published": "is_merge_published", + "Value #is_mixed_page_allocation_on": "is_mixed_page_allocation_on", + "Value #is_nested_triggers_on": "is_nested_triggers_on", + "Value #is_numeric_roundabort_on": "is_numeric_roundabort_on", + "Value #is_parameterization_forced": "is_parameterization_forced", + "Value #is_published": "is_published", + "Value #is_query_store_on": "is_query_store_on", + "Value #is_quoted_identifier_on": "is_quoted_identifier_on", + "Value #is_read_committed_snapshot_on": "is_read_committed_snapshot_on", + "Value #is_read_only": "is_read_only", + "Value #is_recursive_triggers_on": "is_recursive_triggers_on", + "Value #is_remote_data_archive_enabled": "is_remote_data_archive_enabled", + "Value #is_result_set_caching_on": "is_result_set_caching_on", + "Value #is_stale_page_detection_on": "is_stale_page_detection_on", + "Value #is_subscribed": "is_subscribed", + "Value #is_supplemental_logging_enabled": "is_supplemental_logging_enabled", + "Value #is_sync_with_backup": "is_sync_with_backup", + "Value #is_tempdb_spill_to_remote_store": "is_tempdb_spill_to_remote_store", + "Value #is_temporal_history_retention_enabled": "is_temporal_history_retention_enabled", + "Value #is_transform_noise_words_on": "is_transform_noise_words_on", + "Value #is_trustworthy_on": "is_trustworthy_on", + "db_name": "db_name" + } + } + } + ], + "type": "table" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Latest sys.databases identity, metadata, and mode-related values — one row per database.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "collection_time_utc" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + }, + { + "matcher": { + "id": "byName", + "options": "create_date" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 10 + }, + "id": 86, + "options": { + "cellHeight": "sm", + "showHeader": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__collection_time_utc{instance=\"$Server\"} * 1000", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "collection_time_utc" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__create_date{instance=\"$Server\"} * 1000", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "create_date" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__database_id{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "database_id" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__source_database_id{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "source_database_id" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__compatibility_level{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "compatibility_level" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__user_access{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "user_access" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__state{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "state" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__snapshot_isolation_state{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "snapshot_isolation_state" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__recovery_model{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "recovery_model" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__page_verify_option{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "page_verify_option" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__resource_pool_id{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "resource_pool_id" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__default_language_lcid{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "default_language_lcid" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__default_fulltext_language_lcid{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "default_fulltext_language_lcid" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__two_digit_year_cutoff{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "two_digit_year_cutoff" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__containment{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "containment" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__target_recovery_time_in_seconds{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "target_recovery_time_in_seconds" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__delayed_durability{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "delayed_durability" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "mssql_db__catalog_collation_type{instance=\"$Server\"}", + "format": "table", + "instant": true, + "legendFormat": "__auto", + "range": false, + "refId": "catalog_collation_type" + } + ], + "title": "Stable Info - sys.databases - Latest - Identity & Modes", + "transformations": [ + { + "id": "joinByField", + "options": { + "byField": "db_name", + "mode": "outer" + } + }, + { + "id": "filterFieldsByName", + "options": { + "include": { + "names": [ + "db_name", + "Value #collection_time_utc", + "Value #create_date", + "Value #database_id", + "Value #source_database_id", + "owner_sid", + "collation_name", + "service_broker_guid", + "replica_id", + "group_database_id", + "default_language_name", + "default_fulltext_language_name", + "physical_database_name", + "Value #compatibility_level", + "user_access_desc", + "Value #user_access", + "state_desc", + "Value #state", + "snapshot_isolation_state_desc", + "Value #snapshot_isolation_state", + "recovery_model_desc", + "Value #recovery_model", + "page_verify_option_desc", + "Value #page_verify_option", + "Value #resource_pool_id", + "Value #default_language_lcid", + "Value #default_fulltext_language_lcid", + "containment_desc", + "Value #containment", + "Value #target_recovery_time_in_seconds", + "delayed_durability_desc", + "Value #delayed_durability", + "catalog_collation_type_desc", + "Value #catalog_collation_type", + "Value #two_digit_year_cutoff" + ] + } + } + }, + { + "id": "organize", + "options": { + "indexByName": { + "Value #catalog_collation_type": 29, + "Value #collection_time_utc": 1, + "Value #compatibility_level": 13, + "Value #containment": 25, + "Value #create_date": 2, + "Value #database_id": 3, + "Value #default_fulltext_language_lcid": 32, + "Value #default_language_lcid": 31, + "Value #delayed_durability": 27, + "Value #page_verify_option": 23, + "Value #recovery_model": 21, + "Value #resource_pool_id": 30, + "Value #snapshot_isolation_state": 19, + "Value #source_database_id": 4, + "Value #state": 17, + "Value #target_recovery_time_in_seconds": 33, + "Value #two_digit_year_cutoff": 34, + "Value #user_access": 15, + "catalog_collation_type_desc": 28, + "collation_name": 6, + "containment_desc": 24, + "db_name": 0, + "default_fulltext_language_name": 12, + "default_language_name": 11, + "delayed_durability_desc": 26, + "group_database_id": 10, + "owner_sid": 5, + "page_verify_option_desc": 22, + "physical_database_name": 7, + "recovery_model_desc": 20, + "replica_id": 9, + "service_broker_guid": 8, + "snapshot_isolation_state_desc": 18, + "state_desc": 16, + "user_access_desc": 14 + }, + "renameByName": { + "Value #catalog_collation_type": "catalog_collation_type", + "Value #collection_time_utc": "collection_time_utc", + "Value #compatibility_level": "compatibility_level", + "Value #containment": "containment", + "Value #create_date": "create_date", + "Value #database_id": "database_id", + "Value #default_fulltext_language_lcid": "default_fulltext_language_lcid", + "Value #default_language_lcid": "default_language_lcid", + "Value #delayed_durability": "delayed_durability", + "Value #page_verify_option": "page_verify_option", + "Value #recovery_model": "recovery_model", + "Value #resource_pool_id": "resource_pool_id", + "Value #snapshot_isolation_state": "snapshot_isolation_state", + "Value #source_database_id": "source_database_id", + "Value #state": "state", + "Value #target_recovery_time_in_seconds": "target_recovery_time_in_seconds", + "Value #two_digit_year_cutoff": "two_digit_year_cutoff", + "Value #user_access": "user_access", + "catalog_collation_type_desc": "catalog_collation_type_desc", + "collation_name": "collation_name", + "containment_desc": "containment_desc", + "db_name": "db_name", + "default_fulltext_language_name": "default_fulltext_language_name", + "default_language_name": "default_language_name", + "delayed_durability_desc": "delayed_durability_desc", + "group_database_id": "group_database_id", + "owner_sid": "owner_sid", + "page_verify_option_desc": "page_verify_option_desc", + "physical_database_name": "physical_database_name", + "recovery_model_desc": "recovery_model_desc", + "replica_id": "replica_id", + "service_broker_guid": "service_broker_guid", + "snapshot_isolation_state_desc": "snapshot_isolation_state_desc", + "state_desc": "state_desc", + "user_access_desc": "user_access_desc" + } + } + } + ], + "type": "table" + } + ], + "title": "Stable Info - sys.databases - Latest", + "type": "row" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 18 + }, + "id": 84, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "description": "Database setting change events within the selected time range. Each row represents one detected change between consecutive 10-minute collections.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "custom": { + "align": "auto", + "cellOptions": { + "type": "auto" + }, + "filterable": true, + "footer": { + "reducers": [] + }, + "inspect": false + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": 0 + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [ + { + "matcher": { + "id": "byName", + "options": "Updated Time" + }, + "properties": [ + { + "id": "unit", + "value": "dateTimeAsIso" + } + ] + } + ] + }, + "gridPos": { + "h": 10, + "w": 24, + "x": 0, + "y": 19 + }, + "id": 90, + "options": { + "cellHeight": "sm", + "showHeader": true + }, + "pluginVersion": "12.4.1", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "label_join(label_replace(max without(state_desc) (mssql_db__state{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__state{instance=\"$Server\"}), \"config_name\", \"state\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(max without(recovery_model_desc) (mssql_db__recovery_model{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__recovery_model{instance=\"$Server\"}), \"config_name\", \"recovery_model\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__compatibility_level{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__compatibility_level{instance=\"$Server\"}, \"config_name\", \"compatibility_level\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_read_only{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__is_read_only{instance=\"$Server\"}, \"config_name\", \"is_read_only\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_trustworthy_on{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__is_trustworthy_on{instance=\"$Server\"}, \"config_name\", \"is_trustworthy_on\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_cdc_enabled{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__is_cdc_enabled{instance=\"$Server\"}, \"config_name\", \"is_cdc_enabled\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_query_store_on{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__is_query_store_on{instance=\"$Server\"}, \"config_name\", \"is_query_store_on\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_encrypted{instance=\"$Server\"} offset 10m != on(db_name) mssql_db__is_encrypted{instance=\"$Server\"}, \"config_name\", \"is_encrypted\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\")", + "format": "table", + "instant": false, + "interval": "10m", + "legendFormat": "__auto", + "range": true, + "refId": "old_db_value" + }, + { + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "editorMode": "code", + "exemplar": false, + "expr": "label_join(label_replace(max without(state_desc) (mssql_db__state{instance=\"$Server\"} != on(db_name) mssql_db__state{instance=\"$Server\"} offset 10m), \"config_name\", \"state\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(max without(recovery_model_desc) (mssql_db__recovery_model{instance=\"$Server\"} != on(db_name) mssql_db__recovery_model{instance=\"$Server\"} offset 10m), \"config_name\", \"recovery_model\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__compatibility_level{instance=\"$Server\"} != on(db_name) mssql_db__compatibility_level{instance=\"$Server\"} offset 10m, \"config_name\", \"compatibility_level\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_read_only{instance=\"$Server\"} != on(db_name) mssql_db__is_read_only{instance=\"$Server\"} offset 10m, \"config_name\", \"is_read_only\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_trustworthy_on{instance=\"$Server\"} != on(db_name) mssql_db__is_trustworthy_on{instance=\"$Server\"} offset 10m, \"config_name\", \"is_trustworthy_on\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_cdc_enabled{instance=\"$Server\"} != on(db_name) mssql_db__is_cdc_enabled{instance=\"$Server\"} offset 10m, \"config_name\", \"is_cdc_enabled\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_query_store_on{instance=\"$Server\"} != on(db_name) mssql_db__is_query_store_on{instance=\"$Server\"} offset 10m, \"config_name\", \"is_query_store_on\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\") or label_join(label_replace(mssql_db__is_encrypted{instance=\"$Server\"} != on(db_name) mssql_db__is_encrypted{instance=\"$Server\"} offset 10m, \"config_name\", \"is_encrypted\", \"db_name\", \".*\"), \"change_key\", \" :: \", \"db_name\", \"config_name\")", + "format": "table", + "instant": false, + "interval": "10m", + "legendFormat": "__auto", + "range": true, + "refId": "new_db_value" + } + ], + "title": "Stable Info - sys.databases - Changed in Range", + "transformations": [ + { + "id": "merge", + "options": {} + }, + { + "id": "organize", + "options": { + "excludeByName": { + "__name__": true, + "change_key": true, + "exported_job": true, + "instance": true, + "job": true, + "recovery_model_desc": true, + "state_desc": true, + "target": true + }, + "indexByName": { + "Time": 0, + "Value #new_db_value": 4, + "Value #old_db_value": 3, + "config_name": 2, + "db_name": 1 + }, + "renameByName": { + "Time": "Updated Time", + "Value #new_db_value": "New Value", + "Value #old_db_value": "Old Value", + "config_name": "Config Name", + "db_name": "Database Name" + } + } + }, + { + "id": "sortBy", + "options": { + "fields": {}, + "sort": [ + { + "desc": true, + "field": "Updated Time" + } + ] + } + } + ], + "type": "table" + } + ], + "title": "Stable Info - sys.databases - Changes", + "type": "row" + } + ], + "preload": false, + "refresh": "30s", + "schemaVersion": 42, + "tags": [ + "SQL Server", + "sql_exporter", + "v2", + "prometheus" + ], + "templating": { + "list": [ + { + "current": { + "text": "", + "value": "${DS_PROMETHEUS}", + "selected": true + }, + "description": "Prometheus data source for SQL Exporter metrics", + "hide": 2, + "includeAll": false, + "label": "Data Source", + "name": "DS_PROMETHEUS", + "options": [], + "query": "prometheus", + "refresh": 1, + "regex": "", + "type": "datasource" + }, + { + "current": {}, + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "definition": "label_values(mssql_up, instance)", + "description": "Select exactly one Prometheus target/server at a time. Multi-select is disabled by design.", + "includeAll": false, + "label": "Server", + "name": "Server", + "options": [], + "query": "label_values(mssql_up, instance)", + "refresh": 1, + "regex": "", + "regexApplyTo": "value", + "sort": 1, + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "definition": "query_result(mssql_local_time_seconds{instance=\"$Server\"})", + "hide": 2, + "name": "local_time_seconds", + "options": [], + "query": { + "qryType": 3, + "query": "query_result(mssql_local_time_seconds{instance=\"$Server\"})", + "refId": "PrometheusVariableQueryEditor-VariableQuery" + }, + "refresh": 2, + "regex": "/.* ([0-9]+(\\.[0-9]+)?).*/", + "regexApplyTo": "value", + "type": "query" + }, + { + "current": {}, + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "definition": "query_result(max(mssql_sqlserver_cpu_count{instance=\"$Server\"}))", + "hide": 2, + "label": "SQL CPU Count", + "name": "SQLCPUCount", + "options": [], + "query": { + "qryType": 3, + "query": "query_result(max(mssql_sqlserver_cpu_count{instance=\"$Server\"}))", + "refId": "PrometheusVariableQueryEditor-VariableQuery" + }, + "refresh": 1, + "regex": "/.*} ([0-9]+(\\.[0-9]+)?)( .*)?/", + "regexApplyTo": "value", + "type": "query" + }, + { + "description": "", + "hide": 2, + "label": "TOP_K_WAITS", + "name": "TOP_K_WAITS", + "query": "${VAR_TOP_K_WAITS}", + "skipUrlSync": true, + "type": "constant", + "current": { + "value": "${VAR_TOP_K_WAITS}", + "text": "${VAR_TOP_K_WAITS}", + "selected": false + }, + "options": [ + { + "value": "${VAR_TOP_K_WAITS}", + "text": "${VAR_TOP_K_WAITS}", + "selected": false + } + ] + }, + { + "allowCustomValue": false, + "current": {}, + "datasource": { + "type": "prometheus", + "uid": "${DS_PROMETHEUS}" + }, + "definition": "query_result(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} * 1000)", + "hide": 2, + "name": "whoisactive_collection_time", + "options": [], + "query": { + "qryType": 3, + "query": "query_result(mssql_whoisactive__collection_time_utc{instance=\"$Server\"} * 1000)", + "refId": "PrometheusVariableQueryEditor-VariableQuery" + }, + "refresh": 2, + "regex": "/.* ([0-9]+(\\.[0-9]+)?).*/", + "regexApplyTo": "value", + "type": "query" + } + ] + }, + "time": { + "from": "now-3h", + "to": "now" + }, + "timepicker": {}, + "timezone": "browser", + "title": "SQL Exporter Metrics", + "uid": "sql-exporter-metrics-v2", + "version": 55, + "weekStart": "" +} \ No newline at end of file diff --git a/sql_exporter/README-sql_exporter.md b/sql_exporter/README-sql_exporter.md new file mode 100644 index 0000000..970f7eb --- /dev/null +++ b/sql_exporter/README-sql_exporter.md @@ -0,0 +1,265 @@ +# [sql_exporter](https://github.com/burningalchemist/sql_exporter) + +## Create windows service +``` +New-Service -Name "sql_exporter" ` + -BinaryPathName "E:\Github\SQLMonitor\sql_exporter\sql_exporter.exe --config.file E:\Github\SQLMonitor\sql_exporter\sql_exporter.yml" ` + -StartupType Automatic ` + -DisplayName "SQL Exporter for Prometheus" + +# Set service account to Lab\SQLService + +# Start the service +Start-Service sql_exporter + +# Check the service status +Get-Service sql_exporter + +# Remove the service +Stop-Service sql_exporter +sc.exe delete sql_exporter + +# Add firewall rule +New-NetFirewallRule -DisplayName "SQL Exporter (TCP/9399)" -Direction Inbound -Protocol TCP -LocalPort 9399 -Action Allow + +# Validate at http://localhost:9399/metrics +``` + +## Create mac service +``` + +sudo launchctl list | grep prometheus +brew services list | grep prometheus + +brew services info prometheus + +cat $(brew --prefix)/etc/prometheus.yml +brew services restart prometheus + +# Update grafana +vim /opt/homebrew/etc/grafana/grafana.ini +brew services restart grafana +``` + +### Validate on Mac - Service Management + +| Action | Command | +|---------------|---| +| Check status | `sudo launchctl list \| grep sql_exporter` | +| Stop | `sudo launchctl unload /Library/LaunchDaemons/com.sql_exporter.plist` | +| Start | `sudo launchctl load /Library/LaunchDaemons/com.sql_exporter.plist` | +| View logs | `tail -f /usr/local/var/log/sql_exporter.log` | +| View errors | `tail -f /usr/local/var/log/sql_exporter.err.log` | +| Test metrics | `curl -s http://localhost:9399/metrics \| head -20` | + +The service auto-starts on every reboot and auto-restarts if it crashes (KeepAlive). + +# How to add in Prometheus + +``` +|------------$ sudo cat /etc/prometheus/prometheus.yml + + +# my global config +global: + scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. + evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. + # scrape_timeout is set to the global default (10s). + +# Alertmanager configuration +alerting: + alertmanagers: + - static_configs: + - targets: + # - alertmanager:9093 + +# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. +rule_files: + - "prometheus.rules.yml" + # - "second_rules.yml" + +# A scrape configuration containing exactly one endpoint to scrape: +# Here it's Prometheus itself. +scrape_configs: + # The job name is added as a label `job=` to any timeseries scraped from this config. + - job_name: "prometheus" + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ["localhost:9091"] + # The label name is added as a label `label_name=` to any timeseries scraped from this config. + labels: + app: "prometheus" + + - job_name: "node_exporter" + static_configs: + - targets: + - "localhost:9100" + + - job_name: 'win-exporter' + static_configs: + - targets: + - 'sqlmonitor:9182' + + - job_name: mssql_exporter_common + scrape_interval: 30s + params: + 'jobs[]': [mssql_common] + static_configs: + - targets: + - "sqlmonitor:9399" + - "AgHost-1A:9399" + - "AgHost-1B:9399" + + - job_name: mssql_exporter_longrunning + scrape_interval: 2m + params: + 'jobs[]': [mssql_long_running] + static_configs: + - targets: + - "sqlmonitor:9399" + - "AgHost-1A:9399" + - "AgHost-1B:9399" + + - job_name: mssql_exporter_ag + scrape_interval: 30s + params: + 'jobs[]': [mssql_ag] + static_configs: + - targets: + - "AgHost-1A:9399" + - "AgHost-1B:9399" + +``` + + +# Collectors + +`sql_exporter.yml` binds each collector file (`mssql_*.collector.yml`) to a +job; every target in the job runs every metric definition in the named +collectors. + +| Collector file | Job | Scrape interval | What it publishes | +|---|---|---|---| +| `mssql_standard.collector.yml` | `mssql_common` | default | Upstream [sql_exporter](https://github.com/burningalchemist/sql_exporter/tree/master/examples/mssql_standard) baseline — `mssql_perfmon__*`, `mssql_up`, `mssql_sqlserver_*`. | +| `mssql_dba_cached.collector.yml` | `mssql_common` | 1m | `mssql_virtualfilestats__*`, `mssql_waits__*`, `mssql_cpu_utilization__*`. | +| `mssql_dba_regular.collector.yml` | `mssql_common` | default | `mssql_service_info`, `mssql_db_state`, registry/config snapshot. | +| `mssql_dba_stableinfo.collector.yml` | `mssql_common` | 10m | Low-churn info (CPU count, version, clustering). | +| `mssql_dba_aghealth.collector.yml` | `mssql_ag` | 1m | `mssql_aghealth__*` (AG sync_health / latency / queues). | +| `mssql_dba_whoisactive.collector.yml` | `mssql_long_running` | 2m | `mssql_whoisactive__*` — current sp_WhoIsActive snapshot. | +| `mssql_sqlagent_jobs.collector.yml` | `mssql_msdb` | 1m | `mssql_sqlagent_job__*` — job enabled / outcome / duration / next run / 24h step failures. | +| `mssql_backup_history.collector.yml` | `mssql_msdb` | 5m | `mssql_backup__*` — per-(db, backup_type) last-time / size / duration / age. | +| `mssql_xevent.collector.yml` | `mssql_xevent` | 1m | `mssql_xevent__*` — 5-minute aggregates of `DBA.dbo.xevent_metrics` (guarded; no-op where the XEvent proc isn't installed). | + +The three **msdb / xevent** collectors are new in Phase 1 of the Prometheus +dashboard rollout and feed the new Grafana dashboards under +`sql_exporter/Prometheus-Dashboards/`. See [`Prometheus-Dashboards/README.md`](Prometheus-Dashboards/README.md) +for the generator / spec workflow and the per-dashboard panel inventory. + + +# Refresh Collectors +``` +# E:\Github\SQLMonitor\sql_exporter\sql_exporter.exe --config.file E:\Github\SQLMonitor\sql_exporter\sql_exporter.yml + +# Get local data collectors in Github Repo +$collectors = Get-ChildItem -Path "E:\Github\SQLMonitor\sql_exporter\mssql_*.collector.yml" + +# Remove old collectors from remote machines +Get-ChildItem "\\aghost-1a\d$\sql_exporter\mssql_*.collector.yml" | ForEach-Object {if($_.Name -notin $collectors.Name){$_}} | Remove-Item +Get-ChildItem "\\aghost-1b\d$\sql_exporter\mssql_*.collector.yml" | ForEach-Object {if($_.Name -notin $collectors.Name){$_}} | Remove-Item + +# Copy sql_exporter.yml config file +Get-ChildItem "E:\Github\SQLMonitor\sql_exporter\sql_exporter.yml" | Copy-Item -Destination "\\aghost-1a\d$\sql_exporter\" -Verbose -Force +Get-ChildItem "E:\Github\SQLMonitor\sql_exporter\sql_exporter.yml" | Copy-Item -Destination "\\aghost-1a\d$\sql_exporter\" -Verbose -Force + +# Add new collectors to remote machines +$collectors | Copy-Item -Destination "\\aghost-1a\d$\sql_exporter\" -Verbose -Force +$collectors | Copy-Item -Destination "\\aghost-1b\d$\sql_exporter\" -Verbose -Force + +# Restart sql_exporter service +# get-service sql_exporter | Stop-Service -PassThru +get-service sql_exporter | Restart-Service -PassThru +Invoke-Command -ComputerName aghost-1a -ScriptBlock {get-service sql_exporter | Restart-Service -PassThru} +Invoke-Command -ComputerName aghost-1b -ScriptBlock {get-service sql_exporter | Restart-Service -PassThru} + +``` + + +# PasteThePlan Bridge +- Uses `sql_exporter/pastetheplan-proxy.py` to bridge Grafana data link to PasteThePlan website. + - Reads the raw query plan XML from the ?xml= URL query parameter + - POSTs `{"queryplan_xml": ""}` to the same AWS Lambda API that the `sqlops-pastetheplan` extension uses + - Redirects the browser to `https://www.brentozar.com/pastetheplan/?id=` +``` + +# Step 1 — Start the local HTTP server +cd sql_exporter + +python3 pastetheplan-proxy.py # Prometheus on localhost:9090 +# OR if Prometheus is elsewhere: +python3 pastetheplan-proxy.py 8080 http://your-prometheus-host:9090 + +# → PasteThePlan proxy → http://localhost:8080/pastetheplan-bridge.html + +# Step 2 — Import the updated dashboard JSON into Grafana. +# Step 3 — Click any non-empty query_plan cell → a "Paste The Plan" icon/link appears → click it → the bridge page opens, submits the XML, and auto-redirects you to the Brent Ozar plan viewer. +``` + + +# Grafana Dashboard Specifications for AI Tool + +``` +# Step 01 +I want you to check dashboards present in folder Grafana-Dashboards. Don't modify them. Just read to check all metrics that are covered. +Similarly, read procedure code in file DDLs\SCH-usp_collect_performance_metrics.sql. Check all the metrics that are collected in this procedure also. + +Ensure that any metric/data that is found in Step 01 are present in sql_exporter\mssql_*.collector.yml files. If missing, add them in dba collector. + +# Step 02 +Copy perfmon dashboard Grafana-Dashboards\Monitoring - Perfmon Counters - Quest Softwares - Distributed.json to sql_exporter directory. +Modify queries of this new perfmon dashboard to use sql_exporter collected metrics. Don't change panels layout or thresholds or titles. +Just change datasource and queries. + +Do this activity with Resumable Retry Plan. That means, break the work into multiple steps. keep saving intermediate result and progress so that you can resume the timed out work from last step. + + + + +I want to build histogram panel in grafana based on mssql_waits__* metrics. + +I want to have maximum 12 vertical sticks. Say $wait_sticks = 12. + +Assume user want wait treand/histrogram for 2 hours. So internal time should be 120 minutes divide $wait_sticks = 10. + +So take value of mssql_waits__wait_time_seconds for every 10 minutes, subtract its previous value and plot it in histogram. + +Give me grafana promQL for same. + +Wait Histogram Query +----------------------- +clamp_min(max by (wait_type) (mssql_waits__wait_time_seconds{instance=~"$Server"}) - max by (wait_type) (mssql_waits__wait_time_seconds{instance=~"$Server"} offset $wait_bucket), 0) > 0 + +clamp_min(max by (wait_type) (mssql_waits__wait_time_seconds{instance=~"sqlmonitor:9399"}) - max by (wait_type) (mssql_waits__wait_time_seconds{instance=~"sqlmonitor:9399"} offset $wait_bucket), 0) > 0 + +rate(mssql_log_growths{instance=\"$Server\"}[$__rate_interval]) +``` + +# Prompt for Augment to build data collector based on query result + +``` +Use below query to create metrics for ag health into aghealth collector. +Just like earlier, each numeric value should become a metric. +Name should be like mssql_aghealth__ +Query result is unique for a combination of (replica_server_name, ag_name, database_name). So using these 3 columns, I have created a unique key column named [unique_key]. Use this as key label in all metrics. +Columns having numeric value as we as another column with description should be clubbed as one where description columnd should be used as label. +All the remaining string columns should go as label for metric mssql_aghealth__synchronization_health. + + + + +``` + + + diff --git a/sql_exporter/_README-PromQL-Querying.md b/sql_exporter/_README-PromQL-Querying.md new file mode 100644 index 0000000..35cf1e7 --- /dev/null +++ b/sql_exporter/_README-PromQL-Querying.md @@ -0,0 +1,98 @@ +# PromQL Querying +- [PromQL Tutorial: A COMPLETE Guide to Prometheus Queries](https://www.youtube.com/watch?v=RC1ivt-ZN_U) +- [Query Functions](https://prometheus.io/docs/prometheus/latest/querying/functions/) + +## Some functions +- `delta(v range-vector)` - Calculates the difference between the first and last value of each time series element in a range vector. +- `idelta(v range-vector)` - Calculates the difference between the last two samples in a range vector. +- `deriv(v range-vector)` - Calculates the per-second derivative of the time series in a range vector. +- `rate(v range-vector)` - Calculates the per-second average rate of increase of the time series in a range vector. +- `irate(v range-vector)` - Calculates the per-second instant rate of increase of the time series in a range vector. +- `increase(v range-vector)` - Calculates the increase in the time series in a range vector. +- `increase_by_time(v range-vector, t delta_time)` - Calculates the increase in the time series in a range vector over a specified time interval. + +## Examples + +### Subquery +``` +# Returns the rate of increase of http_requests_total over the last 5 minutes for the last 30 minutes with a 1 minute resolution. +rate(http_requests_total[5m])[30m:1m] + +# Get top 15 wait types by resource time increase in last 5 minutes +topk(15, (increase(mssql_waits__resource_time_seconds{instance="AgHost-1A:9399"}[1m])) > 0 ) + +topk(15, (increase(mssql_waits__resource_time_seconds{instance="$exporter_instance"}[$__interval])) > 0 ) + +``` + +### [Using functions, operators, etc](https://prometheus.io/docs/prometheus/latest/querying/examples/#using-functions-operators-etc) +``` +sum by (job) ( + rate(http_requests_total[5m]) +) +``` + +## exporter_instance variable +``` +Name -> exporter_instance +Query + Query type -> Label Values + Label* -> instance + Metric -> mssql_up +``` + +## PromQL Metric Value Extraction +``` +Name -> sql_instance +Query + Query type -> Query result + Query -> mssql_local_time_seconds{instance="$exporter_instance"} + Regex -> /.* ([0-9]+(\.[0-9]+)?).*/ + + +# In grafana row +Collected @ ${local_time_seconds:date:YYYY-MM-DD HH:mm:ss} + +# In Stats Panel +$local_time_seconds + +Standard Options + Unit -> dateTimeAsIso +``` + +## Wait Stats +``` +$TOP_K_WAITS = 12 + +# Top 12 wait_types by [wait_percentage] +topk($TOP_K_WAITS, mssql_waits__wait_percentage{instance="$Server"}) + +# Top 12 wait_types by [wait_time_seconds] +topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"}) + +# [resource_time_seconds] for Top 12 wait_types by [wait_time_seconds] +mssql_waits__resource_time_seconds{instance="$Server"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"}) + +# [signal_time_seconds] for Top 12 wait_types by [wait_time_seconds] +mssql_waits__signal_time_seconds{instance="$Server"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"}) + +# [waiting_tasks_count] for Top 12 wait_types by [wait_time_seconds] +mssql_waits__waiting_tasks_count{instance="$Server"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"}) + +# [wait_percentage] for Top 12 wait_types by [wait_time_seconds] +mssql_waits__wait_percentage{instance="$Server"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"}) + +# [time_sampled_seconds] for Top 12 wait_types by [wait_time_seconds] +0*topk($TOP_K_WAITS,mssql_waits__wait_time_seconds{instance="$Server"}) + scalar(mssql_sqlserver_uptime_seconds{instance="$Server"}) + +# [waits_per_core_per_minute] for Top 12 wait_types by [wait_time_seconds] +topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"}) / ${SQLCPUCount:raw} / (scalar(mssql_sqlserver_uptime_seconds{instance="$Server"}) / 60) + +# [percent_signal_wait] for Top 12 wait_types by [wait_time_seconds] +100 * (mssql_waits__signal_time_seconds{instance="$Server"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"})) / (topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"})) + +# [avg_time_per_wait_seconds] for Top 12 wait_types by [wait_time_seconds] +(topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"})) / (mssql_waits__waiting_tasks_count{instance="$Server"} and on(wait_type) topk($TOP_K_WAITS, mssql_waits__wait_time_seconds{instance="$Server"})) +``` + + diff --git a/sql_exporter/alert-engine/Alert-Rules-SQLMonitor.yaml b/sql_exporter/alert-engine/Alert-Rules-SQLMonitor.yaml new file mode 100644 index 0000000..73af956 --- /dev/null +++ b/sql_exporter/alert-engine/Alert-Rules-SQLMonitor.yaml @@ -0,0 +1,264 @@ +apiVersion: 1 +# Note: Grafana does not interpolate environment variables inside groups[].rules[].data.model. +# Replace __SQLMONITOR_PROMETHEUS_DATASOURCE_UID__ during deployment scripting. +groups: + - orgId: 1 + name: SQLMonitor + folder: $SQLMONITOR_GRAFANA_ALERT_FOLDER + interval: 1m + rules: + - uid: sqlmCpu5m80 + title: CPU > 80% for 5m + condition: C + data: + - refId: A + relativeTimeRange: + from: 600 + to: 0 + datasourceUid: $SQLMONITOR_PROMETHEUS_DATASOURCE_UID + model: + datasource: + type: prometheus + uid: __SQLMONITOR_PROMETHEUS_DATASOURCE_UID__ + editorMode: code + expr: | + label_replace( + 100 - avg by (instance) ( + rate(windows_cpu_time_total{mode="idle"}[1m]) + ) * 100, + "server", "$1", "instance", "([^:]+)(?::.*)?" + ) + instant: true + intervalMs: 1000 + legendFormat: __auto + maxDataPoints: 43200 + range: false + refId: A + - refId: B + relativeTimeRange: + from: 0 + to: 0 + datasourceUid: __expr__ + model: + datasource: + type: __expr__ + uid: __expr__ + expression: A + intervalMs: 1000 + maxDataPoints: 43200 + reducer: last + refId: B + settings: + mode: dropNN + type: reduce + - refId: C + relativeTimeRange: + from: 0 + to: 0 + datasourceUid: __expr__ + model: + conditions: + - evaluator: + params: + - 80 + type: gt + operator: + type: and + query: + params: + - C + reducer: + params: [] + type: last + type: query + datasource: + type: __expr__ + uid: __expr__ + expression: B + intervalMs: 1000 + maxDataPoints: 43200 + refId: C + type: threshold + noDataState: OK + execErrState: Error + for: 5m + annotations: + summary: '{{ $labels.server }} CPU is above 80% for 5 minutes' + labels: + name: CpuHigh-SQLMonitor + target: sqlmonitor-default + isPaused: false + + - uid: sqlmMem5mLow + title: Available memory below min(4 GB, 10%) for 5m + condition: C + data: + - refId: A + relativeTimeRange: + from: 600 + to: 0 + datasourceUid: $SQLMONITOR_PROMETHEUS_DATASOURCE_UID + model: + datasource: + type: prometheus + uid: __SQLMONITOR_PROMETHEUS_DATASOURCE_UID__ + editorMode: code + expr: | + clamp_max( + label_replace( + mssql_host_physical_memory_bytes{state="total"}, + "server", "$1", "instance", "([^:]+)(?::.*)?" + ) * 0.10, + 4 * 1024 * 1024 * 1024 + ) + - on(server) + label_replace( + windows_memory_available_bytes, + "server", "$1", "instance", "([^:]+)(?::.*)?" + ) + instant: true + intervalMs: 1000 + legendFormat: __auto + maxDataPoints: 43200 + range: false + refId: A + - refId: B + relativeTimeRange: + from: 0 + to: 0 + datasourceUid: __expr__ + model: + datasource: + type: __expr__ + uid: __expr__ + expression: A + intervalMs: 1000 + maxDataPoints: 43200 + reducer: last + refId: B + settings: + mode: dropNN + type: reduce + - refId: C + relativeTimeRange: + from: 0 + to: 0 + datasourceUid: __expr__ + model: + conditions: + - evaluator: + params: + - 0 + type: gt + operator: + type: and + query: + params: + - C + reducer: + params: [] + type: last + type: query + datasource: + type: __expr__ + uid: __expr__ + expression: B + intervalMs: 1000 + maxDataPoints: 43200 + refId: C + type: threshold + noDataState: OK + execErrState: Error + for: 5m + annotations: + summary: '{{ $labels.server }} available memory is below min(4 GB, 10%) for 5 minutes' + labels: + name: MemoryLow-SQLMonitor + target: sqlmonitor-default + isPaused: false + + - uid: sqlmDisk70 + title: Disk utilization > 70% (latest scrape) + condition: C + data: + - refId: A + relativeTimeRange: + from: 600 + to: 0 + datasourceUid: $SQLMONITOR_PROMETHEUS_DATASOURCE_UID + model: + datasource: + type: prometheus + uid: __SQLMONITOR_PROMETHEUS_DATASOURCE_UID__ + editorMode: code + expr: | + label_replace( + 100 * ( + 1 - ( + windows_logical_disk_free_bytes{volume!~"HarddiskVolume.*"} + / + clamp_min(windows_logical_disk_size_bytes{volume!~"HarddiskVolume.*"}, 1) + ) + ), + "server", "$1", "instance", "([^:]+)(?::.*)?" + ) + instant: true + intervalMs: 1000 + legendFormat: __auto + maxDataPoints: 43200 + range: false + refId: A + - refId: B + relativeTimeRange: + from: 0 + to: 0 + datasourceUid: __expr__ + model: + datasource: + type: __expr__ + uid: __expr__ + expression: A + intervalMs: 1000 + maxDataPoints: 43200 + reducer: last + refId: B + settings: + mode: dropNN + type: reduce + - refId: C + relativeTimeRange: + from: 0 + to: 0 + datasourceUid: __expr__ + model: + conditions: + - evaluator: + params: + - 70 + type: gt + operator: + type: and + query: + params: + - C + reducer: + params: [] + type: last + type: query + datasource: + type: __expr__ + uid: __expr__ + expression: B + intervalMs: 1000 + maxDataPoints: 43200 + refId: C + type: threshold + noDataState: OK + execErrState: Error + for: 0s + annotations: + summary: '{{ $labels.server }} volume {{ $labels.volume }} is above 70% utilized' + labels: + name: DiskHigh-SQLMonitor + target: sqlmonitor-default + isPaused: false diff --git a/sql_exporter/alert-engine/Contact-Points-SQLMonitor.yaml b/sql_exporter/alert-engine/Contact-Points-SQLMonitor.yaml new file mode 100644 index 0000000..f1683d0 --- /dev/null +++ b/sql_exporter/alert-engine/Contact-Points-SQLMonitor.yaml @@ -0,0 +1,51 @@ +apiVersion: 1 +contactPoints: + - orgId: 1 + name: sqlmonitor-email + receivers: + - uid: sqlmonitoremail + type: email + settings: + addresses: $SQLMONITOR_EMAIL_ADDRESSES + singleEmail: false + disableResolveMessage: false + + - orgId: 1 + name: sqlmonitor-slack + receivers: + - uid: sqlmonitorslack + type: slack + settings: + recipient: $SQLMONITOR_SLACK_CHANNEL + url: $SQLMONITOR_SLACK_WEBHOOK_URL + disableResolveMessage: false + + - orgId: 1 + name: sqlmonitor-teams + receivers: + - uid: sqlmonitorteams + type: teams + settings: + url: $SQLMONITOR_TEAMS_WEBHOOK_URL + disableResolveMessage: false + + - orgId: 1 + name: sqlmonitor-default + receivers: + - uid: sqlmonitordefaultemail + type: email + settings: + addresses: $SQLMONITOR_EMAIL_ADDRESSES + singleEmail: false + disableResolveMessage: false + - uid: sqlmonitordefaultslack + type: slack + settings: + recipient: $SQLMONITOR_SLACK_CHANNEL + url: $SQLMONITOR_SLACK_WEBHOOK_URL + disableResolveMessage: false + - uid: sqlmonitordefaultteams + type: teams + settings: + url: $SQLMONITOR_TEAMS_WEBHOOK_URL + disableResolveMessage: false diff --git a/sql_exporter/alert-engine/Notification-Policies-SQLMonitor.yaml b/sql_exporter/alert-engine/Notification-Policies-SQLMonitor.yaml new file mode 100644 index 0000000..fe4da21 --- /dev/null +++ b/sql_exporter/alert-engine/Notification-Policies-SQLMonitor.yaml @@ -0,0 +1,46 @@ +apiVersion: 1 +policies: + - orgId: 1 + receiver: sqlmonitor-email + group_by: + - alertname + routes: + - receiver: sqlmonitor-slack + group_by: + - server + - volume + - alertname + object_matchers: + - - target + - "=" + - sqlmonitor-slack + + - receiver: sqlmonitor-email + group_by: + - server + - volume + - alertname + object_matchers: + - - target + - "=" + - sqlmonitor-email + + - receiver: sqlmonitor-teams + group_by: + - server + - volume + - alertname + object_matchers: + - - target + - "=" + - sqlmonitor-teams + + - receiver: sqlmonitor-default + group_by: + - server + - volume + - alertname + object_matchers: + - - target + - "=" + - sqlmonitor-default diff --git a/sql_exporter/generate_sql_exporter_dashboard.py b/sql_exporter/generate_sql_exporter_dashboard.py new file mode 100644 index 0000000..97e9cf3 --- /dev/null +++ b/sql_exporter/generate_sql_exporter_dashboard.py @@ -0,0 +1,283 @@ +import json +import pathlib +import re + +ROOT = pathlib.Path(__file__).resolve().parent +METRICS_FILE = ROOT / "sql_exporter_metrics.txt" +OUTPUT_FILE = ROOT / "SQL-Exporter-Metrics-All-Metrics-Dashboard.json" +PROM = {"type": "prometheus", "uid": "${DS_PROMETHEUS}"} + + +def parse_metric_meta(path: pathlib.Path): + meta = {} + for line in path.read_text().splitlines(): + if line.startswith("# HELP "): + rest = line[7:] + name, help_text = rest.split(" ", 1) + meta.setdefault(name, {})["help"] = help_text + elif line.startswith("# TYPE "): + rest = line[7:] + name, metric_type = rest.split(" ", 1) + meta.setdefault(name, {})["type"] = metric_type + elif line and not line.startswith("#"): + m = re.match(r"([a-zA-Z_:][a-zA-Z0-9_:]*)(?:\{([^}]*)\})?", line) + if not m: + continue + name = m.group(1) + raw = m.group(2) + item = meta.setdefault(name, {}) + item.setdefault("help", "") + item.setdefault("type", "gauge") + labels = item.setdefault("labels", set()) + if raw: + for part in raw.split(","): + if "=" in part: + labels.add(part.split("=", 1)[0].strip()) + for item in meta.values(): + item["labels"] = sorted(item.get("labels", set())) + return meta + + +class DashboardBuilder: + def __init__(self, meta): + self.meta = meta + self.panels = [] + self.covered = set() + self.next_id = 1 + self.y = 0 + + def _id(self): + out = self.next_id + self.next_id += 1 + return out + + def _unit(self, metrics, unit): + if unit != "auto": + return unit + names = " ".join(metrics) + if "bytes" in names: + return "bytes" + if "percent" in names or "percentage" in names: + return "percent" + if "_ms" in names or "wait_time_ms" in names: + return "ms" + if "seconds" in names or names.endswith("_sec"): + return "s" + return "short" + + def _desc(self, metrics): + return "\n".join(f"- {m}: {self.meta[m]['help']}" for m in metrics) + + def row(self, title): + self.panels.append({ + "collapsed": False, + "gridPos": {"h": 1, "w": 24, "x": 0, "y": self.y}, + "id": self._id(), + "panels": [], + "title": title, + "type": "row", + }) + self.y += 1 + + def stat(self, title, metric, expr, x, w=3, h=4, unit="auto", thresholds=None, mappings=None): + self.covered.add(metric) + self.panels.append({ + "datasource": PROM, + "description": self.meta[metric]["help"], + "fieldConfig": { + "defaults": { + "unit": self._unit([metric], unit), + "color": {"mode": "thresholds"}, + "thresholds": thresholds or {"mode": "absolute", "steps": [{"color": "green", "value": None}]}, + "mappings": mappings or [], + }, + "overrides": [], + }, + "gridPos": {"h": h, "w": w, "x": x, "y": self.y}, + "id": self._id(), + "options": { + "colorMode": "value", + "graphMode": "area", + "justifyMode": "center", + "orientation": "auto", + "reduceOptions": {"calcs": ["lastNotNull"], "fields": "", "values": False}, + "textMode": "auto", + }, + "targets": [{"refId": "A", "expr": expr, "datasource": PROM}], + "title": title, + "type": "stat", + }) + + def timeseries(self, title, metrics, targets, x, w=8, h=8, unit="auto"): + self.covered.update(metrics) + self.panels.append({ + "datasource": PROM, + "description": self._desc(metrics), + "fieldConfig": { + "defaults": { + "unit": self._unit(metrics, unit), + "color": {"mode": "palette-classic"}, + "custom": { + "axisCenteredZero": False, + "axisColorMode": "text", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 8, + "gradientMode": "none", + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 3, + "scaleDistribution": {"type": "linear"}, + "showPoints": "auto", + "stacking": {"group": "A", "mode": "none"}, + "thresholdsStyle": {"mode": "off"}, + }, + }, + "overrides": [], + }, + "gridPos": {"h": h, "w": w, "x": x, "y": self.y}, + "id": self._id(), + "options": { + "legend": {"calcs": [], "displayMode": "table", "placement": "bottom", "showLegend": True}, + "tooltip": {"mode": "multi", "sort": "desc"}, + }, + "targets": targets, + "title": title, + "type": "timeseries", + }) + + def table(self, title, metrics, expr, x=0, w=24, h=8): + self.covered.update(metrics) + self.panels.append({ + "datasource": PROM, + "description": self._desc(metrics), + "fieldConfig": {"defaults": {}, "overrides": []}, + "gridPos": {"h": h, "w": w, "x": x, "y": self.y}, + "id": self._id(), + "options": {"cellHeight": "sm", "footer": {"countRows": False, "fields": "", "reducer": ["sum"], "show": False}, "showHeader": True}, + "targets": [{"refId": "A", "expr": expr, "format": "table", "instant": True, "datasource": PROM}], + "title": title, + "transformations": [], + "type": "table", + }) + + def gap(self, h): + self.y += h + + +def q(ref_id, expr, legend=None): + out = {"refId": ref_id, "expr": expr, "datasource": PROM} + if legend: + out["legendFormat"] = legend + return out + + +def build_dashboard(meta): + b = DashboardBuilder(meta) + b.row("Overview") + b.stat("SQL Server Up", "mssql_service_info", 'max(mssql_service_info{instance="$Server",service_name="MSSQLSERVER"})', 0, thresholds={"mode": "absolute", "steps": [{"color": "red", "value": None}, {"color": "green", "value": 1}]}, mappings=[{"type": "value", "options": {"0": {"text": "Down", "color": "red"}, "1": {"text": "Up", "color": "green"}}}]) + b.stat("User Connections", "mssql_user_connections", 'mssql_user_connections{instance="$Server"}', 3) + b.stat("Batch Req/sec", "mssql_batch_requests", 'sum(rate(mssql_batch_requests{instance="$Server"}[$__rate_interval]))', 6) + b.stat("SQL CPU %", "mssql_cpu_utilization_percentage", 'mssql_cpu_utilization_percentage{instance="$Server",scope="sqlserver"}', 9, unit="percent", thresholds={"mode": "absolute", "steps": [{"color": "green", "value": None}, {"color": "yellow", "value": 70}, {"color": "red", "value": 90}]}) + b.stat("Memory Util %", "mssql_memory_utilization_percentage", 'mssql_memory_utilization_percentage{instance="$Server"}', 12, unit="percent", thresholds={"mode": "absolute", "steps": [{"color": "green", "value": None}, {"color": "yellow", "value": 75}, {"color": "red", "value": 90}]}) + b.stat("Blocked Processes", "mssql_processes_blocked", 'mssql_processes_blocked{instance="$Server"}', 15) + b.stat("Deadlocks/sec", "mssql_deadlocks", 'sum(rate(mssql_deadlocks{instance="$Server"}[$__rate_interval]))', 18) + b.stat("Max Log Used %", "mssql_database_percent_log_used", 'max(mssql_database_percent_log_used{instance="$Server"})', 21, unit="percent", thresholds={"mode": "absolute", "steps": [{"color": "green", "value": None}, {"color": "yellow", "value": 70}, {"color": "red", "value": 90}]}) + b.gap(4) + + b.row("Availability & Instance") + b.timeseries("Service Availability Trend", ["mssql_service_info"], [q("A", 'mssql_service_info{instance="$Server"}', "{{service_name_str}}")], 0) + b.stat("Exporter Local Time", "mssql_local_time_seconds", 'mssql_local_time_seconds{instance="$Server"}', 8, w=4, h=8, unit="s") + b.timeseries("Replication / AG Queue Gauges", ["mssql_log_apply_pending_queue", "mssql_log_remaining_for_undo", "mssql_log_send_queue", "mssql_transaction_delay"], [q("A", 'mssql_log_apply_pending_queue{instance="$Server"}', "Log apply pending"), q("B", 'mssql_log_remaining_for_undo{instance="$Server"}', "Remaining for undo"), q("C", 'mssql_log_send_queue{instance="$Server"}', "Log send queue"), q("D", 'mssql_transaction_delay{instance="$Server"}', "Transaction delay")], 12) + b.gap(8) + + b.row("Workload, Connections & Sessions") + b.timeseries("Connections by Database", ["mssql_connections"], [q("A", 'mssql_connections{instance="$Server"}', "{{db}}")], 0) + b.timeseries("Session Activity Rates", ["mssql_logins", "mssql_logouts", "mssql_connection_reset"], [q("A", 'rate(mssql_logins{instance="$Server"}[$__rate_interval])', "Logins/sec"), q("B", 'rate(mssql_logouts{instance="$Server"}[$__rate_interval])', "Logouts/sec"), q("C", 'rate(mssql_connection_reset{instance="$Server"}[$__rate_interval])', "Resets/sec")], 8) + b.timeseries("User Connections & Active Cursors", ["mssql_user_connections", "mssql_active_cursors"], [q("A", 'mssql_user_connections{instance="$Server"}', "User connections"), q("B", 'mssql_active_cursors{instance="$Server"}', "{{cursor_type}}")], 16) + b.gap(8) + + b.row("CPU, Scheduling & Wait Pressure") + b.timeseries("CPU Utilization", ["mssql_cpu_utilization_percentage"], [q("A", 'mssql_cpu_utilization_percentage{instance="$Server"}', "{{scope}}")], 0, unit="percent") + b.timeseries("Resource Pool / Workload Group CPU", ["mssql_resource_pool_cpu_usage_percentage", "mssql_workload_group_cpu_usage_percentage"], [q("A", 'mssql_resource_pool_cpu_usage_percentage{instance="$Server"}', "Pool {{resource_pool}}"), q("B", 'mssql_workload_group_cpu_usage_percentage{instance="$Server"}', "Group {{workload_group}}")], 8, unit="percent") + b.timeseries("Current Waits In Progress", ["mssql_waits_in_progress"], [q("A", 'mssql_waits_in_progress{instance="$Server"}', "{{wait_type}}")], 16) + b.gap(8) + + b.row("Memory & Buffer Pool") + b.timeseries("Host / OS Memory", ["mssql_host_physical_memory_bytes", "mssql_os_memory", "mssql_os_page_file"], [q("A", 'mssql_host_physical_memory_bytes{instance="$Server"}', "Host {{state}}"), q("B", 'mssql_os_memory{instance="$Server"}', "OS memory {{state}}"), q("C", 'mssql_os_page_file{instance="$Server"}', "OS page file {{state}}")], 0, unit="bytes") + b.timeseries("SQL Process Memory", ["mssql_resident_memory_bytes", "mssql_virtual_memory_bytes", "mssql_sql_process_memory_bytes"], [q("A", 'mssql_resident_memory_bytes{instance="$Server"}', "Resident"), q("B", 'mssql_virtual_memory_bytes{instance="$Server"}', "Virtual"), q("C", 'mssql_sql_process_memory_bytes{instance="$Server"}', "{{state}}")], 8, unit="bytes") + b.timeseries("Memory Manager Breakdown", ["mssql_memory_manager_bytes", "mssql_memory_grants_outstanding", "mssql_memory_grants_pending", "mssql_memory_utilization_percentage"], [q("A", 'mssql_memory_manager_bytes{instance="$Server"}', "Manager {{state}}"), q("B", 'mssql_memory_grants_outstanding{instance="$Server"}', "Outstanding grants"), q("C", 'mssql_memory_grants_pending{instance="$Server"}', "Pending grants"), q("D", 'mssql_memory_utilization_percentage{instance="$Server"}', "Memory util %")], 16) + b.gap(8) + b.timeseries("Buffer Pool Size & Commit Targets", ["mssql_buffer_database_pages", "mssql_buffer_target_pages", "mssqlbuffer_pool_committed", "mssqlbuffer_pool_committed_target"], [q("A", 'mssql_buffer_database_pages{instance="$Server"}', "Database pages"), q("B", 'mssql_buffer_target_pages{instance="$Server"}', "Target pages"), q("C", 'mssqlbuffer_pool_committed{instance="$Server"}', "Committed bytes"), q("D", 'mssqlbuffer_pool_committed_target{instance="$Server"}', "Committed target bytes")], 0) + b.timeseries("Buffer Cache Health", ["mssql_buffer_cache_hit_ratio", "mssql_page_life_expectancy_seconds", "mssql_page_fault_count"], [q("A", 'mssql_buffer_cache_hit_ratio{instance="$Server"}', "Buffer cache hit ratio"), q("B", 'mssql_page_life_expectancy_seconds{instance="$Server"}', "Page life expectancy"), q("C", 'rate(mssql_page_fault_count{instance="$Server"}[$__rate_interval])', "Page faults/sec")], 8) + b.timeseries("Buffer Manager Ops & Checkpoints", ["mssql_buffer_manager_operations_total", "mssql_checkpoint_pages_sec"], [q("A", 'rate(mssql_buffer_manager_operations_total{instance="$Server"}[$__rate_interval])', "{{operation}}"), q("B", 'mssql_checkpoint_pages_sec{instance="$Server"}', "Checkpoint pages/sec")], 16) + b.gap(8) + + b.row("Access Methods, I/O & Latches") + b.timeseries("Access Methods Activity", ["mssql_access_methods_total"], [q("A", 'rate(mssql_access_methods_total{instance="$Server"}[$__rate_interval])', "{{operation}}")], 0) + b.timeseries("Page Lookups / Reads / Writes", ["mssql_page_lookups", "mssql_page_reads", "mssql_page_writes"], [q("A", 'rate(mssql_page_lookups{instance="$Server"}[$__rate_interval])', "Page lookups/sec"), q("B", 'rate(mssql_page_reads{instance="$Server"}[$__rate_interval])', "Page reads/sec"), q("C", 'rate(mssql_page_writes{instance="$Server"}[$__rate_interval])', "Page writes/sec")], 8) + b.timeseries("I/O Stall by DB and Operation", ["mssql_io_stall_seconds", "mssql_io_stall_total_seconds"], [q("A", 'rate(mssql_io_stall_seconds{instance="$Server"}[$__rate_interval])', "{{db}} {{operation}}"), q("B", 'rate(mssql_io_stall_total_seconds{instance="$Server"}[$__rate_interval])', "{{db}} total")], 16) + b.gap(8) + b.timeseries("Latch / Network Wait Gauges", ["mssql_average_latch_wait_time_ms", "mssql_network_io_waits_ms", "mssql_page_io_latch_waits_ms"], [q("A", 'mssql_average_latch_wait_time_ms{instance="$Server"}', "Average latch wait ms"), q("B", 'mssql_network_io_waits_ms{instance="$Server"}', "Network IO waits ms"), q("C", 'mssql_page_io_latch_waits_ms{instance="$Server"}', "Page IO latch waits ms")], 0, unit="ms") + b.gap(8) + + b.row("Transactions, Locks & Concurrency") + b.timeseries("Transaction Gauges", ["mssql_active_transactions_count", "mssql_longest_transaction_running_time_seconds", "mssql_processes_blocked"], [q("A", 'mssql_active_transactions_count{instance="$Server"}', "Active transactions"), q("B", 'mssql_longest_transaction_running_time_seconds{instance="$Server"}', "Longest running transaction sec"), q("C", 'mssql_processes_blocked{instance="$Server"}', "Blocked processes")], 0) + b.timeseries("Lock Waits / Wait Time / Deadlocks", ["mssql_lock_waits_total", "mssql_lock_wait_time_ms_total", "mssql_deadlocks"], [q("A", 'rate(mssql_lock_waits_total{instance="$Server"}[$__rate_interval])', "Lock waits/sec"), q("B", 'rate(mssql_lock_wait_time_ms_total{instance="$Server"}[$__rate_interval])', "Lock wait ms/sec"), q("C", 'rate(mssql_deadlocks{instance="$Server"}[$__rate_interval])', "Deadlocks/sec")], 8) + b.timeseries("Database Active Transactions", ["mssql_database_active_transactions"], [q("A", 'mssql_database_active_transactions{instance="$Server"}', "{{db}}")], 16) + b.gap(8) + + b.row("Databases, Files & Inventory") + b.timeseries("Database File Sizes", ["mssql_database_file_size_bytes"], [q("A", 'mssql_database_file_size_bytes{instance="$Server"}', "{{db}} {{state}}")], 0, unit="bytes") + b.timeseries("Database Log Used %", ["mssql_database_percent_log_used"], [q("A", 'mssql_database_percent_log_used{instance="$Server"}', "{{db}}")], 8, unit="percent") + b.timeseries("Database XTP Memory", ["mssql_database_xtp_memory_used_bytes"], [q("A", 'mssql_database_xtp_memory_used_bytes{instance="$Server"}', "{{db}}")], 16, unit="bytes") + b.gap(8) + b.table("Database Inventory Snapshot", ["mssql_database_info"], 'mssql_database_info{instance="$Server"}') + b.gap(8) + + b.row("Transaction Log, Redo & Data Movement") + b.timeseries("Log Flushes / Bytes / Waits", ["mssql_database_log_flushes_total", "mssql_database_log_bytes_flushed_total", "mssql_database_log_waits_total"], [q("A", 'rate(mssql_database_log_flushes_total{instance="$Server"}[$__rate_interval])', "{{db}} flushes/sec"), q("B", 'rate(mssql_database_log_bytes_flushed_total{instance="$Server"}[$__rate_interval])', "{{db}} bytes/sec"), q("C", 'rate(mssql_database_log_waits_total{instance="$Server"}[$__rate_interval])', "{{db}} waits/sec")], 0) + b.timeseries("Log Flush Wait Time", ["mssql_database_log_flush_wait_time_ms_total"], [q("A", 'rate(mssql_database_log_flush_wait_time_ms_total{instance="$Server"}[$__rate_interval])', "{{db}} ms/sec")], 8, unit="ms") + b.timeseries("Log Events & Growths", ["mssql_database_log_events_total", "mssql_log_growths"], [q("A", 'rate(mssql_database_log_events_total{instance="$Server"}[$__rate_interval])', "{{db}} {{event}}"), q("B", 'rate(mssql_log_growths{instance="$Server"}[$__rate_interval])', "{{db}} growths")], 16) + b.gap(8) + b.timeseries("Redo / Mirroring Movement", ["mssql_mirrored_write_transactions", "mssql_redo_blocked"], [q("A", 'rate(mssql_mirrored_write_transactions{instance="$Server"}[$__rate_interval])', "Mirrored write tx/sec"), q("B", 'rate(mssql_redo_blocked{instance="$Server"}[$__rate_interval])', "Redo blocked/sec")], 0) + b.gap(8) + + b.row("SQL Compilation, Errors & Tempdb") + b.timeseries("Compilations / Recompilations / Auto Params", ["mssql_sql_compilations", "mssql_sql_recompilations", "mssql_sql_auto_params_total"], [q("A", 'rate(mssql_sql_compilations{instance="$Server"}[$__rate_interval])', "Compilations/sec"), q("B", 'rate(mssql_sql_recompilations{instance="$Server"}[$__rate_interval])', "Recompilations/sec"), q("C", 'rate(mssql_sql_auto_params_total{instance="$Server"}[$__rate_interval])', "{{mode}}/sec")], 0) + b.timeseries("SQL Errors & Attentions", ["mssql_sql_errors_total", "mssql_user_errors", "mssql_kill_connection_errors", "mssql_sql_attentions_total"], [q("A", 'rate(mssql_sql_errors_total{instance="$Server"}[$__rate_interval])', "{{error_type}}/sec"), q("B", 'rate(mssql_user_errors{instance="$Server"}[$__rate_interval])', "User errors/sec"), q("C", 'rate(mssql_kill_connection_errors{instance="$Server"}[$__rate_interval])', "Kill connection errors/sec"), q("D", 'rate(mssql_sql_attentions_total{instance="$Server"}[$__rate_interval])', "SQL attentions/sec")], 8) + b.timeseries("Tempdb & Temp Objects", ["mssql_tempdb_active_temp_tables", "mssql_tempdb_space_bytes", "mssql_temp_tables_for_destruction"], [q("A", 'mssql_tempdb_active_temp_tables{instance="$Server"}', "Active temp tables"), q("B", 'mssql_tempdb_space_bytes{instance="$Server"}', "tempdb {{state}}"), q("C", 'mssql_temp_tables_for_destruction{instance="$Server"}', "Awaiting destruction")], 16, unit="bytes") + b.gap(8) + + uncovered = sorted(set(meta) - b.covered) + if uncovered: + raise RuntimeError(f"Uncovered metrics: {uncovered}") + return { + "__inputs": [{"name": "DS_PROMETHEUS", "label": "Prometheus", "description": "Prometheus data source for SQL Exporter metrics", "type": "datasource", "pluginId": "prometheus", "pluginName": "Prometheus"}], + "__requires": [{"type": "grafana", "id": "grafana", "name": "Grafana", "version": "11.3.1"}, {"type": "datasource", "id": "prometheus", "name": "Prometheus", "version": "1.0.0"}, {"type": "panel", "id": "timeseries", "name": "Time series", "version": ""}, {"type": "panel", "id": "stat", "name": "Stat", "version": ""}, {"type": "panel", "id": "table", "name": "Table", "version": ""}], + "annotations": {"list": [{"builtIn": 1, "datasource": {"type": "grafana", "uid": "-- Grafana --"}, "enable": True, "hide": True, "iconColor": "rgba(0, 211, 255, 1)", "name": "Annotations & Alerts", "type": "dashboard"}]}, + "editable": True, + "fiscalYearStartMonth": 0, + "graphTooltip": 1, + "id": None, + "links": [], + "panels": b.panels, + "refresh": "30s", + "schemaVersion": 40, + "tags": ["SQL Server", "sql_exporter", "all-metrics", "prometheus"], + "templating": {"list": [{"current": {"text": "Prometheus", "value": "Prometheus"}, "description": "Prometheus data source for SQL Exporter metrics", "hide": 2, "includeAll": False, "label": "Data Source", "multi": False, "name": "prometheus_datasource", "options": [], "query": "prometheus", "refresh": 1, "regex": "", "skipUrlSync": False, "sort": 0, "tagValuesQuery": "", "tagsQuery": "", "type": "datasource", "useTags": False}, {"current": {}, "datasource": {"type": "prometheus", "uid": "${DS_PROMETHEUS}"}, "definition": "label_values(mssql_up, instance)", "description": "Select one Prometheus target/server at a time. Multi-select is intentionally disabled.", "hide": 0, "includeAll": False, "label": "Server", "multi": False, "name": "Server", "options": [], "query": "label_values(mssql_up, instance)", "refresh": 1, "regex": "", "skipUrlSync": False, "sort": 1, "tagValuesQuery": "", "tagsQuery": "", "type": "query", "useTags": False}]}, + "time": {"from": "now-6h", "to": "now"}, + "timepicker": {}, + "timezone": "browser", + "title": "SQL Exporter Metrics - All Metrics", + "uid": "sql-exporter-metrics-all", + "version": 1, + "weekStart": "", + } + + +if __name__ == "__main__": + dashboard = build_dashboard(parse_metric_meta(METRICS_FILE)) + OUTPUT_FILE.write_text(json.dumps(dashboard, indent=2)) + print(f"Wrote {OUTPUT_FILE}") \ No newline at end of file diff --git a/sql_exporter/logsaver.xml b/sql_exporter/logsaver.xml new file mode 100644 index 0000000..38c6d5e --- /dev/null +++ b/sql_exporter/logsaver.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/sql_exporter/mssql_backup_history.collector.yml b/sql_exporter/mssql_backup_history.collector.yml new file mode 100644 index 0000000..5046c42 --- /dev/null +++ b/sql_exporter/mssql_backup_history.collector.yml @@ -0,0 +1,118 @@ +# Backup history metrics for SQLMonitor. +# +# Required permissions (on msdb): +# +# GRANT SELECT ON OBJECT::msdb.dbo.backupset TO ; +# GRANT SELECT ON OBJECT::msdb.dbo.backupmediafamily TO ; +# +# One sample per (database_name, backup_type) so cardinality is bounded: +# SQL Server itself exposes at most ~8 backup_type values per database. +# +collector_name: mssql_backup_history + +min_interval: 5m + +metrics: + - metric_name: mssql_backup__last_time_utc + type: gauge + help: 'Unix epoch seconds when the last backup of the given type finished. 0 if no history.' + key_labels: [database_name, backup_type, backup_type_desc, recovery_model] + values: [last_backup_time_utc] + query_ref: mssql_backup_history_latest + + - metric_name: mssql_backup__last_duration_seconds + type: gauge + help: 'Duration (seconds) of the last backup of the given type.' + key_labels: [database_name, backup_type] + values: [last_backup_duration_seconds] + query_ref: mssql_backup_history_latest + + - metric_name: mssql_backup__last_size_bytes + type: gauge + help: 'Size (bytes) of the last backup of the given type.' + key_labels: [database_name, backup_type] + values: [last_backup_size_bytes] + query_ref: mssql_backup_history_latest + + - metric_name: mssql_backup__last_compressed_size_bytes + type: gauge + help: 'Compressed size (bytes) of the last backup of the given type.' + key_labels: [database_name, backup_type] + values: [last_backup_compressed_size_bytes] + query_ref: mssql_backup_history_latest + + - metric_name: mssql_backup__age_seconds + type: gauge + help: 'Age (seconds) of the last backup of the given type relative to server time.' + key_labels: [database_name, backup_type] + values: [backup_age_seconds] + query_ref: mssql_backup_history_latest + + - metric_name: mssql_backup__count_last_24h + type: gauge + help: 'Count of successful backups of the given type that finished within the last 24 hours.' + key_labels: [database_name, backup_type] + values: [count_last_24h] + query_ref: mssql_backup_history_latest + +queries: + - query_name: mssql_backup_history_latest + query: | + SET NOCOUNT ON; + + IF OBJECT_ID('tempdb..#latest') IS NOT NULL DROP TABLE #latest; + IF OBJECT_ID('tempdb..#count_24h') IS NOT NULL DROP TABLE #count_24h; + + -- Latest row per (database_name, type) from backupset. + WITH b AS ( + SELECT database_name, type, + backup_finish_date, backup_start_date, + backup_size, compressed_backup_size, recovery_model, + rn = ROW_NUMBER() OVER ( + PARTITION BY database_name, type + ORDER BY backup_finish_date DESC, backup_set_id DESC) + FROM msdb.dbo.backupset + WHERE backup_finish_date IS NOT NULL + ) + SELECT database_name, type, backup_finish_date, backup_start_date, + backup_size, compressed_backup_size, recovery_model + INTO #latest + FROM b WHERE rn = 1; + + -- 24-hour count per (database_name, type). + SELECT database_name, type, c = COUNT(*) + INTO #count_24h + FROM msdb.dbo.backupset + WHERE backup_finish_date IS NOT NULL + AND backup_finish_date >= DATEADD(hour, -24, GETDATE()) + GROUP BY database_name, type; + + SELECT database_name = CAST(COALESCE(l.database_name, N'') AS nvarchar(256)), + backup_type = CAST(l.type AS nvarchar(4)), + backup_type_desc = CASE l.type + WHEN 'D' THEN N'Database (Full)' + WHEN 'I' THEN N'Differential' + WHEN 'L' THEN N'Log' + WHEN 'F' THEN N'File or Filegroup' + WHEN 'G' THEN N'File Differential' + WHEN 'P' THEN N'Partial' + WHEN 'Q' THEN N'Partial Differential' + ELSE N'Unknown' END, + recovery_model = CAST(COALESCE(l.recovery_model, N'') AS nvarchar(64)), + last_backup_time_utc = CAST( + DATEDIFF_BIG(second, + CONVERT(datetime2, '19700101'), + DATEADD(mi, DATEDIFF(mi, GETDATE(), GETUTCDATE()), + l.backup_finish_date)) AS bigint), + last_backup_duration_seconds = CAST( + DATEDIFF(second, l.backup_start_date, l.backup_finish_date) + AS bigint), + last_backup_size_bytes = CAST(COALESCE(l.backup_size, 0) AS bigint), + last_backup_compressed_size_bytes = CAST( + COALESCE(l.compressed_backup_size, l.backup_size, 0) AS bigint), + backup_age_seconds = CAST( + DATEDIFF_BIG(second, l.backup_finish_date, GETDATE()) AS bigint), + count_last_24h = CAST(COALESCE(c24.c, 0) AS bigint) + FROM #latest l + LEFT JOIN #count_24h c24 + ON c24.database_name = l.database_name AND c24.type = l.type; diff --git a/sql_exporter/mssql_dba_aghealth.collector.yml b/sql_exporter/mssql_dba_aghealth.collector.yml new file mode 100644 index 0000000..0fb866a --- /dev/null +++ b/sql_exporter/mssql_dba_aghealth.collector.yml @@ -0,0 +1,200 @@ +# Similar to global.min_interval, but applies to the queries defined by this collector only. +# min_interval: 2m + +collector_name: mssql_dba_aghealth + +metrics: + - metric_name: mssql_aghealth__collection_time_utc + type: gauge + help: 'UTC collection time for the AG health snapshot as Unix epoch seconds.' + key_labels: [unique_key] + values: [collection_time_utc] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__is_primary_replica + type: gauge + help: 'Whether the replica is primary for the AG database.' + key_labels: [unique_key] + values: [is_primary_replica] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__is_local + type: gauge + help: 'Whether the AG replica row is local to the monitored SQL instance.' + key_labels: [unique_key] + values: [is_local] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__is_distributed + type: gauge + help: 'Whether the availability group is distributed.' + key_labels: [unique_key] + values: [is_distributed] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__synchronization_state + type: gauge + help: 'Synchronization state code for the AG database replica.' + key_labels: [unique_key, synchronization_state_desc] + values: [synchronization_state] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__synchronization_health + type: gauge + help: 'Synchronization health code for the AG database replica with AG metadata labels.' + key_labels: [unique_key, replica_server_name, database_name, ag_name, ag_listener, synchronization_state_desc, synchronization_health_desc, suspend_reason_desc] + values: [synchronization_health] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__latency_seconds + type: gauge + help: 'Commit latency in seconds versus the primary replica for the AG database.' + key_labels: [unique_key] + values: [latency_seconds] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__redo_queue_size + type: gauge + help: 'Redo queue size for the AG database replica.' + key_labels: [unique_key] + values: [redo_queue_size] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__log_send_queue_size + type: gauge + help: 'Log send queue size for the AG database replica.' + key_labels: [unique_key] + values: [log_send_queue_size] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__last_redone_time + type: gauge + help: 'Last redone time for the AG database replica as Unix epoch seconds.' + key_labels: [unique_key] + values: [last_redone_time] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__log_send_rate + type: gauge + help: 'Log send rate for the AG database replica.' + key_labels: [unique_key] + values: [log_send_rate] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__redo_rate + type: gauge + help: 'Redo rate for the AG database replica.' + key_labels: [unique_key] + values: [redo_rate] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__estimated_redo_completion_time_min + type: gauge + help: 'Estimated redo completion time in minutes for the AG database replica.' + key_labels: [unique_key] + values: [estimated_redo_completion_time_min] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__last_commit_time + type: gauge + help: 'Last commit time for the AG database replica as Unix epoch seconds.' + key_labels: [unique_key] + values: [last_commit_time] + query_ref: mssql_aghealth + + - metric_name: mssql_aghealth__is_suspended + type: gauge + help: 'Whether data movement is suspended for the AG database replica.' + key_labels: [unique_key, suspend_reason_desc] + values: [is_suspended] + query_ref: mssql_aghealth + +queries: + - query_name: mssql_aghealth + query: | + SET NOCOUNT ON; + + IF OBJECT_ID('tempdb..#availability_databases') IS NOT NULL + DROP TABLE #availability_databases; + + SELECT + ar.replica_server_name, + drs.is_primary_replica, + adc.database_name, + ag.name AS ag_name, + drs.is_local, + ag.is_distributed, + drs.synchronization_state, + drs.synchronization_state_desc, + drs.synchronization_health, + drs.synchronization_health_desc, + drs.last_redone_time, + drs.log_send_queue_size, + drs.log_send_rate, + drs.redo_queue_size, + drs.redo_rate, + estimated_redo_completion_time_min = CASE + WHEN drs.redo_rate <> 0 THEN (drs.redo_queue_size / drs.redo_rate) / 60.0 + ELSE (drs.redo_queue_size / 1.0) / 60.0 + END, + drs.last_commit_time, + drs.is_suspended, + drs.suspend_reason_desc, + ag.group_id + INTO #availability_databases + FROM sys.dm_hadr_database_replica_states AS drs + INNER JOIN sys.availability_databases_cluster AS adc + ON drs.group_id = adc.group_id + AND drs.group_database_id = adc.group_database_id + INNER JOIN sys.availability_groups AS ag + ON ag.group_id = drs.group_id + INNER JOIN sys.availability_replicas AS ar + ON drs.group_id = ar.group_id + AND drs.replica_id = ar.replica_id; + + SELECT + collection_time_utc = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), SYSUTCDATETIME()) AS bigint), + unique_key = CONCAT(ag.replica_server_name, N' | ', ag.ag_name, N' | ', ag.database_name), + replica_server_name = COALESCE(ag.replica_server_name, N''), + is_primary_replica = CAST(COALESCE(ag.is_primary_replica, 0) AS tinyint), + database_name = COALESCE(ag.database_name, N''), + ag_name = COALESCE(ag.ag_name, N''), + ag_listener = COALESCE(agl.dns_name + N' (' + ia.ip_address + N')', agl.dns_name, ia.ip_address, N''), + is_local = CAST(COALESCE(ag.is_local, 0) AS tinyint), + is_distributed = CAST(COALESCE(ag.is_distributed, 0) AS tinyint), + synchronization_state = CAST(COALESCE(ag.synchronization_state, 0) AS bigint), + synchronization_state_desc = COALESCE(ag.synchronization_state_desc, N''), + synchronization_health = CAST(COALESCE(ag.synchronization_health, 0) AS bigint), + synchronization_health_desc = COALESCE(ag.synchronization_health_desc, N''), + latency_seconds = CAST(COALESCE(CASE + WHEN ag.is_primary_replica = 1 THEN 0 + ELSE ( + SELECT DATEDIFF(second, ag.last_commit_time, p.last_commit_time) + FROM #availability_databases AS p + WHERE p.is_primary_replica = 1 + AND p.ag_name = ag.ag_name + AND p.database_name = ag.database_name + ) + END, 0) AS bigint), + redo_queue_size = CAST(COALESCE(ag.redo_queue_size, 0) AS bigint), + log_send_queue_size = CAST(COALESCE(ag.log_send_queue_size, 0) AS bigint), + last_redone_time = CAST(CASE + WHEN ag.last_redone_time IS NULL THEN 0 + ELSE DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), ag.last_redone_time) + END AS bigint), + log_send_rate = CAST(COALESCE(ag.log_send_rate, 0) AS bigint), + redo_rate = CAST(COALESCE(ag.redo_rate, 0) AS bigint), + estimated_redo_completion_time_min = CAST(COALESCE(ag.estimated_redo_completion_time_min, 0) AS float), + last_commit_time = CAST(CASE + WHEN ag.last_commit_time IS NULL THEN 0 + ELSE DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), ag.last_commit_time) + END AS bigint), + is_suspended = CAST(COALESCE(ag.is_suspended, 0) AS tinyint), + suspend_reason_desc = COALESCE(ag.suspend_reason_desc, N'') + FROM #availability_databases AS ag + LEFT JOIN sys.availability_group_listeners AS agl + ON agl.group_id = ag.group_id + LEFT JOIN sys.availability_group_listener_ip_addresses AS ia + ON ia.listener_id = agl.listener_id + AND ia.state_desc = 'ONLINE' + ORDER BY ag.ag_name, ag.replica_server_name, ag.database_name; diff --git a/sql_exporter/mssql_dba_cached.collector.yml b/sql_exporter/mssql_dba_cached.collector.yml new file mode 100644 index 0000000..49299fd --- /dev/null +++ b/sql_exporter/mssql_dba_cached.collector.yml @@ -0,0 +1,900 @@ +# A collector defining standard metrics for Microsoft SQL Server. +# +# It is required that the SQL Server user has the following permissions: +# +# GRANT VIEW ANY DEFINITION TO +# GRANT VIEW SERVER STATE TO +# +collector_name: mssql_dba_cached + +# Similar to global.min_interval, but applies to the queries defined by this collector only. +min_interval: 2m + +metrics: + + # + # Collected from sys.dm_os_wait_stats + # + - metric_name: mssql_waits__collection_time_utc + type: gauge + help: 'Collection time as Unix epoch seconds for sys.dm_os_wait_stats rows.' + key_labels: + - wait_type + values: [collection_time_utc] + query_ref: mssql_os_wait_stats + + - metric_name: mssql_waits__wait_time_seconds + type: counter + help: 'Total wait time in seconds by wait type since SQL Server startup.' + key_labels: + - wait_type + # - wait_percentage + # - wait_rank_no + values: [wait_time_seconds] + query_ref: mssql_os_wait_stats + + - metric_name: mssql_waits__resource_time_seconds + type: counter + help: 'Total resource wait time in seconds by wait type since SQL Server startup.' + key_labels: + - wait_type + # - wait_percentage + # - wait_rank_no + values: [resource_time_seconds] + query_ref: mssql_os_wait_stats + + - metric_name: mssql_waits__signal_time_seconds + type: counter + help: 'Total signal wait time in seconds by wait type since SQL Server startup.' + key_labels: + - wait_type + # - wait_percentage + # - wait_rank_no + values: [signal_time_seconds] + query_ref: mssql_os_wait_stats + + - metric_name: mssql_waits__waiting_tasks_count + type: counter + help: 'Total waiting tasks by wait type since SQL Server startup.' + key_labels: + - wait_type + # - wait_percentage + # - wait_rank_no + values: [waiting_tasks_count] + query_ref: mssql_os_wait_stats + + - metric_name: mssql_waits__wait_percentage + type: counter + help: 'Percentage of wait time by wait type since SQL Server startup.' + key_labels: + - wait_type + # - wait_percentage + # - wait_rank_no + values: [wait_percentage] + query_ref: mssql_os_wait_stats + + - metric_name: mssql_waits__wait_rank_no + type: counter + help: 'Rank of wait time by wait type since SQL Server startup.' + key_labels: + - wait_type + # - wait_percentage + # - wait_rank_no + values: [wait_rank_no] + query_ref: mssql_os_wait_stats + + # - metric_name: mssql_diskspace__capacity_mb + # type: gauge + # help: 'Disk capacity in megabytes.' + # key_labels: + # - host_name + # - disk_volume + # # - label + # - filesystem + # values: [capacity_mb] + # query_ref: mssql_diskspace + + # - metric_name: mssql_diskspace__free_mb + # type: gauge + # help: 'Disk free space in megabytes.' + # key_labels: + # - host_name + # - disk_volume + # # - label + # - filesystem + # values: [free_mb] + # query_ref: mssql_diskspace + + # - metric_name: mssql_diskspace__used_mb + # type: gauge + # help: 'Disk used space in megabytes.' + # key_labels: + # - host_name + # - disk_volume + # # - label + # - filesystem + # values: [used_mb] + # query_ref: mssql_diskspace + + - metric_name: mssql_virtualfilestats__collection_time_utc + type: gauge + help: 'Collection time as Unix epoch seconds for the SQL Server file virtual stats row.' + key_labels: [file_location, database_name, file_logical_name, disk_volume, disk_filesystem] + values: [collection_time_utc] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__sample_ms + type: counter + help: 'Virtual file stats sample age in milliseconds for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [sample_ms] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__num_of_reads + type: counter + help: 'Total reads for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [num_of_reads] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__num_of_bytes_read + type: counter + help: 'Total bytes read for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [num_of_bytes_read] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__io_stall_read_ms + type: counter + help: 'Total read stall time in milliseconds for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [io_stall_read_ms] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__io_stall_queued_read_ms + type: counter + help: 'Total queued read stall time in milliseconds for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [io_stall_queued_read_ms] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__num_of_writes + type: counter + help: 'Total writes for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [num_of_writes] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__num_of_bytes_written + type: counter + help: 'Total bytes written for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [num_of_bytes_written] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__io_stall_write_ms + type: counter + help: 'Total write stall time in milliseconds for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [io_stall_write_ms] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__io_stall_queued_write_ms + type: counter + help: 'Total queued write stall time in milliseconds for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [io_stall_queued_write_ms] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__io_stall + type: counter + help: 'Total I/O stall time in milliseconds for the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [io_stall] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__size_on_disk_bytes + type: gauge + help: 'Current on-disk size in bytes for the SQL Server file.' + key_labels: [file_location, database_name, file_logical_name, disk_volume, disk_filesystem] + values: [size_on_disk_bytes] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__disk_capacity_mb + type: gauge + help: 'Disk capacity in megabytes for the volume hosting the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [disk_capacity_mb] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__disk_free_mb + type: gauge + help: 'Disk free space in megabytes for the volume hosting the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [disk_free_mb] + query_ref: mssql_virtualfilestats + + - metric_name: mssql_virtualfilestats__disk_used_mb + type: gauge + help: 'Disk used space in megabytes for the volume hosting the SQL Server file.' + key_labels: [file_location, database_name, disk_volume] + values: [disk_used_mb] + query_ref: mssql_virtualfilestats + + # + # Collected using tempdb file space usage DMVs + # + - metric_name: mssql_tempdb_usage__collection_time_utc + type: gauge + help: 'Collection time as Unix epoch seconds for the tempdb usage snapshot.' + values: [collection_time_utc] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__data_size_mb + type: gauge + help: 'Total tempdb data file size in megabytes.' + values: [data_size_mb] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__data_used_mb + type: gauge + help: 'Used tempdb data file space in megabytes.' + values: [data_used_mb] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__data_used_pct + type: gauge + help: 'Used tempdb data file space as a percentage of total data size.' + values: [data_used_pct] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__log_size_mb + type: gauge + help: 'Total tempdb log file size in megabytes.' + values: [log_size_mb] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__log_used_mb + type: gauge + help: 'Used tempdb log file space in megabytes.' + values: [log_used_mb] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__log_used_pct + type: gauge + help: 'Used tempdb log file space as a percentage of total log size.' + values: [log_used_pct] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__version_store_mb + type: gauge + help: 'Tempdb version store size in megabytes.' + values: [version_store_mb] + query_ref: mssql_tempdb_usage + + - metric_name: mssql_tempdb_usage__version_store_pct + type: gauge + help: 'Tempdb version store size as a percentage of used data space.' + values: [version_store_pct] + query_ref: mssql_tempdb_usage + + # + # Collected using tempdb consumer session details + # + - metric_name: mssql_tempdb_consumers__collection_time_utc + type: gauge + help: 'Collection time as Unix epoch seconds for the tempdb consumer row.' + key_labels: [spid] + values: [collection_time_utc] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__host_process_id + type: gauge + help: 'Host process ID for the tempdb consumer session.' + key_labels: [spid] + values: [host_process_id] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__is_active_session + type: gauge + help: 'Whether the tempdb consumer session currently has an active request.' + key_labels: [spid] + values: [is_active_session] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__open_transaction_count + type: gauge + help: 'Open transaction count for the tempdb consumer session.' + key_labels: [spid] + values: [open_transaction_count] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__size_bytes + type: gauge + help: 'Current tempdb space consumed by the session in bytes.' + key_labels: [spid] + values: [size_bytes] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__transaction_begin_time + type: gauge + help: 'Transaction begin time as Unix epoch seconds for the tempdb consumer session.' + key_labels: [spid] + values: [transaction_begin_time] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__is_snapshot + type: gauge + help: 'Whether the tempdb consumer session is associated with snapshot activity.' + key_labels: [spid] + values: [is_snapshot] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__log_bytes + type: gauge + help: 'Transaction log bytes used by the tempdb consumer session.' + key_labels: [spid] + values: [log_bytes] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__log_rsvd + type: gauge + help: 'Transaction log bytes reserved by the tempdb consumer session.' + key_labels: [spid] + values: [log_rsvd] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__data_used_pct_threshold + type: gauge + help: 'Configured tempdb data used percentage threshold.' + key_labels: [spid] + values: [data_used_pct_threshold] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__data_used_gb_threshold + type: gauge + help: 'Configured tempdb data used threshold in gigabytes.' + key_labels: [spid] + values: [data_used_gb_threshold] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__version_store_pct_threshold + type: gauge + help: 'Configured version store percentage threshold.' + key_labels: [spid] + values: [version_store_pct_threshold] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__thresholds_violated + type: gauge + help: 'Whether the configured tempdb thresholds are violated for the session result row.' + key_labels: + - spid + - login_name + - program_name + - host_name + - transaction_isolation_level + - action_taken + - issue_type + - threshold_condition + values: [thresholds_violated] + query_ref: mssql_tempdb_consumers + + - metric_name: mssql_tempdb_consumers__usage_rank + type: gauge + help: 'Ranking of the tempdb consumer row within the result set.' + key_labels: [spid] + values: [usage_rank] + query_ref: mssql_tempdb_consumers + +queries: + - query_name: mssql_os_wait_stats + query: | + SELECT + [collection_time_utc] = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint), + [wait_type], + [wait_time_ms] / 1000.0 AS [wait_time_seconds], + ([wait_time_ms] - [signal_wait_time_ms]) / 1000.0 AS [resource_time_seconds], + [signal_wait_time_ms] / 1000.0 AS [signal_time_seconds], + [waiting_tasks_count], + 100.0 * [wait_time_ms] / SUM([wait_time_ms]) OVER() AS [wait_percentage], + ROW_NUMBER() OVER (ORDER BY [wait_time_ms] DESC) AS [wait_rank_no] + FROM sys.dm_os_wait_stats + WHERE [wait_type] NOT IN ( + N'BROKER_EVENTHANDLER', N'BROKER_RECEIVE_WAITFOR', N'BROKER_TASK_STOP', N'BROKER_TO_FLUSH', N'BROKER_TRANSMITTER', N'CHECKPOINT_QUEUE', N'CHKPT', + N'CLR_AUTO_EVENT', N'CLR_MANUAL_EVENT', N'CLR_SEMAPHORE', N'DBMIRROR_DBM_EVENT', N'DBMIRROR_EVENTS_QUEUE', N'DBMIRROR_WORKER_QUEUE', + N'DBMIRRORING_CMD', N'DIRTY_PAGE_POLL', N'DISPATCHER_QUEUE_SEMAPHORE', N'EXECSYNC', N'FSAGENT', N'FT_IFTS_SCHEDULER_IDLE_WAIT', N'FT_IFTSHC_MUTEX', + N'HADR_CLUSAPI_CALL', N'HADR_FILESTREAM_IOMGR_IOCOMPLETION', N'HADR_LOGCAPTURE_WAIT', N'HADR_NOTIFICATION_DEQUEUE', N'HADR_TIMER_TASK', + N'HADR_WORK_QUEUE', N'KSOURCE_WAKEUP', N'LAZYWRITER_SLEEP', N'LOGMGR_QUEUE', N'MEMORY_ALLOCATION_EXT', N'ONDEMAND_TASK_QUEUE', + N'PARALLEL_REDO_DRAIN_WORKER', N'PARALLEL_REDO_LOG_CACHE', N'PARALLEL_REDO_TRAN_LIST', N'PARALLEL_REDO_WORKER_SYNC', N'PARALLEL_REDO_WORKER_WAIT_WORK', + N'PREEMPTIVE_OS_FLUSHFILEBUFFERS', N'PREEMPTIVE_XE_GETTARGETSTATE', N'PVS_PREALLOCATE', N'PWAIT_ALL_COMPONENTS_INITIALIZED', + N'PWAIT_DIRECTLOGCONSUMER_GETNEXT', N'PWAIT_EXTENSIBILITY_CLEANUP_TASK', N'QDS_PERSIST_TASK_MAIN_LOOP_SLEEP', N'QDS_ASYNC_QUEUE', + N'QDS_CLEANUP_STALE_QUERIES_TASK_MAIN_LOOP_SLEEP', N'QDS_SHUTDOWN_QUEUE', N'REDO_THREAD_PENDING_WORK', N'REQUEST_FOR_DEADLOCK_SEARCH', + N'RESOURCE_QUEUE', N'SERVER_IDLE_CHECK', N'SLEEP_BPOOL_FLUSH', N'SLEEP_DBSTARTUP', N'SLEEP_DCOMSTARTUP', N'SLEEP_MASTERDBREADY', + N'SLEEP_MASTERMDREADY', N'SLEEP_MASTERUPGRADED', N'SLEEP_MSDBSTARTUP', N'SLEEP_SYSTEMTASK', N'SLEEP_TASK', N'SLEEP_TEMPDBSTARTUP', + N'SNI_HTTP_ACCEPT', N'SOS_WORK_DISPATCHER', N'SP_SERVER_DIAGNOSTICS_SLEEP', N'SQLTRACE_BUFFER_FLUSH', N'SQLTRACE_INCREMENTAL_FLUSH_SLEEP', + N'SQLTRACE_WAIT_ENTRIES', N'VDI_CLIENT_OTHER', N'WAIT_FOR_RESULTS', N'WAITFOR', N'WAITFOR_TASKSHUTDOWN', N'WAIT_XTP_RECOVERY', N'WAIT_XTP_HOST_WAIT', + N'WAIT_XTP_OFFLINE_CKPT_NEW_LOG', N'WAIT_XTP_CKPT_CLOSE', N'XE_DISPATCHER_JOIN', N'XE_DISPATCHER_WAIT', N'XE_TIMER_EVENT' + ) + AND [wait_type] NOT LIKE 'SLEEP_%' + AND [wait_type] NOT LIKE 'BROKER_%' + AND [waiting_tasks_count] > 0; + + # - query_name: mssql_diskspace + # query: | + # select distinct + # --[collection_time_utc] = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint), + # [host_name] = convert(varchar(225),COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'),SERVERPROPERTY('ServerName'))), + # [disk_volume] = vs.volume_mount_point, + # --[label] = null, + # [capacity_mb] = vs.total_bytes / 1048576, + # [free_mb] = vs.available_bytes / 1048576, + # [used_mb] = (vs.total_bytes - vs.available_bytes) / 1048576, + # --[block_size] = null, + # [filesystem] = file_system_type + # from sys.master_files as mf + # cross apply sys.dm_os_volume_stats(mf.database_id, mf.file_id) as vs + # join sys.databases as db on db.database_id = mf.database_id; + + - query_name: mssql_virtualfilestats + query: | + select [collection_time_utc] = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint), + d.name as [database_name], mf.name as file_logical_name, + --mf.file_id, d.database_id, + [disk_volume] = coalesce(vs.volume_mount_point, N''), mf.physical_name as file_location, + vfs.sample_ms, vfs.num_of_reads, vfs.num_of_bytes_read, vfs.io_stall_read_ms, vfs.io_stall_queued_read_ms, + vfs.num_of_writes, vfs.num_of_bytes_written, vfs.io_stall_write_ms, vfs.io_stall_queued_write_ms, + vfs.io_stall, vfs.size_on_disk_bytes, + [disk_capacity_mb] = coalesce(vs.total_bytes / 1048576, 0), + [disk_free_mb] = coalesce(vs.available_bytes / 1048576, 0), + [disk_used_mb] = coalesce((vs.total_bytes - vs.available_bytes) / 1048576, 0), + --[block_size] = null, + [disk_filesystem] = coalesce(file_system_type, N'') + from sys.dm_io_virtual_file_stats(null,null) vfs + join sys.master_files mf on mf.database_id = vfs.database_id and mf.file_id = vfs.file_id + join sys.databases d on d.database_id = mf.database_id + outer apply (select * from sys.dm_os_volume_stats(mf.database_id, mf.file_id) as vs) vs; + + - query_name: mssql_tempdb_usage + query: | + use tempdb ; + + ;with t_files_size as ( + select [file_type] = f.type_desc, + [size_mb] = convert(numeric(10,2), (f.size*8.0)/1024 ), + [used_mb] = convert(numeric(10,2), CAST(FILEPROPERTY(f.name, 'SpaceUsed') as int)/128.0 ) + from sys.database_files f left join sys.filegroups fg on fg.data_space_id = f.data_space_id + ) + ,t_files_by_type as ( + select [data_size_mb] = case when file_type = 'ROWS' then [size_mb] else 0.0 end, + [data_used_mb] = case when file_type = 'ROWS' then [used_mb] else 0.0 end, + [log_size_mb] = case when file_type = 'LOG' then [size_mb] else 0.0 end, + [log_used_mb] = case when file_type = 'LOG' then [used_mb] else 0 end, + vs.[version_store_mb] + from t_files_size fs + full outer join ( SELECT [version_store_mb] = (SUM(version_store_reserved_page_count) / 128.0) + FROM tempdb.sys.dm_db_file_space_usage fsu with (nolock) + ) vs + on 1=1 + ) + select [collection_time_utc] = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint), + [data_size_mb] = sum([data_size_mb]), + [data_used_mb] = sum([data_used_mb]), + [data_used_pct] = convert(numeric(12,2),sum([data_used_mb])*100.0/(sum([data_size_mb]))), + [log_size_mb] = sum([log_size_mb]), + [log_used_mb] = sum([log_used_mb]), + [log_used_pct] = convert(numeric(12,2),sum([log_used_mb])*100.0/(sum([log_size_mb]))), + [version_store_mb] = max([version_store_mb]), + [version_store_pct] = convert(numeric(12,2),max([version_store_mb])*100.0/sum([data_used_mb])) + from t_files_by_type; + + + - query_name: mssql_tempdb_consumers + query: | + SET NOCOUNT ON; + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SET XACT_ABORT ON; + + DECLARE + @data_used_pct_threshold tinyint = 90, + @data_used_gb_threshold int = 100, + @version_store_pct_threshold tinyint = 30, + @threshold_condition varchar(5) = 'or', /* {and | or} */ + @kill_spids bit = 0, + @verbose tinyint = 0, /* 1 => messages, 2 => messages + table results */ + @first_x_rows int = 10 + + IF @data_used_pct_threshold IS NULL AND @data_used_gb_threshold IS NULL + THROW 51000, '@data_used_pct_threshold or @data_used_gb_threshold must be provided.', 1; + IF @threshold_condition NOT IN ('and','or') + THROW 51000, 'Expected value for @threshold_condition are {and || or} only.', 1; + + DECLARE @_start_time datetime2; + SET @_start_time = SYSDATETIME(); + + DECLARE @_sql nvarchar(max) = ''; + DECLARE @_sql_kill nvarchar(max); + DECLARE @_params nvarchar(max); + DECLARE @_email_body varchar(max) = null; + DECLARE @_email_subject nvarchar(255) = 'Tempdb Saver: ' + @@SERVERNAME; + DECLARE @_data_used_pct_current decimal(5,2); + DECLARE @_is_pct_threshold_violated bit = 0; + DECLARE @_is_gb_threshold_violated bit = 0; + DECLARE @_is_version_store_issue bit = 0; + DEclare @_thresholds_violated bit = 0; + + SET @_params = N'@collection_time datetime2'; + + DECLARE @t_login_exceptions TABLE (login_name sysname); + /* + INSERT @t_login_exceptions + VALUES ('sa'), + ('NT AUTHORITY\SYSTEM'); + */ + + IF (@verbose > 0) + PRINT '('+convert(varchar, getdate(), 21)+') Creating table variable @t_tempdb_space_usage..'; + DECLARE @t_tempdb_space_usage TABLE + ( [collection_time] datetime2 not null, + [data_size_mb] varchar(100) not null, + [data_used_mb] varchar(100) not null, + [data_used_pct] decimal(5,2) not null, + [log_size_mb] varchar(100) not null, + [log_used_mb] varchar(100) null, + [log_used_pct] decimal(5,2) null, + [version_store_mb] decimal(20,2) null, + [version_store_pct] decimal(20,2) null + ); + + IF (@verbose > 0) + PRINT '('+convert(varchar, getdate(), 21)+') Creating table variable @t_tempdb_consumers..'; + DECLARE @t_tempdb_consumers TABLE + ( + [collection_time] [datetime2] NOT NULL, + [spid] [smallint] NULL, + [login_name] [nvarchar](128) NOT NULL, + [program_name] [nvarchar](128) NULL, + [host_name] [nvarchar](128) NULL, + [host_process_id] [int] NULL, + [is_active_session] [int] NOT NULL, + [open_transaction_count] [int] NOT NULL, + [transaction_isolation_level] [varchar](15) NULL, + [size_bytes] [bigint] NULL, + [transaction_begin_time] [datetime] NULL, + [is_snapshot] [int] NOT NULL, + [log_bytes] [bigint] NULL, + [log_rsvd] [bigint] NULL, + [action_taken] [varchar](200) NULL + ); + + IF (@verbose > 0) + PRINT '('+convert(varchar, getdate(), 21)+') Populate table @t_tempdb_space_usage..' + SET @_sql = ' + use tempdb ; + + ;with t_files_size as ( + select [file_type] = f.type_desc, + [size_mb] = convert(numeric(10,2), (f.size*8.0)/1024 ), + [used_mb] = convert(numeric(10,2), CAST(FILEPROPERTY(f.name, ''SpaceUsed'') as int)/128.0 ) + from sys.database_files f left join sys.filegroups fg on fg.data_space_id = f.data_space_id + ) + ,t_files_by_type as ( + select [data_size_mb] = case when file_type = ''ROWS'' then [size_mb] else 0.0 end, + [data_used_mb] = case when file_type = ''ROWS'' then [used_mb] else 0.0 end, + [log_size_mb] = case when file_type = ''LOG'' then [size_mb] else 0.0 end, + [log_used_mb] = case when file_type = ''LOG'' then [used_mb] else 0 end, + vs.[version_store_mb] + from t_files_size fs + full outer join ( SELECT [version_store_mb] = (SUM(version_store_reserved_page_count) / 128.0) + FROM tempdb.sys.dm_db_file_space_usage fsu with (nolock) + ) vs + on 1=1 + ) + select [collection_time] = @collection_time, + [data_size_mb] = sum([data_size_mb]), + [data_used_mb] = sum([data_used_mb]), + [data_used_pct] = convert(numeric(12,2),sum([data_used_mb])*100.0/(sum([data_size_mb]))), + [log_size_mb] = sum([log_size_mb]), + [log_used_mb] = sum([log_used_mb]), + [log_used_pct] = convert(numeric(12,2),sum([log_used_mb])*100.0/(sum([log_size_mb]))), + [version_store_mb] = max([version_store_mb]), + [version_store_pct] = convert(numeric(12,2),max([version_store_mb])*100.0/sum([data_used_mb])) + from t_files_by_type; '; + + IF (@verbose > 0) + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') Insert @t_tempdb_space_usage..'+CHAR(10)+CHAR(13); + PRINT @_sql; + END + + INSERT INTO @t_tempdb_space_usage + ([collection_time], data_size_mb, data_used_mb, data_used_pct, log_size_mb, log_used_mb, log_used_pct, version_store_mb, version_store_pct) + EXEC sp_executesql @_sql, @_params, @collection_time = @_start_time; + + IF (@verbose >= 2) + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') select * from @t_tempdb_space_usage..' + select top 1 running_query, t.* + from @t_tempdb_space_usage t + full outer join (values ('@t_tempdb_space_usage') )dummy(running_query) on 1 = 1 + where t.collection_time = @_start_time; + END + + IF (@verbose > 0) + PRINT '('+convert(varchar, getdate(), 21)+') Populate table @t_tempdb_consumers..' + SET @_sql = ' + ;WITH T_SnapshotTran + AS ( + SELECT [s_tst].[session_id], --DB_NAME(s_tdt.database_id) as database_name, + [begin_time] = ISNULL(MIN([s_tdt].[database_transaction_begin_time]),MIN(DATEADD(SECOND,-snp.elapsed_time_seconds,GETDATE()))), + --[database_transaction_begin_time] = MIN([s_tdt].[database_transaction_begin_time]), + --[database_transaction_begin_time2] = MIN(DATEADD(SECOND,-snp.elapsed_time_seconds,GETDATE())), + SUM([s_tdt].[database_transaction_log_bytes_used]) AS [log_bytes], + SUM([s_tdt].[database_transaction_log_bytes_reserved]) AS [log_rsvd], + MAX(CASE WHEN snp.elapsed_time_seconds IS NOT NULL THEN 1 ELSE 0 END) AS is_snapshot + FROM sys.dm_tran_database_transactions [s_tdt] + JOIN sys.dm_tran_session_transactions [s_tst] + ON [s_tst].[transaction_id] = [s_tdt].[transaction_id] + LEFT JOIN sys.dm_tran_active_snapshot_database_transactions snp + ON snp.session_id = s_tst.session_id AND snp.transaction_id = s_tst.transaction_id + --WHERE s_tdt.database_id = 2 + GROUP BY [s_tst].[session_id] --,s_tdt.database_id + ) + ,T_TempDbTrans AS + ( + SELECT [collection_time] = @collection_time, + [spid] = des.session_id, + [login_name] = des.original_login_name, + --des.program_name, + [program_name] = CASE WHEN des.program_name like ''SQLAgent - TSQL JobStep %'' + THEN ( select top 1 ''SQL Job = ''+j.name + from msdb.dbo.sysjobs (nolock) as j + inner join msdb.dbo.sysjobsteps (nolock) AS js on j.job_id=js.job_id + where right(cast(js.job_id as nvarchar(50)),10) = RIGHT(substring(des.program_name,30,34),10) + ) + '' ( ''+SUBSTRING(LTRIM(RTRIM(des.program_name)), CHARINDEX('': Step '',LTRIM(RTRIM(des.program_name)))+2,LEN(LTRIM(RTRIM(des.program_name)))-CHARINDEX('': Step '',LTRIM(RTRIM(des.program_name)))-2)+'' )'' collate SQL_Latin1_General_CP1_CI_AS + ELSE des.program_name + END, + des.host_name, + des.host_process_id, + [is_active_session] = CASE WHEN er.request_id IS NOT NULL THEN 1 ELSE 0 END, + des.open_transaction_count, + [transaction_isolation_level] = (CASE des.transaction_isolation_level + WHEN 0 THEN ''Unspecified'' + WHEN 1 THEN ''ReadUncommitted'' + WHEN 2 THEN ''ReadCommitted'' + WHEN 3 THEN ''Repeatable'' + WHEN 4 THEN ''Serializable'' + WHEN 5 THEN ''Snapshot'' END ), + [size_bytes] = ((ssu.user_objects_alloc_page_count+ssu.internal_objects_alloc_page_count)-(ssu.internal_objects_dealloc_page_count+ssu.user_objects_dealloc_page_count))*8192, + [transaction_begin_time] = case when des.open_transaction_count > 0 then (case when ott.begin_time is not null then ott.begin_time when er.start_time is not null then er.start_time else des.last_request_start_time end) else er.start_time end, + [is_snapshot] = CASE WHEN ISNULL(ott.is_snapshot,0) = 1 THEN 1 + WHEN tasdt.is_snapshot = 1 THEN 1 + ELSE ISNULL(ott.is_snapshot,0) + END, + ott.[log_bytes], ott.log_rsvd, + CONVERT(varchar(200),NULL) AS action_taken + FROM sys.dm_exec_sessions des + LEFT JOIN sys.dm_db_session_space_usage ssu on ssu.session_id = des.session_id + LEFT JOIN T_SnapshotTran ott ON ott.session_id = ssu.session_id + LEFT JOIN sys.dm_exec_requests er ON er.session_id = des.session_id + OUTER APPLY (SELECT ( (tsu.user_objects_alloc_page_count+tsu.internal_objects_alloc_page_count)-(tsu.user_objects_dealloc_page_count+tsu.internal_objects_dealloc_page_count) )*8192 AS size_bytes + FROM sys.dm_db_task_space_usage tsu + WHERE ((tsu.user_objects_alloc_page_count+tsu.internal_objects_alloc_page_count)-(tsu.user_objects_dealloc_page_count+tsu.internal_objects_dealloc_page_count)) > 0 + AND tsu.session_id = er.session_id + ) as ra + OUTER APPLY (select 1 as [is_snapshot] from sys.dm_tran_active_snapshot_database_transactions asdt where asdt.session_id = des.session_id) as tasdt + WHERE des.session_id <> @@SPID --AND (er.request_id IS NOT NULL OR des.open_transaction_count > 0) + --AND ssu.database_id = 2 + ) + SELECT top (@first_x_rows) * + FROM T_TempDbTrans ot + WHERE 1=1 + and ot.[spid] >= 50 + and (size_bytes > 0 OR is_active_session = 1 OR open_transaction_count > 0 OR is_snapshot = 1) + ' + IF EXISTS (SELECT * FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time and s.version_store_pct >= @version_store_pct_threshold) + BEGIN + SET @_sql = @_sql + 'order by is_snapshot DESC, (case when transaction_begin_time is null then dateadd(day,1,getdate()) else transaction_begin_time end ) ASC, size_bytes desc;'+CHAR(10) + END + ELSE + SET @_sql = @_sql + 'order by size_bytes desc;'+CHAR(10) + + IF (@verbose > 1) + PRINT @_sql + + SET @_params = N'@collection_time datetime2, @first_x_rows int'; + INSERT @t_tempdb_consumers + EXEC sp_executesql @_sql, @_params, @collection_time = @_start_time, @first_x_rows = @first_x_rows; + + IF (@verbose > 1) + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') select * from @t_tempdb_consumers..' + + IF EXISTS (SELECT * FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time and s.version_store_pct >= @version_store_pct_threshold) + select running_query, t.* + from @t_tempdb_consumers t + full outer join (values ('@t_tempdb_consumers') )dummy(running_query) on 1 = 1 + order by is_snapshot DESC, transaction_begin_time ASC; + ELSE + select running_query, t.* --top (@first_x_rows) + from @t_tempdb_consumers t + full outer join (values ('@t_tempdb_consumers') )dummy(running_query) on 1 = 1 + order by size_bytes desc; + END + + IF @data_used_pct_threshold IS NOT NULL AND EXISTS (SELECT 1/0 FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time and data_used_pct > @data_used_pct_threshold) + SET @_is_pct_threshold_violated = 1; + IF @data_used_gb_threshold IS NOT NULL AND EXISTS (SELECT 1/0 FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time and data_used_mb > (@data_used_gb_threshold*1024.0)) + SET @_is_gb_threshold_violated = 1; + SET @_thresholds_violated = (CASE WHEN @threshold_condition = 'and' and (@_is_pct_threshold_violated = 1 and @_is_gb_threshold_violated = 1) THEN 1 + WHEN @threshold_condition = 'or' and (@_is_pct_threshold_violated = 1 OR @_is_gb_threshold_violated = 1) THEN 1 + ELSE 0 + END); + + IF @verbose >= 1 + BEGIN + SELECT [@data_used_pct_threshold] = @data_used_pct_threshold, + [@_is_pct_threshold_violated] = @_is_pct_threshold_violated, + [@data_used_gb_threshold] = @data_used_gb_threshold, + [@_is_gb_threshold_violated] = @_is_gb_threshold_violated, + [@_thresholds_violated] = @_thresholds_violated, + [@threshold_condition] = @threshold_condition; + END + + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Validate @_thresholds_violated, and take action..' + IF @_thresholds_violated = 1 + BEGIN + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Found @_thresholds_violated to be true.' + + IF EXISTS (SELECT * FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time and s.version_store_pct >= @version_store_pct_threshold) -- If Version Store Issue + SET @_is_version_store_issue = 1; + ELSE + SET @_is_version_store_issue = 0; + + IF @_is_version_store_issue = 1 + BEGIN + IF @verbose > 0 + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') Version Store Issue.'; + PRINT '('+convert(varchar, getdate(), 21)+') version_store_mb >= '+convert(varchar(20), @version_store_pct_threshold)+'% of data_used_mb'; + PRINT '('+convert(varchar, getdate(), 21)+') Pick top spid (@_sql_kill) order by ''ORDER BY is_snapshot DESC, transaction_begin_time ASC'''; + END + SELECT TOP 1 @_sql_kill = CONVERT(varchar(30), tu.spid) + FROM @t_tempdb_consumers tu + WHERE host_process_id IS NOT NULL + AND login_name NOT IN (SELECT ex.login_name FROM @t_login_exceptions ex) + ORDER BY is_snapshot DESC, transaction_begin_time ASC; + END + ELSE + BEGIN -- Not Version Store issue. + IF @verbose > 0 + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') Not Version Store Issue.'; + PRINT '('+convert(varchar, getdate(), 21)+') version_store_mb < '+convert(varchar(20), @version_store_pct_threshold)+'% of data_used_mb'; + PRINT '('+convert(varchar, getdate(), 21)+') Pick top spid (@_sql_kill) order by ''(ISNULL(size_bytes,0)+ISNULL(log_bytes,0)+ISNULL(log_rsvd,0)) DESC'''; + END + SELECT TOP 1 @_sql_kill = CONVERT(varchar(30), tu.spid) + FROM @t_tempdb_consumers tu + WHERE host_process_id IS NOT NULL + AND login_name NOT IN (SELECT ex.login_name FROM @t_login_exceptions ex) + AND size_bytes <> 0 + ORDER BY (ISNULL(size_bytes,0)+ISNULL(log_bytes,0)+ISNULL(log_rsvd,0)) DESC; + END + + + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Top tempdb consumer spid (@_sql_kill) = '+@_sql_kill; + + IF (@_sql_kill IS NOT NULL) + BEGIN + IF (@kill_spids = 1) + BEGIN + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Kill top consumer.'; + UPDATE @t_tempdb_consumers SET action_taken = 'Process Terminated' WHERE spid = @_sql_kill + SET @_sql = 'kill ' + @_sql_kill; + PRINT (@_sql); + EXEC (@_sql); + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Update @t_tempdb_consumers with action_taken ''Process Terminated''.'; + END + ELSE + BEGIN + UPDATE @t_tempdb_consumers SET action_taken = 'Notified DBA' WHERE spid = @_sql_kill + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Update @t_tempdb_consumers with action_taken ''Notified DBA''.'; + END; + + SET @_email_body = 'The following SQL Server process ' + CASE WHEN @kill_spids = 1 THEN 'was' ELSE 'is' END + ' consuming the most tempdb space.' + CHAR(10) + CHAR(10) + SELECT @_data_used_pct_current = data_used_pct FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time; + SELECT @_email_body = @_email_body + + ' date_time: ' + CONVERT(varchar(100), collection_time, 121) + CHAR(10) + + 'tempdb_used_pct: ' + CONVERT(varchar(100), @_data_used_pct_current) + CHAR(10) + + ' spid: ' + CONVERT(varchar(30), spid) + CHAR(10) + + ' login_name: ' + login_name + CHAR(10) + + ' program_name: ' + ISNULL(program_name, '') + CHAR(10) + + ' host_name: ' + ISNULL(host_name, '') + CHAR(10) + + 'host_process_id: ' + CONVERT(varchar(30), host_process_id) + CHAR(10) + + ' is_active: ' + CONVERT(varchar(30), is_active_session) + CHAR(10) + + ' tran_count: ' + CONVERT(varchar(30), open_transaction_count) + CHAR(10) + + ' is_snapshot: ' + CONVERT(varchar(30), is_snapshot) + CHAR(10) + + 'tran_start_time: ' + (case when transaction_begin_time is null then '' else CONVERT(varchar(100), transaction_begin_time, 121) end) + CHAR(10) + + ' action_taken: ' + action_taken + CHAR(10) + CHAR(10) + FROM @t_tempdb_consumers tu + WHERE spid = @_sql_kill; + + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Email body to send to DBA..'+CHAR(10)+CHAR(13)+@_email_body; + END; + END; + ELSE + BEGIN + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Current tempdb space usage under threshold.'; + END + + IF @verbose > 0 + PRINT '('+convert(varchar, getdate(), 21)+') Get top 10 session details causing version store issue..'; + IF EXISTS (SELECT * FROM @t_tempdb_space_usage s WHERE s.collection_time = @_start_time and s.version_store_pct >= @version_store_pct_threshold) + BEGIN + SELECT [collection_time_utc] = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint), + [spid] = COALESCE(CONVERT(varchar(30), c.[spid]), ''), + [login_name] = COALESCE(c.[login_name], ''), + [program_name] = COALESCE(c.[program_name], ''), + [host_name] = COALESCE(c.[host_name], ''), + [host_process_id] = COALESCE(c.[host_process_id], 0), + [is_active_session] = COALESCE(c.[is_active_session], 0), + [open_transaction_count] = COALESCE(c.[open_transaction_count], 0), + [transaction_isolation_level] = COALESCE(c.[transaction_isolation_level], ''), + [size_bytes] = COALESCE(c.[size_bytes], 0), + [transaction_begin_time] = CASE WHEN c.[transaction_begin_time] IS NULL THEN 0 ELSE DATEDIFF_BIG(SECOND, '1970-01-01T00:00:00', CAST(c.[transaction_begin_time] AS datetime2)) END, + [is_snapshot] = COALESCE(c.[is_snapshot], 0), + [log_bytes] = COALESCE(c.[log_bytes], 0), + [log_rsvd] = COALESCE(c.[log_rsvd], 0), + [action_taken] = COALESCE(c.[action_taken], ''), + [issue_type] = 'version-store', + [data_used_pct_threshold] = COALESCE(@data_used_pct_threshold, 0), + [data_used_gb_threshold] = COALESCE(@data_used_gb_threshold, 0), + [version_store_pct_threshold] = COALESCE(@version_store_pct_threshold, 0), + [threshold_condition] = COALESCE(@threshold_condition, ''), + [thresholds_violated] = CONVERT(int, COALESCE(@_thresholds_violated, 0)), + [usage_rank] = ROW_NUMBER()over(order by collection_time, is_snapshot DESC, (case when transaction_begin_time is null then dateadd(day,1,getdate()) else transaction_begin_time end) ASC) + FROM @t_tempdb_consumers c + WHERE @_thresholds_violated = 1 + order by collection_time, is_snapshot DESC, (case when transaction_begin_time is null then dateadd(day,1,getdate()) else transaction_begin_time end) ASC, [size_bytes] desc, [log_rsvd] desc + offset 0 rows fetch next @first_x_rows rows only; + END + ELSE + BEGIN + SELECT [collection_time_utc] = CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint), + [spid] = COALESCE(CONVERT(varchar(30), [spid]), ''), + [login_name] = COALESCE([login_name], ''), + [program_name] = COALESCE([program_name], ''), + [host_name] = COALESCE([host_name], ''), + [host_process_id] = COALESCE([host_process_id], 0), + [is_active_session] = COALESCE([is_active_session], 0), + [open_transaction_count] = COALESCE([open_transaction_count], 0), + [transaction_isolation_level] = COALESCE([transaction_isolation_level], ''), + [size_bytes] = COALESCE([size_bytes], 0), + [transaction_begin_time] = CASE WHEN [transaction_begin_time] IS NULL THEN 0 ELSE DATEDIFF_BIG(SECOND, '1970-01-01T00:00:00', CAST([transaction_begin_time] AS datetime2)) END, + [is_snapshot] = COALESCE([is_snapshot], 0), + [log_bytes] = COALESCE([log_bytes], 0), + [log_rsvd] = COALESCE([log_rsvd], 0), + [action_taken] = COALESCE([action_taken], ''), + [issue_type] = 'non-version-store', + [data_used_pct_threshold] = COALESCE(@data_used_pct_threshold, 0), + [data_used_gb_threshold] = COALESCE(@data_used_gb_threshold, 0), + [version_store_pct_threshold] = COALESCE(@version_store_pct_threshold, 0), + [threshold_condition] = COALESCE(@threshold_condition, ''), + [thresholds_violated] = CONVERT(int, COALESCE(@_thresholds_violated, 0)), + [usage_rank] = ROW_NUMBER()over(order by collection_time, size_bytes DESC) + FROM @t_tempdb_consumers + WHERE @_thresholds_violated = 1 + order by collection_time, size_bytes DESC + offset 0 rows fetch next @first_x_rows rows only; + END + + + diff --git a/sql_exporter/mssql_dba_regular.collector.yml b/sql_exporter/mssql_dba_regular.collector.yml new file mode 100644 index 0000000..10a3820 --- /dev/null +++ b/sql_exporter/mssql_dba_regular.collector.yml @@ -0,0 +1,1549 @@ +# A collector defining standard metrics for Microsoft SQL Server. +# +# It is required that the SQL Server user has the following permissions: +# +# GRANT VIEW ANY DEFINITION TO +# GRANT VIEW SERVER STATE TO +# +collector_name: mssql_dba_regular + +# Similar to global.min_interval, but applies to the queries defined by this collector only. +#min_interval: 0s + +metrics: + - metric_name: mssql_service_info + type: gauge + help: 'SQL Server instance/service availability and metadata.' + static_labels: + env: prod + infra_type: on-prem + owner: dba_team + app: sqlmonitor + key_labels: + - domain + - ip + - at_server_name + - machine_name + - server_name + - host_name + - sql_version + - service_name_str + - service_name + - instance_name + - service_account + - ports + - edition + - product_version + - product_level + values: [up] + query: | + ;WITH cte_ports as ( + SELECT [ports] = + STUFF(( + SELECT ', ' + CONVERT(varchar(20), p.local_tcp_port) + FROM ( + SELECT DISTINCT local_net_address, local_tcp_port + FROM sys.dm_exec_connections + WHERE local_net_address IS NOT NULL + ) p + FOR XML PATH(''), TYPE).value('.', 'varchar(2000)'), 1, 2, '') + ) + ,server_services AS ( + SELECT * + FROM sys.dm_server_services + WHERE servicename LIKE 'SQL Server (%)' + OR servicename LIKE 'SQL Server Agent (%)' + ) + SELECT + [domain] = DEFAULT_DOMAIN(), + [ip] = CONNECTIONPROPERTY('local_net_address'), + [at_server_name] = @@SERVERNAME, + [machine_name] = SERVERPROPERTY('MachineName'), + [server_name] = SERVERPROPERTY('ServerName'), + [host_name] = COALESCE(SERVERPROPERTY('ComputerNamePhysicalNetBIOS'), SERVERPROPERTY('ServerName')), + [sql_version] = @@VERSION, + [service_name_str] = servicename, + [service_name] = CASE + WHEN @@SERVICENAME IS NULL THEN 'MSSQLSERVER' + WHEN @@SERVICENAME = 'MSSQLSERVER' AND servicename LIKE 'SQL Server (%)' THEN 'MSSQLSERVER' + WHEN @@SERVICENAME = 'MSSQLSERVER' AND servicename LIKE 'SQL Server Agent (%)' THEN 'SQLSERVERAGENT' + WHEN @@SERVICENAME <> 'MSSQLSERVER' AND servicename LIKE 'SQL Server (%)' THEN 'MSSQL$' + @@SERVICENAME + WHEN @@SERVICENAME <> 'MSSQLSERVER' AND servicename LIKE 'SQL Server Agent (%)' THEN 'SQLAgent' + @@SERVICENAME + ELSE 'MSSQL$' + @@SERVICENAME + END, + [instance_name] = COALESCE(@@SERVICENAME, 'MSSQLSERVER'), + [service_account] = service_account, + [ports], + [edition] = SERVERPROPERTY('Edition'), + [product_version] = SERVERPROPERTY('ProductVersion'), + [product_level] = SERVERPROPERTY('ProductLevel'), + [up] = CAST(1 AS bigint) + FROM (VALUES ('Basic-Details')) d (RunningQuery) + FULL OUTER JOIN server_services ss ON 1 = 1 + FULL OUTER JOIN cte_ports p ON 1=1; + + - metric_name: mssql_host_physical_memory_bytes + type: gauge + help: 'Host physical memory in bytes.' + value_label: state + values: [total, available] + query_ref: mssql_sys_memory + + - metric_name: mssql_sql_process_memory_bytes + type: gauge + help: 'SQL Server process memory allocations in bytes.' + value_label: state + values: [in_use, locked_pages, large_pages] + query_ref: mssql_process_memory + + - metric_name: mssql_cpu_utilization_percentage + type: gauge + help: 'CPU utilization percentages from the SQL Server scheduler monitor ring buffer.' + value_label: scope + values: [system, sqlserver] + query_ref: mssql_cpu_utilization + + - metric_name: mssql_memory_manager_bytes + type: gauge + help: 'SQL Server memory manager allocations in bytes.' + value_label: state + values: [granted_workspace, maximum_workspace, stolen_server, total_server, target_server, sql_cache, free] + query_ref: mssql_memory_manager + + - metric_name: mssql_memory_grants_outstanding + type: gauge + help: 'Outstanding workspace memory grants.' + values: [memory_grants_outstanding] + query_ref: mssql_memory_manager + + - metric_name: mssql_database_file_size_bytes + type: gauge + help: 'Database file sizes in bytes.' + key_labels: + - db + value_label: state + values: [data, log, log_used] + query_ref: mssql_database_space + + - metric_name: mssql_database_percent_log_used + type: gauge + help: 'Transaction log space used as a percentage per database.' + key_labels: + - db + values: [percent_log_used] + query_ref: mssql_database_space + + - metric_name: mssql_database_active_transactions + type: gauge + help: 'Current active transactions per database.' + key_labels: + - db + values: [active_transactions] + query_ref: mssql_database_space + + - metric_name: mssql_database_xtp_memory_used_bytes + type: gauge + help: 'XTP memory usage in bytes per database.' + key_labels: + - db + values: [xtp_memory_used] + query_ref: mssql_database_space + + - metric_name: mssql_database_log_events_total + type: counter + help: 'Database log event counts since SQL Server startup.' + key_labels: + - db + value_label: event + values: [growths, shrinks, truncations] + query_ref: mssql_database_space + + - metric_name: mssql_active_cursors + type: gauge + help: 'Current active cursors by cursor type.' + key_labels: + - cursor_type + values: [active_cursors] + query_ref: mssql_active_cursors + + - metric_name: mssql_user_connections + type: gauge + help: 'Current user connections from SQL Server general statistics.' + values: [user_connections] + query_ref: mssql_general_statistics_snapshot + + - metric_name: mssql_temp_tables_for_destruction + type: gauge + help: 'Temp tables waiting to be destroyed.' + values: [temp_tables_for_destruction] + query_ref: mssql_general_statistics_snapshot + + - metric_name: mssql_waits_in_progress + type: gauge + help: 'Current waits in progress by wait category.' + key_labels: + - wait_type + values: [waits_in_progress] + query_ref: mssql_wait_stats + + - metric_name: mssql_transactions + type: gauge + help: 'Current transaction count per database from the Transactions object.' + key_labels: + - db + values: [transactions] + query_ref: mssql_transactions + + - metric_name: mssql_tempdb_space_bytes + type: gauge + help: 'tempdb free space and version store size in bytes.' + value_label: state + values: [free_space, version_store] + query_ref: mssql_tempdb_transactions + + - metric_name: mssql_longest_transaction_running_time_seconds + type: gauge + help: 'Longest transaction running time in seconds.' + values: [longest_transaction_running_time] + query_ref: mssql_tempdb_transactions + + - metric_name: mssql_worktables_from_cache_ratio + type: gauge + help: 'Worktables-from-cache ratio as a percentage.' + values: [worktables_from_cache_ratio] + query_ref: mssql_access_methods_ratio + + - metric_name: mssql_resource_pool_cpu_usage_percentage + type: gauge + help: 'CPU usage percentage by resource pool.' + key_labels: + - resource_pool + values: [cpu_usage_percentage] + query_ref: mssql_resource_pool_cpu + + - metric_name: mssql_workload_group_cpu_usage_percentage + type: gauge + help: 'CPU usage percentage by workload group.' + key_labels: + - workload_group + values: [cpu_usage_percentage] + query_ref: mssql_workload_group_cpu + + - metric_name: mssql_sql_auto_params_total + type: counter + help: 'Automatic parameterization activity counters since SQL Server startup.' + value_label: mode + values: [attempts, failed, safe, unsafe] + query_ref: mssql_sql_statistics + + - metric_name: mssql_sql_attentions_total + type: counter + help: 'SQL attentions since SQL Server startup.' + values: [sql_attentions] + query_ref: mssql_sql_statistics + + - metric_name: mssql_buffer_manager_operations_total + type: counter + help: 'Buffer manager operation counters since SQL Server startup.' + value_label: operation + values: [lazy_writes, free_list_stalls, readahead_pages] + query_ref: mssql_buffer_manager_counters + + - metric_name: mssql_lock_waits_total + type: counter + help: 'Lock waits since SQL Server startup.' + values: [lock_waits] + query_ref: mssql_lock_counters + + - metric_name: mssql_lock_wait_time_ms_total + type: counter + help: 'Accumulated lock wait time in milliseconds since SQL Server startup.' + values: [lock_wait_time_ms] + query_ref: mssql_lock_counters + + - metric_name: mssql_access_methods_total + type: counter + help: 'Access methods counters since SQL Server startup.' + value_label: operation + values: [full_scans, forwarded_records, index_searches, page_splits, workfiles_created, worktables_created, table_lock_escalations] + query_ref: mssql_access_methods_counters + + - metric_name: mssql_database_log_bytes_flushed_total + type: counter + help: 'Database log bytes flushed since SQL Server startup.' + key_labels: + - db + values: [log_bytes_flushed] + query_ref: mssql_database_log_counters + + - metric_name: mssql_database_log_flush_wait_time_ms_total + type: counter + help: 'Accumulated database log flush wait time in milliseconds since SQL Server startup.' + key_labels: + - db + values: [log_flush_wait_time_ms] + query_ref: mssql_database_log_counters + + - metric_name: mssql_database_log_waits_total + type: counter + help: 'Database log flush waits since SQL Server startup.' + key_labels: + - db + values: [log_flush_waits] + query_ref: mssql_database_log_counters + + - metric_name: mssql_database_log_flushes_total + type: counter + help: 'Database log flushes since SQL Server startup.' + key_labels: + - db + values: [log_flushes] + query_ref: mssql_database_log_counters + + - metric_name: mssql_sql_errors_total + type: counter + help: 'SQL error counters since SQL Server startup.' + key_labels: + - error_type + values: [errors] + query_ref: mssql_sql_errors + + - metric_name: mssqlbuffer_pool_committed + type: gauge + help: 'SQL Server buffer pool committed bytes.' + values: [buffer_pool_committed] + query_ref: mssql_sys_info_memory + + - metric_name: mssqlbuffer_pool_committed_target + type: gauge + help: 'SQL Server buffer pool committed target bytes.' + values: [buffer_pool_committed_target] + query_ref: mssql_sys_info_memory + + - metric_name: mssql_active_transactions_count + type: gauge + help: 'Count of active transactions from sys.dm_tran_active_transactions.' + values: [active_transactions_count] + query_ref: mssql_active_transactions_count + + - metric_name: mssql_write_transactions + type: counter + help: 'Write transactions since SQL Server startup.' + values: [write_transactions] + query_ref: mssql_replica_counters + + - metric_name: mssql_processes_blocked + type: gauge + help: 'Currently blocked processes.' + values: [processes_blocked] + query_ref: mssql_general_statistics_snapshot + + - metric_name: mssql_average_latch_wait_time_ms + type: gauge + help: 'Average latch wait time metric from SQL Server performance counters.' + values: [average_latch_wait_time_ms] + query_ref: mssql_latch_wait_stats + + - metric_name: mssql_memory_grants_pending + type: gauge + help: 'Pending workspace memory grants.' + values: [memory_grants_pending] + query_ref: mssql_memory_manager + + - metric_name: mssql_sql_compilations + type: counter + help: 'SQL compilations since SQL Server startup.' + values: [sql_compilations] + query_ref: mssql_sql_statistics + + - metric_name: mssql_sql_recompilations + type: counter + help: 'SQL recompilations since SQL Server startup.' + values: [sql_recompilations] + query_ref: mssql_sql_statistics + + - metric_name: mssql_page_reads + type: counter + help: 'Buffer manager page reads since SQL Server startup.' + values: [page_reads] + query_ref: mssql_buffer_manager_counters + + - metric_name: mssql_page_lookups + type: counter + help: 'Buffer manager page lookups since SQL Server startup.' + values: [page_lookups] + query_ref: mssql_buffer_manager_counters + + - metric_name: mssql_page_writes + type: counter + help: 'Buffer manager page writes since SQL Server startup.' + values: [page_writes] + query_ref: mssql_buffer_manager_counters + + - metric_name: mssql_connection_reset + type: counter + help: 'Connection resets since SQL Server startup.' + values: [connection_reset] + query_ref: mssql_general_statistics_counters + + - metric_name: mssql_logins + type: counter + help: 'Logins since SQL Server startup.' + values: [logins] + query_ref: mssql_general_statistics_counters + + - metric_name: mssql_logouts + type: counter + help: 'Logouts since SQL Server startup.' + values: [logouts] + query_ref: mssql_general_statistics_counters + + - metric_name: mssql_buffer_database_pages + type: gauge + help: 'Database pages in the SQL Server buffer pool.' + values: [buffer_database_pages] + query_ref: mssql_buffer_manager_counters + + - metric_name: mssql_buffer_target_pages + type: gauge + help: 'Target pages in the SQL Server buffer pool.' + values: [buffer_target_pages] + query_ref: mssql_buffer_manager_counters + + - metric_name: mssql_tempdb_active_temp_tables + type: gauge + help: 'Active temp tables from SQL Server general statistics.' + values: [tempdb_active_temp_tables] + query_ref: mssql_general_statistics_snapshot + + - metric_name: mssql_log_apply_pending_queue + type: gauge + help: 'Log apply pending queue.' + values: [log_apply_pending_queue] + query_ref: mssql_replica_counters + + - metric_name: mssql_log_remaining_for_undo + type: gauge + help: 'Log remaining for undo.' + values: [log_remaining_for_undo] + query_ref: mssql_replica_counters + + - metric_name: mssql_log_send_queue + type: gauge + help: 'Log send queue.' + values: [log_send_queue] + query_ref: mssql_replica_counters + + - metric_name: mssql_redo_blocked + type: counter + help: 'Redo blocked events since SQL Server startup.' + values: [redo_blocked] + query_ref: mssql_replica_counters + + - metric_name: mssql_transaction_delay + type: gauge + help: 'Transaction delay for database replicas.' + values: [transaction_delay] + query_ref: mssql_replica_counters + + - metric_name: mssql_page_io_latch_waits_ms + type: gauge + help: 'Page IO latch wait time metric from SQL Server wait statistics.' + values: [page_io_latch_waits_ms] + query_ref: mssql_wait_stats_averages + + - metric_name: mssql_network_io_waits_ms + type: gauge + help: 'Network IO wait time metric from SQL Server wait statistics.' + values: [network_io_waits_ms] + query_ref: mssql_wait_stats_averages + + - metric_name: mssql_mirrored_write_transactions + type: counter + help: 'Mirrored write transactions since SQL Server startup.' + values: [mirrored_write_transactions] + query_ref: mssql_replica_counters + + - metric_name: mssql_bytes_sent_to_replica + type: counter + help: 'Bytes sent to replica since SQL Server startup.' + values: [bytes_sent_to_replica] + query_ref: mssql_replica_counters + + - metric_name: mssql_database_info + type: gauge + help: 'Info metric with database state and AG status. Value is always 1.' + key_labels: + - database_name + - state + - state_desc + - is_ha_instance + - is_in_ag + values: [value] + query_ref: mssql_database_info_query + + - metric_name: mssql_ag_database_out_of_sync + type: gauge + help: 'Indicates if an AG database is out of sync. Metric only appears if a problem exists.' + values: [value] + key_labels: + - AR_REPLICA_SERVER_NAME + - DATABASE_NAME + - SYNC_STATE_DESC + - DB_STATE_DESC + query_ref: mssql_ag_database_out_of_sync_query + + - metric_name: mssql_last_logsweep + type: gauge + help: 'Shows databases with log backup overdue by more than 4 hours.' + key_labels: + - OVERDUE_DATABASES + - LAST_LOGSWEEP_DATES + values: [max_minutes_overdue] + query_ref: mssql_last_logsweep_query + + - metric_name: mssql_max_server_memory_mb + type: gauge + help: 'Configured max server memory in MB.' + values: [max_server_memory_mb] + query: | + SELECT CAST(value_in_use AS bigint) AS max_server_memory_mb + FROM sys.configurations + WHERE name = 'max server memory (MB)' + + - metric_name: mssql_memory_consumers + type: gauge + help: 'Requests currently consuming more than 500 MB of granted query memory.' + values: [memory_consumers] + query: | + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + DECLARE @granted_memory_threshold_mb int = 500; + SELECT COUNT(*) AS memory_consumers + FROM sys.dm_exec_requests + WHERE (granted_query_memory * 8) > (@granted_memory_threshold_mb * 1024); + + - metric_name: mssql_active_requests_count + type: gauge + help: 'Count of active, blocking, or open-transaction sessions based on DBA usp_active_requests_count logic.' + values: [active_requests_count] + query_ref: mssql_active_requests_count + + - metric_name: mssql_db__log_reuse_wait + type: gauge + help: 'sys.databases.log_reuse_wait value.' + key_labels: + - db_name + - log_reuse_wait_desc + values: [log_reuse_wait] + query: + SELECT name AS db_name, log_reuse_wait, log_reuse_wait_desc from sys.databases + + - metric_name: mssql_memory_clerks__size_mb + type: gauge + help: 'Memory clerk size in MB information.' + key_labels: + - memory_clerk_type + - memory_clerk_name + values: [size_mb] + query: | + SELECT [memory_clerk_type] = [type], + [memory_clerk_name] = MAX(name), + [size_mb] = SUM(pages_kb) / 1024 + FROM sys.dm_os_memory_clerks WITH (NOLOCK) + GROUP BY [type] + HAVING (SUM(pages_kb) / 1024) > 0 + ORDER BY SUM(pages_kb) DESC; + + - metric_name: mssql_logsaver__collection_time + type: gauge + help: 'Log saver collection time as Unix epoch seconds per database.' + key_labels: + - database_name + values: [collection_time] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__thresholds_violated + type: gauge + help: 'Whether configured log saver thresholds are violated for the database.' + key_labels: + - database_name + - recovery_model + - log_reuse_wait_desc + - threshold_condition + values: [thresholds_violated] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__transaction_start_time + type: gauge + help: 'Oldest open transaction start time as Unix epoch seconds per database.' + key_labels: + - database_name + - spid + - login_name + - program_name + - host_name + - host_process_id + - command + - sql_text + values: [transaction_start_time] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__log_size_mb + type: gauge + help: 'Transaction log size in MB per database.' + key_labels: + - database_name + values: [log_size_mb] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__log_used_mb + type: gauge + help: 'Transaction log used space in MB per database.' + key_labels: + - database_name + values: [log_used_mb] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__exists_valid_autogrowing_file + type: gauge + help: 'Whether a valid auto-growing log file exists for the database.' + key_labels: + - database_name + values: [exists_valid_autogrowing_file] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__log_used_pct + type: gauge + help: 'Transaction log used percentage per database.' + key_labels: + - database_name + values: [log_used_pct] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__log_used_pct_threshold + type: gauge + help: 'Configured transaction log used percentage threshold per database.' + key_labels: + - database_name + values: [log_used_pct_threshold] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__log_used_gb_threshold + type: gauge + help: 'Configured transaction log used GB threshold per database.' + key_labels: + - database_name + values: [log_used_gb_threshold] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__is_pct_threshold_valid + type: gauge + help: 'Whether the log used percentage threshold is valid for alerting.' + key_labels: + - database_name + values: [is_pct_threshold_valid] + query_ref: mssql_logsaver + + - metric_name: mssql_logsaver__is_gb_threshold_valid + type: gauge + help: 'Whether the log used GB threshold is valid for alerting.' + key_labels: + - database_name + values: [is_gb_threshold_valid] + query_ref: mssql_logsaver + +queries: + - query_name: mssql_sys_memory + query: | + SELECT + total_physical_memory_kb * 1024 AS total, + available_physical_memory_kb * 1024 AS available + FROM sys.dm_os_sys_memory + + - query_name: mssql_process_memory + query: | + SELECT + physical_memory_in_use_kb * 1024 AS in_use, + locked_page_allocations_kb * 1024 AS locked_pages, + large_page_allocations_kb * 1024 AS large_pages + FROM sys.dm_os_process_memory + + - query_name: mssql_cpu_utilization + query: | + WITH rb AS ( + SELECT TOP (1) + 100 - record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS system, + record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS sqlserver, + DATEADD(ms, -1 * (ts_now - [timestamp]), SYSDATETIME()) AS sample_time + FROM ( + SELECT + CAST(record AS xml) AS record, + [timestamp], + cpu_ticks / (cpu_ticks / ms_ticks) AS ts_now + FROM sys.dm_os_ring_buffers + CROSS APPLY sys.dm_os_sys_info + WHERE ring_buffer_type = 'RING_BUFFER_SCHEDULER_MONITOR' + AND record LIKE '%%' + ) s + ORDER BY sample_time DESC + ) + SELECT system, sqlserver + FROM rb + + - query_name: mssql_memory_manager + query: | + SELECT + MAX(CASE WHEN counter_name = 'Granted Workspace Memory (KB)' THEN cntr_value * 1024 END) AS granted_workspace, + MAX(CASE WHEN counter_name = 'Maximum Workspace Memory (KB)' THEN cntr_value * 1024 END) AS maximum_workspace, + MAX(CASE WHEN counter_name = 'Memory Grants Pending' THEN cntr_value END) AS memory_grants_pending, + MAX(CASE WHEN counter_name = 'Memory Grants Outstanding' THEN cntr_value END) AS memory_grants_outstanding, + MAX(CASE WHEN counter_name = 'Stolen Server Memory (KB)' THEN cntr_value * 1024 END) AS stolen_server, + MAX(CASE WHEN counter_name = 'Total Server Memory (KB)' THEN cntr_value * 1024 END) AS total_server, + MAX(CASE WHEN counter_name = 'Target Server Memory (KB)' THEN cntr_value * 1024 END) AS target_server, + MAX(CASE WHEN counter_name = 'SQL Cache Memory (KB)' THEN cntr_value * 1024 END) AS sql_cache, + MAX(CASE WHEN counter_name = 'Free Memory (KB)' THEN cntr_value * 1024 END) AS free + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Memory Manager%' + AND counter_name IN ( + 'Granted Workspace Memory (KB)', + 'Maximum Workspace Memory (KB)', + 'Memory Grants Pending', + 'Memory Grants Outstanding', + 'Stolen Server Memory (KB)', + 'Total Server Memory (KB)', + 'Target Server Memory (KB)', + 'SQL Cache Memory (KB)', + 'Free Memory (KB)' + ) + + - query_name: mssql_database_space + query: | + SELECT + RTRIM(instance_name) AS db, + MAX(CASE WHEN counter_name = 'Data File(s) Size (KB)' THEN cntr_value * 1024 END) AS data, + MAX(CASE WHEN counter_name = 'Log File(s) Size (KB)' THEN cntr_value * 1024 END) AS log, + MAX(CASE WHEN counter_name = 'Log File(s) Used Size (KB)' THEN cntr_value * 1024 END) AS log_used, + MAX(CASE WHEN counter_name = 'Percent Log Used' THEN cntr_value END) AS percent_log_used, + MAX(CASE WHEN counter_name = 'Active Transactions' THEN cntr_value END) AS active_transactions, + MAX(CASE WHEN counter_name = 'XTP Memory Used (KB)' THEN cntr_value * 1024 END) AS xtp_memory_used, + MAX(CASE WHEN counter_name = 'Log Growths' THEN cntr_value END) AS growths, + MAX(CASE WHEN counter_name = 'Log Shrinks' THEN cntr_value END) AS shrinks, + MAX(CASE WHEN counter_name = 'Log Truncations' THEN cntr_value END) AS truncations + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Databases%' + AND instance_name NOT IN ('_Total', 'mssqlsystemresource') + AND counter_name IN ( + 'Data File(s) Size (KB)', + 'Log File(s) Size (KB)', + 'Log File(s) Used Size (KB)', + 'Percent Log Used', + 'Active Transactions', + 'XTP Memory Used (KB)', + 'Log Growths', + 'Log Shrinks', + 'Log Truncations' + ) + GROUP BY RTRIM(instance_name) + + - query_name: mssql_active_cursors + query: | + SELECT + RTRIM(instance_name) AS cursor_type, + cntr_value AS active_cursors + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Cursor Manager by Type%' + AND counter_name = 'Active cursors' + AND instance_name <> '_Total' + + - query_name: mssql_general_statistics_snapshot + query: | + SELECT + MAX(CASE WHEN counter_name = 'User Connections' THEN cntr_value END) AS user_connections, + MAX(CASE WHEN counter_name = 'Temp Tables For Destruction' THEN cntr_value END) AS temp_tables_for_destruction, + MAX(CASE WHEN counter_name = 'Processes blocked' THEN cntr_value END) AS processes_blocked, + MAX(CASE WHEN counter_name = 'Active Temp Tables' THEN cntr_value END) AS tempdb_active_temp_tables + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:General Statistics%' + AND counter_name IN ('User Connections', 'Temp Tables For Destruction', 'Processes blocked', 'Active Temp Tables') + + - query_name: mssql_wait_stats + query: | + SELECT + RTRIM(counter_name) AS wait_type, + cntr_value AS waits_in_progress + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Wait Statistics%' + AND instance_name = 'Waits in progress' + + - query_name: mssql_transactions + query: | + SELECT + RTRIM(instance_name) AS db, + cntr_value AS transactions + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Transactions%' + AND counter_name = 'Transactions' + AND instance_name NOT IN ('', '_Total') + + - query_name: mssql_tempdb_transactions + query: | + SELECT + MAX(CASE WHEN counter_name = 'Free Space in tempdb (KB)' THEN cntr_value * 1024 END) AS free_space, + MAX(CASE WHEN counter_name = 'Version Store Size (KB)' THEN cntr_value * 1024 END) AS version_store, + MAX(CASE WHEN counter_name = 'Longest Transaction Running Time' THEN cntr_value END) AS longest_transaction_running_time + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Transactions%' + AND counter_name IN ( + 'Free Space in tempdb (KB)', + 'Version Store Size (KB)', + 'Longest Transaction Running Time' + ) + + - query_name: mssql_access_methods_ratio + query: | + WITH ratio AS ( + SELECT TOP (1) CAST(cntr_value AS float) AS numerator + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Access Methods%' + AND counter_name = 'Worktables From Cache Ratio' + ), + base AS ( + SELECT TOP (1) CAST(cntr_value AS float) AS denominator + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Access Methods%' + AND counter_name = 'Worktables From Cache Ratio Base' + ) + SELECT + CASE WHEN base.denominator = 0 THEN NULL ELSE 100.0 * ratio.numerator / base.denominator END AS worktables_from_cache_ratio + FROM ratio + CROSS JOIN base + + - query_name: mssql_resource_pool_cpu + query: | + SELECT + RTRIM(fr.instance_name) AS resource_pool, + CASE WHEN bs.cntr_value = 0 THEN NULL ELSE 100.0 * fr.cntr_value / bs.cntr_value END AS cpu_usage_percentage + FROM sys.dm_os_performance_counters fr WITH (NOLOCK) + JOIN sys.dm_os_performance_counters bs WITH (NOLOCK) + ON bs.object_name = fr.object_name + AND ISNULL(bs.instance_name, '') = ISNULL(fr.instance_name, '') + AND bs.counter_name = 'CPU usage % base' + WHERE fr.object_name LIKE '%:Resource Pool Stats%' + AND fr.counter_name = 'CPU usage %' + + - query_name: mssql_workload_group_cpu + query: | + SELECT + RTRIM(fr.instance_name) AS workload_group, + CASE WHEN bs.cntr_value = 0 THEN NULL ELSE 100.0 * fr.cntr_value / bs.cntr_value END AS cpu_usage_percentage + FROM sys.dm_os_performance_counters fr WITH (NOLOCK) + JOIN sys.dm_os_performance_counters bs WITH (NOLOCK) + ON bs.object_name = fr.object_name + AND ISNULL(bs.instance_name, '') = ISNULL(fr.instance_name, '') + AND bs.counter_name = 'CPU usage % base' + WHERE fr.object_name LIKE '%:Workload Group Stats%' + AND fr.counter_name = 'CPU usage %' + + - query_name: mssql_sql_statistics + query: | + SELECT + MAX(CASE WHEN counter_name = 'SQL Attention rate' THEN cntr_value END) AS sql_attentions, + MAX(CASE WHEN counter_name = 'Auto-Param Attempts/sec' THEN cntr_value END) AS attempts, + MAX(CASE WHEN counter_name = 'Failed Auto-Params/sec' THEN cntr_value END) AS failed, + MAX(CASE WHEN counter_name = 'Safe Auto-Params/sec' THEN cntr_value END) AS safe, + MAX(CASE WHEN counter_name = 'Unsafe Auto-Params/sec' THEN cntr_value END) AS unsafe, + MAX(CASE WHEN counter_name = 'SQL Compilations/sec' THEN cntr_value END) AS sql_compilations, + MAX(CASE WHEN counter_name = 'SQL Re-Compilations/sec' THEN cntr_value END) AS sql_recompilations + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:SQL Statistics%' + AND counter_name IN ( + 'SQL Attention rate', + 'Auto-Param Attempts/sec', + 'Failed Auto-Params/sec', + 'Safe Auto-Params/sec', + 'Unsafe Auto-Params/sec', + 'SQL Compilations/sec', + 'SQL Re-Compilations/sec' + ) + + - query_name: mssql_buffer_manager_counters + query: | + SELECT + MAX(CASE WHEN counter_name = 'Lazy writes/sec' THEN cntr_value END) AS lazy_writes, + MAX(CASE WHEN counter_name = 'Free list stalls/sec' THEN cntr_value END) AS free_list_stalls, + MAX(CASE WHEN counter_name = 'Readahead pages/sec' THEN cntr_value END) AS readahead_pages, + MAX(CASE WHEN counter_name = 'Page reads/sec' THEN cntr_value END) AS page_reads, + MAX(CASE WHEN counter_name = 'Page lookups/sec' THEN cntr_value END) AS page_lookups, + MAX(CASE WHEN counter_name = 'Page writes/sec' THEN cntr_value END) AS page_writes, + MAX(CASE WHEN counter_name = 'Database pages' THEN cntr_value END) AS buffer_database_pages, + MAX(CASE WHEN counter_name = 'Target pages' THEN cntr_value END) AS buffer_target_pages + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Buffer Manager%' + AND counter_name IN ( + 'Lazy writes/sec', + 'Free list stalls/sec', + 'Readahead pages/sec', + 'Page reads/sec', + 'Page lookups/sec', + 'Page writes/sec', + 'Database pages', + 'Target pages' + ) + + - query_name: mssql_sys_info_memory + query: | + SELECT + committed_kb * 1024.0 AS buffer_pool_committed, + committed_target_kb * 1024.0 AS buffer_pool_committed_target + FROM sys.dm_os_sys_info + + - query_name: mssql_active_transactions_count + query: | + SELECT COUNT(*) AS active_transactions_count + FROM sys.dm_tran_active_transactions + + - query_name: mssql_latch_wait_stats + query: | + SELECT TOP (1) + cntr_value AS average_latch_wait_time_ms + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Latches%' + AND counter_name = 'Average Latch Wait Time (ms)' + + - query_name: mssql_general_statistics_counters + query: | + SELECT + MAX(CASE WHEN counter_name = 'Connection Reset/sec' THEN cntr_value END) AS connection_reset, + MAX(CASE WHEN counter_name = 'Logins/sec' THEN cntr_value END) AS logins, + MAX(CASE WHEN counter_name = 'Logouts/sec' THEN cntr_value END) AS logouts + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:General Statistics%' + AND counter_name IN ('Connection Reset/sec', 'Logins/sec', 'Logouts/sec') + + - query_name: mssql_replica_counters + query: | + SELECT + MAX(CASE WHEN counter_name = 'Write Transactions/sec' THEN cntr_value END) AS write_transactions, + MAX(CASE WHEN counter_name = 'Log Apply Pending Queue' THEN cntr_value END) AS log_apply_pending_queue, + MAX(CASE WHEN counter_name = 'Log remaining for undo' THEN cntr_value END) AS log_remaining_for_undo, + MAX(CASE WHEN counter_name = 'Log Send Queue' THEN cntr_value END) AS log_send_queue, + MAX(CASE WHEN counter_name = 'Redo blocked/sec' THEN cntr_value END) AS redo_blocked, + MAX(CASE WHEN counter_name = 'Transaction Delay' THEN cntr_value END) AS transaction_delay, + MAX(CASE WHEN counter_name = 'Mirrored Write Transactions/sec' THEN cntr_value END) AS mirrored_write_transactions, + MAX(CASE WHEN counter_name = 'Bytes Sent to Replica/sec' THEN cntr_value END) AS bytes_sent_to_replica + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Database Replica%' + AND instance_name = '_Total' + AND counter_name IN ( + 'Write Transactions/sec', + 'Log Apply Pending Queue', + 'Log remaining for undo', + 'Log Send Queue', + 'Redo blocked/sec', + 'Transaction Delay', + 'Mirrored Write Transactions/sec', + 'Bytes Sent to Replica/sec' + ) + + - query_name: mssql_wait_stats_averages + query: | + SELECT + MAX(CASE WHEN counter_name = 'Page IO latch waits' THEN cntr_value END) AS page_io_latch_waits_ms, + MAX(CASE WHEN counter_name = 'Network IO waits' THEN cntr_value END) AS network_io_waits_ms + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Wait Statistics%' + AND instance_name = 'Average wait time (ms)' + AND counter_name IN ('Page IO latch waits', 'Network IO waits') + + - query_name: mssql_database_info_query + query: | + DECLARE @is_ha_instance BIT = CASE WHEN EXISTS (SELECT 1 FROM sys.availability_groups) THEN 1 ELSE 0 END; + SELECT + d.name AS database_name, + d.state, + d.state_desc, + @is_ha_instance AS is_ha_instance, + CASE WHEN ag_db.replica_id IS NOT NULL THEN 1 ELSE 0 END AS is_in_ag, + 1 AS value + FROM sys.databases d + LEFT JOIN sys.dm_hadr_database_replica_states ag_db + ON is_local = 1 AND d.database_id = ag_db.database_id + WHERE d.is_distributor <> 1; + + - query_name: mssql_ag_database_out_of_sync_query + query: | + SELECT DISTINCT + ar.replica_server_name AS AR_REPLICA_SERVER_NAME, + drcs.database_name AS DATABASE_NAME, + drs.synchronization_state_desc AS SYNC_STATE_DESC, + COALESCE(drs.database_state_desc, '') AS DB_STATE_DESC, + 1 AS value + FROM sys.availability_replicas ar + JOIN sys.dm_hadr_database_replica_states drs + ON ar.replica_id = drs.replica_id + JOIN sys.dm_hadr_database_replica_cluster_states drcs + ON drs.group_database_id = drcs.group_database_id + WHERE drs.synchronization_state_desc <> 'SYNCHRONIZED'; + + - query_name: mssql_last_logsweep_query + query: | + SELECT + COALESCE(STRING_AGG(sub.DATABASE_NAME, ', ') WITHIN GROUP (ORDER BY sub.DATABASE_NAME), '') AS OVERDUE_DATABASES, + COALESCE(STRING_AGG(sub.LAST_LOGSWEEP_DATE, ', ') WITHIN GROUP (ORDER BY sub.DATABASE_NAME), '') AS LAST_LOGSWEEP_DATES, + COALESCE(MAX(sub.minutes_since_last_logbackup), 0) AS max_minutes_overdue + FROM ( + SELECT + d.name AS DATABASE_NAME, + COALESCE(FORMAT(MAX(bs.backup_finish_date), 'yyyy-MM-dd HH:mm:ss'), 'Never Backed Up') AS LAST_LOGSWEEP_DATE, + COALESCE(DATEDIFF(MINUTE, MAX(bs.backup_finish_date), GETDATE()), -1) AS minutes_since_last_logbackup + FROM sys.databases d + LEFT JOIN msdb.dbo.backupset bs + ON d.name = bs.database_name + AND bs.type = 'L' + AND bs.backup_finish_date IS NOT NULL + WHERE d.database_id > 4 + AND d.state_desc = 'ONLINE' + GROUP BY d.name + ) AS sub + WHERE sub.minutes_since_last_logbackup > 240 + HAVING STRING_AGG(sub.DATABASE_NAME, ', ') IS NOT NULL + + - query_name: mssql_lock_counters + query: | + SELECT + MAX(CASE WHEN counter_name = 'Lock Wait Time (ms)' THEN cntr_value END) AS lock_wait_time_ms, + MAX(CASE WHEN counter_name = 'Lock Waits/sec' THEN cntr_value END) AS lock_waits + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Locks%' + AND instance_name = '_Total' + AND counter_name IN ('Lock Wait Time (ms)', 'Lock Waits/sec') + + - query_name: mssql_access_methods_counters + query: | + SELECT + MAX(CASE WHEN counter_name = 'Full Scans/sec' THEN cntr_value END) AS full_scans, + MAX(CASE WHEN counter_name = 'Forwarded Records/sec' THEN cntr_value END) AS forwarded_records, + MAX(CASE WHEN counter_name = 'Index Searches/sec' THEN cntr_value END) AS index_searches, + MAX(CASE WHEN counter_name = 'Page Splits/sec' THEN cntr_value END) AS page_splits, + MAX(CASE WHEN counter_name = 'Workfiles Created/sec' THEN cntr_value END) AS workfiles_created, + MAX(CASE WHEN counter_name = 'Worktables Created/sec' THEN cntr_value END) AS worktables_created, + MAX(CASE WHEN counter_name = 'Table Lock Escalations/sec' THEN cntr_value END) AS table_lock_escalations + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Access Methods%' + AND counter_name IN ( + 'Full Scans/sec', + 'Forwarded Records/sec', + 'Index Searches/sec', + 'Page Splits/sec', + 'Workfiles Created/sec', + 'Worktables Created/sec', + 'Table Lock Escalations/sec' + ) + + - query_name: mssql_database_log_counters + query: | + SELECT + RTRIM(instance_name) AS db, + MAX(CASE WHEN counter_name = 'Log Bytes Flushed/sec' THEN cntr_value END) AS log_bytes_flushed, + MAX(CASE WHEN counter_name = 'Log Flush Wait Time' THEN cntr_value END) AS log_flush_wait_time_ms, + MAX(CASE WHEN counter_name = 'Log Flush Waits/sec' THEN cntr_value END) AS log_flush_waits, + MAX(CASE WHEN counter_name = 'Log Flushes/sec' THEN cntr_value END) AS log_flushes + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:Databases%' + AND instance_name NOT IN ('_Total', 'mssqlsystemresource') + AND counter_name IN ( + 'Log Bytes Flushed/sec', + 'Log Flush Wait Time', + 'Log Flush Waits/sec', + 'Log Flushes/sec' + ) + GROUP BY RTRIM(instance_name) + + - query_name: mssql_sql_errors + query: | + SELECT + RTRIM(instance_name) AS error_type, + cntr_value AS errors + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE object_name LIKE '%:SQL Errors%' + AND counter_name = 'Errors/sec' + + - query_name: mssql_active_requests_count + query: | + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; + SELECT COUNT(*) AS active_requests_count + FROM sys.dm_exec_sessions AS s + LEFT JOIN sys.dm_exec_requests AS r + ON r.session_id = s.session_id + WHERE s.session_id <> @@SPID + AND ( + s.session_id IN ( + SELECT ri.blocking_session_id + FROM sys.dm_exec_requests AS ri + WHERE ri.blocking_session_id IS NOT NULL + AND ri.blocking_session_id <> 0 + ) + OR (r.blocking_session_id IS NOT NULL AND r.blocking_session_id <> 0) + OR (s.session_id > 50 AND r.session_id IS NOT NULL AND s.status <> 'sleeping') + OR (s.session_id > 50 AND s.open_transaction_count <> 0) + ) + + - query_name: mssql_logsaver + query: | + SET NOCOUNT ON + SET XACT_ABORT ON + SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED + + DECLARE + @log_used_pct_threshold tinyint = 80, + @log_used_gb_threshold int = 100, + @threshold_condition varchar(5) = 'or', /* and | or */ + @databases varchar(max) = NULL, /* Comma separated list of databases. -ve (negative) if database has to be excluded */ + @skip_autogrowth_validation bit = 0, + @kill_spids bit = 0, + @verbose tinyint = 0 /* 1 => messages, 2 => messages + table results */ + + DECLARE @_start_time datetime2; + SET @_start_time = SYSDATETIME(); + + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Declaring local variables..'; + + DECLARE @_spid int + DECLARE @_sql varchar(max) + DECLARE @_params nvarchar(max) + DECLARE @_email_body varchar(max) = '' + DECLARE @_email_subject nvarchar(255) + DECLARE @_log_used_pct decimal(38,2) + DECLARE @_error varchar(8000) + DECLARE @_is_pct_threshold_valid bit = 0; + DECLARE @_is_gb_threshold_valid bit = 0; + DECLARE @_thresholds_violated bit = 0; + DECLARE @_exists_valid_autogrowing_file bit = 0; + DECLARE @_transaction_start_time datetime; + + DECLARE @_tab nchar(1) = CHAR(9); + + declare @c_database_name sysname; + declare @c_recovery_model varchar(50); + declare @c_log_reuse_wait_desc varchar(125); + declare @c_log_size_mb numeric(12,2); + declare @c_log_used_pct numeric(6,2); + + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Creating #temp tables..'; + + IF OBJECT_ID('tempdb..#logspace') IS NOT NULL + DROP TABLE #logspace; + CREATE TABLE #logspace + ( [database_name] sysname, + log_size_mb decimal(38,2), + log_used_pct decimal(38,2), + log_status int + ); + + IF OBJECT_ID('tempdb..#dbcc_opentran') IS NOT NULL + DROP TABLE #dbcc_opentran; + CREATE TABLE #dbcc_opentran + ( [database_name] sysname NULL, + transaction_property varchar(100) NULL, + transaction_property_value varchar(1000) NULL + ); + + IF OBJECT_ID('tempdb..#log_space_consumers') IS NOT NULL + DROP TABLE #log_space_consumers; + CREATE TABLE #log_space_consumers + ( + [collection_time] datetime2 not null + ,[database_name] sysname not null + ,[recovery_model] varchar(20) not null + ,[log_reuse_wait_desc] varchar(125) not null + ,[log_size_mb] decimal(20, 2) not null + ,[log_used_mb] decimal(20, 2) not null + ,[exists_valid_autogrowing_file] bit null + ,[log_used_pct] decimal(10, 2) default 0.0 not null + ,[thresholds_violated] bit default 0 not null + ,[threshold_condition] varchar(5) not null + ,[log_used_pct_threshold] decimal(10,2) not null + ,[log_used_gb_threshold] decimal(20,2) null + ,[is_pct_threshold_valid] bit default 0 not null + ,[is_gb_threshold_valid] bit default 0 not null + ,[spid] int null + ,[transaction_start_time] datetime null + ,[login_name] sysname null + ,[program_name] sysname null + ,[host_name] sysname null + ,[host_process_id] int null + ,[command] varchar(16) null + ,[sql_text] varchar(max) null + ); + + -- Create Temporary Tables + IF OBJECT_ID('tempdb..#db_list_from_params') IS NOT NULL + DROP TABLE #db_list_from_params; + CREATE TABLE #db_list_from_params ([database_name] sysname, [skip_db] bit); + + IF OBJECT_ID('tempdb..#db_list') IS NOT NULL + DROP TABLE #db_list; + CREATE TABLE #db_list ([database_name] sysname, [recovery_model] varchar(50), [log_reuse_wait_desc] varchar(125)); + + IF @databases IS NOT NULL + BEGIN + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Extracting database names from ('+@databases+') parameter value..'; + ;WITH t1([database_name], [databases]) AS + ( + SELECT CAST(LEFT(@databases, CHARINDEX(',',@databases+',')-1) AS VARCHAR(500)) as [database_name], + STUFF(@databases, 1, CHARINDEX(',',@databases+','), '') as [databases] + -- + UNION ALL + -- + SELECT CAST(LEFT([databases], CHARINDEX(',',[databases]+',')-1) AS VARChAR(500)) AS [database_name], + STUFF([databases], 1, CHARINDEX(',',[databases]+','), '') as [databases] + FROM t1 + WHERE [databases] > '' + ) + INSERT #db_list_from_params ([database_name], [skip_db]) + SELECT [database_name] = case when left(ltrim(rtrim([database_name])),1) = '-' then RIGHT(ltrim(rtrim([database_name])),len(ltrim(rtrim([database_name])))-1) else ltrim(rtrim([database_name])) end, + [skip_db] = case when left(ltrim(rtrim([database_name])),1) = '-' then 1 else 0 end + FROM t1 + OPTION (MAXRECURSION 32000); + + IF @verbose >= 2 + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') select * from #db_list_from_params..' + select running_query, t.* + from #db_list_from_params t + full outer join (values ('#db_list_from_params') )dummy(running_query) on 1 = 1 + END + END + + IF @databases IS NOT NULL + BEGIN + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Extracting database names from ('+@databases+') parameter value..'; + + IF EXISTS (SELECT 1/0 FROM #db_list_from_params p WHERE p.skip_db = 0) + BEGIN + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Databases parameter has databases for Inclusion logic. Working on same..'; + + INSERT #db_list ([database_name], [recovery_model], [log_reuse_wait_desc]) + SELECT d.name, d.recovery_model_desc, d.[log_reuse_wait_desc] + FROM sys.databases d + INNER JOIN #db_list_from_params pl + ON pl.database_name = d.name + WHERE 1=1 + AND d.state_desc = 'ONLINE' + AND pl.skip_db = 0; + END + ELSE + BEGIN + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Databases parameter has databases for Exclusion logic only. Working on same..'; + + INSERT #db_list ([database_name], [recovery_model], [log_reuse_wait_desc]) + SELECT d.name, d.recovery_model_desc, d.[log_reuse_wait_desc] + FROM sys.databases d + WHERE 1=1 + AND d.state_desc = 'ONLINE' + AND d.name NOT IN (SELECT pl.database_name FROM #db_list_from_params pl WHERE pl.skip_db = 1); + END + END + ELSE + BEGIN + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Databases parameter not provided. Working on #db_list..'; + + INSERT #db_list ([database_name], [recovery_model], [log_reuse_wait_desc]) + SELECT d.name, d.recovery_model_desc, d.[log_reuse_wait_desc] + FROM sys.databases d + WHERE 1=1 + AND d.state_desc = 'ONLINE'; + END + + IF @verbose >= 2 + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') select * from #db_list..' + select running_query, t.* + from #db_list t + full outer join (values ('#db_list') )dummy(running_query) on 1 = 1 + END + + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Populate #logspace using SQLPERF(LOGSPACE)..' + INSERT INTO #logspace ([database_name], log_size_mb, log_used_pct, log_status) + EXEC ('DBCC SQLPERF(LOGSPACE) WITH NO_INFOMSGS'); + + IF @verbose >= 2 + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') select * from #logspace join #db_list..' + select running_query, t.*, dl.* + from #logspace t + join #db_list dl on dl.database_name = t.database_name + full outer join (values ('#logspace + #db_list') )dummy(running_query) on 1 = 1 + END + + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Start a cursor, and loop through each database..' + + DECLARE cur_databases CURSOR LOCAL FORWARD_ONLY FOR + SELECT dl.database_name, dl.recovery_model, dl.log_reuse_wait_desc, + ls.log_size_mb, ls.log_used_pct + FROM #db_list dl + JOIN #logspace ls + ON ls.database_name = dl.database_name; + + OPEN cur_databases; + FETCH NEXT FROM cur_databases INTO @c_database_name, @c_recovery_model, @c_log_reuse_wait_desc, @c_log_size_mb, @c_log_used_pct; + + WHILE @@FETCH_STATUS = 0 + BEGIN + SET @_is_pct_threshold_valid = 0; + SET @_is_gb_threshold_valid = 0; + SET @_thresholds_violated = 0; + SET @_exists_valid_autogrowing_file = 0; + SET @_spid = NULL; + SET @_transaction_start_time = NULL; + TRUNCATE TABLE #dbcc_opentran; + + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Working on database ['+@c_database_name+']..'; + + IF @verbose >= 1 + PRINT @_tab+@_tab+'Validate if a valid auto-growing log file exists..' + IF EXISTS ( SELECT * FROM sys.master_files mf + WHERE mf.database_id = DB_ID(@c_database_name) AND mf.type_desc = 'LOG' + AND mf.growth > 0 AND mf.max_size <> 0 -- auto growth is enabled + AND ( mf.max_size = -1 + OR ((mf.max_size*8.0/1024)*@log_used_pct_threshold/100.0) > @c_log_size_mb -- @log_used_pct_threshold of max size is not reached + ) + ) + BEGIN + SET @_exists_valid_autogrowing_file = 1; + END + + -- Validate if % thresholds are crossed + IF ( @log_used_pct_threshold IS NOT NULL + AND @c_log_used_pct >= @log_used_pct_threshold + AND ( @_exists_valid_autogrowing_file = 0 + OR @skip_autogrowth_validation = 1 + ) + ) + BEGIN + SET @_is_pct_threshold_valid = 1; + END + + IF (@log_used_gb_threshold IS NOT NULL) AND ((@c_log_size_mb * @c_log_used_pct / 100.0) > (@log_used_gb_threshold*1024.0)) + BEGIN + SET @_is_gb_threshold_valid = 1; + END + + SET @_thresholds_violated = (CASE WHEN @threshold_condition = 'and' and (@_is_pct_threshold_valid = 1 and @_is_gb_threshold_valid = 1) THEN 1 + WHEN @threshold_condition = 'or' and (@_is_pct_threshold_valid = 1 OR @_is_gb_threshold_valid = 1) THEN 1 + ELSE 0 + END); + + IF (@_thresholds_violated = 1) AND (@verbose >= 1) + PRINT @_tab+@_tab+'Threshold broken for database ['+@c_database_name+']..'; + + IF @verbose >= 1 + BEGIN + PRINT @_tab+@_tab+'@c_log_reuse_wait_desc = '+ @c_log_reuse_wait_desc; + PRINT @_tab+@_tab+'@c_log_size_mb = '+ convert(varchar,@c_log_size_mb); + PRINT @_tab+@_tab+'@c_log_used_pct = '+ convert(varchar,@c_log_used_pct); + PRINT @_tab+@_tab+'@_exists_valid_autogrowing_file = '+ convert(varchar,@_exists_valid_autogrowing_file); + PRINT @_tab+@_tab+'@_is_pct_threshold_valid = '+ convert(varchar,@_is_pct_threshold_valid); + PRINT @_tab+@_tab+'@_is_gb_threshold_valid = '+ convert(varchar,@_is_gb_threshold_valid); + --PRINT @_tab+@_tab+'@_thresholds_violated = '+ convert(varchar,@_thresholds_violated); + END + + -- If Thresholds are met, then find out oldest transaction details + IF @_thresholds_violated = 1 + BEGIN + IF @verbose >= 1 + PRINT @_tab+@_tab+'Find longest open transaction using DBCC OPENTRAN..'; + SET @_sql = 'DBCC OPENTRAN(''' + @c_database_name + ''') WITH TABLERESULTS, NO_INFOMSGS;'; + INSERT INTO #dbcc_opentran (transaction_property, transaction_property_value) + EXEC (@_sql); + + IF @verbose >= 2 + BEGIN + PRINT @_tab+@_tab+' select * from #dbcc_opentran..' + select running_query, [@c_database_name] = @c_database_name, t.* + from #dbcc_opentran t + full outer join (values ('#dbcc_opentran') )dummy(running_query) on 1 = 1 + END + + SELECT @_spid = CASE WHEN transaction_property = 'OLDACT_SPID' THEN transaction_property_value ELSE @_spid END, + @_transaction_start_time = CASE WHEN transaction_property = 'OLDACT_STARTTIME' THEN convert(datetime,transaction_property_value) ELSE @_transaction_start_time END + FROM #dbcc_opentran + WHERE transaction_property IN ('OLDACT_SPID','OLDACT_STARTTIME'); + END + + IF @verbose >= 1 + PRINT @_tab+@_tab+'Populate #log_space_consumers with log usage + transaction details..'; + + INSERT INTO #log_space_consumers + ( [collection_time], [database_name], [recovery_model], [log_reuse_wait_desc], [log_size_mb], [log_used_mb], + [exists_valid_autogrowing_file], [log_used_pct], [log_used_pct_threshold], [log_used_gb_threshold], [spid], + [transaction_start_time], [login_name], [program_name], [host_name], [host_process_id], [command], [sql_text], + [is_pct_threshold_valid], [is_gb_threshold_valid], [threshold_condition], + [thresholds_violated] + ) + SELECT [collection_time] = @_start_time, + [database_name] = @c_database_name, + [recovery_model] = @c_recovery_model, + [log_reuse_wait_desc] = @c_log_reuse_wait_desc, + [log_size_mb] = @c_log_size_mb, + [log_used_mb] = convert(numeric(20,2), @c_log_size_mb*@c_log_used_pct/100.0), + [exists_valid_autogrowing_file] = @_exists_valid_autogrowing_file, + [log_used_pct] = @c_log_used_pct, + [log_used_pct_threshold] = @log_used_pct_threshold, + [log_used_gb_threshold] = @log_used_gb_threshold, + [spid] = des.session_id, + [transaction_start_time] = @_transaction_start_time, + [login_name] = des.login_name, + [program_name] = CASE WHEN des.program_name like 'SQLAgent - TSQL JobStep %' + THEN ( select top 1 'SQL Job = '+j.name + from msdb.dbo.sysjobs (nolock) as j + inner join msdb.dbo.sysjobsteps (nolock) AS js on j.job_id=js.job_id + where right(cast(js.job_id as nvarchar(50)),10) = RIGHT(substring(des.program_name,30,34),10) + ) + ' ( '+SUBSTRING(LTRIM(RTRIM(des.program_name)), CHARINDEX(': Step ',LTRIM(RTRIM(des.program_name)))+2,LEN(LTRIM(RTRIM(des.program_name)))-CHARINDEX(': Step ',LTRIM(RTRIM(des.program_name)))-2)+' )' + ELSE des.program_name + END, + [host_name] = des.[host_name], + [host_process_id] = des.host_process_id, + [command] = der.command, + [sql_text] = est.text, + [is_pct_threshold_valid] = @_is_pct_threshold_valid, + [is_gb_threshold_valid] = @_is_gb_threshold_valid, + [threshold_condition] = @threshold_condition, + [thresholds_violated] = @_thresholds_violated + FROM #logspace t + INNER JOIN #db_list dl + ON dl.database_name = t.database_name + AND dl.database_name = @c_database_name + LEFT JOIN sys.dm_exec_sessions des + ON des.session_id = @_spid + AND dl.log_reuse_wait_desc IN ('ACTIVE_TRANSACTION') + LEFT JOIN sys.dm_exec_requests der + ON der.session_id = des.session_id + OUTER APPLY sys.dm_exec_sql_text(der.sql_handle) est + WHERE 1=1; + --AND status <> 'background' + --AND loginame NOT IN ('sa', 'NT AUTHORITY\SYSTEM'); + + -- If Thresholds are met, and ACTIVE_TRANSACTION is main issue, then take action if allowed. + IF (@_thresholds_violated = 1) AND (@_spid IS NOT NULL) AND (@c_log_reuse_wait_desc = 'ACTIVE_TRANSACTION') + BEGIN + IF ( (@_spid <> 0 AND @_spid >= 50) AND (@c_log_reuse_wait_desc = 'ACTIVE_TRANSACTION') ) + BEGIN + SET @_sql = 'KILL ' + CONVERT(varchar(30), @_spid) + ';' + IF @verbose >= 1 + PRINT @_tab+@_tab+(CASE WHEN @kill_spids = 1 THEN 'Execute: ' ELSE 'DryRun: ' END)+@_sql; + END + ELSE + BEGIN + IF @verbose >= 1 + PRINT @_tab+@_tab+'Log space is NOT caused by active transaction. So no action taken.'; + END + END + ELSE IF (@_thresholds_violated = 1) AND (@c_log_reuse_wait_desc <> 'ACTIVE_TRANSACTION') AND (@verbose >= 1) + PRINT @_tab+@_tab+'Log space is NOT caused by active transaction. So no action taken.'; + ELSE IF (@verbose >= 1) + PRINT @_tab+@_tab+'No action required for database ['+@c_database_name+']..'; + + FETCH NEXT FROM cur_databases INTO @c_database_name, @c_recovery_model, @c_log_reuse_wait_desc, @c_log_size_mb, @c_log_used_pct; + END + + IF @verbose >= 2 + BEGIN + PRINT '('+convert(varchar, getdate(), 21)+') select * from #log_space_consumers..' + select running_query, t.* + from #log_space_consumers t + full outer join (values ('#log_space_consumers') )dummy(running_query) on 1 = 1 + END + + IF EXISTS (SELECT 1/0 FROM #log_space_consumers) + BEGIN + IF @verbose >= 1 + PRINT '('+convert(varchar, getdate(), 21)+') Return resultset from #log_space_consumers..' + SELECT [collection_time] = DATEDIFF_BIG(SECOND, '1970-01-01T00:00:00', CAST([collection_time] AS datetime2)), + [database_name], + [recovery_model] = COALESCE([recovery_model], ''), + [log_reuse_wait_desc] = COALESCE([log_reuse_wait_desc], ''), + [log_size_mb], + [log_used_mb], + [exists_valid_autogrowing_file] = CONVERT(int, COALESCE([exists_valid_autogrowing_file], 0)), + [log_used_pct], + [thresholds_violated] = CONVERT(int, COALESCE([thresholds_violated], 0)), + [threshold_condition] = COALESCE([threshold_condition], ''), + [log_used_pct_threshold], + [log_used_gb_threshold] = COALESCE([log_used_gb_threshold], 0), + [is_pct_threshold_valid] = CONVERT(int, COALESCE([is_pct_threshold_valid], 0)), + [is_gb_threshold_valid] = CONVERT(int, COALESCE([is_gb_threshold_valid], 0)), + [spid] = COALESCE(CONVERT(varchar(30), [spid]), ''), + [transaction_start_time] = CASE WHEN [transaction_start_time] IS NULL THEN 0 ELSE DATEDIFF_BIG(SECOND, '1970-01-01T00:00:00', CAST([transaction_start_time] AS datetime2)) END, + [login_name] = COALESCE([login_name], ''), + [program_name] = COALESCE([program_name], ''), + [host_name] = COALESCE([host_name], ''), + [host_process_id] = COALESCE(CONVERT(varchar(30), [host_process_id]), ''), + [command] = COALESCE([command], ''), + [sql_text] = COALESCE(CONVERT(varchar(max), [sql_text]), '') + FROM #log_space_consumers; + END + + IF EXISTS (SELECT 1/0 FROM #log_space_consumers WHERE thresholds_violated = 1 and spid IS NOT NULL) + BEGIN + -- Get 1st spid to report about + select @c_database_name = lsc.database_name, @_spid = lsc.spid + from #log_space_consumers lsc + where thresholds_violated = 1 + and spid IS NOT NULL + order by spid + offset 0 rows fetch next 1 row only; + + WHILE @@ROWCOUNT <> 0 + BEGIN + SET @_email_subject = NULL; + SET @_email_body = NULL; + + SET @_email_body = 'The following SQL Server process ' + CASE WHEN @kill_spids = 1 THEN 'was' ELSE 'is' END + ' preventing the ['+@c_database_name+'] database transaction log from clearing. Please visit https://ajaydwivedi.com/go/logsaver to view killed database process history.' + CHAR(10) + CHAR(10); + + SELECT @_email_subject = 'Log Saver: ' + CONVERT(nvarchar(255), SERVERPROPERTY('ServerName')) + ' - ' + QUOTENAME(@c_database_name), + @_email_body = @_email_body + + ' current time: ' + CONVERT(varchar(100), collection_time, 121) + CHAR(10) + + ' spid: ' + CONVERT(varchar(30), spid) + CHAR(10) + + ' tran start time: ' + CONVERT(varchar(100), transaction_start_time, 121) + CHAR(10) + + ' database_name: ' + database_name + CHAR(10) + + ' recovery_model: ' + recovery_model + CHAR(10) + + ' log_reuse_wait_desc: ' + log_reuse_wait_desc + CHAR(10) + + ' log_size_gb: ' + CONVERT(varchar(100), log_size_mb/1024) + CHAR(10) + + ' log_used_gb: ' + CONVERT(varchar(100), log_used_mb/1024) + CHAR(10) + + ' log_used_pct: ' + CONVERT(varchar(100), log_used_pct) + CHAR(10) + + ' login_name: ' + login_name + CHAR(10) + + ' program_name: ' + program_name + CHAR(10) + + ' host_name: ' + host_name + CHAR(10) + + ' host_process_id: ' + CONVERT(varchar(30), host_process_id) + CHAR(10) + + ' command: ' + command + CHAR(10) + + ' sql_text: ' + LTRIM(REPLACE(REPLACE(REPLACE(LEFT(sql_text, 200), CHAR(10), ' '), CHAR(13), ' '), ' ', ' ')) + CHAR(10) + FROM #log_space_consumers + WHERE thresholds_violated = 1 + and spid = @_spid; + + IF @verbose > 0 + BEGIN + PRINT CHAR(13)+CHAR(13)+@_email_subject; + PRINT @_email_body; + END + + select @c_database_name = lsc.database_name, @_spid = lsc.spid + from #log_space_consumers lsc + where thresholds_violated = 1 + and spid IS NOT NULL + and lsc.spid > @_spid + order by spid + offset 0 rows fetch next 1 row only; + END + END diff --git a/sql_exporter/mssql_dba_stableinfo.collector.yml b/sql_exporter/mssql_dba_stableinfo.collector.yml new file mode 100644 index 0000000..59c3e34 --- /dev/null +++ b/sql_exporter/mssql_dba_stableinfo.collector.yml @@ -0,0 +1,732 @@ +# A collector defining standard metrics for Microsoft SQL Server. +# +# It is required that the SQL Server user has the following permissions: +# +# GRANT VIEW ANY DEFINITION TO +# GRANT VIEW SERVER STATE TO +# +collector_name: mssql_dba_stableinfo + +# Similar to global.min_interval, but applies to the queries defined by this collector only. +min_interval: 1m + +metrics: + # + # Collected using sys.databases + # + - metric_name: mssql_db__collection_time_utc + type: gauge + help: 'Collection time as Unix epoch seconds for sys.databases rows.' + key_labels: + - db_name + values: [collection_time_utc] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__create_date + type: gauge + help: 'Database create date from sys.databases.' + key_labels: + - db_name + - owner_sid + - collation_name + - service_broker_guid + - replica_id + - group_database_id + - default_language_name + - default_fulltext_language_name + - physical_database_name + values: [create_date] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__database_id + type: gauge + help: 'sys.databases.database_id value.' + key_labels: + - db_name + values: [database_id] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__source_database_id + type: gauge + help: 'sys.databases.source_database_id value.' + key_labels: + - db_name + values: [source_database_id] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__compatibility_level + type: gauge + help: 'sys.databases.compatibility_level value.' + key_labels: + - db_name + values: [compatibility_level] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__user_access + type: gauge + help: 'sys.databases.user_access value.' + key_labels: + - db_name + - user_access_desc + values: [user_access] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_read_only + type: gauge + help: 'sys.databases.is_read_only value.' + key_labels: + - db_name + values: [is_read_only] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_auto_close_on + type: gauge + help: 'sys.databases.is_auto_close_on value.' + key_labels: + - db_name + values: [is_auto_close_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_auto_shrink_on + type: gauge + help: 'sys.databases.is_auto_shrink_on value.' + key_labels: + - db_name + values: [is_auto_shrink_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__state + type: gauge + help: 'sys.databases.state value.' + key_labels: + - db_name + - state_desc + values: [state] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_in_standby + type: gauge + help: 'sys.databases.is_in_standby value.' + key_labels: + - db_name + values: [is_in_standby] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_cleanly_shutdown + type: gauge + help: 'sys.databases.is_cleanly_shutdown value.' + key_labels: + - db_name + values: [is_cleanly_shutdown] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_supplemental_logging_enabled + type: gauge + help: 'sys.databases.is_supplemental_logging_enabled value.' + key_labels: + - db_name + values: [is_supplemental_logging_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__snapshot_isolation_state + type: gauge + help: 'sys.databases.snapshot_isolation_state value.' + key_labels: + - db_name + - snapshot_isolation_state_desc + values: [snapshot_isolation_state] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_read_committed_snapshot_on + type: gauge + help: 'sys.databases.is_read_committed_snapshot_on value.' + key_labels: + - db_name + values: [is_read_committed_snapshot_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__recovery_model + type: gauge + help: 'sys.databases.recovery_model value.' + key_labels: + - db_name + - recovery_model_desc + values: [recovery_model] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__page_verify_option + type: gauge + help: 'sys.databases.page_verify_option value.' + key_labels: + - db_name + - page_verify_option_desc + values: [page_verify_option] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_auto_create_stats_on + type: gauge + help: 'sys.databases.is_auto_create_stats_on value.' + key_labels: + - db_name + values: [is_auto_create_stats_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_auto_create_stats_incremental_on + type: gauge + help: 'sys.databases.is_auto_create_stats_incremental_on value.' + key_labels: + - db_name + values: [is_auto_create_stats_incremental_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_auto_update_stats_on + type: gauge + help: 'sys.databases.is_auto_update_stats_on value.' + key_labels: + - db_name + values: [is_auto_update_stats_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_auto_update_stats_async_on + type: gauge + help: 'sys.databases.is_auto_update_stats_async_on value.' + key_labels: + - db_name + values: [is_auto_update_stats_async_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_ansi_null_default_on + type: gauge + help: 'sys.databases.is_ansi_null_default_on value.' + key_labels: + - db_name + values: [is_ansi_null_default_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_ansi_nulls_on + type: gauge + help: 'sys.databases.is_ansi_nulls_on value.' + key_labels: + - db_name + values: [is_ansi_nulls_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_ansi_padding_on + type: gauge + help: 'sys.databases.is_ansi_padding_on value.' + key_labels: + - db_name + values: [is_ansi_padding_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_ansi_warnings_on + type: gauge + help: 'sys.databases.is_ansi_warnings_on value.' + key_labels: + - db_name + values: [is_ansi_warnings_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_arithabort_on + type: gauge + help: 'sys.databases.is_arithabort_on value.' + key_labels: + - db_name + values: [is_arithabort_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_concat_null_yields_null_on + type: gauge + help: 'sys.databases.is_concat_null_yields_null_on value.' + key_labels: + - db_name + values: [is_concat_null_yields_null_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_numeric_roundabort_on + type: gauge + help: 'sys.databases.is_numeric_roundabort_on value.' + key_labels: + - db_name + values: [is_numeric_roundabort_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_quoted_identifier_on + type: gauge + help: 'sys.databases.is_quoted_identifier_on value.' + key_labels: + - db_name + values: [is_quoted_identifier_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_recursive_triggers_on + type: gauge + help: 'sys.databases.is_recursive_triggers_on value.' + key_labels: + - db_name + values: [is_recursive_triggers_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_cursor_close_on_commit_on + type: gauge + help: 'sys.databases.is_cursor_close_on_commit_on value.' + key_labels: + - db_name + values: [is_cursor_close_on_commit_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_local_cursor_default + type: gauge + help: 'sys.databases.is_local_cursor_default value.' + key_labels: + - db_name + values: [is_local_cursor_default] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_fulltext_enabled + type: gauge + help: 'sys.databases.is_fulltext_enabled value.' + key_labels: + - db_name + values: [is_fulltext_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_trustworthy_on + type: gauge + help: 'sys.databases.is_trustworthy_on value.' + key_labels: + - db_name + values: [is_trustworthy_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_db_chaining_on + type: gauge + help: 'sys.databases.is_db_chaining_on value.' + key_labels: + - db_name + values: [is_db_chaining_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_parameterization_forced + type: gauge + help: 'sys.databases.is_parameterization_forced value.' + key_labels: + - db_name + values: [is_parameterization_forced] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_master_key_encrypted_by_server + type: gauge + help: 'sys.databases.is_master_key_encrypted_by_server value.' + key_labels: + - db_name + values: [is_master_key_encrypted_by_server] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_query_store_on + type: gauge + help: 'sys.databases.is_query_store_on value.' + key_labels: + - db_name + values: [is_query_store_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_published + type: gauge + help: 'sys.databases.is_published value.' + key_labels: + - db_name + values: [is_published] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_subscribed + type: gauge + help: 'sys.databases.is_subscribed value.' + key_labels: + - db_name + values: [is_subscribed] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_merge_published + type: gauge + help: 'sys.databases.is_merge_published value.' + key_labels: + - db_name + values: [is_merge_published] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_distributor + type: gauge + help: 'sys.databases.is_distributor value.' + key_labels: + - db_name + values: [is_distributor] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_sync_with_backup + type: gauge + help: 'sys.databases.is_sync_with_backup value.' + key_labels: + - db_name + values: [is_sync_with_backup] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_broker_enabled + type: gauge + help: 'sys.databases.is_broker_enabled value.' + key_labels: + - db_name + values: [is_broker_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_date_correlation_on + type: gauge + help: 'sys.databases.is_date_correlation_on value.' + key_labels: + - db_name + values: [is_date_correlation_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_cdc_enabled + type: gauge + help: 'sys.databases.is_cdc_enabled value.' + key_labels: + - db_name + values: [is_cdc_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_encrypted + type: gauge + help: 'sys.databases.is_encrypted value.' + key_labels: + - db_name + values: [is_encrypted] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_honor_broker_priority_on + type: gauge + help: 'sys.databases.is_honor_broker_priority_on value.' + key_labels: + - db_name + values: [is_honor_broker_priority_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__resource_pool_id + type: gauge + help: 'sys.databases.resource_pool_id value.' + key_labels: + - db_name + values: [resource_pool_id] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__default_language_lcid + type: gauge + help: 'sys.databases.default_language_lcid value.' + key_labels: + - db_name + values: [default_language_lcid] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__default_fulltext_language_lcid + type: gauge + help: 'sys.databases.default_fulltext_language_lcid value.' + key_labels: + - db_name + values: [default_fulltext_language_lcid] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_nested_triggers_on + type: gauge + help: 'sys.databases.is_nested_triggers_on value.' + key_labels: + - db_name + values: [is_nested_triggers_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_transform_noise_words_on + type: gauge + help: 'sys.databases.is_transform_noise_words_on value.' + key_labels: + - db_name + values: [is_transform_noise_words_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__two_digit_year_cutoff + type: gauge + help: 'sys.databases.two_digit_year_cutoff value.' + key_labels: + - db_name + values: [two_digit_year_cutoff] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__containment + type: gauge + help: 'sys.databases.containment value.' + key_labels: + - db_name + - containment_desc + values: [containment] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__target_recovery_time_in_seconds + type: gauge + help: 'sys.databases.target_recovery_time_in_seconds value.' + key_labels: + - db_name + values: [target_recovery_time_in_seconds] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__delayed_durability + type: gauge + help: 'sys.databases.delayed_durability value.' + key_labels: + - db_name + - delayed_durability_desc + values: [delayed_durability] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_memory_optimized_elevate_to_snapshot_on + type: gauge + help: 'sys.databases.is_memory_optimized_elevate_to_snapshot_on value.' + key_labels: + - db_name + values: [is_memory_optimized_elevate_to_snapshot_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_federation_member + type: gauge + help: 'sys.databases.is_federation_member value.' + key_labels: + - db_name + values: [is_federation_member] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_remote_data_archive_enabled + type: gauge + help: 'sys.databases.is_remote_data_archive_enabled value.' + key_labels: + - db_name + values: [is_remote_data_archive_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_mixed_page_allocation_on + type: gauge + help: 'sys.databases.is_mixed_page_allocation_on value.' + key_labels: + - db_name + values: [is_mixed_page_allocation_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_temporal_history_retention_enabled + type: gauge + help: 'sys.databases.is_temporal_history_retention_enabled value.' + key_labels: + - db_name + values: [is_temporal_history_retention_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__catalog_collation_type + type: gauge + help: 'sys.databases.catalog_collation_type value.' + key_labels: + - db_name + - catalog_collation_type_desc + values: [catalog_collation_type] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_result_set_caching_on + type: gauge + help: 'sys.databases.is_result_set_caching_on value.' + key_labels: + - db_name + values: [is_result_set_caching_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_accelerated_database_recovery_on + type: gauge + help: 'sys.databases.is_accelerated_database_recovery_on value.' + key_labels: + - db_name + values: [is_accelerated_database_recovery_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_tempdb_spill_to_remote_store + type: gauge + help: 'sys.databases.is_tempdb_spill_to_remote_store value.' + key_labels: + - db_name + values: [is_tempdb_spill_to_remote_store] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_stale_page_detection_on + type: gauge + help: 'sys.databases.is_stale_page_detection_on value.' + key_labels: + - db_name + values: [is_stale_page_detection_on] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_memory_optimized_enabled + type: gauge + help: 'sys.databases.is_memory_optimized_enabled value.' + key_labels: + - db_name + values: [is_memory_optimized_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_db__is_data_retention_enabled + type: gauge + help: 'sys.databases.is_data_retention_enabled value.' + key_labels: + - db_name + values: [is_data_retention_enabled] + query_ref: mssql_sys_databases + + - metric_name: mssql_config__value + type: gauge + help: 'sys.configurations.value value.' + key_labels: + - config_name + - config_description + values: [config_value] + query_ref: mssql_sys_configurations + + - metric_name: mssql_config__collection_time_utc + type: gauge + help: 'Collection time as Unix epoch seconds for sys.configurations rows.' + key_labels: + - config_name + values: [collection_time_utc] + query_ref: mssql_sys_configurations + + - metric_name: mssql_config__value_in_use + type: gauge + help: 'sys.configurations.value_in_use value.' + key_labels: + - config_name + values: [config_value_in_use] + query_ref: mssql_sys_configurations + +queries: + - query_name: mssql_sys_databases + query: | + -- Dynamic SQL: substitute CAST(0 AS bigint) for columns that do not + -- exist on the running SQL Server version so the same metric schema + -- is emitted on SQL 2016 through SQL 2025. The following columns are + -- present only on SQL Server 2022 (16.x) or later. + DECLARE @c_tempdb_spill_to_remote_store nvarchar(200) = IIF( + COL_LENGTH('sys.databases','is_tempdb_spill_to_remote_store') IS NOT NULL, + N'CAST(d.is_tempdb_spill_to_remote_store AS bigint)', N'CAST(0 AS bigint)'); + DECLARE @c_stale_page_detection_on nvarchar(200) = IIF( + COL_LENGTH('sys.databases','is_stale_page_detection_on') IS NOT NULL, + N'CAST(d.is_stale_page_detection_on AS bigint)', N'CAST(0 AS bigint)'); + DECLARE @c_memory_optimized_enabled nvarchar(200) = IIF( + COL_LENGTH('sys.databases','is_memory_optimized_enabled') IS NOT NULL, + N'CAST(d.is_memory_optimized_enabled AS bigint)', N'CAST(0 AS bigint)'); + DECLARE @c_data_retention_enabled nvarchar(200) = IIF( + COL_LENGTH('sys.databases','is_data_retention_enabled') IS NOT NULL, + N'CAST(d.is_data_retention_enabled AS bigint)', N'CAST(0 AS bigint)'); + + DECLARE @sql nvarchar(max) = N' + SELECT + CAST(DATEDIFF_BIG(second, CONVERT(datetime2, ''19700101''), GETUTCDATE()) AS bigint) as collection_time_utc, + d.name AS db_name, + COALESCE(sys.fn_varbintohexstr(d.owner_sid), N'''') AS owner_sid, + CAST(DATEDIFF_BIG(second, CONVERT(datetime2, ''19700101''), d.create_date) AS bigint) AS create_date, + COALESCE(d.collation_name, N'''') AS collation_name, + COALESCE(CONVERT(nvarchar(36), d.service_broker_guid), N'''') AS service_broker_guid, + COALESCE(CONVERT(nvarchar(36), d.replica_id), N'''') AS replica_id, + COALESCE(CONVERT(nvarchar(36), d.group_database_id), N'''') AS group_database_id, + COALESCE(d.default_language_name, N'''') AS default_language_name, + COALESCE(d.default_fulltext_language_name, N'''') AS default_fulltext_language_name, + COALESCE(d.physical_database_name, N'''') AS physical_database_name, + d.database_id AS database_id, + d.source_database_id AS source_database_id, + d.compatibility_level AS compatibility_level, + d.user_access AS user_access, + CAST(d.is_read_only AS bigint) AS is_read_only, + CAST(d.is_auto_close_on AS bigint) AS is_auto_close_on, + CAST(d.is_auto_shrink_on AS bigint) AS is_auto_shrink_on, + d.state AS state, + CAST(d.is_in_standby AS bigint) AS is_in_standby, + CAST(d.is_cleanly_shutdown AS bigint) AS is_cleanly_shutdown, + CAST(d.is_supplemental_logging_enabled AS bigint) AS is_supplemental_logging_enabled, + d.snapshot_isolation_state AS snapshot_isolation_state, + CAST(d.is_read_committed_snapshot_on AS bigint) AS is_read_committed_snapshot_on, + d.recovery_model AS recovery_model, + d.page_verify_option AS page_verify_option, + CAST(d.is_auto_create_stats_on AS bigint) AS is_auto_create_stats_on, + CAST(d.is_auto_create_stats_incremental_on AS bigint) AS is_auto_create_stats_incremental_on, + CAST(d.is_auto_update_stats_on AS bigint) AS is_auto_update_stats_on, + CAST(d.is_auto_update_stats_async_on AS bigint) AS is_auto_update_stats_async_on, + CAST(d.is_ansi_null_default_on AS bigint) AS is_ansi_null_default_on, + CAST(d.is_ansi_nulls_on AS bigint) AS is_ansi_nulls_on, + CAST(d.is_ansi_padding_on AS bigint) AS is_ansi_padding_on, + CAST(d.is_ansi_warnings_on AS bigint) AS is_ansi_warnings_on, + CAST(d.is_arithabort_on AS bigint) AS is_arithabort_on, + CAST(d.is_concat_null_yields_null_on AS bigint) AS is_concat_null_yields_null_on, + CAST(d.is_numeric_roundabort_on AS bigint) AS is_numeric_roundabort_on, + CAST(d.is_quoted_identifier_on AS bigint) AS is_quoted_identifier_on, + CAST(d.is_recursive_triggers_on AS bigint) AS is_recursive_triggers_on, + CAST(d.is_cursor_close_on_commit_on AS bigint) AS is_cursor_close_on_commit_on, + CAST(d.is_local_cursor_default AS bigint) AS is_local_cursor_default, + CAST(d.is_fulltext_enabled AS bigint) AS is_fulltext_enabled, + CAST(d.is_trustworthy_on AS bigint) AS is_trustworthy_on, + CAST(d.is_db_chaining_on AS bigint) AS is_db_chaining_on, + CAST(d.is_parameterization_forced AS bigint) AS is_parameterization_forced, + CAST(d.is_master_key_encrypted_by_server AS bigint) AS is_master_key_encrypted_by_server, + CAST(d.is_query_store_on AS bigint) AS is_query_store_on, + CAST(d.is_published AS bigint) AS is_published, + CAST(d.is_subscribed AS bigint) AS is_subscribed, + CAST(d.is_merge_published AS bigint) AS is_merge_published, + CAST(d.is_distributor AS bigint) AS is_distributor, + CAST(d.is_sync_with_backup AS bigint) AS is_sync_with_backup, + CAST(d.is_broker_enabled AS bigint) AS is_broker_enabled, + CAST(d.is_date_correlation_on AS bigint) AS is_date_correlation_on, + CAST(d.is_cdc_enabled AS bigint) AS is_cdc_enabled, + CAST(d.is_encrypted AS bigint) AS is_encrypted, + CAST(d.is_honor_broker_priority_on AS bigint) AS is_honor_broker_priority_on, + d.resource_pool_id AS resource_pool_id, + d.default_language_lcid AS default_language_lcid, + d.default_fulltext_language_lcid AS default_fulltext_language_lcid, + CAST(d.is_nested_triggers_on AS bigint) AS is_nested_triggers_on, + CAST(d.is_transform_noise_words_on AS bigint) AS is_transform_noise_words_on, + d.two_digit_year_cutoff AS two_digit_year_cutoff, + d.containment AS containment, + d.target_recovery_time_in_seconds AS target_recovery_time_in_seconds, + d.delayed_durability AS delayed_durability, + CAST(d.is_memory_optimized_elevate_to_snapshot_on AS bigint) AS is_memory_optimized_elevate_to_snapshot_on, + CAST(d.is_federation_member AS bigint) AS is_federation_member, + CAST(d.is_remote_data_archive_enabled AS bigint) AS is_remote_data_archive_enabled, + CAST(d.is_mixed_page_allocation_on AS bigint) AS is_mixed_page_allocation_on, + CAST(d.is_temporal_history_retention_enabled AS bigint) AS is_temporal_history_retention_enabled, + d.catalog_collation_type AS catalog_collation_type, + CAST(d.is_result_set_caching_on AS bigint) AS is_result_set_caching_on, + CAST(d.is_accelerated_database_recovery_on AS bigint) AS is_accelerated_database_recovery_on, + ' + @c_tempdb_spill_to_remote_store + N' AS is_tempdb_spill_to_remote_store, + ' + @c_stale_page_detection_on + N' AS is_stale_page_detection_on, + ' + @c_memory_optimized_enabled + N' AS is_memory_optimized_enabled, + ' + @c_data_retention_enabled + N' AS is_data_retention_enabled, + COALESCE(d.user_access_desc, N'''') AS user_access_desc, + COALESCE(d.state_desc, N'''') AS state_desc, + COALESCE(d.snapshot_isolation_state_desc, N'''') AS snapshot_isolation_state_desc, + COALESCE(d.recovery_model_desc, N'''') AS recovery_model_desc, + COALESCE(d.page_verify_option_desc, N'''') AS page_verify_option_desc, + COALESCE(d.containment_desc, N'''') AS containment_desc, + COALESCE(d.delayed_durability_desc, N'''') AS delayed_durability_desc, + COALESCE(d.catalog_collation_type_desc, N'''') AS catalog_collation_type_desc + FROM sys.databases AS d + ORDER BY d.name'; + + EXEC sp_executesql @sql; + + - query_name: mssql_sys_configurations + query: | + SELECT + CAST(DATEDIFF_BIG(second, CONVERT(datetime2, '19700101'), GETUTCDATE()) AS bigint) as collection_time_utc, + name AS config_name, + CAST(value AS int) AS config_value, + CAST(value_in_use AS int) AS config_value_in_use, + description AS config_description + FROM sys.configurations + ORDER BY name \ No newline at end of file diff --git a/sql_exporter/mssql_dba_whoisactive.collector.yml b/sql_exporter/mssql_dba_whoisactive.collector.yml new file mode 100644 index 0000000..d1bc960 --- /dev/null +++ b/sql_exporter/mssql_dba_whoisactive.collector.yml @@ -0,0 +1,259 @@ +# A collector defining standard metrics for Microsoft SQL Server. +# +# It is required that the SQL Server user has the following permissions: +# +# GRANT VIEW ANY DEFINITION TO +# GRANT VIEW SERVER STATE TO +# +collector_name: mssql_dba_whoisactive + +# Similar to global.min_interval, but applies to the queries defined by this collector only. +min_interval: 2m + +metrics: + + # + # Collected using sp_WhoIsActive + # + - metric_name: mssql_whoisactive__start_time + type: gauge + help: 'Start time of the session along with other session details.' + key_labels: + - unique_key + - session_id + - program_name + - login_name + - database_name + - status + - wait_info + - sql_command + - blocked_session_count + - blocking_session_id + - sql_text + - avg_elapsed_time + - context_switches + - tran_start_time + - tran_log_writes + - implicit_tran + - query_plan + - host_name + - additional_info + - memory_info + - login_time + - request_id + values: [start_time] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__reads + type: gauge + help: 'Number of reads for the session.' + key_labels: + - unique_key + values: [reads] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__used_memory + type: gauge + help: 'Used memory for the session.' + key_labels: + - unique_key + values: [used_memory] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__cpu + type: gauge + help: 'CPU used by the session.' + key_labels: + - unique_key + values: [CPU] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__open_tran_count + type: gauge + help: 'Open transaction count for the session.' + key_labels: + - unique_key + values: [open_tran_count] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__writes + type: gauge + help: 'Number of writes for the session.' + key_labels: + - unique_key + values: [writes] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__tempdb_allocations + type: gauge + help: 'Tempdb allocations for the session.' + key_labels: + - unique_key + values: [tempdb_allocations] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__percent_complete + type: gauge + help: 'Percent complete for the session.' + key_labels: + - unique_key + values: [percent_complete] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__collection_time_utc + type: gauge + help: 'Collection time for the session snapshot.' + key_labels: + - unique_key + values: [collection_time_utc] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__tasks + type: gauge + help: 'Number of tasks for the session.' + key_labels: + - unique_key + values: [tasks] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__physical_reads + type: gauge + help: 'Physical reads performed by the session, in KB (8 KB pages).' + key_labels: + - unique_key + values: [physical_reads] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__physical_io + type: gauge + help: 'Physical I/O (reads + writes) for the session, in KB (8 KB pages).' + key_labels: + - unique_key + values: [physical_io] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__tempdb_current + type: gauge + help: 'Current tempdb pages held by the session, in KB (8 KB pages).' + key_labels: + - unique_key + values: [tempdb_current] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__granted_memory + type: gauge + help: 'Memory grant given to the session, in KB (8 KB pages).' + key_labels: + - unique_key + values: [granted_memory] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__requested_memory + type: gauge + help: 'Memory grant requested by the session, in KB (8 KB pages).' + key_labels: + - unique_key + values: [requested_memory] + query_ref: mssql_sp_whoisactive + + - metric_name: mssql_whoisactive__max_used_memory + type: gauge + help: 'Maximum memory used by the session, in KB (8 KB pages).' + key_labels: + - unique_key + values: [max_used_memory] + query_ref: mssql_sp_whoisactive + +queries: + - query_name: mssql_sp_whoisactive + query: | + SET NOCOUNT ON; + EXEC(N' + CREATE TABLE #whoisactive ( + [collection_time] datetime NOT NULL, + [session_id] smallint NOT NULL, + [program_name] nvarchar(128) NULL, + [login_name] nvarchar(128) NOT NULL, + [database_name] nvarchar(128) NULL, + [CPU] bigint NULL, + [used_memory] bigint NOT NULL, + [open_tran_count] smallint NULL, + [status] varchar(30) NOT NULL, + [wait_info] nvarchar(4000) NULL, + [sql_command] nvarchar(max) NULL, + [blocked_session_count] smallint NULL, + [blocking_session_id] smallint NULL, + [sql_text] nvarchar(max) NULL, + [avg_elapsed_time] int NULL, + [physical_io] bigint NULL, + [reads] bigint NULL, + [physical_reads] bigint NULL, + [writes] bigint NULL, + [tempdb_allocations] bigint NULL, + [tempdb_current] bigint NULL, + [context_switches] bigint NULL, + [max_used_memory] bigint NULL, + [requested_memory] bigint NULL, + [granted_memory] bigint NULL, + [tasks] smallint NULL, + [tran_start_time] datetime NULL, + [tran_log_writes] nvarchar(4000) NULL, + [implicit_tran] nvarchar(3) NULL, + [query_plan] xml NULL, + [percent_complete] real NULL, + [host_name] nvarchar(128) NULL, + [additional_info] xml NULL, + [memory_info] xml NULL, + [start_time] datetime NOT NULL, + [login_time] datetime NULL, + [request_id] int NULL + ); + + EXEC dbo.sp_WhoIsActive + @get_outer_command=1, @get_task_info=2, @find_block_leaders=1, @get_plans=1, @get_avg_time=1, + @get_additional_info=1, @get_transaction_info=1, @get_memory_info = 1, @format_output = 0, + @output_column_list = ''[collection_time][session_id][program_name][login_name][database_name][CPU][used_memory][open_tran_count][status][wait_info][sql_command][blocked_session_count][blocking_session_id][sql_text][avg_elapsed_time][physical_io][reads][physical_reads][writes][tempdb_allocations][tempdb_current][context_switches][max_used_memory][requested_memory][granted_memory][tasks][tran_start_time][tran_log_writes][implicit_tran][query_plan][percent_complete][host_name][additional_info][memory_info][start_time][login_time][request_id]'', + @destination_table = ''#whoisactive''; + + SELECT + [unique_key] = convert(varchar(30),getdate(),121) + '' | '' + convert(varchar(10),[session_id]), + CAST(DATEDIFF_BIG(second, CONVERT(datetime2, ''19700101''), getutcdate()) AS bigint) AS [collection_time_utc], + [session_id], + COALESCE([program_name], '''') AS [program_name], + [login_name], + COALESCE([database_name], '''') AS [database_name], + [status], + COALESCE([wait_info], '''') AS [wait_info], + COALESCE([sql_command], '''') AS [sql_command], + COALESCE([blocked_session_count], 0) AS [blocked_session_count], + COALESCE([blocking_session_id],'''') AS [blocking_session_id], + COALESCE([sql_text], '''') AS [sql_text], + COALESCE([avg_elapsed_time], 0) AS [avg_elapsed_time], + COALESCE([physical_io], 0) AS [physical_io], + COALESCE([physical_reads], 0) AS [physical_reads], + COALESCE([tempdb_current], 0) AS [tempdb_current], + COALESCE([context_switches], 0) AS [context_switches], + COALESCE([max_used_memory], 0) AS [max_used_memory], + COALESCE([requested_memory], 0) AS [requested_memory], + COALESCE([granted_memory], 0) AS [granted_memory], + COALESCE([tasks], 0) AS [tasks], + COALESCE(CONVERT(nvarchar(33), [tran_start_time], 126), '''') AS [tran_start_time], + COALESCE([tran_log_writes], '''') AS [tran_log_writes], + COALESCE([implicit_tran], '''') AS [implicit_tran], + COALESCE(CONVERT(nvarchar(max), [query_plan]), '''') AS [query_plan], + COALESCE([host_name], '''') AS [host_name], + COALESCE(CONVERT(nvarchar(max), [additional_info]), '''') AS [additional_info], + COALESCE(CONVERT(nvarchar(max), [memory_info]), '''') AS [memory_info], + COALESCE(CONVERT(nvarchar(33), [login_time], 126), '''') AS [login_time], + COALESCE([request_id], 0) AS [request_id], + COALESCE([reads], 0) AS [reads], + [used_memory], + COALESCE([CPU], 0) AS [CPU], + COALESCE([open_tran_count], 0) AS [open_tran_count], + COALESCE([writes], 0) AS [writes], + COALESCE([tempdb_allocations], 0) AS [tempdb_allocations], + COALESCE([percent_complete], 0) AS [percent_complete], + CAST(DATEDIFF_BIG(second, CONVERT(datetime2, ''19700101''), [start_time]) AS bigint) AS [start_time] + FROM #whoisactive; + '); + diff --git a/sql_exporter/mssql_sqlagent_jobs.collector.yml b/sql_exporter/mssql_sqlagent_jobs.collector.yml new file mode 100644 index 0000000..83d9f36 --- /dev/null +++ b/sql_exporter/mssql_sqlagent_jobs.collector.yml @@ -0,0 +1,144 @@ +# SQL Agent job activity metrics for SQLMonitor. +# +# Required permissions (on msdb): +# +# GRANT SELECT ON OBJECT::msdb.dbo.sysjobs TO ; +# GRANT SELECT ON OBJECT::msdb.dbo.sysjobhistory TO ; +# GRANT SELECT ON OBJECT::msdb.dbo.sysjobactivity TO ; +# GRANT SELECT ON OBJECT::msdb.dbo.sysjobschedules TO ; +# GRANT SELECT ON OBJECT::msdb.dbo.sysschedules TO ; +# GRANT SELECT ON OBJECT::msdb.dbo.syscategories TO ; +# +collector_name: mssql_sqlagent_jobs + +min_interval: 1m + +metrics: + - metric_name: mssql_sqlagent_job__enabled + type: gauge + help: 'Whether the SQL Agent job is enabled (1) or disabled (0).' + key_labels: [job_name, job_id, category_name, owner_name] + values: [enabled] + query_ref: mssql_sqlagent_jobs_latest + + - metric_name: mssql_sqlagent_job__last_run_outcome + type: gauge + help: 'Last run outcome code: 0=Failed, 1=Succeeded, 2=Retry, 3=Canceled, 5=Unknown/no-history.' + key_labels: [job_name, job_id, category_name, owner_name, last_run_outcome_desc] + values: [last_run_outcome] + query_ref: mssql_sqlagent_jobs_latest + + - metric_name: mssql_sqlagent_job__last_run_duration_seconds + type: gauge + help: 'Duration (seconds) of the last completed run of the job.' + key_labels: [job_name, job_id] + values: [last_run_duration_seconds] + query_ref: mssql_sqlagent_jobs_latest + + - metric_name: mssql_sqlagent_job__last_run_end_time_utc + type: gauge + help: 'Unix epoch seconds when the last completed run of the job ended. 0 if no history.' + key_labels: [job_name, job_id] + values: [last_run_end_time_utc] + query_ref: mssql_sqlagent_jobs_latest + + - metric_name: mssql_sqlagent_job__next_run_time_utc + type: gauge + help: 'Unix epoch seconds for the next scheduled run of the job. 0 if unscheduled.' + key_labels: [job_name, job_id] + values: [next_run_time_utc] + query_ref: mssql_sqlagent_jobs_latest + + - metric_name: mssql_sqlagent_job__is_running + type: gauge + help: 'Whether the SQL Agent job is currently running (1) or idle (0).' + key_labels: [job_name, job_id] + values: [is_running] + query_ref: mssql_sqlagent_jobs_latest + + - metric_name: mssql_sqlagent_job__step_failures_last_24h + type: gauge + help: 'Count of job step failures (run_status=0, step_id>0) in the last 24 hours.' + key_labels: [job_name, job_id] + values: [step_failures_last_24h] + query_ref: mssql_sqlagent_jobs_latest + +queries: + # NOTE: sql_exporter (go-mssqldb) only reads the FIRST result-set from a + # batch and treats `SELECT INTO #tmp` statements as zero-column resultsets + # that interleave with the final SELECT -- which causes silent scrape + # failures. So this query is deliberately a SINGLE SELECT using derived + # tables instead of temp tables. + - query_name: mssql_sqlagent_jobs_latest + query: | + SELECT job_name = CAST(j.name AS nvarchar(256)), + job_id = CONVERT(nvarchar(36), j.job_id), + category_name = ISNULL(c.name, N''), + owner_name = ISNULL(SUSER_SNAME(j.owner_sid), N''), + enabled = CAST(j.enabled AS int), + last_run_outcome = ISNULL(lo.run_status, 5), + last_run_outcome_desc = CASE ISNULL(lo.run_status, 5) + WHEN 0 THEN N'Failed' + WHEN 1 THEN N'Succeeded' + WHEN 2 THEN N'Retry' + WHEN 3 THEN N'Canceled' + ELSE N'Unknown' END, + last_run_duration_seconds = ISNULL(lo.run_seconds, 0), + last_run_end_time_utc = CAST(ISNULL( + DATEDIFF_BIG(second, + CONVERT(datetime2, '19700101'), + DATEADD(mi, DATEDIFF(mi, GETDATE(), GETUTCDATE()), lo.run_end_dt)), 0) AS bigint), + next_run_time_utc = CAST(ISNULL( + DATEDIFF_BIG(second, + CONVERT(datetime2, '19700101'), + DATEADD(mi, DATEDIFF(mi, GETDATE(), GETUTCDATE()), nr.next_run_dt)), 0) AS bigint), + is_running = ISNULL(r.is_running, 0), + step_failures_last_24h = ISNULL(sf.failures_24h, 0) + FROM msdb.dbo.sysjobs j + LEFT JOIN msdb.dbo.syscategories c + ON c.category_id = j.category_id + LEFT JOIN ( + SELECT job_id, run_status, run_end_dt, run_seconds + FROM ( + SELECT job_id, run_status, + run_end_dt = msdb.dbo.agent_datetime(run_date, run_time), + run_seconds = (run_duration / 10000) * 3600 + + ((run_duration / 100) % 100) * 60 + + (run_duration % 100), + rn = ROW_NUMBER() OVER ( + PARTITION BY job_id + ORDER BY run_date DESC, run_time DESC, instance_id DESC) + FROM msdb.dbo.sysjobhistory + WHERE step_id = 0 + ) x + WHERE rn = 1 + ) lo ON lo.job_id = j.job_id + LEFT JOIN ( + SELECT h.job_id, failures_24h = COUNT(*) + FROM msdb.dbo.sysjobhistory h + WHERE h.step_id > 0 AND h.run_status = 0 + AND msdb.dbo.agent_datetime(h.run_date, h.run_time) + >= DATEADD(hour, -24, GETDATE()) + GROUP BY h.job_id + ) sf ON sf.job_id = j.job_id + LEFT JOIN ( + SELECT a.job_id, is_running = 1 + FROM msdb.dbo.sysjobactivity a + INNER JOIN ( + SELECT job_id, max_ts = MAX(session_id) + FROM msdb.dbo.sysjobactivity GROUP BY job_id + ) m ON m.job_id = a.job_id AND m.max_ts = a.session_id + WHERE a.start_execution_date IS NOT NULL + AND a.stop_execution_date IS NULL + ) r ON r.job_id = j.job_id + LEFT JOIN ( + SELECT js.job_id, + next_run_dt = MIN(msdb.dbo.agent_datetime( + NULLIF(js.next_run_date, 0), + NULLIF(js.next_run_time, 0))) + FROM msdb.dbo.sysjobschedules js + INNER JOIN msdb.dbo.sysschedules s + ON s.schedule_id = js.schedule_id + WHERE s.enabled = 1 AND js.next_run_date > 0 + GROUP BY js.job_id + ) nr ON nr.job_id = j.job_id; diff --git a/sql_exporter/mssql_standard.collector.yml b/sql_exporter/mssql_standard.collector.yml new file mode 100644 index 0000000..e573ccd --- /dev/null +++ b/sql_exporter/mssql_standard.collector.yml @@ -0,0 +1,246 @@ +# A collector defining standard metrics for Microsoft SQL Server. +# +# It is required that the SQL Server user has the following permissions: +# +# GRANT VIEW ANY DEFINITION TO +# GRANT VIEW SERVER STATE TO +# +collector_name: mssql_standard + +# Similar to global.min_interval, but applies to the queries defined by this collector only. +#min_interval: 0s + +metrics: + - metric_name: mssql_up + type: gauge + help: 'SQL Server instance availability' + values: [up] + query: | + select 1 as [up]; + + - metric_name: mssql_local_time_seconds + type: gauge + help: 'Local time in seconds since epoch (Unix time).' + values: [unix_time] + query: | + SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time + + - metric_name: mssql_sqlserver_uptime_seconds + type: gauge + help: 'SQL Server uptime in seconds since the SQL Server service started.' + values: [uptime_seconds] + query: | + SELECT CAST(DATEDIFF(second, sqlserver_start_time, SYSDATETIME()) AS bigint) AS uptime_seconds + FROM sys.dm_os_sys_info + + - metric_name: mssql_os_cpu_count + type: gauge + help: 'Number of CPUs available to the host OS from sys.dm_os_sys_info.' + values: [cpu_count] + query: | + SELECT CAST(cpu_count AS bigint) AS cpu_count + FROM sys.dm_os_sys_info + + - metric_name: mssql_sqlserver_cpu_count + type: gauge + help: 'Number of logical CPUs used by SQL Serverfrom sys.dm_os_sys_info.' + values: [cpu_count] + query: | + SELECT CAST(scheduler_count AS bigint) AS cpu_count + FROM sys.dm_os_sys_info + + - metric_name: mssql_os_uptime_seconds + type: gauge + help: 'Operating system uptime in seconds since the host booted.' + values: [uptime_seconds] + query: | + SELECT CAST(ms_ticks / 1000 AS bigint) AS uptime_seconds + FROM sys.dm_os_sys_info + + - metric_name: mssql_connections + type: gauge + help: 'Number of active connections.' + key_labels: + - db + values: [count] + query: | + SELECT DB_NAME(sp.dbid) AS db, COUNT(sp.spid) AS count + FROM sys.sysprocesses sp + GROUP BY DB_NAME(sp.dbid) + + # + # Collected from sys.dm_os_performance_counters + # + - metric_name: mssql_deadlocks + type: counter + help: 'Number of lock requests that resulted in a deadlock.' + values: [cntr_value] + query: | + SELECT cntr_value + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE counter_name = 'Number of Deadlocks/sec' AND instance_name = '_Total' + + - metric_name: mssql_user_errors + type: counter + help: 'Number of user errors.' + values: [cntr_value] + query: | + SELECT cntr_value + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE counter_name = 'Errors/sec' AND instance_name = 'User Errors' + + - metric_name: mssql_kill_connection_errors + type: counter + help: 'Number of severe errors that caused SQL Server to kill the connection.' + values: [cntr_value] + query: | + SELECT cntr_value + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE counter_name = 'Errors/sec' AND instance_name = 'Kill Connection Errors' + + - metric_name: mssql_page_life_expectancy_seconds + type: gauge + help: 'The minimum number of seconds a page will stay in the buffer pool on this node without references.' + values: [cntr_value] + query: | + SELECT top(1) cntr_value + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE counter_name = 'Page life expectancy' + + - metric_name: mssql_batch_requests + type: counter + help: 'Number of command batches received.' + values: [cntr_value] + query: | + SELECT cntr_value + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE counter_name = 'Batch Requests/sec' + + - metric_name: mssql_log_growths + type: counter + help: 'Number of times the transaction log has been expanded, per database.' + key_labels: + - db + values: [cntr_value] + query: | + SELECT rtrim(instance_name) AS db, cntr_value + FROM sys.dm_os_performance_counters WITH (NOLOCK) + WHERE counter_name = 'Log Growths' AND instance_name <> '_Total' + + - metric_name: mssql_buffer_cache_hit_ratio + type: gauge + help: 'Ratio of requests that hit the buffer cache' + values: [cntr_value] + query: | + SELECT cntr_value + FROM sys.dm_os_performance_counters + WHERE [counter_name] = 'Buffer cache hit ratio' + + - metric_name: mssql_checkpoint_pages_sec + type: gauge + help: 'Checkpoint Pages Per Second' + values: [cntr_value] + query: | + SELECT cntr_value + FROM sys.dm_os_performance_counters + WHERE [counter_name] = 'Checkpoint pages/sec' + + # + # Collected from sys.dm_io_virtual_file_stats + # + - metric_name: mssql_io_stall_seconds + type: counter + help: 'Stall time in seconds per database and I/O operation.' + key_labels: + - db + value_label: operation + values: + - read + - write + query_ref: mssql_io_stall + - metric_name: mssql_io_stall_total_seconds + type: counter + help: 'Total stall time in seconds per database.' + key_labels: + - db + values: + - io_stall + query_ref: mssql_io_stall + + # + # Collected from sys.dm_os_process_memory + # + - metric_name: mssql_resident_memory_bytes + type: gauge + help: 'SQL Server resident memory size (AKA working set).' + values: [resident_memory_bytes] + query_ref: mssql_process_memory + + - metric_name: mssql_virtual_memory_bytes + type: gauge + help: 'SQL Server committed virtual memory size.' + values: [virtual_memory_bytes] + query_ref: mssql_process_memory + + - metric_name: mssql_memory_utilization_percentage + type: gauge + help: 'The percentage of committed memory that is in the working set.' + values: [memory_utilization_percentage] + query_ref: mssql_process_memory + + - metric_name: mssql_page_fault_count + type: counter + help: 'The number of page faults that were incurred by the SQL Server process.' + values: [page_fault_count] + query_ref: mssql_process_memory + + # + # Collected from sys.dm_os_sys_memory + # + - metric_name: mssql_os_memory + type: gauge + help: 'OS physical memory, used and available.' + value_label: 'state' + values: [used, available] + query: | + SELECT + (total_physical_memory_kb - available_physical_memory_kb) * 1024 AS used, + available_physical_memory_kb * 1024 AS available + FROM sys.dm_os_sys_memory + + - metric_name: mssql_os_page_file + type: gauge + help: 'OS page file, used and available.' + value_label: 'state' + values: [used, available] + query: | + SELECT + (total_page_file_kb - available_page_file_kb) * 1024 AS used, + available_page_file_kb * 1024 AS available + FROM sys.dm_os_sys_memory + +queries: + # Populates `mssql_io_stall` and `mssql_io_stall_total` + - query_name: mssql_io_stall + query: | + SELECT + cast(DB_Name(a.database_id) as varchar) AS [db], + sum(io_stall_read_ms) / 1000.0 AS [read], + sum(io_stall_write_ms) / 1000.0 AS [write], + sum(io_stall) / 1000.0 AS io_stall + FROM + sys.dm_io_virtual_file_stats(null, null) a + INNER JOIN sys.master_files b ON a.database_id = b.database_id AND a.file_id = b.file_id + GROUP BY a.database_id + + # Populates `mssql_resident_memory_bytes`, `mssql_virtual_memory_bytes`, `mssql_memory_utilization_percentage` and + # `mssql_page_fault_count`. + - query_name: mssql_process_memory + query: | + SELECT + physical_memory_in_use_kb * 1024 AS resident_memory_bytes, + virtual_address_space_committed_kb * 1024 AS virtual_memory_bytes, + memory_utilization_percentage, + page_fault_count + FROM sys.dm_os_process_memory + diff --git a/sql_exporter/mssql_xevent.collector.yml b/sql_exporter/mssql_xevent.collector.yml new file mode 100644 index 0000000..a6247d9 --- /dev/null +++ b/sql_exporter/mssql_xevent.collector.yml @@ -0,0 +1,124 @@ +# Extended Event (xevent_metrics) aggregates for SQLMonitor. +# +# Source table: [DBA].[dbo].[xevent_metrics] populated by +# dbo.usp_collect_xevent_metrics (file target) or +# usp_collect_xevent_metrics_ringbuffer (ring buffer target). See +# SCH-Create-XEvents.sql / SCH-Create-XEvents-RingBuffer.sql. +# +# Cardinality strategy: aggregate per (event_name, database_name, result, +# client_app_name) over the most recent 5 minutes. Top 500 rows by event +# count to cap label combos even on busy workloads. Use PromQL rate() on +# the *_total counters to render the XEvent Trend dashboard. +# +# Required permissions: +# +# USE [DBA]; GRANT SELECT ON OBJECT::dbo.xevent_metrics TO ; +# +collector_name: mssql_xevent + +min_interval: 1m + +metrics: + - metric_name: mssql_xevent__events_last_5m + type: gauge + help: 'Count of extended events in the last 5 minutes, grouped by event/database/result/app.' + key_labels: [event_name, database_name, result, client_app_name] + values: [events_count] + query_ref: mssql_xevent_recent + + - metric_name: mssql_xevent__cpu_time_ms_last_5m + type: gauge + help: 'Sum of cpu_time_ms over extended events in the last 5 minutes.' + key_labels: [event_name, database_name, result, client_app_name] + values: [cpu_time_ms_sum] + query_ref: mssql_xevent_recent + + - metric_name: mssql_xevent__duration_seconds_last_5m + type: gauge + help: 'Sum of duration_seconds over extended events in the last 5 minutes.' + key_labels: [event_name, database_name, result, client_app_name] + values: [duration_seconds_sum] + query_ref: mssql_xevent_recent + + - metric_name: mssql_xevent__logical_reads_last_5m + type: gauge + help: 'Sum of logical_reads over extended events in the last 5 minutes.' + key_labels: [event_name, database_name, result, client_app_name] + values: [logical_reads_sum] + query_ref: mssql_xevent_recent + + - metric_name: mssql_xevent__physical_reads_last_5m + type: gauge + help: 'Sum of physical_reads over extended events in the last 5 minutes.' + key_labels: [event_name, database_name, result, client_app_name] + values: [physical_reads_sum] + query_ref: mssql_xevent_recent + + - metric_name: mssql_xevent__writes_last_5m + type: gauge + help: 'Sum of writes over extended events in the last 5 minutes.' + key_labels: [event_name, database_name, result, client_app_name] + values: [writes_sum] + query_ref: mssql_xevent_recent + + - metric_name: mssql_xevent__collection_time_utc + type: gauge + help: 'Unix epoch seconds when this 5-minute window was aggregated.' + key_labels: [event_name, database_name, result, client_app_name] + values: [collection_time_utc] + query_ref: mssql_xevent_recent + +queries: + # NOTE: sql_exporter (go-mssqldb) reads only the first result-set from a + # batch. IF / ELSE branches + a stand-alone `SELECT TOP 0` gate upstream + # of sp_executesql confuse the driver and cause silent scrape failures. + # We collapse the query into a single sp_executesql call whose dynamic + # SQL is chosen up front (real query if DBA.dbo.xevent_metrics exists, + # otherwise a TOP 0 shim). Exactly one result-set, always. + - query_name: mssql_xevent_recent + query: | + DECLARE @now datetime2 = SYSDATETIME(); + DECLARE @from datetime2 = DATEADD(minute, -5, @now); + DECLARE @now_utc bigint = DATEDIFF_BIG(second, + CONVERT(datetime2, '19700101'), SYSUTCDATETIME()); + DECLARE @have_xev bit = CASE WHEN DB_ID('DBA') IS NOT NULL + AND OBJECT_ID('DBA.dbo.xevent_metrics') IS NOT NULL + THEN 1 ELSE 0 END; + + DECLARE @sql nvarchar(max) = + CASE WHEN @have_xev = 1 THEN N' + SELECT TOP 500 + event_name = CAST(COALESCE(x.event_name, N'''') AS nvarchar(128)), + database_name = CAST(COALESCE(x.database_name, N'''') AS nvarchar(128)), + result = CAST(COALESCE(x.result, N'''') AS nvarchar(32)), + client_app_name = CAST(COALESCE(x.client_app_name, N'''') AS nvarchar(256)), + events_count = CAST(COUNT_BIG(*) AS bigint), + cpu_time_ms_sum = CAST(SUM(CAST(x.cpu_time_ms AS bigint)) AS bigint), + duration_seconds_sum = CAST(SUM(CAST(x.duration_seconds AS bigint)) AS bigint), + logical_reads_sum = CAST(SUM(CAST(x.logical_reads AS bigint)) AS bigint), + physical_reads_sum = CAST(SUM(CAST(x.physical_reads AS bigint)) AS bigint), + writes_sum = CAST(SUM(CAST(x.writes AS bigint)) AS bigint), + collection_time_utc = @now_utc + FROM [DBA].[dbo].[xevent_metrics] AS x WITH (NOLOCK) + WHERE x.event_time >= @from AND x.event_time <= @now + GROUP BY x.event_name, x.database_name, x.result, x.client_app_name + ORDER BY events_count DESC' + ELSE N' + SELECT TOP 0 + CAST(N'''' AS nvarchar(128)) AS event_name, + CAST(N'''' AS nvarchar(128)) AS database_name, + CAST(N'''' AS nvarchar(32)) AS result, + CAST(N'''' AS nvarchar(256)) AS client_app_name, + CAST(0 AS bigint) AS events_count, + CAST(0 AS bigint) AS cpu_time_ms_sum, + CAST(0 AS bigint) AS duration_seconds_sum, + CAST(0 AS bigint) AS logical_reads_sum, + CAST(0 AS bigint) AS physical_reads_sum, + CAST(0 AS bigint) AS writes_sum, + CAST(@now_utc AS bigint) AS collection_time_utc' + END; + + EXEC sp_executesql + @sql, + N'@from datetime2, @now datetime2, @now_utc bigint', + @from = @from, @now = @now, @now_utc = @now_utc; diff --git a/sql_exporter/pastetheplan-proxy.py b/sql_exporter/pastetheplan-proxy.py new file mode 100644 index 0000000..2f7a838 --- /dev/null +++ b/sql_exporter/pastetheplan-proxy.py @@ -0,0 +1,183 @@ +#!/usr/bin/env python3 +""" +pastetheplan-proxy.py – Grafana → PasteThePlan local bridge (Python stdlib only) + +Usage +----- + cd sql_exporter + python3 pastetheplan-proxy.py # port 8080, Prometheus on 9090 + python3 pastetheplan-proxy.py 8080 http://prom:9090 # custom port / Prometheus URL + +Grafana data link (set on the query_plan column override in both table panels): + http://localhost:8080/pastetheplan-bridge.html?key=${__data.fields.unique_key}&server=${Server} + +How it works +------------ +1. Grafana opens the URL above in a new browser tab. + Only the tiny unique_key value is passed — no large/newline-containing XML in the URL. +2. THIS server queries the Prometheus instant-query API server-side: + mssql_whoisactive__start_time{unique_key="", instance=""} +3. It reads the "query_plan" label from the returned metric. +4. It POSTs {"queryplan_xml": ""} to the PasteThePlan AWS Lambda API (no CORS). +5. It 302-redirects the browser to https://www.brentozar.com/pastetheplan/?id= +""" + +import html as _html_escape +import http.server +import json +import sys +import urllib.error +import urllib.parse +import urllib.request + +PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8080 +PROM_URL = sys.argv[2].rstrip("/") if len(sys.argv) > 2 else "http://localhost:9090" + +# Current PasteThePlan API endpoint (updated 2026-04; old jeczi7iqj8 endpoint is dead) +PTP_API = "https://i7is2wx2pd.execute-api.us-west-2.amazonaws.com/id" +PTP_VIEWER = "https://www.brentozar.com/pastetheplan/?id=" + +HANDLED_PATHS = {"/pastetheplan-bridge.html", "/pastetheplan-bridge", "/"} + + +class PTPHandler(http.server.BaseHTTPRequestHandler): + + def do_GET(self): + parsed = urllib.parse.urlparse(self.path) + + if parsed.path not in HANDLED_PATHS: + self._html(404, "

404 – Not Found

" + "

Use /pastetheplan-bridge.html" + "?key=<unique_key>&server=<instance>

") + return + + params = urllib.parse.parse_qs(parsed.query, keep_blank_values=True) + key = (params.get("key") or [""])[0].strip() + server = (params.get("server") or [""])[0].strip() + + if not key: + self._html(400, "

Missing parameter

" + "

key (unique_key) is required.

") + return + + # ------------------------------------------------------------------ + # 1. Fetch query_plan XML from Prometheus (server-side, no CORS, + # no URL-size / newline-in-URL problems). + # ------------------------------------------------------------------ + # Use last_over_time([24h]) so the query succeeds even after a session + # ends and its time series becomes stale (Prometheus default staleness + # window is only 5 minutes for instant queries). + selector = f'mssql_whoisactive__start_time{{unique_key="{key}"' + if server: + selector += f',instance="{server}"' + selector += "}" + promql = f"last_over_time({selector}[24h])" + prom_req_url = (PROM_URL + "/api/v1/query?query=" + + urllib.parse.quote(promql, safe="")) + print(f"[PTP proxy] Querying Prometheus: {prom_req_url}") + + try: + with urllib.request.urlopen(prom_req_url, timeout=10) as r: + prom_data = json.loads(r.read().decode("utf-8")) + except Exception as exc: + self._html(502, "

Could not reach Prometheus

" + "

" + str(exc) + "

" + "

Is Prometheus running at " + PROM_URL + "?

") + return + + results = prom_data.get("data", {}).get("result", []) + if not results: + self._html(404, "

No data found in Prometheus

" + "

No metric for unique_key=" + _html_escape.escape(key) + + " within the last 24 hours.

" + "

PromQL used: " + _html_escape.escape(promql) + "

" + "

Is the proxy pointing at the right Prometheus? " + "Current: " + PROM_URL + "
" + "Override: python3 pastetheplan-proxy.py 8080 http://<prom-host>:<port>

") + return + + xml = results[0].get("metric", {}).get("query_plan", "").strip() + if not xml: + self._html(400, "

No query plan for this session

" + "

The query_plan label is empty — " + "this session had no execution plan at collection time.

") + return + + print(f"[PTP proxy] Got query_plan: {len(xml):,} chars") + + # ------------------------------------------------------------------ + # 2. POST the XML to PasteThePlan (server-side, no CORS). + # ------------------------------------------------------------------ + try: + body = json.dumps({"queryplan_xml": xml}).encode("utf-8") + req = urllib.request.Request(PTP_API, data=body, + headers={"Content-Type": "application/json"}, + method="POST") + with urllib.request.urlopen(req, timeout=20) as resp: + result = json.loads(resp.read().decode("utf-8")) + except urllib.error.HTTPError as exc: + err_body = "" + try: + err_body = exc.read().decode("utf-8", errors="replace") + except Exception: + pass + self._html(502, "

PasteThePlan API error (HTTP " + str(exc.code) + ")

" + "

" + str(exc.reason) + "

" + err_body + "
") + return + except urllib.error.URLError as exc: + self._html(502, "

Could not reach PasteThePlan API

" + "

" + str(exc.reason) + "

") + return + except Exception as exc: + self._html(500, "

Unexpected error

" + str(exc) + "

") + return + + if result.get("error"): + preview = _html_escape.escape((xml[:600] + " …") if len(xml) > 600 else xml) + self._html(400, "

PasteThePlan rejected the plan

" + "

" + str(result.get("message", "")) + "

" + "
"
+                       + preview + "
") + return + + plan_id = result.get("id") + if not plan_id: + self._html(502, "

Unexpected API response (no id)

" + "
" + json.dumps(result, indent=2) + "
") + return + + location = PTP_VIEWER + urllib.parse.quote(str(plan_id), safe="") + print(f"[PTP proxy] Redirecting → {location}") + self.send_response(302) + self.send_header("Location", location) + self.send_header("Content-Length", "0") + self.end_headers() + + def _html(self, code, body_html): + page = ( + "" + "PasteThePlan Proxy" + "" + "" + body_html + "" + ) + enc = page.encode("utf-8") + self.send_response(code) + self.send_header("Content-Type", "text/html; charset=utf-8") + self.send_header("Content-Length", str(len(enc))) + self.end_headers() + self.wfile.write(enc) + + def log_message(self, fmt, *args): # suppress default stderr noise + print(f"[PTP proxy] {self.address_string()} – {fmt % args}") + + +if __name__ == "__main__": + server = http.server.HTTPServer(("localhost", PORT), PTPHandler) + print(f"PasteThePlan proxy → http://localhost:{PORT}/pastetheplan-bridge.html") + print("Press Ctrl+C to stop.\n") + try: + server.serve_forever() + except KeyboardInterrupt: + print("\nStopped.") + diff --git a/sql_exporter/sql_exporter.yml b/sql_exporter/sql_exporter.yml new file mode 100644 index 0000000..3ca522b --- /dev/null +++ b/sql_exporter/sql_exporter.yml @@ -0,0 +1,63 @@ +# Global defaults. +global: + # If scrape_timeout <= 0, no timeout is set unless Prometheus provides one. The default is 10s. + scrape_timeout: 120s + # Subtracted from Prometheus' scrape_timeout to give us some headroom and prevent Prometheus from timing out first. + scrape_timeout_offset: 500ms + # Minimum interval between collector runs: by default (0s) collectors are executed on every scrape. + min_interval: 0s + # Maximum number of open connections to any one target. Metric queries will run concurrently on multiple connections, + # as will concurrent scrapes. + max_connections: 4 + # Maximum number of idle connections to any one target. Unless you use very long collection intervals, this should + # always be the same as max_connections. + max_idle_connections: 3 + # Maximum number of maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. + # If 0, connections are not closed due to a connection's age. + max_connection_lifetime: 5m + +# Multiple target groups. +jobs: + - job_name: mssql_common + collectors: + - mssql_dba_cached + - mssql_dba_regular + - mssql_dba_stableinfo + - mssql_standard + static_configs: + - targets: + localhost: 'sqlserver://localhost:1433/master?trusted_connection=yes&encrypt=true&TrustServerCertificate=true' + + - job_name: mssql_ag + collectors: + - mssql_dba_aghealth + static_configs: + - targets: + localhost: 'sqlserver://localhost:1433/master?trusted_connection=yes&encrypt=true&TrustServerCertificate=true' + + - job_name: mssql_long_running + collectors: + - mssql_dba_whoisactive + static_configs: + - targets: + localhost: 'sqlserver://localhost:1433/master?trusted_connection=yes&encrypt=true&TrustServerCertificate=true' + + - job_name: mssql_msdb + collectors: + - mssql_sqlagent_jobs + - mssql_backup_history + static_configs: + - targets: + localhost: 'sqlserver://localhost:1433/master?trusted_connection=yes&encrypt=true&TrustServerCertificate=true' + + - job_name: mssql_xevent + collectors: + - mssql_xevent + static_configs: + - targets: + localhost: 'sqlserver://localhost:1433/master?trusted_connection=yes&encrypt=true&TrustServerCertificate=true' + +# Collector files specifies a list of globs. One collector definition is read from each matching file. +# Glob patterns are supported (see for syntax). +collector_files: + - "*.collector.yml" diff --git a/sql_exporter/windows_exporter_config.yml b/sql_exporter/windows_exporter_config.yml new file mode 100644 index 0000000..77bb3b6 --- /dev/null +++ b/sql_exporter/windows_exporter_config.yml @@ -0,0 +1,10 @@ +collectors: + enabled: cpu,net,service,diskdrive,logical_disk,memory,mssql,os,physical_disk,service,system,process + #mssql,os,physical_disk,service,system +collector: + service: + include: windows_exporter +log: + level: warn +web: + listen-address: ":9182" \ No newline at end of file