Dispatcher and Variants: What PSP Optimization Creates in the SQL Server Plan Cache
At the end of part 1, you turned PSP optimization back on. After that, customers 20001 and 20008 got different plans: an index seek for the small one and a scan for the large one. This part shows what that creates in the plan cache: a dispatcher and up to three variants. You see at which boundary values the dispatcher switches from one variant to the next, and why the estimate from part 2 decides the variant.
Learning goal: You can recognize a plan with PSP optimization and determine which variant a value gets.
Prerequisite: Part 1 and part 2. You have built the course database PspKurs.
In short
- With PSP optimization, SQL Server creates a dispatcher for the query in the procedure. On every call, it estimates the rows for the parameter value and assigns the call to a variant. For a predicate such as
KundeId = @KundeId, there are up to three variants. - The boundary values between the variants come from the statistics. In the course database, they are 100 and 100,000 estimated rows.
- SQL Server compiles each variant when an estimate falls into its span for the first time, for the value of that call. Within a variant, all values therefore still share one plan.
Before you begin
Warning: Run the experiments only on a test instance with the course database. They clear the plan cache of the course database. Every modifying block therefore first checks that it runs in
PspKursand otherwise aborts.
For the experiments, you need the permissions you have as the owner of the course database in PspKurs. For the two queries on the plan cache, you also need VIEW SERVER PERFORMANCE STATE, and you must be allowed to see the procedures.
Predict first
PSP optimization is on, and the plan cache of the course database is empty. You call the procedure for customer 20001 (10 orders) and then for customer 20008 (500,000).
How many variants are in the plan cache afterwards?
- one
- two
- three
Show answer
Answer: 2. A variant is created only when an estimate falls into its span for the first time. The variant for estimates from 100 to under 100,000 rows is still missing.
Experiment 1: Variants appear one by one
Run the following block. It clears the plan cache of the course database and calls the procedure for the small and the large customer.
USE PspKurs;GOIF DB_NAME() <> N'PspKurs' OR NOT EXISTS (SELECT 1 FROM sys.extended_properties WHERE class = 0 AND name = N'PSG_Kurs') THROW 50000, N'Nicht in der Kursdatenbank PspKurs, nichts geändert. / Not in the course database PspKurs, nothing changed.', 1;ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SENSITIVE_PLAN_OPTIMIZATION = ON;ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;GOIF DB_NAME() <> N'PspKurs' THROW 50000, N'Nicht in PspKurs. / Not in PspKurs.', 1;EXEC dbo.Bestellungen_Kunde @KundeId = 20001; -- 10 orders, estimated 1.337EXEC dbo.Bestellungen_Kunde @KundeId = 20008; -- 500,000 ordersGOThe following query shows the variants of a procedure in the plan cache of the current database. It only reads.
USE PspKurs;GO-- Variants of a procedure in the plan cache of the current databaseDECLARE @prozedur nvarchar(300) = N'dbo.Bestellungen_Kunde';IF OBJECT_ID(@prozedur, N'P') IS NULL THROW 50000, N'Prozedur nicht gefunden oder für dieses Login nicht sichtbar. / Procedure not found or not visible.', 1;DECLARE @muster nvarchar(100) = N'%PLAN PER VALUE(ObjectID = ' + CONVERT(nvarchar(20), OBJECT_ID(@prozedur, N'P')) + N',%';WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan'),varianten AS ( SELECT qs.execution_count, qs.last_logical_reads, qs.min_logical_reads, qs.max_logical_reads, TRY_CONVERT(xml, tqp.query_plan) AS plan_xml FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_plan_attributes(qs.plan_handle) AS pa CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st CROSS APPLY sys.dm_exec_text_query_plan(qs.plan_handle, qs.statement_start_offset, qs.statement_end_offset) AS tqp WHERE pa.attribute = N'dbid' AND CONVERT(int, pa.value) = DB_ID() AND st.text LIKE @muster)SELECT plan_xml.value('(//@QueryVariantID)[1]', 'int') AS variante, execution_count AS ausfuehrungen, plan_xml.value('(//ParameterList/ColumnReference/@ParameterCompiledValue)[1]', 'nvarchar(50)') AS kompiliert_fuer, plan_xml.value('(//RelOp[@PhysicalOp = "Index Seek" or @PhysicalOp = "Clustered Index Scan"]/@PhysicalOp)[1]', 'nvarchar(50)') AS zugriff, plan_xml.value('(//RelOp[@PhysicalOp = "Index Seek" or @PhysicalOp = "Clustered Index Scan"]/@EstimateRows)[1]', 'float') AS schaetzung_beim_kompilieren, last_logical_reads AS lesezugriffe_zuletzt, min_logical_reads AS lesezugriffe_min, max_logical_reads AS lesezugriffe_maxFROM variantenWHERE plan_xml.exist('//Dispatcher/ParameterSensitivePredicate') = 1ORDER BY varianteOPTION (MAXDOP 1);The columns kompiliert_fuer, zugriff and schaetzung_beim_kompilieren fit queries with one parameter on one table, as in this course. The logical reads come from sys.dm_exec_query_stats. For plans with a key lookup, they were a few percent above the value from STATISTICS IO in the lab; we did not investigate the cause. The output shows numbers with a decimal point and the compiled value in parentheses, such as 1.337 and (20001).
In the lab, two variants were in the cache afterwards:
| Variant | Executions | Compiled for | Access | Estimate at compile time | Logical reads last (plan cache) |
|---|---|---|---|---|---|
| 1 | 1 | 20001 | Index Seek | 1.337 | 33 |
| 3 | 1 | 20008 | Clustered Index Scan | 500,000 | 13,718 |
Now run the calls for customer 20005 (1,000 orders) and customer 20004 (101 orders).
USE PspKurs;GOIF DB_NAME() <> N'PspKurs' THROW 50000, N'Nicht in PspKurs. / Not in PspKurs.', 1;EXEC dbo.Bestellungen_Kunde @KundeId = 20005; -- 1,000 ordersEXEC dbo.Bestellungen_Kunde @KundeId = 20004; -- 101 orders, estimated 1.337GOThen run the variants query again. In the lab:
| Variant | Executions | Compiled for | Access | Estimate at compile time | Logical reads last (plan cache) |
|---|---|---|---|---|---|
| 1 | 2 | 20001 | Index Seek | 1.337 | 307 |
| 2 | 1 | 20005 | Index Seek | 1,000 | 3,218 |
| 3 | 1 | 20008 | Clustered Index Scan | 500,000 | 13,718 |
Customer 20005 compiled variant 2. Customer 20004 did not create a new variant but shared variant 1; the 307 logical reads are its call. Its estimate is 1.337 rows (part 2), which is below the lower boundary value of 100. The dispatcher does not see that it actually has 101 orders.
Find the dispatcher in the plan
The dispatcher is part of the procedure’s plan. The variants lie in the plan cache as separate entries of type Prepared, not under the procedure. The variants query therefore searches through the text of the variants. The following query reads the dispatcher’s boundary values from the plans of the procedures in the current database.
USE PspKurs;GO-- Dispatchers in the plans of the procedures in the current database: statistics and boundary valuesWITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')SELECT QUOTENAME(OBJECT_SCHEMA_NAME(ps.object_id)) + N'.' + QUOTENAME(OBJECT_NAME(ps.object_id)) AS prozedur, ps.object_id AS objekt_id, d.n.value('(StatisticsInfo/@Statistics)[1]', 'sysname') AS statistik, d.n.value('@LowBoundary', 'float') AS unterer_grenzwert, d.n.value('@HighBoundary', 'float') AS oberer_grenzwertFROM sys.dm_exec_procedure_stats AS psCROSS APPLY sys.dm_exec_query_plan(ps.plan_handle) AS qpCROSS APPLY qp.query_plan.nodes('//Dispatcher/ParameterSensitivePredicate') AS d(n)WHERE ps.database_id = DB_ID()OPTION (MAXDOP 1);In the lab: procedure [dbo].[Bestellungen_Kunde], statistics [IX_Bestellungen_KundeId], lower boundary value 100, upper boundary value 100,000.
If you have turned on the actual execution plan, you find the same information there. Open the plan XML of a call. Search for Dispatcher. In the lab, the actual plan of the call for customer 20005 contained (shortened):
<Dispatcher>
<ParameterSensitivePredicate LowBoundary="100" HighBoundary="100000">
<StatisticsInfo … Statistics="[IX_Bestellungen_KundeId]" … />
…
<QueryPlan … QueryVariantID="2" …>
…
… option (PLAN PER VALUE(ObjectID = …, QueryVariantID = 2,
predicate_range([PspKurs].[dbo].[Bestellungen].[KundeId] = @KundeId, 100.0, 100000.0)))You recognize three features in it: the predicate with both boundary values, the number of the variant, and the PLAN PER VALUE hint that SQL Server itself appends to the text of the variant. The number applies within this dispatcher. For queries sent with sp_executesql, the hint says ObjectID = 0; the two queries on this page do not find them. Part 5 shows a query for all forms.
Experiment 2: Where exactly the boundary values lie
The customers in the orders table are unsuitable for the borderline cases: customers with 99, 100 and 101 orders share one estimate there (part 2). The table dbo.Grenzen therefore has only seven values, and each is a step boundary in the histogram. For each, the statistics have stored the row count.
| Value | Rows |
|---|---|
| 1 | 1 |
| 2 | 99 |
| 3 | 100 |
| 4 | 101 |
| 5 | 99,999 |
| 6 | 100,000 |
| 7 | 150,000 |
The dispatcher of the procedure dbo.Grenzen_Wert has the same boundary values, 100 and 100,000.
Prediction: Which values land in the same variant?
Show answer
Answer:
- Variant 1, under 100 rows: values 1 and 2.
- Variant 2, 100 to under 100,000 rows: values 3, 4 and 5.
- Variant 3, from 100,000 rows: values 6 and 7.
The boundary value 100 therefore belongs to the middle variant, the boundary value 100,000 to the upper one.
USE PspKurs;GOIF DB_NAME() <> N'PspKurs' OR NOT EXISTS (SELECT 1 FROM sys.extended_properties WHERE class = 0 AND name = N'PSG_Kurs') THROW 50000, N'Nicht in der Kursdatenbank PspKurs, nichts geändert. / Not in the course database PspKurs, nothing changed.', 1;ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;GOIF DB_NAME() <> N'PspKurs' THROW 50000, N'Nicht in PspKurs. / Not in PspKurs.', 1;EXEC dbo.Grenzen_Wert @Wert = 1;EXEC dbo.Grenzen_Wert @Wert = 2;EXEC dbo.Grenzen_Wert @Wert = 3;EXEC dbo.Grenzen_Wert @Wert = 4;EXEC dbo.Grenzen_Wert @Wert = 5;EXEC dbo.Grenzen_Wert @Wert = 6;EXEC dbo.Grenzen_Wert @Wert = 7;GOIn the variants query, set @prozedur to N'dbo.Grenzen_Wert'. Run the query. Then run the dispatcher query.
In the lab: dispatcher of [dbo].[Grenzen_Wert] with statistics [IX_Grenzen_Wert] and the boundary values 100 and 100,000; variants:
| Variant | Executions | Compiled for | Access | Estimate at compile time |
|---|---|---|---|---|
| 1 | 2 | Value 1 | Index Seek | 1 |
| 2 | 3 | Value 3 | Index Seek | 100 |
| 3 | 2 | Value 6 | Clustered Index Scan | 100,000 |
Values 1 and 2 share variant 1, values 3, 4 and 5 share variant 2, and values 6 and 7 share variant 3. Variant 2 was compiled for value 3 with 100 rows. Value 5 with 99,999 rows therefore read 306,426 pages (STATISTICS IO); a scan of the table reads 5,097.
Value 7 · actual 150,000 · estimate 150,000 → variant 3
Course lab, run 7: SQL Server 2022 CU27 and 2025 CU10 identical. Dispatcher with boundary values 100 and 100,000; variant and compiled value from the plan cache. Order of calls as in the experiment.
What happens here
On the first call, SQL Server compiles the procedure. For the query with KundeId = @KundeId, it checks whether PSP optimization comes into question; the column is strongly skewed (part 2). It therefore creates a dispatcher for this query instead of a fixed plan. The dispatcher knows two boundary values. On every call, it uses the statistics to estimate how many rows the parameter value returns and compares the estimate with the boundary values:
- Estimate below the lower boundary value: variant 1, for rare values,
- Estimate from the lower boundary value to below the upper one: variant 2, for medium values,
- Estimate from the upper boundary value: variant 3, for frequent values.
If the variant is not yet in the cache, SQL Server compiles it now, for the value of this call. If it already exists, SQL Server reuses it.
Microsoft does not document where the boundary values come from. In 2025, on SQL Server 2022 CU17, Paul White described a rule. The upper boundary value is the largest number in the column “rows at the boundary” of the histogram, rounded down to a power of ten: 500,000 becomes 100,000. We confirmed this on our versions, also after growth to 1.1 million rows, where the upper boundary value rose to 1,000,000. According to Paul White, the lower boundary value follows the same rule from the smallest number in this column and is at least 100. In all our experiments, the smallest number was 1 and the lower boundary value was 100; we did not measure a higher lower boundary value.
For customer 20004, the dispatcher estimates 1.337 rows. It therefore chooses variant 1 despite 101 actual orders.
The query in the course has one such predicate. At compatibility level 160, SQL Server uses at most one predicate per table of the query for the dispatcher. With level 170 on SQL Server 2025, several predicates of the same table can be included; there are then more than three variants. Part 6 shows this.
Experiment 3: The middle variant stays vulnerable
Variant 2 covers estimates from 100 to under 100,000 rows. Customer 20005 with 1,000 orders and customer 20006 with 10,000 share it. Which of the two compiles it is again decided by the first call.
USE PspKurs;GOIF DB_NAME() <> N'PspKurs' OR NOT EXISTS (SELECT 1 FROM sys.extended_properties WHERE class = 0 AND name = N'PSG_Kurs') THROW 50000, N'Nicht in der Kursdatenbank PspKurs, nichts geändert. / Not in the course database PspKurs, nothing changed.', 1;ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;GOIF DB_NAME() <> N'PspKurs' THROW 50000, N'Nicht in PspKurs. / Not in PspKurs.', 1;SET STATISTICS IO ON;EXEC dbo.Bestellungen_Kunde @KundeId = 20005; -- 1,000 orders, compiles variant 2EXEC dbo.Bestellungen_Kunde @KundeId = 20006; -- 10,000 orders, uses the already compiled variant 2SET STATISTICS IO OFF;GOPrediction: Which plan does variant 2 get if customer 20006 comes first?
Show answer
A clustered index scan, compiled for 10,000 rows. Customer 20005 then also reads the whole table.
USE PspKurs;GOIF DB_NAME() <> N'PspKurs' OR NOT EXISTS (SELECT 1 FROM sys.extended_properties WHERE class = 0 AND name = N'PSG_Kurs') THROW 50000, N'Nicht in der Kursdatenbank PspKurs, nichts geändert. / Not in the course database PspKurs, nothing changed.', 1;ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;GOIF DB_NAME() <> N'PspKurs' THROW 50000, N'Nicht in PspKurs. / Not in PspKurs.', 1;SET STATISTICS IO ON;EXEC dbo.Bestellungen_Kunde @KundeId = 20006; -- 10,000 orders, compiles variant 2EXEC dbo.Bestellungen_Kunde @KundeId = 20005; -- 1,000 orders, uses the already compiled variant 2SET STATISTICS IO OFF;GOPages read from STATISTICS IO, table Bestellungen:
| Order | Plan of variant 2 | Customer 20005 (1,000) | Customer 20006 (10,000) |
|---|---|---|---|
| 20005 first | Index Seek + Key Lookup | 3,076 pages | 30,647 pages |
| 20006 first | Clustered Index Scan | 13,718 pages | 13,718 pages |
A scan over the whole table reads 13,718 pages. If customer 20005 comes first, customer 20006 reads more than twice as much with the plan for 1,000 orders. If customer 20006 comes first, customer 20005 reads 13,718 instead of 3,076 pages. Within a variant, what part 1 showed for the whole procedure applies: one plan for all values whose estimate falls into the variant’s span.
How to determine the variant for a value
- Use the dispatcher query to read the two boundary values.
- Derive the estimate for the value from the histogram (part 2).
- Compare the estimate with the boundary values. Below the lower boundary value, variant 1 applies; from the lower boundary value to below the upper one, variant 2; from the upper one, variant 3.
- Check the result in the actual plan at
QueryVariantID.
How to find dispatchers on your system
Warning: Run the two queries on a production system outside peak load. They read the plans in the plan cache and can use noticeable CPU on large servers. They change nothing.
- In both queries, remove the line
USE PspKurs;. - Choose the database you want to check.
- Run the dispatcher query. For each procedure, note the statistics and the two boundary values.
- In the variants query, set
@prozedurto one of these procedures, with schema. - Run the variants query.
- For each variant, compare the smallest and the largest value of the logical reads.
If the largest value is a multiple of the smallest, values with very different row counts share one plan in this variant. Both queries see only plans that are in the cache right now. Without VIEW DEFINITION on the procedure, the variants query aborts with a message, and the dispatcher query shows only the object ID instead of the name.
What the experiment shows and what it doesn't
- It shows the dispatcher, boundary values and variants for a query with one parameter in a procedure.
- Checked on SQL Server 2022 CU27 and 2025 CU10 in a Linux container, compatibility level 160, statistics with
FULLSCAN: one pass each with the code blocks of this page; variants, boundary values and experiment 3 additionally in two earlier passes per version. The page counts come from SQL Server 2022; 2025 differed by at most 2 pages. - It does not show several parameters or several queries with a dispatcher in one procedure. Part 6 shows that.
- It does not show when SQL Server creates no dispatcher at all. That is the topic of part 4.
Check your understanding
1. When does variant 2 come into being?
Show answer
On the first call for which the dispatcher estimates 100 to under 100,000 rows. Before that, it is not in the cache.
2. Value 6 is a step boundary. The statistics report 100,000 rows for it. Which variant does the dispatcher choose?
Show answer
Variant 3. From the upper boundary value 100,000, the variant for frequent values applies. In the experiment, value 6 got variant 3.
3. Customer 20005 came first. Why does customer 20006 then read more than a scan would, although PSP optimization is on?
Show answer
It shares variant 2 with customer 20005. Variant 2 is compiled for 1,000 rows: an index seek with a key lookup for each row. For 10,000 rows, that causes more logical reads than a scan.
Sources
- Microsoft: Parameter Sensitive Plan Optimization
- Paul White: Parameter Sensitive Plan Optimization Thresholds (January 2025, SQL Server 2022 CU17)
- Hugo Kornelis: Execution Plan Reference, Plan properties (dispatcher, QueryVariantID)
Next: Part 4: When does PSP optimization apply, and when not? (coming) · Back: Part 2 · Course overview