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

来源:互联网 发布:淘宝买欧式家具靠谱吗 编辑:程序博客网 时间:2024/05/21 08:02

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* FROMDimCustomer whereGender = 'M'
 
SELECT@EndTime=GETDATE()
 
SELECTDATEDIFF(ms,@StartTime,@EndTime) AS[Duration inmicroseconds]

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
 
SETSTATISTICS TIME ON
GO
 
SELECT* FROMDimCustomer whereGender = 'M'
Go
 
SETSTATISTICS 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