Overview

Plan quality depends on row-count estimates. SQL Server builds statistics (a histogram plus density information) on indexed columns and on columns used in predicates, and the cardinality estimator (CE) turns them into estimates for each operator.

Core concepts

  • A statistics object has a header (rows, rows sampled, last update), a density vector (average duplicates per key prefix) and a histogram of up to 200 steps for the leading column.
  • Auto-create (AUTO_CREATE_STATISTICS, on by default) makes single-column _WA_Sys_... statistics. Auto-update recomputes them after enough modifications; from compatibility level 130 the threshold is dynamic (about sqrt(1000 x rows) changes for big tables) rather than the old 20%.
  • Statistics are updated with sampling; use FULLSCAN for skewed data, PERSIST_SAMPLE_PERCENT to keep the setting.
  • Legacy CE (compatibility level 70-110) vs new CE (120 and above) differ in how they combine predicates and handle joins; plans can change when you change the compatibility level (sqlserver-upgrades-compat).
  • Parameters and local variables: with a parameter the optimizer uses the histogram for the sniffed value; with a local variable it uses the density average (sqlserver-parameter-sniffing).

Worked example

Statistics on the test table dbo.Orders:

DBCC SHOW_STATISTICS('dbo.Orders', 'IX_Orders_Cust') WITH STAT_HEADER;
-- Rows 500000, Rows Sampled 500000, Steps 89, Density 0.0202188
 
SELECT s.name, s.auto_created, sp.rows, sp.modification_counter
FROM sys.stats s CROSS APPLY sys.dm_db_stats_properties(s.object_id, s.stats_id) sp
WHERE s.object_id = OBJECT_ID('dbo.Orders');

The result lists the primary key statistics, the index statistics and an auto-created _WA_Sys_00000002_... statistic on Status (the column used in a GROUP BY). The index statistics were built with a full scan (500,000 rows sampled) and the histogram has 89 of a possible 200 steps. A skewed value such as customer 42 (about 5,000 rows) gets its own histogram step, which is why plans for 42 and 7 differ.

Pitfalls and production notes

  • Stale statistics after a large load or delete are a classic cause of bad plans; update them in the load job (UPDATE STATISTICS dbo.Orders WITH FULLSCAN).
  • Index rebuilds update the index statistics with a full scan; reorganise does not.
  • Multi-column correlation is not captured by single-column histograms; create multi-column statistics (CREATE STATISTICS ... (a, b)) or filtered statistics.
  • Table variables have no statistics (a fixed low estimate before SQL Server 2019’s deferred compilation). Prefer temp tables for non-trivial row counts.
  • AUTO_UPDATE_STATISTICS_ASYNC avoids waiting for an update during compile but queries may use stale statistics once.
  • Do not disable auto-create/update to “save overhead”.

Practice questions

  1. Why does an estimate of 1 row for a filter that returns 500,000 often produce a nested loops plan?
  2. When would you choose FULLSCAN over the default sample?
  3. How do you see whether auto-update statistics fired?

References

  • Microsoft Learn, Statistics; Cardinality estimation (SQL Server)

sqlserver-execution-plans · sqlserver-parameter-sniffing · sqlserver-indexes · query-planning