Overview
When a parameterised query or stored procedure is compiled, SQL Server sniffs the parameter values and builds a plan optimised for them. The plan is cached and reused for later values. If the data is skewed, a plan that is ideal for one value can be terrible for another. This is called parameter sniffing; it is a normal optimisation, and a regression source.
Core concepts
- The plan depends on the first call after (re)compile (or after plan cache eviction, restart, statistics update,
DBCC FREEPROCCACHE). - Typical signature: “fast in the morning, slow after a restart”, or the same procedure fast for one customer and very slow for another.
- Fixes, from least to most invasive:
OPTION (RECOMPILE)on the statement: compile every time, using the actual value. Good for infrequent, expensive queries; costs CPU on hot ones.OPTION (OPTIMIZE FOR (@p UNKNOWN))or a local variable: use average density, a “middling” plan.OPTION (OPTIMIZE FOR (@p = value)): pin to a representative value.- Separate code paths or procedures for the skewed values.
- Query Store plan forcing or Query Store hints (sqlserver-query-store).
- Parameter Sensitive Plan (PSP) optimization, SQL Server 2022 at compatibility level 160: may build several plans for one parameterised statement when the histogram shows large skew. It applies only to certain predicate shapes, so verify in your plans.
Worked example
dbo.Orders (500,000 rows): customer 7 has about 49 rows, customer 42 has about 5,000. The procedure:
CREATE PROCEDURE dbo.GetByCust @c int AS
BEGIN
DECLARE @n bigint, @m char(100);
SELECT @n = COUNT_BIG(*), @m = MAX(Pad) FROM dbo.Orders WHERE CustomerId = @c;
ENDPad is not in the index, so the optimizer must choose between seek + key lookup and a scan. Measured logical reads with SET STATISTICS IO ON (compatibility level 160):
| First call compiles for | Call with 7 | Call with 42 |
|---|---|---|
@c = 7 (rare) | 162 (seek + lookups) | 15,481 (the same plan, thousands of lookups) |
@c = 42 (frequent) | 9,088 (scan reused) | 9,088 (scan) |
OPTION (RECOMPILE) | 162 | 9,088 |
Plan cache confirmation: the cached plan for the second run contained ParameterCompiledValue="(42)" and no key lookup. With OPTION (RECOMPILE) each call gets the best plan. Even at compatibility level 160 the PSP feature did not produce separate plans in this test, so do not rely on it without checking.
Pitfalls and production notes
- Compare actual and estimated rows in the cached plan to confirm the diagnosis, not just the symptom.
OPTION (RECOMPILE)is simple and often the right first fix; measure CPU if the query runs thousands of times per second.- Do not “fix” it by clearing the whole plan cache in production; it forces every query to recompile at once.
- Beware of changing
SEToptions (ARITHABORT) in SSMS; they create a separate plan and explain “fast in SSMS, slow in the app”. - Local variables look like a fix but give mediocre estimates for all values.
- PostgreSQL has a similar issue with generic versus custom plans (query-planning).
Practice questions
- Why does restarting the server sometimes “fix” a slow procedure for a while?
- Choose a mitigation for a procedure that runs 5,000 times a second with a highly skewed parameter.
- How do you retrieve the compiled parameter values of a cached plan?
References
- Microsoft Learn, Parameter sensitive plan optimization; Query hints
Related
sqlserver-statistics-cardinality · sqlserver-query-store · sqlserver-execution-plans · sqlserver-indexes