Reading the Statistics Histogram: How SQL Server Knows How Values Are Distributed
In part 1, SQL Server estimated a little more than one row for customer 20001; in fact there were 10. That estimate comes from the histogram of the statistics on KundeId. PSP optimization needs this histogram as well. At compile time, it uses the histogram to check how skewed the values are. On every call, the choice of the variant depends on the row estimate for the value. This part shows how to read the histogram and how to derive the estimate for a value.
Learning goal: For a comparison such as KundeId = 20004, you can derive the row estimate from the histogram and check it in the execution plan.
Prerequisite: Part 1. You have built the course database PspKurs.
In short
- SQL Server summarizes the values of a column in at most 200 histogram steps. For the step boundary of a step, it stores how many rows have exactly that value.
- For values between two step boundaries, SQL Server stores only an average. The experiment shows what that means for individual customers.
- PSP optimization works with these estimates, not with the actual rows. Part 3 shows how this affects the choice of the variant.
Predict first
Customer 20001 has 10 orders, customer 20004 has 101, customer 20005 has 1,000. In part 1, SQL Server estimated a little more than one row (1.337) for customer 20001.
What estimate does customer 20004 get?
- about 101
- a little more than 1, like customer 20001
- about 1,000
Show answer
Answer: 2. According to the histogram, customers 20001 to 20004 each get 1.337 rows; checked in the plan for 20001 and 20004. Customer 20005 gets 1,000. The experiment shows why.
The experiment
The experiment first reads the histogram of the statistics IX_Bestellungen_KundeId. It then calculates the estimate for a few customers from the histogram. At the end, you check in the estimated execution plan that the optimizer uses the same estimate. All queries only read.
Read the histogram
The query needs SELECT on the column KundeId or ownership of the table.
USE PspKurs;GO-- Histogram of the statistics on KundeId, excerpt around customers 20001 to 20008 and 30001 to 30012SELECT h.step_number AS schritt, h.range_high_key AS schrittgrenze, h.equal_rows AS zeilen_an_der_grenze, h.range_rows AS zeilen_im_bereich, h.distinct_range_rows AS werte_im_bereich, h.average_range_rows AS zeilen_je_wert_im_bereichFROM sys.stats AS sCROSS APPLY sys.dm_db_stats_histogram(s.object_id, s.stats_id) AS hWHERE s.object_id = OBJECT_ID(N'dbo.Bestellungen') AND s.name = N'IX_Bestellungen_KundeId' AND TRY_CONVERT(int, h.range_high_key) BETWEEN 19000 AND 30012ORDER BY h.step_numberOPTION (MAXDOP 1);Result in the lab (SQL Server 2022 CU27 and 2025 CU10 equal):
| Step | Step boundary | Rows at the boundary | Rows in the range | Values in the range | Rows per value in the range |
|---|---|---|---|---|---|
| 5 | 19096 | 1 | 4,095 | 4,095 | 1 |
| 6 | 20005 | 1,000 | 1,214 | 908 | 1.337 |
| 7 | 20006 | 10,000 | 0 | 0 | 1 |
| 8 | 20007 | 100,000 | 0 | 0 | 1 |
| 9 | 20008 | 500,000 | 0 | 0 | 1 |
| 10 | 30005 | 1,000 | 470 | 4 | 117.5 |
| 11 | 30010 | 300 | 180 | 4 | 45 |
| 12 | 30011 | 1,000 | 0 | 0 | 1 |
Here is how to read a row, using step 6 as the example:
- Step boundary 20005: The step ends at customer 20005. Its range begins directly after the step boundary of the previous step, that is, at customer 19097.
- Rows at the boundary 1,000: This is how many orders customer 20005 had when the statistics were created. Because the setup script read all rows (
FULLSCAN), it is exactly its 1,000. - Rows in the range 1,214, values in the range 908: Between the previous step boundary and 20005 there are 908 different customers with 1,214 orders in total. Among them are 904 one-off customers with one order each and customers 20001 to 20004 with 10, 99, 100 and 101.
- Rows per value in the range 1.337: 1,214 divided by 908. SQL Server stores nothing more about the individual customers in this range.
Steps 7 to 9 have no range: there is no further customer between their step boundaries. The last column still shows 1 there.
The columns are called this in DBCC SHOW_STATISTICS … WITH HISTOGRAM and in the documentation:
| In the course | sys.dm_db_stats_histogram | DBCC SHOW_STATISTICS |
|---|---|---|
| step boundary | range_high_key | RANGE_HI_KEY |
| rows at the boundary | equal_rows | EQ_ROWS |
| rows in the range | range_rows | RANGE_ROWS |
| values in the range | distinct_range_rows | DISTINCT_RANGE_ROWS |
| rows per value in the range | average_range_rows | AVG_RANGE_ROWS |
Your prediction: According to this table, what estimate do you expect for customer 30002 and for customer 30005?
Show answer
Customer 30002 lies in the range of step 10: 117.5. Customer 30005 is the step boundary of step 10: 1,000. In fact, 30002 has only 50 orders. Within a range, the estimate can therefore also be too high.
Derive the estimate
The following query looks up, for each customer, the step the customer falls into. If the customer is exactly on the step boundary, it takes the rows at the boundary. If the customer is in the range before it, it takes the rows per value in the range. It reports values outside the histogram without an estimate. For comparison, it counts the actual orders; this needs SELECT on the table.
USE PspKurs;GO-- Estimate per customer from the histogram, next to the actual number of ordersWITH histogramm AS ( SELECT TRY_CONVERT(int, h.range_high_key) AS schrittgrenze, h.step_number, h.equal_rows, h.average_range_rows FROM sys.stats AS s CROSS APPLY sys.dm_db_stats_histogram(s.object_id, s.stats_id) AS h WHERE s.object_id = OBJECT_ID(N'dbo.Bestellungen') AND s.name = N'IX_Bestellungen_KundeId')SELECT k.KundeId AS kunde, (SELECT COUNT(*) FROM dbo.Bestellungen AS b WHERE b.KundeId = k.KundeId) AS tatsaechlich, x.step_number AS schritt, CASE WHEN x.step_number IS NULL THEN N'nach der letzten Schrittgrenze' WHEN x.schrittgrenze = k.KundeId THEN N'an der Schrittgrenze' WHEN x.step_number = 1 THEN N'vor der ersten Schrittgrenze' ELSE N'im Bereich' END AS lage, CASE WHEN x.schrittgrenze = k.KundeId THEN x.equal_rows WHEN x.step_number > 1 THEN x.average_range_rows END AS schaetzung_laut_histogrammFROM (VALUES (42), (20001), (20003), (20004), (20005), (30002), (30005), (30006)) AS k(KundeId)OUTER APPLY (SELECT TOP (1) * FROM histogramm AS h WHERE h.schrittgrenze >= k.KundeId ORDER BY h.schrittgrenze) AS xORDER BY k.KundeIdOPTION (MAXDOP 1);| Customer | Actual | Step | Position | Estimate per histogram |
|---|---|---|---|---|
| 42 | 1 | 2 | im Bereich (in the range) | 1 |
| 20001 | 10 | 6 | im Bereich (in the range) | 1.337 |
| 20003 | 100 | 6 | im Bereich (in the range) | 1.337 |
| 20004 | 101 | 6 | im Bereich (in the range) | 1.337 |
| 20005 | 1,000 | 6 | an der Schrittgrenze (at the step boundary) | 1,000 |
| 30002 | 50 | 10 | im Bereich (in the range) | 117.5 |
| 30005 | 1,000 | 10 | an der Schrittgrenze (at the step boundary) | 1,000 |
| 30006 | 10 | 11 | im Bereich (in the range) | 45 |
Customer 20004 · actual 101 · estimate 1.337 · range of step 6
Histogram of the statistics IX_Bestellungen_KundeId after FULLSCAN, course lab: SQL Server 2022 CU27 and 2025 CU10 identical. Steps drawn with equal width, height on a log scale. Estimate as shown at the Index Seek in the estimated plan.
Look in the plan
Check for two customers that the optimizer uses exactly this estimate. For the estimated plan, you need the SHOWPLAN permission in the database.
- Select the whole following block, including the line
USE PspKurs;. - Display the estimated execution plan (menu Query > Display Estimated Execution Plan).
- At the
Index Seekoperator, check the estimated number of rows. For customer 20004 it says 1.337, for customer 20005 it says 1,000. A German user interface shows 1,337 with a comma; the number is the same.
USE PspKurs;GOSELECT COUNT(*) AS Bestellungen, SUM(Betrag) AS Umsatz FROM dbo.Bestellungen WHERE KundeId = 20004;SELECT COUNT(*) AS Bestellungen, SUM(Betrag) AS Umsatz FROM dbo.Bestellungen WHERE KundeId = 20005;What happens here
For a statistic, SQL Server sorts the values of the first key column and summarizes them in at most 200 steps. Microsoft calls the method “Maximum Difference”: neighboring values whose merging costs little information end up in the same step. In the course database there are 192 steps. For the step boundary, SQL Server stores its row count. For all values before it, it stores only two totals: how many rows and how many different values the range contains.
When a query asks for a specific value, the optimizer looks for the step the value falls into. In the experiment, a value on the step boundary got its row count, and a value in the range got the average of the range. The three Microsoft Learn pages we checked describe the columns but do not state this calculation rule explicitly. We verified it against the estimated plan.
Customer 20004 has 101 orders but lies in the same range as 904 one-off customers with one order each. The average of this range is 1.337, and the 101 orders disappear in the total. Conversely, customer 30002 with 50 orders lies in a range with large neighbors and is overestimated at 117.5. Customer 20005, on the other hand, is a step boundary itself. For it, SQL Server stored the 1,000.
When the reading matches the plan
In the experiment, reading and plan agreed. This holds only under these conditions:
- The statistics read all rows (
rows_sampledequal torows). With a sample, all columns of the histogram are extrapolations. - The table has not changed since the statistics were last updated (
modification_counter0). Otherwise the estimate in the plan can differ from the reading. - The optimizer knows the value at compile time: a literal or a sniffed parameter. With a local variable, or with parameter sniffing turned off, it uses an average over the whole column, not the histogram. In the course lab, the optimizer estimated 37.9 rows for every customer with a local variable.
- The value lies within the histogram. For values before the first or after the last step boundary, other rules apply.
- The comparison is an equality on the first column of the statistics. The optimizer estimates comparisons with
>orBETWEENand multi-column statistics differently.
Which values become a step boundary
Microsoft does not describe which values exactly become a step boundary. The 600 regular customers of the course database were distributed like this:
| Orders per regular customer | Customers | of these step boundary | Estimate per histogram, smallest | Estimate per histogram, largest |
|---|---|---|---|---|
| 10 | 100 | 1 | 10 | 96 |
| 20 | 100 | 0 | 45 | 117.5 |
| 50 | 100 | 0 | 45 | 117.5 |
| 100 | 100 | 0 | 45 | 117.5 |
| 300 | 100 | 82 | 96 | 300 |
| 1,000 | 100 | 100 | 1,000 | 1,000 |
With 1,000 orders, all 100 customers were a step boundary; with 300 orders, 82 of 100. In the groups with 20, 50 and 100 orders, none was. These customers landed in ranges and were partly underestimated and partly overestimated. We did not check how the method decides for differently distributed data.
What this means for PSP optimization
PSP optimization uses the histogram in two ways:
- At compile time, it checks, among other things, how skewed the values are. According to Paul White and our experiment in part 4, what counts here is the ratio between the largest and the smallest number in the column “rows at the boundary”. In the course database it is 500,000 to 1. Part 4 shows the threshold and the other conditions.
- On every call, the dispatcher assigns the value to a variant, based on the estimated row count. Customer 20004 is estimated at 1.337 rows and therefore gets the variant for rare values, although it has 101 orders. Part 3 shows this in the plan.
The following query calculates the ratio for the statistics on KundeId:
USE PspKurs;GO-- How skewed is the distribution? Largest and smallest number in the column equal_rowsSELECT COUNT(*) AS schritte, MAX(h.equal_rows) AS max_zeilen_an_der_grenze, MIN(h.equal_rows) AS min_zeilen_an_der_grenze, MAX(h.equal_rows) / NULLIF(MIN(h.equal_rows), 0) AS verhaeltnisFROM sys.stats AS sCROSS APPLY sys.dm_db_stats_histogram(s.object_id, s.stats_id) AS hWHERE s.object_id = OBJECT_ID(N'dbo.Bestellungen') AND s.name = N'IX_Bestellungen_KundeId'OPTION (MAXDOP 1);In the lab: 192 steps; the largest number at a step boundary is 500,000, the smallest 1; ratio 500,000.
How to read a histogram on your system
- In SQL Server Management Studio, choose your target database.
- In the following query, replace
dbo.BestellungenandKundeIdwith your table and column. - Run the query. It shows all statistics whose first column is your column, with sample, date and changes since the last update.
-- Which statistics start with the column? Only reads.SELECT s.name AS statistik, s.has_filter AS gefiltert, p.last_updated AS aktualisiert, p.rows AS zeilen, p.rows_sampled AS gelesen, p.modification_counter AS aenderungen_seither, p.steps AS schritteFROM sys.stats AS sJOIN sys.stats_columns AS sc ON sc.object_id = s.object_id AND sc.stats_id = s.stats_id AND sc.stats_column_id = 1JOIN sys.columns AS c ON c.object_id = sc.object_id AND c.column_id = sc.column_idCROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) AS pWHERE s.object_id = OBJECT_ID(N'dbo.Bestellungen') AND c.name = N'KundeId'OPTION (MAXDOP 1);- Choose the statistics. If there are several, the plan XML shows under
OptimizerStatsUsagewhich one the optimizer used. - In the query “Read the histogram”, replace table and statistics. Remove the line
USE PspKurs;. - Remove the condition on
range_high_keythere. - In the query “Derive the estimate”, replace the data type
intwith the data type of your column and the customer numbers with your values. The count reads the matching rows for each value. - Check the conditions from “When the reading matches the plan”.
- Check the derived estimate in the estimated execution plan.
If an important value lies in a range with a small average, the optimizer underestimates it.
What the experiment shows and what it doesn't
- It shows the histogram of statistics created with
FULLSCANfrom all rows, and the estimates derived from it for equality comparisons with a known value. - Checked on SQL Server 2022 CU27 and 2025 CU10, compatibility level 160, table unchanged since the build: in run 2, two passes each with a freshly built database; in run 6, the code blocks of this page.
- It does not show what statistics with a sample look like. Microsoft points out that small samples give less accurate histograms for skewed data; part 4 shows what this means for PSP optimization.
- It does not show estimates for comparisons with
>orBETWEEN, values outside the histogram, or multi-column statistics.
Check your understanding
1. A value is a step boundary. Which column provides its estimate?
Show answer
“Rows at the boundary” (equal_rows). It states how many rows had exactly this value when the statistics were created.
2. Why do customers 20001 and 20004 get the same estimate although they have 10 and 101 orders?
Show answer
Both lie in the range of step 6. For values in the range, SQL Server stores only the average, here 1,214 rows over 908 values, that is, 1.337.
3. Customer 30004 and customer 30010 have 300 orders each. What estimate does each get according to the table above?
Show answer
Customer 30004 lies in the range of step 10: 117.5. Customer 30010 is the step boundary of step 11: 300. Customers of the same size can therefore be estimated differently.
4. Which variant will PSP optimization probably give customer 20004, and why?
Show answer
The variant for rare values. The dispatcher decides by the estimate of 1.337 rows, not by the actual 101 orders. Part 3 shows this in the plan.
Sources
- Microsoft: Statistics, section Histogram
- Microsoft:
sys.dm_db_stats_histogram - Microsoft: DBCC SHOW_STATISTICS
- Microsoft: Cardinality Estimation
- Paul White: Parameter Sensitive Plan Optimization Thresholds (January 2025)
Next: Part 3: Dispatcher and variants · Back: Part 1 · Course overview