ANALYZE TABLE
Aplica-se a: Databricks SQL
Databricks Runtime
A instrução ANALYZE TABLE
coleta estatísticas estimadas sobre uma tabela específica ou todas as tabelas em um esquema especificado. Essas estatísticas são usadas pelo otimizador de consulta para gerar um plano de consulta ideal.
A otimização preditiva é executada automaticamente nas tabelas geridas pelo Unity Catalog ANALYZE
. A Databricks recomenda habilitar a otimização preditiva para todas as tabelas gerenciadas pelo Unity Catalog para simplificar a manutenção de dados e reduzir os custos de armazenamento. Consulte Otimização preditiva para tabelas gerenciadas do Unity Catalog.
Sintaxe
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 ]
Parâmetros
-
Identifica a tabela a ser analisada. O nome não deve incluir uma especificação temporal ou uma especificação ou um caminho de opções. Se a tabela não puder ser encontrada, o Azure Databricks gerará um erro TABLE_OR_VIEW_NOT_FOUND.
-
Opcionalmente, limita o comando a um subconjunto de partições.
Esta cláusula não é suportada para tabelas Delta Lake.
DELTA
Aplica-se a:
Databricks SQL
Databricks Runtime 14.3 LTS e superior
Recalcula as estatísticas armazenadas no log Delta para as colunas configuradas para coleta de estatísticas em uma tabela Delta.
Quando a
DELTA
palavra-chave é especificada, as estatísticas normais para o otimizador de consulta não são coletadas.O Databricks recomenda executar
ANALYZE TABLE table_name COMPUTE DELTA STATISTICS
depois de definir novas colunas para pular dados para atualizar as estatísticas de todas as linhas de uma tabela. Para um desempenho otimizado, executeANALYZE TABLE table_name COMPUTE STATISTICS
para atualizar o plano de consulta após a conclusão da atualização do log Delta.[ NOSCAN | PARA COLUMNS col [, ...] | PARA TODOS OS COLUMNS ]
Se nenhuma opção de análise for especificada,
ANALYZE TABLE
coletará o número de linhas e o tamanho da tabela em bytes.EXAME
Colete apenas o tamanho da tabela em bytes (o que não requer a verificação de toda a tabela).
PARA COLUMNS col [, ...] | PARA TODOS OS COLUMNS
Colete estatísticas para cada coluna especificada ou, de forma alternativa, para todas as colunas, bem como estatísticas da tabela.
As estatísticas de coluna não são suportadas em combinação com a cláusula
PARTITION
.
{ DE
|
EM } schema_nameEspecifica o nome do esquema a ser analisado. Sem um nome de esquema,
ANALYZE TABLES
coleta todas as tabelas no esquema atual que o usuário atual tem permissão para analisar.
Exemplos
> 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;