Reputation: 12857
Imagine a procedure named dbo.TheBestCodedProcedure
, if it is executed in the following ways:
EXEC dbo.TheBestCodedProcedure
And
EXEC TheBestCodedProcedure
Both will execute successfully, however how many execution plans are created? It is my understanding that the execution plan is based off the name of the object that is called and parameters provided. I have seen examples of multiple execution plans even when the same arguments are provided, the only difference being the dbo.
prefix, is this true or not? What are the rules of WHEN an additional execution plan is created?
Upvotes: 1
Views: 853
Reputation: 69564
The only difference in two statements you have shown in your question is that in one statement you have used TWO PART NAME
i.e [Schema].[Object]
and in other statement you have used only the object name to call that procedure.
This has no effect on stored procedure's execution plans but its does make a difference on how sql server finds this stored procedure.
It is always best to use two part name as it eliminates the need to resolve schema and sql server has less work to do in order to execute this procedure.
Schema Name
On the other hand if you do not use the two part name for a stored procedure when you are calling it sql server has to go through a few steps to find the stored procedure which are as follows.
sys
schema.Caller's Default Schema
.dbo
schema. As you can see by just adding the schema name when calling a procedure can save some work for sql server therefore it is always best practice to use schema name when calling a procedure in sql server.
Execution Plan
When we execute a Sql Query in Sql server, Sql Server goes through 4 steps in order to successfully execute the query.
In a stored procedure's case, 1st two steps only take place when the stored procedure is created.
3rd step takes place when the stored procedure is executed for the very first time.
and finally 4th step takes place for every subsequent execution.
Now the Creation of execution plan can take place for a number of reasons. Some of them are
Upvotes: 2
Reputation: 10098
To answer your original question, try this:
create procedure dbo.proc_test
@i int
as
select @i
go
exec proc_test 1
exec dbo.proc_test 2
go
select usecounts, text
from sys.dm_exec_cached_plans
cross apply sys.dm_exec_sql_text(plan_handle)
where text like '%proc_test%'
and text not like '%dm_exec_cached_plans%'
There is only one plan in the cache, as you can see.
For the complete discussion of plan cachability and reusability, pls see here:
http://technet.microsoft.com/en-us/library/ee343986%28v=sql.100%29.aspx
IMO it's too broad topic to discuss it at length here.
Upvotes: 3