分析數據表

適用於:check marked yes Databricks SQL check marked yes Databricks Runtime

語句 ANALYZE TABLE 會收集特定數據表或指定架構中所有數據表的相關統計數據。 查詢優化器會使用這些統計數據來產生最佳的查詢計劃。 由於數據變更可能會過時,因此這些統計數據不會用來直接回應查詢。 建立查詢計劃時,過時的統計數據仍然對查詢優化器很有用。

語法

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 ]

參數

  • table_name

    識別要分析的數據表。 名稱不得包含 時態規格 或路徑。 如果找不到數據表,Azure Databricks 就會 引發TABLE_OR_VIEW_NOT_FOUND 錯誤。

  • PARTITION 子句

    選擇性地將命令限制為分割區子集。

    Delta Lake 數據表不支持這個子句。

  • DELTA

    適用於:check marked yes Databricks Runtime 14.3 LTS 和更新版本

    針對 Delta 資料表中針對針對統計數據集合所設定的數據行,重新計算儲存在 Delta 記錄中的統計數據。

    DELTA指定 關鍵詞時,不會收集查詢優化工具的一般統計數據。

    Databricks 建議在設定新的數據行之後執行 ANALYZE TABLE table_name COMPUTE DELTA STATISTICS ,以略過數據來更新數據表中所有數據列的統計數據。 針對優化的效能,請在差異記錄更新完成之後執行 ANALYZE TABLE table_name COMPUTE STATISTICS 以更新查詢計劃。

  • [ NOSCAN |FOR COLUMNS col [, ...] |所有資料行 ]

    如果未指定分析選項, ANALYZE TABLE 請收集資料表的數據列數目和位元元組大小。

    • NOSCAN

      只收集以位元組為單位的數據表大小(不需要掃描整個資料表 )。

    • FOR COLUMNS col [, ...] |所有數據行

      收集每個指定數據行的數據行統計數據,或針對每個數據行以及數據表統計數據。

      數據行統計數據與 子句組合 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;