Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-11-10 : 18:36:29
|
| I was speaking to a MS SQL Technical representative today. He tells me that if a statement in a stored procedure uses a local variable in a WHERE clause, that statement alone (not the whole procedure) will be recompiled every time the stored proc is executed to draft a new execution plan for said statement.This does not apply to WHERE clauses using passed parameters.Has anyone else heard of this?Sam |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2005-11-10 : 22:18:20
|
| That seems to contradict what Ken says here:http://blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-11-10 : 22:19:34
|
Yes, but until you posted this I haven't given much thought to it.We often pass parameters to SProcs as Strings (because they come from Web <INPUT> fields, and Dates / Numbers might be "" which we want to interpret as NULL, rather than 0 or 01-Jan-1900. So we convert them to local parameters of the correct type, and use those in our WHERE clause Perhaps I should take a closer look.Kristen |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-11-10 : 22:57:15
|
quote: Originally posted by robvolk That seems to contradict what Ken says here:http://blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx
I've asked the tech rep to follow up with a BOL citation to back up that claim.If I hear back from him, I'll post a follow up.Sam |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-11-11 : 01:59:47
|
| "That seems to contradict what Ken says here"Hes using a variable that was defined as a parameter, rather than a local variable. So that suggests to me that a local variable would not have worked - indeed, the value of a parameter changing from its initial value will fool the optimiser too.Kristen |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-11-11 : 13:18:19
|
Here's a demo which relates to this subject. Show how the execution plan of a stored procedure can be fixed as either: (1) a CLUSTERED INDEX SEEK or (2) an INDEX SCAN, depending on the value of a passed paremeter. Once the execution plan is cached, it is used for subsequent calls irrespective of the passed parameter value. ---dbo.ParameterSniffing DemoUSE NorthwindCreate table dbo.ParameterSniffing ( i int identity(1,1), Gender Char(1), lastname varchar(100))GO-- Fill the table with lopsided data: 10000 rows of 'M', 2 rows of 'F'Declare @i intSet @i = 0While @i < 10000 Begin Insert into dbo.ParameterSniffing(Gender) values('M') Set @i = @i + 1 end-- Fill only 2 rows for Female:Insert into dbo.ParameterSniffing(Gender) values('F')Insert into dbo.ParameterSniffing(Gender) values('F')--Create indicies on the table ParameterSniffingCreate clustered index clus_ind on dbo.ParameterSniffing(i)Create index ind on dbo.ParameterSniffing(Gender)GO--Stored procedure takes one parameter and gives us output:Create procedure dbo.SniffTest @Gender char(1)As Select * from dbo.ParameterSniffing where Gender = @GenderGO-- Free Procedure cache clearing out all execution plans.DBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSSet statistics profile onSet statistics time onSet statistics io onEXEC dbo.SniffTest 'M' --Execute procedure with parameter male('M'): Execution plan shows clustered index SCAN--Execute procedure with parameter male('M'): Execution plan shows clustered index SCAN (sub-optimal for parameter F)EXEC dbo.SniffTest 'F'-- Free Procedure cache clearing out all execution plans.DBCC FREEPROCCACHEDBCC DROPCLEANBUFFERSEXEC dbo.SniffTest 'F' --Execute procedure with parameter male('M'): Execution plan shows index SEEK--Execute procedure with parameter male('M'): Execution plan shows index SEEK (sub-optimal for parameter M)EXEC dbo.SniffTest 'M' Set statistics profile offSet statistics time offSet statistics io offGo-- Clean UpDROP Procedure dbo.SniffTestDROP TABLE dbo.ParameterSniffingGO |
 |
|
|
|
|
|
|
|