Skip to content

Commit 9e4f062

Browse files
committed
Refresh article
Fix minor typos as well
1 parent 0ee5cdd commit 9e4f062

1 file changed

Lines changed: 107 additions & 161 deletions

File tree

Lines changed: 107 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,124 @@
11
---
22
title: Create Statistics
33
description: Learn how to create query optimization statistics on columns of a table or indexed view in SQL Server by using SQL Server Management Studio or Transact-SQL.
4+
author: WilliamDAssafMSFT
5+
ms.author: wiassaf
6+
ms.reviewer: katsmith, randolphwest
7+
ms.date: 07/22/2022
48
ms.prod: sql
59
ms.technology: performance
610
ms.topic: conceptual
7-
f1_keywords:
11+
ms.custom: event-tier1-build-2022
12+
f1_keywords:
813
- "sql13.swb.stat.properties.f1"
914
- "sql13.swb.statistics.filter.f1"
1015
- "sql13.swb.stat.columns.f1"
11-
- "sql13.swb.statistics.propertis.f1"
12-
helpviewer_keywords:
16+
- "sql13.swb.statistics.properties.f1"
17+
helpviewer_keywords:
1318
- "creating statistics"
1419
- "statistics [SQL Server], creating"
15-
author: WilliamDAssafMSFT
16-
ms.author: wiassaf
17-
ms.reviewer: "katsmith"
18-
ms.custom:
19-
- event-tier1-build-2022
20-
ms.date: "03/14/2017"
21-
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
20+
monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current"
2221
---
23-
# Create Statistics
22+
# Create statistics
2423

2524
[!INCLUDE [sqlserver2022-asdb-asmi](../../includes/applies-to-version/sqlserver2022-asdb-asmi.md)]
2625

27-
You can create query optimization statistics on one or more columns of a table or indexed view in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. 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.
28-
29-
**In This Topic**
30-
31-
- **Before you begin:**
32-
33-
[Limitations and Restrictions](#Restrictions)
34-
35-
[Security](#Security)
36-
37-
- **To create statistics, using:**
38-
39-
[SQL Server Management Studio](#SSMSProcedure)
40-
41-
[Transact-SQL](#TsqlProcedure)
42-
43-
## <a name="BeforeYouBegin"></a> Before You Begin
44-
45-
### <a name="Restrictions"></a> Limitations and Restrictions
46-
47-
- Before creating statistics with the CREATE STATISTICS statement, verify that the AUTO_CREATE_STATISTICS option is set at the database level. This will ensure that the query optimizer continues to routinely create single-column statistics for query predicate columns.
48-
49-
- You can list up to 32 columns per statistics object.
50-
51-
- You cannot drop, rename, or alter the definition of a table column that is defined in a filtered statistics predicate.
52-
53-
### <a name="Security"></a> Security
54-
55-
#### <a name="Permissions"></a> Permissions
56-
57-
Requires that the user be the table or indexed view owner, or a member of one of the following roles: **sysadmin** fixed server role, **db_owner** fixed database role, or the **db_ddladmin** fixed database role.
58-
59-
### <a name="SSMSProcedure"></a> Using SQL Server Management Studio
60-
61-
#### Create statistics using SQL Server Management Studio
62-
63-
1. In **Object Explorer**, Select the plus sign to expand the database in which you want to create a new statistic.
64-
65-
2. Select the plus sign to expand the **Tables** folder.
66-
67-
3. Select the plus sign to expand the table in which you want to create a new statistic.
68-
69-
4. Right-select the **Statistics** folder and select **New Statistics...**.
70-
71-
The following properties show on the **General** page in the **New Statistics on Table**_table\_name_ dialog box.
72-
73-
**Table Name**
74-
Displays the name of the table described by the statistics.
75-
76-
**Statistics Name**
77-
Displays the name of the database object where the statistics are stored.
78-
79-
**Statistics Columns**
80-
This grid shows the columns described by this set of statistics. All values in the grid are read-only.
81-
82-
**Name**
83-
Displays the name of the column described by the statistics. This can be a single column or a combination of columns in a single table.
84-
85-
**Data Type**
86-
Indicates the data type of the columns described by the statistics.
87-
88-
**Size**
89-
Displays the size of the data type for each column.
90-
91-
**Identity**
92-
Indicates an identity column when it is checked.
93-
94-
**Allow Nulls**
95-
Indicates whether the column accepts NULL values.
96-
97-
**Add**
98-
Add additional columns from the table to the statistics grid.
99-
100-
**Remove**
101-
Remove the selected column from the statistics grid.
102-
103-
**Move Up**
104-
Move the selected column to an earlier location in the statistics grid. The location in the grid can substantially impact the usefulness of the statistics.
105-
106-
**Move Down**
107-
Move the selected column to a later location in the statistics grid.
108-
109-
**Statistics for these columns were last updated:**
110-
Indicates how old the statistics are. Statistics are more valuable when they are current. Update statistics after large changes to the data or after adding atypical data. Statistics for tables that have a consistent distribution of data need to be updated less frequently.
111-
112-
**Update statistics for these columns**
113-
Check to update the statistics when the dialog box is closed.
114-
115-
The following property shows on the **Filter** page in the **New Statistics on Table**_table\_name_ dialog box.
116-
117-
**Filter Expression**
118-
Defines which data rows to include in the filtered statistics. For example, `Production.ProductSubcategoryID IN ( 1,2,3 )`
119-
120-
5. In the **New Statistics on Table**_table\_name_ dialog box, on the **General** page, Select **Add**.
121-
122-
The following properties show in the **Select Columns** dialog box. This information is read-only.
123-
124-
**Name**
125-
Displays the name of the column described by the statistics. This can be a single column or a combination of columns in a single table.
126-
127-
**Data Type**
128-
Indicates the data type of the columns described by the statistics.
129-
130-
**Size**
131-
Displays the size of the data type for each column.
132-
133-
**Identity**
134-
Indicates an identity column when checked.
135-
136-
**Allow NULLs**
137-
Indicates whether the column accepts NULL values.
138-
139-
6. In the **Select Columns** dialog box, select the check box or check boxes of each column for which you want to create a statistic and then Select **OK**.
140-
141-
7. In the **New Statistics on Table**_table\_name_ dialog box, Select **OK**.
142-
143-
## <a name="TsqlProcedure"></a> Using Transact-SQL
144-
145-
### Create statistics using Transact-SQL
146-
147-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
148-
149-
2. On the Standard bar, Select **New Query**.
150-
151-
3. Copy and paste the following example into the query window and Select **Execute**.
152-
153-
```sql
154-
USE AdventureWorks2012;
155-
GO
156-
-- Create new statistic object called ContactMail1
157-
-- on the BusinessEntityID and EmailPromotion columns in the Person.Person table.
158-
159-
CREATE STATISTICS ContactMail1
160-
ON Person.Person (BusinessEntityID, EmailPromotion);
161-
GO
162-
```
163-
164-
4. The statistic created above potentially improves the results for the following query.
165-
166-
```sql
167-
USE AdventureWorks2012;
168-
GO
169-
SELECT LastName, FirstName
170-
FROM Person.Person
171-
WHERE EmailPromotion = 2
172-
ORDER BY LastName, FirstName;
173-
GO
174-
```
175-
26+
You can create query optimization statistics on one or more columns of a table or indexed view in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. 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.
27+
28+
## <a id="Restrictions"></a> Limitations and restrictions
29+
30+
Before creating statistics with the CREATE STATISTICS statement, verify that the AUTO_CREATE_STATISTICS option is set at the database level. This will ensure that the query optimizer continues to routinely create single-column statistics for query predicate columns.
31+
32+
You can list up to 32 columns per statistics object.
33+
34+
You cannot drop, rename, or alter the definition of a table column that is defined in a filtered statistics predicate.
35+
36+
## Permissions
37+
38+
Requires that the user be the table or indexed view owner, or a member of one of the following roles: **sysadmin** fixed server role, **db_owner** fixed database role, or the **db_ddladmin** fixed database role.
39+
40+
### Use SQL Server Management Studio
41+
42+
1. In **Object Explorer**, Select the plus sign to expand the database in which you want to create a new statistic.
43+
44+
1. Select the plus sign to expand the **Tables** folder.
45+
46+
1. Select the plus sign to expand the table in which you want to create a new statistic.
47+
48+
1. Right-select the **Statistics** folder and select **New Statistics...**.
49+
50+
The following properties show on the **General** page in the **New Statistics on Table** *table_name* dialog box.
51+
52+
|Property|Description|
53+
| --- | --- |
54+
|**Table Name**|Displays the name of the table described by the statistics.|
55+
|**Statistics Name**|Displays the name of the database object where the statistics are stored.|
56+
|**Statistics Columns**|This grid shows the columns described by this set of statistics. All values in the grid are read-only.|
57+
|**Name**|Displays the name of the column described by the statistics. This can be a single column or a combination of columns in a single table.|
58+
|**Data Type**|Indicates the data type of the columns described by the statistics.|
59+
|**Size**|Displays the size of the data type for each column.|
60+
|**Identity**|Indicates an identity column when it is checked.|
61+
|**Allow NULLs**|Indicates whether the column accepts NULL values.|
62+
|**Add**|Add additional columns from the table to the statistics grid.|
63+
|**Remove**|Remove the selected column from the statistics grid.|
64+
|**Move Up**|Move the selected column to an earlier location in the statistics grid. The location in the grid can substantially impact the usefulness of the statistics.|
65+
|**Move Down**|Move the selected column to a later location in the statistics grid.|
66+
|**Statistics for these columns were last updated**|Indicates how old the statistics are. Statistics are more valuable when they are current. Update statistics after large changes to the data or after adding atypical data. Statistics for tables that have a consistent distribution of data need to be updated less frequently.|
67+
|**Update statistics for these columns**|Check to update the statistics when the dialog box is closed.|
68+
69+
The following property shows on the **Filter** page in the **New Statistics on Table** *table_name* dialog box.
70+
71+
|Property|Description|
72+
| --- | --- |
73+
|**Filter Expression**|Defines which data rows to include in the filtered statistics. For example, `Production.ProductSubcategoryID IN ( 1, 2, 3 )`|
74+
75+
1. In the **New Statistics on Table** *table_name* dialog box, on the **General** page, Select **Add**.
76+
77+
The following properties show in the **Select Columns** dialog box. This information is read-only.
78+
79+
|Property|Description|
80+
| --- | --- |
81+
|**Name**|Displays the name of the column described by the statistics. This can be a single column or a combination of columns in a single table.|
82+
|**Data Type**|Indicates the data type of the columns described by the statistics.|
83+
|**Size**|Displays the size of the data type for each column.|
84+
|**Identity**|Indicates an identity column when checked.|
85+
|**Allow NULLs**|Indicates whether the column accepts NULL values.|
86+
87+
1. In the **Select Columns** dialog box, select the check box or check boxes of each column for which you want to create a statistic and then Select **OK**.
88+
89+
1. In the **New Statistics on Table** *table_name* dialog box, select **OK**.
90+
91+
## Use Transact-SQL
92+
93+
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
94+
95+
1. On the Standard bar, Select **New Query**.
96+
97+
1. Copy and paste the following example into the query window and select **Execute**.
98+
99+
```sql
100+
USE AdventureWorks2012;
101+
GO
102+
-- Create new statistic object called ContactMail1
103+
-- on the BusinessEntityID and EmailPromotion columns in the Person.Person table.
104+
105+
CREATE STATISTICS ContactMail1
106+
ON Person.Person (BusinessEntityID, EmailPromotion);
107+
GO
108+
```
109+
110+
1. The statistic created above potentially improves the results for the following query.
111+
112+
```sql
113+
USE AdventureWorks2012;
114+
GO
115+
SELECT LastName, FirstName
116+
FROM Person.Person
117+
WHERE EmailPromotion = 2
118+
ORDER BY LastName, FirstName;
119+
GO
120+
```
121+
176122
## Next steps
177123

178-
For more information, see [CREATE STATISTICS (Transact-SQL)](../../t-sql/statements/create-statistics-transact-sql.md).
124+
- [CREATE STATISTICS (Transact-SQL)](../../t-sql/statements/create-statistics-transact-sql.md)

0 commit comments

Comments
 (0)