

Serious performance issues can arise when this plan is reused for different parameter values that generate a much larger number of rows. Such plans will often use single-threaded execution, nested loops joins, and lookups. One such scenario occurs when the atypical value is highly selective, resulting in a plan optimized for a small number of rows. It is not easy to predict when a particular execution plan will be recompiled (for example, because statistics have changed sufficiently) resulting in a situation where a good-quality reusable plan can be suddenly replaced by a quite different plan optimized for atypical parameter values. This can result in a new cached plan (based on the sniffed atypical parameter value) that is not good for the majority of executions for which it will be reused. Whatever the exact cause of the plan recompilation, there is a chance that an atypical value is being passed as a parameter at the time the new plan is generated. A recompilation may be triggered for all sorts of reasons, for example because an index used by the cached plan has been dropped (a correctness recompilation) or because statistical information has changed (an optimality recompile). This sounds reasonable enough (even obvious) and indeed it often works well.Ī problem can occur when an automatic recompilation of the cached plan occurs. The implicit assumption is that parameterized statements are most commonly executed with the most common parameter values. When parameter sniffing is enabled (the default), SQL Server chooses an execution plan based on the particular parameter values that exist at compilation time.

An execution plan that is efficient for one parameter value may not be a good choice for other possible parameter values. These are all good things, provided the query being parameterized really ought to use the same cached execution plan for different parameter values. Query parameterization promotes the reuse of cached execution plans, thereby avoiding unnecessary compilations, and reducing the number of ad-hoc queries in the plan cache.
