ANALYZE TABLE

ANALYZE TABLE collects statistics for one or more tables, optionally restricted to a column list, so the query optimizer has up-to-date information when choosing execution plans.

Description

ANALYZE TABLE computes and stores statistics for the named tables. These statistics help the query optimizer estimate cardinality and choose better plans.

The statement supports three forms:

  • ANALYZE TABLE t; analyzes the whole table using all columns.

  • ANALYZE TABLE t(a, b); analyzes only the named columns.

  • ANALYZE TABLE t1, t2; analyzes multiple tables in one statement.

Quoted database, table, and column identifiers are supported.

Syntax

ANALYZE TABLE table [(column [, column] ...)] [, table [(column [, column] ...)] ...]

Arguments

Argument

Description

table

The table to analyze.

column

Optional. A column of the table to include in the analysis. When omitted, all columns are analyzed.

Examples

DROP DATABASE IF EXISTS analyze_table_demo;
CREATE DATABASE analyze_table_demo;
USE analyze_table_demo;

CREATE TABLE t_analyze (a INT, b VARCHAR(10));
INSERT INTO t_analyze VALUES (1, 'a'), (1, 'a'), (2, 'b'), (2, 'c');
ANALYZE TABLE t_analyze(a, b);
ANALYZE TABLE t_analyze;

DROP DATABASE analyze_table_demo;