Tag Archives: Parameter Sniffing

Making SSRS reports faster: get rid of Parameter Sniffing

Is your SSRS report running slowly? Are you using a stored procedure to pull the data and pass them to report? If your answer to these questions is yes, then you are a victim of SQL Server’s Parameter Sniffing.
The first question is, what is Parameter Sniffing? It refers to SQL Server’s effort to reduce CPU overhead by using the same query execution plan for all similar queries instead of compiling the query each time it is being executed. As long as the queries would have really returned the same plan, this is a big performance winner. SQL Server internally tries to automatically turn simple non-parameterized user queries into parameterized queries to take advantage of this performance gain.
Parameter use, especially in more complex scenarios, can also cause performance issues. If the queries are complex and/or the data distribution on the columns against which the parameter is compared vary, the cost of different plan choices can change. A plan that is optimal for one parameter value may perform poorly for another value. The query optimizer still needs to estimate the selectivity and cardinality of predicates using a parameter value. This section describes how this process works in more detail.
So, what to do to prevent being a victim of this process? It is very easy to achieve: all you need to do is to declare local parmeter(s) inside your stored procedure’s code and assign the values passed to the SP to the newly introduced parameters. for example, lets say we have a sp with following definition:

CREATE PROCEDURE [SP_Test_ParameterSniffing]
@CustomerID INT
AS
BEGIN

SELECT *
FROM Customer c
WHERE c.CustomerID = @CustomerID

END

All you need to do is to add a new parameter to your SP code and assign the value passed to the SP to theis new parameter and use it in your WHERE clause:

CREATE PROCEDURE [SP_Test_ParameterSniffing]
@CustomerID INT
AS
BEGIN

DECLARE @CustomerID2 INT;
SET @CustomerID2 = @CustomerID;
SELECT *
FROM Customer c
WHERE c.CustomerID = @CustomerID2

END

This will cause the original query execution plan to be bypassed and have no effect on the query performance.

Advertisements