Search code examples
mysqlmysql-workbenchdatabase-performance

Explain plan MySQL on empty database


I have a copy of the schema of a database in MySQL 5.6 (+ Percona toolkit) because I have to evaluate the performance of the queries put on an app (and report them in order to take measures for optimizing queries/schema). This schema (for security reasons) is not filled with data and original tables might have between 100k-10 records.

I'm running Explain on the queries (using MySQL Workbench 6.3), but seems useless because It doesn't show all the analysis. Example: An Explain for a (known working) Select query joining three tables shows the message "Impossible where noticed after reading const tables".

So, the questions are:

  • It's useless running Explain on an empty database?
  • How can I get a reliable Explain for the queries I have to review?

Solution

  • It depends on the query, but yes, I often recommend that you need to fill the tables with some sample data to get accurate EXPLAIN reports.

    Even if you fill with 1 row so you avoid the "impossible where" note, that's not good enough because a table with very few rows is likely to be treated specially by the optimizer. That is, the optimizer knows that such a small table will fit on a single page of storage anyway, and the minimum unit the storage engine will retrieve is one page, so it may do a "table scan" instead of using an index, whereas it would be important to use an index for the same query if the table had a few thousand rows.

    You don't necessarily need to fill the table with real, sensitive data. It's common to write a script to generate meaningless data that is similar to your real data, enough to fill the tables so you can test.

    You don't necessarily need to fill 100k rows of data to get EXPLAIN to show you the same optimizer plan it would use for that much data. It's good enough if you can fill the tables with a few hundred or a few thousand rows of artificial data.