Learning Horizon | For Learners

ASP.NET, SQL SERVER, JQUERY,JAVASCRIPT, WEBSPHERE

Friday 10 May 2013

How To Find Execution Time Of Stored Procedure | Sql Server

In this tutorial I am going to tell you how we can find the execution time of stored procedure in Sql server.

First Method: -

  DECLARE @FIRST DATETIME
  DECLARE @SECOND DATETIME
  
  SET @FIRST = GETDATE()

-- Execute your stored procedure here. i.e exec name of your SP

  EXEC STORED PROCEDURE 

  SET @SECOND = GETDATE()

  SELECT DATEDIFF(SECOND,@FIRST,@SECOND)  AS TOTAL_TIME

Second Method: -
You can use this method as well to find execution time and it will return you timing of all single queries inside your stored procedure. So if you have bunch of queries inside then it might be not very convenient.

 
 SET STATISTICS TIME ON
-- Execute your stored procedure here. i.e exec name of your SP
 EXEC STORED PROCEDURE 
 SET STATISTICS TIME OFF

Third Method: -

  DECLARE @T DATETIME
  SET @T = CURRENT_TIMESTAMP
  -- Execute your stored procedure here. i.e exec name of your SP
  EXEC STORED PROCEDURE 
  SELECT DATEDIFF(SECOND,@T,CURRENT_TIMESTAMP) AS TOTAL_TIME

If you want to measure execution time in milliseconds then it is also simple
    
   DECLARE @T DATETIME
   SET @T = CURRENT_TIMESTAMP
   -- Execute your stored procedure here. i.e exec name of your SP
   EXEC STORED PROCEDURE 
   SELECT DATEDIFF(MILLISECOND,@T,CURRENT_TIMESTAMP) AS TOTAL_TIME

No comments:

Post a Comment

Please do not enter spam links.