COMPUTE STATS Statement

Gathers information about volume and distribution of data in a table and all associated columns and
partitions. The information is stored in the metastore database, and used by Impala to help optimize queries.
For example, if Impala can determine that a table is large or small, or has many or few distinct values it
can organize parallelize the work appropriately for a join query or insert operation. For details about the
kinds of information gathered by this statement, see Table and Column Statistics.

The PARTITION clause is only allowed in combination with the INCREMENTAL
clause. It is optional for COMPUTE INCREMENTAL STATS, and required for DROP
INCREMENTAL STATS. Whenever you specify partitions through the PARTITION
(partition_spec) clause in a COMPUTE INCREMENTAL STATS or
DROP INCREMENTAL STATS statement, you must include all the partitioning columns in the
specification, and specify constant values for all the partition key columns.

Usage notes:

Originally, Impala relied on users to run the Hive ANALYZE TABLE statement, but that method
of gathering statistics proved unreliable and difficult to use. The Impala COMPUTE STATS
statement is built from the ground up to improve the reliability and user-friendliness of this operation.
COMPUTE STATS does not require any setup steps or special configuration. You only run a
single Impala COMPUTE STATS statement to gather both table and column statistics, rather
than separate Hive ANALYZE TABLE statements for each kind of statistics.

The COMPUTE INCREMENTAL STATS variation is a shortcut for partitioned tables that works on a
subset of partitions rather than the entire table. The incremental nature makes it suitable for large tables
with many partitions, where a full COMPUTE STATS operation takes too long to be practical
each time a partition is added or dropped. See Overview of Incremental Statistics
for full usage details.

COMPUTE INCREMENTAL STATS only applies to partitioned tables. If you use the
INCREMENTAL clause for an unpartitioned table, Impala automatically uses the original
COMPUTE STATS statement. Such tables display false under the
Incremental stats column of the SHOW TABLE STATS output.

Note:
Because many of the most performance-critical and resource-intensive operations rely on table and column
statistics to construct accurate and efficient plans, COMPUTE STATS is an important step at
the end of your ETL process. Run COMPUTE STATS on all tables as your first step during
performance tuning for slow queries, or troubleshooting for out-of-memory conditions:

Accurate statistics help Impala distribute the work effectively for insert operations into Parquet
tables, improving performance and reducing memory usage.

Accurate statistics help Impala estimate the memory required for each query, which is important when you
use resource management features, such as admission control and the YARN resource management framework.
The statistics help Impala to achieve high concurrency, full utilization of available memory, and avoid
contention with workloads from other Hadoop components.

In Impala 2.8 and higher, when you run the
COMPUTE STATS or COMPUTE INCREMENTAL STATS
statement against a Parquet table, Impala automatically applies the query
option setting MT_DOP=4 to increase the amount of intra-node
parallelism during this CPU-intensive operation. See MT_DOP Query Option
for details about what this query option does and how to use it with
CPU-intensive SELECT statements.

Computing stats for groups of partitions:

In Impala 2.8 and higher, you can run COMPUTE INCREMENTAL STATS
on multiple partitions, instead of the entire table or one partition at a time. You include
comparison operators other than = in the PARTITION clause,
and the COMPUTE INCREMENTAL STATS statement applies to all partitions that
match the comparison expression.

For example, the INT_PARTITIONS table contains 4 partitions.
The following COMPUTE INCREMENTAL STATS statements affect some but not all
partitions, as indicated by the Updated n partition(s)
messages. The partitions that are affected depend on values in the partition key column X
that match the comparison expression in the PARTITION clause.

Currently, the statistics created by the COMPUTE STATS statement do not include
information about complex type columns. The column stats metrics for complex columns are always shown
as -1. For queries involving complex type columns, Impala uses
heuristics to estimate the data distribution within such columns.

HBase considerations:

COMPUTE STATS works for HBase tables also. The statistics gathered for HBase tables are
somewhat different than for HDFS-backed tables, but that metadata is still used for optimization when HBase
tables are involved in join queries.

The statistics collected by COMPUTE STATS are used to optimize join queries
INSERT operations into Parquet tables, and other resource-intensive kinds of SQL statements.
See Table and Column Statistics for details.

For large tables, the COMPUTE STATS statement itself might take a long time and you
might need to tune its performance. The COMPUTE STATS statement does not work with the
EXPLAIN statement, or the SUMMARY command in impala-shell.
You can use the PROFILE statement in impala-shell to examine timing information
for the statement as a whole. If a basic COMPUTE STATS statement takes a long time for a
partitioned table, consider switching to the COMPUTE INCREMENTAL STATS syntax so that only
newly added partitions are analyzed each time.

Examples:

This example shows two tables, T1 and T2, with a small number distinct
values linked by a parent-child relationship between T1.ID and T2.PARENT.
T1 is tiny, while T2 has approximately 100K rows. Initially, the statistics
includes physical measurements such as the number of files, the total size, and size measurements for
fixed-length columns such as with the INT type. Unknown values are represented by -1. After
running COMPUTE STATS for each table, much more information is available through the
SHOW STATS statements. If you were running a join query involving both of these tables, you
would need statistics for both tables to get the most effective optimization for the query.

The following example shows how to use the INCREMENTAL clause, available in Impala 2.1.0 and
higher. The COMPUTE INCREMENTAL STATS syntax lets you collect statistics for newly added or
changed partitions, without rescanning the entire table.

The COMPUTE STATS statement works with tables created with any of the file formats supported
by Impala. See How Impala Works with Hadoop File Formats for details about working with the
different file formats. The following considerations apply to COMPUTE STATS depending on the
file format of the table.

The COMPUTE STATS statement works with text tables with no restrictions. These tables can be
created through either Impala or Hive.

The COMPUTE STATS statement works with Parquet tables. These tables can be created through
either Impala or Hive.

The COMPUTE STATS statement works with Avro tables without restriction in Impala 2.2
and higher. In earlier releases, COMPUTE STATS worked only for Avro tables created through Hive,
and required the CREATE TABLE statement to use SQL-style column names and types rather than an
Avro-style schema specification.

The COMPUTE STATS statement works with RCFile tables with no restrictions. These tables can
be created through either Impala or Hive.

The COMPUTE STATS statement works with SequenceFile tables with no restrictions. These
tables can be created through either Impala or Hive.

The COMPUTE STATS statement works with partitioned tables, whether all the partitions use
the same file format, or some partitions are defined through ALTER TABLE to use different
file formats.

Statement type: DDL

Cancellation: Certain multi-stage statements (CREATE TABLE AS SELECT and
COMPUTE STATS) can be cancelled during some stages, when running INSERT
or SELECT operations internally. To cancel this statement, use Ctrl-C from the
impala-shell interpreter, the Cancel button from the
Watch page in Hue, or Cancel from the list of
in-flight queries (for a particular node) on the Queries tab in the Impala web UI
(port 25000).

Restrictions:

Note: Prior to Impala 1.4.0,
COMPUTE STATS counted the number of
NULL values in each column and recorded that figure
in the metastore database. Because Impala does not currently use the
NULL count during query planning, Impala 1.4.0 and
higher speeds up the COMPUTE STATS statement by
skipping this NULL counting.

Internal details:

Behind the scenes, the COMPUTE STATS statement
executes two statements: one to count the rows of each partition
in the table (or the entire table if unpartitioned) through the
COUNT(*) function,
and another to count the approximate number of distinct values
in each column through the NDV() function.
You might see these queries in your monitoring and diagnostic displays.
The same factors that affect the performance, scalability, and
execution of other queries (such as parallel execution, memory usage,
admission control, and timeouts) also apply to the queries run by the
COMPUTE STATS statement.

HDFS permissions:

The user ID that the impalad daemon runs under,
typically the impala user, must have read
permission for all affected files in the source directory:
all files in the case of an unpartitioned table or
a partitioned table in the case of COMPUTE STATS;
or all the files in partitions without incremental stats in
the case of COMPUTE INCREMENTAL STATS.
It must also have read and execute permissions for all
relevant directories holding the data files.
(Essentially, COMPUTE STATS requires the
same permissions as the underlying SELECT queries it runs
against the table.)

Kudu considerations:

The COMPUTE STATS statement applies to Kudu tables.
Impala does not compute the number of rows for each partition for
Kudu tables. Therefore, you do not need to re-run the operation when
you see -1 in the # Rows column of the output from
SHOW TABLE STATS. That column always shows -1 for
all Kudu tables.