I am working with MS SQL Server 2008 R2. I have a stored procedure named rpt_getWeeklyScheduleData. This is the query I used to look up its execution plan in a specific database:
select
*
from
sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
where
OBJECT_NAME(st.objectid, st.dbid) = 'rpt_getWeeklyScheduleData' and
st.dbid = DB_ID()
The above query returns me 9 rows. I was expecting 1 row.
This stored procedure has been modified multiple times so I believe SQL Server has been building a new execution plan for it whenever it was modified and run. Is it correct explanation? If not then how can you explain this?
Also is it possible to see when each plan was created? If yes then how?
This is the stored proc's signature:
CREATE procedure [dbo].[rpt_getWeeklyScheduleData]
(
@a_paaipk int,
@a_location_code int,
@a_department_code int,
@a_week_start_date varchar(12),
@a_week_end_date varchar(12),
@a_language_code int,
@a_flag int
)
as
begin
...
end
The stored proc is long; has only 2 if conditions both for @a_flag parameter.
if @a_flag = 0
begin
...
end
if @a_flag = 1
begin
...
end
SQL Server Management Studio has three options to display execution plans: The Estimated Execution Plan is the compiled plan, as produced by the Query Optimizer based on estimations. This is the query plan that is stored in the plan cache. The Actual Execution Plan is the compiled plan plus its execution context.
Execution plans are stored in memory called plan cache, hence can be reused. Each plan is stored once unless optimizer decides parallelism for the execution of the query. There are three different formats of execution plans available in SQL Server - Graphical plans, Text plans, and XML plans.
You can use the administrative task scheduler to execute stored procedures at a specific time. You must first define a task for the stored procedure execution. Then, when the specified time or event occurs for the stored procedure to run, the administrative task scheduler calls the stored procedure.
Depending on the nature of the stored procedure (which wasn't provided) this is very possible for any number of reasons (most likely not limited to below):
if this then this select, else this select/update
Going to try an analogy which might help... maybe...
Say you have a stored procedure for your weekend shopping. You typically need to get groceries, sometimes an air filter, and even less often a big pack of something that needs replacing 4 times a year.
So here, depending on your parameters @needsAirFilter
and @needsBigPackOfSomething
could vastly change your "execution plan" of your stored procedure of "shopping".
If @needsAirFilter
and @needsBigPackOfSomething
is false
, there's no reason to make the 30 minute or hour drive, as everything you need is at the grocery store.
One a month, @needsAirFilter
is true, in that case we need to go to Target, as the grocery store's execution plan is insufficient.
4 times a year @needsBigPackOfSomething
is true, and we need to make the hour drive to get the big pack of something, while grabbing groceries, and airfilter since we're there.
Sure... we could make the hour drive every time to get groceries, and the other things when needed (imagine single execution plan). But this is in no way the most efficient way to do it. In instances like this, we have different execution plans for what information/goods are actually needed.
No idea if that helps... but I had fun :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With