The `column_statistics`

data dictionary table
stores histogram statistics about column values, for use by the
optimizer in constructing query execution plans. To perform
histogram management, use the ```
ANALYZE
TABLE
```

statement; see Section 13.7.3.1, “ANALYZE TABLE Syntax”.

The `column_statistics`

table has these
characteristics:

The table contains statistics for columns of all data types except geometry types (spatial data) and

`JSON`

.The table is persistent so that column statistics need not be created each time the server starts.

The server performs updates to the table; users do not.

The `column_statistics`

table is not directly
accessible by users because it is part of the data dictionary.
Histogram information is available using
`INFORMATION_SCHEMA.COLUMN_STATISTICS`

,
which is implemented as a view on the data dictionary table.
`COLUMN_STATISTICS`

has these
columns:

`SCHEMA_NAME`

,`TABLE_NAME`

,`COLUMN_NAME`

: The names of the schema, table, and column for which the statistics apply.`HISTOGRAM`

: A`JSON`

value describing the column statistics, stored as a histogram.

Column histograms contain buckets for parts of the range of
values stored in the column. Histograms are
`JSON`

objects to permit
flexibility in the representation of column statistics. Here is
a sample histogram object:

```
{
"buckets": [
[
1,
0.3333333333333333
],
[
2,
0.6666666666666666
],
[
3,
1
]
],
"null-values": 0,
"last-updated": "2017-03-24 13:32:40.000000",
"sampling-rate": 1,
"histogram-type": "singleton",
"number-of-buckets-specified": 128,
"data-type": "int",
"collation-id": 8
}
```

Histogram objects have these keys:

`buckets`

: The histogram buckets. Bucket structure depends on the histogram type.For

`singleton`

histograms, buckets contain two values:Value 1: The value for the bucket. The type depends on the column data type.

Value 2: A double representing the cumulative frequency for the value. For example, .25 and .75 indicate that 25% and 75% of the values in the column are less than or equal to the bucket value.

For

`equi-height`

histograms, buckets contain four values:Values 1, 2: The lower and upper inclusive values for the bucket. The type depends on the column data type.

Value 3: A double representing the cumulative frequency for the value. For example, .25 and .75 indicate that 25% and 75% of the values in the column are less than or equal to the bucket upper value.

Value 4: The number of distinct values in the range from the bucket lower value to its upper value.

`null-values`

: A number between 0.0 and 1.0 indicating the fraction of column values that are SQL`NULL`

values. If 0, the column contains no`NULL`

values.`last-updated`

: When the histogram was generated, as a UTC value informat.`YYYY-MM-DD hh:mm:ss.uuuuuu`

`sampling-rate`

: A number between 0.0 and 1.0 indicating the fraction of data that was sampled to create the histogram. A value of 1 means that all of the data was read (no sampling).`histogram-type`

: The histogram type:`singleton`

: One bucket represents one single value in the column. This histogram type is created when the number of distinct values in the column is less than or equal to the number of buckets specified in the`ANALYZE TABLE`

statement that generated the histogram.`equi-height`

: One bucket represents a range of values. This histogram type is created when the number of distinct values in the column is greater than the number of buckets specified in the`ANALYZE TABLE`

statement that generated the histogram.

`number-of-buckets-specified`

: The number of buckets specified in the`ANALYZE TABLE`

statement that generated the histogram.`data-type`

: The type of data this histogram contains. This is needed when reading and parsing histograms from persistent storage into memory. The value is one of`int`

,`uint`

(unsigned integer),`double`

,`decimal`

,`datetime`

, or`string`

(includes character and binary strings).`collation-id`

: The collation ID for the histogram data. It is mostly meaningful when the`data-type`

value is`string`

. Values correspond to`ID`

column values in the`INFORMATION_SCHEMA.COLLATIONS`

table.

To extract particular values from the histogram objects, you can
use `JSON`

operations. For example:

```
mysql> SELECT
TABLE_NAME, COLUMN_NAME,
HISTOGRAM->>'$."data-type"' AS 'data-type',
JSON_LENGTH(HISTOGRAM->>'$."buckets"') AS 'bucket-count'
FROM INFORMATION_SCHEMA.COLUMN_STATISTICS;
+-----------------+-------------+-----------+--------------+
| TABLE_NAME | COLUMN_NAME | data-type | bucket-count |
+-----------------+-------------+-----------+--------------+
| country | Population | int | 226 |
| city | Population | int | 1024 |
| countrylanguage | Language | string | 457 |
+-----------------+-------------+-----------+--------------+
```

The optimizer uses histogram statistics, if applicable, for columns of any data type for which statistics are collected. The optimizer applies histogram statistics to determine row estimates based on the selectivity (filtering effect) of column value comparisons against constant values. Predicates of these forms qualify for histogram use:

*col_name* = *constant*
*col_name* <> *constant*
*col_name* != *constant*
*col_name* > *constant*
*col_name* < *constant*
*col_name* >= *constant*
*col_name* <= *constant*
*col_name* IS NULL
*col_name* IS NOT NULL
*col_name* BETWEEN *constant* AND *constant*
*col_name* NOT BETWEEN *constant* AND *constant*
*col_name* IN (*constant*[, *constant*] ...)
*col_name* NOT IN (*constant*[, *constant*] ...)

For example, these statements contain predicates that qualify for histogram use:

```
SELECT * FROM orders WHERE amount BETWEEN 100.0 AND 300.0;
SELECT * FROM tbl WHERE col1 = 15 AND col2 > 100;
```

The requirement for comparison against a constant value includes
functions that are constant, such as
`ABS()`

and
`FLOOR()`

:

`SELECT * FROM tbl WHERE col1 < ABS(-34);`

Histogram statistics are useful primarily for nonindexed columns. Adding an index to a column for which histogram statistics are applicable might also help the optimizer make row estimates. The tradeoffs are:

An index must be updated when table data is modified.

A histogram is created or updated only on demand, so it adds no overhead when table data is modified. On the other hand, the statistics become progressively more out of date when table modifications occur, until the next time they are updated.

The optimizer prefers range optimizer row estimates to those obtained from histogram statistics. If the optimizer determines that the range optimizer applies, it does not use histogram statistics.

For columns that are indexed, row estimates can be obtained for equality comparisons using index dives (see Section 8.2.1.2, “Range Optimization”). In this case, histogram statistics are not necessarily useful because index dives can yield better estimates.

In some cases, use of histogram statistics may not improve query
execution (for example, if the statistics are out of date). To
check whether this is the case, use ```
ANALYZE
TABLE
```

to regenerate the histogram statistics, then run
the query again.

Alternatively, to disable histogram statistics, use
`ANALYZE TABLE`

to drop them. A
different method of disabling histogram statistics is to turn
off the `condition_fanout_filter`

flag of the
`optimizer_switch`

system
variable (although this may disable other optimizations as
well):

`SET optimizer_switch='condition_fanout_filter=off';`

If histogram statistics are used, the resulting effect is
visible using `EXPLAIN`

. Consider
the following query, where no index is available for column
`col1`

:

`SELECT * FROM t1 WHERE col1 < 24;`

If histogram statistics indicate that 57% of the rows in
`t1`

satisfy the ```
col1 <
24
```

predicate, filtering can occur even in the absence
of an index, and `EXPLAIN`

shows
57.00 in the `filtered`

column.