Skip to content

Commit 7a9919f

Browse files
Merge fix/query-store-cleanup-batch-pacing into dev
2 parents 1057e2b + 48315e8 commit 7a9919f

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

sp_QueryStoreCleanup/sp_QueryStoreCleanup.sql

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ALTER PROCEDURE
4040
@dedupe_by varchar(50) = 'all', /*deduplication strategy: all, query_hash, plan_hash, none*/
4141
@min_age_days integer = NULL, /*only remove queries not executed in this many days*/
4242
@report_only bit = 0, /*1 = report what would be removed without removing*/
43+
@pause_milliseconds integer = 0, /*sleep this many ms between each remove to pace log writes on bulk purges; 0 = no sleep (default)*/
4344
@debug bit = 0, /*prints dynamic sql and diagnostics*/
4445
@help bit = 0, /*prints help information*/
4546
@version varchar(30) = NULL OUTPUT, /*OUTPUT; for support*/
@@ -96,6 +97,8 @@ BEGIN
9697
THEN 'only remove queries whose last execution is older than this many days; NULL = no age filter'
9798
WHEN N'@report_only'
9899
THEN 'report what would be removed without removing'
100+
WHEN N'@pause_milliseconds'
101+
THEN 'sleep this many ms between each sp_query_store_remove_query call so a bulk purge does not hammer the log; 0 = no sleep'
99102
WHEN N'@debug'
100103
THEN 'prints dynamic sql and diagnostics'
101104
WHEN N'@help'
@@ -120,6 +123,8 @@ BEGIN
120123
THEN 'any positive integer, e.g. 7, 30, 90'
121124
WHEN N'@report_only'
122125
THEN '0 or 1'
126+
WHEN N'@pause_milliseconds'
127+
THEN 'any non-negative integer, e.g. 25, 100, 500'
123128
WHEN N'@debug'
124129
THEN '0 or 1'
125130
WHEN N'@help'
@@ -144,6 +149,8 @@ BEGIN
144149
THEN 'NULL; no age filter'
145150
WHEN N'@report_only'
146151
THEN '0'
152+
WHEN N'@pause_milliseconds'
153+
THEN '0'
147154
WHEN N'@debug'
148155
THEN '0'
149156
WHEN N'@help'
@@ -939,6 +946,35 @@ OPTION(RECOMPILE);';
939946
FROM @c
940947
INTO @query_id;
941948

949+
/*
950+
Precompute the WAITFOR DELAY string once — WAITFOR's argument has to
951+
be a varchar literal or variable, so we convert @pause_milliseconds
952+
to hh:mm:ss.mmm format up front. Clamp to [0, 60000] to avoid
953+
surprising 10-minute sleeps from a fat-finger and negative values
954+
that would raise at the WAITFOR.
955+
*/
956+
DECLARE
957+
@pause_delay varchar(12) = N'';
958+
959+
IF @pause_milliseconds IS NOT NULL
960+
AND @pause_milliseconds > 0
961+
BEGIN
962+
IF @pause_milliseconds > 60000
963+
BEGIN
964+
SELECT
965+
@pause_milliseconds = 60000;
966+
END;
967+
968+
SELECT
969+
@pause_delay =
970+
CONVERT
971+
(
972+
varchar(12),
973+
DATEADD(MILLISECOND, @pause_milliseconds, CONVERT(time(3), '00:00:00')),
974+
114
975+
);
976+
END;
977+
942978
WHILE @@FETCH_STATUS = 0
943979
BEGIN
944980
SELECT
@@ -963,6 +999,11 @@ OPTION(RECOMPILE);';
963999
RAISERROR('Query %I64d of %I64d: query_id %I64d not removed (%s)', 0, 1, @current, @total, @query_id, @error_message) WITH NOWAIT;
9641000
END CATCH;
9651001

1002+
IF @pause_delay <> N''
1003+
BEGIN
1004+
WAITFOR DELAY @pause_delay;
1005+
END;
1006+
9661007
FETCH NEXT
9671008
FROM @c
9681009
INTO @query_id;

0 commit comments

Comments
 (0)