测量执行 t-sql 查询所需的时间 [英] Measure the time it takes to execute a t-sql query

查看:57
本文介绍了测量执行 t-sql 查询所需的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用 SqlServer 2005 的 t-sql 查询.我如何测量每个查询运行需要多长时间?

I have two t-sql queries using SqlServer 2005. How can I measure how long it takes for each one to run?

使用我的秒表并不能解决问题.

Using my stopwatch doesn't cut it.

推荐答案

测量事件之间经过的时间"的一种简单方法是仅获取当前日期和时间.

One simplistic approach to measuring the "elapsed time" between events is to just grab the current date and time.

在 SQL Server Management Studio 中

In SQL Server Management Studio

SELECT GETDATE();
SELECT /* query one */ 1 ;
SELECT GETDATE();
SELECT /* query two */ 2 ; 
SELECT GETDATE(); 

要计算经过的时间,您可以将这些日期值提取到变量中,然后使用 DATEDIFF 函数:

To calculate elapsed times, you could grab those date values into variables, and use the DATEDIFF function:

DECLARE @t1 DATETIME;
DECLARE @t2 DATETIME;

SET @t1 = GETDATE();
SELECT /* query one */ 1 ;
SET @t2 = GETDATE();
SELECT DATEDIFF(millisecond,@t1,@t2) AS elapsed_ms;

SET @t1 = GETDATE();
SELECT /* query two */ 2 ;
SET @t2 = GETDATE();
SELECT DATEDIFF(millisecond,@t1,@t2) AS elapsed_ms;

这只是一种方法.您还可以使用 SQL Profiler 获取查询的经过时间.

That's just one approach. You can also get elapsed times for queries using SQL Profiler.

这篇关于测量执行 t-sql 查询所需的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆