Knowledge · Course PSP optimization

One Plan for All Values: Reproduce Parameter Sniffing

A procedure returns the orders of one customer. For most customers it is fast, for a few it is very slow. Overnight SQL Server updated the statistics, and by morning everything is different: the large customers are fast, the small ones slow. Nobody changed the code. This part shows why. It lays the groundwork for everything that PSP optimization changes later.

Updated Dec 1, 2026 · approx. 9 min read

Part 1 of 6Topic Parameter sniffing · plan cacheLab SQL Server 2022 and 2025
FROM THE COURSE LAB · PAGES READPlan in cacheone for allIndex Seek + Key Lookupcompiled for customer 20001 · 10 ordersCustomer 2000110 orders33pagesCustomer 20008500,000 orders1.5 MpagesA scan of the whole table13,718 pages, for any customerSame plan, two values:about 109 times as much as a scan.

Learning goal: You can explain why a stored plan fits one value and fails for another, and how the order of the calls decides which plan is stored.

Prerequisite: You have built the course database PspKurs from the course overview.

In short

  • SQL Server compiles a procedure for the value that the first call brings. It stores the plan in the plan cache and reuses it until the procedure is recompiled.
  • A plan that fits a small customer does not fit a large one. The experiment shows how big the difference is.
  • Which plan sits in the cache depends on who comes first after compilation. That makes the problem hard to pin down in production. This is where PSP optimization comes in.

The experiment

Customer 20001 has 10 orders, customer 20008 has 500,000. The experiment calls the procedure dbo.Bestellungen_Kunde for both customers, once with the small one first and once with the large one first. To show the pure problem, it turns PSP optimization off for the course database. Before each call order, it clears the plan cache of the course database so that the first call compiles anew.

Warning: Run the tabs only on a test instance with the course database. CLEAR PROCEDURE_CACHE removes all stored plans of the current database. Each tab therefore first checks that it is in PspKurs and stops otherwise.

Before you run them, include the actual execution plan in SQL Server Management Studio (menu Query > Include Actual Execution Plan).

Predict first

PSP optimization is off, and the cache holds no plan for the procedure yet. A scan over the whole Bestellungen table reads about 14,000 pages.

You call customer 20001 first, then customer 20008. About how many pages does the second call read?

  1. about 30, as many as the first
  2. about 14,000, as many as a scan
  3. clearly more than a scan
Show answer

Answer: 3. In the experiment it was about 1.5 million page accesses, roughly 109 times as many as a scan. The section “What happens here” explains why.

And the other way round: How many pages does customer 20001 read if customer 20008 came first?

Show answer

Answer: As many as a scan, 13,718 pages. Without the large customer before it, it would be 33.

The same procedure, two call orders

First 10 orders, then 500,000
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 = OFF;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 = 20001;   -- 10 ordersEXEC dbo.Bestellungen_Kunde @KundeId = 20008;   -- 500,000 ordersGO

20001: 33 pages · 20008: 1,500,874 pages, both with Index Seek and Key Lookup

First 500,000 orders, then 10
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 = OFF;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 = 20008;   -- 500,000 ordersEXEC dbo.Bestellungen_Kunde @KundeId = 20001;   -- 10 ordersGO

20008: 13,718 pages · 20001: 13,718 pages, both with Clustered Index Scan

Code as in the lab. Each tab runs on its own. SQL Server 2022 CU27 and 2025 CU10, compatibility level 160, PSP optimization turned off. Pages read from STATISTICS IO; 2025 read 1,500,873 pages for the large customer with the seek plan.

The pages read appear on the Messages tab, in the Table 'Bestellungen' line under logical reads. Your numbers may differ by a few pages.

Call orderPlan in cacheCustomer 20001 (10)Customer 20008 (500,000)
Small firstIndex Seek + Key Lookup33 pages1,500,874 pages
Large firstClustered Index Scan13,718 pages13,718 pages
Plan in cacheIndex Seek + Key Lookupcompiled for customer 20001101,000100,00010 Mscan: 13,718 pages1st call · customer 2000110 orders332nd call · customer 20008500,000 orders1,500,874Plan in cacheIndex Seek + Key Lookupcompiled for customer 20001101,000100,00010 Mscan: 13,718 pages1st call · customer 2000110 orders332nd call · customer 20008500,000 orders1,500,874

The same plan reads about 109 times as much as a scan for customer 20008.

Pages read from STATISTICS IO, course lab: SQL Server 2022 CU27 and 2025 CU10, two runs each with a freshly built database, compatibility level 160, PSP optimization off. Logarithmic axis. 2025 read one page less for the large customer.

One plan, two customers: the first call decides which plan is cached. The second customer gets it, whether it fits or not.

Check the plan

After the “Small first” tab, open the execution plan of the second call, that is, for customer 20008.

  1. Check the operators. The plan shows an Index Seek on IX_Bestellungen_KundeId and a Key Lookup on the clustered index.
  2. Check the estimated rows at the Index Seek. It says 1.337; the actual number was 500,000.
  3. Open the properties of the SELECT operator and look at its parameter list. The compiled value (ParameterCompiledValue) is 20001, and the runtime value (ParameterRuntimeValue) is 20008.

What happens here

On the first call there is no plan for the procedure yet. SQL Server therefore compiles it and looks at the value the procedure is being called with. This peeking is called parameter sniffing. For customer 20001, the optimizer estimates only a little more than one row (1.337); in fact there are 10. Part 2 shows where this estimate comes from. For so few rows, it is cheap to look up the entries in the index on KundeId and to fetch the amount for each one from the clustered index. That costs 33 page accesses.

SQL Server stores this plan in the plan cache. The second call brings customer 20008, but SQL Server does not compile again. It reuses the stored plan and looks up the amount in the clustered index one by one for 500,000 matches. Together with the index seek, that came to about 1.5 million page accesses. The plan reads many pages several times; each access counts. The scan came to 13,718 in the experiment.

In the opposite order, SQL Server compiles for 500,000 rows and picks the scan. That is right for the large customer. The small customer then also reads the whole table, although 33 page accesses would have been enough.

Neither plan is wrong. Each fits the value it was compiled for. The problem is that there is only one plan for all values.

Why this is hard to pin down in production

Which plan sits in the cache depends on who calls the procedure first after the old plan became invalid or left the cache. That happens, for example, after a restart, after a statistics update, or when the cache needs space. This is why the behavior looks random. A restart seems to fix the problem until the next unlucky first call arrives. How to recognize such cases in the plan and its parameters is shown in our guide Sometimes Fast, Sometimes Slow.

What PSP optimization changes

Instead of one plan for all values, SQL Server can, from compatibility level 160, create up to three plans for one query parameter, the variants: one for rare, one for medium and one for frequent values. On each call, a plan component called the dispatcher decides which variant fits the value. With several parameters, there can be more variants; part 6 shows this. Part 2 shows how SQL Server knows which values are rare and which are frequent. Part 3 shows what the dispatcher and the variants look like in the plan.

One limitation up front: PSP optimization does not apply to every query, and for medium values, very different values still share one variant. Parts 4 and 6 cover both.

Finish the experiment

Turn PSP optimization back on for the course database. The following parts assume this.

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

Then check the switch. The output must show PspKurs and the value 1.

USE PspKurs;GOSELECT DB_NAME() AS datenbank, name, valueFROM sys.database_scoped_configurationsWHERE name = N'PARAMETER_SENSITIVE_PLAN_OPTIMIZATION';

If you want to see what changes right away, run the “Small first” order once more with PSP optimization turned on:

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 = 20001;   -- 10 ordersEXEC dbo.Bestellungen_Kunde @KundeId = 20008;   -- 500,000 ordersGOSET STATISTICS IO OFF;GO

In the experiment, customer 20001 again read 33 pages and customer 20008 read 13,718 pages with a scan. Each customer got its own plan. Part 3 shows how this works.

How to spot the pattern on your system

  1. In Query Store, look for queries whose pages read fluctuate strongly. To do this, compare min_logical_io_reads and max_logical_io_reads per plan in sys.query_store_runtime_stats.
  2. Check whether the fluctuation matches the amount of data. If a call for 500,000 rows reads more than one for 10, that is normal.
  3. Open the plan XML of a suspicious plan.
  4. Note the compiled value of the parameter (ParameterCompiledValue). It can be missing, for example in queries with local variables.
  5. Pick a rare and a frequent value for the counter-test.
  6. On the test system, clear the plan cache of the test database.
  7. Call the query first with the rare value, then with the frequent one. Note the pages read.
  8. Clear the plan cache again. Call the values in the opposite order.
  9. Compare the two measurements for the same value. If the same value reads many times more depending on the order, the query is sensitive to the value of its parameter.

What the experiment shows and what it doesn't

  • It shows the mechanism in a simple procedure with one parameter.
  • Measured are pages read from STATISTICS IO:

- SQL Server 2022 CU27 and 2025 CU10 in a Linux container, - two passes each with a freshly built database, equal to within one page, - compatibility level 160, statistics with FULLSCAN, data in memory.

  • It does not show how long the calls take. Durations depend on hardware, parallelism and cache; pages read show the difference between the plans more reliably.
  • It simplifies. Real queries have several parameters and joins. The same mechanism applies there. We did not measure how strongly.

Check your understanding

1. When does SQL Server fix the plan for the course procedure with PSP optimization turned off?

Show answer

If no reusable plan is in the cache, SQL Server compiles the procedure on the call. It uses the value of @KundeId that this call brings.

2. Why does the large customer read so much more with the small customer’s plan than with a scan?

Show answer

The plan looks up each of the 500,000 orders one by one in the clustered index. It reads many pages several times, and each access counts. The scan reads the table in one pass.

3. What does customer 20001 read if customer 20008 came first after clearing the cache, and why?

Show answer

13,718 pages instead of 33. The first call compiled a scan for 500,000 rows, and the small customer reuses that plan.

4. After a restart, a query is fast again. Which explanation other than “The server was overloaded” fits?

Show answer

The restart cleared the plan cache. The first call after it compiled a plan for a different value that fits most calls better.

Sources

Next: Part 2: Reading the statistics histogram · Back: Course overview

More parts of the course