Slow report in Reporting Services, even though the stored procedure runs so quickly?

Apologies for the long title, however it had to be said.  I have been working with Reporting Services recently, with very complex reports, not from a reporting services point of view, but from a T-SQL stored procedure aspect.  The query in question is over 1500 lines long, and needs to perform well when the report is run.  So with a little bit of performance tuning we managed to get said stored procedure to run in under 4 seconds (on a fairly powerful box).  However as soon as this was run through Reporting Services, the performance ground to a halt.  We are talking about 1 minute 30 seconds to run this report. So after a little help from my friends (thanks to Tim for finding this blog) we found that this was the exact problem.

So – its all down to “Parameter Sniffing” !!  But what on earth is parameter sniffing, and how does it help/hinder us?

As taken from a Microsoft white paper:

“Parameter sniffing” refers to a process whereby SQL Server’s execution environment “sniffs” the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word “current” refers to the parameter values present in the statement call that caused a compilation or a recompilation.

Just a bit of “Googling” will get you many more resources on the subject, and well worth reading.