Skip to content

Commit a2c973f

Browse files
20221013 1506
1 parent f796e1b commit a2c973f

5 files changed

Lines changed: 83 additions & 63 deletions

File tree

‎docs/relational-databases/statistics/statistics.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,34 @@ In [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], [!INCLUDE[ssSDSMIfull]
188188
189189
### AUTO_DROP option
190190

191-
*Applies to**: [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)] and later.
191+
*Applies to**: [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], [!INCLUDE[ssazuremi_md](../../includes/ssazuremi_md.md)], and starting with [!INCLUDE[ssSQL22](../../includes/sssql22-md.md)]
192192

193-
Prior to [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)], if statistics are manually created by a user or third party tool on a user database, those statistics objects can block or interfere with schema changes the customer may desire.
193+
In [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] prior to [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)], if statistics are manually created by a user or third party tool on a user database, those statistics objects can block or interfere with schema changes the customer may desire.
194194

195-
Starting with [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)], the AUTO_DROP option is enabled by default on all new and migrated databases. The AUTO_DROP property allows the creation of statistics objects in a mode such that a subsequent schema change will *not* be blocked by the statistic object, but instead the statistics will be dropped as necessary. In this way, manually-created statistics with AUTO_DROP enabled behave like auto-created statistics.
195+
Starting with [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)], the auto drop option is enabled by default on all new and migrated databases. The AUTO_DROP property allows the creation of statistics objects in a mode such that a subsequent schema change will *not* be blocked by the statistic object, but instead the statistics will be dropped as necessary. In this way, manually-created statistics with auto drop enabled behave like auto-created statistics.
196196

197197
> [!NOTE]
198-
> Trying to set or unset the *Auto_Drop* property on auto-created statistics may raise errors. Auto-created statistics always uses auto drop. Some backups, when restored, may have this property set incorrectly until the next time the statistics object is updated (manually or automatically). However, auto-created statistics always behave like auto drop statistics. When restoring a database to [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)] from a previous version, it is recommended to execute `sp_updatestats` on the database, setting the proper metadata for the statistics AUTO_DROP feature.
198+
> Trying to set or unset the auto drop property on auto-created statistics may raise errors. Auto-created statistics always uses auto drop. Some backups, when restored, may have this property set incorrectly until the next time the statistics object is updated (manually or automatically). However, auto-created statistics always behave like auto drop statistics. When restoring a database to [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)] from a previous version, it is recommended to execute `sp_updatestats` on the database, setting the proper metadata for the statistics auto drop feature.
199199
200200
For example, to manually create a statistics object on the `dbo.DatabaseLog` table:
201201

202202
```sql
203203
CREATE STATISTICS [mystats] ON [dbo].[DatabaseLog]([DatabaseLogID], [PostTime], [DatabaseUser]) WITH AUTO_DROP = ON;
204204
```
205205

206-
For example, to update a statistics object AUTO_DROP setting on the `dbo.DatabaseLog` table:
206+
For example, to update a statistics object auto drop setting on the `dbo.DatabaseLog` table:
207207

208208
```sql
209209
UPDATE STATISTICS [dbo].[DatabaseLog] [mystats] WITH AUTO_DROP = ON;
210210
```
211211

212+
To evaluate the auto drop setting on existing statistics, use the `auto_drop` column in `sys.stats`:
213+
214+
```sql
215+
SELECT object_id, [name], auto_drop
216+
FROM sys.stats;
217+
```
218+
212219
For more information, see [CREATE STATISTICS (Transact-SQL)](../../t-sql/statements/create-statistics-transact-sql.md#auto_drop---on--off-)
213220

214221
### INCREMENTAL

‎docs/relational-databases/system-catalog-views/sys-stats-transact-sql.md‎

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ title: "sys.stats (Transact-SQL)"
33
description: sys.stats (Transact-SQL)
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: "12/18/2017"
6+
ms.date: 10/13/2022
77
ms.prod: sql
8-
ms.prod_service: "database-engine, sql-database, synapse-analytics, pdw"
98
ms.technology: system-objects
109
ms.topic: "reference"
1110
f1_keywords:
@@ -17,60 +16,67 @@ helpviewer_keywords:
1716
- "sys.stats catalog view"
1817
dev_langs:
1918
- "TSQL"
20-
ms.assetid: 42605c80-126f-460a-befb-a0b7482fae6a
2119
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
2220
---
2321
# sys.stats (Transact-SQL)
22+
2423
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
2524

26-
Contains a row for each statistics object that exists for the tables, indexes, and indexed views in the database in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. Every index will have a corresponding statistics row with the same name and ID (**index_id** = **stats_id**), but not every statistics row has a corresponding index.
27-
28-
The catalog view [sys.stats_columns](../../relational-databases/system-catalog-views/sys-stats-columns-transact-sql.md) provides statistics information for each column in the database. For more information about statistics, see [Statistics](../../relational-databases/statistics/statistics.md).
29-
30-
|Column name|Data type|Description|
31-
|-----------------|---------------|-----------------|
32-
|**object_id**|**int**|ID of the object to which these statistics belong.|
33-
|**name**|**sysname**|Name of the statistics. Is unique within the object.|
34-
|**stats_id**|**int**|ID of the statistics. Is unique within the object.<br /><br />If statistics correspond to an index, the *stats_id* value is the same as the *index_id* value in the [sys.indexes](../../relational-databases/system-catalog-views/sys-indexes-transact-sql.md) catalog view.|
35-
|**auto_created**|**bit**|Indicates whether the statistics were automatically created by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].<br /><br /> 0 = Statistics were not automatically created by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].<br /><br /> 1 = Statistics were automatically created by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].|
36-
|**user_created**|**bit**|Indicates whether the statistics were created by a user.<br /><br /> 0 = Statistics were not created by a user.<br /><br /> 1 = Statistics were created by a user.|
37-
|**no_recompute**|**bit**|Indicates whether the statistics were created with the **NORECOMPUTE** option.<br /><br /> 0 = Statistics were not created with the **NORECOMPUTE** option.<br /><br /> 1 = Statistics were created with the **NORECOMPUTE** option.|
38-
|**has_filter**|**bit**|0 = Statistics do not have a filter and are computed on all rows.<br /><br /> 1 = Statistics have a filter and are computed only on rows that satisfy the filter definition.|
39-
|**filter_definition**|**nvarchar(max)**|Expression for the subset of rows included in filtered statistics.<br /><br /> NULL = Non-filtered statistics.|
40-
|**is_temporary**|**bit**|Indicates whether the statistics is temporary. Temporary statistics support [!INCLUDE[ssHADR](../../includes/sshadr-md.md)] secondary databases that are enabled for read-only access.<br /><br /> 0 = The statistics is not temporary.<br /><br /> 1 = The statistics is temporary.<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)])|
41-
|**is_incremental**|**bit**|Indicate whether the statistics are created as incremental statistics.<br /><br /> 0 = The statistics are not incremental.<br /><br /> 1 = The statistics are incremental.<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[ssSQL14](../../includes/sssql14-md.md)])|
42-
|**has_persisted_sample**|**bit**|Indicates whether the statistics were created or updated with the PERSIST_SAMPLE_PERCENT option.<br /><br /> **0** = Statistics are not persisting the sample percentage.<br /><br /> 1 = Statistics were created or updated with the PERSIST_SAMPLE_PERCENT option.<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)])|
43-
|**stats_generation_method**|**int**|Indicates the method by which statistics are created.<br /><br />**0** = Sort based statistics<br /><br />1 = Internal use only<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)])|
44-
|**stats_generation_method_desc**|**varchar(255)**|The text description of the method by which statistics are created.<br /><br />Sort based statistics<br /><br />Internal use only<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)])|
45-
46-
## Permissions
47-
[!INCLUDE[ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../../relational-databases/security/metadata-visibility-configuration.md).
48-
49-
## Examples
50-
The following examples returns all the statistics and statistics columns for the `HumanResources.Employee` table.
51-
52-
```sql
53-
USE AdventureWorks2012;
54-
GO
55-
SELECT s.name AS statistics_name
56-
,c.name AS column_name
57-
,sc.stats_column_id
58-
FROM sys.stats AS s
59-
INNER JOIN sys.stats_columns AS sc
60-
ON s.object_id = sc.object_id AND s.stats_id = sc.stats_id
61-
INNER JOIN sys.columns AS c
62-
ON sc.object_id = c.object_id AND c.column_id = sc.column_id
63-
WHERE s.object_id = OBJECT_ID('HumanResources.Employee');
64-
```
65-
66-
## See Also
67-
[Object Catalog Views &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/object-catalog-views-transact-sql.md)
68-
[Catalog Views &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/catalog-views-transact-sql.md)
69-
[Querying the SQL Server System Catalog FAQ](../../relational-databases/system-catalog-views/querying-the-sql-server-system-catalog-faq.yml)
70-
[Statistics](../../relational-databases/statistics/statistics.md)
71-
[sys.dm_db_stats_properties &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-db-stats-properties-transact-sql.md)
72-
[sys.dm_db_stats_histogram &#40;Transact-SQL&#41;](../../relational-databases/system-dynamic-management-views/sys-dm-db-stats-histogram-transact-sql.md)
73-
[sys.stats_columns &#40;Transact-SQL&#41;](../../relational-databases/system-catalog-views/sys-stats-columns-transact-sql.md)
74-
25+
Contains a row for each statistics object that exists for the tables, indexes, and indexed views in the database in [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)]. Every index will have a corresponding statistics row with the same name and ID (`index_id` = `stats_id`), but not every statistics row has a corresponding index.
26+
27+
The catalog view [sys.stats_columns](../../relational-databases/system-catalog-views/sys-stats-columns-transact-sql.md) provides statistics information for each column in the database. For more information about statistics, see [Statistics](../../relational-databases/statistics/statistics.md).
28+
29+
|Column name|Data type|Description|
30+
|-----------------|---------------|-----------------|
31+
|**object_id**|**int**|ID of the object to which these statistics belong.|
32+
|**name**|**sysname**|Name of the statistics. Is unique within the object.|
33+
|**stats_id**|**int**|ID of the statistics. Is unique within the object.<br /><br />If statistics correspond to an index, the *stats_id* value is the same as the *index_id* value in the [sys.indexes](../../relational-databases/system-catalog-views/sys-indexes-transact-sql.md) catalog view.|
34+
|**auto_created**|**bit**|Indicates whether the statistics were automatically created by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].<br /><br />0 = Statistics were not automatically created by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].<br /><br />1 = Statistics were automatically created by [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)].|
35+
|**user_created**|**bit**|Indicates whether the statistics were created by a user.<br /><br />0 = Statistics were not created by a user.<br /><br />1 = Statistics were created by a user.|
36+
|**no_recompute**|**bit**|Indicates whether the statistics were created with the **NORECOMPUTE** option.<br /><br />0 = Statistics were not created with the **NORECOMPUTE** option.<br /><br />1 = Statistics were created with the **NORECOMPUTE** option.|
37+
|**has_filter**|**bit**|0 = Statistics do not have a filter and are computed on all rows.<br /><br />1 = Statistics have a filter and are computed only on rows that satisfy the filter definition.|
38+
|**filter_definition**|**nvarchar(max)**|Expression for the subset of rows included in filtered statistics.<br /><br />NULL = Non-filtered statistics.|
39+
|**is_temporary**|**bit**|Indicates whether the statistics is temporary. Temporary statistics support [!INCLUDE[ssHADR](../../includes/sshadr-md.md)] secondary databases that are enabled for read-only access.<br /><br />0 = The statistics is not temporary.<br /><br />1 = The statistics is temporary.<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[ssSQL11](../../includes/sssql11-md.md)])|
40+
|**is_incremental**|**bit**|Indicate whether the statistics are created as incremental statistics.<br /><br />0 = The statistics are not incremental.<br /><br />1 = The statistics are incremental.<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[ssSQL14](../../includes/sssql14-md.md)])|
41+
|**has_persisted_sample**|**bit**|Indicates whether the statistics were created or updated with the PERSIST_SAMPLE_PERCENT option.<br /><br />**0** = Statistics are not persisting the sample percentage.<br /><br />1 = Statistics were created or updated with the PERSIST_SAMPLE_PERCENT option.<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)])|
42+
|**stats_generation_method**|**int**|Indicates the method by which statistics are created.<br /><br />**0** = Sort based statistics<br /><br />1 = Internal use only<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)])|
43+
|**stats_generation_method_desc**|**varchar(255)**|The text description of the method by which statistics are created.<br /><br />Sort based statistics<br /><br />Internal use only<br /><br />**Applies to**: [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (Starting with [!INCLUDE[sssql19-md](../../includes/sssql19-md.md)])|
44+
|**auto_drop**|**bit**|Indicates whether or not the auto drop feature is enabled for this statistics object. The AUTO_DROP property allows the creation of statistics objects in a mode such that a subsequent schema change will not be blocked by the statistic object, but instead the statistics will be dropped as necessary. In this way, manually-created statistics with AUTO_DROP enabled behave like auto-created statistics. For more information, see [AUTO_DROP option](../statistics/statistics.md#auto_drop-option).<br /><br />**Applies to**: [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], [!INCLUDE[ssazuremi_md](../../includes/ssazuremi_md.md)], and starting with [!INCLUDE[ssSQL22](../../includes/sssql22-md.md)]. |
45+
46+
## Permissions
47+
48+
[!INCLUDE[ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../../relational-databases/security/metadata-visibility-configuration.md).
49+
50+
## Examples
51+
52+
The following examples returns all the statistics and statistics columns for the `HumanResources.Employee` table.
53+
54+
```sql
55+
USE AdventureWorks2019;
56+
GO
57+
SELECT s.name AS statistics_name
58+
,c.name AS column_name
59+
,sc.stats_column_id
60+
FROM sys.stats AS s
61+
INNER JOIN sys.stats_columns AS sc
62+
ON s.object_id = sc.object_id AND s.stats_id = sc.stats_id
63+
INNER JOIN sys.columns AS c
64+
ON sc.object_id = c.object_id AND c.column_id = sc.column_id
65+
WHERE s.object_id = OBJECT_ID('HumanResources.Employee');
66+
```
67+
68+
## See also
69+
70+
- [Object Catalog Views (Transact-SQL)](../../relational-databases/system-catalog-views/object-catalog-views-transact-sql.md)
71+
- [Catalog Views (Transact-SQL)](../../relational-databases/system-catalog-views/catalog-views-transact-sql.md)
72+
- [Querying the SQL Server System Catalog FAQ](../../relational-databases/system-catalog-views/querying-the-sql-server-system-catalog-faq.yml)
73+
- [sys.dm_db_stats_properties (Transact-SQL)](../../relational-databases/system-dynamic-management-views/sys-dm-db-stats-properties-transact-sql.md)
74+
- [sys.dm_db_stats_histogram (Transact-SQL)](../../relational-databases/system-dynamic-management-views/sys-dm-db-stats-histogram-transact-sql.md)
75+
- [sys.stats_columns (Transact-SQL)](../../relational-databases/system-catalog-views/sys-stats-columns-transact-sql.md)
76+
77+
## Next steps
7578

76-
79+
- [Statistics](../../relational-databases/statistics/statistics.md)
80+
- [sp_updatestats (Transact-SQL)](../system-stored-procedures/sp-updatestats-transact-sql.md)
81+
- [CREATE STATISTICS (Transact-SQL)](../../t-sql/statements/create-statistics-transact-sql.md)
82+
- [Create statistics in SSMS or T-SQL](../statistics/create-statistics.md)

‎docs/relational-databases/system-stored-procedures/sp-updatestats-transact-sql.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ For disk-based tables, `sp_updatestats` updates statistics based on the **modifi
5555

5656
For databases with a compatibility level below 90, executing `sp_updatestats` does not preserve the latest NORECOMPUTE setting for specific statistics. For databases with a compatibility level of 90 or higher, `sp_updatestats` does preserve the latest NORECOMPUTE option for specific statistics. For more information about disabling and re-enabling statistics updates, see [Statistics](../../relational-databases/statistics/statistics.md).
5757

58-
When restoring a database to [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)] from a previous version, it is recommended to execute `sp_updatestats` on the database. This is related to setting proper metadata for the [statistics AUTO_DROP feature](../statistics/statistics.md#auto_drop-option) introduced in [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)].
58+
When restoring a database to [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)] from a previous version, it is recommended to execute `sp_updatestats` on the database. This is related to setting proper metadata for the [statistics auto drop feature](../statistics/statistics.md#auto_drop-option) introduced in [!INCLUDE[sssql22-md](../../includes/sssql22-md.md)]. The auto drop feature is available in [!INCLUDE[ssSDSfull](../../includes/sssdsfull-md.md)], [!INCLUDE[ssazuremi_md](../../includes/ssazuremi_md.md)], and starting with [!INCLUDE[ssSQL22](../../includes/sssql22-md.md)].
5959

6060
## Permissions
6161

0 commit comments

Comments
 (0)