4

In a vanilla build of MySQL 5.5.20, when are InnoDB table index statistics updated? What events trigger such updates? I've seen comments suggesting that the following might trigger it:

  • table is opened for first time
  • query is run against table
  • ANALYZE TABLE
  • size of table changes by some threshold

1 Answer 1

5

You should look for this variable

mysql> show variables like '%metadata%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| innodb_stats_on_metadata | ON    |
+--------------------------+-------+
1 row in set (0.00 sec)

mysql>

According to the MySQL Docs, when innodb_stats_on_metadata is set (by default), InnoDB updates statistics during metadata statements such as SHOW TABLE STATUS or SHOW INDEX, or when accessing the INFORMATION_SCHEMA tables TABLES or STATISTICS. (These updates are similar to what happens for ANALYZE TABLE.) When disabled, InnoDB does not update statistics during these operations. Disabling this variable can improve access speed for schemas that have a large number of tables or indexes. It can also improve the stability of execution plans for queries that involve InnoDB tables.

Once disabled, you would have to run ANALYZE TABLE on the InnoDB tables of your choice. Make sure you have SELECT and INSERT privileges.

7
  • 1
    Thanks. If It turn it off, when will index statistics be updated?
    – Eric Rath
    Commented Mar 26, 2012 at 18:52
  • You have to do ANALYZE TABLE then Commented Mar 26, 2012 at 18:56
  • I found a Percona reference --mysqlperformanceblog.com/2011/10/06/… -- that suggests the statistics are updated when the table size changes by 1/16, but I can't find any corroboration for that statement. Do you know if that's true or not?
    – Eric Rath
    Commented Mar 26, 2012 at 19:04
  • 1
    I checked row0mysql.c for 5.5.20. It defines a function row_update_statistics_if_needed that behaves as described: it calls another method dict_update_statistics if the table-update counter is over 2 billion, or if the table-update counter is greater than 1/16 of the number of rows in the table. This row_update_statistics_if_needed method is called on insert, update, and cascade-update.
    – Eric Rath
    Commented Mar 26, 2012 at 19:53
  • 1
    @All: Also see changes in 5.6.2 blogs.innodb.com/wp/2011/04/…
    – gbn
    Commented Mar 27, 2012 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.