Skip to content
All posts
Database Administration·December 3, 2026·5 min read

Fast in Testing, Slow in the App: Parameter Sniffing in SQL Server

One plan compiled for one value serves every call. Learn the symptoms of parameter sniffing in SQL Server and which fix fits, from Query Store to a split procedure.

By Chris Sparks

A stored procedure runs in a second when you test it. The same procedure, called from the application, times out. Yesterday it was fine. Nobody deployed anything.

This pattern usually means parameter sniffing. It is a normal feature of SQL Server that sometimes works against you.


What sniffing is

When SQL Server compiles a stored procedure or parameterized query, it looks at the parameter values supplied on that first call. It uses them, along with column statistics, to estimate how many rows each step will touch, and builds a plan from those estimates. Then it caches the plan.

Later calls reuse the cached plan, whatever values they pass. That saves compile time, and for evenly distributed data it works well.

For skewed data, it can hurt. Suppose a table of sensor readings has one row per tag per minute. Most tags have a few thousand rows in the range you ask for. A handful of tags have millions. A plan compiled for a small tag may do a nested loop with key lookups, which is fast for ten rows and awful for ten million. A plan compiled for a big tag may scan, which wastes effort when you only needed ten rows.

The plan is not wrong. It is right for the values it was built on and wrong for the values that follow.


The symptoms

  • A query that is fast with some inputs and slow with others.
  • Slow in the application, fast when you paste the same call into a query window.
  • A sudden slowdown with no code change, often after a restart, a statistics update or a plan being evicted from cache.

The second symptom confuses people. The application and your query window often connect with different SET options, such as ARITHABORT, and SQL Server keeps separate cached plans for different option combinations. Your window gets a fresh plan built on your test value, while the application keeps using the bad cached one. So your test proves nothing about what the application sees.


Confirm it

Look at the cached plan the application actually uses. Pull it from the plan cache, or from Query Store if it is enabled, and compare the compile-time parameter values in the plan with the runtime values of a slow call.

In the plan XML, the ParameterCompiledValue attribute shows the value used to build the plan. If it is a tiny tag and the slow calls are for a huge one, you have found it.

Estimated rows versus actual rows tells the same story. A large gap between the two on an operator, repeated across calls, is the signature.


Fixes, in rough order of preference

Not every fix suits every situation. Test each on a copy of the workload.

Update statistics. Stale statistics make bad estimates worse. If the data changed a lot, refresh them before anything else.

Query Store plan forcing. In SQL Server 2016 and later, Query Store records plans per query. If a good plan exists in the history, you can force it with sp_query_store_force_plan. This needs no code change. Review forced plans periodically, because a forced plan can go stale when data shapes change.

Parameter-sensitive plan optimization. SQL Server 2022 added a feature that can keep multiple plans for one parameterized query, based on how the parameter value splits the data. It applies at database compatibility level 160 and has limits on which queries qualify, so check the documentation and test.

OPTION (RECOMPILE). This compiles a fresh plan on every execution using the actual values. It gives the best plan each time at the cost of compile time on every call. That is acceptable for a report run a few times an hour and a bad idea for a query that runs hundreds of times a second.

OPTIMIZE FOR. You can tell the optimizer to plan for a specific value, or for an average with OPTIMIZE FOR UNKNOWN. The unknown form uses average density statistics, which gives a middling plan that is rarely terrible and rarely ideal. It can mask the problem instead of solving it.

Split the work. If a procedure serves two very different shapes of request, such as one tag for ten minutes and all tags for a month, give each shape its own procedure or branch. Each then gets a plan that fits.


What to avoid

Do not clear the whole plan cache in production to “fix” it. That forces every query to recompile at once and can spike CPU. If you need to evict one plan, target it by its handle.

Do not reach for WITH RECOMPILE on every procedure as a habit. It hides problems and adds cost everywhere.

Do not copy a fix from a forum without understanding which skew you have. The right answer for one distribution is wrong for another.


Design to reduce the risk

Some sniffing trouble starts in the schema and queries. Indexes that match the real access path give the optimizer fewer bad options. A covering index for the common query removes the lookup cost that makes a small-row plan so bad on large inputs.

Be careful with catch-all procedures, where a single query takes many optional parameters and uses @param IS NULL OR column = @param. These are a classic source of poor plans, since one plan has to serve every combination. Dynamic SQL, built safely with parameters, often gives each combination its own plan.


A short routine

  1. Capture the slow call’s parameter values.
  2. Find the plan the application is using, not the one from your query window.
  3. Compare compiled and runtime values, and estimated against actual rows.
  4. Pick the narrowest fix that fits: statistics, Query Store, a recompile hint or a split.
  5. Watch the query for a week, since data shape changes can bring it back.

You will not stop sniffing from happening. You can recognize it in minutes and fix it without guessing.


Get in touch