If you’re like most web site owners, you’re probably familiar with the concept of caching. Caching is a technique that can help speed up your web site by storing copies of pages that have been requested recently. One popular way to cache pages is to use MySQL query caching. This article will show you how to set up MySQL query caching on your web site, and how it can help speed up your web site. To get started, first make sure that you have MySQL installed on your server. Then, follow these steps to set up MySQL query caching:
- Open the mysql client program on your server. You can find it in the Applications menu under Utilities or in the Start menu under Programs -> MySQL -> MySQL Client.
- In the mysql client program, type the following command: SET GLOBAL QUERY CACHE = ON; This command tells MySQL to start caching queries. You can also disable query caching by typing this command: SET GLOBAL QUERY CACHE = OFF; 3) Next, type the following command to create a table named cache in your database: CREATE TABLE cache ( id INTEGER NOT NULL AUTO_INCREMENT , page VARCHAR(255) NOT NULL , time TIMESTAMP NOT NULL ); 4) Now insert some test data into the cache table: INSERT INTO cache (page, time) VALUES (‘sample1’,‘2012-01-01 00:00:00’); INSERT INTO cache (page, time) VALUES (‘sample2’,‘2012-01-02 00:00:00’); 5) To check whether query caching is working correctly, type the following command into the mysql client program: SELECT * FROM cache; The output should show that both sample1 and sample2 have been cached and retrieved from memory without having to be queried from the database again. 6) If you want to disable ..
The reason this method is so powerful is that you don’t have to make any changes to your web application, you just have to sacrifice a little bit of memory. This isn’t going to fix all of your problems, but it definitely can’t hurt.
Note: if your application updates tables frequently, then the query cache will be constantly purged and you won’t get much or any benefit from this. This is ideal for an application that mostly does reads against the database, such as a WordPress blog. This also won’t work if you are running on shared hosting.
Enable Caching with Server Running
The first thing you’ll want to do is make sure that your installation of MySQL actually has query caching support available. Most distributions do, but you should check anyway.
You’ll want to run this command from your MySQL console, which will tell you if query caching is available.
Don’t mistake this as meaning that query caching is actually enabled, because most hosting providers aren’t going to enable this by default. Oddly enough, my Ubuntu Feisty installation already had it enabled…
Next we’ll need to check and see if query caching is enabled. We’ll need to check more than one variable, so we may as well do it all at once by checking for the variable query%
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.
Similarly, the other options can be set with the same syntax:
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.
You’ll notice in the stats that I have plenty of free memory left. If your server shows a lot of lowmem prunes, you might need to consider increasing this value, but I wouldn’t spend too much memory on query caching for a web server… you need to leave memory available for apache, php, ruby, or whatever you are using.
Enable in Config File
If you want these changes to survive a reboot or restart of the mysql server, you’ll need to add them into your /etc/mysql/my.cnf configuration file for MySQL. Note that it might be in a different location on your installation.
Open up the file using a text editor in sudo or root mode, and then add these values if they don’t already exist in the file. If they do exist, just uncomment them.
Query caching can significantly improve the speed of your web application, especially if your application does mostly reads. Monitor the status using the methods above and see how it works over time.