In some cases, the server creates internal temporary tables
while processing queries. A temporary table can be held in
memory and processed by the MEMORY storage
engine, or stored on disk and processed by the
MyISAM storage engine. Temporary tables can
be created under conditions such as these:
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, a temporary table is created.
If you use the SQL_SMALL_RESULT option,
MySQL uses an in-memory temporary table.
DISTINCT combined with ORDER
BY may require a temporary table.
You can tell whether a query requires a temporary table by using
EXPLAIN and checking the
Extra column to see whether it says
Using temporary. See
Section 12.3.2, âEXPLAIN Syntaxâ.
Some conditions prevent the use of a MEMORY
temporary table, in which case the server uses a
MyISAM table instead:
Presence of a TEXT or
BLOB 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 is used.
A temporary table that is created initially as a
MEMORY table might be converted to a
MyISAM table and stored on disk if it becomes
too large. The max_heap_table_size system
variable determines how large MEMORY tables
are allowed to grow. It applies to all MEMORY
tables, including those created with CREATE
TABLE. However, for internal MEMORY
tables, the actual maximum size is determined by
max_heap_table_size in combination with
tmp_table_size: Whichever value is smaller is
the one that applies. If the size of an internal
MEMORY table exceeds the limit, MySQL
automatically converts it to an on-disk
MyISAM table.
Note, that temporary tables on your hard disk create lots of additional IO. So either move your temporary directory to a virtual ram-disk or take care that you are not using BLOB or TEXT fields inside your db architecture.
User Comments
MySQL also uses temporary tables when processing subqueries in the FROM clause (derived tables), some UNION queries, and some VIEW queries.
Note, that temporary tables on your hard disk create lots of additional IO. So either move your temporary directory to a virtual ram-disk or take care that you are not using BLOB or TEXT fields inside your db architecture.
Add your own comment.