Thursday, May 1, 2008

How to find the time taken (total elapsed time) by the query?

Though SQL Server provides SET STATISTICS TIME option to find the total elapsed time taken by a query or a sp, I generally do not use this because, for large number of statement reading the output of Statistics time will be difficult. It is better to keep your own script to get the elapsed time. Here is the script

@total_elapsed_time VARCHAR(100),
@start_time Datetime,
@complete_time Datetime

SELECT @start_time = GETDATE()
Print @start_time
-- here Paste the query which you want to execute
select *From sysobjects ,sys.tables – Replace this query with your query /sp

Set @complete_time=getdate()

@total_elapsed_time = 'Total Elapsed Time (minutes:seconds) ' +
DATEDIFF(SS,@start_time,@complete_time)/60) +
':' +
print @total_elapsed_time

Note : If you want to know how long compilation and optimization took then use SET STATISTICS TIME

No comments:

Locations of visitors to this page