diff --git a/Presentations/Defeating Parameter Sniffing/00 Demo.sql b/Presentations/Defeating Parameter Sniffing/00 Demo.sql index d549286d..472d90a1 100644 --- a/Presentations/Defeating Parameter Sniffing/00 Demo.sql +++ b/Presentations/Defeating Parameter Sniffing/00 Demo.sql @@ -635,7 +635,7 @@ GO /* This can be a parameter, depending on how you use it -https://www.erikdarlingdata.com/sql-server/yet-another-post-about-local-variables/ +https://www.erikdarling.com/sql-server/yet-another-post-about-local-variables/ */ DECLARE @@ -1634,8 +1634,8 @@ What we learned: /* - * W: www.erikdarlingdata.com - * E: erik@erikdarlingdata.com + * W: www.erikdarling.com + * E: erik@erikdarling.com * T: @erikdarlingdata Demo database: diff --git a/README.md b/README.md index e403f4d4..72f24903 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ All you need to do is hit F5 to get information about: * Any current THREADPOOL waits (best observed with the DAC) * Currently executing queries, along with other execution details -For a video walkthrough of the script and the results, [head over here](https://www.erikdarlingdata.com/sp_pressuredetector/). +For a video walkthrough of the script and the results, [head over here](https://www.erikdarling.com/sp_pressuredetector/). Current valid parameter details: @@ -100,7 +100,7 @@ Misuse of this procedure can harm performance. Be very careful about introducing More resources: * For a video walkthrough of the procedure, code, etc. there's a [YouTube playlist here](https://www.youtube.com/playlist?list=PLt4QZ-7lfQifgpvqsa21WLt-u2tZlyoC_). - * For a text-based adventure, head to [my site here](https://www.erikdarlingdata.com/sp_humanevents/). + * For a text-based adventure, head to [my site here](https://www.erikdarling.com/sp_humanevents/). Current valid parameter details: | parameter | name | description | valid_inputs | defaults | @@ -248,7 +248,7 @@ More examples can be found here: [Examples](https://github.com/erikdarlingdata/D More resources: * For a video walkthrough of the procedure, code, etc. there's a [YouTube playlist here](https://www.youtube.com/playlist?list=PLt4QZ-7lfQie1XZHEm0HN-Zt1S7LFEx1P). - * For a text-based adventure, head to [my site here](https://www.erikdarlingdata.com/sp_quickiestore/). + * For a text-based adventure, head to [my site here](https://www.erikdarling.com/sp_quickiestore/). Current valid parameter details: diff --git a/sp_HealthParser/sp_HealthParser.sql b/sp_HealthParser/sp_HealthParser.sql index 41ade443..fe7ccdcd 100644 --- a/sp_HealthParser/sp_HealthParser.sql +++ b/sp_HealthParser/sp_HealthParser.sql @@ -348,8 +348,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONVERT(nchar(1), @azure), @mi_msg = CONVERT(nchar(1), @mi), - @timestamp_utc_mode = - CASE + @timestamp_utc_mode = + CASE WHEN EXISTS ( SELECT @@ -369,7 +369,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. SERVERPROPERTY('PRODUCTVERSION') ), 4 - ) > 17 + ) > 16 THEN 1 ELSE 0 END + @@ -451,10 +451,10 @@ AND ca.utc_timestamp < @end_date'; IF @timestamp_utc_mode = 1 SET @time_filter = N' - AND CONVERT(datetimeoffset(7), fx.timestamp_utc) BETWEEN @start_date AND @end_date'; + AND CONVERT(datetimeoffset(7), fx.timestamp_utc) BETWEEN @start_date AND @end_date'; ELSE SET @time_filter = ' - AND fx.timestamp_utc BETWEEN @start_date AND @end_date'; + AND fx.timestamp_utc BETWEEN @start_date AND @end_date'; END; SET @sql_template = diff --git a/sp_HumanEvents/README.md b/sp_HumanEvents/README.md index 27591218..e28150c4 100644 --- a/sp_HumanEvents/README.md +++ b/sp_HumanEvents/README.md @@ -112,7 +112,7 @@ EXECUTE dbo.sp_HumanEvents ## Resources * [YouTube playlist](https://www.youtube.com/playlist?list=PLt4QZ-7lfQifgpvqsa21WLt-u2tZlyoC_) -* [Blog post](https://www.erikdarlingdata.com/sp_humanevents/) +* [Blog post](https://www.erikdarling.com/sp_humanevents/) --- diff --git a/sp_HumanEvents/sp_HumanEvents.sql b/sp_HumanEvents/sp_HumanEvents.sql index f370a0c6..e2fda99f 100644 --- a/sp_HumanEvents/sp_HumanEvents.sql +++ b/sp_HumanEvents/sp_HumanEvents.sql @@ -98,7 +98,7 @@ BEGIN SELECT N'misuse of this procedure can harm performance' UNION ALL SELECT N'be very careful about introducing observer overhead, especially when gathering query plans' UNION ALL SELECT N'be even more careful when setting up permanent sessions!' UNION ALL - SELECT N'for additional support: https://github.com/erikdarlingdata/DarlingData/tree/main/sp_HumanEvents' UNION ALL + SELECT N'for additional support: https://code.erikdarling.com' UNION ALL SELECT N'from your loving sql server consultant, erik darling: https://erikdarling.com'; diff --git a/sp_HumanEvents/sp_HumanEventsBlockViewer.sql b/sp_HumanEvents/sp_HumanEventsBlockViewer.sql index 479c0ccb..1792e3c8 100644 --- a/sp_HumanEvents/sp_HumanEventsBlockViewer.sql +++ b/sp_HumanEvents/sp_HumanEventsBlockViewer.sql @@ -105,7 +105,7 @@ BEGIN SELECT 'it will also work with any other extended event session that captures blocking' UNION ALL SELECT 'just use the @session_name parameter to point me there' UNION ALL SELECT 'EXECUTE dbo.sp_HumanEventsBlockViewer @session_name = N''blocked_process_report'';' UNION ALL - SELECT 'all scripts and documentation are available here: https://github.com/erikdarlingdata/DarlingData/tree/main/sp_HumanEvents' UNION ALL + SELECT 'all scripts and documentation are available here: https://code.erikdarling.com' UNION ALL SELECT 'from your loving sql server consultant, erik darling: https://erikdarling.com'; SELECT @@ -2391,10 +2391,8 @@ FROM FROM #blocks AS b ) AS b WHERE b.n = 1 -AND (b.contentious_object = @object_name - OR @object_name IS NULL) - -'; +AND (b.contentious_object = @object_name + OR @object_name IS NULL)'; /* Add the WHERE clause only for table logging */ IF @log_to_table = 1 diff --git a/sp_IndexCleanup/.DS_Store b/sp_IndexCleanup/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/sp_IndexCleanup/.DS_Store differ diff --git a/sp_LogHunter/sp_LogHunter.sql b/sp_LogHunter/sp_LogHunter.sql index 5ca6c883..5b920589 100644 --- a/sp_LogHunter/sp_LogHunter.sql +++ b/sp_LogHunter/sp_LogHunter.sql @@ -81,7 +81,7 @@ BEGIN introduction = 'hi, i''m sp_LogHunter!' UNION ALL SELECT 'you can use me to look through your error logs for bad stuff' UNION ALL - SELECT 'all scripts and documentation are available here: https://github.com/erikdarlingdata/DarlingData/tree/main/sp_LogHunter' UNION ALL + SELECT 'all scripts and documentation are available here: https://code.erikdarling.com' UNION ALL SELECT 'from your loving sql server consultant, erik darling: https://erikdarling.com'; SELECT diff --git a/sp_PressureDetector/README.md b/sp_PressureDetector/README.md index 14f39294..3d3b94ff 100644 --- a/sp_PressureDetector/README.md +++ b/sp_PressureDetector/README.md @@ -69,4 +69,4 @@ EXECUTE dbo.sp_PressureDetector ``` ## Resources -* [Video walkthrough](https://www.erikdarlingdata.com/sp_pressuredetector/) \ No newline at end of file +* [Video walkthrough](https://www.erikdarling.com/sp_pressuredetector/) \ No newline at end of file diff --git a/sp_PressureDetector/sp_PressureDetector.sql b/sp_PressureDetector/sp_PressureDetector.sql index b838457b..e7fe2a2f 100644 --- a/sp_PressureDetector/sp_PressureDetector.sql +++ b/sp_PressureDetector/sp_PressureDetector.sql @@ -88,7 +88,7 @@ BEGIN SELECT introduction = 'hi, i''m sp_PressureDetector!' UNION ALL - SELECT 'you got me from https://github.com/erikdarlingdata/DarlingData/tree/main/sp_PressureDetector' UNION ALL + SELECT 'you got me from https://code.erikdarling.com' UNION ALL SELECT 'i''m a lightweight tool for monitoring cpu and memory pressure' UNION ALL SELECT 'i''ll tell you: ' UNION ALL SELECT ' * what''s currently consuming memory on your server' UNION ALL @@ -445,6 +445,7 @@ OPTION(MAXDOP 1, RECOMPILE);', @log_table_cpu_queries sysname, @log_table_cpu_events sysname, @cleanup_date datetime2(7), + @max_sample_time datetime, @check_sql nvarchar(max) = N'', @create_sql nvarchar(max) = N'', @insert_sql nvarchar(max) = N'', @@ -453,12 +454,13 @@ OPTION(MAXDOP 1, RECOMPILE);', /* Validate logging parameters */ IF @log_to_table = 1 - BEGIN - /* Default database name to current database if not specified */ - SELECT @log_database_name = ISNULL(@log_database_name, DB_NAME()); - - /* Default schema name to dbo if not specified */ - SELECT @log_schema_name = ISNULL(@log_schema_name, N'dbo'); + BEGIN + + SELECT + /* Default database name to current database if not specified */ + @log_database_name = ISNULL(@log_database_name, DB_NAME()), + /* Default schema name to dbo if not specified */ + @log_schema_name = ISNULL(@log_schema_name, N'dbo'); /* Validate database exists */ IF NOT EXISTS @@ -473,8 +475,8 @@ OPTION(MAXDOP 1, RECOMPILE);', RETURN; END; - SET - @log_database_schema = + SELECT + @log_database_schema = QUOTENAME(@log_database_name) + N'.' + QUOTENAME(@log_schema_name) + @@ -560,8 +562,6 @@ OPTION(MAXDOP 1, RECOMPILE);', avg_ms_per_wait decimal(38,2) NULL, percent_signal_waits decimal(38,2) NULL, waiting_tasks_count bigint NULL, - sample_time datetime NULL, - sorting bigint NULL, PRIMARY KEY CLUSTERED (collection_time, id) ); IF @debug = 1 BEGIN RAISERROR(''Created table %s for wait stats logging.'', 0, 1, ''' + @log_table_waits + N''') WITH NOWAIT; END; @@ -607,7 +607,6 @@ OPTION(MAXDOP 1, RECOMPILE);', avg_write_stall_ms decimal(38,2) NULL, io_stall_read_ms bigint NULL, io_stall_write_ms bigint NULL, - sample_time datetime NULL, PRIMARY KEY CLUSTERED (collection_time, id) ); IF @debug = 1 BEGIN RAISERROR(''Created table %s for file metrics logging.'', 0, 1, ''' + @log_table_file_metrics + N''') WITH NOWAIT; END; @@ -638,7 +637,6 @@ OPTION(MAXDOP 1, RECOMPILE);', ( id bigint IDENTITY, collection_time datetime2(7) NOT NULL DEFAULT SYSDATETIME(), - sample_time datetime NULL, object_name sysname NOT NULL, counter_name sysname NOT NULL, counter_name_clean sysname NULL, @@ -1509,28 +1507,24 @@ OPTION(MAXDOP 1, RECOMPILE);', SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; INSERT INTO ' + @log_table_waits + N' ( - hours_uptime, - hours_cpu_time, - wait_type, - description, - hours_wait_time, - avg_ms_per_wait, - percent_signal_waits, - waiting_tasks_count, - sample_time, - sorting + hours_uptime, + hours_cpu_time, + wait_type, + description, + hours_wait_time, + avg_ms_per_wait, + percent_signal_waits, + waiting_tasks_count ) - SELECT - w.hours_uptime, - w.hours_cpu_time, - w.wait_type, - w.description, - w.hours_wait_time, - w.avg_ms_per_wait, - w.percent_signal_waits, - w.waiting_tasks_count_n, - w.sample_time, - w.sorting + SELECT + w.hours_uptime, + w.hours_cpu_time, + w.wait_type, + w.description, + w.hours_wait_time, + w.avg_ms_per_wait, + w.percent_signal_waits, + w.waiting_tasks_count_n FROM #waits AS w; '; @@ -2023,40 +2017,38 @@ OPTION(MAXDOP 1, RECOMPILE);', SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; INSERT INTO ' + @log_table_file_metrics + N' ( - hours_uptime, - drive, - database_name, - database_file_details, - file_size_gb, - total_gb_read, - total_mb_read, - total_read_count, - avg_read_stall_ms, - total_gb_written, - total_mb_written, - total_write_count, - avg_write_stall_ms, - io_stall_read_ms, - io_stall_write_ms, - sample_time + hours_uptime, + drive, + database_name, + database_file_details, + file_size_gb, + total_gb_read, + total_mb_read, + total_read_count, + avg_read_stall_ms, + total_gb_written, + total_mb_written, + total_write_count, + avg_write_stall_ms, + io_stall_read_ms, + io_stall_write_ms ) - SELECT - fm.hours_uptime, - fm.drive, - fm.database_name, - fm.database_file_details, - fm.file_size_gb, - fm.total_gb_read, - fm.total_mb_read, - fm.total_read_count, - fm.avg_read_stall_ms, - fm.total_gb_written, - fm.total_mb_written, - fm.total_write_count, - fm.avg_write_stall_ms, - fm.io_stall_read_ms, - fm.io_stall_write_ms, - fm.sample_time + SELECT + fm.hours_uptime, + fm.drive, + fm.database_name, + fm.database_file_details, + fm.file_size_gb, + fm.total_gb_read, + fm.total_mb_read, + fm.total_read_count, + fm.avg_read_stall_ms, + fm.total_gb_written, + fm.total_mb_written, + fm.total_write_count, + fm.avg_write_stall_ms, + fm.io_stall_read_ms, + fm.io_stall_write_ms FROM #file_metrics AS fm; '; @@ -2276,16 +2268,14 @@ OPTION(MAXDOP 1, RECOMPILE);', SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; INSERT INTO ' + @log_table_perfmon + N' ( - sample_time, - object_name, - counter_name, - counter_name_clean, - instance_name, - cntr_value, + object_name, + counter_name, + counter_name_clean, + instance_name, + cntr_value, cntr_type ) SELECT - dopc.sample_time, dopc.object_name, dopc.counter_name, dopc.counter_name_clean, @@ -3545,6 +3535,28 @@ OPTION(MAXDOP 1, RECOMPILE);', END; IF @log_to_table = 1 BEGIN + /* Get the maximum sample_time from the CPU events table */ + SET @insert_sql = N' + SELECT + @max_sample_time_out = + ISNULL + ( + MAX(sample_time), + ''19000101'' + ) + FROM ' + @log_table_cpu_events + N' + OPTION(RECOMPILE);'; + + IF @debug = 1 + BEGIN + PRINT @insert_sql; + END; + + EXECUTE sys.sp_executesql + @insert_sql, + N'@max_sample_time_out datetime OUTPUT', + @max_sample_time OUTPUT; + SET @insert_sql = N' SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; INSERT INTO ' + @log_table_cpu_events + N' @@ -3559,17 +3571,20 @@ OPTION(MAXDOP 1, RECOMPILE);', sqlserver_cpu_utilization = event.value(''(./sqlserver_cpu_utilization)[1]'', ''integer''), other_process_cpu_utilization = event.value(''(./other_process_cpu_utilization)[1]'', ''integer''), total_cpu_utilization = event.value(''(./total_cpu_utilization)[1]'', ''integer'') - FROM @cpu_utilization.nodes(''/cpu_utilization'') AS cpu(event);'; + FROM @cpu_utilization.nodes(''/cpu_utilization'') AS cpu(event) + WHERE event.exist(''(./sample_time)[. > sql:variable("@max_sample_time")]'') = 1;'; IF @debug = 1 BEGIN PRINT @insert_sql; END; - - EXECUTE sys.sp_executesql - @insert_sql, - N'@cpu_utilization xml', - @cpu_utilization; + + EXECUTE sys.sp_executesql + @insert_sql, + N'@cpu_utilization xml, + @max_sample_time datetime', + @cpu_utilization, + @max_sample_time; END; /*Thread usage*/ diff --git a/sp_QuickieStore/README.md b/sp_QuickieStore/README.md index 82d60123..c7b73023 100644 --- a/sp_QuickieStore/README.md +++ b/sp_QuickieStore/README.md @@ -129,4 +129,4 @@ EXECUTE dbo.sp_QuickieStore ## Resources * [YouTube playlist](https://www.youtube.com/playlist?list=PLt4QZ-7lfQie1XZHEm0HN-Zt1S7LFEx1P) -* [Blog post](https://www.erikdarlingdata.com/sp_quickiestore/) \ No newline at end of file +* [Blog post](https://www.erikdarling.com/sp_quickiestore/) \ No newline at end of file diff --git a/sp_QuickieStore/sp_QuickieStore.sql b/sp_QuickieStore/sp_QuickieStore.sql index 08503b20..bab9de23 100644 --- a/sp_QuickieStore/sp_QuickieStore.sql +++ b/sp_QuickieStore/sp_QuickieStore.sql @@ -153,7 +153,7 @@ BEGIN SELECT introduction = 'hi, i''m sp_QuickieStore!' UNION ALL - SELECT 'you got me from https://github.com/erikdarlingdata/DarlingData/tree/main/sp_QuickieStore' UNION ALL + SELECT 'you got me from https://code.erikdarling.com' UNION ALL SELECT 'i can be used to quickly grab misbehaving queries from query store' UNION ALL SELECT 'the plan analysis is up to you; there will not be any XML shredding here' UNION ALL SELECT 'so what can you do, and how do you do it? read below!' UNION ALL @@ -848,12 +848,12 @@ CREATE TABLE database_id integer NOT NULL, plan_id bigint NOT NULL, query_id bigint NOT NULL, - all_plan_ids varchar(MAX), + all_plan_ids varchar(max), plan_group_id bigint NULL, engine_version nvarchar(32) NULL, compatibility_level smallint NOT NULL, query_plan_hash binary(8) NOT NULL, - query_plan nvarchar(MAX) NULL, + query_plan nvarchar(max) NULL, is_online_index_plan bit NOT NULL, is_trivial_plan bit NOT NULL, is_parallel_plan bit NOT NULL, @@ -1163,7 +1163,7 @@ CREATE TABLE plan_feedback_id bigint NOT NULL, plan_id bigint NULL, feature_desc nvarchar(120) NULL, - feedback_data nvarchar(MAX) NULL, + feedback_data nvarchar(max) NULL, state_desc nvarchar(120) NULL, create_time datetimeoffset(7) NOT NULL, last_updated_time datetimeoffset(7) NULL @@ -1178,7 +1178,7 @@ CREATE TABLE database_id integer NOT NULL, query_hint_id bigint NOT NULL, query_id bigint NOT NULL, - query_hint_text nvarchar(MAX) NULL, + query_hint_text nvarchar(max) NULL, last_query_hint_failure_reason_desc nvarchar(256) NULL, query_hint_failure_count bigint NOT NULL, source_desc nvarchar(256) NULL @@ -1208,6 +1208,27 @@ CREATE TABLE replica_name nvarchar(1288) NULL ); +/*Gonna try gathering this based on*/ +CREATE TABLE + #query_hash_totals +( + database_id integer NOT NULL, + query_hash binary(8) NOT NULL, + total_executions bigint NOT NULL, + total_duration_ms decimal(19,2) NOT NULL, + total_cpu_time_ms decimal(19,2) NOT NULL, + total_logical_reads_mb decimal(19,2) NOT NULL, + total_physical_reads_mb decimal(19,2) NOT NULL, + total_logical_writes_mb decimal(19,2) NOT NULL, + total_clr_time_ms decimal(19,2) NOT NULL, + total_memory_mb decimal(19,2) NOT NULL, + total_rowcount decimal(19,2) NOT NULL, + total_num_physical_io_reads decimal(19,2) NULL, + total_log_bytes_used_mb decimal(19,2) NULL, + total_tempdb_space_used_mb decimal(19,2) NULL, + PRIMARY KEY CLUSTERED(query_hash, database_id) +); + /* Location, location, location */ @@ -1298,7 +1319,7 @@ DECLARE metric_group nvarchar(50) NOT NULL, /* Grouping (duration, cpu, etc.) */ metric_type nvarchar(20) NOT NULL, /* Type within group (avg, total, last, min, max) */ column_name nvarchar(100) NOT NULL, /* Column name as it appears in output */ - column_source nvarchar(MAX) NOT NULL, /* Source expression or formula */ + column_source nvarchar(max) NOT NULL, /* Source expression or formula */ is_conditional bit NOT NULL, /* Is this a conditional column (depends on a parameter) */ condition_param nvarchar(50) NULL, /* Parameter name this column depends on */ condition_value sql_variant NULL, /* Value the parameter must have */ @@ -1416,7 +1437,7 @@ VALUES (1230, 'num_physical_io_reads', 'min', 'min_num_physical_io_reads_mb', 'qsrs.min_num_physical_io_reads_mb', 1, 'new', 1, 1, 'N0'), (1240, 'num_physical_io_reads', 'max', 'max_num_physical_io_reads_mb', 'qsrs.max_num_physical_io_reads_mb', 1, 'new', 1, 0, 'N0'), /* Hash totals for new physical IO reads */ - (1215, 'num_physical_io_reads', 'total_hash', 'total_num_physical_io_reads_mb_by_query_hash', 'SUM(qsrs.total_num_physical_io_reads_mb) OVER (PARTITION BY qsq.query_hash ORDER BY qsq.query_hash)', 1, 'new_with_hash_totals', 1, 0, 'N0'), + (1215, 'num_physical_io_reads', 'total_hash', 'total_num_physical_io_reads_mb_by_query_hash', 'qht.total_num_physical_io_reads', 1, 'new_with_hash_totals', 1, 0, 'N0'), /* Finish adding the remaining columns (log bytes and tempdb usage) */ /* Log bytes used */ (1300, 'log_bytes', 'avg', 'avg_log_bytes_used_mb', 'qsrs.avg_log_bytes_used_mb', 1, 'new', 1, 0, 'N0'), @@ -1425,7 +1446,7 @@ VALUES (1330, 'log_bytes', 'min', 'min_log_bytes_used_mb', 'qsrs.min_log_bytes_used_mb', 1, 'new', 1, 1, 'N0'), (1340, 'log_bytes', 'max', 'max_log_bytes_used_mb', 'qsrs.max_log_bytes_used_mb', 1, 'new', 1, 0, 'N0'), /* Hash totals for log bytes */ - (1315, 'log_bytes', 'total_hash', 'total_log_bytes_used_mb_by_query_hash', 'SUM(qsrs.total_log_bytes_used_mb) OVER (PARTITION BY qsq.query_hash ORDER BY qsq.query_hash)', 1, 'new_with_hash_totals', 1, 0, 'N0'), + (1315, 'log_bytes', 'total_hash', 'total_log_bytes_used_mb_by_query_hash', 'qht.total_log_bytes_used_mb', 1, 'new_with_hash_totals', 1, 0, 'N0'), /* TempDB usage */ (1400, 'tempdb', 'avg', 'avg_tempdb_space_used_mb', 'qsrs.avg_tempdb_space_used_mb', 1, 'new', 1, 0, 'N0'), (1410, 'tempdb', 'total', 'total_tempdb_space_used_mb', 'qsrs.total_tempdb_space_used_mb', 1, 'new', 1, 0, 'N0'), @@ -1433,7 +1454,7 @@ VALUES (1430, 'tempdb', 'min', 'min_tempdb_space_used_mb', 'qsrs.min_tempdb_space_used_mb', 1, 'new', 1, 1, 'N0'), (1440, 'tempdb', 'max', 'max_tempdb_space_used_mb', 'qsrs.max_tempdb_space_used_mb', 1, 'new', 1, 0, 'N0'), /* Hash totals for tempdb */ - (1415, 'tempdb', 'total_hash', 'total_tempdb_space_used_mb_by_query_hash', 'SUM(qsrs.total_tempdb_space_used_mb) OVER (PARTITION BY qsq.query_hash ORDER BY qsq.query_hash)', 1, 'new_with_hash_totals', 1, 0, 'N0'), + (1415, 'tempdb', 'total_hash', 'total_tempdb_space_used_mb_by_query_hash', 'qht.total_tempdb_space_used_mb', 1, 'new_with_hash_totals', 1, 0, 'N0'), /* Context settings and sorting columns */ (1500, 'metadata', 'context', 'context_settings', 'qsrs.context_settings', 0, NULL, NULL, 0, NULL); @@ -1636,13 +1657,13 @@ DECLARE @procedure_name_quoted nvarchar(1024), @collation sysname, @new bit, - @sql nvarchar(MAX), - @isolation_level nvarchar(MAX), + @sql nvarchar(max), + @isolation_level nvarchar(max), @parameters nvarchar(4000), @plans_top bigint, @queries_top bigint, @nc10 nvarchar(2), - @where_clause nvarchar(MAX), + @where_clause nvarchar(max), @query_text_search_original_value nvarchar(4000), @query_text_search_not_original_value nvarchar(4000), @procedure_exists bit, @@ -1654,9 +1675,9 @@ DECLARE @string_split_ints nvarchar(1500), @string_split_strings nvarchar(1500), @current_table nvarchar(100), - @troubleshoot_insert nvarchar(MAX), - @troubleshoot_update nvarchar(MAX), - @troubleshoot_info nvarchar(MAX), + @troubleshoot_insert nvarchar(max), + @troubleshoot_update nvarchar(max), + @troubleshoot_info nvarchar(max), @rc bigint, @em tinyint, @fo tinyint, @@ -1671,16 +1692,16 @@ DECLARE @regression_baseline_start_date_original datetimeoffset(7), @regression_baseline_end_date_original datetimeoffset(7), @regression_mode bit, - @regression_where_clause nvarchar(MAX), - @column_sql nvarchar(MAX), - @param_name nvarchar(100), - @param_value nvarchar(4000), - @temp_table sysname, - @column_name sysname, - @data_type sysname, + @regression_where_clause nvarchar(max), + @column_sql nvarchar(max), + @param_name nvarchar(100), + @param_value nvarchar(4000), + @temp_table sysname, + @column_name sysname, + @data_type sysname, @is_include bit, @requires_secondary_processing bit, - @split_sql nvarchar(MAX), + @split_sql nvarchar(max), @error_msg nvarchar(2000), @conflict_list nvarchar(max) = N''; @@ -3442,24 +3463,6 @@ BEGIN @sort_order_is_a_wait = 1; END; -/* -These columns are only available in 2017+ -*/ -IF -( - ( - @sort_order = 'tempdb' - OR @sort_order_is_a_wait = 1 - ) - AND @new = 0 -) -BEGIN - RAISERROR('The sort order (%s) you chose is invalid in product version %i, reverting to cpu', 10, 1, @sort_order, @product_version) WITH NOWAIT; - - SELECT - @sort_order = N'cpu'; -END; - /* See if our cool new 2022 views exist. May have to tweak this if views aren't present in some cloudy situations. @@ -3653,6 +3656,28 @@ OPTION(RECOMPILE);' + @nc10; END; END; /*End wait stats checks*/ +/* +These columns are only available in 2017+ +*/ +IF +( + ( + @sort_order = 'tempdb' + OR @sort_order_is_a_wait = 1 + ) + AND + ( + @new = 0 + OR @query_store_waits_enabled = 0 + ) +) +BEGIN + RAISERROR('The sort order (%s) you chose is invalid in product version %i, reverting to cpu', 10, 1, @sort_order, @product_version) WITH NOWAIT; + + SELECT + @sort_order = N'cpu'; +END; + /*Check that the selected @timezone is valid*/ IF @timezone IS NOT NULL BEGIN @@ -4109,9 +4134,9 @@ BEGIN END; /* Execute the dynamic SQL to populate the temporary table */ - DECLARE @dynamic_sql nvarchar(MAX) = N' - INSERT INTO - ' + @temp_table + N' + DECLARE @dynamic_sql nvarchar(max) = N' + INSERT INTO + ' + @temp_table + N' WITH (TABLOCK) ( @@ -4153,10 +4178,10 @@ BEGIN @current_table = 'inserting #include_plan_ids for ' + @param_name; /* Build appropriate SQL based on parameter type */ - DECLARE - @secondary_sql nvarchar(MAX) = N''; - - IF @param_name = 'include_query_ids' + DECLARE + @secondary_sql nvarchar(max) = N''; + + IF @param_name = 'include_query_ids' OR @param_name = 'ignore_query_ids' BEGIN SELECT @secondary_sql = N' @@ -6996,7 +7021,20 @@ BEGIN SUM(qsrs.count_executions * (qsrs.avg_logical_io_writes * 8.)) / 1024., SUM(qsrs.count_executions * qsrs.avg_clr_time) / 1000., SUM(qsrs.count_executions * (qsrs.avg_query_max_used_memory * 8.)) / 1024., - SUM(qsrs.count_executions * qsrs.avg_rowcount) + SUM(qsrs.count_executions * qsrs.avg_rowcount)' + + CASE + @new + WHEN 1 + THEN N', + SUM(qsrs.count_executions * (qsrs.avg_num_physical_io_reads * 8)) / 1024., + SUM(qsrs.count_executions * qsrs.avg_log_bytes_used) / 100000000., + SUM(qsrs.count_executions * (qsrs.avg_tempdb_space_used * 8)) / 1024.' + ELSE N' + NULL, + NULL, + NULL' + END + + N' FROM ' + @database_name_quoted + N'.sys.query_store_runtime_stats AS qsrs JOIN ' + @database_name_quoted + N'.sys.query_store_plan AS qsp ON qsrs.plan_id = qsp.plan_id @@ -7035,7 +7073,10 @@ BEGIN total_logical_writes_mb, total_clr_time_ms, total_memory_mb, - total_rowcount + total_rowcount, + total_num_physical_io_reads, + total_log_bytes_used_mb, + total_tempdb_space_used_mb ) EXECUTE sys.sp_executesql @sql, @@ -8236,7 +8277,7 @@ BEGIN @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' SELECT x.* @@ -8339,8 +8380,13 @@ FROM THEN @regression_mode WHEN cd.condition_param = N'include_query_hash_totals' THEN @include_query_hash_totals - WHEN cd.condition_param = N'new_with_hash_totals' - THEN CASE WHEN @new = 1 AND @include_query_hash_totals = 1 THEN 1 ELSE 0 END + WHEN cd.condition_param = N'new_with_hash_totals' + THEN CASE + WHEN @new = 1 + AND @include_query_hash_totals = 1 + THEN 1 + ELSE 0 + END ELSE 0 END = cd.condition_value ) @@ -8375,7 +8421,7 @@ FROM @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' FROM #query_store_runtime_stats AS qsrs' ); @@ -8422,7 +8468,7 @@ SELECT @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' CROSS APPLY ( @@ -8483,7 +8529,7 @@ SELECT @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' CROSS APPLY ( @@ -8559,7 +8605,7 @@ SELECT @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' ) AS x ' + CASE WHEN @regression_mode = 1 THEN N'' ELSE N'WHERE x.n = 1 ' END @@ -8760,7 +8806,7 @@ BEGIN EXECUTE sys.sp_executesql @sql, - N'@timezone sysname, @utc_offset_string nvarchar(max)', + N'@timezone sysname, @utc_offset_string nvarchar(6)', @timezone, @utc_offset_string; END; ELSE IF @only_queries_with_feedback = 1 @@ -9157,7 +9203,7 @@ BEGIN EXECUTE sys.sp_executesql @sql, - N'@timezone sysname, @utc_offset_string nvarchar(max)', + N'@timezone sysname, @utc_offset_string nvarchar(6)', @timezone, @utc_offset_string; END; /*End compilation query section*/ @@ -9180,7 +9226,7 @@ BEGIN @sql = CONVERT ( - nvarchar(MAX), + nvarchar(max), N' SELECT source = @@ -9406,7 +9452,7 @@ BEGIN @sql = CONVERT ( - nvarchar(MAX), + nvarchar(max), N' SELECT DISTINCT source = @@ -9546,7 +9592,7 @@ BEGIN @sql = CONVERT ( - nvarchar(MAX), + nvarchar(max), N' SELECT source = @@ -9709,7 +9755,7 @@ BEGIN @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' SELECT source = @@ -9757,7 +9803,7 @@ BEGIN @sql += CONVERT ( - nvarchar(MAX), + nvarchar(max), N' dqso.size_based_cleanup_mode_desc FROM #database_query_store_options AS dqso @@ -9957,9 +10003,9 @@ BEGIN XML PATH(''), TYPE - ).value('.', 'nvarchar(MAX)'), - 1, - 2, + ).value('.', 'nvarchar(max)'), + 1, + 2, N'' ), N'None' diff --git a/sp_WhoIsActive Logging/04 sp_WhoIsActive Logging Agent Job.sql b/sp_WhoIsActive Logging/04 sp_WhoIsActive Logging Agent Job.sql index 3fa8bda6..27364e0f 100644 --- a/sp_WhoIsActive Logging/04 sp_WhoIsActive Logging Agent Job.sql +++ b/sp_WhoIsActive Logging/04 sp_WhoIsActive Logging Agent Job.sql @@ -84,7 +84,7 @@ EXECUTE @ReturnCode = msdb.dbo.sp_add_job @notify_level_page = 0, @delete_level = 0, @description = N'Copyright 2022 Darling Data, LLC -https://www.erikdarlingdata.com/ +https://www.erikdarling.com/ This will log sp_WhoIsActive to a table. It will create a new table every day,