DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

What Is SQL Server Parameter Sniffing—and When Should You Recompile a Query?

SQL Server reuses plans compiled with parameter values. Learn when that becomes a performance problem and how to weigh OPTION (RECOMPILE) against PSP and other options.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a SQL Server query is fast for some parameter values and slow for others, parameter sniffing may be involved—but a slow run by itself does not prove it. SQL Server normally uses parameter values during compilation to choose a plan it can reuse. Trouble arises when that plan performs poorly for inputs with a materially different data distribution. OPTION (RECOMPILE) can make the optimizer compile for the current execution, but it also adds compilation work, so it is best considered for a demonstrated, specific problem rather than applied by default.

What is SQL Server parameter sniffing?

When SQL Server compiles a parameterized statement, it can use the parameter values available at compilation to estimate how many rows a query will process and choose an execution plan. SQL Server can then reuse that plan for later executions. This is normal plan reuse, not inherently a fault.

As an Amazon Associate I earn from qualifying purchases.

It becomes a parameter-sensitive performance problem when the plan selected using one set of values is reused for different values whose data distribution or expected row counts call for a substantially different plan. For example, one value might match a small number of rows while another matches a large share of a table. A plan well suited to one case may be inefficient for the other.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why is a query slow for some parameter values but fast for others?

Parameter sensitivity is one possible explanation, but slowness alone is not enough to identify it. Compare executions using representative values and look for a repeatable relationship between the input values and runtime or resource use. Inspect actual execution plans and Query Store history when available. The relevant question is whether the values that perform poorly are being served by a plan that is a poor fit for those executions.

Microsoft describes targeted removal of a plan from the cache as a diagnostic indication: if the query improves after recompilation, parameter sensitivity may be involved. That is evidence to investigate, not a lasting remedy. Avoid clearing the whole plan cache casually; doing so forces plans to be compiled again and can cause one-time longer execution times. Where appropriate, use the specific plan handle rather than flushing the cache globally. Microsoft’s parameter-sensitive plan troubleshooting guidance explains this diagnostic approach.

What should you check before adding a hint?

  1. Verify the pattern. Compare representative parameter values, execution behavior, actual plans, and Query Store records where available. Do not treat a general report of slowness as proof of parameter sniffing.
  2. Check statistics and indexes. Review whether statistics reflect the current data distribution and whether statistics or index maintenance is needed. Microsoft recommends this before evaluating Query Store hints. Query Store hints guidance
  3. Confirm engine version and database compatibility level. SQL Server 2022 (16.x) Parameter Sensitive Plan (PSP) optimization requires compatibility level 160 in the documented guidance and is on by default starting at that level. Confirm the actual version and compatibility level for the database; Azure SQL products have their own applicable feature scope. If PSP is eligible, test the compatibility change and inspect Query Store for dispatcher and query variant plans. Microsoft’s PSP optimization documentation
  4. Compare interventions with realistic inputs. Test across the values and execution frequency the workload actually sees. A fix that helps one value can make another worse, and compilation itself consumes resources.

When should you use OPTION (RECOMPILE)?

Consider OPTION (RECOMPILE) when investigation shows that one statement’s reusable plan is a poor fit across parameter values and compiling for each execution is likely to save more execution cost than it adds in compilation work. With the hint, SQL Server compiles that statement using the current execution’s parameter values rather than relying on a reused plan.

Prefer statement-level recompilation when one statement is the problem. Recompiling an entire stored procedure on every execution is a broader intervention and should not be the default response. Estimate the trade-off using the query’s call frequency, compilation cost, and the distribution of values it receives—not just a single fast or slow run. A query-level RECOMPILE hint also prevents PSP from operating on that query, so weigh it against PSP where both are relevant. Microsoft’s query hint reference

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do the main alternatives compare?

Option How it behaves Scope and trade-off
OPTION (RECOMPILE) Compiles the statement using the current execution’s parameter values. Statement scope; adds compilation work on each execution. Can prevent PSP for that query.
PSP optimization For eligible parameter-sensitive queries, supports multiple active plans rather than relying on one plan for every parameter case. Requires SQL Server 2022 (16.x) or applicable Azure SQL offering and, in the documented SQL Server guidance, compatibility level 160. Check dispatcher and variant plans in Query Store.
OPTIMIZE FOR (@parameter = value) Optimizes using a chosen representative value. Can suit a workload with a known representative case, but that choice may not suit other values or remain representative as data changes.
OPTIMIZE FOR UNKNOWN Uses density-vector average estimates rather than optimizing for the current sniffed value. May provide a more general plan, but an average estimate is not necessarily suitable for strongly skewed inputs.
Disable parameter sniffing Uses a more generic planning approach instead of plans tailored to sniffed parameter values. Can trade a parameter-specific plan for a generic one; disabling sniffing can also disable PSP for associated workloads or contexts.
Targeted plan-cache eviction Causes the affected plan to be compiled again on a subsequent execution. Useful as a targeted diagnostic or temporary action, not a durable solution to recurring parameter sensitivity.
Query Store hint Applies supported plan behavior through Query Store without changing application query text. Availability and constraints depend on the exact SQL Server or Azure product and version. Microsoft notes that Query Store RECOMPILE hints are not supported when database parameterization is forced.

These choices are not universally interchangeable. Judge them by plan quality across skewed values, compilation cost and execution frequency, scope, whether application changes are possible, version and PSP eligibility, and how easily the change can be monitored, removed, and retested.

How should you manage a Query Store hint?

A Query Store hint can be useful when changing application code is not practical and the target environment supports the desired hint. Treat it as a scoped intervention: test it before production use, check whether the hint is being applied, and plan how to remove or revise it. Microsoft advises revisiting hints as data volume or distribution changes and during database migrations; a previously suitable hint can become stale. Consult the product-specific guidance for support and restrictions, including the forced-parameterization limitation for Query Store RECOMPILE hints. Query Store hints guidance

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Does SQL Server recompile automatically?

Yes. SQL Server can recompile statements for engine-related reasons, including changes to cardinality estimates after statistics updates. That is different from forcing recompilation proactively on every execution. Microsoft’s sp_recompile reference notes that automatic recompilation can occur; a forced recompilation strategy is not usually necessary simply because a query has a cached plan. Microsoft’s sp_recompile reference

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.