Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit cfca4d8

Browse files
authored
Merge pull request #30249 from rwestMSFT/rw-0401-fix-9742-9743
Refresh UPDATE STATISTICS article and include PRs 9742 and 9743
2 parents 6fb23de + 6398645 commit cfca4d8

2 files changed

Lines changed: 89 additions & 105 deletions

File tree

Lines changed: 84 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,102 @@
11
---
2-
title: Update Statistics
2+
title: Update statistics
33
description: Learn how to update query optimization statistics on a table or indexed view in SQL Server by using SQL Server Management Studio or Transact-SQL.
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.reviewer: derekw
7-
ms.date: "03/14/2017"
6+
ms.reviewer: derekw, randolphwest
7+
ms.date: 04/01/2024
88
ms.service: sql
99
ms.subservice: performance
1010
ms.topic: conceptual
1111
helpviewer_keywords:
1212
- "updating statistics"
1313
- "statistics [SQL Server], updating"
14-
monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
14+
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
1515
---
1616

17-
# Update Statistics
18-
19-
[!INCLUDE[SQL Server Azure SQL Database Synapse Analytics PDW](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
20-
21-
You can update query optimization statistics on 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)]. By default, the query optimizer already updates statistics as necessary to improve the query plan; in some cases you can improve query performance by using UPDATE STATISTICS or the stored procedure `sp_updatestats` to update statistics more frequently than the default updates.
22-
23-
Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently because there is a performance tradeoff between improving query plans and the time it takes to recompile queries. The specific tradeoffs depend on your application. UPDATE STATISTICS can use tempdb to sort the sample of rows for building statistics.
24-
25-
**In This Topic**
26-
27-
- **Before you begin:**
28-
29-
[Security](#Security)
30-
31-
- **To update a statistics object, using:**
32-
33-
[SQL Server Management Studio](#SSMSProcedure)
34-
35-
[Transact-SQL](#TsqlProcedure)
36-
37-
## <a name="BeforeYouBegin"></a> Before You Begin
38-
39-
### <a name="Security"></a> Security
40-
41-
#### <a name="Permissions"></a> Permissions
42-
43-
If using UPDATE STATISTICS or making changes through [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)], requires ALTER permission on the table or view. If using `sp_updatestats`, requires membership in the **sysadmin** fixed server role, or ownership of the database (**dbo**).
44-
45-
## <a name="SSMSProcedure"></a> Using SQL Server Management Studio
46-
47-
#### To update a statistics object
48-
49-
1. In **Object Explorer**, click the plus sign to expand the database in which you want to update the statistic.
50-
51-
2. Click the plus sign to expand the **Tables** folder.
52-
53-
3. Click the plus sign to expand the table in which you want to update the statistic.
54-
55-
4. Click the plus sign to expand the **Statistics** folder.
56-
57-
5. Right-click the statistics object you wish to update and select **Properties**.
58-
59-
6. In the **Statistics Properties -**_statistics\_name_ dialog box, select the **Update statistics for these columns** check box and then click **OK**.
60-
61-
## <a name="TsqlProcedure"></a> Using Transact-SQL
62-
63-
### To update a specific statistics object
64-
65-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
66-
67-
2. On the Standard bar, click **New Query**.
68-
69-
3. Copy and paste the following example into the query window and click **Execute**.
70-
71-
```sql
72-
USE AdventureWorks2022;
73-
GO
74-
-- The following example updates the statistics for the AK_SalesOrderDetail_rowguid index of the SalesOrderDetail table.
75-
UPDATE STATISTICS Sales.SalesOrderDetail AK_SalesOrderDetail_rowguid;
76-
GO
77-
```
78-
79-
### To update all statistics in a table
80-
81-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
82-
83-
2. On the Standard bar, click **New Query**.
84-
85-
3. Copy and paste the following example into the query window and click **Execute**.
86-
87-
```sql
88-
USE AdventureWorks2022;
89-
GO
17+
# Update statistics
18+
19+
[!INCLUDE [SQL Server Azure SQL Database Synapse Analytics PDW](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
20+
21+
You can update query optimization statistics on 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)]. By default, the query optimizer already updates statistics as necessary to improve the query plan; in some cases you can improve query performance by using `UPDATE STATISTICS` or the stored procedure `sp_updatestats` to update statistics more frequently than the default updates.
22+
23+
Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently, because there's a performance tradeoff between improving query plans and the time it takes to recompile queries. The specific tradeoffs depend on your application. `UPDATE STATISTICS` can use `tempdb` to sort the sample of rows for building statistics.
24+
25+
## Permissions
26+
27+
If using `UPDATE STATISTICS` or making changes through [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)], requires ALTER permission on the table or view. If using `sp_updatestats`, requires membership in the **sysadmin** fixed server role, or ownership of the database (**dbo**).
28+
29+
## <a id="SSMSProcedure"></a> Use SQL Server Management Studio
30+
31+
#### Update a statistics object
32+
33+
1. In **Object Explorer**, select the plus sign to expand the database in which you want to update the statistic.
34+
35+
1. Select the plus sign to expand the **Tables** folder.
36+
37+
1. Select the plus sign to expand the table in which you want to update the statistic.
38+
39+
1. Select the plus sign to expand the **Statistics** folder.
40+
41+
1. Right-click the statistics object you wish to update and select **Properties**.
42+
43+
1. In the **Statistics Properties -**_statistics\_name_ dialog box, select the **Update statistics for these columns** check box and then select **OK**.
44+
45+
## <a id="TsqlProcedure"></a> Use Transact-SQL
46+
47+
### Update a specific statistics object
48+
49+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
50+
51+
1. On the Standard bar, select **New Query**.
52+
53+
1. Copy and paste the following example into the query window and select **Execute**.
54+
55+
```sql
56+
USE AdventureWorks2022;
57+
GO
58+
-- The following example updates the statistics for the AK_SalesOrderDetail_rowguid index of the SalesOrderDetail table.
59+
UPDATE STATISTICS Sales.SalesOrderDetail AK_SalesOrderDetail_rowguid;
60+
GO
61+
```
62+
63+
### Update all statistics in a table
64+
65+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
66+
67+
1. On the Standard bar, select **New Query**.
68+
69+
1. Copy and paste the following example into the query window and select **Execute**.
70+
71+
```sql
72+
USE AdventureWorks2022;
73+
GO
9074
-- The following example updates the statistics for all indexes on the SalesOrderDetail table.
91-
UPDATE STATISTICS Sales.SalesOrderDetail;
92-
GO
93-
```
94-
95-
For more information, see [UPDATE STATISTICS &#40;Transact-SQL&#41;](../../t-sql/statements/update-statistics-transact-sql.md).
96-
97-
### To update all statistics in a database
98-
99-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
100-
101-
2. On the Standard bar, click **New Query**.
102-
103-
3. Copy and paste the following example into the query window and click **Execute**.
104-
105-
```sql
106-
USE AdventureWorks2022;
107-
GO
108-
-- The following example updates the statistics for all tables in the database.
109-
EXEC sp_updatestats;
110-
```
75+
UPDATE STATISTICS Sales.SalesOrderDetail;
76+
GO
77+
```
78+
79+
For more information, see [UPDATE STATISTICS](../../t-sql/statements/update-statistics-transact-sql.md).
80+
81+
### Update all statistics in a database
82+
83+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
84+
85+
1. On the Standard bar, select **New Query**.
86+
87+
1. Copy and paste the following example into the query window and select **Execute**.
88+
89+
```sql
90+
USE AdventureWorks2022;
91+
GO
92+
-- The following example updates the statistics for all tables in the database.
93+
EXEC sp_updatestats;
94+
```
11195

11296
### Automatic index and statistics management
11397

114-
Leverage solutions such as [Adaptive Index Defrag](https://github.com/Microsoft/tigertoolbox/tree/master/AdaptiveIndexDefrag) to automatically manage index defragmentation and statistics updates for one or more databases. This procedure automatically chooses whether to rebuild or reorganize an index according to its fragmentation level, amongst other parameters, and update statistics with a linear threshold.
98+
Use solutions such as [Adaptive Index Defrag](https://github.com/Microsoft/tigertoolbox/tree/master/AdaptiveIndexDefrag) to automatically manage index defragmentation and statistics updates for one or more databases. This procedure automatically chooses whether to rebuild or reorganize an index according to its fragmentation level, among other parameters, and update statistics with a linear threshold.
11599

116-
## Next steps
100+
## Related content
117101

118-
For more information, see [UPDATE STATISTICS (Transact-SQL)](../../relational-databases/system-stored-procedures/sp-updatestats-transact-sql.md).
102+
- [UPDATE STATISTICS (Transact-SQL)](../../t-sql/statements/update-statistics-transact-sql.md)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Runs UPDATE STATISTICS against all user-defined and internal table
44
author: markingmyname
55
ms.author: maghan
66
ms.reviewer: wiassaf, randolphwest
7-
ms.date: 11/02/2023
7+
ms.date: 04/01/2024
88
ms.service: sql
99
ms.subservice: system-objects
1010
ms.topic: "reference"
@@ -41,19 +41,19 @@ sp_updatestats [ [ @resample = ] 'resample' ]
4141

4242
#### [ @resample = ] 'resample'
4343

44-
Specifies that `sp_updatestats` uses the RESAMPLE option of the [UPDATE STATISTICS](../../t-sql/statements/update-statistics-transact-sql.md) statement. If `resample` isn't specified, `sp_updatestats` updates statistics by using the default sampling. The `resample` argument is **varchar(8)** with a default value of `NO`.
44+
Specifies that `sp_updatestats` uses the `RESAMPLE` option of the [UPDATE STATISTICS](../../t-sql/statements/update-statistics-transact-sql.md) statement. If `resample` isn't specified, `sp_updatestats` updates statistics by using the default sampling. The `resample` argument is **varchar(8)** with a default value of `NO`.
4545

4646
## Remarks
4747

48-
`sp_updatestats` executes `UPDATE STATISTICS`, by specifying the `ALL` keyword, on all user-defined and internal tables in the database. `sp_updatestats` displays messages that indicate its progress. When the update is completed, it reports that statistics have been updated for all tables.
48+
`sp_updatestats` executes `UPDATE STATISTICS`, by specifying the `ALL` keyword, on all user-defined and internal tables in the database. `sp_updatestats` displays messages that indicate its progress. When the update is completed, it reports that statistics are updated for all tables.
4949

5050
`sp_updatestats` updates statistics on disabled nonclustered indexes and doesn't update statistics on disabled clustered indexes.
5151

52-
For disk-based tables, `sp_updatestats` updates statistics based on the `modification_counter` information in the [sys.dm_db_stats_properties](../system-dynamic-management-views/sys-dm-db-stats-properties-transact-sql.md) catalog view, updating statistics where at least one row has been modified. Statistics on memory-optimized tables are always updated when executing `sp_updatestats`. Therefore don't execute `sp_updatestats` more than necessary.
52+
For disk-based tables, `sp_updatestats` updates statistics based on the `modification_counter` information in the [sys.dm_db_stats_properties](../system-dynamic-management-views/sys-dm-db-stats-properties-transact-sql.md) catalog view, updating statistics where at least one row is modified. Statistics on memory-optimized tables are always updated when executing `sp_updatestats`. Therefore don't execute `sp_updatestats` more than necessary.
5353

5454
`sp_updatestats` can trigger a recompile of stored procedures or other compiled code. However, `sp_updatestats` might not cause a recompile, if only one query plan is possible for the tables referenced and the indexes on them. A recompilation would be unnecessary in these cases even if statistics are updated.
5555

56-
For databases with a compatibility level below 90, executing `sp_updatestats` doesn't 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](../statistics/statistics.md).
56+
`sp_updatestats` preserves the latest `NORECOMPUTE` option for specific statistics. For more information about disabling and re-enabling statistics updates, see [Statistics](../statistics/statistics.md).
5757

5858
When restoring a database to [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] from a previous version, you should 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 [ssazure-sqldb](../../includes/ssazure-sqldb.md)], [!INCLUDE [ssazuremi-md](../../includes/ssazuremi-md.md)], and starting with [!INCLUDE [ssSQL22](../../includes/sssql22-md.md)].
5959

0 commit comments

Comments
 (0)