MySQL Optimization

There is one specific MySQL configuration parameter above all others that will produce significant performance improvements and takes very little understanding to implement.

“query_cache_size”

This configuration directive tells the MySQL server to store the result of the query in memory—much faster than accessing from disk.

In a hosting environment with 1 GB RAM, this allows for a query cache size of 64 MB or larger depending on other memory use factors, such as other services running on the server.

This allows 64 MB of queries to be stored in the very fast RAM, or memory of your hosting environment, as opposed to having to access the disk each time this query is executed or each time the page is loaded. This is an extremely large amount of data.

Invaldating a subset of a sufficiently large query cache can take a long time. During this time the server is efficiently frozen and no other operation can be executed. This patch addresses this problem by moving the locks which cause the freezing and also by temporarily disable the query cache while the invalidation takes place.

Extremely large query cache sizes should not be used. The larger the query cache size, the greater the impact of the data validation MySQL performs on the cache when a table has changed, and the resulting set of data in the cache is no longer valid. The bottom line is not to use a query cache size larger than what you need.

On a Magento installation with over 80,000 products, we have found 64 MB to provide 10x the performance improvement over disabled query cache. The actual use or capacity of the cache is less than 50 percent, which provides room for continued cache growth up to 64 MB.

The performance increase you should see from this single configuration enhancement will be in the order of 100-1000% depending on the query executed and the disk being accessed.

To check if your host has query caching enabled in the MySQL server, you can issue the following command from your MySQL command prompt:

mysql> SHOW VARIABLES LIKE 'have_query_cache';
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| have_query_cache | YES      |
+------------------+----------+

 

Here’s the important items in the list and what they mean:

query_cache_size – This is the size of the cache in bytes. Setting this value to 0 will effectively disable caching.
query_cache_type – This value must be ON or 1 for query caching to be enabled by default.
query_cache_limit – This is the maximum size query (in bytes) that will be cached.

If the query_cache_size value is set to 0 or you just want to change it, you’ll need to run the following command, keeping in mind that the value is in bytes. For instance, if you wanted to allocate 8MB to the cache we’d use 1024 * 1024 * 8 = 8388608 as the value.

SET GLOBAL query_cache_size = 8388608;

Similarly, the other options can be set with the same syntax:

SET GLOBAL query_cache_limit = 1048576;
SET GLOBAL query_cache_type = 1;

Now how do we tell if it’s actually working? You can use the SHOW STATUS command to pull all the variables that start with “Qc” to take a look at what is going on under the hood.

mysql> SHOW STATUS LIKE ‘Qc%’;
+————————-+——–+
| Variable_name           | Value  |
+————————-+——–+
| Qcache_free_blocks      | 65     |
| Qcache_free_memory      | 201440 |
| Qcache_hits             | 18868  |
| Qcache_inserts          | 2940   |
| Qcache_lowmem_prunes    | 665    |
| Qcache_not_cached       | 246    |
| Qcache_queries_in_cache | 492    |
| Qcache_total_blocks     | 1430   |
+————————-+——–+
8 rows in set (0.00 sec)

Learn more about MySQL query cache from = http://www.mysqlperformanceblog.com/2006/07/27/mysql-query-cache/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s