---
metadata:
  - name: generator
    content: Diplodoc Platform v5.70.2
alternate:
  - https://ydb.tech/docs/en/yql/reference/syntax/create_table/statistics.md
  - https://ydb.tech/docs/ru/yql/reference/syntax/create_table/statistics.md
  - href: https://ydb.tech/docs/en/yql/reference/syntax/create_table/statistics.md
    type: text/markdown
    title: Markdown version
  - href: https://ydb.tech/docs/en/llms.txt?revision=d94cda5622eadbc2010c4ce972644123d8f317b6&version=v26.3
    rel: describedby
sourcePath: en/core/yql/reference/syntax/create_table/statistics.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb.tech/docs/en/llms.txt?revision=d94cda5622eadbc2010c4ce972644123d8f317b6&version=v26.3

# STATISTICS

`STATISTICS` defines multi-column statistics for a new table. Run [ANALYZE](https://ydb.tech/docs/en/yql/reference/syntax/analyze.md) to collect them.

The [cost-based optimizer](https://ydb.tech/docs/en/concepts/query_execution/optimizer.md) uses the resulting data to choose a query plan. By default, `ANALYZE` collects information about the table as a whole and about each column separately. Use `STATISTICS` to also analyze combinations of values across multiple columns.

Add `STATISTICS` to the table definition:

```yql
CREATE TABLE <table_name> (
    ...
    STATISTICS <statistics_name> ON ( <column_name> [, ...] )
        WITH ( <statistics_type> ),
    PRIMARY KEY ( ... )
);
```

[`SHOW CREATE TABLE`](https://ydb.tech/docs/en/yql/reference/syntax/show_create.md) displays the multi-column statistics settings. To add or remove them for an existing table, use [`ALTER TABLE`](https://ydb.tech/docs/en/yql/reference/syntax/alter_table/statistics.md).

## Parameters

* `table_name`: name of the table to create.
* `statistics_name`: name of the multi-column statistics.
* `column_name`: names of existing table columns. The column order matters.
* `statistics_type`: `COUNT_MIN_SKETCH`. Specify `WITH (COUNT_MIN_SKETCH)` explicitly: in this version, multi-column statistics are not collected if `WITH` is omitted.

## Statistics types

`COUNT_MIN_SKETCH` estimates how often a combination of values from multiple columns occurs. Specify at least two columns: a single column does not produce separate statistics.

For individual columns, `ANALYZE` builds sketches without `STATISTICS` when the estimated number of distinct values is less than 80% of the row count.

## Example

Configure a multi-column sketch when creating a table and start collecting statistics:

```yql
CREATE TABLE orders (
    customer_id Uint64,
    order_date Date,
    status Utf8,
    amount Int64,
    STATISTICS orders_cms ON (customer_id, order_date) WITH (COUNT_MIN_SKETCH),
    PRIMARY KEY (customer_id, order_date)
);

ANALYZE orders;
```
