You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Stored_Procedure/sp_BenchmarkTSQL.sql
+24-17Lines changed: 24 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,23 @@
1
-
IFOBJECT_ID('dbo.sp_BenchmarkTSQL', 'P') ISNULL
1
+
IFOBJECT_ID('dbo.sp_BenchmarkTSQL', 'P') ISNULL
2
2
EXECUTE ('CREATE PROCEDURE dbo.sp_BenchmarkTSQL AS SELECT 1;');
3
3
GO
4
4
5
5
6
6
ALTERPROCEDUREdbo.sp_BenchmarkTSQL(
7
-
@tsqlStatement NVARCHAR(MAX)
8
-
, @numberOfExecution INT=10
9
-
, @saveResults BIT=0
10
-
, @clearCache BIT=0
11
-
, @calcMedian BIT=0
12
-
, @printStepInfo BIT=1
13
-
, @durationAccuracy VARCHAR(10) ='ns'
7
+
@tsqlStatement NVARCHAR(MAX)
8
+
, @numberOfExecution INT=10
9
+
, @saveResults BIT=0
10
+
, @clearCache BIT=0
11
+
, @calcMedian BIT=0
12
+
, @printStepInfo BIT=1
13
+
, @durationAccuracy VARCHAR(3)='ns'
14
14
)
15
15
/*
16
16
.SYNOPSIS
17
-
Calculate SQL statement execution time, save results if need.
17
+
Calculate TSQL statement execution time, save results if needed.
18
18
19
19
.DESCRIPTION
20
-
Run SQL statement specified times, show results in ms, insert execution details into table dbo.BenchmarkTSQL (create if not exist).
20
+
Run SQL statement specified times, show results, insert execution details into table dbo.BenchmarkTSQL (create if not exist).
21
21
22
22
.PARAMETER @tsqlStatement
23
23
TSQL statement for benchmarking.
@@ -26,7 +26,7 @@ ALTER PROCEDURE dbo.sp_BenchmarkTSQL(
26
26
Number of execution TSQL statement.
27
27
28
28
.PARAMETER @saveResults
29
-
Save benchmark details to dbo.BenchmarkTSQL table.
29
+
Save benchmark details to dbo.BenchmarkTSQL table if @saveResults = 1.
30
30
31
31
.PARAMETER @clearCache
32
32
Clear cached plan for TSQL statement.
@@ -35,7 +35,10 @@ ALTER PROCEDURE dbo.sp_BenchmarkTSQL(
35
35
Calculate pseudo median of execution time.
36
36
37
37
.PARAMETER @printStepInfo
38
-
PRINT detailed step information: step count, start time, end time, duration
38
+
PRINT detailed step information: step count, start time, end time, duration.
39
+
40
+
.PARAMETER @durationAccuracy
41
+
Duration accuracy calculation, possible values: ns, mcs, ms, ss, s, mi, n, hh, wk, ww, dd, d.
39
42
40
43
.EXAMPLE
41
44
EXEC sp_BenchmarkTSQL @tsqlStatement = 'SELECT * FROM , sys.databases';
@@ -50,7 +53,7 @@ ALTER PROCEDURE dbo.sp_BenchmarkTSQL(
50
53
, @saveResults = 1
51
54
, @calcMedian = 1
52
55
, @clearCache = 1
53
-
, @printStepInfo = 0
56
+
, @printStepInfo = 1
54
57
, @durationAccuracy = 'ms';
55
58
56
59
.LICENSE MIT
@@ -78,6 +81,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
78
81
Author: Konstantin Taranov
79
82
Modified date: 2018-01-01
80
83
Version: 2.2
84
+
85
+
Author: Konstantin Taranov
86
+
Modified date: 2018-01-04
87
+
Version: 2.3
81
88
*/
82
89
AS
83
90
BEGINTRY
@@ -103,7 +110,7 @@ BEGIN TRY
103
110
, 'dd'-- day
104
111
, 'd'-- day
105
112
)
106
-
THROW55003, '@durationAccuracy can be only in this values: ns, mcs, ms, ss, s, mi, n, hh. See DATEDIFF https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql' , 1;
113
+
THROW55003, '@durationAccuracy can be only in this values: ns, mcs, ms, ss, s, mi, n, hh, wk, ww, dd, d. See DATEDIFF https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql' , 1;
107
114
108
115
IFEXISTS (
109
116
SELECT1
@@ -148,7 +155,7 @@ BEGIN TRY
148
155
, ClearCache BIT
149
156
, PrintStepInfo BIT
150
157
, DurationAccuracy VARCHAR(10)
151
-
);
158
+
);
152
159
153
160
SET @startTime =CONVERT(VARCHAR(27), CAST(CURRENT_TIMESTAMPASDATETIME2(7)), 121);
154
161
PRINT('Benchmark started at '+ @startTime +' by '+ @originalLogin);
@@ -163,7 +170,7 @@ BEGIN TRY
163
170
SELECT @plan_handle = plan_handle
164
171
FROMsys.dm_exec_cached_plans
165
172
CROSSAPPLYsys.dm_exec_sql_text(plan_handle)
166
-
WHERE [text] LIKE @tsqlStatement; -- LIKE instead = (equal) because = ignoring trailing spaces
173
+
WHERE [text] LIKE @tsqlStatement; -- LIKE instead = (equal) because = ignore trailing spaces
167
174
168
175
IF @plan_handle ISNOTNULL DBCC FREEPROCCACHE (@plan_handle);
0 commit comments