ANALYZE TABLE
适用于: Databricks SQL Databricks Runtime
ANALYZE TABLE
语句收集有关指定架构中特定表或所有表的估计统计信息。 查询优化器使用这些统计信息来生成最佳查询计划。
预测优化会在 Unity Catalog 托管表上自动运行 ANALYZE
。 Databricks 建议为所有 Unity Catalog 托管表启用预测优化,以简化数据维护并降低存储成本。
语法
ANALYZE TABLE table_name [ PARTITION clause ]
COMPUTE [ DELTA ] STATISTICS [ NOSCAN | FOR COLUMNS col1 [, ...] | FOR ALL COLUMNS ]
ANALYZE TABLES [ { FROM | IN } schema_name ] COMPUTE STATISTICS [ NOSCAN ]
参数
-
标识要分析的表。 名称不得包含时态规范或路径。 如果找不到表,Azure Databricks 会引发 TABLE_OR_VIEW_NOT_FOUND 错误。
-
(可选)将命令限制为分区的子集。
Delta Lake 表不支持此子句。
DELTA
适用于: Databricks SQL Databricks Runtime 14.3 LTS 及更高版本
为 Delta 表中的统计信息集合配置的列重新计算存储在 Delta 日志中的统计信息。
指定
DELTA
关键字时,不会收集查询优化器的正常统计信息。Databricks 建议在为数据设置新列来跳转更新表中所有行的统计信息设置之后运行
ANALYZE TABLE table_name COMPUTE DELTA STATISTICS
。 为了优化性能,请在 Delta 日志更新完成后运行ANALYZE TABLE table_name COMPUTE STATISTICS
来更新查询计划。[ NOSCAN | FOR COLUMNS col [, …] | FOR ALL COLUMNS ]
如果未指定分析选项,
ANALYZE TABLE
将收集表的行数和大小(以字节为单位)。NOSCAN
仅收集表的大小(以字节为单位)(不需要扫描整个表)。
FOR COLUMNS col [, …] | FOR ALL COLUMNS
收集每个指定列或每列的列统计信息,以及表统计信息。
不支持将列统计信息与
PARTITION
子句结合使用。
{ FROM
|
IN } schema_name指定要分析的架构的名称。 如果没有架构名称,
ANALYZE TABLES
将收集当前用户有权分析的当前架构中的所有表。
示例
> CREATE TABLE students (name STRING, student_id INT) PARTITIONED BY (student_id);
> INSERT INTO students PARTITION (student_id = 111111) VALUES ('Mark');
> INSERT INTO students PARTITION (student_id = 222222) VALUES ('John');
> ANALYZE TABLE students COMPUTE STATISTICS NOSCAN;
> DESC EXTENDED students;
col_name data_type comment
-------------------- -------------------- -------
name string null
student_id int null
... ... ...
Statistics 864 bytes
... ... ...
> ANALYZE TABLE students COMPUTE STATISTICS;
> DESC EXTENDED students;
col_name data_type comment
-------------------- -------------------- -------
name string null
student_id int null
... ... ...
Statistics 864 bytes, 2 rows
... ... ...
-- Note: ANALYZE TABLE .. PARTITION is not supported for Delta tables.
> ANALYZE TABLE students PARTITION (student_id = 111111) COMPUTE STATISTICS;
> DESC EXTENDED students PARTITION (student_id = 111111);
col_name data_type comment
-------------------- -------------------- -------
name string null
student_id int null
... ... ...
Partition Statistics 432 bytes, 1 rows
... ... ...
OutputFormat org.apache.hadoop...
> ANALYZE TABLE students COMPUTE STATISTICS FOR COLUMNS name;
> DESC EXTENDED students name;
info_name info_value
-------------- ----------
col_name name
data_type string
comment NULL
min NULL
max NULL
num_nulls 0
distinct_count 2
avg_col_len 4
max_col_len 4
histogram NULL
> ANALYZE TABLES IN school_schema COMPUTE STATISTICS NOSCAN;
> DESC EXTENDED teachers;
col_name data_type comment
-------------------- -------------------- -------
name string null
teacher_id int null
... ... ...
Statistics 1382 bytes
... ... ...
> DESC EXTENDED students;
col_name data_type comment
-------------------- -------------------- -------
name string null
student_id int null
... ... ...
Statistics 864 bytes
... ... ...
> ANALYZE TABLES COMPUTE STATISTICS;
> DESC EXTENDED teachers;
col_name data_type comment
-------------------- -------------------- -------
name string null
teacher_id int null
... ... ...
Statistics 1382 bytes, 2 rows
... ... ...
> DESC EXTENDED students;
col_name data_type comment
-------------------- -------------------- -------
name string null
student_id int null
... ... ...
Statistics 864 bytes, 2 rows
... ... ...
> ANALYZE TABLE some_delta_table COMPUTE DELTA STATISTICS;