如何查看SqlServer查询语句的执行效率

如何查看SqlServer查询语句的执行效率

一:查看执行时间

SQL Server DATEDIFF() 函数

定义和用法

DATEDIFF() 函数返回两个日期之间的时间。
        语法
DATEDIFF(datepart,startdate,enddate)
startdate 和 enddate 参数是合法的日期表达式。
datepart 参数可以是下列的值:

            datepart                  缩写
yy, yyyy
季度 qq, q
mm, m
年中的日 dy, y
dd, d
wk, ww
星期 dw, w
小时 hh
分钟 mi, n
ss, s
毫秒 ms
微妙 mcs
纳秒 ns

使用方式:(通过这种方式我们可以指定查询时间的数量级)

 

declare @begin_date datetime 
declare @end_date datetime 
select @begin_date = getdate() 
  --此处为需要查询的sql脚本
select @end_date = getdate() 
select datediff( ms,@begin_date,@end_date) as '用时/毫秒'

 二、查看执行效率详细信息

使用方式:通过这种方式我们得到更全面的执行信息,比如扫描次数,逻辑读次数,物理读次数,IO耗时,CPU耗时等,有利于我们优化sql语句

SET STATISTICS PROFILE ON 
SET STATISTICS IO ON 
SET STATISTICS TIME ON
GO 
  --此处为需要查询的sql脚本 
GO 
SET STATISTICS PROFILE OFF 
SET STATISTICS IO OFF 
SET STATISTICS TIME OFF

  通过上述方式查询结果如下:

SQL Server 分析和编译时间:
 CPU 时间= 0 毫秒,耗费时间= 0 毫秒。 
SQL Server 执行时间: 
 CPU 时间= 0 毫秒,耗费时间= 0 毫秒。 
SQL Server 执行时间:
 CPU 时间= 0 毫秒,耗费时间= 0 毫秒。 
SQL Server 分析和编译时间:
 CPU 时间= 0 毫秒,耗费时间= 0 毫秒。 
表'_XT_USER'。扫描计数1,逻辑读63 次,物理读0 次,预读0 次。 
SQL Server 执行时间:
 CPU 时间= 0 毫秒,耗费时间= 69 毫秒。 (2042 行受影响) 
SQL Server 执行时间:
 CPU 时间= 0 毫秒,耗费时间= 69 毫秒。 (2 行受影响) 
SQL Server 执行时间:
 CPU 时间= 0 毫秒,耗费时间= 69 毫秒。 
SQL Server 分析和编译时间:
 CPU 时间= 0 毫秒,耗费时间= 0 毫秒。 
SQL Server 执行时间:
 CPU 时间= 0 毫秒,耗费时间= 0 毫秒。

 

猜你喜欢

转载自zq782388989.iteye.com/blog/2144813