Подія
31 бер., 23 - 2 квіт., 23
Найбільший навчальний захід SQL, Fabric і Power BI. 31 березня – 2 квітня. Щоб заощадити 400 грн, скористайтеся кодом FABINSIDER.
Реєструйтеся сьогодніЦей браузер більше не підтримується.
Замініть його на Microsoft Edge, щоб користуватися перевагами найновіших функцій, оновлень безпеки та технічної підтримки.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
SQL analytics endpoint in Microsoft Fabric
Warehouse in Microsoft Fabric
SQL database in Microsoft Fabric
Updates query optimization statistics on a table or indexed view. 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.
Updating statistics ensures that queries compile with up-to-date statistics. Updating statistics via any process can cause query plans to recompile automatically. 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.
Примітка
For more information on statistics in Microsoft Fabric, see Statistics in Fabric data warehousing.
Transact-SQL syntax conventions
Syntax for SQL Server and Azure SQL Database.
UPDATE STATISTICS table_or_indexed_view_name
[
{
{ index_or_statistics__name }
| ( { index_or_statistics_name } [ , ...n ] )
}
]
[ WITH
[
FULLSCAN
[ [ , ] PERSIST_SAMPLE_PERCENT = { ON | OFF } ]
| SAMPLE number { PERCENT | ROWS }
[ [ , ] PERSIST_SAMPLE_PERCENT = { ON | OFF } ]
| RESAMPLE
[ ON PARTITIONS ( { <partition_number> | <range> } [ , ...n ] ) ]
| <update_stats_stream_option> [ , ...n ]
]
[ [ , ] [ ALL | COLUMNS | INDEX ]
[ [ , ] NORECOMPUTE ]
[ [ , ] INCREMENTAL = { ON | OFF } ]
[ [ , ] MAXDOP = max_degree_of_parallelism ]
[ [ , ] AUTO_DROP = { ON | OFF } ]
] ;
<update_stats_stream_option> ::=
[ STATS_STREAM = stats_stream ]
[ ROWCOUNT = numeric_constant ]
[ PAGECOUNT = numeric_constant ]
Syntax for Azure Synapse Analytics and Parallel Data Warehouse.
UPDATE STATISTICS [ schema_name . ] table_name
[ ( { statistics_name | index_name } ) ]
[ WITH
{
FULLSCAN
| SAMPLE number PERCENT
| RESAMPLE
}
]
[;]
Syntax for Microsoft Fabric.
UPDATE STATISTICS [ schema_name . ] table_name
[ ( { statistics_name } ) ]
[ WITH
{
FULLSCAN
| SAMPLE number PERCENT
}
]
[;]
Примітка
This syntax is not supported by serverless SQL pool in Azure Synapse Analytics.
The name of the table or indexed view that contains the statistics object.
The name of the index to update statistics on or name of the statistics to update. If index_or_statistics_name or statistics_name isn't specified, the query optimizer updates all statistics for the table or indexed view. This includes statistics created using the CREATE STATISTICS
statement, single-column statistics created when AUTO_CREATE_STATISTICS
is on, and statistics created for indexes.
For more information about AUTO_CREATE_STATISTICS
, see ALTER DATABASE SET Options. To view all indexes for a table or view, you can use sp_helpindex.
Compute statistics by scanning all rows in the table or indexed view. FULLSCAN
and SAMPLE 100 PERCENT
have the same results. FULLSCAN
can't be used with the SAMPLE
option.
Specifies the approximate percentage or number of rows in the table or indexed view for the query optimizer to use when it updates statistics. For PERCENT
, number can be from 0 through 100 and for ROWS
, number can be from 0 to the total number of rows. The actual percentage or number of rows the query optimizer samples might not match the percentage or number specified. For example, the query optimizer scans all rows on a data page.
SAMPLE
is useful for special cases in which the query plan, based on default sampling, isn't optimal. In most situations, it isn't necessary to specify SAMPLE
because the query optimizer uses sampling and determines the statistically significant sample size by default, as required to create high-quality query plans.
Примітка
In SQL Server 2016 (13.x) when using database compatibility level 130, sampling of data to build statistics is done in parallel to improve the performance of statistics collection. The query optimizer will use parallel sample statistics whenever a table size exceeds a certain threshold. Starting with SQL Server 2017 (14.x), regardless of database compatibility level, the behavior was changed back to using a serial scan in order to avoid potential performance issues with excessive LATCH
waits. The rest of the query plan while updating statistics will maintain parallel execution if qualified.
SAMPLE
can't be used with the FULLSCAN
option. When neither SAMPLE
nor FULLSCAN
is specified, the query optimizer uses sampled data and computes the sample size by default.
We recommend against specifying 0 PERCENT
or 0 ROWS
. When 0 PERCENT
or 0 ROWS
is specified, the statistics object is updated but doesn't contain statistics data.
For most workloads, a full scan isn't required, and default sampling is adequate. However, certain workloads that are sensitive to widely varying data distributions might require an increased sample size, or even a full scan. While estimates might become more accurate with a full scan than a sampled scan, complex plans might not substantially benefit.
For more information, see Components and concepts of statistics.
Update each statistic using its most recent sample rate.
Using RESAMPLE
can result in a full-table scan. For example, statistics for indexes use a full-table scan for their sample rate. When none of the sample options (SAMPLE
, FULLSCAN
, RESAMPLE
) are specified, the query optimizer samples the data and computes the sample size by default.
In Warehouse in Microsoft Fabric, RESAMPLE
isn't supported.
Applies to: SQL Server 2016 (13.x) Service Pack 1 CU4, SQL Server 2017 (14.x) Service Pack 1, or SQL Server 2019 (15.x) and later versions, Azure SQL Database, Azure SQL Managed Instance
When ON
, the statistics will retain the set sampling percentage for subsequent updates that don't explicitly specify a sampling percentage. When OFF
, statistics sampling percentage will get reset to default sampling in subsequent updates that don't explicitly specify a sampling percentage. The default is OFF
.
DBCC SHOW_STATISTICS and sys.dm_db_stats_properties expose the persisted sample percent value for the selected statistic.
If AUTO_UPDATE_STATISTICS
is executed, it uses the persisted sampling percentage if available, or use default sampling percentage if not. RESAMPLE
behavior isn't affected by this option.
If the table is truncated, all statistics built on the truncated heap or B-tree (HoBT) will revert to using the default sampling percentage.
Примітка
In SQL Server, when rebuilding an index which previously had statistics updated with PERSIST_SAMPLE_PERCENT
, the persisted sample percent is reset back to default. Starting with SQL Server 2016 (13.x) SP2 CU17, SQL Server 2017 (14.x) CU26, and SQL Server 2019 (15.x) CU10, the persisted sample percent is kept even when rebuilding an index.
Applies to: SQL Server 2014 (12.x) and later versions
Forces the leaf-level statistics covering the partitions specified in the ON PARTITIONS
clause to be recomputed, and then merged to build the global statistics. WITH RESAMPLE
is required because partition statistics built with different sample rates can't be merged together.
Update all existing statistics, statistics created on one or more columns, or statistics created for indexes. If none of the options are specified, the UPDATE STATISTICS
statement updates all statistics on the table or indexed view.
Disable the automatic statistics update option, AUTO_UPDATE_STATISTICS
, for the specified statistics. If this option is specified, the query optimizer completes this statistics update and disables future updates.
To re-enable the AUTO_UPDATE_STATISTICS
option behavior, run UPDATE STATISTICS
again without the NORECOMPUTE
option or run sp_autostats
.
Попередження
Using this option can produce suboptimal query plans. We recommend using this option sparingly, and then only by a qualified system administrator.
For more information about the AUTO_STATISTICS_UPDATE
option, see ALTER DATABASE SET Options.
Applies to: SQL Server 2014 (12.x) and later versions
When ON
, the statistics are recreated as per partition statistics. When OFF
, the statistics tree is dropped and SQL Server re-computes the statistics. The default is OFF
.
If per partition statistics aren't supported an error is generated. Incremental stats aren't supported for following statistics types:
Applies to: SQL Server (Starting with SQL Server 2016 (13.x) SP2 and SQL Server 2017 (14.x) CU3).
Overrides the max degree of parallelism
configuration option for the duration of the statistic operation. For more information, see Configure the max degree of parallelism Server Configuration Option. Use MAXDOP
to limit the number of processors used in a parallel plan execution. The maximum is 64 processors.
max_degree_of_parallelism can be:
Suppresses parallel plan generation.
Restricts the maximum number of processors used in a parallel statistic operation to the specified number or fewer based on the current system workload.
Uses the actual number of processors or fewer based on the current system workload.
Identified for informational purposes only. Not supported. Future compatibility is not guaranteed.
Applies to: SQL Server 2022 (16.x) and later versions
Currently, if statistics are created by a third party tool on a customer database, those statistics objects can block or interfere with schema changes the customer might desire.
(Starting with SQL Server 2022 (16.x))| This feature allows the creation of statistics objects in a mode such that a schema change will not be blocked by the statistics, but instead the statistics will be dropped. In this way, auto drop statistics behave like auto created statistics.
Примітка
Trying to set or unset the Auto_Drop property on auto created statistics might raise errors - auto created statistics always uses auto drop. Some backups, when restored, might 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.
For more information about when to use UPDATE STATISTICS
, see When to update statistics.
MAXDOP
option isn't compatible with STATS_STREAM
, ROWCOUNT
and PAGECOUNT
options.MAXDOP
option is limited by the Resource Governor workload group MAX_DOP
setting, if used.For information about how to update statistics for all user-defined and internal tables in the database, see the stored procedure sp_updatestats. For example, the following command calls sp_updatestats
to update all statistics for the database.
EXECUTE sp_updatestats;
Use solutions such as Adaptive Index Defrag 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.
To determine when statistics were last updated, use the STATS_DATE function.
The following syntax isn't supported by Analytics Platform System (PDW) / Azure Synapse Analytics:
UPDATE STATISTICS t1 (a, b);
UPDATE STATISTICS t1 (a) WITH SAMPLE 10 ROWS;
UPDATE STATISTICS t1 (a) WITH NORECOMPUTE;
UPDATE STATISTICS t1 (a) WITH INCREMENTAL = ON;
UPDATE STATISTICS t1 (a) WITH STATS_STREAM = 0x01;
Requires ALTER
permission on the table or view.
The following example updates all statistics on the SalesOrderDetail
table.
USE AdventureWorks2022;
GO
UPDATE STATISTICS Sales.SalesOrderDetail;
GO
The following example updates the statistics for the AK_SalesOrderDetail_rowguid
index of the SalesOrderDetail
table.
USE AdventureWorks2022;
GO
UPDATE STATISTICS Sales.SalesOrderDetail (AK_SalesOrderDetail_rowguid);
GO
The following example creates and then updates the statistics for the Name
and ProductNumber
columns in the Product
table.
USE AdventureWorks2022;
GO
CREATE STATISTICS Products
ON Production.Product([Name], ProductNumber)
WITH SAMPLE 50 PERCENT;
-- Time passes. The UPDATE STATISTICS statement is then executed.
UPDATE STATISTICS Production.Product (Products)
WITH SAMPLE 50 PERCENT;
The following example updates the Products
statistics in the Product
table, forces a full scan of all rows in the Product
table, and turns off automatic statistics for the Products
statistics.
USE AdventureWorks2022;
GO
UPDATE STATISTICS Production.Product (Products)
WITH FULLSCAN, NORECOMPUTE;
GO
The following example updates the CustomerStats1
statistics on the Customer
table.
UPDATE STATISTICS Customer (CustomerStats1);
The following example updates the CustomerStats1
statistics, based on scanning all of the rows in the Customer
table.
UPDATE STATISTICS Customer (CustomerStats1) WITH FULLSCAN;
The following example updates all statistics on the Customer
table.
UPDATE STATISTICS Customer;
To use auto drop statistics, just add the following to the "WITH" clause of statistics create or update.
UPDATE STATISTICS Customer (CustomerStats1) WITH AUTO_DROP = ON;
Подія
31 бер., 23 - 2 квіт., 23
Найбільший навчальний захід SQL, Fabric і Power BI. 31 березня – 2 квітня. Щоб заощадити 400 грн, скористайтеся кодом FABINSIDER.
Реєструйтеся сьогодні