使用Microsoft SQL Server查询时如何获得计时

时间:2017-10-31 06:08:10

标签: sql-server shell tsql

我正在测试Microsoft SQL Server,我想检查执行查询所需的时间。

3 个答案:

答案 0 :(得分:2)

在查询窗口中放置SQL,如下所示:

SET STATISTICS TIME ON;  

--Your Query Goes Here 
SELECT * FROM Table

SET STATISTICS TIME OFF;  

答案 1 :(得分:1)

Declare @startDate datetime
Declare @endDate datetime
begin

set @startDate=GETDATE()
select * from Table1
set @endDate=GETDATE()
 select DATEDIFF(ms,@startDate,@endDate) as 'Execution Time '


     /*
 ms is for millisecond you can also use following argument
 minute mi, n
second  ss, s
millisecond ms
microsecond mcs
nanosecond  ns
 */
 end

答案 2 :(得分:0)

DECLARE @EndT datetime
DECLARE @StartT datetime 
SELECT @StartT=GETDATE() 

-- Write Your Query


SELECT @EndT=GETDATE()

--This will return execution time of your query
SELECT DATEDIFF(ms,@StartT,EndT) AS [Execution Time in millisecs]

有关DATEDIFF的更多信息,请参阅https://docs.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql