ANALYZE [NO_WRITE_TO_BINLOG | LOCAL] TABLE tbl_name [,tbl_name ...] [PERSISTENT FOR [ALL|COLUMNS ([col_name [,col_name ...]])] [INDEXES ([index_name [,index_name ...]])]]
ANALYZE TABLE analyzes and stores the key distribution for a
table (index statistics). During the analysis, the table is locked with a read lock. This statement works with MyISAM, Aria and InnoDB tables. For MyISAM tables, this statement is equivalent
to using myisamchk --analyze.
For more information on how the analysis works within InnoDB, see InnoDB Limitations.
MariaDB uses the stored key distribution to decide the order in which tables should be joined when you perform a join on something other than a constant. In addition, key distributions can be used when deciding which indexes to use for a specific table within a query.
This statement requires SELECT and INSERT privileges for the table.
By default, ANALYZE TABLE statements are written to the binary log and will be replicated. The
NO_WRITE_TO_BINLOG keyword (
LOCAL is an alias) will ensure the statement is not written to the binary log.
ANALYZE TABLE is also supported for partitioned tables. You
ALTER TABLE ... ANALYZE PARTITION to analyze one or
Engine-independent statistics are collected by doing full table and full index scans, and collecting can be quite expensive.
ANALYZE TABLE behavior depends on the value of the use_stat_tables system variable.
use_stat_tables is set to
never (default in <= MariaDB 10.4.0),
preferably_for_queries (default in >= MariaDB 10.4.1),
ANALYZE TABLE t will collect storage engine statistics, and not engine-independent statistics.
use_stat_tables is set to
ANALYZE TABLE t will collect engine-independent statistics.
In order to collect engine-independent statistics when
use_stat_tables is not set to one of the above two values (it is not by default), the extended form of ANALYZE TABLE, using
PERSISTENT FOR, must be used. This also allows one to collect statistics only for particular columns or indexes. Note that engine-independent statistics are used by default from MariaDB 10.4 (
use_stat_tables=preferably_for_queries), so you will probably want to use
It is recommended to collect engine-independent statistics on as-needed basis, so typically one will not have engine-independent statistics for all indexes/all columns.
Note that statistics for blob and text columns are not collected. If explicitly specified, a warning is returned.
-- update all engine-independent statistics for all columns and indexes ANALYZE TABLE tbl PERSISTENT FOR ALL; -- update specific columns and indexes: ANALYZE TABLE tbl PERSISTENT FOR COLUMNS (col1,col2,...) INDEXES (idx1,idx2,...); -- empty lists are allowed: ANALYZE TABLE tbl PERSISTENT FOR COLUMNS (col1,col2,...) INDEXES (); ANALYZE TABLE tbl PERSISTENT FOR COLUMNS () INDEXES (idx1,idx2,...); -- the following will only update mysql.table_stat fields: ANALYZE TABLE tbl PERSISTENT FOR COLUMNS () INDEXES (); -- with use_stat_tables = COMPLEMENTARY, a simple ANALYZE TABLE -- collects engine-independent statistics for all columns and indexes. SET SESSION use_stat_tables='COMPLEMENTARY'; ANALYZE TABLE tbl;