Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdarlingdata authored Jan 23, 2024
2 parents c500538 + 06cbf08 commit 4406d36
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 42 deletions.
1 change: 0 additions & 1 deletion Install-All/DarlingData.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-- Compile Date: 11/22/2023 01:11:09 UTC
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ Current valid parameter details:
| @format_output | bit | returns numbers formatted with commas | 0 or 1 | 1 |
| @get_all_databases | bit | looks for query store enabled databases and returns combined results from all of them | 0 or 1 | 0 |
| @workdays | bit | use this to filter out weekends and after-hours queries | 0 or 1 | 0 |
| @work_start | time(0) | use this to set a specific start of your work days | a "time" like 8am, 9am or something | 9am |
| @work_end | time(0) | use this to set a specific end of your work days | a "time" like 5pm, 6pm or something | 5pm |
| @work_start | varchar | use this to set a specific start of your work days | a "time" like 8am, 9am or something | 9am |
| @work_end | varchar | use this to set a specific end of your work days | a "time" like 5pm, 6pm or something | 5pm |
| @help | bit | how you got here | 0 or 1 | 0 |
| @debug | bit | prints dynamic sql, statement length, parameter and variable values, and raw temp table contents | 0 or 1 | 0 |
| @troubleshoot_performance | bit | set statistics xml on for queries against views | 0 or 1 | 0 |
Expand Down Expand Up @@ -348,4 +348,4 @@ Current valid parameter details:

[*Back to top*](#navigatory)

[licence badge]:https://img.shields.io/badge/license-MIT-blue.svg
[licence badge]:https://img.shields.io/badge/license-MIT-blue.svg
187 changes: 149 additions & 38 deletions sp_QuickieStore/sp_QuickieStore.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ ALTER PROCEDURE
@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*/
@work_start varchar(4) = '9am', /*Use this to set a specific start of your work days*/
@work_end varchar(4) = '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*/
Expand Down Expand Up @@ -995,6 +995,18 @@ CREATE TABLE
database_name sysname PRIMARY KEY
);

/*
AM/PM mapping table for workday stuff
*/
CREATE TABLE
#am_pm
(
am_pm varchar(4),
t12 integer,
t24 integer
);


/*
Try to be helpful by subbing in a database name if null
*/
Expand Down Expand Up @@ -1060,8 +1072,8 @@ DECLARE
@utc_minutes_difference bigint,
@utc_minutes_original bigint,
@df integer,
@work_start_utc time(0),
@work_end_utc time(0);
@work_start_int integer,
@work_end_int integer;


/*
Expand Down Expand Up @@ -1194,8 +1206,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
@execution_type_desc nvarchar(60),
@database_id int,
@queries_top bigint,
@work_start_utc time(0),
@work_end_utc time(0)',
@work_start_int integer,
@work_end_int integer',
@plans_top =
CASE
WHEN @include_plan_ids IS NULL
Expand Down Expand Up @@ -1397,8 +1409,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
SYSDATETIME()
),
@df = @@DATEFIRST,
@work_start_utc = @work_start,
@work_end_utc = @work_end;
@work_start_int = 0,
@work_end_int = 0;

/*
Some parameters can't be NULL,
Expand Down Expand Up @@ -2026,16 +2038,90 @@ END;

IF @workdays = 1
BEGIN
IF @work_start_utc IS NULL
AND @work_end_utc IS NULL
SELECT
@work_start = LOWER(REPLACE(@work_start, ' ', '')),
@work_end = LOWER(REPLACE(@work_end, ' ', ''));

INSERT
#am_pm
(
am_pm,
t12,
t24
)
SELECT
am_pm =
CASE
WHEN y.t24 BETWEEN 1 AND 11
THEN RTRIM(y.t12) + 'am'
WHEN y.t24 = 0
THEN RTRIM(y.t12) + 'am'
ELSE RTRIM(y.t12) + 'pm'
END,
y.t12,
y.t24
FROM
(
SELECT
t12 =
CASE x.t12
WHEN 0
THEN 12
ELSE x.t12
END,
t24 =
CASE
WHEN x.t24 < 24
THEN x.t24
ELSE 0
END
FROM
(
SELECT TOP (24)
t12 =
ROW_NUMBER() OVER
(
ORDER BY
1/0
) % 12,
t24 =
ROW_NUMBER() OVER
(
ORDER BY
1/0
)
FROM sys.messages AS m
) AS x
) AS y
ORDER BY
y.t24;

SELECT
@work_start_int =
(
SELECT
ap.t24
FROM #am_pm AS ap
WHERE ap.am_pm = @work_start
),
@work_end_int =
(
SELECT
ap.t24
FROM #am_pm AS ap
WHERE ap.am_pm = @work_end
);

IF @work_start_int IS NULL
AND @work_end_int IS NULL
BEGIN
SELECT
@work_start_utc = '09:00',
@work_end_utc = '17:00';
@work_start_int = 9,
@work_end_int = 17;
END;

IF @work_start_utc IS NOT NULL
AND @work_end_utc IS NULL
IF @work_start_int IS NOT NULL
AND @work_end_int IS NULL
BEGIN
SELECT
@work_end_utc =
Expand All @@ -2046,9 +2132,9 @@ BEGIN
@work_start_utc
);
END;

IF @work_start_utc IS NULL
AND @work_end_utc IS NOT NULL
IF @work_start_int IS NULL
AND @work_end_int IS NOT NULL
BEGIN
SELECT
@work_start_utc =
Expand All @@ -2061,20 +2147,20 @@ BEGIN
END;

SELECT
@work_start_utc =
DATEADD
@work_start_int +=
DATEDIFF
(
MINUTE,
@utc_minutes_difference,
@work_start_utc
),
@work_end_utc =
DATEADD
SYSDATETIME(),
SYSUTCDATETIME()
) / 60,
@work_end_int +=
DATEDIFF
(
MINUTE,
@utc_minutes_difference,
@work_end_utc
);
SYSDATETIME(),
SYSUTCDATETIME()
) / 60;

IF @df = 1
BEGIN
Expand All @@ -2088,8 +2174,8 @@ BEGIN
@where_clause += N'AND DATEPART(WEEKDAY, qsrs.last_execution_time) BETWEEN 2 AND 6' + @nc10;
END;/*df 7*/

IF @work_start_utc IS NOT NULL
AND @work_end_utc IS NOT NULL
IF @work_start_int IS NOT NULL
AND @work_end_int IS NOT NULL
BEGIN
/*
depending on local TZ, work time might span midnight UTC;
Expand Down Expand Up @@ -3627,8 +3713,8 @@ EXEC sys.sp_executesql
@execution_type_desc,
@database_id,
@queries_top,
@work_start_utc,
@work_end_utc;
@work_start_int,
@work_end_int;

IF @troubleshoot_performance = 1
BEGIN
Expand Down Expand Up @@ -3812,8 +3898,8 @@ EXEC sys.sp_executesql
@execution_type_desc,
@database_id,
@queries_top,
@work_start_utc,
@work_end_utc;
@work_start_int,
@work_end_int;

IF @troubleshoot_performance = 1
BEGIN
Expand Down Expand Up @@ -5155,6 +5241,8 @@ BEGIN
#dm_exec_query_stats;
TRUNCATE TABLE
#query_types;
TRUNCATE TABLE
#am_pm;
END;

FETCH NEXT
Expand Down Expand Up @@ -7644,10 +7732,10 @@ BEGIN
@utc_minutes_original,
df =
@df,
work_start_utc =
@work_start_utc,
work_end_utc =
@work_end_utc;
work_start_int =
@work_start_int,
work_end_int =
@work_end_int;

IF EXISTS
(
Expand Down Expand Up @@ -8386,6 +8474,29 @@ BEGIN
result =
'#troubleshoot_performance is empty';
END;

IF EXISTS
(
SELECT
1/0
FROM #am_pm AS ap
)
BEGIN
SELECT
table_name =
'#am_pm',
ap.*
FROM #am_pm AS ap
ORDER BY
ap.t24
OPTION(RECOMPILE);
END;
ELSE
BEGIN
SELECT
result =
'#troubleshoot_performance is empty';
END;
RETURN; /*Stop doing anything, I guess*/
END; /*End debug*/
RETURN; /*Yeah sure why not?*/
Expand Down

0 comments on commit 4406d36

Please sign in to comment.