Capture And Display Execution Time Of SQL Query In SQL Server
Posted by in SQL Server May 31, 2011 1 Comment

This tutorial proposes 3 ways in order for you to get the Execution time of SQL Query or Stored Procedures are called or submitted to your SQL Server.

They will give you durations in microseconds and base on the execution time, you may have a deeper understand and will do some optimization for your database structure/indexing to make it runs better.

1. Using SQL Server Profiler

I think it’s a easiest way for you to trace/track Which Stored Procedures Or SQL Commands Are Running On SQL Server and how long it takes for each of SQL Query/ Stored Procedure execution.

SQL Server Profiler Execution Time

SQL Server Profiler Execution Time

As you see, all commands are in TextData column and all Execution time for each are in Duration column respectively.

2. Using SQL Script with @StartTime and @EndTime parameters

The script should be run on SQL Server Management Studio Query.

USE AdventureWorksDW;
GO 

DECLARE @StartTime datetime,@EndTime datetime

SELECT @StartTime=GETDATE()

SELECT * FROM DimCustomer where Gender = 'M'

SELECT @EndTime=GETDATE()

SELECT DATEDIFF(ms,@StartTime,@EndTime) AS [Duration in microseconds]

Just replace your own SQL statements with line 2, after execute the statement, it will show the Duration in microseconds in another result panel.

Get Execution Time With Start Time And End Time

Get Execution Time With Start Time And End Time

3. Using SQL Script with SET STATISTICS TIME (Transact-SQL)

It displays the number of milliseconds required to parse, compile, and execute each statement.

Run this SQL script on your SQL Query:

USE AdventureWorksDW;
GO 

SET STATISTICS TIME ON
GO

SELECT * FROM DimCustomer where Gender = 'M'
Go

SET STATISTICS TIME OFF;
GO

And below is the result set:

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 1 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 1 ms.

(9351 row(s) affected)

SQL Server Execution Times:
   CPU time = 63 ms,  elapsed time = 479 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 1 ms.
Get Execution Time With SET STATISTICS TIME

Get Execution Time With SET STATISTICS TIME

That’s all! Feel free to contribute your own solution by submitting your comments as you’re always be welcome.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.facebook.com/j.pacuta Honza Pačuta

    Results in these examples are in MILLISECONDS not microseconds. Datetime data type has an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds).

    You can get duration in milliseconds when you use SQL Server 2008 and up and its function SYSDATETIME.
    E.g.:

    USE AdventureWorksDW;
    GO

    DECLARE @StartTime datetime2,@EndTime datetime2 

    SELECT @StartTime=SYSDATETIME() 

    SELECT * FROM DimCustomer where Gender = ‘M’

    SELECT @EndTime=SYSDATETIME() 
    SELECT DATEDIFF(mcs,@StartTime,@EndTime) AS [Duration in microseconds]