Views, indexes, and query performance
Statistics and cardinality estimates
Inspect statistics objects used to estimate how many rows a query may return.
Lesson 76 of 84
Reading: about 2 min
Lesson + practice: about 15 min
Difficulty: Advanced
On this page4 sections
This lesson uses DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Inspect statistics objects used to estimate how many rows a query may return.
Run the example
SELECT name AS StatisticName, auto_created AS AutoCreated, user_created AS UserCreated
FROM sys.stats
WHERE object_id = OBJECT_ID(N'dbo.Orders')
AND name = N'PK_Orders'
ORDER BY name;Read it step by step
- sys.stats lists statistics associated with Orders.
- The PK index has a statistics object that the optimizer can use for cardinality estimates.
- Metadata flags identify creation source; estimated rows must still be checked in an execution plan.
Check the result
| StatisticName | AutoCreated | UserCreated |
|---|---|---|
| PK_Orders | 0 | 0 |
Filtering to the named PK_Orders statistic avoids depending on optional automatically created statistics. Both creation flags are 0 here because this statistic belongs to an index.
Practice
Inspect the named PK statistic on dbo.Products.
Check your answer and expected result
Reveal answer
SELECT name AS StatisticName, auto_created AS AutoCreated, user_created AS UserCreated
FROM sys.stats
WHERE object_id = OBJECT_ID(N'dbo.Products')
AND name = N'PK_Products'
ORDER BY name;| StatisticName | AutoCreated | UserCreated |
|---|---|---|
| PK_Products | 0 | 0 |
The result identifies the product primary-key statistic without assuming a particular plan.
Your progress
Reveal the check answer or reach the end to complete this lesson.