Skip to main content
Use the CREATE STATISTICS to to use. Once you and load data into it (e.g., , ), table statistics can be generated. Table statistics help the cost-based optimizer determine the cardinality of the rows used in each query, which helps to predict more accurate costs. For compatibility with PostgreSQL, CockroachDB supports the ANALYZE/ANALYSE statement as an alias for CREATE STATISTICS. For syntax, see Aliases. By default, CockroachDB on all indexed columns and up to 100 non-indexed columns, and automatically collects multi-column statistics on columns that prefix each index. As a result, most users do not need to directly issue CREATE STATISTICS statements.

Syntax

create_stats syntax diagram

Parameters

ParameterDescription
statistics_nameThe name of the set of statistics you are creating.
opt_stats_columnsThe name of the column(s) to create statistics for.
create_stats_targetThe name of the table to create statistics for.
opt_as_of_clauseCreate historical stats using the clause. For instructions, see Create statistics as of a given time.

Required privileges

The user must have the or the SELECT privilege on the target table. implicitly have this privilege.

Aliases

For , CockroachDB supports ANALYZE and ANALYSE as aliases for CREATE STATISTICS.

Alias syntax

analyze syntax diagram

Alias parameters

ParameterDescription
analyze_targetThe name of the table to create statistics for.

Examples

Setup

To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:

Create statistics on a single column

Statistics are automatically collected for all columns, making the revenue_stats statistics a duplicate of the statistics automatically collected on the revenue column.

Create statistics on multiple columns

Multi-column statistics are automatically collected for all columns that prefix an index. In this example, city and revenue are not an index prefix, making the city_revenue_stats statistics unique for the table.

Create statistics on a default set of columns

The CREATE STATISTICS statement shown below automatically figures out which columns to get statistics on.
This statement creates statistics identical to the statistics that CockroachDB creates automatically.

Create statistics as of a given time

To create statistics as of a given time (in this example, 1 minute ago to avoid interfering with the production workload), run a statement like the following:
For more information about how the AS OF SYSTEM TIME clause works, including supported time formats, see .

Create partial statistics using extremes

CockroachDB supports , which collect statistics on a subset of table data to provide more up-to-date information without scanning the entire table. To create that collect statistics on the highest and lowest index values:
This creates partial statistics on all single-column prefixes of non-inverted indexes in the rides table by scanning only the highest and lowest index values, rather than performing a full table scan. You can also create extremes statistics on specific columns, provided there is an index with the specified column as the first key column:

Create partial statistics on specific data

To create on a specific column and values matching specific conditions, ensure the column is indexed:
Partial statistics can target any subset of data that matches specific conditions. For example, to create statistics on high-value rides:
This statement creates partial statistics that cover only high-value rides.

Delete statistics

To delete statistics for all tables in all databases:
To delete a named set of statistics (e.g, one named “users_stats”), run a query like the following:
For more information about the DELETE statement, see .

View statistics jobs

Every time the CREATE STATISTICS statement is executed, it starts a background job. This is true for queries issued by your application as well as queries issued for . To view statistics jobs, there are two options:
  1. Use to see all statistics jobs that were created by user queries (i.e., someone entering CREATE STATISTICS at the SQL prompt or via application code):
  2. Use SHOW AUTOMATIC JOBS to see statistics jobs that were created by :

Known limitations

The ANALYZE alias of does not support specifying an AS OF SYSTEM TIME timestamp. ANALYZE statements use AS OF SYSTEM TIME '-0.001ms' automatically. For more control over the statistics interval, use the CREATE STATISTICS syntax instead.

See also