Support / KnowledgeBase

 
Search the KnowledgeBase Search

Basic MySQL performance tuning (MySQLd)

  • Applies to: (gs), All (dv)

  • Difficulty: Easy

  • Time needed: 15 minutes

  • Tools needed: root, SSH, vi knowledge

 
  • Applies to: (gs)
    • Difficulty: Easy
    • Time Needed: 10
    • Tools Required: AccountCenter access, phpMyAdmin access
  • Applies to: All (dv)
    • Difficulty: Medium
    • Time Needed: 20
    • Tools Required: SSH, root, vi knowledge

Introduction

The following is just one simple way to get started with optimizing your MySQL databases.

Requirements

Be sure to back up your database before continuing with this article.

Instructions

  1. Log into your AccountCenter.
  2. Click on your primary domain.
  3. Click on Manage Databases.
  4. Click on Admin for the database you wish to optimize.
  5. This will open in a new window/tab of your browser. Log into phpMyAdmin with your MySQL username and password.
  6. Click on the database name on the left hand side frame (not the table, the database name).
  7. On the right hand column, your tables should be listed. Scroll down till you see the Check all link.
  8. Click on that link, make sure all database tables are checked and then from the dropdown next to it, carefully select Optimize table.

This should perform an analysis, repair, and an optimization of your database. Another fairly certain method of fixing your database/tables with phpMyAdmin is to export your data, delete your existing tables, and then re-import. PhpMyAdmin performs certain optimization tasks on the data that may be very beneficial for your databases. You can find even more MySQL Optimization tips at dev.mysql.com.

TIP:

If you haven't already purchased one already, you may want to consider getting a MySQL GridContainer add-on. The MySQL GridContainer gives your databases dedicated resources, most importantly RAM (memory). The container will also provide two very useful tools in finding ways to better optimize your database. These tools are the Query Analyzer and MySQL Report. For more details regarding the MySQL GridContainer, please visit our website.

Overview

This article will help you enable query caching in your my.cnf configuration file for MySQL. Although this article will actually increase the memory used by MySQL the performance gain can be dramatic depending on your queries and database usage.

This is Part 2 of a series of articles on how to tune your (dv) for better performance. Please see Optimizing your (dv) server for additional articles in this series.

Enable query caching

Enabling query caching can improve performance of frequently used MySQL queries.

  1. Create a backup of your MySQL config:
    cp /etc/my.cnf /etc/my.cnf.YYYY-MM-DD.bak 

    (Remember to replace YYYY-MM-DD with today's date, ie: 2011-03-30)

  2. Edit MySQL configuration:
    vi /etc/my.cnf

    Insert the following lines under the [MySQLd] section:

    query-cache-type = 1
    query-cache-size = 8M

    The ##M is the amount of RAM to use for query cache. You may want to increase / decrease this value depending on your needs. Rage or Extreme server may want to set this value to 16 MBs. Save the file.

  3. Restart MySQL
    /etc/init.d/mysqld stop
    /etc/init.d/mysqld start

    You can verify your changes via command line MySQL with the following method:

    mysql -uadmin -p`cat /etc/psa/.psa.shadow`

     

    You can check whether the query cache is present in your MySQL server using the following statements from the MySQL prompt type:

    SHOW VARIABLES LIKE 'have_query_cache';

     

    To check that caching is now enabled:

    SHOW VARIABLES LIKE 'query%';

     

    This will cache a lot of the frequently used queries, so the performance increase may not be seen immediately.

More tips

  • Defragment the query cache to better utilize its memory with the FLUSH QUERY CACHE statement. The statement does not remove any queries from the cache.
  • The RESET QUERY CACHE statement removes all query results from the query cache.
  • The FLUSH TABLES statement also does this.
  • To monitor query cache performance, use SHOW STATUS to view the cache status variables:
    SHOW STATUS LIKE 'Qcache%';

The output of this command will look similar to this:

 | Qcache_free_blocks             | 376       |
 | Qcache_free_memory             | 1724608   |
 | Qcache_hits                    | 65894     |
 | Qcache_inserts                 | 53777     |
 | Qcache_lowmem_prunes           | 9307      |
 | Qcache_not_cached              | 1348      |
 | Qcache_queries_in_cache        | 1676      |
 | Qcache_total_blocks            | 4168      | 

In the above example, you can see that Qcache_lowmem_prunes is a large number. This means that the query cache had to remove queries to make room for other queries. Based on this example, you would want to increase query-cache-size slightly to keep your queries inside cache. If your Qcache_lowmem_prunes is "0", then you may want to decrease query-cache-size to free up memory for other processes.


User Comments

No visitor comments posted. Post a comment
Fields marked with an asterisk(*) are required. Comment on this article
Fill out the form below if you would like to comment on this article.
 
 
 

(code is not case-sensitive)
 
Submit
 
 

Continue