Skip to content

Commit 02dece1

Browse files
committed
Add Estimate When Long Running SQL Processes Will Finish script
Fix and test udf_PatternSplitLoop
1 parent 789087a commit 02dece1

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Author: Tim Ford
3+
Original link: http://sqlmag.com/database-administration/estimate-when-long-running-sql-processes-will-finish
4+
*/
5+
6+
7+
SET NOCOUNT ON;
8+
9+
SELECT R.session_id
10+
, R.percent_complete
11+
, R.total_elapsed_time/1000 AS elapsed_seconds
12+
, R.wait_type
13+
, R.wait_time
14+
, R.last_wait_type,
15+
, DATEADD(s,100/((R.percent_complete)/ (R.total_elapsed_time/1000)), R.start_time) AS est_complete_time
16+
, ST.text AS batch_text
17+
, CAST(SUBSTRING(ST.text, R.statement_start_offset / 2,
18+
(
19+
CASE WHEN R.statement_end_offset = -1 THEN DATALENGTH(ST.text)
20+
ELSE R.statement_end_offset
21+
END - R.statement_start_offset
22+
) / 2
23+
) AS varchar(1024)) AS statement_executing
24+
FROM sys.dm_exec_requests AS R
25+
CROSS APPLY sys.dm_exec_sql_text(R.sql_handle) AS ST
26+
WHERE R.percent_complete > 0
27+
AND R.session_id <> @@spid;

User_Defined_Function/udf_PatternSplitLoop.sql

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ CREATE FUNCTION [dbo].[udf_PatternSplitLoop]
88
) RETURNS
99
@Results TABLE ( ItemNumber INT
1010
,Item VARCHAR(400)
11-
,[Matched] INT )
11+
,[Matched] INT)
1212
WITH SCHEMABINDING
1313
AS
1414
BEGIN;
1515

16-
-- DECLARE a couple of variables we'll need in our loop
16+
-- DECLARE a couple of variables we'll need in our loop
1717
DECLARE
1818
@ItemNumber INT = 0
19-
, @Remaining VARCHAR(400) = ISNULL(@String, '')
19+
, @Remaining VARCHAR(400) = ISNULL(@String, '')
2020
-- Create the "not pattern"
2121
, @NotPattern VARCHAR(500) = REPLACE(REPLACE(@Pattern, '[', '[^'), '^^', '')
2222
, @Matched INT
@@ -27,14 +27,15 @@ IF @String IS NULL OR @Pattern IS NULL
2727
WHILE DATALENGTH(@Remaining) > 0
2828
BEGIN
2929
SELECT @ItemNumber = @ItemNumber + 1
30-
-- The item returned from the cascaded CROSS APPLY b below ,@String = CASE
31-
-- When a+b = 1, then either a=1 and b=0 (the pattern was found but not pattern
32-
-- was not found) or a=0 and b=1 (the not pattern was found but pattern was
30+
-- The item returned from the cascaded CROSS APPLY b below
31+
,@String = CASE
32+
-- When a+b = 1, then either a=1 and b=0 (the pattern was found but not pattern
33+
-- was not found) or a=0 and b=1 (the not pattern was found but pattern was
3334
-- not found).
34-
-- This means that no meaninful patterns are found in what remains so we’re done.
35-
WHEN a+b = 1 THEN @Remaining
36-
-- This case returns the chunk up to the start of the next pattern/not pattern
37-
WHEN (a=1 AND b>0) OR (b=1 AND a>0) THEN SUBSTRING(@Remaining, 1, CASE a
35+
-- This means that no meaninful patterns are found in what remains so we’re done.
36+
WHEN a+b = 1 THEN @Remaining
37+
-- This case returns the chunk up to the start of the next pattern/not pattern
38+
WHEN (a=1 AND b>0) OR (b=1 AND a>0) THEN SUBSTRING(@Remaining, 1, CASE a
3839
WHEN 1 THEN b
3940
ELSE a
4041
END - 1)
@@ -49,10 +50,10 @@ WHILE DATALENGTH(@Remaining) > 0
4950
-- Now that we have our ItemNumber and Item (in @String) INSERT them into our results
5051
INSERT INTO @Results SELECT @ItemNumber, @String, @Matched
5152

52-
-- Find the remaining characters in the string
53+
-- Find the remaining characters in the string
5354
SELECT @Remaining = CASE
5455
WHEN DATALENGTH(@Remaining) = DATALENGTH(@String) THEN ''
55-
ELSE SUBSTRING(@Remaining, DATALENGTH(@String)+1, DATALENGTH(@Remaining))
56+
ELSE SUBSTRING(@Remaining, DATALENGTH(@String)+1, DATALENGTH(@Remaining))
5657
END
5758
END
5859

0 commit comments

Comments
 (0)