简体   繁体   English

计算SQL查询的执行时间?

[英]Calculate execution time of a SQL query?

I am providing search functionality in my website, when user searches a record then I want to display the time the query taken to get the results same as google does. 我在我的网站上提供搜索功能,当用户搜索记录然后我想显示查询所需的时间以获得与谷歌相同的结果。 When we search anything then google displays how much time it takes to get results? 当我们搜索任何内容时,谷歌会显示获得结果所需的时间吗?

For this I have declared a @start variable in my SP and finding the difference in the end, as below; 为此我在我的SP中声明了一个@start变量,并在最后找到差异,如下所示;

DECLARE @start_time DATETIME

SET @start_time = GETDATE()

-- my query
SELECT * FROM @search_temp_table

SELECT RTRIM(CAST(DATEDIFF(MS, @start_time, GETDATE()) AS CHAR(10))) AS 'TimeTaken'

Is there any other easy and fast way, or a single line of query with which we can find out the time a query taken to execute? 有没有其他简单快捷的方法,或者单行查询,我们可以通过它查找执行查询的时间?

I'm using SQL Server 2005. 我正在使用SQL Server 2005。

Well, If you really want to do it in your DB there is a more accurate way as given in MSDN : 好吧,如果您真的想在数据库中执行此操作,则可以使用MSDN中提供的更准确的方法:

SET STATISTICS TIME ON

You can read this information from your application as well. 您也可以从您的应用程序中读取此信息。

I found this one more helpful and simple 我发现这个更有帮助和简单

DECLARE @StartTime datetime,@EndTime datetime   
SELECT @StartTime=GETDATE() 
--Your Query to be run goes here--  
SELECT @EndTime=GETDATE()   
SELECT DATEDIFF(ms,@StartTime,@EndTime) AS [Duration in milliseconds]   

We monitor this from the application code, just to include the time required to establish/close the connection and transmit data across the network. 我们从应用程序代码中监视这一点,只是为了包括建立/关闭连接和通过网络传输数据所需的时间。 It's pretty straight-forward... 这很直截了当......

Dim Duration as TimeSpan
Dim StartTime as DateTime = DateTime.Now

'Call the database here and execute your SQL statement

Duration = DateTime.Now.Subtract(StartTime)
Console.WriteLine(String.Format("Query took {0} seconds", Duration.TotalSeconds.ToString()))
Console.ReadLine()

Please use 请用

-- turn on statistics IO for IO related 
SET STATISTICS IO ON 
GO

and for time calculation use 并用于计算时间

SET STATISTICS TIME ON
GO

then It will give result for every query . 然后它将为每个查询提供结果。 In messages window near query input window. 在查询输入窗口附近的消息窗口中。

declare @sttime  datetime
set @sttime=getdate()
print @sttime
Select * from ProductMaster   
SELECT RTRIM(CAST(DATEDIFF(MS, @sttime, GETDATE()) AS CHAR(10))) AS 'TimeTaken'    

Why are you doing it in SQL? 你为什么在SQL中这样做? Admittedly that does show a "true" query time as opposed to the query time + time taken to shuffle data each way across the network, but it's polluting your database code. 不可否认,这确实显示了“真正的”查询时间,而不是查询时间+在网络中每次移动数据所花费的时间,但它会污染您的数据库代码。 I doubt that your users will care - in fact, they'd probably rather include the network time, as it all contributes to the time taken for them to see the page. 我怀疑你的用户会关心 - 事实上,他们可能更愿意包括网络时间,因为它们都有助于他们看到页面所花费的时间。

Why not do the timing in your web application code? 为什么不在Web应用程序代码中进行计时? Aside from anything else, that means that for cases where you don't want to do any timing, but you want to execute the same proc, you don't need to mess around with something you don't need. 除了别的,这意味着,你不想做任何时机,但要执行相同的PROC情况下,你不需要浪费时间的东西你不需要。

You can use 您可以使用

SET STATISTICS TIME { ON | OFF }

Displays the number of milliseconds required to parse, compile, and execute each statement 显示解析,编译和执行每个语句所需的毫秒数

When SET STATISTICS TIME is ON, the time statistics for a statement are displayed. 当SET STATISTICS TIME为ON时,将显示语句的时间统计信息。 When OFF, the time statistics are not displayed OFF时,不显示时间统计

USE AdventureWorks2012;  
GO         
SET STATISTICS TIME ON;  
GO  
SELECT ProductID, StartDate, EndDate, StandardCost   
FROM Production.ProductCostHistory  
WHERE StandardCost < 500.00;  
GO  
SET STATISTICS TIME OFF;  
GO  

try this 试试这个

DECLARE @StartTime DATETIME
SET @StartTime = GETDATE()

  SET @EndTime = GETDATE()
  PRINT 'StartTime = ' + CONVERT(VARCHAR(30),@StartTime,121)
  PRINT '  EndTime = ' + CONVERT(VARCHAR(30),@EndTime,121)
  PRINT ' Duration = ' + CONVERT(VARCHAR(30),@EndTime -@starttime,114)

If that doesn't do it, then try SET STATISTICS TIME ON 如果没有,请尝试SET STATISTICS TIME ON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM