Skip to main content

What is Parameter Sniffing and how do you handle it?

Senior MS SQL
Quick Answer Parameter sniffing caches a plan based on first-call parameters. If subsequent calls use different values, the cached plan may be inefficient. Fixes: OPTION(RECOMPILE) forces fresh compilation each time; OPTIMIZE FOR UNKNOWN makes the optimizer ignore sniffed values; local variables break plan reuse entirely.

Answer

Parameter sniffing occurs when SQL Server compiles a plan using the initial parameter values it sees, then reuses that plan for subsequent executions. If data distribution is skewed, one plan may not fit all parameter scenarios.

Symptoms: Some calls are lightning fast, others very slow, using the same procedure and query shape.

Handling strategies:

  • Use OPTION (RECOMPILE) for highly skewed queries where compilation cost is acceptable.
  • Use OPTIMIZE FOR UNKNOWN or OPTIMIZE FOR (@param = ...) hints to choose more robust plans.
  • Capture parameters in local variables inside the procedure to discourage sniffing and produce more average plans.
  • Split logic into separate procedures for different parameter ranges if patterns are distinct.
  • Use Query Store to force stable plans when appropriate.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice