Updates information about the distribution of key values in specified indexes or for specified columns, for all columns in an index or for all columns in a table.
update statistics table_name [ [index_name] | [( column_list ) ] ] [using step values] [with consumers = consumers ]
update index statistics table_name [index_name] [using step values] [with consumers = consumers ]
When used with update statistics, table_name is the name of the table with which the index is associated. table_name is required, since Transact-SQL does not require index names to be unique in a database.
is the name of the index to be updated. If an index name is not specified, the distribution statistics for all the indexes in the specified table are updated.
is a comma-separated list of columns.
specifies the number of histogram steps. The default value is 20, for columns where no statistics exist. If you need to change the default for the this, use sp_configure. If statistics for a column already exist in sysstatistics, the default value is the current number of steps.
specifies the number of consumer processes to be used for a sort when column_list is provided and parallel query processing is enabled.
specifies that statistics for all columns in an index are to be updated.
Generates statistics for the price column of the titles table:
update statistics titles (price) using 40 values
Generates statistics for all columns in all indexes of the authors table:
update index statistics authors
Generates statistics for all columns in the au_names_ix index of the authors table:
update index statistics authors au_names_ix
Adaptive Server keeps statistics about the distribution of the key values in each index, and uses these statistics in its decisions about which index(es) to use in query processing.
When you create a nonclustered index on a table that contains data, update statistics is automatically run for the new index. When you create a clustered index on a table that contains data, update statistics is automatically run for all indexes.
The optimization of your queries depends on the accuracy of the statistics. If there is significant change in the key values in your index, you should rerun update statistics on that index or column. Use the update statistics command if a great deal of data in an indexed column has been added, changed, or removed (that is, if you suspect that the distribution of key values has changed).
update statistics, when used with a table name and an index name, updates statistics for the leading column of an index. If update statistics is used with just a table name, it updates statistics for the leading columns of all indexes on the table.
update index statistics, when used with a table name and an index name, updates statistics for all columns in the specified index. If update index statistics is used with just a table name, it updates statistics for all columns in all indexes of the table.
Specifying the name of an unindexed column or the nonleading column of an index generates statistics for that column without creating an index.
Specifying more than one column in a column list generates or updates a histogram for the first column, and density statistics for all prefix subsets of the list of columns.
If you use update statistics to generate statistics for a column or list of columns, update statistics must scan the table and perform a sort.
The with consumers clause is designed for use on partitioned tables on RAID devices, which appear to Adaptive Server as a single I/O device, but which are capable of producing the high throughput required for parallel sorting. For more information, see Chapter 24, “Parallel Sorting,” in the Performance and Tuning Guide.
Table 1-40 shows the types of scans performed during update statistics, the types of locks acquired, and when sorts are needed.
update statistics specifying |
Scans and sorts performed |
Locking |
|
---|---|---|---|
Table name |
|||
Allpages-locked table |
Table scan, plus a leaf-level scan of each nonclustered index |
Level 1; shared intent table lock, shared lock on current page |
|
Data-only-locked table |
Table scan, plus a leaf-level scan of each nonclustered index and the clustered index, if one exists |
Level 0; dirty reads |
|
Table name and clustered index name |
|||
Allpages-locked table |
Table scan |
Level 1; shared intent table lock, shared lock on current page |
|
Data-only-locked table |
Leaf level index scan |
Level 0; dirty reads |
|
Table name and nonclustered index name |
|||
Allpages-locked table |
Leaf level index scan |
Level 1; shared intent table lock, shared lock on current page |
|
Data-only-locked table |
Leaf level index scan |
Level 0; dirty reads |
|
Table name and column name |
|||
Allpages-locked table |
Table scan; creates a worktable and sorts the worktable |
Level 1; shared intent table lock, shared lock on current page |
|
Data-only-locked table |
Table scan; creates a worktable and sorts the worktable |
Level 0; dirty reads |
The update index statistics command generates a series of update statistics operations that use the same locking, scanning, and sorting as the equivalent index-level and column-level command. For example, if the salesdetail table has a nonclustered index named sales_det_ix on salesdetail(stor_id, ord_num, title_id), this command:
update index statistics salesdetail
performs these update statistics operations:
update statistics salesdetail sales_det_ix
update statistics salesdetail (ord_num)
update statistics salesdetail (title_id)
The update all statistics commands generates a series of update statistics operations for each index on the table, followed by a series of update statistics operations for all unindexed columns, followed by an update partition statistics operation.
update statistics is not run on system tables in the master database during upgrade from earlier versions. Indexes exist on columns queried by most system procedures, and running update statistics on these tables is not required for normal usage. However, running update statistics is allowed on all system tables in all databases, except those that are not normal tables. These tables, which are built from internal structures when queried, include syscurconfigs, sysengines, sysgams, syslisteners, syslocks, syslogs, syslogshold, sysmonitors, sysprocesses, syssecmechs, systestlog and systransactions.
Adaptive Server automatically recompiles stored procedures after executing update statistics statements. Although adhoc queries that you start before executing update statistics still continue to work, they do not take advantage of the new statistics.In Adaptive Server versions 12.5 and earlier, update statistics was ignored by cached stored procedures.
ANSI SQL – Compliance level: Transact-SQL extension.
update statistics permission defaults to the table owner and is not transferable. The command can also be executed by the Database Owner, who can impersonate the table owner by running the setuser command.
Commands – delete statistics