STOPWATCH is used to simply measure queries, especially long, complex ones. This is not a replacement for set statistics time on but it's a handy way to measure query behavior in a way that feels comfortable to C# developers, especially. STOPWATCH uses the system time to calculate elapsed durations.
Note: STOPWATCH uses SQL Server's
session_contextto persist the start time.
Example usage
EXEC STOPWATCH_START
SELECT * FROM USERS
EXEC STOPWATCH_READExample output
Start: 2021-03-23 17:50:52.2300000
(1 row affected)
Delta: 00:00:00.1200000
Use STOPWATCH_START to trigger the moment in time to base elapsed duration.
print: (optional/default=false): prints the current time when called.message: (optional/default=null): A custom message to append to time.
Syntax
DECLARE @print BIT = 1;
DECLARE @message VARCHAR(50) = 'Jerry Nixon';
EXEC STOPWATCH_START @print, @message;Result Time is logged and printed with custom message.
Use STOPWATCH_READ to print the elapsed duration.
message: (optional/default=null): A custom message to append to the elapsed duration.
Syntax
DECLARE @message VARCHAR(50) = 'Jerry Nixon';
EXEC STOPWATCH_READ @message;Result Elapsed duration is printed with custom message.