Files
sql-scripts/EXPLOIT - quicky store.sql

10528 lines
292 KiB
Transact-SQL

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 NUMERIC_ROUNDABORT OFF;
SET IMPLICIT_TRANSACTIONS OFF;
SET STATISTICS TIME, IO OFF;
GO
/*
██████╗ ██╗ ██╗██╗ ██████╗██╗ ██╗██╗███████╗
██╔═══██╗██║ ██║██║██╔════╝██║ ██╔╝██║██╔════╝
██║ ██║██║ ██║██║██║ █████╔╝ ██║█████╗
██║▄▄ ██║██║ ██║██║██║ ██╔═██╗ ██║██╔══╝
╚██████╔╝╚██████╔╝██║╚██████╗██║ ██╗██║███████╗
╚══▀▀═╝ ╚═════╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═╝╚══════╝
███████╗████████╗ ██████╗ ██████╗ ███████╗██╗
██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝██║
███████╗ ██║ ██║ ██║██████╔╝█████╗ ██║
╚════██║ ██║ ██║ ██║██╔══██╗██╔══╝ ╚═╝
███████║ ██║ ╚██████╔╝██║ ██║███████╗██╗
╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝
Copyright 2024 Darling Data, LLC
https://www.erikdarling.com/
For usage and licensing details, run:
EXEC sp_QuickieStore
@help = 1;
For working through errors:
EXEC sp_QuickieStore
@debug = 1;
For performance issues:
EXEC sp_QuickieStore
@troubleshoot_performance = 1;
For support, head over to GitHub:
https://github.com/erikdarlingdata/DarlingData
*/
IF OBJECT_ID('dbo.sp_QuickieStore') IS NULL
BEGIN
EXEC ('CREATE PROCEDURE dbo.sp_QuickieStore AS RETURN 138;');
END;
GO
ALTER PROCEDURE
dbo.sp_QuickieStore
(
@database_name sysname = NULL, /*the name of the database you want to look at query store in*/
@sort_order varchar(20) = 'cpu', /*the runtime metric you want to prioritize results by*/
@top bigint = 10, /*the number of queries you want to pull back*/
@start_date datetimeoffset(7) = NULL, /*the begin date of your search, will be converted to UTC internally*/
@end_date datetimeoffset(7) = NULL, /*the end date of your search, will be converted to UTC internally*/
@timezone sysname = NULL, /*user specified time zone to override dates displayed in results*/
@execution_count bigint = NULL, /*the minimum number of executions a query must have*/
@duration_ms bigint = NULL, /*the minimum duration a query must have to show up in results*/
@execution_type_desc nvarchar(60) = NULL, /*the type of execution you want to filter by (success, failure)*/
@procedure_schema sysname = NULL, /*the schema of the procedure you're searching for*/
@procedure_name sysname = NULL, /*the name of the programmable object you're searching for*/
@include_plan_ids nvarchar(4000) = NULL, /*a list of plan ids to search for*/
@include_query_ids nvarchar(4000) = NULL, /*a list of query ids to search for*/
@include_query_hashes nvarchar(4000) = NULL, /*a list of query hashes to search for*/
@include_plan_hashes nvarchar(4000) = NULL, /*a list of query plan hashes to search for*/
@include_sql_handles nvarchar(4000) = NULL, /*a list of sql handles to search for*/
@ignore_plan_ids nvarchar(4000) = NULL, /*a list of plan ids to ignore*/
@ignore_query_ids nvarchar(4000) = NULL, /*a list of query ids to ignore*/
@ignore_query_hashes nvarchar(4000) = NULL, /*a list of query hashes to ignore*/
@ignore_plan_hashes nvarchar(4000) = NULL, /*a list of query plan hashes to ignore*/
@ignore_sql_handles nvarchar(4000) = NULL, /*a list of sql handles to ignore*/
@query_text_search nvarchar(4000) = NULL, /*query text to search for*/
@query_text_search_not nvarchar(4000) = NULL, /*query text to exclude*/
@escape_brackets bit = 0, /*Set this bit to 1 to search for query text containing square brackets (common in .NET Entity Framework and other ORM queries)*/
@escape_character nchar(1) = N'\', /*Sets the ESCAPE character for special character searches, defaults to the SQL standard backslash (\) character*/
@only_queries_with_hints bit = 0, /*Set this bit to 1 to retrieve only queries with query hints*/
@only_queries_with_feedback bit = 0, /*Set this bit to 1 to retrieve only queries with query feedback*/
@only_queries_with_variants bit = 0, /*Set this bit to 1 to retrieve only queries with query variants*/
@only_queries_with_forced_plans bit = 0, /*Set this bit to 1 to retrieve only queries with forced plans*/
@only_queries_with_forced_plan_failures bit = 0, /*Set this bit to 1 to retrieve only queries with forced plan failures*/
@wait_filter varchar(20) = NULL, /*wait category to search for; category details are below*/
@query_type varchar(11) = NULL, /*filter for only ad hoc queries or only from queries from modules*/
@expert_mode bit = 0, /*returns additional columns and results*/
@hide_help_table bit = 0, /*hides the "bottom table" that shows help and support information*/
@format_output bit = 1, /*returns numbers formatted with commas*/
@get_all_databases bit = 0, /*looks for query store enabled databases and returns combined results from all of them*/
@workdays bit = 0, /*Use this to filter out weekends and after-hours queries*/
@work_start time(0) = '9am', /*Use this to set a specific start of your work days*/
@work_end time(0) = '5pm', /*Use this to set a specific end of your work days*/
@help bit = 0, /*return available parameter details, etc.*/
@debug bit = 0, /*prints dynamic sql, statement length, parameter and variable values, and raw temp table contents*/
@troubleshoot_performance bit = 0, /*set statistics xml on for queries against views*/
@version varchar(30) = NULL OUTPUT, /*OUTPUT; for support*/
@version_date datetime = NULL OUTPUT /*OUTPUT; for support*/
)
WITH RECOMPILE
AS
BEGIN
SET STATISTICS XML OFF;
SET NOCOUNT ON;
SET XACT_ABORT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRY
/*
If this column doesn't exist, you're not on a good version of SQL Server
*/
IF NOT EXISTS
(
SELECT
1/0
FROM sys.all_columns AS ac
WHERE ac.object_id = OBJECT_ID(N'sys.dm_exec_query_stats', N'V')
AND ac.name = N'total_spills'
)
BEGIN
RAISERROR('This procedure only runs on supported versions of SQL Server:
* 2016 SP2+
* 2017 CU3+
* 2019+
* Probably Azure?', 11, 1) WITH NOWAIT;
RETURN;
END;
/*
These are for your outputs.
*/
SELECT
@version = '4.9',
@version_date = '20240915';
/*
Helpful section! For help.
*/
IF @help = 1
BEGIN
/*
Introduction
*/
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 '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
SELECT 'from your loving sql server consultant, erik darling: https://erikdarling.com';
/*
Parameters
*/
SELECT
parameter_name =
ap.name,
data_type = t.name,
description =
CASE
ap.name
WHEN N'@database_name' THEN 'the name of the database you want to look at query store in'
WHEN N'@sort_order' THEN 'the runtime metric you want to prioritize results by'
WHEN N'@top' THEN 'the number of queries you want to pull back'
WHEN N'@start_date' THEN 'the begin date of your search, will be converted to UTC internally'
WHEN N'@end_date' THEN 'the end date of your search, will be converted to UTC internally'
WHEN N'@timezone' THEN 'user specified time zone to override dates displayed in results'
WHEN N'@execution_count' THEN 'the minimum number of executions a query must have'
WHEN N'@duration_ms' THEN 'the minimum duration a query must have to show up in results'
WHEN N'@execution_type_desc' THEN 'the type of execution you want to filter by (success, failure)'
WHEN N'@procedure_schema' THEN 'the schema of the procedure you''re searching for'
WHEN N'@procedure_name' THEN 'the name of the programmable object you''re searching for'
WHEN N'@include_plan_ids' THEN 'a list of plan ids to search for'
WHEN N'@include_query_ids' THEN 'a list of query ids to search for'
WHEN N'@include_query_hashes' THEN 'a list of query hashes to search for'
WHEN N'@include_plan_hashes' THEN 'a list of query plan hashes to search for'
WHEN N'@include_sql_handles' THEN 'a list of sql handles to search for'
WHEN N'@ignore_plan_ids' THEN 'a list of plan ids to ignore'
WHEN N'@ignore_query_ids' THEN 'a list of query ids to ignore'
WHEN N'@ignore_query_hashes' THEN 'a list of query hashes to ignore'
WHEN N'@ignore_plan_hashes' THEN 'a list of query plan hashes to ignore'
WHEN N'@ignore_sql_handles' THEN 'a list of sql handles to ignore'
WHEN N'@query_text_search' THEN 'query text to search for'
WHEN N'@query_text_search_not' THEN 'query text to exclude'
WHEN N'@escape_brackets' THEN 'Set this bit to 1 to search for query text containing square brackets (common in .NET Entity Framework and other ORM queries)'
WHEN N'@escape_character' THEN 'Sets the ESCAPE character for special character searches, defaults to the SQL standard backslash (\) character'
WHEN N'@only_queries_with_hints' THEN 'only return queries with query hints'
WHEN N'@only_queries_with_feedback' THEN 'only return queries with query feedback'
WHEN N'@only_queries_with_variants' THEN 'only return queries with query variants'
WHEN N'@only_queries_with_forced_plans' THEN 'only return queries with forced plans'
WHEN N'@only_queries_with_forced_plan_failures' THEN 'only return queries with forced plan failures'
WHEN N'@wait_filter' THEN 'wait category to search for; category details are below'
WHEN N'@query_type' THEN 'filter for only ad hoc queries or only from queries from modules'
WHEN N'@expert_mode' THEN 'returns additional columns and results'
WHEN N'@hide_help_table' THEN 'hides the "bottom table" that shows help and support information'
WHEN N'@format_output' THEN 'returns numbers formatted with commas'
WHEN N'@get_all_databases' THEN 'looks for query store enabled databases and returns combined results from all of them'
WHEN N'@workdays' THEN 'use this to filter out weekends and after-hours queries'
WHEN N'@work_start' THEN 'use this to set a specific start of your work days'
WHEN N'@work_end' THEN 'use this to set a specific end of your work days'
WHEN N'@help' THEN 'how you got here'
WHEN N'@debug' THEN 'prints dynamic sql, statement length, parameter and variable values, and raw temp table contents'
WHEN N'@troubleshoot_performance' THEN 'set statistics xml on for queries against views'
WHEN N'@version' THEN 'OUTPUT; for support'
WHEN N'@version_date' THEN 'OUTPUT; for support'
END,
valid_inputs =
CASE
ap.name
WHEN N'@database_name' THEN 'a database name with query store enabled'
WHEN N'@sort_order' THEN 'cpu, logical reads, physical reads, writes, duration, memory, tempdb, executions, recent, plan count by hashes, cpu waits, lock waits, locks waits, latch waits, latches waits, buffer latch waits, buffer latches waits, buffer io waits, log waits, log io waits, network waits, network io waits, parallel waits, parallelism waits, memory waits, total waits'
WHEN N'@top' THEN 'a positive integer between 1 and 9,223,372,036,854,775,807'
WHEN N'@start_date' THEN 'January 1, 1753, through December 31, 9999'
WHEN N'@end_date' THEN 'January 1, 1753, through December 31, 9999'
WHEN N'@timezone' THEN 'SELECT tzi.* FROM sys.time_zone_info AS tzi;'
WHEN N'@execution_count' THEN 'a positive integer between 1 and 9,223,372,036,854,775,807'
WHEN N'@duration_ms' THEN 'a positive integer between 1 and 9,223,372,036,854,775,807'
WHEN N'@execution_type_desc' THEN 'regular, aborted, exception'
WHEN N'@procedure_schema' THEN 'a valid schema in your database'
WHEN N'@procedure_name' THEN 'a valid programmable object in your database, can use wildcards'
WHEN N'@include_plan_ids' THEN 'a string; comma separated for multiple ids'
WHEN N'@include_query_ids' THEN 'a string; comma separated for multiple ids'
WHEN N'@include_query_hashes' THEN 'a string; comma separated for multiple hashes'
WHEN N'@include_plan_hashes' THEN 'a string; comma separated for multiple hashes'
WHEN N'@include_sql_handles' THEN 'a string; comma separated for multiple handles'
WHEN N'@ignore_plan_ids' THEN 'a string; comma separated for multiple ids'
WHEN N'@ignore_query_ids' THEN 'a string; comma separated for multiple ids'
WHEN N'@ignore_query_hashes' THEN 'a string; comma separated for multiple hashes'
WHEN N'@ignore_plan_hashes' THEN 'a string; comma separated for multiple hashes'
WHEN N'@ignore_sql_handles' THEN 'a string; comma separated for multiple handles'
WHEN N'@query_text_search' THEN 'a string; leading and trailing wildcards will be added if missing'
WHEN N'@query_text_search_not' THEN 'a string; leading and trailing wildcards will be added if missing'
WHEN N'@escape_brackets' THEN '0 or 1'
WHEN N'@escape_character' THEN 'some escape character, SQL standard is backslash (\)'
WHEN N'@only_queries_with_hints' THEN '0 or 1'
WHEN N'@only_queries_with_feedback' THEN '0 or 1'
WHEN N'@only_queries_with_variants' THEN '0 or 1'
WHEN N'@only_queries_with_forced_plans' THEN '0 or 1'
WHEN N'@only_queries_with_forced_plan_failures' THEN '0 or 1'
WHEN N'@wait_filter' THEN 'cpu, lock, latch, buffer latch, buffer io, log io, network io, parallelism, memory'
WHEN N'@query_type' THEN 'ad hoc, adhoc, proc, procedure, whatever.'
WHEN N'@expert_mode' THEN '0 or 1'
WHEN N'@hide_help_table' THEN '0 or 1'
WHEN N'@format_output' THEN '0 or 1'
WHEN N'@get_all_databases' THEN '0 or 1'
WHEN N'@workdays' THEN '0 or 1'
WHEN N'@work_start' THEN 'a time like 8am, 9am or something'
WHEN N'@work_end' THEN 'a time like 5pm, 6pm or something'
WHEN N'@help' THEN '0 or 1'
WHEN N'@debug' THEN '0 or 1'
WHEN N'@troubleshoot_performance' THEN '0 or 1'
WHEN N'@version' THEN 'none; OUTPUT'
WHEN N'@version_date' THEN 'none; OUTPUT'
END,
defaults =
CASE
ap.name
WHEN N'@database_name' THEN 'NULL; current non-system database name if NULL'
WHEN N'@sort_order' THEN 'cpu'
WHEN N'@top' THEN '10'
WHEN N'@start_date' THEN 'the last seven days'
WHEN N'@end_date' THEN 'NULL'
WHEN N'@timezone' THEN 'NULL'
WHEN N'@execution_count' THEN 'NULL'
WHEN N'@duration_ms' THEN 'NULL'
WHEN N'@execution_type_desc' THEN 'NULL'
WHEN N'@procedure_schema' THEN 'NULL; dbo if NULL and procedure name is not NULL'
WHEN N'@procedure_name' THEN 'NULL'
WHEN N'@include_plan_ids' THEN 'NULL'
WHEN N'@include_query_ids' THEN 'NULL'
WHEN N'@include_query_hashes' THEN 'NULL'
WHEN N'@include_plan_hashes' THEN 'NULL'
WHEN N'@include_sql_handles' THEN 'NULL'
WHEN N'@ignore_plan_ids' THEN 'NULL'
WHEN N'@ignore_query_ids' THEN 'NULL'
WHEN N'@ignore_query_hashes' THEN 'NULL'
WHEN N'@ignore_plan_hashes' THEN 'NULL'
WHEN N'@ignore_sql_handles' THEN 'NULL'
WHEN N'@query_text_search' THEN 'NULL'
WHEN N'@query_text_search_not' THEN 'NULL'
WHEN N'@escape_brackets' THEN '0'
WHEN N'@escape_character' THEN '\'
WHEN N'@only_queries_with_hints' THEN '0'
WHEN N'@only_queries_with_feedback' THEN '0'
WHEN N'@only_queries_with_variants' THEN '0'
WHEN N'@only_queries_with_forced_plans' THEN '0'
WHEN N'@only_queries_with_forced_plan_failures' THEN '0'
WHEN N'@wait_filter' THEN 'NULL'
WHEN N'@query_type' THEN 'NULL'
WHEN N'@expert_mode' THEN '0'
WHEN N'@hide_help_table' THEN '0'
WHEN N'@format_output' THEN '1'
WHEN N'@get_all_databases' THEN '0'
WHEN N'@workdays' THEN '0'
WHEN N'@work_start' THEN '9am'
WHEN N'@work_end' THEN '5pm'
WHEN N'@debug' THEN '0'
WHEN N'@help' THEN '0'
WHEN N'@troubleshoot_performance' THEN '0'
WHEN N'@version' THEN 'none; OUTPUT'
WHEN N'@version_date' THEN 'none; OUTPUT'
END
FROM sys.all_parameters AS ap
INNER JOIN sys.all_objects AS o
ON ap.object_id = o.object_id
INNER JOIN sys.types AS t
ON ap.system_type_id = t.system_type_id
AND ap.user_type_id = t.user_type_id
WHERE o.name = N'sp_QuickieStore'
OPTION(RECOMPILE);
/*
Wait categories: Only 2017+
*/
IF EXISTS
(
SELECT
1/0
FROM sys.all_objects AS ao
WHERE ao.name = N'query_store_wait_stats'
)
BEGIN
SELECT
wait_categories =
'cpu (1): SOS_SCHEDULER_YIELD' UNION ALL
SELECT 'lock (3): LCK_M_%' UNION ALL
SELECT 'latch (4): LATCH_%' UNION ALL
SELECT 'buffer latch (5): PAGELATCH_%' UNION ALL
SELECT 'buffer io (6): PAGEIOLATCH_%' UNION ALL
SELECT 'log io (14): LOGMGR, LOGBUFFER, LOGMGR_RESERVE_APPEND, LOGMGR_FLUSH, LOGMGR_PMM_LOG, CHKPT, WRITELOG' UNION ALL
SELECT 'network io (15): ASYNC_NETWORK_IO, NET_WAITFOR_PACKET, PROXY_NETWORK_IO, EXTERNAL_SCRIPT_NETWORK_IOF' UNION ALL
SELECT 'parallelism (16): CXPACKET, EXCHANGE, HT%, BMP%, BP%' UNION ALL
SELECT 'memory (17): RESOURCE_SEMAPHORE, CMEMTHREAD, CMEMPARTITIONED, EE_PMOLOCK, MEMORY_ALLOCATION_EXT, RESERVED_MEMORY_ALLOCATION_EXT, MEMORY_GRANT_UPDATE';
END;
/*
Results
*/
SELECT
results =
'results returned at the end of the procedure:' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Runtime Stats: data from query_store_runtime_stats, along with query plan, query text, wait stats (2017+, when enabled), and parent object' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Compilation Stats (expert mode only): data from query_store_query about compilation metrics' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Resource Stats (expert mode only): data from dm_exec_query_stats, when available' UNION ALL
SELECT 'query store does not currently track some details about memory grants and thread usage' UNION ALL
SELECT 'so i go back to a plan cache view to try to track it down' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Store Plan Feedback (2022+, expert mode, or when using only_queries_with_feedback): Lists queries that have been adjusted based on automated feedback mechanisms' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Store Hints (2022+, expert mode or when using @only_queries_with_hints): lists hints applied to queries from automated feedback mechanisms' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Variants (2022+, expert mode or when using @only_queries_with_variants): lists plan variants from the Parameter Sensitive Plan feedback mechanism' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Store Waits By Query (2017+, expert mode only): information about query duration and logged wait stats' UNION ALL
SELECT 'it can sometimes be useful to compare query duration to query wait times' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Store Waits Total (2017+, expert mode only): total wait stats for the chosen date range only' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Replicas (2022+, expert mode only): lists plans forced on AG replicas' UNION ALL
SELECT REPLICATE('-', 100) UNION ALL
SELECT 'Query Store Options (expert mode only): details about current query store configuration';
/*
Limitations
*/
SELECT
limitations =
'frigid shortcomings:' UNION ALL
SELECT 'you need to be on at least SQL Server 2016 SP2, 2017 CU3, or any higher version to run this' UNION ALL
SELECT 'if you''re on azure sql db then you''ll need to be in compat level 130' UNION ALL
SELECT 'i do not currently support synapse or edge or other memes, and azure sql db support is not guaranteed';
/*
License to F5
*/
SELECT
mit_license_yo =
'i am MIT licensed, so like, do whatever'
UNION ALL
SELECT
mit_license_yo =
'see printed messages for full license';
RAISERROR('
MIT License
Copyright 2024 Darling Data, LLC
https://www.erikdarling.com/
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.
', 0, 1) WITH NOWAIT;
RETURN;
END; /*End @help section*/
/*
These are the tables that we'll use to grab data from query store
It will be fun
You'll love it
*/
/*
Plans we'll be working on
*/
CREATE TABLE
#distinct_plans
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for procedures we're searching
*/
CREATE TABLE
#procedure_plans
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for procedures we're searching
*/
CREATE TABLE
#procedure_object_ids
(
[object_id] bigint PRIMARY KEY
);
/*
Hold plan_ids for ad hoc or procedures we're searching for
*/
CREATE TABLE
#query_types
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for plans we want
*/
CREATE TABLE
#include_plan_ids
(
plan_id bigint PRIMARY KEY WITH (IGNORE_DUP_KEY = ON)
);
/*
Hold query_ids for plans we want
*/
CREATE TABLE
#include_query_ids
(
query_id bigint PRIMARY KEY
);
/*
Hold plan_ids for ignored plans
*/
CREATE TABLE
#ignore_plan_ids
(
plan_id bigint PRIMARY KEY WITH (IGNORE_DUP_KEY = ON)
);
/*
Hold query_ids for ignored plans
*/
CREATE TABLE
#ignore_query_ids
(
query_id bigint PRIMARY KEY
);
/*
Hold query hashes for plans we want
*/
CREATE TABLE
#include_query_hashes
(
query_hash_s varchar(131),
query_hash AS
CONVERT
(
binary(8),
query_hash_s,
1
) PERSISTED NOT NULL PRIMARY KEY
);
/*
The following two tables are for adding extra columns
on to our output. We need these for sorting by anything
that isn't in #query_store_runtime_stats.
We still have to declare these tables even when they're
not used because the debug output breaks if we don't.
They are database dependent but not truncated at
the end of each loop, so we need a database_id
column.
We do not truncate these because we need them to still
be in scope and fully populated when we return our
final results from #query_store_runtime_stats, which
is done after the point where we would truncate.
*/
/*
Holds plan_id with the count of the number of query hashes they have.
Only used when we're sorting by how many plan hashes each
query hash has.
*/
CREATE TABLE
#plan_ids_with_query_hashes
(
database_id int NOT NULL,
plan_id bigint NOT NULL,
query_hash binary(8) NOT NULL,
plan_hash_count_for_query_hash INT NOT NULL,
PRIMARY KEY (database_id, plan_id, query_hash)
);
/*
Largely just exists because total_query_wait_time_ms
isn't in our normal output.
*/
CREATE TABLE
#plan_ids_with_total_waits
(
database_id int NOT NULL,
plan_id bigint NOT NULL,
total_query_wait_time_ms bigint NOT NULL,
PRIMARY KEY (database_id, plan_id)
);
/*
Hold plan hashes for plans we want
*/
CREATE TABLE
#include_plan_hashes
(
plan_hash_s varchar(131),
plan_hash AS
CONVERT
(
binary(8),
plan_hash_s,
1
) PERSISTED NOT NULL PRIMARY KEY
);
/*
Hold query hashes for ignored plans
*/
CREATE TABLE
#ignore_query_hashes
(
query_hash_s varchar(131),
query_hash AS
CONVERT
(
binary(8),
query_hash_s,
1
) PERSISTED NOT NULL PRIMARY KEY
);
/*
Hold plan hashes for ignored plans
*/
CREATE TABLE
#ignore_plan_hashes
(
plan_hash_s varchar(131),
plan_hash AS
CONVERT
(
binary(8),
plan_hash_s,
1
) PERSISTED NOT NULL PRIMARY KEY
);
/*
Hold sql handles for plans we want
*/
CREATE TABLE
#include_sql_handles
(
sql_handle_s varchar(131),
sql_handle AS
CONVERT
(
varbinary(64),
sql_handle_s,
1
) PERSISTED NOT NULL PRIMARY KEY
);
/*
Hold sql handles for ignored plans
*/
CREATE TABLE
#ignore_sql_handles
(
sql_handle_s varchar(131),
sql_handle AS
CONVERT
(
varbinary(64),
sql_handle_s,
1
) PERSISTED NOT NULL PRIMARY KEY
);
/*
Hold plan_ids for only query with hints
*/
CREATE TABLE
#only_queries_with_hints
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for only query with feedback
*/
CREATE TABLE
#only_queries_with_feedback
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for only query with variants
*/
CREATE TABLE
#only_queries_with_variants
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for forced plans and/or forced plan failures
I'm overloading this a bit for simplicity, since searching for
failures is just an extension of searching for forced plans
*/
CREATE TABLE
#forced_plans_failures
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for matching query text
*/
CREATE TABLE
#query_text_search
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for matching query text (not)
*/
CREATE TABLE
#query_text_search_not
(
plan_id bigint PRIMARY KEY
);
/*
Hold plan_ids for matching wait filter
*/
CREATE TABLE
#wait_filter
(
plan_id bigint PRIMARY KEY
);
/*
Index and statistics entries to avoid
*/
CREATE TABLE
#maintenance_plans
(
plan_id bigint PRIMARY KEY
);
/*
Query Store Setup
*/
CREATE TABLE
#database_query_store_options
(
database_id int NOT NULL,
desired_state_desc nvarchar(60) NULL,
actual_state_desc nvarchar(60) NULL,
readonly_reason nvarchar(100) NULL,
current_storage_size_mb bigint NULL,
flush_interval_seconds bigint NULL,
interval_length_minutes bigint NULL,
max_storage_size_mb bigint NULL,
stale_query_threshold_days bigint NULL,
max_plans_per_query bigint NULL,
query_capture_mode_desc nvarchar(60) NULL,
capture_policy_execution_count int NULL,
capture_policy_total_compile_cpu_time_ms bigint NULL,
capture_policy_total_execution_cpu_time_ms bigint NULL,
capture_policy_stale_threshold_hours int NULL,
size_based_cleanup_mode_desc nvarchar(60) NULL,
wait_stats_capture_mode_desc nvarchar(60) NULL
);
/*
Query Store Trouble
*/
CREATE TABLE
#query_store_trouble
(
database_id int NOT NULL,
desired_state_desc nvarchar(60) NULL,
actual_state_desc nvarchar(60) NULL,
readonly_reason nvarchar(100) NULL,
current_storage_size_mb bigint NULL,
flush_interval_seconds bigint NULL,
interval_length_minutes bigint NULL,
max_storage_size_mb bigint NULL,
stale_query_threshold_days bigint NULL,
max_plans_per_query bigint NULL,
query_capture_mode_desc nvarchar(60) NULL,
size_based_cleanup_mode_desc nvarchar(60) NULL
);
/*
Plans and Plan information
*/
CREATE TABLE
#query_store_plan
(
database_id int NOT NULL,
plan_id bigint NOT NULL,
query_id bigint NOT NULL,
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,
is_online_index_plan bit NOT NULL,
is_trivial_plan bit NOT NULL,
is_parallel_plan bit NOT NULL,
is_forced_plan bit NOT NULL,
is_natively_compiled bit NOT NULL,
force_failure_count bigint NOT NULL,
last_force_failure_reason_desc nvarchar(128) NULL,
count_compiles bigint NULL,
initial_compile_start_time datetimeoffset(7) NOT NULL,
last_compile_start_time datetimeoffset(7) NULL,
last_execution_time datetimeoffset(7) NULL,
avg_compile_duration_ms float NULL,
last_compile_duration_ms bigint NULL,
plan_forcing_type_desc nvarchar(60) NULL,
has_compile_replay_script bit NULL,
is_optimized_plan_forcing_disabled bit NULL,
plan_type_desc nvarchar(120) NULL
);
/*
Queries and Compile Information
*/
CREATE TABLE
#query_store_query
(
database_id int NOT NULL,
query_id bigint NOT NULL,
query_text_id bigint NOT NULL,
context_settings_id bigint NOT NULL,
object_id bigint NULL,
object_name AS
ISNULL
(
QUOTENAME
(
OBJECT_SCHEMA_NAME
(
object_id,
database_id
)
) +
N'.' +
QUOTENAME
(
OBJECT_NAME
(
object_id,
database_id
)
),
N'Adhoc'
),
batch_sql_handle varbinary(64) NULL,
query_hash binary(8) NOT NULL,
is_internal_query bit NOT NULL,
query_parameterization_type_desc nvarchar(60) NULL,
initial_compile_start_time datetimeoffset(7) NOT NULL,
last_compile_start_time datetimeoffset(7) NULL,
last_execution_time datetimeoffset(7) NULL,
last_compile_batch_sql_handle varbinary(64) NULL,
last_compile_batch_offset_start bigint NULL,
last_compile_batch_offset_end bigint NULL,
count_compiles bigint NULL,
avg_compile_duration_ms float NULL,
total_compile_duration_ms AS
(count_compiles * avg_compile_duration_ms),
last_compile_duration_ms bigint NULL,
avg_bind_duration_ms float NULL,
total_bind_duration_ms AS
(count_compiles * avg_bind_duration_ms),
last_bind_duration_ms bigint NULL,
avg_bind_cpu_time_ms float NULL,
total_bind_cpu_time_ms AS
(count_compiles * avg_bind_cpu_time_ms),
last_bind_cpu_time_ms bigint NULL,
avg_optimize_duration_ms float NULL,
total_optimize_duration_ms AS
(count_compiles * avg_optimize_duration_ms),
last_optimize_duration_ms bigint NULL,
avg_optimize_cpu_time_ms float NULL,
total_optimize_cpu_time_ms AS
(count_compiles * avg_optimize_cpu_time_ms),
last_optimize_cpu_time_ms bigint NULL,
avg_compile_memory_mb float NULL,
total_compile_memory_mb AS
(count_compiles * avg_compile_memory_mb),
last_compile_memory_mb bigint NULL,
max_compile_memory_mb bigint NULL,
is_clouddb_internal_query bit NULL
);
/*
Query Text And Columns From sys.dm_exec_query_stats
*/
CREATE TABLE
#query_store_query_text
(
database_id int NOT NULL,
query_text_id bigint NOT NULL,
query_sql_text xml NULL,
statement_sql_handle varbinary(64) NULL,
is_part_of_encrypted_module bit NOT NULL,
has_restricted_text bit NOT NULL,
total_grant_mb bigint NULL,
last_grant_mb bigint NULL,
min_grant_mb bigint NULL,
max_grant_mb bigint NULL,
total_used_grant_mb bigint NULL,
last_used_grant_mb bigint NULL,
min_used_grant_mb bigint NULL,
max_used_grant_mb bigint NULL,
total_ideal_grant_mb bigint NULL,
last_ideal_grant_mb bigint NULL,
min_ideal_grant_mb bigint NULL,
max_ideal_grant_mb bigint NULL,
total_reserved_threads bigint NULL,
last_reserved_threads bigint NULL,
min_reserved_threads bigint NULL,
max_reserved_threads bigint NULL,
total_used_threads bigint NULL,
last_used_threads bigint NULL,
min_used_threads bigint NULL,
max_used_threads bigint NULL
);
/*
Figure it out.
*/
CREATE TABLE
#dm_exec_query_stats
(
statement_sql_handle varbinary(64) NOT NULL,
total_grant_mb bigint NULL,
last_grant_mb bigint NULL,
min_grant_mb bigint NULL,
max_grant_mb bigint NULL,
total_used_grant_mb bigint NULL,
last_used_grant_mb bigint NULL,
min_used_grant_mb bigint NULL,
max_used_grant_mb bigint NULL,
total_ideal_grant_mb bigint NULL,
last_ideal_grant_mb bigint NULL,
min_ideal_grant_mb bigint NULL,
max_ideal_grant_mb bigint NULL,
total_reserved_threads bigint NULL,
last_reserved_threads bigint NULL,
min_reserved_threads bigint NULL,
max_reserved_threads bigint NULL,
total_used_threads bigint NULL,
last_used_threads bigint NULL,
min_used_threads bigint NULL,
max_used_threads bigint NULL
);
/*
Runtime stats information
*/
CREATE TABLE
#query_store_runtime_stats
(
database_id int NOT NULL,
runtime_stats_id bigint NOT NULL,
plan_id bigint NOT NULL,
runtime_stats_interval_id bigint NOT NULL,
execution_type_desc nvarchar(60) NULL,
first_execution_time datetimeoffset(7) NOT NULL,
last_execution_time datetimeoffset(7) NOT NULL,
count_executions bigint NOT NULL,
executions_per_second AS
ISNULL
(
count_executions /
NULLIF
(
DATEDIFF
(
SECOND,
first_execution_time,
last_execution_time
),
0
),
0
),
avg_duration_ms float NULL,
last_duration_ms bigint NOT NULL,
min_duration_ms bigint NOT NULL,
max_duration_ms bigint NOT NULL,
total_duration_ms AS
(avg_duration_ms * count_executions),
avg_cpu_time_ms float NULL,
last_cpu_time_ms bigint NOT NULL,
min_cpu_time_ms bigint NOT NULL,
max_cpu_time_ms bigint NOT NULL,
total_cpu_time_ms AS
(avg_cpu_time_ms * count_executions),
avg_logical_io_reads_mb float NULL,
last_logical_io_reads_mb bigint NOT NULL,
min_logical_io_reads_mb bigint NOT NULL,
max_logical_io_reads_mb bigint NOT NULL,
total_logical_io_reads_mb AS
(avg_logical_io_reads_mb * count_executions),
avg_logical_io_writes_mb float NULL,
last_logical_io_writes_mb bigint NOT NULL,
min_logical_io_writes_mb bigint NOT NULL,
max_logical_io_writes_mb bigint NOT NULL,
total_logical_io_writes_mb AS
(avg_logical_io_writes_mb * count_executions),
avg_physical_io_reads_mb float NULL,
last_physical_io_reads_mb bigint NOT NULL,
min_physical_io_reads_mb bigint NOT NULL,
max_physical_io_reads_mb bigint NOT NULL,
total_physical_io_reads_mb AS
(avg_physical_io_reads_mb * count_executions),
avg_clr_time_ms float NULL,
last_clr_time_ms bigint NOT NULL,
min_clr_time_ms bigint NOT NULL,
max_clr_time_ms bigint NOT NULL,
total_clr_time_ms AS
(avg_clr_time_ms * count_executions),
last_dop bigint NOT NULL,
min_dop bigint NOT NULL,
max_dop bigint NOT NULL,
avg_query_max_used_memory_mb float NULL,
last_query_max_used_memory_mb bigint NOT NULL,
min_query_max_used_memory_mb bigint NOT NULL,
max_query_max_used_memory_mb bigint NOT NULL,
total_query_max_used_memory_mb AS
(avg_query_max_used_memory_mb * count_executions),
avg_rowcount float NULL,
last_rowcount bigint NOT NULL,
min_rowcount bigint NOT NULL,
max_rowcount bigint NOT NULL,
total_rowcount AS
(avg_rowcount * count_executions),
avg_num_physical_io_reads_mb float NULL,
last_num_physical_io_reads_mb bigint NULL,
min_num_physical_io_reads_mb bigint NULL,
max_num_physical_io_reads_mb bigint NULL,
total_num_physical_io_reads_mb AS
(avg_num_physical_io_reads_mb * count_executions),
avg_log_bytes_used_mb float NULL,
last_log_bytes_used_mb bigint NULL,
min_log_bytes_used_mb bigint NULL,
max_log_bytes_used_mb bigint NULL,
total_log_bytes_used_mb AS
(avg_log_bytes_used_mb * count_executions),
avg_tempdb_space_used_mb float NULL,
last_tempdb_space_used_mb bigint NULL,
min_tempdb_space_used_mb bigint NULL,
max_tempdb_space_used_mb bigint NULL,
total_tempdb_space_used_mb AS
(avg_tempdb_space_used_mb * count_executions),
context_settings nvarchar(256) NULL
);
/*
Wait Stats, When Available (2017+)
*/
CREATE TABLE
#query_store_wait_stats
(
database_id int NOT NULL,
plan_id bigint NOT NULL,
wait_category_desc nvarchar(60) NOT NULL,
total_query_wait_time_ms bigint NOT NULL,
avg_query_wait_time_ms float NULL,
last_query_wait_time_ms bigint NOT NULL,
min_query_wait_time_ms bigint NOT NULL,
max_query_wait_time_ms bigint NOT NULL
);
/*
Context is everything
*/
CREATE TABLE
#query_context_settings
(
database_id int NOT NULL,
context_settings_id bigint NOT NULL,
set_options varbinary(8) NULL,
language_id smallint NOT NULL,
date_format smallint NOT NULL,
date_first tinyint NOT NULL,
status varbinary(2) NULL,
required_cursor_options int NOT NULL,
acceptable_cursor_options int NOT NULL,
merge_action_type smallint NOT NULL,
default_schema_id int NOT NULL,
is_replication_specific bit NOT NULL,
is_contained varbinary(1) NULL
);
/*
Feed me Seymour
*/
CREATE TABLE
#query_store_plan_feedback
(
database_id int NOT NULL,
plan_feedback_id bigint,
plan_id bigint,
feature_desc nvarchar(120),
feedback_data nvarchar(MAX),
state_desc nvarchar(120),
create_time datetimeoffset(7),
last_updated_time datetimeoffset(7)
);
/*
America's Most Hinted
*/
CREATE TABLE
#query_store_query_hints
(
database_id int NOT NULL,
query_hint_id bigint,
query_id bigint,
query_hint_text nvarchar(MAX),
last_query_hint_failure_reason_desc nvarchar(256),
query_hint_failure_count bigint,
source_desc nvarchar(256)
);
/*
Variant? Deviant? You decide!
*/
CREATE TABLE
#query_store_query_variant
(
database_id int NOT NULL,
query_variant_query_id bigint,
parent_query_id bigint,
dispatcher_plan_id bigint
);
/*
Replicants
*/
CREATE TABLE
#query_store_replicas
(
database_id int NOT NULL,
replica_group_id bigint,
role_type smallint,
replica_name nvarchar(1288)
);
/*
Location, location, location
*/
CREATE TABLE
#query_store_plan_forcing_locations
(
database_id int NOT NULL,
plan_forcing_location_id bigint,
query_id bigint,
plan_id bigint,
replica_group_id bigint
);
/*
Trouble Loves Me
*/
CREATE TABLE
#troubleshoot_performance
(
id bigint IDENTITY,
current_table nvarchar(100),
start_time datetime,
end_time datetime,
runtime_ms AS
FORMAT
(
DATEDIFF
(
MILLISECOND,
start_time,
end_time
),
'N0'
)
);
/*GET ALL THOSE DATABASES*/
CREATE TABLE
#databases
(
database_name sysname PRIMARY KEY
);
/*
Try to be helpful by subbing in a database name if null
*/
IF
(
@database_name IS NULL
AND LOWER(DB_NAME())
NOT IN
(
N'master',
N'model',
N'msdb',
N'tempdb',
N'dbatools',
N'dbadmin',
N'dbmaintenance',
N'rdsadmin',
N'other_memes'
)
AND @get_all_databases = 0
)
BEGIN
SELECT
@database_name =
DB_NAME();
END;
/*
Variables for the variable gods
*/
DECLARE
@azure bit,
@engine int,
@product_version int,
@database_id int,
@database_name_quoted sysname,
@procedure_name_quoted nvarchar(1024),
@collation sysname,
@new bit,
@sql nvarchar(MAX),
@isolation_level nvarchar(MAX),
@parameters nvarchar(4000),
@plans_top bigint,
@queries_top bigint,
@nc10 nvarchar(2),
@where_clause nvarchar(MAX),
@query_text_search_original_value nvarchar(4000),
@query_text_search_not_original_value nvarchar(4000),
@procedure_exists bit,
@query_store_exists bit,
@query_store_trouble bit,
@query_store_waits_enabled bit,
@sql_2022_views bit,
@ags_present bit,
@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),
@rc bigint,
@em tinyint,
@fo tinyint,
@start_date_original datetimeoffset(7),
@end_date_original datetimeoffset(7),
@utc_minutes_difference bigint,
@utc_offset_string nvarchar(6),
@df integer,
@work_start_utc time(0),
@work_end_utc time(0),
@sort_order_is_a_wait bit;
/*
In cases where we are escaping @query_text_search and
looping over multiple databases, we need to make sure
to not escape the string more than once.
The solution is to reset to the original value each loop.
This therefore needs to be done before the cursor.
*/
IF
(
@get_all_databases = 1
AND @escape_brackets = 1
)
BEGIN
SELECT
@query_text_search_original_value = @query_text_search,
@query_text_search_not_original_value = @query_text_search_not;
END;
/*
We also need to capture original values here.
Doing it inside a loop over multiple databases
would break the UTC conversion.
*/
SELECT
@start_date_original =
ISNULL
(
@start_date,
DATEADD
(
DAY,
-7,
DATEDIFF
(
DAY,
'19000101',
SYSUTCDATETIME()
)
)
),
@end_date_original =
ISNULL
(
@end_date,
DATEADD
(
DAY,
1,
DATEADD
(
MINUTE,
0,
DATEDIFF
(
DAY,
'19000101',
SYSUTCDATETIME()
)
)
)
);
/*
This section is in a cursor whether we
hit one database, or multiple
I do all the variable assignment in the
cursor block because some of them
are assigned for the specific database
that is currently being looked at
*/
IF
(
SELECT
CONVERT
(
sysname,
SERVERPROPERTY('EngineEdition')
)
) IN (5, 8)
BEGIN
INSERT INTO
#databases WITH(TABLOCK)
(
database_name
)
SELECT
database_name =
ISNULL(@database_name, DB_NAME())
WHERE @get_all_databases = 0
UNION ALL
SELECT
database_name =
d.name
FROM sys.databases AS d
WHERE @get_all_databases = 1
AND d.is_query_store_on = 1
AND d.database_id > 4
AND d.state = 0
AND d.is_in_standby = 0
AND d.is_read_only = 0
OPTION(RECOMPILE);
END
ELSE
BEGIN
INSERT
#databases WITH(TABLOCK)
(
database_name
)
SELECT
database_name =
ISNULL(@database_name, DB_NAME())
WHERE @get_all_databases = 0
UNION ALL
SELECT
database_name =
d.name
FROM sys.databases AS d
WHERE @get_all_databases = 1
AND d.is_query_store_on = 1
AND d.database_id > 4
AND d.state = 0
AND d.is_in_standby = 0
AND d.is_read_only = 0
AND NOT EXISTS
(
SELECT
1/0
FROM sys.dm_hadr_availability_replica_states AS s
JOIN sys.availability_databases_cluster AS c
ON s.group_id = c.group_id
AND d.name = c.database_name
WHERE s.is_local <> 1
AND s.role_desc <> N'PRIMARY'
AND DATABASEPROPERTYEX(c.database_name, N'Updateability') <> N'READ_WRITE'
)
OPTION(RECOMPILE);
END;
DECLARE
@database_cursor CURSOR;
SET
@database_cursor = CURSOR
LOCAL
SCROLL
DYNAMIC
READ_ONLY
FOR
SELECT
d.database_name
FROM #databases AS d;
OPEN @database_cursor;
FETCH FIRST
FROM @database_cursor
INTO @database_name;
WHILE @@FETCH_STATUS = 0
BEGIN
/*
Some variable assignment, because why not?
*/
IF @debug = 1
BEGIN
RAISERROR('Starting analysis for database %s', 0, 1, @database_name) WITH NOWAIT;
END;
SELECT
@azure =
CASE
WHEN
CONVERT
(
sysname,
SERVERPROPERTY('EDITION')
) = N'SQL Azure'
THEN 1
ELSE 0
END,
@engine =
CONVERT
(
int,
SERVERPROPERTY('ENGINEEDITION')
),
@product_version =