
Same Query, Different Speed: Parameter Sniffing and Plan Reuse in SQL Server
- كاتب موثّق
- SQL Server 2025
محتويات المقال
The common misconception
What actually happens
Sniffed once, reused for everyone
sp_executesql, and prepared queries. The optimizer looks those values up in the statistics, picks the best plan for them, and caches it. The next call with any other value skips compilation and runs the same plan.
Skewed data makes one plan wrong for others
dbo.Orders held 1,040,000 orders. Customer 42 had 200,035 of them, customer 4242 had 48. The histogram knew it: compiled for 42, the estimate was exactly 200,035 rows. Compiled for 42, the best plan was a parallel scan plus a sort with a 62 MB . Compiled for 4242, it was an index seek plus 48 key lookups. Run each with the other's plan:
Whoever calls first decides
SQL Server 2022 and 2025 keep more than one plan
SkewnessThresholdNotMet, and kicked in only after 20,000 single-order customers widened the gap. Then customer 4242 got its own variant (159 reads) and customer 42 another (50,914), whichever came first. SQL Server 2025 at compatibility level 170 adds optional parameter plan optimization for @p IS NULL OR column = @p filters, and lets PSP pick several predicates from one table. Those 2025 behaviors are described here from Microsoft's docs only; the runs were on SQL Server 2022.
Fix the query, not the cache
CREATE OR ALTER PROCEDURE dbo.GetCustomerOrders @customerId intASSELECT Id, CreatedAt, Status, Total, EmailFROM dbo.OrdersWHERE CustomerId = @customerIdORDER BY CreatedAt DESC;-- Compiled for customer 42, reused for 4242:-- 50,914 reads and ~200 ms of CPU for 48 rows-- Make one plan good for every value: cover the queryCREATE INDEX IX_Orders_Customer_CreatedAt ON dbo.Orders (CustomerId, CreatedAt DESC) INCLUDE (Status, Total, Email) WITH (DROP_EXISTING = ON);-- Tested, plan compiled for 4242 and reused:-- 4242 read 3 pages, 42 read 1,873SELECT Id, CreatedAt, Status, TotalFROM dbo.OrdersWHERE (@customerId IS NULL OR CustomerId = @customerId) AND (@status IS NULL OR Status = @status)OPTION (RECOMPILE);-- SQL Server 2022, customer 4242: 11,577 reads without RECOMPILE, 3 with it@customerId is null, so it scanned the whole index. With RECOMPILE, the optimizer turns the parameters into constants and simplifies the filter. Microsoft warns that compiling on every execution can cost a lot, which is what optional parameter plan optimization avoids in SQL Server 2025.
OPTION (RECOMPILE) | ||
OPTIMIZE FOR UNKNOWN | ||
RECOMPILE queries |
How to apply it in your project
Find queries with unstable performance In Query Store, look for queries with several plans or a wide spread of durations, and check the regressed queries report after statistics updates or restarts. Compare compiled and runtime values Get the actual plan of a slow execution and read ParameterCompiledValueagainstParameterRuntimeValue. Count rows per value of the filtered column to confirm the skew.Make one plan fit, or fit the plan per call Try an index that makes the same plan good for all values first. Otherwise, use OPTION (RECOMPILE)on that statement, or a Query Store hint when you can't change the code.Plan the compatibility level PSP needs level 160 on SQL Server 2022, and optional parameter plan optimization needs level 170 on SQL Server 2025. Capture a Query Store baseline before you raise the level, and compare after.
Key takeaways
A cached plan is compiled for the values of one call and reused for all values. On skewed data, the wrong plan cost a 48-order customer 50,914 reads and about 200 ms of CPU instead of 159 reads and at most 3 ms in the test. Which plan you get depends on who calls first after a recompile, so the slowdown looks random. SQL Server 2022's PSP keeps up to three plans for skewed equality filters; SQL Server 2025 adds plans per null state for optional filters. Prefer an index that makes one plan good for everyone, then targeted RECOMPILEor hints, never local variables.
التعليقات (0)