Home Forums SQL Server 2008 T-SQL (SS2K8) OPTIMIZE FOR UNKNOWN (parameter sniffing problem) RE: OPTIMIZE FOR UNKNOWN (parameter sniffing problem)

  • That depends on any number of factors. Parameter sniffing only presents a problem if there are multiple possible execution plans, some of which may be suboptimal for the parameter value passed. Consider a customer table with a state field, clustered on customer account number, with a nonclustered index on state. Assume ten thousand rows in the table. Eight thousand (80%) are from Texas. So if we have a stored procedure that selects based on state, the optimal plan involves scanning the clustered index, since this will be much more efficient than trying to do a key lookup. BUT there are only two rows from Vermont, and the select would benefit greatly from a key lookup for this parameter value. If the first plan compiled for the SP involved TX as a parameter, we will wind up using the same plan (CI scan) for Vermont as well as Texas, and this is suboptimal (obviously) for Vermont. Forcing the issue by using local variables will result in the optimizer generating a new plan that makes use of index statistics to generate the optimal plan for the value.

    Roland Alexander 
    The Monday Morning DBA 
    There are two means of refuge from the miseries of life: music and cats. ~ Albert Schweitzer