Tuning MySQL Server Settings

The default configuration file for MySQL is intended not to use many resources, because its a general purpose sort of a configuration file. The default configuration does enough to have MySQL running happily with limited resources and catering to simple queries and small data-sets. The configuration file would most definitely need to be customized and tuned if you intend on using complex queries and when you have good amount of data.

Most of the tunings mentioned in this post are applicable to the MyISAM storage engine, I will soon be posting tunings applicable to the Innodb storage engine.

Getting started

Before you start changing configuration settings, you should keep in mind the fact that “more is not always better”. You might have 16-20 GB of RAM, but that doesn’t mean that you should start setting values too high. Setting too high values, can degrade performance and might cause your server to run out of memory. So be very calculative about how much memory to assign.
Although there are many different configuration options, but I will only be touching the important ones, that will help you out the most in tuning the MySQL server.

Now let’s get our hands dirty.

Key buffer size

The the most important option if you are using lots of MyISAM tables is key_buffer. You should set it up to be 25% to 50% of available memory. You have to leave memory for OS cache as well because MyISAM uses OS cache to cache table data. Even if you are not using MyISAM tables, still you would be better of allocating 32M to 64M of memory to the key buffer, because internally MySQL uses MyISAM tables for chores such as creating temporary tables.

Thread cache

The thread cache holds threads that are not being used by any connection, but are available to be used. A thread is created for each connection to the MySQL server, and of course thread creation takes time and resources. The way MySQL works is, when a new connection is requested MySQL first looks into the thread cache to see if there is any ideal thread, if it finds one it uses that one for the connection, if it doesn’t find any it has to create the thread. Obviously MySQL response would be much faster if its just using threads from the cache and not creating them.
The number of threads in the cache is dictated by the thread_cache_size variable. This variable should have an adequate value, according to the connections requests per second that your server is receiving.
A good approach is to look at the Threads Created and make sure that the number of threads created per second does not exceed 1.
Each thread in a thread cache typically uses 128KB of memory, so its not really memory intensive and high numbers here won’t hurt you. But don’t keep it at more than 1000 because some OS don’t handle large number of threads well.

Table cache

Just like creating threads can be expensive, similarly opening tables is expensive too. Of course if the the table definition and data is already in the cache, then it can be queried much faster, as compared to opening the table file for each query. Keep an eye on the Opened Tables status variable and if you see the number large, increase the value of your table cache. Also keep in mind that every connection to the MySQL server needs to have its own copy of the table in the table cache, which means that if you have say 10 tables that are opened simultaneously by 5 connections, then each of the connection will have a separate entry for the same table in the table cache, hence in this case there will be 10×5 entries in the table cache. So generally a good value is calculated by multiplying the number of most frequently used tables by the average number of concurrent connections you have at peak times. For example, if you have 10 frequently accessed tables and 200 concurrent connections, table cache size should be 10×200. The configuration variable for table cache size is table_cache

Query cache size

Query cache is a very important functionality for read-intensive applications. Query cache stores complete result-sets of SELECT queries, enabling very fast retrievals on query cache-hits. The query cache is stored completely in memory, which of course is the reason for very fast retrievals from query cache. The configuration variable that governs the size of the query cache is query_cache_size. A value between 64M to 1024M is sufficient for most of the use cases.

Sort buffer size

Sort buffer size controls who big the buffer should be for filesorts. Sort buffer size is one of those configuration setting that will make the performance of MySQL server suffer, if set very high. And there are reasons for this. A sort buffer is all at once by the MySQL server although it many not be needed to be entirely filled (say if you have a sort buffer of 1MB and you only need 100KB for sorting, still MySQL will use the whole 1M and hence waste memory). Besides sort buffer is one of those settings that is used per connection, meaning if you have 100 connections open and each performing a sort query and the size of your sort buffer is 1MB, then the amount of memory being utilized for sorting would be 100 x 1MB = 100MB.
Hence, its best to keep this value small. The best method is to keep it small between 128KB to 1M and increase it only during the length of the connection if needed.

Read buffer size

Read buffer size is another of those variables whose value you should keep as small as possible. MySQL doesn’t allocate any memory from this buffer until a query needs it, but when it does it allocates the entire chunk of memory even if the entire chunk is needed or not. Hence large values here can really be a waste here.
Similar to the sort buffer size, the optimal way is to keep it small initially 128KB to 1MB, and then increase it only for the connection that needs more buffer memory.

Having gone through this article, I hope you will understand that allocating large amounts of memory is not always the right solution. You have to work on a case by case basis.
One more thing to keep in mind is that the best way to start changing the configuration is to change one or two values, and then run a benchmark test to see the effect of the change, this way you have a total grasp of how the configuration changes are effecting the performance of your server.

You might also want to read me recommending the Innodb storage engine here.

  • http://mysql.darmowe-blogi.pisz.pl/bez-kategorii/tuning-mysql-server-settings-ovais-tariq.html Tuning MySQL Server Settings | ovais.tariq | mysql

    [...] URL: Tuning MySQL Server Settings | ovais.tariq a-general-purpose, because-its, default-configuration, file-for, general-purpose, intended-not [...]

  • http://abcphp.com/story/36062/ abcphp.com

    Tuning MySQL Server Settings | ovais.tariq…

    The default configuration file for MySQL is intended not to use many resources, because its a general purpose sort of a configuration file. The default configuration does enough to have MySQL running happily with limited resources and catering to simpl…

  • http://topsy.com/www.ovaistariq.net/358/tuning-mysql-server-settings/?utm_source=pingback&utm_campaign=L2 Tweets that mention Tuning MySQL Server Settings | ovais.tariq — Topsy.com

    [...] This post was mentioned on Twitter by Rodrigo Sales and Mahesh Prasad, Ovais Tariq. Ovais Tariq said: Tuning MySQL Server Settings | ovais.tariq http://t.co/zzc6t9x via @ovaistariq #mysql #performance #configuration [...]

  • http://donmcarthur.com/ Don McArthur

    This doesn’t address InnoDB storage engine at all. Unless you have very specific and articuable reasons to use the MyISAM storage engine, you should be using InnoDB.

  • Desk Clock

    [...] Tuning MySQL Server Settings — ovais.tariq [...]

  • http://www.ovaistariq.net/ Ovais Tariq

    I very much agree with your point on using InnoDB and I will soon be posting on the topic of “Tuning Innodb Configuration”. However, this article is supposed to be a starter, and I have choosen MyISAM related settings because even now most of the web applications use MyISAM.

  • Must-Have

    [...] Tuning MySQL Server Settings — ovais.tariq [...]

  • Google Instant has changed

    [...] Tuning MySQL Server Settings — ovais.tariq [...]

  • http://www.ovaistariq.net/ Ovais Tariq

    Do check out my recommendations for using Innodb http://www.ovaistariq.net/460/why-do-i-recommend-switching-over-from-myisam-to-innodb/

    I would love to hear your comments!

  • http://www.ovaistariq.net/496/tuning-innodb-configuration/ Tuning InnoDB Configuration | ovais.tariq

    [...] had earlier written a post on tuning the MySQL server configuration which was more geared towards the MyISAM storage engine. While that is not because I didn’t [...]

  • james

    Thanks for post. Yes. more is NOT always better. Still some of your recommendation are way too big. Esp query cache. We run over 1 mil page views monthly using:

    key_buffer_size=8M
    table_cache=512
    thread_cache_size=16

    etc… ect…

  • http://www.ovaistariq.net/ Ovais Tariq

    Are you using MyISAM only tables or using a mix of MyISAM and InnoDB??

    Well a large key buffer size will definitely give u a performance boost., do try out a value larger than 8M in your benchmarks, if you can,.

  • http://www.3-ik.com.ar/blog/2011/06/15/mejore-la-performance-de-mysql-mediante-las-variables-del-sistema/ Mejore la performance de MySQL mediante las variables del sistema

    [...] Tuning de MySQL Server (Inglés) [...]

  • Hoockayrs

    You should have articulable reasons for choosing any storage engine. InnoDB is not the be-all and end-all. 

  • http://www.linuxquestions.org/questions/linux-server-73/lamp-server-perfornace-on-amazon-942075/#post4664526 LAMP server perfornace on Amazon

    [...] to related references) https://paragasu.wordpress.com/2008/…configuration/ (a tight fit…) http://www.ovaistariq.net/358/tuning…rver-settings/ A key thing to realize about high-performance server programs is that, left to their own devisings [...]

  • http://127.0.0.1/sitio/?p=63 Nothing found for Sitio ?p=63

    [...] Tuning MySQL Server Settings (81 views) [...]

blog comments powered by Disqus