Turn on MySQL query cache to speed up query performance?

As we know, speed is always the most important element in developing a website especially for those high traffic database driven website. You can try to turn on query cache to speed up query.

To speed up query, enable the MySQL query cache, before that you need to set few variables in mysql configuration file (usually is my.cnf or my.ini)


- 1st, set query_cache_type to 1. (There are 3 possible settings: 0 (disable / off), 1 (enable / on) and 2 (on demand).

query_cache_type = 1

- 2nd, set query_cache_size to your expected size. I’d prefer to set it at 20MB.

query_cache_size = 20M

If you set your query_cache_type = 2 ( on demand ), you would wan to modify your sql query to support cache.

SELECT SQL_CACHE field1, field2 FROM table1 WHERE field3 = ‘yes’

To check if your mysql server already enable query cache, simply run this query:-

SHOW VARIABLES LIKE ‘%query_cache%’;

You will see this result:-

+——————-+————————————————————————+
| Variable_name | Value |
+——————-+————————————————————————+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_size | 20971520 |
| query_cache_type | ON |
+——————-+————————————————————————+
4 rows in set (0.06 sec)

To check if your MySQL query cache is working, simply perform a sql query for 2 times and check the query cache variable like below:-

SHOW STATUS LIKE ‘%qcache%’;

+————————+———————————————————————————+
| Variable_name | Value |
+————————+———————————————————————————+
| Qcache_queries_in_cache | 1 |
| Qcache_inserts | 3 |
| Qcache_hits | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 2 |
| Qcache_free_memory | 20947592 |
| Qcache_free_blocks | 1 |
| Qcache_total_blocks | 4 |
+————————+———————————————————————————+

For the first time you execute your SQL query, the time it should take take be longer compare to the second time query. This is due to the MySQL query cache is working!

To know more about MySQL query cache click here

Comments

Popular posts from this blog

(VFP) - Running Visual FoxPro on Linux

(VFP) - How to put calendar on your vfp application

(VFP) - How To Run Exe File and include them to your Project