Вопрос

I am developing a .NET program to retrieve execution plans of all stored procedures in a given database. I am going to parse these XML's at program side.

SET SHOWPLAN_XML ON and execute the procedure gives the plan. However, the problem is that all the parameters of the procedure needs to be passed.

Is there any way to retrieve execution plan without passing any parameters of the stored procedure, by giving the name of the procedure only?

Это было полезно?

Решение

Yes, you probably want to take a look at the plan cache - SQL Server caches all execution plans and allows you to take a look at them using DMVs. For example the following is a basic query which will list all cached query plans (as xml) along with their SQL text. On most database you will also need to add additional filtering clauses to filter the results down to just the plans you are interested in.

SELECT UseCounts, Cacheobjtype, Objtype, TEXT, query_plan
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
CROSS APPLY sys.dm_exec_query_plan(plan_handle)

Note that these plans only include the estimated execution plan and won't contain the "actual" values (e.g. actual number of reads). Also you can't use this to get the query plan for an encrypted stored procedure.

See also How do I obtain a Query Execution Plan?, also Tools for visualising execution xml plans as HTML may be useful for the "parsing the XML" part.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top