How SQL Server 2022's Parameter Sensitive Plan (PSP) optimisation builds a dispatcher plan with query variants, and the parameter-sniffing cases it leaves unsolved.
At compatibility level 160 (SQL Server 2022, Azure SQL), when a parameterised query has an equality predicate on a column whose histogram shows heavy skew, the optimiser compiles a dispatcher plan instead of one plan. At run time the dispatcher looks up the parameter value's cardinality in the statistics, puts it in a low, medium or high bucket, and runs the matching query variant, each with its own cached plan. It considers up to three such predicates. It does not handle range predicates, catch-all @p IS NULL OR col = @p queries (SQL Server 2025's Optional Parameter Plan Optimisation, OPPO, targets those), skew the statistics can't see, or estimation problems that aren't about the parameter.
The problem it targets. Classic parameter sniffing: the first execution's parameter value decides the cached plan. If CustomerId = 1 has 2 million orders and most customers have 20, whichever compiles first gives the wrong plan to the other group.
How PSP works.
OPTION (PLAN PER VALUE(...)) — compiled and cached separately. So the 2-million-row customer gets a scan / hash plan and the 20-row customers get a seek / nested-loops plan.How you see it. The actual plan shows Dispatcher and QueryVariantID in the plan XML. In Query Store, variants are separate query_ids linked to the parent in sys.query_store_query_variant. Forcing applies to a variant's plan, not the dispatcher.
What it doesn't fix.
OrderDate >= @from) and LIKE — only equality qualifies.WHERE (@status IS NULL OR Status = @status)) — still one compromised plan. SQL Server 2025 adds OPPO for these, or you use OPTION (RECOMPILE) or dynamic SQL.OPTIMIZE FOR UNKNOWN — no sniffed value, so PSP can't help.Production example. A multi-client B2B API had one client with 40% of all orders. Upgrading to compat 160 gave a dispatcher plan for the GetOrdersByClient query and the nightly "fast for everyone except the big client" alerts stopped. The search screen with optional filters still regressed, because it used the catch-all pattern; we moved that to dynamic, parameterised SQL built per filter combination.
Checks currency with modern SQL Server and, more importantly, knowledge of its limits.
Green flags: explains the dispatcher and variants; knows equality-only, skew-based, up to three predicates and three buckets; lists what it doesn't fix (ranges, catch-all, local variables); knows how variants show in Query Store; mentions compat level and how to disable it.
Red flags: says PSP solves parameter sniffing; can't describe what triggers it; doesn't know about compat levels; confuses it with OPTIMIZE FOR or RECOMPILE.
Eligibility. The query must be parameterised (stored procedure, sp_executesql, or EF Core's parameterised SQL), at compat level 160, with the database scoped configuration PARAMETER_SENSITIVE_PLAN_OPTIMIZATION ON (the default). The optimiser checks each equality predicate's histogram for skew — a large spread between the most and least frequent values. Predicates on columns without useful statistics are ignored.
Bucket boundaries. The boundaries are cardinality values computed from the histogram at compile time and stored in the dispatcher. A recompile (statistics update, plan eviction) recomputes them. Three buckets per predicate, up to three predicates, gives at most 27 combinations in theory, but only combinations actually executed are compiled.
Plan cache and Query Store. Each variant is a separate cache entry and Query Store query. Monitoring tools that aggregate by query_hash combine them; tools that look by query_id see them separately — aggregate by the parent query or you will miss the total cost.
Turning it off. ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SENSITIVE_PLAN_OPTIMIZATION = OFF; or the query hint USE HINT('DISABLE_PARAMETER_SENSITIVE_PLAN_OPTIMIZATION') if a variant misbehaves.
OPPO (SQL Server 2025). Optional Parameter Plan Optimisation uses a similar dispatcher idea for predicates like @p IS NULL OR col = @p, choosing between a variant that applies the filter and one that doesn't. It needs the SQL Server 2025 compatibility level (170).
-- Check compat level and the PSP setting
SELECT name, compatibility_level FROM sys.databases WHERE name = DB_NAME();
SELECT name, value FROM sys.database_scoped_configurations
WHERE name = 'PARAMETER_SENSITIVE_PLAN_OPTIMIZATION';
-- An eligible query: equality predicate on a skewed column
EXEC sys.sp_executesql
N'SELECT OrderId, Total FROM dbo.Orders WHERE ClientId = @clientId',
N'@clientId int', @clientId = 1;
-- Variants of a parent query in Query Store
SELECT v.parent_query_id, v.query_variant_query_id, v.dispatcher_plan_id,
qt.query_sql_text
FROM sys.query_store_query_variant AS v
JOIN sys.query_store_query AS q ON q.query_id = v.query_variant_query_id
JOIN sys.query_store_query_text AS qt ON qt.query_text_id = q.query_text_id;
-- Not eligible: catch-all predicate (one plan for every combination)
SELECT OrderId, Total
FROM dbo.Orders
WHERE (@status IS NULL OR Status = @status)
AND (@clientId IS NULL OR ClientId = @clientId);
query_id and missing that the cost is split across variants.PSP is a good default to have on, but I don't plan around it. I still design queries to be stable: indexes that make both the small and large cases acceptable, separate code paths for genuinely different workloads (the one huge client), dynamic parameterised SQL for optional filters, and OPTION (RECOMPILE) where compile cost is small relative to run time. When upgrading compat level I'd capture a Query Store baseline at the old level first so any variant-related regressions are visible and fixable with plan forcing or the hint.
Last updated 23 Nov 2026.