Skip to content

Commit aae1397

Browse files
Merge pull request #350 from ben-thul/bthul/time-to-change
Change work_start/work_end params to time(0) DT
2 parents d25e785 + e29795a commit aae1397

File tree

2 files changed

+81
-159
lines changed

2 files changed

+81
-159
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ Current valid parameter details:
260260
| @format_output | bit | returns numbers formatted with commas | 0 or 1 | 1 |
261261
| @get_all_databases | bit | looks for query store enabled databases and returns combined results from all of them | 0 or 1 | 0 |
262262
| @workdays | bit | use this to filter out weekends and after-hours queries | 0 or 1 | 0 |
263-
| @work_start | varchar | use this to set a specific start of your work days | a "time" like 8am, 9am or something | 9am |
264-
| @work_end | varchar | use this to set a specific end of your work days | a "time" like 5pm, 6pm or something | 5pm |
263+
| @work_start | time(0) | use this to set a specific start of your work days | a "time" like 8am, 9am or something | 9am |
264+
| @work_end | time(0) | use this to set a specific end of your work days | a "time" like 5pm, 6pm or something | 5pm |
265265
| @help | bit | how you got here | 0 or 1 | 0 |
266266
| @debug | bit | prints dynamic sql, statement length, parameter and variable values, and raw temp table contents | 0 or 1 | 0 |
267267
| @troubleshoot_performance | bit | set statistics xml on for queries against views | 0 or 1 | 0 |
@@ -348,4 +348,4 @@ Current valid parameter details:
348348

349349
[*Back to top*](#navigatory)
350350

351-
[licence badge]:https://img.shields.io/badge/license-MIT-blue.svg
351+
[licence badge]:https://img.shields.io/badge/license-MIT-blue.svg

sp_QuickieStore/sp_QuickieStore.sql

Lines changed: 78 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ ALTER PROCEDURE
8282
@format_output bit = 1, /*returns numbers formatted with commas*/
8383
@get_all_databases bit = 0, /*looks for query store enabled databases and returns combined results from all of them*/
8484
@workdays bit = 0, /*Use this to filter out weekends and after-hours queries*/
85-
@work_start varchar(4) = '9am', /*Use this to set a specific start of your work days*/
86-
@work_end varchar(4) = '5pm', /*Use this to set a specific end of your work days*/
85+
@work_start time(0) = '9am', /*Use this to set a specific start of your work days*/
86+
@work_end time(0) = '5pm', /*Use this to set a specific end of your work days*/
8787
@help bit = 0, /*return available parameter details, etc.*/
8888
@debug bit = 0, /*prints dynamic sql, statement length, parameter and variable values, and raw temp table contents*/
8989
@troubleshoot_performance bit = 0, /*set statistics xml on for queries against views*/
@@ -995,18 +995,6 @@ CREATE TABLE
995995
database_name sysname PRIMARY KEY
996996
);
997997

998-
/*
999-
AM/PM mapping table for workday stuff
1000-
*/
1001-
CREATE TABLE
1002-
#am_pm
1003-
(
1004-
am_pm varchar(4),
1005-
t12 integer,
1006-
t24 integer
1007-
);
1008-
1009-
1010998
/*
1011999
Try to be helpful by subbing in a database name if null
10121000
*/
@@ -1072,8 +1060,8 @@ DECLARE
10721060
@utc_minutes_difference bigint,
10731061
@utc_minutes_original bigint,
10741062
@df integer,
1075-
@work_start_int integer,
1076-
@work_end_int integer;
1063+
@work_start_utc time(0),
1064+
@work_end_utc time(0);
10771065

10781066

10791067
/*
@@ -1206,8 +1194,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
12061194
@execution_type_desc nvarchar(60),
12071195
@database_id int,
12081196
@queries_top bigint,
1209-
@work_start_int integer,
1210-
@work_end_int integer',
1197+
@work_start_utc time(0),
1198+
@work_end_utc time(0)',
12111199
@plans_top =
12121200
CASE
12131201
WHEN @include_plan_ids IS NULL
@@ -1409,8 +1397,8 @@ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
14091397
SYSDATETIME()
14101398
),
14111399
@df = @@DATEFIRST,
1412-
@work_start_int = 0,
1413-
@work_end_int = 0;
1400+
@work_start_utc = @work_start,
1401+
@work_end_utc = @work_end;
14141402

14151403
/*
14161404
Some parameters can't be NULL,
@@ -2038,135 +2026,94 @@ END;
20382026

20392027
IF @workdays = 1
20402028
BEGIN
2041-
SELECT
2042-
@work_start = LOWER(REPLACE(@work_start, ' ', '')),
2043-
@work_end = LOWER(REPLACE(@work_end, ' ', ''));
2044-
2045-
INSERT
2046-
#am_pm
2047-
(
2048-
am_pm,
2049-
t12,
2050-
t24
2051-
)
2052-
SELECT
2053-
am_pm =
2054-
CASE
2055-
WHEN y.t24 BETWEEN 1 AND 11
2056-
THEN RTRIM(y.t12) + 'am'
2057-
WHEN y.t24 = 0
2058-
THEN RTRIM(y.t12) + 'am'
2059-
ELSE RTRIM(y.t12) + 'pm'
2060-
END,
2061-
y.t12,
2062-
y.t24
2063-
FROM
2064-
(
2065-
SELECT
2066-
t12 =
2067-
CASE x.t12
2068-
WHEN 0
2069-
THEN 12
2070-
ELSE x.t12
2071-
END,
2072-
t24 =
2073-
CASE
2074-
WHEN x.t24 < 24
2075-
THEN x.t24
2076-
ELSE 0
2077-
END
2078-
FROM
2079-
(
2080-
SELECT TOP (24)
2081-
t12 =
2082-
ROW_NUMBER() OVER
2083-
(
2084-
ORDER BY
2085-
1/0
2086-
) % 12,
2087-
t24 =
2088-
ROW_NUMBER() OVER
2089-
(
2090-
ORDER BY
2091-
1/0
2092-
)
2093-
FROM sys.messages AS m
2094-
) AS x
2095-
) AS y
2096-
ORDER BY
2097-
y.t24;
2098-
2099-
SELECT
2100-
@work_start_int =
2101-
(
2102-
SELECT
2103-
ap.t24
2104-
FROM #am_pm AS ap
2105-
WHERE ap.am_pm = @work_start
2106-
),
2107-
@work_end_int =
2108-
(
2109-
SELECT
2110-
ap.t24
2111-
FROM #am_pm AS ap
2112-
WHERE ap.am_pm = @work_end
2113-
);
2114-
2115-
IF @work_start_int IS NULL
2116-
AND @work_end_int IS NULL
2029+
IF @work_start_utc IS NULL
2030+
AND @work_end_utc IS NULL
21172031
BEGIN
21182032
SELECT
2119-
@work_start_int = 9,
2120-
@work_end_int = 17;
2033+
@work_start_utc = '09:00',
2034+
@work_end_utc = '17:00';
21212035
END;
2122-
2123-
IF @work_start_int IS NOT NULL
2124-
AND @work_end_int IS NULL
2036+
2037+
IF @work_start_utc IS NOT NULL
2038+
AND @work_end_utc IS NULL
21252039
BEGIN
21262040
SELECT
2127-
@work_end_int = @work_start_int + 8;
2041+
@work_end_utc =
2042+
DATEADD
2043+
(
2044+
HOUR,
2045+
8,
2046+
@work_start_utc
2047+
);
21282048
END;
2129-
2130-
IF @work_start_int IS NULL
2131-
AND @work_end_int IS NOT NULL
2049+
2050+
IF @work_start_utc IS NULL
2051+
AND @work_end_utc IS NOT NULL
21322052
BEGIN
21332053
SELECT
2134-
@work_start_int = @work_end_int - 8;
2054+
@work_start_utc =
2055+
DATEADD
2056+
(
2057+
HOUR,
2058+
-8,
2059+
@work_end_utc
2060+
);
21352061
END;
21362062

21372063
SELECT
2138-
@work_start_int +=
2139-
DATEDIFF
2064+
@work_start_utc =
2065+
DATEADD
21402066
(
21412067
MINUTE,
2142-
SYSDATETIME(),
2143-
SYSUTCDATETIME()
2144-
) / 60,
2145-
@work_end_int +=
2146-
DATEDIFF
2068+
@utc_minutes_difference,
2069+
@work_start_utc
2070+
),
2071+
@work_end_utc =
2072+
DATEADD
21472073
(
21482074
MINUTE,
2149-
SYSDATETIME(),
2150-
SYSUTCDATETIME()
2151-
) / 60;
2075+
@utc_minutes_difference,
2076+
@work_end_utc
2077+
);
21522078

21532079
IF @df = 1
21542080
BEGIN
2155-
SELECT
2156-
@where_clause += N'AND DATEPART(WEEKDAY, qsrs.last_execution_time) BETWEEN 1 AND 6' + @nc10;
2081+
SELECT
2082+
@where_clause += N'AND DATEPART(WEEKDAY, qsrs.last_execution_time) BETWEEN 1 AND 6' + @nc10;
21572083
END;/*df 1*/
21582084

21592085
IF @df = 7
21602086
BEGIN
2161-
SELECT
2162-
@where_clause += N'AND DATEPART(WEEKDAY, qsrs.last_execution_time) BETWEEN 2 AND 6' + @nc10;
2087+
SELECT
2088+
@where_clause += N'AND DATEPART(WEEKDAY, qsrs.last_execution_time) BETWEEN 2 AND 6' + @nc10;
21632089
END;/*df 7*/
21642090

2165-
IF @work_start_int IS NOT NULL
2166-
AND @work_end_int IS NOT NULL
2091+
IF @work_start_utc IS NOT NULL
2092+
AND @work_end_utc IS NOT NULL
21672093
BEGIN
2094+
/*
2095+
depending on local TZ, work time might span midnight UTC;
2096+
account for that by splitting the interval into before/after midnight.
2097+
for example:
2098+
[09:00 - 17:00] PST
2099+
= [17:00 - 01:00] UTC
2100+
= [17:00 - 00:00) + [00:00 - 01:00] UTC
2101+
2102+
NB: because we don't have the benefit of the context of what day midnight
2103+
is occurring on, we have to rely on the behavior from the documentation of
2104+
the time DT of higher to lower precision resulting in truncation to split
2105+
the interval. i.e. 23:59:59.9999999 -> 23:59:59. which should make that
2106+
value safe to use as the endpoint for our "before midnight" interval.
2107+
*/
2108+
IF (@work_start_utc < @work_end_utc)
2109+
SELECT
2110+
@where_clause += N'AND CAST(qsrs.last_execution_time as time(0)) BETWEEN @work_start_utc AND @work_end_utc' + @nc10;
2111+
ELSE
21682112
SELECT
2169-
@where_clause += N'AND DATEPART(HOUR, qsrs.last_execution_time) BETWEEN @work_start_int AND @work_end_int' + @nc10;
2113+
@where_clause += N'AND (' + @nc10 +
2114+
' CAST(qsrs.last_execution_time as time(0)) BETWEEN @work_start_utc AND ''23:59:59'' ' + @nc10 +
2115+
' OR CAST(qsrs.last_execution_time as time(0)) BETWEEN ''00:00:00'' AND @work_end_utc' + @nc10 +
2116+
')' + @nc10;
21702117
END; /*Work hours*/
21712118
END; /*Final end*/
21722119

@@ -3679,8 +3626,8 @@ EXEC sys.sp_executesql
36793626
@execution_type_desc,
36803627
@database_id,
36813628
@queries_top,
3682-
@work_start_int,
3683-
@work_end_int;
3629+
@work_start_utc,
3630+
@work_end_utc;
36843631

36853632
IF @troubleshoot_performance = 1
36863633
BEGIN
@@ -3864,8 +3811,8 @@ EXEC sys.sp_executesql
38643811
@execution_type_desc,
38653812
@database_id,
38663813
@queries_top,
3867-
@work_start_int,
3868-
@work_end_int;
3814+
@work_start_utc,
3815+
@work_end_utc;
38693816

38703817
IF @troubleshoot_performance = 1
38713818
BEGIN
@@ -5207,8 +5154,6 @@ BEGIN
52075154
#dm_exec_query_stats;
52085155
TRUNCATE TABLE
52095156
#query_types;
5210-
TRUNCATE TABLE
5211-
#am_pm;
52125157
END;
52135158

52145159
FETCH NEXT
@@ -7698,10 +7643,10 @@ BEGIN
76987643
@utc_minutes_original,
76997644
df =
77007645
@df,
7701-
work_start_int =
7702-
@work_start_int,
7703-
work_end_int =
7704-
@work_end_int;
7646+
work_start_utc =
7647+
@work_start_utc,
7648+
work_end_utc =
7649+
@work_end_utc;
77057650

77067651
IF EXISTS
77077652
(
@@ -8440,29 +8385,6 @@ BEGIN
84408385
result =
84418386
'#troubleshoot_performance is empty';
84428387
END;
8443-
8444-
IF EXISTS
8445-
(
8446-
SELECT
8447-
1/0
8448-
FROM #am_pm AS ap
8449-
)
8450-
BEGIN
8451-
SELECT
8452-
table_name =
8453-
'#am_pm',
8454-
ap.*
8455-
FROM #am_pm AS ap
8456-
ORDER BY
8457-
ap.t24
8458-
OPTION(RECOMPILE);
8459-
END;
8460-
ELSE
8461-
BEGIN
8462-
SELECT
8463-
result =
8464-
'#troubleshoot_performance is empty';
8465-
END;
84668388
RETURN; /*Stop doing anything, I guess*/
84678389
END; /*End debug*/
84688390
RETURN; /*Yeah sure why not?*/

0 commit comments

Comments
 (0)