CREATE STATISTICS (Transact-SQL)
Creates query optimization statistics, including filtered statistics, on one or more columns of a table or indexed view. For most queries, the query optimizer already generates the necessary statistics for a high-quality query plan; in a few cases, you need to create additional statistics with CREATE STATISTICS or modify the query design to improve query performance.
Filtered statistics can improve query performance for queries that select from well-defined subsets of data. Filtered statistics use a filter predicate in the WHERE clause to select the subset of data that is included in the statistics.
For more information about statistics, including when to use CREATE STATISTICS, see Using Statistics to Improve Query Performance.
CREATE STATISTICS statistics_name
ON { table_or_indexed_view_name } (column [ ,...n ] )
[ WHERE <filter_predicate> ]
[ WITH
[ [ FULLSCAN
| SAMPLE number { PERCENT | ROWS }
| STATS_STREAM = stats_stream ] [ , ] ]
[ NORECOMPUTE ]
] ;
<filter_predicate> ::=
<conjunct> [AND <conjunct>]
<conjunct> ::=
<disjunct> | <comparison>
<disjunct> ::=
column_name IN (constant ,…)
<comparison> ::=
column_name <comparison_op> constant
<comparison_op> ::=
IS | IS NOT | = | <> | != | > | >= | !> | < | <= | !<
The following table gives maximum numbers for categories relating to statistics.
Category for statistics | Maximum |
|---|---|
Columns per statistics object | 32 |
Statistics created on indexes | 1 per index and 1000 per table |
Statistics created on columns | 30,000 per table |
When to Use CREATE STATISTICS
For more information about when to use CREATE STATISTICS, see Using Statistics to Improve Query Performance.
Referencing Dependencies for Filtered Statistics
The sys.sql_expression_dependencies catalog view tracks each column in the filtered statistics predicate as a referencing dependency. Consider the operations that you perform on table columns before creating filtered statistics because you cannot drop, rename, or alter the definition of a table column that is defined in a filtered statistics predicate.
A. Using CREATE STATISTICS with SAMPLE number PERCENT
The following example creates the ContactMail1 statistics, using a random sample of 5 percent of the ContactID and EmailAddress columns of the Contact table of the AdventureWorks database.
USE AdventureWorks;
GO
CREATE STATISTICS ContactMail1
ON Person.Contact (ContactID, EmailAddress)
WITH SAMPLE 5 PERCENT;
B. Using CREATE STATISTICS with FULLSCAN and NORECOMPUTE
The following example creates the ContactMail2 statistics for all rows in the ContactID and EmailAddress columns of the Contact table and disables automatic recomputing of statistics.
CREATE STATISTICS NamePurchase
ON AdventureWorks.Person.Contact (ContactID, EmailAddress)
WITH FULLSCAN, NORECOMPUTE;
C. Using CREATE STATISTICS to create filtered statistics
The following example creates the filtered statistics ContactPromotion1. The Database Engine samples 50 percent of the data and then selects the rows with EmailPromotion equal to 2.
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.stats
WHERE name = N'ContactPromotion1'
AND object_id = OBJECT_ID(N'Person.Contact'))
DROP STATISTICS Person.Contact.ContactPromotion1
GO
CREATE STATISTICS ContactPromotion1
ON Person.Contact (ContactID, EmailAddress, EmailPromotion)
WHERE EmailPromotion = 2
WITH SAMPLE 50 PERCENT;
GO
Updated content |
|---|
Revisions throughout the document to improve correctness. |
Topic refers to new statistics content in the topic Using Statistics to Improve Query Performance. |
