SQL Server Statistics

SQL Server keeps statistics per statistics object. Creating an index also creates a statistics object for its key columns, and if a column used in a condition has no statistics, the optimizer automatically creates single-column statistics (_WA_Sys_…). A statistics object consists of a header, a density vector for combinations of the leading key columns, and a histogram (up to 200 steps) on the first column. The optimizer estimates the row count of each operator from these values and the rules of the cardinality estimator (CE).

Summary

ItemDescription
When createdIndex creation (same as a full scan), automatic creation (AUTO_CREATE_STATISTICS), CREATE STATISTICS
UpdateAutomatic update (AUTO_UPDATE_STATISTICS), UPDATE STATISTICS, sp_updatestats, index rebuild
Automatic update thresholdCompatibility 130 and above: when changed rows exceed MIN(500 + 0.2 × row count, √(1000 × row count)). 129 and below: 500 + 20%
Update timingAt the next compilation that uses the statistics after the threshold is exceeded. With AUTO_UPDATE_STATISTICS_ASYNC, this compilation uses the old statistics and the update runs in the background
SampleAutomatic update uses an automatic sample based on table size. WITH FULLSCAN, SAMPLE n PERCENT, PERSIST_SAMPLE_PERCENT = ON
HistogramOnly on the first column of a statistics object, up to 200 steps
FreezingNORECOMPUTE (excluded from automatic update)
SandboxGathered with FULLSCAN after loading, and the database's automatic creation and automatic update are turned off (compatibility level 160)

Reading a Statistics Object

DBCC SHOW_STATISTICS ('orders', 'orders_x1') WITH STAT_HEADER, DENSITY_VECTOR;
SELECT * FROM sys.dm_db_stats_histogram(OBJECT_ID('customers'),
         (SELECT stats_id FROM sys.stats WHERE object_id = OBJECT_ID('customers') AND name = 'customers_x1'));
SELECT s.name, sp.rows, sp.rows_sampled, sp.steps, sp.modification_counter, sp.last_updated
FROM sys.stats s CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) sp
WHERE s.object_id = OBJECT_ID('orders');

Header

ValueSandbox value of orders_x1Meaning
Rows / Rows Sampled2,000,000 / 2,000,000The sample is the whole table (FULLSCAN)
Steps21Number of histogram steps
Updated2026-11-26 13:38Last update
modification_counter0Number of rows changed since the update (compared with the automatic update threshold)

Density Vector

All densityColumns
0.0000099999997cust_id
0.0000005cust_id, ord_dt
0.0000005cust_id, ord_dt, ord_id

Density is 1 ÷ number of distinct values. For cust_id it is 1 ÷ 100,000. If the value of an equality condition is unknown at compile time (a local variable, OPTIMIZE FOR UNKNOWN, an outer value inside an NL join), the estimate is row count × density, an average of 20 rows per customer. Only the densities of leading combinations exist, so the density of (cust_id, ord_dt) is usable, but this object has no density for ord_dt alone.

Histogram

In the histogram of customers_x1(grade), the three values each become a step as they are.

RANGE_HI_KEYEQ_ROWSRANGE_ROWSDISTINCT_RANGE_ROWS
GOLD9,00000
NORMAL90,00000
VIP1,00000
ColumnMeaning
RANGE_HI_KEYUpper boundary value of the step
EQ_ROWSNumber of rows equal to the boundary value
RANGE_ROWSNumber of rows between the previous step's boundary value and this boundary value (both ends excluded)
DISTINCT_RANGE_ROWSNumber of distinct values in that range
AVG_RANGE_ROWSRANGE_ROWS ÷ DISTINCT_RANGE_ROWS. The estimated number of rows per value in the range

grade = 'VIP' equals a boundary value, so the estimate uses EQ_ROWS, 1,000, as it is. Oracle in the sandbox, which has no histogram, sees the same condition as 33,333 customers (Oracle Optimizer Statistics). orders_x1 has 2 million rows but only 21 steps. This is because SQL Server merges similar ranges to reduce steps, and customers between steps are estimated with AVG_RANGE_ROWS (the range average).

Cardinality Estimator

ItemOld estimator (CE 70)New estimator (CE 120 and above, 2014+)
AND conditionsProduct of selectivities (independent)Exponential backoff: most selective condition × square root of the second × fourth root of the third …
JoinsAligns the histograms of both sides step by stepBy default aligns only the minimum and maximum of the histograms (Coarse Alignment)
Values outside the histogram range (recent dates)Estimated as 1 rowEstimated from density (mitigates the ascending key problem)
How to selectCompatibility level 110 and below, USE HINT('FORCE_LEGACY_CARDINALITY_ESTIMATION')Compatibility level 120 and above

The new estimator is not always better. For an AND condition on uncorrelated columns, the old estimator's product is more accurate, and for strongly correlated columns the new estimator is more accurate. Check which one was used with CardinalityEstimationModelVersion in the plan.

How to Change Statistics

-- Update by reading everything
UPDATE STATISTICS dbo.orders orders_x1 WITH FULLSCAN;

-- Set the sample percentage, and later automatic updates use the same percentage
UPDATE STATISTICS dbo.orders WITH SAMPLE 10 PERCENT, PERSIST_SAMPLE_PERCENT = ON;

-- Multi-column statistics (first-column histogram + combination density)
CREATE STATISTICS st_orders_status_dt ON dbo.orders (status, ord_dt) WITH FULLSCAN;

-- Filtered statistics: distribution of only some rows
CREATE STATISTICS st_orders_canceled_dt ON dbo.orders (ord_dt) WHERE status = 'CANCELED' WITH FULLSCAN;
SituationMethod
The sample is small and the histogram is coarseFULLSCAN or PERSIST_SAMPLE_PERCENT
Recent values are outside the histogramUpdate right after loading. Use the new estimator
A skewed value is a problem only under some conditionsFiltered statistics
The best plan differs per parameter valueOPTION (RECOMPILE), OPTIMIZE FOR, Parameter Sensitive Plan (PSP) in 2022
Plans fluctuate because of updatesNORECOMPUTE, force the plan with Query Store

Comparison with Other DBMSs

ItemSQL ServerOraclePostgreSQL
UnitStatistics object (index or column combination)Table, column, indexTable, column
DistributionFirst-column histogram of up to 200 stepsOnly on skewed columns used in conditions (SIZE AUTO)MCV + histogram on every column
Composite columnsDensity vector (leading combinations)Column group statisticsCREATE STATISTICS
Automatic updateAt the next compilation (sync/async)Job in the maintenance windowANALYZE by autovacuum
FreezingNORECOMPUTELOCK_TABLE_STATSNone

Learn more: SQL Server Execution Plan Analysis, SQL Server Connections, Cursors, and Plan Cache.

NOTE The automatic update threshold and estimator behavior differ by compatibility level and version. The values in this document were verified on SQL Server 2022 (compatibility level 160) in the sandbox.

Related pages