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

2022-05-24 0 1,359

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

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

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 MsSql 在SQLServer上查看SQL语句的执行时间的方法 https://www.niceym.com/60057.html