update statistics

Description

Updates information about the distribution of key values in specified indexes, for all columns in an index, table, or partition, and resets the data change counters for global nonclustered indexes.

Syntax

update statistics table_name
	[[partition data_partition_name] 
		[ (column1, column2, …) | (column1), (column2), …] |
	index_name [partition index_partition_name]] 
	[using step values | [out_of_range [on | off| default]]]
	[with consumers = consumers][, sampling=N percent]
		[, no_hashing | partial_hashing | hashing]
		[, max_resource_granularity = N [percent]] 
		[, histogram_tuning_factor = int ]
		[, print_progress = int]

Parameters

table_name

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.

index_name

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.

data_partition_name

is the name of the partition to be updated. Statistics for each local index partition on the data partition is updated. Does not update statistics for global indexes.

index_partition_name

is the name of the index partition to be updated.

(column1, column2, ...)

creates the same statistics that are created if a local index is created and included on this column tuple. That is, creates a histogram on column1, and create multiattribute distributions on all column prefixes (column1, column2), (column1, column2, column3).

(column1), (column2)

comma-separated list of columns on which you are running update statistics. Allows for a single update statistics command to create a histogram on each column in the list, but does not create any multi-attribute distributions.

using step values

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 this, use sp_configure to modify the number of histogram steps parameter. If statistics for a column already exist in sysstatistics, the default value is the current number of steps.

The steps are applied to each partition of a partitioned table—for example, update statistics uses the default value of 20 steps for each data and index partition involved in the scan for updating statistics. If global statistics are generated through an index scan of a global index, then 20 steps are applied by default. If partition statistics are generated, either through a data scan or local index scan, then 20 steps are applied by default for each partition.

If the histogram steps specified through using step values is M, and the histogram_tuning_factor parameter is N, then update statistics uses between 0 and M*N steps, depending on the number of frequency cells that update statistics isolates and whether any range cells exist.

out_of_range [on | off | default]

Column statistics for rapidly growing tables may become out-of-date when update statistics completes, resulting in out-of-range SARGs (search clauses) that select a greater range of values than described by the column’s histogram. Out-of-range SARGS have a selectivity of 0. The out_of_range] histogram adjustment feature adjusts a column’s histogram, and assigns an appropriate selectivity value to such SARGs.

Histogram adjustment for out of range SARGS is enabled server wide by default.

out_of_range [on | off | default] specifies an out-of-range histogram adjustment at the column level. One of:

  • on – Enables out-of-range histogram adjustment for column_name.

  • off – Disables out-of-range histogram adjustment for column_name.

  • default – Affects the out-of-range histogram adjustment depending on the value of trace flag 15355:

    • Disables out-of-range histogram adjustment when Traceflag 15355 is on.

    • Enables out-of-range histogram adjustment when Traceflag 15355 is off.

with consumers = consumers

specifies the number of consumer processes to be used for a sort when column_list is provided and parallel query processing is enabled. The consumers option specifies the degree of parallelism applied to a sort performed for statistics update on a single data partition. For example, if update statistics with a column list is applied to a table with three data partitions, data from each of the three partitions is sorted separately and the consumers option is applied during each of the sort. The three sorts themselves are not performed in parallel.

NoteThe value for the max parallel degree configuration parameter must be be greater than the value for with consumers. For example, if with consumers is set to 2, then max parallel degree must be at least 3.

with sampling = N percent

specifies the percentage of the column to be randomly sampled in order to gather statistics. The value for N is any number between 1 and 100. Sampling applies to all update statistics types:

  • update statistics table_name (col_name)

  • update index statistics

  • update all statistics

index

specifies that statistics for all columns in an index are to be updated.

[no_hashing | partial_hashing | hashing]

indicates the level of hash-based statistics update statistics gathers. One of:

  • no_hashing – (the default) update statistics uses the algorithm from Adaptive Server versions earlier than 15.7, gathering sort-based statistics.

  • partial_hashingupdate statistics uses the algorithm for columns with fewer than 65536 unique values. If update statistics encounters unique column counts that are greater than or equal to the 65536 threshold, it uses an extra scan with a sort.

  • hashingupdate statistics uses low-domain and high-domain hashing to create the histograms.

max_resource_granularity = N percent

limits the amount of tempdb buffer cache used with update statistics and hashing.

with histogram_tuning_factor = integer

determines update statistics’s distribution granularity.

print_progress = int

Determines if update statistics displays progress messages.

  • 0 – (the default) command does not display any progress messages

  • 1 – command displays progress messages

Examples

Example 1

Generates statistics for the price column of the titles table:

update statistics titles (price) using 40 values

Example 2

Updates statistics on the data partition smallsales Adaptive Server creates histograms for each leading column and densities for the composite columns of each local index of the data partition. Statistics are not updated for global indexes:

update statistics titles partition smallsales

Example 3

Updates statistics on the data partition smallsales. Adaptive Server creates histograms on column col1 and creates densities for the composite columns col1 and col2:

update statistics titles partition smallsales (col1, col2)

Example 4

When an out_of_range SARG is detected for a column, the optimizer adjusts the column’s histogram and assigns an appropriate selectivity value to the out-of-range clause:

update statistics TOFO_FUOP_ORD(OrdDt) using
out_of_range on

Example 5

If trace flag 15355 is turned on, the column’s histogram is not adjusted for out-of-range SARGs:

update statistics TOFO_FUOP_ORD(OrdDt) using 
out_of_range default

Example 6

Runs update statistics on the authors table with a histogram_tuning_factor of 5 percent:

update index statistics authors with histogram_tuning_factor = 5

Usage


Using hash-based statistics


update statistics and sampling

Adaptive Server scans samples of data pages. If you specify an index in update statistics, such as in the following:

update statistics table_name [index_name] with sampling = N percent

This command creates and updates statistics on the leading column of all indexes on the specified table, or the leading column of a specified index.

When you use the sampling = N percent option with the using steps value , you must specify the sampling = N percent option last:

update statistics titles (type) 
    using 40 value
    with sampling = 10 percent 

If you do not, you get an error message:

update statistics titles (type) 
    with sampling = 10 percent 
    using 40 value
Msg 156, Level 15, State 2:
Line 1:
Incorrect syntax near the keyword 'using'.

create index and stored procedures

Adaptive Server automatically recompiles stored procedures after executing update statistics statements. Although ad hoc 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.

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

You must be the table owner or a user with update statistics permission on the table to run update statistics.

See also

Commands delete statistics, update all statistics, update index statistics, update table statistics

Documentation Performance and Tuning Guide

For optdiag syntax and usage, see “Statistics Tables and Displaying Statistics with optdiag” in Performance and Tuning Series: Monitoring and Analyzing.