ANALYZE TABLE

Si applica a:check marked yes Databricks SQL check marked yes Databricks Runtime

L'istruzione ANALYZE TABLE raccoglie statistiche su una tabella specifica o su tutte le tabelle in uno schema specificato. Queste statistiche vengono usate da Query Optimizer per generare un piano di query ottimale. Poiché possono diventare obsoleti man mano che cambiano i dati, queste statistiche non vengono usate per rispondere direttamente alle query. Le statistiche non aggiornate sono ancora utili per Query Optimizer durante la creazione di un piano di query.

Sintassi

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 ]

Parametri

  • table_name

    Identifica la tabella da analizzare. Il nome non deve includere una specifica temporale o un percorso. Se la tabella non è stata trovata, Azure Databricks genera un errore di TABLE_OR_VIEW_NOT_FOUND .

  • Clausola PARTITION

    Facoltativamente, limita il comando a un subset di partizioni.

    Questa clausola non è supportata per le tabelle Delta Lake.

  • DELTA

    Si applica a:check marked yes Databricks Runtime 14.3 LTS e versioni successive

    Ricompila le statistiche archiviate nel log Delta per le colonne configurate per la raccolta di statistiche in una tabella Delta.

    Quando si specifica la DELTA parola chiave , le statistiche normali per Query Optimizer non vengono raccolte.

    Databricks consiglia di eseguire ANALYZE TABLE table_name COMPUTE DELTA STATISTICS dopo aver impostato nuove colonne per ignorare i dati per aggiornare le statistiche per tutte le righe di una tabella. Per ottimizzare le prestazioni, eseguire ANALYZE TABLE table_name COMPUTE STATISTICS per aggiornare il piano di query al termine dell'aggiornamento del log Delta.

  • [ NOSCAN | FOR COLUMNS col [, ...] | FOR ALL COLUMNS ]

    Se non viene specificata alcuna opzione di analisi, ANALYZE TABLE raccoglie il numero di righe e dimensioni della tabella in byte.

    • NOSCAN

      Raccogliere solo le dimensioni della tabella in byte ( che non richiede l'analisi dell'intera tabella ).

    • FOR COLUMNS col [, ...] | PER TUTTE LE COLONNE

      Raccogliere le statistiche delle colonne per ogni colonna specificata o in alternativa per ogni colonna, nonché le statistiche delle tabelle.

      Le statistiche delle colonne non sono supportate in combinazione con la PARTITION clausola .

  • { FROM | IN } schema_name

    Specifica il nome dello schema da analizzare. Senza un nome di schema, ANALYZE TABLES raccoglie tutte le tabelle nello schema corrente a cui l'utente corrente dispone dell'autorizzazione per l'analisi.

Esempi

> 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;