Skip to content

Published on · 4 min read

Parameter sniffing in SQL Server: why the same query is sometimes slow

What parameter sniffing in SQL Server is, how to confirm in the execution plan that it causes the slowdown, and how to fix it, from RECOMPILE to Query Store.

  • SQL Server
  • T-SQL
  • Performance

A classic symptom: the same procedure runs in milliseconds most of the time and then, suddenly, takes seconds for everyone. Someone clears the plan cache, everything goes back to normal, and a few days later the problem returns. Most of the time, the cause is parameter sniffing.

What parameter sniffing is

When a procedure or parameterized query runs for the first time, SQL Server looks at the parameter values of that call and builds the execution plan around them. That plan goes into the cache and is reused for the following executions, whatever values they receive.

Most of the time that is great: compiling a plan costs CPU, and reusing it avoids that cost. The problem shows up when the data is uneven. Picture an orders table where most customers have a few dozen orders, but a few large customers have hundreds of thousands:

CREATE PROCEDURE dbo.CustomerOrders @CustomerId int
AS
SELECT OrderId, OrderDate, Total
FROM dbo.Orders
WHERE CustomerId = @CustomerId;

If the first call is for a small customer, the plan probably uses a seek on the CustomerId index followed by key lookups, which is great for a few rows. When a large customer calls the same procedure, that plan performs hundreds of thousands of lookups and is far slower than reading the table directly. The opposite also happens: a plan built for the large customer makes the small calls slow.

How to confirm it

Before fixing, confirm. Capture the actual execution plan of a slow run and open the properties of the root operator. Under Parameter List you see two values for each parameter: the Compiled Value, used to build the plan, and the Runtime Value of the current execution. If they are very different and the row estimates in the plan are far from the actual counts, you have your diagnosis.

Another sign is in the cached execution statistics. A query with a low minimum time and a very high maximum time is a candidate:

SELECT TOP (20)
	qs.execution_count,
	qs.min_elapsed_time / 1000 AS min_ms,
	qs.max_elapsed_time / 1000 AS max_ms,
	SUBSTRING(st.text, 1, 200) AS query_text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
ORDER BY qs.max_elapsed_time - qs.min_elapsed_time DESC;

And if Query Store is enabled, the same query with more than one plan and very different durations between them is practically a confession.

Ways to fix it

There is no single fix. The choice depends on how often the query runs and how uneven the data is.

OPTION (RECOMPILE). Builds a fresh plan on every execution, always suited to the value received. It solves the problem completely but pays the compilation cost every time. It works well for heavy queries that run rarely and badly for queries executed thousands of times per minute.

WHERE CustomerId = @CustomerId
OPTION (RECOMPILE);

OPTIMIZE FOR. Tells SQL Server to build the plan for a specific value, or for the average distribution with OPTIMIZE FOR UNKNOWN. Useful when one plan is good enough for almost every case, but it stops being good if the data changes.

Forcing a plan with Query Store. If you already know which plan works well for everyone, you can force it with sp_query_store_force_plan. It requires no change to application code, which helps with third-party systems. Keep an eye on it afterwards, because a forced plan does not adapt to data growth.

Splitting the paths. When there are two clear profiles, such as small and large customers, sometimes the best option is two queries, each with its own plan, choosing which one to call case by case.

SQL Server 2022 features. With compatibility level 160, SQL Server 2022 brings Parameter Sensitive Plan optimization, which keeps more than one cached plan for the same query depending on the parameter value. It helps in many scenarios, but not all, so it is still worth measuring.

What not to do

Clearing the plan cache with DBCC FREEPROCCACHE in production makes the problem disappear for a while because it forces a new compilation. But it is a band-aid: the next compilation may land on the bad value again, and clearing the cache forces every other query to recompile too, causing a CPU spike.

Summary

Parameter sniffing is not a bug; it is the optimizer doing what it was built to do. The work lies in recognizing when the cached plan does not fit every value, confirming it in the actual plan and choosing the fix that balances compilation cost and stability.

If your SQL Server has queries that get slow for no apparent reason, see how I work on database administration and performance.