Optimize MySQL: The Thread Cache

PostMay 12th, 2006 | Comments (6)
With the recent release of the new Babble site and the resulting increase in mysql activity (MySQL was averaging around 300 queries per second), mysql and server loads rose to a level that was starting to get uncomfortable. So I decided to flip through the MySQL status variables to see if anything looked abnormal. That's when I discovered that my "thread cache hit rate" was abysmal.

The hit rate should be as close to 100% as possible. You can calculate your hit ratio by dividing the 'threads_created' status variable by the 'connections' status variable:

100 - ((Threads_created / Connections) * 100)

My hit rate was less than 1%. Almost every mysql connection was causing a new thread to be created, and a lot of threads were being created, thus creating a lot of unnecessary overhead.

The cause of the problem was that 'thread_cache_size' was set to 0. Thread_cache_size determines how many threads MySQL will hold open in memory to handle new connections. So in my case, MySQL wasn't holding any in cache so it had to create new threads all the time. Not cool.

To determine what you should set 'thread_cache_size' to, pay close attention to the 'threads_created' status variable. If it keeps going up it means your 'thread_cache_size' is set too low. Just keep bumping up 'thread_cache_size' until 'threads_created' no longer increments. My optimal thread_cache_size turned out to be 50.

As soon as I optimized the thread cache, MySQL's server load dropped over 50%!

This MySQL optimization technique worked wonders for me, so I'm sharing it in hopes that it will help you.

BTW- If you don't have it yet, go download the MySQL Administrator. It makes viewing and changing MySQL variables much easier.

Comments

nyscmjhmjgAug 27, 2008
Hello my friend, your site is very good! <a href=http://becsymvbhy.com >http://becsymvbhy.com</a>
hao32Dec 15, 2008
thanks for your article!
JocuriJan 6, 2009
This is very useful thank you very much.
DistressJul 23, 2009
Thanks! Helped Greatly!
My barbieNov 19, 2009
Very useful post but the url for the Mysql administrator is outdated I think.Maybe you know the good link ?
RustedJan 4, 2010
Warning! thread_cache_size may lead to error and data loss. Before enabling this option in mysql check this bug http://bugs.mysql.com/bug.php?id=25255

Post a comment

Name
URL
Email
Comment