A relatively straight-forward stored procedure is working fine until one day it becomes incredibly slow. We look into it assuming it to be an index related issue, but it's not. If we run the SELECT on its own it runs in 0-2 seconds. That same SELECT within a sproc is taking 1:52 consistently. I began hard-coding the parameter values into the SELECT to find out which one was causing the problem and got to the datetime parameters. If those were hard-coded performance was great, but if I used the passed parameters it took close to two minutes.
So I then declared local variables within the sproc and set the values of those variables to equal the passed parameter values. Both the parameters and the variables are being explicitly declared as datetime. When doing this the procedure runs in 0-2 seconds!!!
It would seem to be some sort of problem with interpretation of the parameters, but even if I set the passed parameters values to a cast datetime of the passed value it still won't work. It only works fast when using the local variables.
This is not the case for other sprocs within the project (of which numerous ones have datetime parameters). And it's not a problem with a cached execution plan related to the sproc either - I've been able to recreate this in a brand new procedure.
This is the 3rd time this has happened. The first time I thought it was some odd fluke. The second time was an annoyance. Now it's becoming an issue. When will the other sprocs start behaving as this one did What is causing it Obviously if this starts randomly happening with other sprocs and they are changing execution times form 0-2 to 1:50+ then we're going to have a real serious production problem!
Also, some background on the platform... the 3 different times this has happened have been in different environments - the first time was on Windows 2000 and SQL 2000, and the last two times has been on Windows 2003 and SQL 2005 (but on different servers). In other words, based on what I'm seeing this is not a server configuration issue.
Any ideas anyone