📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How does Parameter Sensitive Plan optimisation in SQL Server 2022 work, and what doesn't it fix?

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.

Senior Debugging Advanced execution-plans parameter-sniffing psp-optimisation query-store sql-server

🎯 Key points

  • Compat level 160: SQL Server 2022 and Azure SQL
  • Triggered by equality predicates on columns with skewed histograms
  • Dispatcher plan picks one of up to 3 variants by cardinality bucket, each with its own plan
  • Considers up to three eligible predicates
  • Not for range predicates, catch-all queries, local variables or non-parameter estimate problems
  • Variants visible in Query Store (sys.query_store_query_variant) and forced per variant
  • SQL Server 2025 OPPO targets optional-parameter (IS NULL OR) patterns

⏱ 30-second answer

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.

🗣 Full answer (senior)

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.

  1. At compile time, with compat level 160, the optimiser looks for parameterised equality predicates whose column statistics are heavily skewed.
  2. If it finds them (up to three), it compiles a dispatcher plan. The dispatcher has no data operators; it holds a Multiple Plan choice and the bucket boundaries derived from the histogram.
  3. At execution, the dispatcher estimates the cardinality for the actual parameter value and assigns it to a low, medium or high bucket per predicate.
  4. Each bucket combination maps to a query variant — the same query text with an internal 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.

  • Range predicates (OrderDate >= @from) and LIKE — only equality qualifies.
  • Catch-all / optional parameter queries (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.
  • Local variables and OPTIMIZE FOR UNKNOWN — no sniffed value, so PSP can't help.
  • Skew the histogram can't see: 200 histogram steps may not capture a hot value; stale statistics bucket wrongly.
  • Skew inside a bucket and correlated predicates — three buckets are coarse.
  • Estimation problems not caused by parameters — table variables, multi-statement TVFs, non-SARGable expressions.

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.

🔍 What the interviewer is checking

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.

📚 Learn in depth

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).

💻 Code

-- 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);

⚠ Common mistakes

  • Assuming PSP ends parameter sniffing — it covers one pattern (equality on skewed columns).
  • Expecting it to help catch-all search procedures or range filters on dates.
  • Forgetting it needs compat level 160; restoring a database at 150 keeps the old behaviour.
  • Analysing Query Store by query_id and missing that the cost is split across variants.
  • Forcing a plan on the dispatcher's parent instead of the variant.
  • Blaming PSP when the real problem is stale statistics that put values in the wrong bucket.

🏛 Senior insight: trade-offs

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.

↩ Usually asked after

More in Query Performance

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me