July 27, 2005 at 11:50 am
So - I have a good one for you guys.... I have stored proc that takes in 2 date parameters - @startDate and @endDate - both varchar(50).... the query inside is a simple query that is a select based on the date criteria. I execute my stored proc -
exec StoredProc '7/1/2005', '7/31/2005'
It takes close to 8 mins....
In the top part of the stored proc I have:
CREATE PROCEDURE StoredProc (@startDate varchar(50), @endDate varchar(50)) AS
SET NOCOUNT ON
select * from orders
where orderDate between @startDate and @endDate
Now.... 8 mins being too long to return this data, I was playing with the stored proc and I changed the name of the parameters - and use local variables to run the query - So now I have...
CREATE PROCEDURE StoredProc (@startingDate varchar(50), @endingDate varchar(50)) AS
declare @startDate varchar(50)
declare @endDate varchar(50)
SET NOCOUNT ON
set @startDate = @startingDate
set @endDate = @endingDate
select * from orders
where orderDate between @startDate and @endDate
And it runs in very few milliseconds....!!
Can anyone tell me why this is case ? I'm afraid it may be affecting performance in other places too....
Thanks !
July 27, 2005 at 11:59 am
When you company orderDate (a datetime datatype) to @startdate which is also a datetime, it runs much faster than when it is compared to a varchar datatype. the conversion takes more time.
July 27, 2005 at 12:30 pm
yes, but it is being passed in as varchar - so the local variable and the input paramters are both varchar...:-/
July 27, 2005 at 12:37 pm
You could try this, should be faster too..
CREATE PROCEDURE StoredProc (@startDate datetime, @endDate datetime) AS
SET NOCOUNT ON
select * from orders
where convert(varchar(10),orderDate,101) between
convert(varchar(10),@startDate,101) and convert(varchar(10),@endDate,101)
July 27, 2005 at 12:40 pm
yes, it is faster - but I still don't get why changing the names would make it so much faster.... they are both varchar - the scenario is both the same... AND - the stored proc ran fine when it was first created - and there were just as many records.....:-/
July 27, 2005 at 12:45 pm
Hi MR,
The input parameters, which I have supplied aren't the VARCHARs,they are DATETIMEs. By using the convert function, and DATETIME style, you can supply any datetime format to the procedure and it converts in to the given style .It makes it faster when comparing the other datefields.
July 27, 2005 at 12:48 pm
ahh - I see - but isn't the convert taking it back to varchar ?
July 27, 2005 at 12:55 pm
Yes it is but when u supply the input parameters like @startdate as '2005-07-27', '2005/07/27' are in any other formats, the convert style 101 change it as '07/27/2005'. So it works pretty fast as it converts all the datetime fields in to the same format. You can also see the CAST and CONVERTstyles in BOL .
July 27, 2005 at 12:58 pm
Thanks for all the helpfull info - but I'm not sure that was what was causing my slowdowns in the first place - when I changed it to a local variables, they are still varchars and all I'm doing is just setting a local variable to the input parameter and using that instead of using the input parameter directly in my query.....
July 27, 2005 at 1:02 pm
Changing the name of the variables can't be the reason. but you could always reduce the exec. time by using the Convert
July 27, 2005 at 1:04 pm
"Changing the name of the variables can't be the reason." - I figured that - I guess it has something to do with using local variables as opposed to input variables ? .... thats why it is perplexing ... although using the local variables has reduced my time to fractions of a second....
July 27, 2005 at 2:45 pm
This is known as a phenomena called parameter sniffing.
But related more to a cached query plan. the optimizer is guessing at the values your going to use, based off previous executions and index/Statistical information available to the optimizer. and executes your parametes against an invalid query plan.
Changing the parameter names as you did hides this from the optimizer, and cannot guess the plan as it tried to before.
Article about parameter sniffing/Procedure recompiling
http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply