Monday, 6 January 2014

TSQL - Timing SQL Queries

Sometimes the best way to narrow down performance issues is just to time the various statement blocks in  TSQL. This little piece of code will do just that for you.

SET STATISTICS TIME OFF--This method allows you to pick when the stopwatch starts and ends which can be more meaningful
--Start the stop watch
DECLARE @Time1 DATETIME, @Time2 DATETIME, @i INT
SELECT
@Time1 = GETDATE(), @i=0--Use this to stop the stopwatchSELECT @Time2 = GETDATE(), @i=@i+1 PRINT 'TIME IS ' + CONVERT(CHAR, @Time2, 14) + ', i = ' + CONVERT(CHAR, @i) + ', TIMEDIFF = ' + CONVERT (CHAR, DATEDIFF(ms, @Time1, @Time2))--Reset the stop watchSET @Time1 = @Time2

No comments: