54

I am able to view the Estimated Execution Plan (Management Studio 9.0) for a query without a problem but when it comes to stored procedures I do not see an easy way to do this without copying the code from the ALTER screen and pasting it into a query window, otherwise it will show the plan for the ALTER and not the procedure. Even after doing this, any inputs are missing and I would need to DECLARE them as such.

Is there an easier way to do this on stored procedures?

Edit: I just thought of something that might work but I am not sure.

Could I do the estimated execution plan on

exec myStoredProc 234

9 Answers 9

67
SET SHOWPLAN_ALL ON
GO

-- FMTONLY will not exec stored proc
SET FMTONLY ON
GO

exec yourproc
GO

SET FMTONLY OFF
GO

SET SHOWPLAN_ALL OFF
GO
7
  • 1
    Why isn't this documented anywhere? Apr 28, 2009 at 15:02
  • 'FMT_ONLY' is not a recognized SET option. Apr 28, 2009 at 15:06
  • I think it's FMTONLY not FMT_ONLY Apr 28, 2009 at 15:11
  • Although it's not the visual estimated execution plan, it still does the job! Apr 28, 2009 at 15:20
  • 22
    BTW, if you want to have the graphical plan, replace SHOWPLAN_ALL with SHOWPLAN_XML. You can then click the underligned XML in SSMS.
    – PollusB
    Jan 5, 2015 at 20:45
37

Select the storedprocedure name (just type it in a query window), right click, and choose the 'Display Estimated Execution Plan' button in the toolbar of SQl Server Mgmt Studio. Note that you don't have to have the stored procedure code open. Just the procedure name has to be selected.

The plan for the stored procedure from with in the called procedures will also be displayed in graphical form.

1
  • Just for completeness, after selecting the "Your_Schema.Your.Stored_Proc_Name" one can press Ctrl + L, which is quick shortcut for "Display Estimated Execution Plan". Sep 21, 2022 at 9:22
4

I know answer was submitted a while ago but I find query below useful

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

SELECT  [ProcedureName]          =   OBJECT_NAME([ps].[object_id], [ps].[database_id]) 
       ,[ProcedureExecutes]      =   [ps].[execution_count] 
       ,[VersionOfPlan]          =   [qs].[plan_generation_num]
       ,[ExecutionsOfCurrentPlan]    =   [qs].[execution_count] 
       ,[Query Plan XML]         =   [qp].[query_plan]  

FROM       [sys].[dm_exec_procedure_stats] AS [ps]
       JOIN [sys].[dm_exec_query_stats] AS [qs] ON [ps].[plan_handle] = [qs].[plan_handle]
       CROSS APPLY [sys].[dm_exec_query_plan]([qs].[plan_handle]) AS [qp]
WHERE   [ps].[database_id] = DB_ID() 
       AND  OBJECT_NAME([ps].[object_id], [ps].[database_id])  = 'TEST'
3
  • I assume this gives historical information? Might not be useful on new sprocs Apr 26, 2017 at 17:43
  • give it a try, it gives sproc version, number of executions and query plans.
    – BI Dude
    Apr 26, 2017 at 17:47
  • i use it to troubleshoot sprocs where plan is not reused
    – BI Dude
    Apr 26, 2017 at 17:48
3

When executing a stored procedure in SQL Management Studio 2008 you can click Query -> Include Actual Execution Plan from the menu...its also on the tool bar

After reading through the comments executing seems to be an issue and to solve this issue i would recommend wrapping the execution of the stored procedure in a transaction rolling it back at the end

2
  • 1
    I can't actually run it though. It needs to be estimated. Apr 27, 2009 at 17:24
  • 1
    You could wrap it in a transaction and just not commit
    – Jon
    Apr 27, 2009 at 17:49
3

Use

SET SHOWPLAN_ALL ON
Go
exec myStoredProc 234
GO
SET SHOWPLAN_ALL OFF
GO

See http://msdn.microsoft.com/en-us/library/aa259203.aspx As long as you aren't using tmp tables i think this will work

1

There are quite a few ways to get the actual execution plan of a stored procedure.

SELECT
qp.query_plan, 
SQLText.text
FROM sys.dm_exec_cached_plans AS CP
 CROSS APPLY sys.dm_exec_sql_text( plan_handle)AS SQLText
 CROSS APPLY sys.dm_exec_query_plan( plan_handle)AS QP
 WHERE objtype = 'Proc' and cp.cacheobjtype = 'Compiled Plan'

looking at the plans on a production server with the statistics of the data in the production server may show a different plan then of a dev box with a smaller dataset.

There is a lot more data to look at, like how often is a procedure executed according to query cache

SELECT
    qp.query_plan, 
    CP.usecounts as [Executed], 
    DB_name(QP.dbid) as [Database],
    OBJECT_NAME(QP.objectid) as [Procedure],
    SQLText.text as [TSQL],
    so.create_date as [Procedure Created],
    so.modify_date as [Procedure  Modified]
FROM sys.dm_exec_cached_plans AS CP
CROSS APPLY sys.dm_exec_sql_text( plan_handle)AS SQLText
CROSS APPLY sys.dm_exec_query_plan( plan_handle)AS QP
join sys.objects as so on so.[object_id]=QP.objectid
WHERE objtype = 'Proc' and cp.cacheobjtype = 'Compiled Plan'

The XML query plan (the first column in both queries), contains the XML of the execution plan allowing you, in SSMS to click on it and view the actual plans but also allows you to scan for things you do not like to have like index scan or "god forbid" table scans.

SELECT
    qp.query_plan, 
    CP.usecounts as [Executed], 
    DB_name(QP.dbid) as [Database],
    OBJECT_NAME(QP.objectid) as [Procedure],
    SQLText.text as [TSQL],
    so.create_date as [Procedure Created],
    so.modify_date as [Procedure  Modified]
FROM sys.dm_exec_cached_plans AS CP
CROSS APPLY sys.dm_exec_sql_text( plan_handle)AS SQLText
CROSS APPLY sys.dm_exec_query_plan( plan_handle)AS QP
join sys.objects as so on so.[object_id]=QP.objectid
WHERE objtype = 'Proc' and cp.cacheobjtype = 'Compiled Plan'
and cast(qp.query_plan as nvarchar(max)) like '%loop%'

I sample this using a really bad way by casting the XML to string and then doing a wildcard search however XML queries are not things most do every day and string wildcards are easy for everyone.

0

Running the stored procedure in management studio (or query analyser) with show actual execution plan (from the query menu) enabled will show you the plan for the stored procedure after you have run it. If you cant run it there is show estimated execution plan (though in my experience that is often less accurate.)

4
  • You missed the point of my question. When I use "show estimated exec plan" it shows the plan for the ALTER, not the actual procedure. Apr 27, 2009 at 17:23
  • Sorry i wasnt clear i meant run i meant run the stored procedure rather than run the alter. In a new window exec MySP 'param1', 'param2' and set the estimated execution plan option
    – u07ch
    Apr 27, 2009 at 17:28
  • Ok, but either way I can't run the procedure because it will cause changes to my data. Apr 27, 2009 at 17:37
  • 1
    You don't have a test system?
    – gbn
    Apr 27, 2009 at 18:15
0

You can also use Profiler to see the execution plan. You'll want to include the Performance : Show Plan Statistics Profile option and be sure to inlcude Binary Data in your columns.

You can then run any query or procedure and see the execution plan.

Edit

If you can't use profiler, and you don't want to open another window I suggest that you include a comment block at the begining of your stored procs. For example imagine the following:

/* 
     Description: This procedure does XYZ etc...
     DevelopedBy: Josh
     Created On:  4/27/09

     Execution: exec my_procName N'sampleparam', N'sampleparam'
*/

ALTER PROCEDURE  my_procName
   @p1 nvarchar(20),
   @p2 nvarchar(20)

AS

What this allows is that you can highlight just the execution purpose and turn on show execution plan. And run it.

2
  • Unfortunately I do not have SQL admin so I can't use the profiler. Apr 27, 2009 at 17:24
  • I actually think you can run profiler as a non-admin if your given the right permissions. Not sure what those are however.
    – JoshBerke
    Apr 28, 2009 at 13:15
0

Here's a screenshot.Took me a while to figure out where to look for.

enter image description here

1
  • Reading the question and a couple of the top answers is also worth a thousand words Apr 4, 2018 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.