ANALYZE TABLE
適用於:Databricks SQL
Databricks Runtime
ANALYZE TABLE
語句會收集特定數據表或指定架構中所有數據表的估計統計數據。 查詢優化器會使用這些統計數據來產生最佳的查詢計劃。
預測性優化會自動 ANALYZE
ON Unity 目錄受控數據表執行。 Databricks 建議為所有 Unity 目錄受控數據表啟用預測優化,以簡化數據維護並減少記憶體成本。 請參閱 Unity 目錄受控數據表的預測性優化。
語法
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 [,...] |全部 COLUMNS ]
如果未指定分析選項,
ANALYZE TABLE
會收集數據表的數據列數目和位元元組大小。NOSCAN
只收集以位元組為單位的數據表大小(不需要掃描整個資料表 )。
FOR COLUMNS col [, ...] |適用於所有 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;