在SQLServer上查看SQL语句的执行时间的方法

发布时间:2022-04-18 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在SQLServer上查看SQL语句的执行时间的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1:下面这种是比较简单查询方法,通过查询前的时间和查询后的时间差来计算的

复制代码 代码如下:

declare @begin_date datetime
declare @end_date datetime
select @begin_date = getdate()
<这里写上你的语句...>
select @end_date = getdate()
select datediff(ms,@begin_date,@end_date) as '用时/毫秒'


2:下面这种方法比较全面,将执行每个语句时采取的步骤作为行集返回,通过层次结构树的形式展示出来
复制代码 代码如下:

set statistics PRofile on
set statistics io on
set statistics time on
go
<这里写上你的语句...>
go
set statistics profile off
set statistics io off
set statistics time off

有时候我们需要知道一段SQL语句执行的时间有多长。下面是其中一种办法。

declare @predate Datetime
select @preDate = getdate()
print @preDate
select datepart(ms,@preDate) as ms, datepart(ss,@preDate) as second,datepart(mi,@preDate) as minute

go
--需要测试的SQL语句
go

declare @preDate Datetime
SELECT @preDate= getdate()
select datepart(ms,@preDate) as ms, datepart(ss,@preDate) as second,datepart(mi,@preDate) as minute

这样可以知道你的语句到底运行了多少毫秒。

脚本宝典总结

以上是脚本宝典为你收集整理的在SQLServer上查看SQL语句的执行时间的方法全部内容,希望文章能够帮你解决在SQLServer上查看SQL语句的执行时间的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。