ANALYZE TABLE

Description

The ANALYZE TABLE statement collects statistics about one specific table or all the tables in one specified database, that are to be used by the query optimizer to find a better query execution plan.

Syntax

  1. ANALYZE TABLE table_identifier [ partition_spec ]
  2. COMPUTE STATISTICS [ NOSCAN | FOR COLUMNS col [ , ... ] | FOR ALL COLUMNS ]
  1. ANALYZE TABLES [ { FROM | IN } database_name ] COMPUTE STATISTICS [ NOSCAN ]

Parameters

  • table_identifier

    Specifies a table name, which may be optionally qualified with a database name.

    Syntax: [ database_name. ] table_name

  • partition_spec

    An optional parameter that specifies a comma separated list of key and value pairs for partitions. When specified, partition statistics is returned.

    Syntax: PARTITION ( partition_col_name [ = partition_col_val ] [ , ... ] )

  • { FROM | IN } database_name

    Specifies the name of the database to be analyzed. Without a database name, ANALYZE collects all tables in the current database that the current user has permission to analyze.

  • NOSCAN

    Collects only the table’s size in bytes (which does not require scanning the entire table).

  • FOR COLUMNS col [ , … ] | FOR ALL COLUMNS

    Collects column statistics for each column specified, or alternatively for every column, as well as table statistics.

If no analyze option is specified, both number of rows and size in bytes are collected.

Examples

  1. CREATE DATABASE school_db;
  2. USE school_db;
  3. CREATE TABLE teachers (name STRING, teacher_id INT);
  4. INSERT INTO teachers VALUES ('Tom', 1), ('Jerry', 2);
  5. CREATE TABLE students (name STRING, student_id INT) PARTITIONED BY (student_id);
  6. INSERT INTO students VALUES ('Mark', 111111), ('John', 222222);
  7. ANALYZE TABLE students COMPUTE STATISTICS NOSCAN;
  8. DESC EXTENDED students;
  9. +--------------------+--------------------+-------+
  10. | col_name| data_type|comment|
  11. +--------------------+--------------------+-------+
  12. | name| string| null|
  13. | student_id| int| null|
  14. | ...| ...| ...|
  15. | Statistics| 864 bytes| |
  16. | ...| ...| ...|
  17. +--------------------+--------------------+-------+
  18. ANALYZE TABLE students COMPUTE STATISTICS;
  19. DESC EXTENDED students;
  20. +--------------------+--------------------+-------+
  21. | col_name| data_type|comment|
  22. +--------------------+--------------------+-------+
  23. | name| string| null|
  24. | student_id| int| null|
  25. | ...| ...| ...|
  26. | Statistics| 864 bytes, 2 rows| |
  27. | ...| ...| ...|
  28. +--------------------+--------------------+-------+
  29. ANALYZE TABLE students PARTITION (student_id = 111111) COMPUTE STATISTICS;
  30. DESC EXTENDED students PARTITION (student_id = 111111);
  31. +--------------------+--------------------+-------+
  32. | col_name| data_type|comment|
  33. +--------------------+--------------------+-------+
  34. | name| string| null|
  35. | student_id| int| null|
  36. | ...| ...| ...|
  37. |Partition Statistics| 432 bytes, 1 rows| |
  38. | ...| ...| ...|
  39. +--------------------+--------------------+-------+
  40. ANALYZE TABLE students COMPUTE STATISTICS FOR COLUMNS name;
  41. DESC EXTENDED students name;
  42. +--------------+----------+
  43. | info_name|info_value|
  44. +--------------+----------+
  45. | col_name| name|
  46. | data_type| string|
  47. | comment| NULL|
  48. | min| NULL|
  49. | max| NULL|
  50. | num_nulls| 0|
  51. |distinct_count| 2|
  52. | avg_col_len| 4|
  53. | max_col_len| 4|
  54. | histogram| NULL|
  55. +--------------+----------+
  56. ANALYZE TABLES IN school_db COMPUTE STATISTICS NOSCAN;
  57. DESC EXTENDED teachers;
  58. +--------------------+--------------------+-------+
  59. | col_name| data_type|comment|
  60. +--------------------+--------------------+-------+
  61. | name| string| null|
  62. | teacher_id| int| null|
  63. | ...| ...| ...|
  64. | Statistics| 1382 bytes| |
  65. | ...| ...| ...|
  66. +--------------------+--------------------+-------+
  67. DESC EXTENDED students;
  68. +--------------------+--------------------+-------+
  69. | col_name| data_type|comment|
  70. +--------------------+--------------------+-------+
  71. | name| string| null|
  72. | student_id| int| null|
  73. | ...| ...| ...|
  74. | Statistics| 864 bytes| |
  75. | ...| ...| ...|
  76. +--------------------+--------------------+-------+
  77. ANALYZE TABLES COMPUTE STATISTICS;
  78. DESC EXTENDED teachers;
  79. +--------------------+--------------------+-------+
  80. | col_name| data_type|comment|
  81. +--------------------+--------------------+-------+
  82. | name| string| null|
  83. | teacher_id| int| null|
  84. | ...| ...| ...|
  85. | Statistics| 1382 bytes, 2 rows| |
  86. | ...| ...| ...|
  87. +--------------------+--------------------+-------+
  88. DESC EXTENDED students;
  89. +--------------------+--------------------+-------+
  90. | col_name| data_type|comment|
  91. +--------------------+--------------------+-------+
  92. | name| string| null|
  93. | student_id| int| null|
  94. | ...| ...| ...|
  95. | Statistics| 864 bytes, 2 rows| |
  96. | ...| ...| ...|
  97. +--------------------+--------------------+-------+