0001 ---
0002 layout: global
0003 title: ANALYZE TABLE
0004 displayTitle: ANALYZE TABLE
0005 license: |
0006 Licensed to the Apache Software Foundation (ASF) under one or more
0007 contributor license agreements. See the NOTICE file distributed with
0008 this work for additional information regarding copyright ownership.
0009 The ASF licenses this file to You under the Apache License, Version 2.0
0010 (the "License"); you may not use this file except in compliance with
0011 the License. You may obtain a copy of the License at
0012
0013 http://www.apache.org/licenses/LICENSE-2.0
0014
0015 Unless required by applicable law or agreed to in writing, software
0016 distributed under the License is distributed on an "AS IS" BASIS,
0017 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018 See the License for the specific language governing permissions and
0019 limitations under the License.
0020 ---
0021
0022 ### Description
0023
0024 The `ANALYZE TABLE` statement collects statistics about the table to be used by the query optimizer to find a better query execution plan.
0025
0026 ### Syntax
0027
0028 ```sql
0029 ANALYZE TABLE table_identifier [ partition_spec ]
0030 COMPUTE STATISTICS [ NOSCAN | FOR COLUMNS col [ , ... ] | FOR ALL COLUMNS ]
0031 ```
0032
0033 ### Parameters
0034
0035 * **table_identifier**
0036
0037 Specifies a table name, which may be optionally qualified with a database name.
0038
0039 **Syntax:** `[ database_name. ] table_name`
0040
0041 * **partition_spec**
0042
0043 An optional parameter that specifies a comma separated list of key and value pairs
0044 for partitions. When specified, partition statistics is returned.
0045
0046 **Syntax:** `PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] )`
0047
0048 * **[ NOSCAN `|` FOR COLUMNS col [ , ... ] `|` FOR ALL COLUMNS ]**
0049
0050 * If no analyze option is specified, `ANALYZE TABLE` collects the table's number of rows and size in bytes.
0051 * **NOSCAN**
0052
0053 Collects only the table's size in bytes ( which does not require scanning the entire table ).
0054 * **FOR COLUMNS col [ , ... ] `|` FOR ALL COLUMNS**
0055
0056 Collects column statistics for each column specified, or alternatively for every column, as well as table statistics.
0057
0058 ### Examples
0059
0060 ```sql
0061 CREATE TABLE students (name STRING, student_id INT) PARTITIONED BY (student_id);
0062 INSERT INTO students PARTITION (student_id = 111111) VALUES ('Mark');
0063 INSERT INTO students PARTITION (student_id = 222222) VALUES ('John');
0064
0065 ANALYZE TABLE students COMPUTE STATISTICS NOSCAN;
0066
0067 DESC EXTENDED students;
0068 +--------------------+--------------------+-------+
0069 | col_name| data_type|comment|
0070 +--------------------+--------------------+-------+
0071 | name| string| null|
0072 | student_id| int| null|
0073 | ...| ...| ...|
0074 | Statistics| 864 bytes| |
0075 | ...| ...| ...|
0076 | Partition Provider| Catalog| |
0077 +--------------------+--------------------+-------+
0078
0079 ANALYZE TABLE students COMPUTE STATISTICS;
0080
0081 DESC EXTENDED students;
0082 +--------------------+--------------------+-------+
0083 | col_name| data_type|comment|
0084 +--------------------+--------------------+-------+
0085 | name| string| null|
0086 | student_id| int| null|
0087 | ...| ...| ...|
0088 | Statistics| 864 bytes, 2 rows| |
0089 | ...| ...| ...|
0090 | Partition Provider| Catalog| |
0091 +--------------------+--------------------+-------+
0092
0093 ANALYZE TABLE students PARTITION (student_id = 111111) COMPUTE STATISTICS;
0094
0095 DESC EXTENDED students PARTITION (student_id = 111111);
0096 +--------------------+--------------------+-------+
0097 | col_name| data_type|comment|
0098 +--------------------+--------------------+-------+
0099 | name| string| null|
0100 | student_id| int| null|
0101 | ...| ...| ...|
0102 |Partition Statistics| 432 bytes, 1 rows| |
0103 | ...| ...| ...|
0104 | OutputFormat|org.apache.hadoop...| |
0105 +--------------------+--------------------+-------+
0106
0107 ANALYZE TABLE students COMPUTE STATISTICS FOR COLUMNS name;
0108
0109 DESC EXTENDED students name;
0110 +--------------+----------+
0111 | info_name|info_value|
0112 +--------------+----------+
0113 | col_name| name|
0114 | data_type| string|
0115 | comment| NULL|
0116 | min| NULL|
0117 | max| NULL|
0118 | num_nulls| 0|
0119 |distinct_count| 2|
0120 | avg_col_len| 4|
0121 | max_col_len| 4|
0122 | histogram| NULL|
0123 +--------------+----------+
0124 ```