查看Sql Server语句执行效率,时间的方法

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.

?
01
02
03
04
05
06
07
08
09
10
11
12
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:

?
01
02
03
04
05
06
07
08
09
10
11
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:

?
01
02
03
04
05
06
07
08
09
10
11
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.

- See more at: http://4rapiddev.com/sql-server/capture-and-display-execution-time-of-sql-query-in-sql-server/#sthash.OgMz1Qq4.dpuf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值