Skip to content

Commit

Permalink
Merge pull request #394 from erikdarlingdata/dev
Browse files Browse the repository at this point in the history
Update sp_QuickieStore.sql
  • Loading branch information
erikdarlingdata authored Apr 2, 2024
2 parents ed99a76 + 5145534 commit 3f46f21
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 50 deletions.
10 changes: 7 additions & 3 deletions sp_PressureDetector/sp_PressureDetector.sql
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ OPTION(MAXDOP 1, RECOMPILE);',
percent_signal_waits decimal(38,2),
waiting_tasks_count_n bigint,
sample_time datetime,
sorting bigint,
waiting_tasks_count AS
REPLACE
(
Expand Down Expand Up @@ -519,7 +520,8 @@ OPTION(MAXDOP 1, RECOMPILE);',
avg_ms_per_wait,
percent_signal_waits,
waiting_tasks_count_n,
sample_time
sample_time,
sorting
)
SELECT
hours_uptime =
Expand Down Expand Up @@ -655,7 +657,9 @@ OPTION(MAXDOP 1, RECOMPILE);',
),
dows.waiting_tasks_count,
sample_time =
GETDATE()
GETDATE(),
sorting =
ROW_NUMBER() OVER (ORDER BY dows.wait_time_ms DESC)
FROM sys.dm_os_wait_stats AS dows
WHERE
(
Expand Down Expand Up @@ -748,7 +752,7 @@ OPTION(MAXDOP 1, RECOMPILE);',
)
FROM @waits AS w
ORDER BY
w.hours_wait_time DESC;
w.sorting;
END;

IF
Expand Down
145 changes: 98 additions & 47 deletions sp_QuickieStore/sp_QuickieStore.sql
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ Hold plan_ids for plans we want
CREATE TABLE
#include_plan_ids
(
plan_id bigint PRIMARY KEY
plan_id bigint PRIMARY KEY WITH (IGNORE_DUP_KEY = ON)
);

/*
Expand All @@ -460,7 +460,7 @@ Hold plan_ids for ignored plans
CREATE TABLE
#ignore_plan_ids
(
plan_id bigint PRIMARY KEY
plan_id bigint PRIMARY KEY WITH (IGNORE_DUP_KEY = ON)
);

/*
Expand Down Expand Up @@ -1182,41 +1182,79 @@ cursor block because some of them
are assigned for the specific database
that is currently being looked at
*/
INSERT
#databases WITH(TABLOCK)

IF
(
database_name
)
SELECT
database_name =
ISNULL(@database_name, DB_NAME())
WHERE @get_all_databases = 0
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
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);
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
Expand Down Expand Up @@ -2281,19 +2319,30 @@ BEGIN
END;
ELSE
BEGIN
SELECT
@ags_present =
CASE
WHEN EXISTS
(
SELECT
1/0
FROM sys.availability_groups AS ag
)
THEN 1
ELSE 0
END
IF
(
SELECT
CONVERT
(
sysname,
SERVERPROPERTY('EngineEdition')
)
) NOT IN (5, 8)
BEGIN
SELECT
@ags_present =
CASE
WHEN EXISTS
(
SELECT
1/0
FROM sys.availability_groups AS ag
)
THEN 1
ELSE 0
END
OPTION(RECOMPILE);
END
END;

/*
Expand Down Expand Up @@ -3793,7 +3842,8 @@ WHERE qsp.is_forced_plan = 1';

IF @only_queries_with_forced_plan_failures = 1
BEGIN
SELECT @sql += N'
SELECT
@sql += N'
AND qsp.last_force_failure_reason > 0'
END

Expand Down Expand Up @@ -4553,7 +4603,8 @@ IF
AND @sql_2022_views = 1
)
BEGIN
SELECT @sql += N'
SELECT
@sql += N'
qsp.plan_forcing_type_desc,
qsp.has_compile_replay_script,
qsp.is_optimized_plan_forcing_disabled,
Expand Down

0 comments on commit 3f46f21

Please sign in to comment.