column_statistics 数据字典表存储关于列值的直方图统计信息,供优化器在构建查询执行计划时使用。要执行直方图管理,请使用 ANALYZE
TABLE 语句。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.
column_statistics 表具有以下特性:The column_statistics table has these
characteristics:
该表包含所有数据类型列的统计信息,但不包括几何类型(空间数据)和
JSON。The table contains statistics for columns of all data types except geometry types (spatial data) andJSON.该表是持久的,因此每次服务器启动时不需要创建列统计信息。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.
column_statistics 表用户无法直接访问,因为它是数据字典的一部分。可以使用 INFORMATION_SCHEMA.COLUMN_STATISTICS 查看直方图信息,该视图是基于数据字典表实现的。COLUMN_STATISTICS 具有以下列: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:
列直方图包含列中存储值范围的各个部分的桶。直方图是 JSON 对象,以允许在列统计信息表示上具有灵活性。以下是一个示例直方图对象: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:直方图的桶。桶的结构取决于直方图类型。buckets: The histogram buckets. Bucket structure depends on the histogram type.对于
singleton直方图,桶包含两个值:Forsingletonhistograms, buckets contain two values:值 1:桶的值。类型取决于列的数据类型。Value 1: The value for the bucket. The type depends on the column data type.
值 2:表示该值的累积频率的双精度数。例如,.25 和 .75 表示该列中 25% 和 75% 的值小于或等于桶值。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.
对于
equi-height直方图,桶包含四个值:Forequi-heighthistograms, buckets contain four values:值 1, 2:桶的下限和上限值(包含边界)。类型取决于列的数据类型。Values 1, 2: The lower and upper inclusive values for the bucket. The type depends on the column data type.
值 3:表示该值累积频率的双精度数。例如,0.25 和 0.75 表示列中 25% 和 75% 的值小于或等于该桶的上限值。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.
值 4:从桶的下限值到上限值范围内的不同值的数量。Value 4: The number of distinct values in the range from the bucket lower value to its upper value.
null-values:一个介于 0.0 和 1.0 之间的数字,表示列值中 SQLNULL值的比例。如果为 0,则该列不包含NULL值。null-values: A number between 0.0 and 1.0 indicating the fraction of column values that are SQLNULLvalues. If 0, the column contains noNULLvalues.last-updated:生成直方图的时间,以 UTC 时间表示,格式为YYYY-MM-DD hh:mm:ss.uuuuuu。last-updated: When the histogram was generated, as a UTC value inYYYY-MM-DD hh:mm:ss.uuuuuuformat.sampling-rate:一个介于 0.0 和 1.0 之间的数字,表示用于创建直方图的数据采样比例。值为 1 表示读取了所有数据(无采样)。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:直方图类型:histogram-type: The histogram type:singleton:一个桶代表列中的一个单独值。当列中的不同值数量小于或等于生成直方图的ANALYZE TABLE语句中指定的桶数量时,会创建这种类型的直方图。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 theANALYZE TABLEstatement that generated the histogram.equi-height:一个桶代表一个值范围。当列中的不同值数量大于生成直方图的ANALYZE TABLE语句中指定的桶数量时,会创建这种类型的直方图。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 theANALYZE TABLEstatement that generated the histogram.
number-of-buckets-specified:在生成直方图的ANALYZE TABLE语句中指定的桶的数量。number-of-buckets-specified: The number of buckets specified in theANALYZE TABLEstatement that generated the histogram.data-type:此直方图包含的数据类型。当从持久存储中读取和解析直方图到内存时需要此信息。取值可以是int、uint(无符号整数)、double、decimal、datetime或string(包括字符和二进制字符串)。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 ofint,uint(unsigned integer),double,decimal,datetime, orstring(includes character and binary strings).collation-id:直方图数据的排序规则 ID。只有当data-type值为string时,这个值才有意义。其数值对应 Information SchemaCOLLATIONS表中的ID列值。collation-id: The collation ID for the histogram data. It is mostly meaningful when thedata-typevalue isstring. Values correspond toIDcolumn values in the Information SchemaCOLLATIONStable.
要从直方图对象中提取特定值,可以使用 JSON 操作。例如:To extract particular values from the histogram objects, you can
use JSON operations. For example:
mysql>SELECTTABLE_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=constantcol_name<>constantcol_name!=constantcol_name>constantcol_name<constantcol_name>=constantcol_name<=constantcol_nameIS NULLcol_nameIS NOT NULLcol_nameBETWEENconstantANDconstantcol_nameNOT BETWEENconstantANDconstantcol_nameIN (constant[,constant] ...)col_nameNOT 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;
与常数值进行比较的要求包括常数函数,例如 ABS() 和 FLOOR():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.
对于已建立索引的列,可以使用索引深入操作获得等值比较的行估计(参见第 10.2.1.2 节,“范围优化”)。在这种情况下,直方图统计信息不一定有用,因为索引深入操作可以产生更好的估计。For columns that are indexed, row estimates can be obtained for equality comparisons using index dives (see Section 10.2.1.2, “Range Optimization”). In this case, histogram statistics are not necessarily useful because index dives can yield better estimates.
在某些情况下,使用直方图统计信息可能不会改善查询执行(例如,如果统计信息过时)。要检查是否存在这种情况,请使用ANALYZE
TABLE 重新生成直方图统计信息,然后再次运行查询。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.
或者,要禁用直方图统计信息,可以使用 ANALYZE TABLE 来删除它们。禁用直方图统计信息的另一种方法是关闭 condition_fanout_filter 标志,该标志属于 optimizer_switch 系统变量(尽管这可能也会禁用其他优化功能):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';
如果使用了直方图统计信息,通过 EXPLAIN 可以看到其效果。考虑以下查询,其中列 col1 没有可用索引: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;
如果直方图统计显示 t1 中有 57% 的行满足 col1 <
24 这一条件,即使没有索引,也可以进行过滤,而且 EXPLAIN 在 filtered 列中显示 57.00。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.