CREATE ANALYZE
CREATE ANALYZE 自定义用于收集 CBO 统计信息的自动收集任务。
默认情况下,StarRocks 会自动收集表的完整统计信息。它会每 5 分钟检查一次是否有任何数据更新。如果检测到数据更改,将自动触发数据收集。如果您不想使用自动完整收集,可以将 FE 配置项 enable_collect_full_statistic
设置为 false
并自定义收集任务。
行为差异
- 在 3.2.12 和 3.3.4 之前的版本中,要创建自定义自动收集任务,必须禁用自动完整收集 (
enable_collect_full_statistic = false
)。否则,自定义任务将无法生效。 - 自这些版本之后,您可以创建 analyze job 并保留系统任务。如果用户创建的任务与系统任务冲突,则用户创建的任务将覆盖系统任务。
该语句从 v2.4 版本开始支持。
语法
-- Automatically collect stats of all databases.
CREATE ANALYZE [FULL|SAMPLE] ALL PROPERTIES (property [,property])
-- Automatically collect stats of all tables in a database.
CREATE ANALYZE [FULL|SAMPLE] DATABASE db_name
PROPERTIES (property [,property])
-- Automatically collect stats of specified columns in a table.
CREATE ANALYZE [FULL|SAMPLE] TABLE tbl_name (col_name [,col_name])
PROPERTIES (property [,property])
参数描述
-
收集类型
- FULL:表示全量收集。
- SAMPLE:表示采样收集。
- 如果未指定收集类型,则默认使用抽样收集。
-
col_name
:从中收集统计信息的列。用逗号 (,
) 分隔多个列。如果未指定此参数,则收集整个表。 -
PROPERTIES
:自定义参数。如果未指定PROPERTIES
,则使用fe.conf
中的默认设置。实际使用的属性可以通过 SHOW ANALYZE JOB 的输出中的Properties
列查看。
PROPERTIES | 类型 | 默认值 | 描述 |
---|---|---|---|
statistic_auto_collect_ratio | FLOAT | 0.8 | 用于确定自动收集统计信息是否健康的阈值。如果统计信息的健康状况低于此阈值,则会触发自动收集。 |
statistics_max_full_collect_data_size | INT | 100 | 用于自动收集以收集数据的最大分区的大小。单位:GB。如果分区超过此值,则放弃完整收集,而执行抽样收集。 |
statistic_sample_collect_rows | INT | 200000 | 要收集的最小行数。如果参数值超过表中的实际行数,则执行完整收集。 |
示例
示例 1:自动完整收集
-- Automatically collect full stats of all databases.
CREATE ANALYZE ALL;
-- Automatically collect full stats of a database.
CREATE ANALYZE DATABASE db_name;
-- Automatically collect full stats of all tables in a database.
CREATE ANALYZE FULL DATABASE db_name;
-- Automatically collect full stats of specified columns in a table.
CREATE ANALYZE TABLE tbl_name(c1, c2, c3);
示例 2:自动抽样收集
-- Automatically collect stats of all tables in a database using default settings.
CREATE ANALYZE SAMPLE DATABASE db_name;
-- Automatically collect stats of specified columns in a table, with statistics health and the number of rows to collect specified.
CREATE ANALYZE SAMPLE TABLE tbl_name(c1, c2, c3) PROPERTIES(
"statistic_auto_collect_ratio" = "0.5",
"statistic_sample_collect_rows" = "1000000"
);
参考
SHOW ANALYZE JOB:查看自定义收集任务的状态。
DROP ANALYZE:删除自定义收集任务。
KILL ANALYZE:取消正在运行的自定义收集任务。
有关为 CBO 收集统计信息的更多信息,请参见为 CBO 收集统计信息。