There are some options that can cause temp tables to materialize as
MyISAM tables or can be configured to delay it. Keep in mind that for
disk-based temp tables, there are no .frm
files, but only .MYD
and .MYI
files (of course. the .MYI file is never used since it is impossible index an internal temp table).
Here are the options:
You should also consider the MySQL Documentation on Internal Temp Table Usage
The situations where in-memory temp tables are made are
- 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.
- DISTINCT combined with ORDER BY may require a temporary table.
- If you use 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.
When an in-memory temp table exceeded the minimum of (tmp_table_size or max_heap_table_size), mysqld does the following:
- Suspends the query
- Copies the in-memory table‘s contents into a MyISAM temp table
- Discards the in-memory table
- Continues the query, sending the temp data into the MyISAM temp table
The situations where in-memory temp tables are bypassed in favor of disk are
- 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 is used
Some due diligence is required to reduce temp table creation on disk
- Setting join_buffer_size bigger
- Setting sort_buffer_size bigger
- Setting tmp_table_size and max_heap_table_size bigger
- Tuning queries to minimize or even prevent temp tables
- Creating indexes to create presorted view of data from individual tables
- Installing additional RAM to accommodate large in-memory temp tables
If after such due diligence, there are still temp tables being formed
on Disk, here is one desperate move: Mapping disk-based temp table
creation to memory.
Here is a quick-and-dirty way to set up a 16GB RAM Disk using tmpdir
STEP01) Create RAM Disk Folder
mkdir /var/mysql_tmpfs
STEP02) Add this to my.cnf
[mysqld]
tmpdir=/var/mysql_tmpfs
STEP03) Add this to /etc/fstab
echo "none /var/mysql_tmpfs tmpfs defaults,size=16g 1 2" >> /etc/fstab
STEP04) Reload /etc/fstab
mount -a
STEP05) service mysql restart
After this, all temp table that become MyISAM are written to the RAM Disk. This should speed disk-based temp table creation.
Give it a Try !!!
filesort
, and 6405 is the PID of mysql to keep temp files from different server instances separate. – Marc B Dec 15 ‘12 at 20:27