Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Stored Procedures execution time in SQL Server?

Tags:

sql

sql-server

I have 10 stored procedures as follows: SP1,SP2,.....,SP10 These stored procedures do some stuff. I need to run these procedures as follows: EXECUTE SP1; EXECUTE SP2; ... EXECUTE SP10;

When SQL server finshes to complete execution of these procedures, it gives ten lines showing any row changes caused by all these stored procedures. What i want to do is that after Execution of all stored procedures SQL Server also gives in output window the execution time for each Stored Procedures. Can i do this? I am sure that for achieving this task i need to modify stored procedures but i don't have any idea how to do it... Please help me. Thanks.

like image 813
Azeem Avatar asked Oct 14 '11 11:10

Azeem


People also ask

How is stored procedure execution time calculated?

You need the actual execution plan. You can see these from running the SQL query in SQL Management studio with the right button pressed (they'll appear as a tab in the results window). This will show you what parts of the sproc is being run and the time taken up by each bit - just what you want to tune.

How do I find the last execution time of a procedure in SQL Server?

The type_desc column includes the object type while the execution_count column shows the number of times the stored procedure has been executed since it was last compiled. This can be useful information when researching performance issues.

How do I see SQL execution time?

Using Client StatisticsGo to Menu >> Query >> Select Include client Statistics. Execute your query. In the results panel, you can see a new tab Client Statistics. Go to the Client Statistics tab to see the execution time.


3 Answers

Assuming management studio or some other environment with an output pane you can;

SET STATISTICS TIME ON
EXEC SP1
EXEC SP2
...
SET STATISTICS TIME OFF
like image 145
Alex K. Avatar answered Oct 17 '22 11:10

Alex K.


You can use Sql Server Profiler for this purposes it provides a lot of useful info along the each executed query and Stored procedure as well.

MSDN: SQL Profiler Data Columns

SQL Profiler displays the execution time for each event

An other straightforward way:

  1. DECLARE 2 datetime variables: start/end
  2. SET start = GETDATE()
  3. EXEC SP_NAME
  4. SET end = GETDATE()
  5. Execution time - difference between end and start
like image 17
sll Avatar answered Oct 17 '22 11:10

sll


declare @start datetime = getdate()

-- your SQL statements
exec dbo.MyStoredProcedure

declare @executionTimeInMilliseconds int = datediff(ms, @start, getdate())
like image 8
Illia Ratkevych Avatar answered Oct 17 '22 10:10

Illia Ratkevych