Why is MySQL is creating so many temporary tables on disk?
mysqltuner rarely provides any useful information. It uses mostly irrelevant statistics about "hit rates" and puts arbitrary limits on what is an acceptable number of widgets are acceptable. If you are not facing a performance problem, then you don't actually need to solve any of the problems that it presents to you. That being said, here's a little background information about temporary tables...
MySQL internally uses the MEMORY storage engine for creating implicit temporary tables. On disk temporary tables use the MyISAM storage engine.
Temporary tables are created on disk when:
- TEXT or BLOB fields are present (because MEMORY doesn't support these types)
- the size of the resulting implicit temporary table exceeds the lesser of
tmp_table_size
ormax_heap_table_size
- If a column w/ more than 512 bytes is used with either a GROUP BY or UNION or ORDER BY
Read the MySQL Documentation on Internal Temporary Tables for more details.
What can you do about this? Presuming that it actually represents a performance problem (rather than just bothering you intelluctually):
- Avoid TEXT/BLOB fields and instead use appropriately sized VARCHAR or CHAR fields where possible.
- If TEXT/BLOB are unavoidable, sequester them to separate tables with a foreign key relationship and JOIN only when you need them.
- Treat large columns, more than 512 bytes as you would the above mentioned TEXT/BLOB fields.
- Make sure your queries are returning only the result set you need (appropriately selective WHERE clauses, avoid SELECT *)
- Avoid subqueries and replace them with joins, especially if they return a large result set
- Last resort - raise both
tmp_table_size
andmax_heap_table_size
. Don't do this unless you find that your queries cannot be optimized.
If you are concerned about your MySQL configuration and are not comfortable with the available settings yourself, you might want to check out the Percona Configuration Wizard as a starting point.
Will changing the db engine from "myisam" to "memory" on tables using "group by" fix this? as explained here
No, it won't and it will make it such that your tables are never persisted to disk. Don't do this.
"using temporary" and "using filesort" are not the end of the world!
SELECT ... GROUP BY a,b ORDER BY c,d -- Requires 1 or 2 "temp tables".
There are simply times when your queries will use temp tables. Temp tables may slow down a query by a small factor. But if the query is still "fast enough" then don't worry about it.
If the query is too slow (with or without tmp tables), let's discuss it. Please provide SHOW CREATE TABLE, SHOW TABLE STATUS, and EXPLAIN.