MySQL optimizer: ANALYZE TABLE and Waiting for table flush

The MySQL optimizer makes the decision of what execution plan to use based on the information provided by the storage engines. That information is not accurate in some engines like InnoDB and they are based in statistics calculations therefore sometimes some tune is needed. In InnoDB these statistics are calculated automatically, check the following blog post for more information:

There are some variables to tune how that statistics are calculated but we need to wait until the gathering process triggers again to see if there is any improvement. Usually the first step to try to get back to the previous execution plan is to force that process with ANALYZE TABLE that is usually fast enough to not cause troubles.

Let’s see an example of how a simple and fast ANALYZE can cause a downtime.

Waiting for table flush:

In order to trigger this problem we need:

– Lot of concurrency
– A long running query
– Run an ANALYZE TABLE on a table accessed by the long running query

So first we need a long running query against table t:

Then in our efforts to get a better execution plan for another query we run ANALYZE TABLE:

Perfect, very fast! But then some seconds later we realize that our application is down. Let’s see the process list. I’ve removed most of the columns to make it clearer:

The ANALYZE TABLE runs perfect but after it the rest of the threads that are running a query against that table need to wait. This is because MySQL has detected that the underlying table has changed and it needs to close and reopen it using FLUSH. Therefore the table will be locked until all queries that are using that table finish. There are only two solutions to this situation, wait until the long query finishes or kill the query. Also, we have to take in account that killing a query could cause even more troubles. If we are dealing with a write query on InnoDB the rollback process could take even more time to finish than the original query. On the other hand, if the table is MyISAM there will be no rollback process so all the already updated rows can’t be recovered.

This particular example is not only a problem of ANALYZE. Other commands like FLUSH TABLES, ALTER, RENAME, OPTIMIZE or REPAIR can cause threads to wait on “Waiting for tables”, “Waiting for table” and “Waiting for table flush”.


Before running an ANALYZE table or any other command listed before, check the running queries. If the table that you are going to work on is very used the recommendation is to run it during the low peak of load or a maintenance window.

Share this post

Comments (6)

  • Vojtech Kurka

    Is this fixed in MySQL 5.6?

    February 27, 2013 at 11:44 am
  • ymhctt

    there is not metadata lock?

    February 28, 2013 at 4:47 am
  • Miguel Angel Nieto

    @Vojtech no, the same locks are used in MySQL 5.6

    @ymhctt I think they are different things. Metadata lock is used to avoid DDL while a transaction is active and modifying a table. For metadata lock you would see “Waiting for table metadata lock”.

    February 28, 2013 at 4:49 pm
  • mike

    Late to the party, but this type of locking is still apparent in MySQL 5.7 Enterprise as well. Application killer….

    January 13, 2018 at 9:01 am
  • David Collier-Brown

    Is there a way to quiesce the activity on a InnodDB table in the same sense that one queisces a service in a load-balancer, to drain it out before doing maintenance on it?

    November 17, 2018 at 9:17 am
  • saurabh bhartia May 8, 2019 at 6:51 pm

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.