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 |
ptramontani
Starting Member
1 Post |
Posted - 2010-10-07 : 06:32:48
|
Hi all,I found a very strange problem that seems to be a mssql bug.I have a stored procedure with some parameters.One of these params is a dateTime.The procedure uses parameters in the where clause of a select statementIE: CREATE PROCEDURE myProc ( @amount decimal, @customer nvarchar, @dueDate datetime) ASselect * from tablewhere amount >= @amount and customer = @customer and dueDate <= @dueDatereturn 0goThe sp was very fast but some days ago I modified it simply adding some comments and I applied it against the db using the "alter procedure" statement.Then the procedure became very slow and I found that the select statement took about 60 seconds every time the procedure was launched. (the same select lauched with SQL Query Analizer ends in some millisecond)I found that the problem was caused by the @dueDate parameter and I found this solution:I introduced a local variable in the sp (@localDueDate)I set its value = the @dueDate param value at the beginning of the spThen I use the local variable in the where clause-> now the sp is fast again and the select statement ends in some millisecond as the direct launch in Query AnalizerCREATE PROCEDURE myProc ( @amount decimal, @customer nvarchar, @dueDate datetime) ASdeclare @localDueDate datetimeset @localDueDate = @dueDateselect * from tablewhere amount >= @amount and customer = @customer and dueDate <= @localDueDate return 0goSomeone found a problem like this?Did you know which kind of problem is and how can I avoid it?ThanksPaolo Tramontani |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-10-07 : 06:38:47
|
It's called parameter sniffing. In my experience it's generally quite rare but when it bites you it bites hard. Rule of thumb is always to declare local variables and assign them from the params.Just do a google search for it "sql parameter sniffing" and start reading.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
|
|
|
|
|