Parameter Sensitive Plan: SQL Server's Context-Aware Intelligence
Every execution plan is a decision about how to get from data to a result. For years, SQL Server could cache a plan optimized for an initial parameter value (parameter sniffing) and reuse it even when later values had very different data distributions.
💡 What does PSP bring to SQL Server 2022?
Parameter Sensitive Plan (PSP) optimization can maintain multiple plan variants for the same query, tuned for different parameter selectivities. A dispatcher chooses the appropriate variant for the current parameter values.
- One logical query can have multiple plan variants.
- Query Store captures information that helps analyze them.
- At runtime, selection depends on parameter selectivity ranges, or “buckets”.
📂 Repository: PSP
⚙️ Running the demo
Open the repository in VS Code.
Set the environment variables:
1 2 3$env:MSSQL_SERVER="localhost" $env:MSSQL_USER="sa" $env:MSSQL_PASS="YourStrong!Passw0rd"Run tasks 00 → 03, followed by 04 – Compare IO/CPU, or execute the queries in
sql/dmv.
🔬 What you will see
- At least two plan variants in Query Store for
dbo.GetOrdersByCustomer. - In the plan XML:
<ParameterSensitivePlan>True</ParameterSensitivePlan>. - I/O and CPU differences between infrequent and “hot” parameter values.
🧩 Good practices
- Keep Query Store in READ_WRITE mode to analyze PSP behavior.
- Be careful when combining PSP with
OPTIMIZE FORhints. - PSP targets eligible parameterized statements, including stored procedures and
sp_executesql.
“Intelligence is the ability to adapt to change.” — Stephen Hawking