A quick one here, we often talk about effectively utilizing memory to get the most of your MySQL Server.  I wanted to remind folks to not forget about allocating memory to a tmpfs for the tmpdir.  Certainly this is not going to help everyone, but those who create lots of temporary table will find the performance boost most welcome. In fact in some cases you maybe better off allocating a little extra memory to a tmpfs then you would be the innodb buffer pool, but like everything it depends on your workload and environment.

What is tmpfs?  In a nutshell a filesystem ontop of a ramdisk  used for temporay file access.  Read the wiki page for more.   Sounds like a great thing for /tmp right? By the way It really urks me that most people leave /tmp as part of the root filesystem ….  shameful, read my common mistakes post for more complaining about that.  Anyways, back to the topic.

What brought this post on? I am working with a client who was having horrendous performance on several seemly simple queries.  The worst query, which will remain hidden to protect the innocent was consistently finishing around 13 second each run…. see here:

1028 rows in set (13.75 sec)
1028 rows in set (13.95 sec)
1028 rows in set (13.64 sec)

looking at the explain for the query, it was using a temporary table.  It is one of these queries that’s always going to build a temporary table on disk and then do a filesort.  You know the ones where you need to rethink the business case for that data because their is not great way to execute it. So without a way to prevent the temp table, and with no way force it into memory within the database I forced it into memory outside the database by pointing the tmpdir to a tmpfs.  This is a fairly dramatic difference ( and probably not typical ), but it really underscores that huge performance gains can be gained from small changes:

1028 rows in set (0.26 sec)
1028 rows in set (0.26 sec)
1028 rows in set (0.27 sec)

In summary, If your using lots of text fields, doing lots of sorting, group by’s, etc you may see a nice performance boost by pointing over your tmpdir to tmpfs.

You can create a new tmpfs by issuing the following:

mkdir -p /tmpmysql
mount -t tmpfs -o size=2048M,mode=0777 tmpfs /tmpmysql

then point tmpdir over to /tmpmysql

Don’t forget to add it to you fstab to make the change permanent.