In some cases, MySQL creates internal temporary tables while processing queries.
On the base of the dimensions of the resultset MySQL will use the MEMORY engine AND/OR the MyISAM engine.
The difference is that MEMORY engine will handle the table in memory, while MyISAM will write it on disk.
A table created using the MEMORY engine can be automatically converted by the MySQL server if exceed the define threshold.
Then there are others circumstances which could create temporary tables using MEMORY but that can go to MyISAM (so disk) if too large:
- If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue;
- DISTINCT combined with ORDER BY may require a temporary table;
- In the case of the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.
There are some conditions which will force the temporary table to use MyISAM :
- Presence of a BLOB or TEXT column in the table;
- Presence of any column in a GROUP BY or DISTINCT clause larger than 512 bytes;
- Presence of any column larger than 512 bytes in the SELECT list, if UNION or UNION ALL;
The tables explicitly created with CREATE TABLE ENGINE MEMORY use ONLY the max_heap_table_size system variable to determines how large the table is permitted to grow and there is no conversion to on-disk format.
Why still doing a post on this issue?
Because i still see on MySQL configuration that DBA seems use it as separate assignments.Let us take this from the start, the manual.
In the manual section (http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_tmp_table_size) we can find the tmp_table_size definition.
What we need to take in account, and in mind, are these two element:
Now let us check for the max_heap_table_size
We can easily identify that both can be dynamically modified (Dynamic Variable Yes), that both the variable could be assign at global scope or per session (Variable Scope Both).
Finally that max_heap_size is by default 16M while tmp_table_size is system dependent.
Let us to try to clarify this digging in the code doing:
and we found this files.
From that my understanding is that both starts with a value of 16MB full-stop.
In set_var.cc the statement is:
we have no explicit reference to allocation.
The place were we have it is in mysqld.cc :
In the MySQL 5.5 and above, we have a clearer place and assignment:
But the result is the same, MySQL start from 16MB, no matter what system you have, or if it does, is very well hide from search.
Now let see what the manual say about how this two variables and their relation:
The maximum size of internal in-memory temporary tables. (The actual limit is determined as the minimum of tmp_table_size and max_heap_table_size.)
If an in-memory temporary table exceeds the limit, MySQL automatically converts it to an on-disk MyISAM table.
Increase the value of tmp_table_size (and max_heap_table_size if necessary) if you do many advanced GROUP BY queries and you have lots of memory.
This variable does not apply to user-created MEMORY tables.
Well this one seems clear enough for everyone but to avoid confusion, check the code:
but this time we open the file
Here is were we find the min() functions used from MySQL.
So as the manual state MySQL will take the LOWER value assign to the two variables, as the good value for tmp_table_size (only).
To be more clear, if we have in the my.cnf:
MySQL will assign to tmp_table_size the value of 16M and to max_heap_table_size 16M.
if we have:
MySQL will assign to tmp_table_size the value of 16M and to max_heap_table_size 32M.
This needs to take in account because I still see at client site a lot of :
Which makes no sense.
Finally, this variable has huge possible impact on memory consumption.
Just consider that having:
These settings could potentially generate a memory usage on the server of (600 x 16MB) = 9.3 GB.
Given that the variables are dynamic, and that could be assign at session level, it is good practics to increase the value of tmp_table_size only when is really needed, in the session before performing the operation which will require it.
Seems all to me but If I have miss something here, corrections are welcome.