Take the 2-minute tour ×

I have a question for large mysql queries. Is it possible to skip the copying to tmp table on disk step that mysql takes for large queries or is it possible to make it go faster? because this step is taking way too long to get the results of my queries back. I read on the MySQL page that mysql performs this to save memory, but I don't care about saving memory I just want to get the results of my queries back FAST, I have enough memory on my machine. Also, my tables are properly indexed so that's not the reason why my queries are slow.

Any help?

Thank you

share|improve this question
I'm no MySql expert, but your probably better off optimizing the query and/or schema. –  mxmissile Sep 23 '11 at 17:04
I'm voting to close this as "not a real question." Question does not include the SQL query to optimize, nor the SHOW CREATE TABLE to show what indexes, nor the EXPLAIN report showing the current optimization plan. –  Bill Karwin Sep 23 '11 at 17:12
I would have to agree with @Bill Karwin in needing to see the queries, create table statements, and/or EXPLAIN plans. My answer would solve the problem you see but allow bad queries to bloat RAM. Knowing the nature of your queries would probably eliminate the need to reconfigure either RAM or mysql. –  RolandoMySQLDBA Sep 23 '11 at 18:48
@ close voters, please have a look at the awesome answer to this question and consider how much visitors this question might have brought to SO. Vote Leave open! –  fancyPants May 29 '13 at 8:09
add comment

1 Answer

up vote 44 down vote accepted

There are two things you can do to lessen the impact by this

OPTION #1 : Increase the variables tmp_table_size and/or max_heap_table_size

These options will govern how large an in-memory temp table can be before it is deemed too large and then pages to disk as a temporary MyISAM table. The larger these values are, the less likely you will get 'copying to tmp table on disk'. Please, make sure your server has enough RAM and max_connections is moderately configured should a single DB connection need a lot of RAM for its own temp tables.

OPTION #2 : Use a RAM disk for tmp tables

You should be able to configure a RAM disk in Linux and then set the tmpdir in mysql to be the folder that has the RAM disk mounted.

For starters, configure a RAM disk in the OS

Create a folder in the Linux called /var/tmpfs

mkdir /var/tmpfs

Next, add this line to /etc/fstab (for example, if you want a 16GB RAM disk)

none                    /var/tmpfs              tmpfs   defaults,size=16g        1 2

and reboot the server.

Note : It is possible to make a RAM disk without rebooting. Just remember to still add the aforementioned line to /etc/fstab to have the RAM disk after a server reboot.

Now for MySQL:

Add this line in /etc/my.cnf


and restart mysql.

OPTION #3 : Get tmp table into the RAM Disk ASAP (assuming you apply OPTION #2 first)

You may want to force tmp tables into the RAM disk as quickly as possible so that MySQL does not spin its wheels migrating large in-memory tmp tables into a RAM disk. Just add this to /etc/my.cnf:


and restart mysql. This will cause even the tiniest temp table to be brought into existence right in the RAM disk. You could periodically run ls -l /var/tmpfs to watch temp tables come and go.

Give it a Try !!!


If you see nothing but temp tables in /var/tmpfs 24/7, this could impact OS functionality/performance. To make sure /var/tmpfs does not get overpopulated, look into tuning your queries. Once you do, you should see less tmp tables materializing in /var/tmpfs.

share|improve this answer
A few other points if you are on linux (specifically ubuntu) You might need to add your tmpdir path to app armour in (/etc/apparmor.d/usr.sbin.mysqld) Also dont forget to chmod 777 the dir as well! –  Dan Oct 17 '12 at 11:49
add comment

Your Answer


